LLVM 23.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Constant.h"
44#include "llvm/IR/Constants.h"
47#include "llvm/IR/Dominators.h"
49#include "llvm/IR/Function.h"
51#include "llvm/IR/GlobalAlias.h"
52#include "llvm/IR/GlobalValue.h"
54#include "llvm/IR/InstrTypes.h"
55#include "llvm/IR/Instruction.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/IntrinsicsAArch64.h"
60#include "llvm/IR/IntrinsicsAMDGPU.h"
61#include "llvm/IR/IntrinsicsRISCV.h"
62#include "llvm/IR/IntrinsicsX86.h"
63#include "llvm/IR/LLVMContext.h"
64#include "llvm/IR/Metadata.h"
65#include "llvm/IR/Module.h"
66#include "llvm/IR/Operator.h"
68#include "llvm/IR/Type.h"
69#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
79#include <algorithm>
80#include <cassert>
81#include <cstdint>
82#include <optional>
83#include <utility>
84
85using namespace llvm;
86using namespace llvm::PatternMatch;
87
88// Controls the number of uses of the value searched for possible
89// dominating comparisons.
90static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
91 cl::Hidden, cl::init(20));
92
93/// Maximum number of instructions to check between assume and context
94/// instruction.
95static constexpr unsigned MaxInstrsToCheckForFree = 16;
96
97/// Returns the bitwidth of the given scalar or pointer type. For vector types,
98/// returns the element type's bitwidth.
99static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
100 if (unsigned BitWidth = Ty->getScalarSizeInBits())
101 return BitWidth;
102
103 return DL.getPointerTypeSizeInBits(Ty);
104}
105
106// Given the provided Value and, potentially, a context instruction, return
107// the preferred context instruction (if any).
108static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
109 // If we've been provided with a context instruction, then use that (provided
110 // it has been inserted).
111 if (CxtI && CxtI->getParent())
112 return CxtI;
113
114 // If the value is really an already-inserted instruction, then use that.
115 CxtI = dyn_cast<Instruction>(V);
116 if (CxtI && CxtI->getParent())
117 return CxtI;
118
119 return nullptr;
120}
121
123 const APInt &DemandedElts,
124 APInt &DemandedLHS, APInt &DemandedRHS) {
125 if (isa<ScalableVectorType>(Shuf->getType())) {
126 assert(DemandedElts == APInt(1,1));
127 DemandedLHS = DemandedRHS = DemandedElts;
128 return true;
129 }
130
131 int NumElts =
132 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
133 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
134 DemandedElts, DemandedLHS, DemandedRHS);
135}
136
137static void computeKnownBits(const Value *V, const APInt &DemandedElts,
138 KnownBits &Known, const SimplifyQuery &Q,
139 unsigned Depth);
140
142 const SimplifyQuery &Q, unsigned Depth) {
143 // Since the number of lanes in a scalable vector is unknown at compile time,
144 // we track one bit which is implicitly broadcast to all lanes. This means
145 // that all lanes in a scalable vector are considered demanded.
146 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
147 APInt DemandedElts =
148 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
149 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
150}
151
153 const DataLayout &DL, AssumptionCache *AC,
154 const Instruction *CxtI, const DominatorTree *DT,
155 bool UseInstrInfo, unsigned Depth) {
156 computeKnownBits(V, Known,
157 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
158 Depth);
159}
160
162 AssumptionCache *AC, const Instruction *CxtI,
163 const DominatorTree *DT, bool UseInstrInfo,
164 unsigned Depth) {
165 return computeKnownBits(
166 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
167}
168
169KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
170 const DataLayout &DL, AssumptionCache *AC,
171 const Instruction *CxtI,
172 const DominatorTree *DT, bool UseInstrInfo,
173 unsigned Depth) {
174 return computeKnownBits(
175 V, DemandedElts,
176 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
177}
178
180 const SimplifyQuery &SQ) {
181 // Look for an inverted mask: (X & ~M) op (Y & M).
182 {
183 Value *M;
184 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
186 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
187 return true;
188 }
189
190 // X op (Y & ~X)
193 return true;
194
195 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
196 // for constant Y.
197 Value *Y;
198 if (match(RHS,
200 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
201 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
202 return true;
203
204 // Peek through extends to find a 'not' of the other side:
205 // (ext Y) op ext(~Y)
206 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
208 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
209 return true;
210
211 // Look for: (A & B) op ~(A | B)
212 {
213 Value *A, *B;
214 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
216 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
217 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
218 return true;
219 }
220
221 // Look for: (X << V) op (Y >> (BitWidth - V))
222 // or (X >> V) op (Y << (BitWidth - V))
223 {
224 const Value *V;
225 const APInt *R;
226 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
227 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
228 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
229 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
230 R->uge(LHS->getType()->getScalarSizeInBits()))
231 return true;
232 }
233
234 return false;
235}
236
238 const WithCache<const Value *> &RHSCache,
239 const SimplifyQuery &SQ) {
240 const Value *LHS = LHSCache.getValue();
241 const Value *RHS = RHSCache.getValue();
242
243 assert(LHS->getType() == RHS->getType() &&
244 "LHS and RHS should have the same type");
245 assert(LHS->getType()->isIntOrIntVectorTy() &&
246 "LHS and RHS should be integers");
247
248 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
250 return true;
251
253 RHSCache.getKnownBits(SQ));
254}
255
257 return !I->user_empty() &&
258 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
259}
260
262 return !I->user_empty() && all_of(I->users(), [](const User *U) {
263 CmpPredicate P;
264 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
265 });
266}
267
269 bool OrZero, AssumptionCache *AC,
270 const Instruction *CxtI,
271 const DominatorTree *DT, bool UseInstrInfo,
272 unsigned Depth) {
273 return ::isKnownToBeAPowerOfTwo(
274 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
275 Depth);
276}
277
278static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
279 const SimplifyQuery &Q, unsigned Depth);
280
282 unsigned Depth) {
283 return computeKnownBits(V, SQ, Depth).isNonNegative();
284}
285
287 unsigned Depth) {
288 if (auto *CI = dyn_cast<ConstantInt>(V))
289 return CI->getValue().isStrictlyPositive();
290
291 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
292 // this updated.
293 KnownBits Known = computeKnownBits(V, SQ, Depth);
294 return Known.isNonNegative() &&
295 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
296}
297
299 unsigned Depth) {
300 return computeKnownBits(V, SQ, Depth).isNegative();
301}
302
303static bool isKnownNonEqual(const Value *V1, const Value *V2,
304 const APInt &DemandedElts, const SimplifyQuery &Q,
305 unsigned Depth);
306
307bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
308 const SimplifyQuery &Q, unsigned Depth) {
309 // We don't support looking through casts.
310 if (V1 == V2 || V1->getType() != V2->getType())
311 return false;
312 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
313 APInt DemandedElts =
314 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
315 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
316}
317
318bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
319 const SimplifyQuery &SQ, unsigned Depth) {
320 KnownBits Known(Mask.getBitWidth());
321 computeKnownBits(V, Known, SQ, Depth);
322 return Mask.isSubsetOf(Known.Zero);
323}
324
325static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
326 const SimplifyQuery &Q, unsigned Depth);
327
328static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
329 unsigned Depth = 0) {
330 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
331 APInt DemandedElts =
332 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
333 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
334}
335
336unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
337 AssumptionCache *AC, const Instruction *CxtI,
338 const DominatorTree *DT, bool UseInstrInfo,
339 unsigned Depth) {
340 return ::ComputeNumSignBits(
341 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
342}
343
345 AssumptionCache *AC,
346 const Instruction *CxtI,
347 const DominatorTree *DT,
348 unsigned Depth) {
349 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
350 return V->getType()->getScalarSizeInBits() - SignBits + 1;
351}
352
353/// Try to detect the lerp pattern: a * (b - c) + c * d
354/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
355///
356/// In that particular case, we can use the following chain of reasoning:
357///
358/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
359///
360/// Since that is true for arbitrary a, b, c and d within our constraints, we
361/// can conclude that:
362///
363/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
364///
365/// Considering that any result of the lerp would be less or equal to U, it
366/// would have at least the number of leading 0s as in U.
367///
368/// While being quite a specific situation, it is fairly common in computer
369/// graphics in the shape of alpha blending.
370///
371/// Modifies given KnownOut in-place with the inferred information.
372static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
373 const APInt &DemandedElts,
374 KnownBits &KnownOut,
375 const SimplifyQuery &Q,
376 unsigned Depth) {
377
378 Type *Ty = Op0->getType();
379 const unsigned BitWidth = Ty->getScalarSizeInBits();
380
381 // Only handle scalar types for now
382 if (Ty->isVectorTy())
383 return;
384
385 // Try to match: a * (b - c) + c * d.
386 // When a == 1 => A == nullptr, the same applies to d/D as well.
387 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
388 const Instruction *SubBC = nullptr;
389
390 const auto MatchSubBC = [&]() {
391 // (b - c) can have two forms that interest us:
392 //
393 // 1. sub nuw %b, %c
394 // 2. xor %c, %b
395 //
396 // For the first case, nuw flag guarantees our requirement b >= c.
397 //
398 // The second case might happen when the analysis can infer that b is a mask
399 // for c and we can transform sub operation into xor (that is usually true
400 // for constant b's). Even though xor is symmetrical, canonicalization
401 // ensures that the constant will be the RHS. We have additional checks
402 // later on to ensure that this xor operation is equivalent to subtraction.
404 m_Xor(m_Value(C), m_Value(B))));
405 };
406
407 const auto MatchASubBC = [&]() {
408 // Cases:
409 // - a * (b - c)
410 // - (b - c) * a
411 // - (b - c) <- a implicitly equals 1
412 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
413 };
414
415 const auto MatchCD = [&]() {
416 // Cases:
417 // - d * c
418 // - c * d
419 // - c <- d implicitly equals 1
421 };
422
423 const auto Match = [&](const Value *LHS, const Value *RHS) {
424 // We do use m_Specific(C) in MatchCD, so we have to make sure that
425 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
426 // has to evaluate first and return true.
427 //
428 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
429 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
430 };
431
432 if (!Match(Op0, Op1) && !Match(Op1, Op0))
433 return;
434
435 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
436 // For some of the values we use the convention of leaving
437 // it nullptr to signify an implicit constant 1.
438 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
440 };
441
442 // Check that all operands are non-negative
443 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
444 if (!KnownA.isNonNegative())
445 return;
446
447 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
448 if (!KnownD.isNonNegative())
449 return;
450
451 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
452 if (!KnownB.isNonNegative())
453 return;
454
455 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
456 if (!KnownC.isNonNegative())
457 return;
458
459 // If we matched subtraction as xor, we need to actually check that xor
460 // is semantically equivalent to subtraction.
461 //
462 // For that to be true, b has to be a mask for c or that b's known
463 // ones cover all known and possible ones of c.
464 if (SubBC->getOpcode() == Instruction::Xor &&
465 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
466 return;
467
468 const APInt MaxA = KnownA.getMaxValue();
469 const APInt MaxD = KnownD.getMaxValue();
470 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
471 const APInt MaxB = KnownB.getMaxValue();
472
473 // We can't infer leading zeros info if the upper-bound estimate wraps.
474 bool Overflow;
475 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
476
477 if (Overflow)
478 return;
479
480 // If we know that x <= y and both are positive than x has at least the same
481 // number of leading zeros as y.
482 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
483 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
484}
485
486static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
487 bool NSW, bool NUW,
488 const APInt &DemandedElts,
489 KnownBits &KnownOut, KnownBits &Known2,
490 const SimplifyQuery &Q, unsigned Depth) {
491 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
492
493 // If one operand is unknown and we have no nowrap information,
494 // the result will be unknown independently of the second operand.
495 if (KnownOut.isUnknown() && !NSW && !NUW)
496 return;
497
498 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
499 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
500
501 if (!Add && NSW && !KnownOut.isNonNegative() &&
503 .value_or(false))
504 KnownOut.makeNonNegative();
505
506 if (Add)
507 // Try to match lerp pattern and combine results
508 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);
509}
510
511static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
512 bool NUW, const APInt &DemandedElts,
513 KnownBits &Known, KnownBits &Known2,
514 const SimplifyQuery &Q, unsigned Depth) {
515 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
516 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
517
518 bool isKnownNegative = false;
519 bool isKnownNonNegative = false;
520 // If the multiplication is known not to overflow, compute the sign bit.
521 if (NSW) {
522 if (Op0 == Op1) {
523 // The product of a number with itself is non-negative.
524 isKnownNonNegative = true;
525 } else {
526 bool isKnownNonNegativeOp1 = Known.isNonNegative();
527 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
528 bool isKnownNegativeOp1 = Known.isNegative();
529 bool isKnownNegativeOp0 = Known2.isNegative();
530 // The product of two numbers with the same sign is non-negative.
531 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
532 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
533 if (!isKnownNonNegative && NUW) {
534 // mul nuw nsw with a factor > 1 is non-negative.
536 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
537 KnownBits::sgt(Known2, One).value_or(false);
538 }
539
540 // The product of a negative number and a non-negative number is either
541 // negative or zero.
544 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
545 Known2.isNonZero()) ||
546 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
547 }
548 }
549
550 bool SelfMultiply = Op0 == Op1;
551 if (SelfMultiply)
552 SelfMultiply &=
553 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
554 Known = KnownBits::mul(Known, Known2, SelfMultiply);
555
556 if (SelfMultiply) {
557 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
558 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
559 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
560
561 if (OutValidBits < TyBits) {
562 APInt KnownZeroMask =
563 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
564 Known.Zero |= KnownZeroMask;
565 }
566 }
567
568 // Only make use of no-wrap flags if we failed to compute the sign bit
569 // directly. This matters if the multiplication always overflows, in
570 // which case we prefer to follow the result of the direct computation,
571 // though as the program is invoking undefined behaviour we can choose
572 // whatever we like here.
573 if (isKnownNonNegative && !Known.isNegative())
574 Known.makeNonNegative();
575 else if (isKnownNegative && !Known.isNonNegative())
576 Known.makeNegative();
577}
578
580 KnownBits &Known) {
581 unsigned BitWidth = Known.getBitWidth();
582 unsigned NumRanges = Ranges.getNumOperands() / 2;
583 assert(NumRanges >= 1);
584
585 Known.setAllConflict();
586
587 for (unsigned i = 0; i < NumRanges; ++i) {
589 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
591 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
592 ConstantRange Range(Lower->getValue(), Upper->getValue());
593 // BitWidth must equal the Ranges BitWidth for the correct number of high
594 // bits to be set.
595 assert(BitWidth == Range.getBitWidth() &&
596 "Known bit width must match range bit width!");
597
598 // The first CommonPrefixBits of all values in Range are equal.
599 unsigned CommonPrefixBits =
600 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
601 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
602 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
603 Known.One &= UnsignedMax & Mask;
604 Known.Zero &= ~UnsignedMax & Mask;
605 }
606}
607
608static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
612
613 // The instruction defining an assumption's condition itself is always
614 // considered ephemeral to that assumption (even if it has other
615 // non-ephemeral users). See r246696's test case for an example.
616 if (is_contained(I->operands(), E))
617 return true;
618
619 while (!WorkSet.empty()) {
620 const Instruction *V = WorkSet.pop_back_val();
621 if (!Visited.insert(V).second)
622 continue;
623
624 // If all uses of this value are ephemeral, then so is this value.
625 if (all_of(V->users(), [&](const User *U) {
626 return EphValues.count(cast<Instruction>(U));
627 })) {
628 if (V == E)
629 return true;
630
631 if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {
632 EphValues.insert(V);
633
634 if (const User *U = dyn_cast<User>(V)) {
635 for (const Use &U : U->operands()) {
636 if (const auto *I = dyn_cast<Instruction>(U.get()))
637 WorkSet.push_back(I);
638 }
639 }
640 }
641 }
642 }
643
644 return false;
645}
646
647// Is this an intrinsic that cannot be speculated but also cannot trap?
649 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
650 return CI->isAssumeLikeIntrinsic();
651
652 return false;
653}
654
656 const Instruction *CxtI,
657 const DominatorTree *DT,
658 bool AllowEphemerals) {
659 // There are two restrictions on the use of an assume:
660 // 1. The assume must dominate the context (or the control flow must
661 // reach the assume whenever it reaches the context).
662 // 2. The context must not be in the assume's set of ephemeral values
663 // (otherwise we will use the assume to prove that the condition
664 // feeding the assume is trivially true, thus causing the removal of
665 // the assume).
666
667 if (Inv->getParent() == CxtI->getParent()) {
668 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
669 // in the BB.
670 if (Inv->comesBefore(CxtI))
671 return true;
672
673 // Don't let an assume affect itself - this would cause the problems
674 // `isEphemeralValueOf` is trying to prevent, and it would also make
675 // the loop below go out of bounds.
676 if (!AllowEphemerals && Inv == CxtI)
677 return false;
678
679 // The context comes first, but they're both in the same block.
680 // Make sure there is nothing in between that might interrupt
681 // the control flow, not even CxtI itself.
682 // We limit the scan distance between the assume and its context instruction
683 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
684 // it can be adjusted if needed (could be turned into a cl::opt).
685 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
687 return false;
688
689 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
690 }
691
692 // Inv and CxtI are in different blocks.
693 if (DT) {
694 if (DT->dominates(Inv, CxtI))
695 return true;
696 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
697 Inv->getParent()->isEntryBlock()) {
698 // We don't have a DT, but this trivially dominates.
699 return true;
700 }
701
702 return false;
703}
704
706 const Instruction *CtxI) {
707 // Helper to check if there are any calls in the range that may free memory.
708 auto hasNoFreeCalls = [](auto Range) {
709 for (const auto &[Idx, I] : enumerate(Range)) {
710 if (Idx > MaxInstrsToCheckForFree)
711 return false;
712 if (const auto *CB = dyn_cast<CallBase>(&I))
713 if (!CB->hasFnAttr(Attribute::NoFree))
714 return false;
715 }
716 return true;
717 };
718
719 // Make sure the current function cannot arrange for another thread to free on
720 // its behalf.
721 if (!CtxI->getFunction()->hasNoSync())
722 return false;
723
724 // Handle cross-block case: CtxI in a successor of Assume's block.
725 const BasicBlock *CtxBB = CtxI->getParent();
726 const BasicBlock *AssumeBB = Assume->getParent();
727 BasicBlock::const_iterator CtxIter = CtxI->getIterator();
728 if (CtxBB != AssumeBB) {
729 if (CtxBB->getSinglePredecessor() != AssumeBB)
730 return false;
731
732 if (!hasNoFreeCalls(make_range(CtxBB->begin(), CtxIter)))
733 return false;
734
735 CtxIter = AssumeBB->end();
736 } else {
737 // Same block case: check that Assume comes before CtxI.
738 if (!Assume->comesBefore(CtxI))
739 return false;
740 }
741
742 // Check if there are any calls between Assume and CtxIter that may free
743 // memory.
744 return hasNoFreeCalls(make_range(Assume->getIterator(), CtxIter));
745}
746
747// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
748// we still have enough information about `RHS` to conclude non-zero. For
749// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
750// so the extra compile time may not be worth it, but possibly a second API
751// should be created for use outside of loops.
752static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
753 // v u> y implies v != 0.
754 if (Pred == ICmpInst::ICMP_UGT)
755 return true;
756
757 // Special-case v != 0 to also handle v != null.
758 if (Pred == ICmpInst::ICMP_NE)
759 return match(RHS, m_Zero());
760
761 // All other predicates - rely on generic ConstantRange handling.
762 const APInt *C;
763 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
764 if (match(RHS, m_APInt(C))) {
766 return !TrueValues.contains(Zero);
767 }
768
770 if (VC == nullptr)
771 return false;
772
773 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
774 ++ElemIdx) {
776 Pred, VC->getElementAsAPInt(ElemIdx));
777 if (TrueValues.contains(Zero))
778 return false;
779 }
780 return true;
781}
782
783static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
784 Value *&ValOut, Instruction *&CtxIOut,
785 const PHINode **PhiOut = nullptr) {
786 ValOut = U->get();
787 if (ValOut == PHI)
788 return;
789 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
790 if (PhiOut)
791 *PhiOut = PHI;
792 Value *V;
793 // If the Use is a select of this phi, compute analysis on other arm to break
794 // recursion.
795 // TODO: Min/Max
796 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
797 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
798 ValOut = V;
799
800 // Same for select, if this phi is 2-operand phi, compute analysis on other
801 // incoming value to break recursion.
802 // TODO: We could handle any number of incoming edges as long as we only have
803 // two unique values.
804 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
805 IncPhi && IncPhi->getNumIncomingValues() == 2) {
806 for (int Idx = 0; Idx < 2; ++Idx) {
807 if (IncPhi->getIncomingValue(Idx) == PHI) {
808 ValOut = IncPhi->getIncomingValue(1 - Idx);
809 if (PhiOut)
810 *PhiOut = IncPhi;
811 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
812 break;
813 }
814 }
815 }
816}
817
818static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
819 // Use of assumptions is context-sensitive. If we don't have a context, we
820 // cannot use them!
821 if (!Q.AC || !Q.CxtI)
822 return false;
823
824 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
825 if (!Elem.Assume)
826 continue;
827
828 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
829 assert(I->getFunction() == Q.CxtI->getFunction() &&
830 "Got assumption for the wrong function!");
831
832 if (Elem.Index != AssumptionCache::ExprResultIdx) {
833 if (!V->getType()->isPointerTy())
834 continue;
836 *I, I->bundle_op_info_begin()[Elem.Index])) {
837 if (RK.WasOn == V &&
838 (RK.AttrKind == Attribute::NonNull ||
839 (RK.AttrKind == Attribute::Dereferenceable &&
841 V->getType()->getPointerAddressSpace()))) &&
843 return true;
844 }
845 continue;
846 }
847
848 // Warning: This loop can end up being somewhat performance sensitive.
849 // We're running this loop for once for each value queried resulting in a
850 // runtime of ~O(#assumes * #values).
851
852 Value *RHS;
853 CmpPredicate Pred;
854 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
855 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
856 continue;
857
858 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
859 return true;
860 }
861
862 return false;
863}
864
866 Value *LHS, Value *RHS, KnownBits &Known,
867 const SimplifyQuery &Q) {
868 if (RHS->getType()->isPointerTy()) {
869 // Handle comparison of pointer to null explicitly, as it will not be
870 // covered by the m_APInt() logic below.
871 if (LHS == V && match(RHS, m_Zero())) {
872 switch (Pred) {
874 Known.setAllZero();
875 break;
878 Known.makeNonNegative();
879 break;
881 Known.makeNegative();
882 break;
883 default:
884 break;
885 }
886 }
887 return;
888 }
889
890 unsigned BitWidth = Known.getBitWidth();
891 auto m_V =
893
894 Value *Y;
895 const APInt *Mask, *C;
896 if (!match(RHS, m_APInt(C)))
897 return;
898
899 uint64_t ShAmt;
900 switch (Pred) {
902 // assume(V = C)
903 if (match(LHS, m_V)) {
904 Known = Known.unionWith(KnownBits::makeConstant(*C));
905 // assume(V & Mask = C)
906 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
907 // For one bits in Mask, we can propagate bits from C to V.
908 Known.One |= *C;
909 if (match(Y, m_APInt(Mask)))
910 Known.Zero |= ~*C & *Mask;
911 // assume(V | Mask = C)
912 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
913 // For zero bits in Mask, we can propagate bits from C to V.
914 Known.Zero |= ~*C;
915 if (match(Y, m_APInt(Mask)))
916 Known.One |= *C & ~*Mask;
917 // assume(V << ShAmt = C)
918 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
919 ShAmt < BitWidth) {
920 // For those bits in C that are known, we can propagate them to known
921 // bits in V shifted to the right by ShAmt.
923 RHSKnown >>= ShAmt;
924 Known = Known.unionWith(RHSKnown);
925 // assume(V >> ShAmt = C)
926 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
927 ShAmt < BitWidth) {
928 // For those bits in RHS that are known, we can propagate them to known
929 // bits in V shifted to the right by C.
931 RHSKnown <<= ShAmt;
932 Known = Known.unionWith(RHSKnown);
933 }
934 break;
935 case ICmpInst::ICMP_NE: {
936 // assume (V & B != 0) where B is a power of 2
937 const APInt *BPow2;
938 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
939 Known.One |= *BPow2;
940 break;
941 }
942 default: {
943 const APInt *Offset = nullptr;
944 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
946 if (Offset)
947 LHSRange = LHSRange.sub(*Offset);
948 Known = Known.unionWith(LHSRange.toKnownBits());
949 }
950 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
951 // X & Y u> C -> X u> C && Y u> C
952 // X nuw- Y u> C -> X u> C
953 if (match(LHS, m_c_And(m_V, m_Value())) ||
954 match(LHS, m_NUWSub(m_V, m_Value())))
955 Known.One.setHighBits(
956 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
957 }
958 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
959 // X | Y u< C -> X u< C && Y u< C
960 // X nuw+ Y u< C -> X u< C && Y u< C
961 if (match(LHS, m_c_Or(m_V, m_Value())) ||
962 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
963 Known.Zero.setHighBits(
964 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
965 }
966 }
967 } break;
968 }
969}
970
971static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
972 KnownBits &Known,
973 const SimplifyQuery &SQ, bool Invert) {
975 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
976 Value *LHS = Cmp->getOperand(0);
977 Value *RHS = Cmp->getOperand(1);
978
979 // Handle icmp pred (trunc V), C
980 if (match(LHS, m_Trunc(m_Specific(V)))) {
981 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
982 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
984 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
985 else
986 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
987 return;
988 }
989
990 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
991}
992
994 KnownBits &Known, const SimplifyQuery &SQ,
995 bool Invert, unsigned Depth) {
996 Value *A, *B;
999 KnownBits Known2(Known.getBitWidth());
1000 KnownBits Known3(Known.getBitWidth());
1001 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
1002 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
1003 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
1005 Known2 = Known2.unionWith(Known3);
1006 else
1007 Known2 = Known2.intersectWith(Known3);
1008 Known = Known.unionWith(Known2);
1009 return;
1010 }
1011
1012 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
1013 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
1014 return;
1015 }
1016
1017 if (match(Cond, m_Trunc(m_Specific(V)))) {
1018 KnownBits DstKnown(1);
1019 if (Invert) {
1020 DstKnown.setAllZero();
1021 } else {
1022 DstKnown.setAllOnes();
1023 }
1025 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1026 return;
1027 }
1028 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1029 return;
1030 }
1031
1033 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1034}
1035
1037 const SimplifyQuery &Q, unsigned Depth) {
1038 // Handle injected condition.
1039 if (Q.CC && Q.CC->AffectedValues.contains(V))
1040 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
1041
1042 if (!Q.CxtI)
1043 return;
1044
1045 if (Q.DC && Q.DT) {
1046 // Handle dominating conditions.
1047 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
1048 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1049 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
1050 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1051 /*Invert*/ false, Depth);
1052
1053 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1054 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
1055 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1056 /*Invert*/ true, Depth);
1057 }
1058
1059 if (Known.hasConflict())
1060 Known.resetAll();
1061 }
1062
1063 if (!Q.AC)
1064 return;
1065
1066 unsigned BitWidth = Known.getBitWidth();
1067
1068 // Note that the patterns below need to be kept in sync with the code
1069 // in AssumptionCache::updateAffectedValues.
1070
1071 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1072 if (!Elem.Assume)
1073 continue;
1074
1075 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1076 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
1077 "Got assumption for the wrong function!");
1078
1079 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1080 if (!V->getType()->isPointerTy())
1081 continue;
1083 *I, I->bundle_op_info_begin()[Elem.Index])) {
1084 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
1085 // be the producer of the pointer in the bundle. At the moment, align
1086 // assumptions aren't optimized away.
1087 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
1088 isPowerOf2_64(RK.ArgValue) &&
1089 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
1090 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
1091 }
1092 continue;
1093 }
1094
1095 // Warning: This loop can end up being somewhat performance sensitive.
1096 // We're running this loop for once for each value queried resulting in a
1097 // runtime of ~O(#assumes * #values).
1098
1099 Value *Arg = I->getArgOperand(0);
1100
1101 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
1102 assert(BitWidth == 1 && "assume operand is not i1?");
1103 (void)BitWidth;
1104 Known.setAllOnes();
1105 return;
1106 }
1107 if (match(Arg, m_Not(m_Specific(V))) &&
1109 assert(BitWidth == 1 && "assume operand is not i1?");
1110 (void)BitWidth;
1111 Known.setAllZero();
1112 return;
1113 }
1114 auto *Trunc = dyn_cast<TruncInst>(Arg);
1115 if (Trunc && Trunc->getOperand(0) == V &&
1117 if (Trunc->hasNoUnsignedWrap()) {
1119 return;
1120 }
1121 Known.One.setBit(0);
1122 return;
1123 }
1124
1125 // The remaining tests are all recursive, so bail out if we hit the limit.
1127 continue;
1128
1129 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1130 if (!Cmp)
1131 continue;
1132
1133 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
1134 continue;
1135
1136 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1137 }
1138
1139 // Conflicting assumption: Undefined behavior will occur on this execution
1140 // path.
1141 if (Known.hasConflict())
1142 Known.resetAll();
1143}
1144
1145/// Compute known bits from a shift operator, including those with a
1146/// non-constant shift amount. Known is the output of this function. Known2 is a
1147/// pre-allocated temporary with the same bit width as Known and on return
1148/// contains the known bit of the shift value source. KF is an
1149/// operator-specific function that, given the known-bits and a shift amount,
1150/// compute the implied known-bits of the shift operator's result respectively
1151/// for that shift amount. The results from calling KF are conservatively
1152/// combined for all permitted shift amounts.
1154 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1155 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1156 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1157 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1158 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1159 // To limit compile-time impact, only query isKnownNonZero() if we know at
1160 // least something about the shift amount.
1161 bool ShAmtNonZero =
1162 Known.isNonZero() ||
1163 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1164 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1165 Known = KF(Known2, Known, ShAmtNonZero);
1166}
1167
1168static KnownBits
1169getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1170 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1171 const SimplifyQuery &Q, unsigned Depth) {
1172 unsigned BitWidth = KnownLHS.getBitWidth();
1173 KnownBits KnownOut(BitWidth);
1174 bool IsAnd = false;
1175 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1176 Value *X = nullptr, *Y = nullptr;
1177
1178 switch (I->getOpcode()) {
1179 case Instruction::And:
1180 KnownOut = KnownLHS & KnownRHS;
1181 IsAnd = true;
1182 // and(x, -x) is common idioms that will clear all but lowest set
1183 // bit. If we have a single known bit in x, we can clear all bits
1184 // above it.
1185 // TODO: instcombine often reassociates independent `and` which can hide
1186 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1187 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1188 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1189 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1190 KnownOut = KnownLHS.blsi();
1191 else
1192 KnownOut = KnownRHS.blsi();
1193 }
1194 break;
1195 case Instruction::Or:
1196 KnownOut = KnownLHS | KnownRHS;
1197 break;
1198 case Instruction::Xor:
1199 KnownOut = KnownLHS ^ KnownRHS;
1200 // xor(x, x-1) is common idioms that will clear all but lowest set
1201 // bit. If we have a single known bit in x, we can clear all bits
1202 // above it.
1203 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1204 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1205 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1206 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1207 if (HasKnownOne &&
1209 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1210 KnownOut = XBits.blsmsk();
1211 }
1212 break;
1213 default:
1214 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1215 }
1216
1217 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1218 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1219 // here we handle the more general case of adding any odd number by
1220 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1221 // TODO: This could be generalized to clearing any bit set in y where the
1222 // following bit is known to be unset in y.
1223 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1227 KnownBits KnownY(BitWidth);
1228 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1229 if (KnownY.countMinTrailingOnes() > 0) {
1230 if (IsAnd)
1231 KnownOut.Zero.setBit(0);
1232 else
1233 KnownOut.One.setBit(0);
1234 }
1235 }
1236 return KnownOut;
1237}
1238
1240 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1241 unsigned Depth,
1242 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1243 KnownBitsFunc) {
1244 APInt DemandedEltsLHS, DemandedEltsRHS;
1246 DemandedElts, DemandedEltsLHS,
1247 DemandedEltsRHS);
1248
1249 const auto ComputeForSingleOpFunc =
1250 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1251 return KnownBitsFunc(
1252 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1253 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1254 };
1255
1256 if (DemandedEltsRHS.isZero())
1257 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1258 if (DemandedEltsLHS.isZero())
1259 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1260
1261 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1262 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1263}
1264
1265// Public so this can be used in `SimplifyDemandedUseBits`.
1267 const KnownBits &KnownLHS,
1268 const KnownBits &KnownRHS,
1269 const SimplifyQuery &SQ,
1270 unsigned Depth) {
1271 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1272 APInt DemandedElts =
1273 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1274
1275 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1276 Depth);
1277}
1278
1280 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1281 // Without vscale_range, we only know that vscale is non-zero.
1282 if (!Attr.isValid())
1284
1285 unsigned AttrMin = Attr.getVScaleRangeMin();
1286 // Minimum is larger than vscale width, result is always poison.
1287 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1288 return ConstantRange::getEmpty(BitWidth);
1289
1290 APInt Min(BitWidth, AttrMin);
1291 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1292 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1294
1295 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1296}
1297
1299 Value *Arm, bool Invert,
1300 const SimplifyQuery &Q, unsigned Depth) {
1301 // If we have a constant arm, we are done.
1302 if (Known.isConstant())
1303 return;
1304
1305 // See what condition implies about the bits of the select arm.
1306 KnownBits CondRes(Known.getBitWidth());
1307 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1308 // If we don't get any information from the condition, no reason to
1309 // proceed.
1310 if (CondRes.isUnknown())
1311 return;
1312
1313 // We can have conflict if the condition is dead. I.e if we have
1314 // (x | 64) < 32 ? (x | 64) : y
1315 // we will have conflict at bit 6 from the condition/the `or`.
1316 // In that case just return. Its not particularly important
1317 // what we do, as this select is going to be simplified soon.
1318 CondRes = CondRes.unionWith(Known);
1319 if (CondRes.hasConflict())
1320 return;
1321
1322 // Finally make sure the information we found is valid. This is relatively
1323 // expensive so it's left for the very end.
1324 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1325 return;
1326
1327 // Finally, we know we get information from the condition and its valid,
1328 // so return it.
1329 Known = CondRes;
1330}
1331
1332// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1333// Returns the input and lower/upper bounds.
1334static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1335 const APInt *&CLow, const APInt *&CHigh) {
1337 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1338 "Input should be a Select!");
1339
1340 const Value *LHS = nullptr, *RHS = nullptr;
1342 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1343 return false;
1344
1345 if (!match(RHS, m_APInt(CLow)))
1346 return false;
1347
1348 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1350 if (getInverseMinMaxFlavor(SPF) != SPF2)
1351 return false;
1352
1353 if (!match(RHS2, m_APInt(CHigh)))
1354 return false;
1355
1356 if (SPF == SPF_SMIN)
1357 std::swap(CLow, CHigh);
1358
1359 In = LHS2;
1360 return CLow->sle(*CHigh);
1361}
1362
1364 const APInt *&CLow,
1365 const APInt *&CHigh) {
1366 assert((II->getIntrinsicID() == Intrinsic::smin ||
1367 II->getIntrinsicID() == Intrinsic::smax) &&
1368 "Must be smin/smax");
1369
1370 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1371 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1372 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1373 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1374 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1375 return false;
1376
1377 if (II->getIntrinsicID() == Intrinsic::smin)
1378 std::swap(CLow, CHigh);
1379 return CLow->sle(*CHigh);
1380}
1381
1383 KnownBits &Known) {
1384 const APInt *CLow, *CHigh;
1385 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1386 Known = Known.unionWith(
1387 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1388}
1389
1391 const APInt &DemandedElts,
1392 KnownBits &Known,
1393 const SimplifyQuery &Q,
1394 unsigned Depth) {
1395 unsigned BitWidth = Known.getBitWidth();
1396
1397 KnownBits Known2(BitWidth);
1398 switch (I->getOpcode()) {
1399 default: break;
1400 case Instruction::Load:
1401 if (MDNode *MD =
1402 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1404 break;
1405 case Instruction::And:
1406 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1407 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1408
1409 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1410 break;
1411 case Instruction::Or:
1412 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1413 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1414
1415 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1416 break;
1417 case Instruction::Xor:
1418 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1419 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1420
1421 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1422 break;
1423 case Instruction::Mul: {
1426 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1427 DemandedElts, Known, Known2, Q, Depth);
1428 break;
1429 }
1430 case Instruction::UDiv: {
1431 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1432 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1433 Known =
1434 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1435 break;
1436 }
1437 case Instruction::SDiv: {
1438 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1439 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1440 Known =
1441 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1442 break;
1443 }
1444 case Instruction::Select: {
1445 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1446 KnownBits Res(Known.getBitWidth());
1447 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1448 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1449 return Res;
1450 };
1451 // Only known if known in both the LHS and RHS.
1452 Known =
1453 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1454 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1455 break;
1456 }
1457 case Instruction::FPTrunc:
1458 case Instruction::FPExt:
1459 case Instruction::FPToUI:
1460 case Instruction::FPToSI:
1461 case Instruction::SIToFP:
1462 case Instruction::UIToFP:
1463 break; // Can't work with floating point.
1464 case Instruction::PtrToInt:
1465 case Instruction::PtrToAddr:
1466 case Instruction::IntToPtr:
1467 // Fall through and handle them the same as zext/trunc.
1468 [[fallthrough]];
1469 case Instruction::ZExt:
1470 case Instruction::Trunc: {
1471 Type *SrcTy = I->getOperand(0)->getType();
1472
1473 unsigned SrcBitWidth;
1474 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1475 // which fall through here.
1476 Type *ScalarTy = SrcTy->getScalarType();
1477 SrcBitWidth = ScalarTy->isPointerTy() ?
1478 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1479 Q.DL.getTypeSizeInBits(ScalarTy);
1480
1481 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1482 Known = Known.anyextOrTrunc(SrcBitWidth);
1483 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1484 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1485 Inst && Inst->hasNonNeg() && !Known.isNegative())
1486 Known.makeNonNegative();
1487 Known = Known.zextOrTrunc(BitWidth);
1488 break;
1489 }
1490 case Instruction::BitCast: {
1491 Type *SrcTy = I->getOperand(0)->getType();
1492 if (SrcTy->isIntOrPtrTy() &&
1493 // TODO: For now, not handling conversions like:
1494 // (bitcast i64 %x to <2 x i32>)
1495 !I->getType()->isVectorTy()) {
1496 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1497 break;
1498 }
1499
1500 const Value *V;
1501 // Handle bitcast from floating point to integer.
1502 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1503 V->getType()->isFPOrFPVectorTy()) {
1504 Type *FPType = V->getType()->getScalarType();
1505 KnownFPClass Result =
1506 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1507 FPClassTest FPClasses = Result.KnownFPClasses;
1508
1509 // TODO: Treat it as zero/poison if the use of I is unreachable.
1510 if (FPClasses == fcNone)
1511 break;
1512
1513 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1514 Known.setAllConflict();
1515
1516 if (FPClasses & fcInf)
1518 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1519
1520 if (FPClasses & fcZero)
1522 APInt::getZero(FPType->getScalarSizeInBits())));
1523
1524 Known.Zero.clearSignBit();
1525 Known.One.clearSignBit();
1526 }
1527
1528 if (Result.SignBit) {
1529 if (*Result.SignBit)
1530 Known.makeNegative();
1531 else
1532 Known.makeNonNegative();
1533 }
1534
1535 break;
1536 }
1537
1538 // Handle cast from vector integer type to scalar or vector integer.
1539 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1540 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1541 !I->getType()->isIntOrIntVectorTy() ||
1542 isa<ScalableVectorType>(I->getType()))
1543 break;
1544
1545 unsigned NumElts = DemandedElts.getBitWidth();
1546 bool IsLE = Q.DL.isLittleEndian();
1547 // Look through a cast from narrow vector elements to wider type.
1548 // Examples: v4i32 -> v2i64, v3i8 -> v24
1549 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1550 if (BitWidth % SubBitWidth == 0) {
1551 // Known bits are automatically intersected across demanded elements of a
1552 // vector. So for example, if a bit is computed as known zero, it must be
1553 // zero across all demanded elements of the vector.
1554 //
1555 // For this bitcast, each demanded element of the output is sub-divided
1556 // across a set of smaller vector elements in the source vector. To get
1557 // the known bits for an entire element of the output, compute the known
1558 // bits for each sub-element sequentially. This is done by shifting the
1559 // one-set-bit demanded elements parameter across the sub-elements for
1560 // consecutive calls to computeKnownBits. We are using the demanded
1561 // elements parameter as a mask operator.
1562 //
1563 // The known bits of each sub-element are then inserted into place
1564 // (dependent on endian) to form the full result of known bits.
1565 unsigned SubScale = BitWidth / SubBitWidth;
1566 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1567 for (unsigned i = 0; i != NumElts; ++i) {
1568 if (DemandedElts[i])
1569 SubDemandedElts.setBit(i * SubScale);
1570 }
1571
1572 KnownBits KnownSrc(SubBitWidth);
1573 for (unsigned i = 0; i != SubScale; ++i) {
1574 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1575 Depth + 1);
1576 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1577 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1578 }
1579 }
1580 // Look through a cast from wider vector elements to narrow type.
1581 // Examples: v2i64 -> v4i32
1582 if (SubBitWidth % BitWidth == 0) {
1583 unsigned SubScale = SubBitWidth / BitWidth;
1584 KnownBits KnownSrc(SubBitWidth);
1585 APInt SubDemandedElts =
1586 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1587 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1588 Depth + 1);
1589
1590 Known.setAllConflict();
1591 for (unsigned i = 0; i != NumElts; ++i) {
1592 if (DemandedElts[i]) {
1593 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1594 unsigned Offset = (Shifts % SubScale) * BitWidth;
1595 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1596 if (Known.isUnknown())
1597 break;
1598 }
1599 }
1600 }
1601 break;
1602 }
1603 case Instruction::SExt: {
1604 // Compute the bits in the result that are not present in the input.
1605 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1606
1607 Known = Known.trunc(SrcBitWidth);
1608 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1609 // If the sign bit of the input is known set or clear, then we know the
1610 // top bits of the result.
1611 Known = Known.sext(BitWidth);
1612 break;
1613 }
1614 case Instruction::Shl: {
1617 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1618 bool ShAmtNonZero) {
1619 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1620 };
1621 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1622 KF);
1623 // Trailing zeros of a right-shifted constant never decrease.
1624 const APInt *C;
1625 if (match(I->getOperand(0), m_APInt(C)))
1626 Known.Zero.setLowBits(C->countr_zero());
1627 break;
1628 }
1629 case Instruction::LShr: {
1630 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1631 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1632 bool ShAmtNonZero) {
1633 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1634 };
1635 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1636 KF);
1637 // Leading zeros of a left-shifted constant never decrease.
1638 const APInt *C;
1639 if (match(I->getOperand(0), m_APInt(C)))
1640 Known.Zero.setHighBits(C->countl_zero());
1641 break;
1642 }
1643 case Instruction::AShr: {
1644 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1645 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1646 bool ShAmtNonZero) {
1647 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1648 };
1649 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1650 KF);
1651 break;
1652 }
1653 case Instruction::Sub: {
1656 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1657 DemandedElts, Known, Known2, Q, Depth);
1658 break;
1659 }
1660 case Instruction::Add: {
1663 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1664 DemandedElts, Known, Known2, Q, Depth);
1665 break;
1666 }
1667 case Instruction::SRem:
1668 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1669 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1670 Known = KnownBits::srem(Known, Known2);
1671 break;
1672
1673 case Instruction::URem:
1674 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1675 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1676 Known = KnownBits::urem(Known, Known2);
1677 break;
1678 case Instruction::Alloca:
1680 break;
1681 case Instruction::GetElementPtr: {
1682 // Analyze all of the subscripts of this getelementptr instruction
1683 // to determine if we can prove known low zero bits.
1684 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1685 // Accumulate the constant indices in a separate variable
1686 // to minimize the number of calls to computeForAddSub.
1687 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1688 APInt AccConstIndices(IndexWidth, 0);
1689
1690 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1691 if (IndexWidth == BitWidth) {
1692 // Note that inbounds does *not* guarantee nsw for the addition, as only
1693 // the offset is signed, while the base address is unsigned.
1694 Known = KnownBits::add(Known, IndexBits);
1695 } else {
1696 // If the index width is smaller than the pointer width, only add the
1697 // value to the low bits.
1698 assert(IndexWidth < BitWidth &&
1699 "Index width can't be larger than pointer width");
1700 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1701 }
1702 };
1703
1705 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1706 // TrailZ can only become smaller, short-circuit if we hit zero.
1707 if (Known.isUnknown())
1708 break;
1709
1710 Value *Index = I->getOperand(i);
1711
1712 // Handle case when index is zero.
1713 Constant *CIndex = dyn_cast<Constant>(Index);
1714 if (CIndex && CIndex->isZeroValue())
1715 continue;
1716
1717 if (StructType *STy = GTI.getStructTypeOrNull()) {
1718 // Handle struct member offset arithmetic.
1719
1720 assert(CIndex &&
1721 "Access to structure field must be known at compile time");
1722
1723 if (CIndex->getType()->isVectorTy())
1724 Index = CIndex->getSplatValue();
1725
1726 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1727 const StructLayout *SL = Q.DL.getStructLayout(STy);
1728 uint64_t Offset = SL->getElementOffset(Idx);
1729 AccConstIndices += Offset;
1730 continue;
1731 }
1732
1733 // Handle array index arithmetic.
1734 Type *IndexedTy = GTI.getIndexedType();
1735 if (!IndexedTy->isSized()) {
1736 Known.resetAll();
1737 break;
1738 }
1739
1740 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1741 uint64_t StrideInBytes = Stride.getKnownMinValue();
1742 if (!Stride.isScalable()) {
1743 // Fast path for constant offset.
1744 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1745 AccConstIndices +=
1746 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1747 continue;
1748 }
1749 }
1750
1751 KnownBits IndexBits =
1752 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1753 KnownBits ScalingFactor(IndexWidth);
1754 // Multiply by current sizeof type.
1755 // &A[i] == A + i * sizeof(*A[i]).
1756 if (Stride.isScalable()) {
1757 // For scalable types the only thing we know about sizeof is
1758 // that this is a multiple of the minimum size.
1759 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1760 } else {
1761 ScalingFactor =
1762 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1763 }
1764 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1765 }
1766 if (!Known.isUnknown() && !AccConstIndices.isZero())
1767 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1768 break;
1769 }
1770 case Instruction::PHI: {
1771 const PHINode *P = cast<PHINode>(I);
1772 BinaryOperator *BO = nullptr;
1773 Value *R = nullptr, *L = nullptr;
1774 if (matchSimpleRecurrence(P, BO, R, L)) {
1775 // Handle the case of a simple two-predecessor recurrence PHI.
1776 // There's a lot more that could theoretically be done here, but
1777 // this is sufficient to catch some interesting cases.
1778 unsigned Opcode = BO->getOpcode();
1779
1780 switch (Opcode) {
1781 // If this is a shift recurrence, we know the bits being shifted in. We
1782 // can combine that with information about the start value of the
1783 // recurrence to conclude facts about the result. If this is a udiv
1784 // recurrence, we know that the result can never exceed either the
1785 // numerator or the start value, whichever is greater.
1786 case Instruction::LShr:
1787 case Instruction::AShr:
1788 case Instruction::Shl:
1789 case Instruction::UDiv:
1790 if (BO->getOperand(0) != I)
1791 break;
1792 [[fallthrough]];
1793
1794 // For a urem recurrence, the result can never exceed the start value. The
1795 // phi could either be the numerator or the denominator.
1796 case Instruction::URem: {
1797 // We have matched a recurrence of the form:
1798 // %iv = [R, %entry], [%iv.next, %backedge]
1799 // %iv.next = shift_op %iv, L
1800
1801 // Recurse with the phi context to avoid concern about whether facts
1802 // inferred hold at original context instruction. TODO: It may be
1803 // correct to use the original context. IF warranted, explore and
1804 // add sufficient tests to cover.
1806 RecQ.CxtI = P;
1807 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1808 switch (Opcode) {
1809 case Instruction::Shl:
1810 // A shl recurrence will only increase the tailing zeros
1811 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1812 break;
1813 case Instruction::LShr:
1814 case Instruction::UDiv:
1815 case Instruction::URem:
1816 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1817 // the start value.
1818 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1819 break;
1820 case Instruction::AShr:
1821 // An ashr recurrence will extend the initial sign bit
1822 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1823 Known.One.setHighBits(Known2.countMinLeadingOnes());
1824 break;
1825 }
1826 break;
1827 }
1828
1829 // Check for operations that have the property that if
1830 // both their operands have low zero bits, the result
1831 // will have low zero bits.
1832 case Instruction::Add:
1833 case Instruction::Sub:
1834 case Instruction::And:
1835 case Instruction::Or:
1836 case Instruction::Mul: {
1837 // Change the context instruction to the "edge" that flows into the
1838 // phi. This is important because that is where the value is actually
1839 // "evaluated" even though it is used later somewhere else. (see also
1840 // D69571).
1842
1843 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1844 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1845 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1846
1847 // Ok, we have a PHI of the form L op= R. Check for low
1848 // zero bits.
1849 RecQ.CxtI = RInst;
1850 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1851
1852 // We need to take the minimum number of known bits
1853 KnownBits Known3(BitWidth);
1854 RecQ.CxtI = LInst;
1855 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1856
1857 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1858 Known3.countMinTrailingZeros()));
1859
1860 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1861 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1862 break;
1863
1864 switch (Opcode) {
1865 // If initial value of recurrence is nonnegative, and we are adding
1866 // a nonnegative number with nsw, the result can only be nonnegative
1867 // or poison value regardless of the number of times we execute the
1868 // add in phi recurrence. If initial value is negative and we are
1869 // adding a negative number with nsw, the result can only be
1870 // negative or poison value. Similar arguments apply to sub and mul.
1871 //
1872 // (add non-negative, non-negative) --> non-negative
1873 // (add negative, negative) --> negative
1874 case Instruction::Add: {
1875 if (Known2.isNonNegative() && Known3.isNonNegative())
1876 Known.makeNonNegative();
1877 else if (Known2.isNegative() && Known3.isNegative())
1878 Known.makeNegative();
1879 break;
1880 }
1881
1882 // (sub nsw non-negative, negative) --> non-negative
1883 // (sub nsw negative, non-negative) --> negative
1884 case Instruction::Sub: {
1885 if (BO->getOperand(0) != I)
1886 break;
1887 if (Known2.isNonNegative() && Known3.isNegative())
1888 Known.makeNonNegative();
1889 else if (Known2.isNegative() && Known3.isNonNegative())
1890 Known.makeNegative();
1891 break;
1892 }
1893
1894 // (mul nsw non-negative, non-negative) --> non-negative
1895 case Instruction::Mul:
1896 if (Known2.isNonNegative() && Known3.isNonNegative())
1897 Known.makeNonNegative();
1898 break;
1899
1900 default:
1901 break;
1902 }
1903 break;
1904 }
1905
1906 default:
1907 break;
1908 }
1909 }
1910
1911 // Unreachable blocks may have zero-operand PHI nodes.
1912 if (P->getNumIncomingValues() == 0)
1913 break;
1914
1915 // Otherwise take the unions of the known bit sets of the operands,
1916 // taking conservative care to avoid excessive recursion.
1917 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1918 // Skip if every incoming value references to ourself.
1919 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1920 break;
1921
1922 Known.setAllConflict();
1923 for (const Use &U : P->operands()) {
1924 Value *IncValue;
1925 const PHINode *CxtPhi;
1926 Instruction *CxtI;
1927 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1928 // Skip direct self references.
1929 if (IncValue == P)
1930 continue;
1931
1932 // Change the context instruction to the "edge" that flows into the
1933 // phi. This is important because that is where the value is actually
1934 // "evaluated" even though it is used later somewhere else. (see also
1935 // D69571).
1937
1938 Known2 = KnownBits(BitWidth);
1939
1940 // Recurse, but cap the recursion to one level, because we don't
1941 // want to waste time spinning around in loops.
1942 // TODO: See if we can base recursion limiter on number of incoming phi
1943 // edges so we don't overly clamp analysis.
1944 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1946
1947 // See if we can further use a conditional branch into the phi
1948 // to help us determine the range of the value.
1949 if (!Known2.isConstant()) {
1950 CmpPredicate Pred;
1951 const APInt *RHSC;
1952 BasicBlock *TrueSucc, *FalseSucc;
1953 // TODO: Use RHS Value and compute range from its known bits.
1954 if (match(RecQ.CxtI,
1955 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1956 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1957 // Check for cases of duplicate successors.
1958 if ((TrueSucc == CxtPhi->getParent()) !=
1959 (FalseSucc == CxtPhi->getParent())) {
1960 // If we're using the false successor, invert the predicate.
1961 if (FalseSucc == CxtPhi->getParent())
1962 Pred = CmpInst::getInversePredicate(Pred);
1963 // Get the knownbits implied by the incoming phi condition.
1964 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1965 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1966 // We can have conflicts here if we are analyzing deadcode (its
1967 // impossible for us reach this BB based the icmp).
1968 if (KnownUnion.hasConflict()) {
1969 // No reason to continue analyzing in a known dead region, so
1970 // just resetAll and break. This will cause us to also exit the
1971 // outer loop.
1972 Known.resetAll();
1973 break;
1974 }
1975 Known2 = KnownUnion;
1976 }
1977 }
1978 }
1979
1980 Known = Known.intersectWith(Known2);
1981 // If all bits have been ruled out, there's no need to check
1982 // more operands.
1983 if (Known.isUnknown())
1984 break;
1985 }
1986 }
1987 break;
1988 }
1989 case Instruction::Call:
1990 case Instruction::Invoke: {
1991 // If range metadata is attached to this call, set known bits from that,
1992 // and then intersect with known bits based on other properties of the
1993 // function.
1994 if (MDNode *MD =
1995 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1997
1998 const auto *CB = cast<CallBase>(I);
1999
2000 if (std::optional<ConstantRange> Range = CB->getRange())
2001 Known = Known.unionWith(Range->toKnownBits());
2002
2003 if (const Value *RV = CB->getReturnedArgOperand()) {
2004 if (RV->getType() == I->getType()) {
2005 computeKnownBits(RV, Known2, Q, Depth + 1);
2006 Known = Known.unionWith(Known2);
2007 // If the function doesn't return properly for all input values
2008 // (e.g. unreachable exits) then there might be conflicts between the
2009 // argument value and the range metadata. Simply discard the known bits
2010 // in case of conflicts.
2011 if (Known.hasConflict())
2012 Known.resetAll();
2013 }
2014 }
2015 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
2016 switch (II->getIntrinsicID()) {
2017 default:
2018 break;
2019 case Intrinsic::abs: {
2020 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2021 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2022 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2023 break;
2024 }
2025 case Intrinsic::bitreverse:
2026 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2027 Known = Known.unionWith(Known2.reverseBits());
2028 break;
2029 case Intrinsic::bswap:
2030 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2031 Known = Known.unionWith(Known2.byteSwap());
2032 break;
2033 case Intrinsic::ctlz: {
2034 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2035 // If we have a known 1, its position is our upper bound.
2036 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2037 // If this call is poison for 0 input, the result will be less than 2^n.
2038 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2039 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2040 unsigned LowBits = llvm::bit_width(PossibleLZ);
2041 Known.Zero.setBitsFrom(LowBits);
2042 break;
2043 }
2044 case Intrinsic::cttz: {
2045 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2046 // If we have a known 1, its position is our upper bound.
2047 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2048 // If this call is poison for 0 input, the result will be less than 2^n.
2049 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2050 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2051 unsigned LowBits = llvm::bit_width(PossibleTZ);
2052 Known.Zero.setBitsFrom(LowBits);
2053 break;
2054 }
2055 case Intrinsic::ctpop: {
2056 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2057 // We can bound the space the count needs. Also, bits known to be zero
2058 // can't contribute to the population.
2059 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2060 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2061 Known.Zero.setBitsFrom(LowBits);
2062 // TODO: we could bound KnownOne using the lower bound on the number
2063 // of bits which might be set provided by popcnt KnownOne2.
2064 break;
2065 }
2066 case Intrinsic::fshr:
2067 case Intrinsic::fshl: {
2068 const APInt *SA;
2069 if (!match(I->getOperand(2), m_APInt(SA)))
2070 break;
2071
2072 // Normalize to funnel shift left.
2073 uint64_t ShiftAmt = SA->urem(BitWidth);
2074 if (II->getIntrinsicID() == Intrinsic::fshr)
2075 ShiftAmt = BitWidth - ShiftAmt;
2076
2077 KnownBits Known3(BitWidth);
2078 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2079 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2080
2081 Known2 <<= ShiftAmt;
2082 Known3 >>= BitWidth - ShiftAmt;
2083 Known = Known2.unionWith(Known3);
2084 break;
2085 }
2086 case Intrinsic::uadd_sat:
2087 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2088 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2089 Known = KnownBits::uadd_sat(Known, Known2);
2090 break;
2091 case Intrinsic::usub_sat:
2092 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2093 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2094 Known = KnownBits::usub_sat(Known, Known2);
2095 break;
2096 case Intrinsic::sadd_sat:
2097 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2098 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2099 Known = KnownBits::sadd_sat(Known, Known2);
2100 break;
2101 case Intrinsic::ssub_sat:
2102 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2103 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2104 Known = KnownBits::ssub_sat(Known, Known2);
2105 break;
2106 // Vec reverse preserves bits from input vec.
2107 case Intrinsic::vector_reverse:
2108 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2109 Depth + 1);
2110 break;
2111 // for min/max/and/or reduce, any bit common to each element in the
2112 // input vec is set in the output.
2113 case Intrinsic::vector_reduce_and:
2114 case Intrinsic::vector_reduce_or:
2115 case Intrinsic::vector_reduce_umax:
2116 case Intrinsic::vector_reduce_umin:
2117 case Intrinsic::vector_reduce_smax:
2118 case Intrinsic::vector_reduce_smin:
2119 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2120 break;
2121 case Intrinsic::vector_reduce_xor: {
2122 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2123 // The zeros common to all vecs are zero in the output.
2124 // If the number of elements is odd, then the common ones remain. If the
2125 // number of elements is even, then the common ones becomes zeros.
2126 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2127 // Even, so the ones become zeros.
2128 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2129 if (EvenCnt)
2130 Known.Zero |= Known.One;
2131 // Maybe even element count so need to clear ones.
2132 if (VecTy->isScalableTy() || EvenCnt)
2133 Known.One.clearAllBits();
2134 break;
2135 }
2136 case Intrinsic::vector_reduce_add: {
2137 auto *VecTy = dyn_cast<FixedVectorType>(I->getOperand(0)->getType());
2138 if (!VecTy)
2139 break;
2140 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2141 Known = Known.reduceAdd(VecTy->getNumElements());
2142 break;
2143 }
2144 case Intrinsic::umin:
2145 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2146 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2147 Known = KnownBits::umin(Known, Known2);
2148 break;
2149 case Intrinsic::umax:
2150 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2151 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2152 Known = KnownBits::umax(Known, Known2);
2153 break;
2154 case Intrinsic::smin:
2155 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2156 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2157 Known = KnownBits::smin(Known, Known2);
2159 break;
2160 case Intrinsic::smax:
2161 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2162 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2163 Known = KnownBits::smax(Known, Known2);
2165 break;
2166 case Intrinsic::ptrmask: {
2167 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2168
2169 const Value *Mask = I->getOperand(1);
2170 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2171 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2172 // TODO: 1-extend would be more precise.
2173 Known &= Known2.anyextOrTrunc(BitWidth);
2174 break;
2175 }
2176 case Intrinsic::x86_sse2_pmulh_w:
2177 case Intrinsic::x86_avx2_pmulh_w:
2178 case Intrinsic::x86_avx512_pmulh_w_512:
2179 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2180 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2181 Known = KnownBits::mulhs(Known, Known2);
2182 break;
2183 case Intrinsic::x86_sse2_pmulhu_w:
2184 case Intrinsic::x86_avx2_pmulhu_w:
2185 case Intrinsic::x86_avx512_pmulhu_w_512:
2186 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2187 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2188 Known = KnownBits::mulhu(Known, Known2);
2189 break;
2190 case Intrinsic::x86_sse42_crc32_64_64:
2191 Known.Zero.setBitsFrom(32);
2192 break;
2193 case Intrinsic::x86_ssse3_phadd_d_128:
2194 case Intrinsic::x86_ssse3_phadd_w_128:
2195 case Intrinsic::x86_avx2_phadd_d:
2196 case Intrinsic::x86_avx2_phadd_w: {
2198 I, DemandedElts, Q, Depth,
2199 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2200 return KnownBits::add(KnownLHS, KnownRHS);
2201 });
2202 break;
2203 }
2204 case Intrinsic::x86_ssse3_phadd_sw_128:
2205 case Intrinsic::x86_avx2_phadd_sw: {
2207 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2208 break;
2209 }
2210 case Intrinsic::x86_ssse3_phsub_d_128:
2211 case Intrinsic::x86_ssse3_phsub_w_128:
2212 case Intrinsic::x86_avx2_phsub_d:
2213 case Intrinsic::x86_avx2_phsub_w: {
2215 I, DemandedElts, Q, Depth,
2216 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2217 return KnownBits::sub(KnownLHS, KnownRHS);
2218 });
2219 break;
2220 }
2221 case Intrinsic::x86_ssse3_phsub_sw_128:
2222 case Intrinsic::x86_avx2_phsub_sw: {
2224 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2225 break;
2226 }
2227 case Intrinsic::riscv_vsetvli:
2228 case Intrinsic::riscv_vsetvlimax: {
2229 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2230 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2232 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2233 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2234 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2235 uint64_t MaxVLEN =
2236 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2237 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2238
2239 // Result of vsetvli must be not larger than AVL.
2240 if (HasAVL)
2241 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2242 MaxVL = std::min(MaxVL, CI->getZExtValue());
2243
2244 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2245 if (BitWidth > KnownZeroFirstBit)
2246 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2247 break;
2248 }
2249 case Intrinsic::vscale: {
2250 if (!II->getParent() || !II->getFunction())
2251 break;
2252
2253 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2254 break;
2255 }
2256 }
2257 }
2258 break;
2259 }
2260 case Instruction::ShuffleVector: {
2261 if (auto *Splat = getSplatValue(I)) {
2262 computeKnownBits(Splat, Known, Q, Depth + 1);
2263 break;
2264 }
2265
2266 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2267 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2268 if (!Shuf) {
2269 Known.resetAll();
2270 return;
2271 }
2272 // For undef elements, we don't know anything about the common state of
2273 // the shuffle result.
2274 APInt DemandedLHS, DemandedRHS;
2275 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2276 Known.resetAll();
2277 return;
2278 }
2279 Known.setAllConflict();
2280 if (!!DemandedLHS) {
2281 const Value *LHS = Shuf->getOperand(0);
2282 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2283 // If we don't know any bits, early out.
2284 if (Known.isUnknown())
2285 break;
2286 }
2287 if (!!DemandedRHS) {
2288 const Value *RHS = Shuf->getOperand(1);
2289 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2290 Known = Known.intersectWith(Known2);
2291 }
2292 break;
2293 }
2294 case Instruction::InsertElement: {
2295 if (isa<ScalableVectorType>(I->getType())) {
2296 Known.resetAll();
2297 return;
2298 }
2299 const Value *Vec = I->getOperand(0);
2300 const Value *Elt = I->getOperand(1);
2301 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2302 unsigned NumElts = DemandedElts.getBitWidth();
2303 APInt DemandedVecElts = DemandedElts;
2304 bool NeedsElt = true;
2305 // If we know the index we are inserting too, clear it from Vec check.
2306 if (CIdx && CIdx->getValue().ult(NumElts)) {
2307 DemandedVecElts.clearBit(CIdx->getZExtValue());
2308 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2309 }
2310
2311 Known.setAllConflict();
2312 if (NeedsElt) {
2313 computeKnownBits(Elt, Known, Q, Depth + 1);
2314 // If we don't know any bits, early out.
2315 if (Known.isUnknown())
2316 break;
2317 }
2318
2319 if (!DemandedVecElts.isZero()) {
2320 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2321 Known = Known.intersectWith(Known2);
2322 }
2323 break;
2324 }
2325 case Instruction::ExtractElement: {
2326 // Look through extract element. If the index is non-constant or
2327 // out-of-range demand all elements, otherwise just the extracted element.
2328 const Value *Vec = I->getOperand(0);
2329 const Value *Idx = I->getOperand(1);
2330 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2331 if (isa<ScalableVectorType>(Vec->getType())) {
2332 // FIXME: there's probably *something* we can do with scalable vectors
2333 Known.resetAll();
2334 break;
2335 }
2336 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2337 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2338 if (CIdx && CIdx->getValue().ult(NumElts))
2339 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2340 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2341 break;
2342 }
2343 case Instruction::ExtractValue:
2344 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2346 if (EVI->getNumIndices() != 1) break;
2347 if (EVI->getIndices()[0] == 0) {
2348 switch (II->getIntrinsicID()) {
2349 default: break;
2350 case Intrinsic::uadd_with_overflow:
2351 case Intrinsic::sadd_with_overflow:
2353 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2354 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2355 break;
2356 case Intrinsic::usub_with_overflow:
2357 case Intrinsic::ssub_with_overflow:
2359 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2360 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2361 break;
2362 case Intrinsic::umul_with_overflow:
2363 case Intrinsic::smul_with_overflow:
2364 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2365 false, DemandedElts, Known, Known2, Q, Depth);
2366 break;
2367 }
2368 }
2369 }
2370 break;
2371 case Instruction::Freeze:
2372 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2373 Depth + 1))
2374 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2375 break;
2376 }
2377}
2378
2379/// Determine which bits of V are known to be either zero or one and return
2380/// them.
2381KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2382 const SimplifyQuery &Q, unsigned Depth) {
2383 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2384 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2385 return Known;
2386}
2387
2388/// Determine which bits of V are known to be either zero or one and return
2389/// them.
2391 unsigned Depth) {
2392 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2393 computeKnownBits(V, Known, Q, Depth);
2394 return Known;
2395}
2396
2397/// Determine which bits of V are known to be either zero or one and return
2398/// them in the Known bit set.
2399///
2400/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2401/// we cannot optimize based on the assumption that it is zero without changing
2402/// it to be an explicit zero. If we don't change it to zero, other code could
2403/// optimized based on the contradictory assumption that it is non-zero.
2404/// Because instcombine aggressively folds operations with undef args anyway,
2405/// this won't lose us code quality.
2406///
2407/// This function is defined on values with integer type, values with pointer
2408/// type, and vectors of integers. In the case
2409/// where V is a vector, known zero, and known one values are the
2410/// same width as the vector element, and the bit is set only if it is true
2411/// for all of the demanded elements in the vector specified by DemandedElts.
2412void computeKnownBits(const Value *V, const APInt &DemandedElts,
2413 KnownBits &Known, const SimplifyQuery &Q,
2414 unsigned Depth) {
2415 if (!DemandedElts) {
2416 // No demanded elts, better to assume we don't know anything.
2417 Known.resetAll();
2418 return;
2419 }
2420
2421 assert(V && "No Value?");
2422 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2423
2424#ifndef NDEBUG
2425 Type *Ty = V->getType();
2426 unsigned BitWidth = Known.getBitWidth();
2427
2428 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2429 "Not integer or pointer type!");
2430
2431 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2432 assert(
2433 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2434 "DemandedElt width should equal the fixed vector number of elements");
2435 } else {
2436 assert(DemandedElts == APInt(1, 1) &&
2437 "DemandedElt width should be 1 for scalars or scalable vectors");
2438 }
2439
2440 Type *ScalarTy = Ty->getScalarType();
2441 if (ScalarTy->isPointerTy()) {
2442 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2443 "V and Known should have same BitWidth");
2444 } else {
2445 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2446 "V and Known should have same BitWidth");
2447 }
2448#endif
2449
2450 const APInt *C;
2451 if (match(V, m_APInt(C))) {
2452 // We know all of the bits for a scalar constant or a splat vector constant!
2453 Known = KnownBits::makeConstant(*C);
2454 return;
2455 }
2456 // Null and aggregate-zero are all-zeros.
2458 Known.setAllZero();
2459 return;
2460 }
2461 // Handle a constant vector by taking the intersection of the known bits of
2462 // each element.
2464 assert(!isa<ScalableVectorType>(V->getType()));
2465 // We know that CDV must be a vector of integers. Take the intersection of
2466 // each element.
2467 Known.setAllConflict();
2468 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2469 if (!DemandedElts[i])
2470 continue;
2471 APInt Elt = CDV->getElementAsAPInt(i);
2472 Known.Zero &= ~Elt;
2473 Known.One &= Elt;
2474 }
2475 if (Known.hasConflict())
2476 Known.resetAll();
2477 return;
2478 }
2479
2480 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2481 assert(!isa<ScalableVectorType>(V->getType()));
2482 // We know that CV must be a vector of integers. Take the intersection of
2483 // each element.
2484 Known.setAllConflict();
2485 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2486 if (!DemandedElts[i])
2487 continue;
2488 Constant *Element = CV->getAggregateElement(i);
2489 if (isa<PoisonValue>(Element))
2490 continue;
2491 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2492 if (!ElementCI) {
2493 Known.resetAll();
2494 return;
2495 }
2496 const APInt &Elt = ElementCI->getValue();
2497 Known.Zero &= ~Elt;
2498 Known.One &= Elt;
2499 }
2500 if (Known.hasConflict())
2501 Known.resetAll();
2502 return;
2503 }
2504
2505 // Start out not knowing anything.
2506 Known.resetAll();
2507
2508 // We can't imply anything about undefs.
2509 if (isa<UndefValue>(V))
2510 return;
2511
2512 // There's no point in looking through other users of ConstantData for
2513 // assumptions. Confirm that we've handled them all.
2514 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2515
2516 if (const auto *A = dyn_cast<Argument>(V))
2517 if (std::optional<ConstantRange> Range = A->getRange())
2518 Known = Range->toKnownBits();
2519
2520 // All recursive calls that increase depth must come after this.
2522 return;
2523
2524 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2525 // the bits of its aliasee.
2526 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2527 if (!GA->isInterposable())
2528 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2529 return;
2530 }
2531
2532 if (const Operator *I = dyn_cast<Operator>(V))
2533 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2534 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2535 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2536 Known = CR->toKnownBits();
2537 }
2538
2539 // Aligned pointers have trailing zeros - refine Known.Zero set
2540 if (isa<PointerType>(V->getType())) {
2541 Align Alignment = V->getPointerAlignment(Q.DL);
2542 Known.Zero.setLowBits(Log2(Alignment));
2543 }
2544
2545 // computeKnownBitsFromContext strictly refines Known.
2546 // Therefore, we run them after computeKnownBitsFromOperator.
2547
2548 // Check whether we can determine known bits from context such as assumes.
2549 computeKnownBitsFromContext(V, Known, Q, Depth);
2550}
2551
2552/// Try to detect a recurrence that the value of the induction variable is
2553/// always a power of two (or zero).
2554static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2555 SimplifyQuery &Q, unsigned Depth) {
2556 BinaryOperator *BO = nullptr;
2557 Value *Start = nullptr, *Step = nullptr;
2558 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2559 return false;
2560
2561 // Initial value must be a power of two.
2562 for (const Use &U : PN->operands()) {
2563 if (U.get() == Start) {
2564 // Initial value comes from a different BB, need to adjust context
2565 // instruction for analysis.
2566 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2567 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2568 return false;
2569 }
2570 }
2571
2572 // Except for Mul, the induction variable must be on the left side of the
2573 // increment expression, otherwise its value can be arbitrary.
2574 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2575 return false;
2576
2577 Q.CxtI = BO->getParent()->getTerminator();
2578 switch (BO->getOpcode()) {
2579 case Instruction::Mul:
2580 // Power of two is closed under multiplication.
2581 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2582 Q.IIQ.hasNoSignedWrap(BO)) &&
2583 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2584 case Instruction::SDiv:
2585 // Start value must not be signmask for signed division, so simply being a
2586 // power of two is not sufficient, and it has to be a constant.
2587 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2588 return false;
2589 [[fallthrough]];
2590 case Instruction::UDiv:
2591 // Divisor must be a power of two.
2592 // If OrZero is false, cannot guarantee induction variable is non-zero after
2593 // division, same for Shr, unless it is exact division.
2594 return (OrZero || Q.IIQ.isExact(BO)) &&
2595 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2596 case Instruction::Shl:
2597 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2598 case Instruction::AShr:
2599 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2600 return false;
2601 [[fallthrough]];
2602 case Instruction::LShr:
2603 return OrZero || Q.IIQ.isExact(BO);
2604 default:
2605 return false;
2606 }
2607}
2608
2609/// Return true if we can infer that \p V is known to be a power of 2 from
2610/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2611static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2612 const Value *Cond,
2613 bool CondIsTrue) {
2614 CmpPredicate Pred;
2615 const APInt *RHSC;
2617 m_APInt(RHSC))))
2618 return false;
2619 if (!CondIsTrue)
2620 Pred = ICmpInst::getInversePredicate(Pred);
2621 // ctpop(V) u< 2
2622 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2623 return true;
2624 // ctpop(V) == 1
2625 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2626}
2627
2628/// Return true if the given value is known to have exactly one
2629/// bit set when defined. For vectors return true if every element is known to
2630/// be a power of two when defined. Supports values with integer or pointer
2631/// types and vectors of integers.
2632bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2633 const SimplifyQuery &Q, unsigned Depth) {
2634 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2635
2636 if (isa<Constant>(V))
2637 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2638
2639 // i1 is by definition a power of 2 or zero.
2640 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2641 return true;
2642
2643 // Try to infer from assumptions.
2644 if (Q.AC && Q.CxtI) {
2645 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2646 if (!AssumeVH)
2647 continue;
2648 CallInst *I = cast<CallInst>(AssumeVH);
2649 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2650 /*CondIsTrue=*/true) &&
2652 return true;
2653 }
2654 }
2655
2656 // Handle dominating conditions.
2657 if (Q.DC && Q.CxtI && Q.DT) {
2658 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2659 Value *Cond = BI->getCondition();
2660
2661 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2663 /*CondIsTrue=*/true) &&
2664 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2665 return true;
2666
2667 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2669 /*CondIsTrue=*/false) &&
2670 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2671 return true;
2672 }
2673 }
2674
2675 auto *I = dyn_cast<Instruction>(V);
2676 if (!I)
2677 return false;
2678
2679 if (Q.CxtI && match(V, m_VScale())) {
2680 const Function *F = Q.CxtI->getFunction();
2681 // The vscale_range indicates vscale is a power-of-two.
2682 return F->hasFnAttribute(Attribute::VScaleRange);
2683 }
2684
2685 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2686 // it is shifted off the end then the result is undefined.
2687 if (match(I, m_Shl(m_One(), m_Value())))
2688 return true;
2689
2690 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2691 // the bottom. If it is shifted off the bottom then the result is undefined.
2692 if (match(I, m_LShr(m_SignMask(), m_Value())))
2693 return true;
2694
2695 // The remaining tests are all recursive, so bail out if we hit the limit.
2697 return false;
2698
2699 switch (I->getOpcode()) {
2700 case Instruction::ZExt:
2701 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2702 case Instruction::Trunc:
2703 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2704 case Instruction::Shl:
2705 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2706 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2707 return false;
2708 case Instruction::LShr:
2709 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2710 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2711 return false;
2712 case Instruction::UDiv:
2714 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2715 return false;
2716 case Instruction::Mul:
2717 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2718 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2719 (OrZero || isKnownNonZero(I, Q, Depth));
2720 case Instruction::And:
2721 // A power of two and'd with anything is a power of two or zero.
2722 if (OrZero &&
2723 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2724 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2725 return true;
2726 // X & (-X) is always a power of two or zero.
2727 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2728 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2729 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2730 return false;
2731 case Instruction::Add: {
2732 // Adding a power-of-two or zero to the same power-of-two or zero yields
2733 // either the original power-of-two, a larger power-of-two or zero.
2735 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2736 Q.IIQ.hasNoSignedWrap(VOBO)) {
2737 if (match(I->getOperand(0),
2738 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2739 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2740 return true;
2741 if (match(I->getOperand(1),
2742 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2743 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2744 return true;
2745
2746 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2747 KnownBits LHSBits(BitWidth);
2748 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2749
2750 KnownBits RHSBits(BitWidth);
2751 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2752 // If i8 V is a power of two or zero:
2753 // ZeroBits: 1 1 1 0 1 1 1 1
2754 // ~ZeroBits: 0 0 0 1 0 0 0 0
2755 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2756 // If OrZero isn't set, we cannot give back a zero result.
2757 // Make sure either the LHS or RHS has a bit set.
2758 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2759 return true;
2760 }
2761
2762 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2763 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2764 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2765 return true;
2766 return false;
2767 }
2768 case Instruction::Select:
2769 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2770 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2771 case Instruction::PHI: {
2772 // A PHI node is power of two if all incoming values are power of two, or if
2773 // it is an induction variable where in each step its value is a power of
2774 // two.
2775 auto *PN = cast<PHINode>(I);
2777
2778 // Check if it is an induction variable and always power of two.
2779 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2780 return true;
2781
2782 // Recursively check all incoming values. Limit recursion to 2 levels, so
2783 // that search complexity is limited to number of operands^2.
2784 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2785 return llvm::all_of(PN->operands(), [&](const Use &U) {
2786 // Value is power of 2 if it is coming from PHI node itself by induction.
2787 if (U.get() == PN)
2788 return true;
2789
2790 // Change the context instruction to the incoming block where it is
2791 // evaluated.
2792 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2793 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2794 });
2795 }
2796 case Instruction::Invoke:
2797 case Instruction::Call: {
2798 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2799 switch (II->getIntrinsicID()) {
2800 case Intrinsic::umax:
2801 case Intrinsic::smax:
2802 case Intrinsic::umin:
2803 case Intrinsic::smin:
2804 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2805 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2806 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2807 // thus dont change pow2/non-pow2 status.
2808 case Intrinsic::bitreverse:
2809 case Intrinsic::bswap:
2810 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2811 case Intrinsic::fshr:
2812 case Intrinsic::fshl:
2813 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2814 if (II->getArgOperand(0) == II->getArgOperand(1))
2815 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2816 break;
2817 default:
2818 break;
2819 }
2820 }
2821 return false;
2822 }
2823 default:
2824 return false;
2825 }
2826}
2827
2828/// Test whether a GEP's result is known to be non-null.
2829///
2830/// Uses properties inherent in a GEP to try to determine whether it is known
2831/// to be non-null.
2832///
2833/// Currently this routine does not support vector GEPs.
2834static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2835 unsigned Depth) {
2836 const Function *F = nullptr;
2837 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2838 F = I->getFunction();
2839
2840 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2841 // may be null iff the base pointer is null and the offset is zero.
2842 if (!GEP->hasNoUnsignedWrap() &&
2843 !(GEP->isInBounds() &&
2844 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2845 return false;
2846
2847 // FIXME: Support vector-GEPs.
2848 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2849
2850 // If the base pointer is non-null, we cannot walk to a null address with an
2851 // inbounds GEP in address space zero.
2852 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2853 return true;
2854
2855 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2856 // If so, then the GEP cannot produce a null pointer, as doing so would
2857 // inherently violate the inbounds contract within address space zero.
2859 GTI != GTE; ++GTI) {
2860 // Struct types are easy -- they must always be indexed by a constant.
2861 if (StructType *STy = GTI.getStructTypeOrNull()) {
2862 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2863 unsigned ElementIdx = OpC->getZExtValue();
2864 const StructLayout *SL = Q.DL.getStructLayout(STy);
2865 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2866 if (ElementOffset > 0)
2867 return true;
2868 continue;
2869 }
2870
2871 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2872 if (GTI.getSequentialElementStride(Q.DL).isZero())
2873 continue;
2874
2875 // Fast path the constant operand case both for efficiency and so we don't
2876 // increment Depth when just zipping down an all-constant GEP.
2877 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2878 if (!OpC->isZero())
2879 return true;
2880 continue;
2881 }
2882
2883 // We post-increment Depth here because while isKnownNonZero increments it
2884 // as well, when we pop back up that increment won't persist. We don't want
2885 // to recurse 10k times just because we have 10k GEP operands. We don't
2886 // bail completely out because we want to handle constant GEPs regardless
2887 // of depth.
2889 continue;
2890
2891 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2892 return true;
2893 }
2894
2895 return false;
2896}
2897
2899 const Instruction *CtxI,
2900 const DominatorTree *DT) {
2901 assert(!isa<Constant>(V) && "Called for constant?");
2902
2903 if (!CtxI || !DT)
2904 return false;
2905
2906 unsigned NumUsesExplored = 0;
2907 for (auto &U : V->uses()) {
2908 // Avoid massive lists
2909 if (NumUsesExplored >= DomConditionsMaxUses)
2910 break;
2911 NumUsesExplored++;
2912
2913 const Instruction *UI = cast<Instruction>(U.getUser());
2914 // If the value is used as an argument to a call or invoke, then argument
2915 // attributes may provide an answer about null-ness.
2916 if (V->getType()->isPointerTy()) {
2917 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2918 if (CB->isArgOperand(&U) &&
2919 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2920 /*AllowUndefOrPoison=*/false) &&
2921 DT->dominates(CB, CtxI))
2922 return true;
2923 }
2924 }
2925
2926 // If the value is used as a load/store, then the pointer must be non null.
2927 if (V == getLoadStorePointerOperand(UI)) {
2930 DT->dominates(UI, CtxI))
2931 return true;
2932 }
2933
2934 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2935 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2936 isValidAssumeForContext(UI, CtxI, DT))
2937 return true;
2938
2939 // Consider only compare instructions uniquely controlling a branch
2940 Value *RHS;
2941 CmpPredicate Pred;
2942 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2943 continue;
2944
2945 bool NonNullIfTrue;
2946 if (cmpExcludesZero(Pred, RHS))
2947 NonNullIfTrue = true;
2949 NonNullIfTrue = false;
2950 else
2951 continue;
2952
2955 for (const auto *CmpU : UI->users()) {
2956 assert(WorkList.empty() && "Should be!");
2957 if (Visited.insert(CmpU).second)
2958 WorkList.push_back(CmpU);
2959
2960 while (!WorkList.empty()) {
2961 auto *Curr = WorkList.pop_back_val();
2962
2963 // If a user is an AND, add all its users to the work list. We only
2964 // propagate "pred != null" condition through AND because it is only
2965 // correct to assume that all conditions of AND are met in true branch.
2966 // TODO: Support similar logic of OR and EQ predicate?
2967 if (NonNullIfTrue)
2968 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2969 for (const auto *CurrU : Curr->users())
2970 if (Visited.insert(CurrU).second)
2971 WorkList.push_back(CurrU);
2972 continue;
2973 }
2974
2975 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2976 assert(BI->isConditional() && "uses a comparison!");
2977
2978 BasicBlock *NonNullSuccessor =
2979 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2980 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2981 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2982 return true;
2983 } else if (NonNullIfTrue && isGuard(Curr) &&
2984 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2985 return true;
2986 }
2987 }
2988 }
2989 }
2990
2991 return false;
2992}
2993
2994/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2995/// ensure that the value it's attached to is never Value? 'RangeType' is
2996/// is the type of the value described by the range.
2997static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2998 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2999 assert(NumRanges >= 1);
3000 for (unsigned i = 0; i < NumRanges; ++i) {
3002 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
3004 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
3005 ConstantRange Range(Lower->getValue(), Upper->getValue());
3006 if (Range.contains(Value))
3007 return false;
3008 }
3009 return true;
3010}
3011
3012/// Try to detect a recurrence that monotonically increases/decreases from a
3013/// non-zero starting value. These are common as induction variables.
3014static bool isNonZeroRecurrence(const PHINode *PN) {
3015 BinaryOperator *BO = nullptr;
3016 Value *Start = nullptr, *Step = nullptr;
3017 const APInt *StartC, *StepC;
3018 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
3019 !match(Start, m_APInt(StartC)) || StartC->isZero())
3020 return false;
3021
3022 switch (BO->getOpcode()) {
3023 case Instruction::Add:
3024 // Starting from non-zero and stepping away from zero can never wrap back
3025 // to zero.
3026 return BO->hasNoUnsignedWrap() ||
3027 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
3028 StartC->isNegative() == StepC->isNegative());
3029 case Instruction::Mul:
3030 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
3031 match(Step, m_APInt(StepC)) && !StepC->isZero();
3032 case Instruction::Shl:
3033 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3034 case Instruction::AShr:
3035 case Instruction::LShr:
3036 return BO->isExact();
3037 default:
3038 return false;
3039 }
3040}
3041
3042static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3044 m_Specific(Op1), m_Zero()))) ||
3046 m_Specific(Op0), m_Zero())));
3047}
3048
3049static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3050 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3051 bool NUW, unsigned Depth) {
3052 // (X + (X != 0)) is non zero
3053 if (matchOpWithOpEqZero(X, Y))
3054 return true;
3055
3056 if (NUW)
3057 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3058 isKnownNonZero(X, DemandedElts, Q, Depth);
3059
3060 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3061 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3062
3063 // If X and Y are both non-negative (as signed values) then their sum is not
3064 // zero unless both X and Y are zero.
3065 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3066 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3067 isKnownNonZero(X, DemandedElts, Q, Depth))
3068 return true;
3069
3070 // If X and Y are both negative (as signed values) then their sum is not
3071 // zero unless both X and Y equal INT_MIN.
3072 if (XKnown.isNegative() && YKnown.isNegative()) {
3074 // The sign bit of X is set. If some other bit is set then X is not equal
3075 // to INT_MIN.
3076 if (XKnown.One.intersects(Mask))
3077 return true;
3078 // The sign bit of Y is set. If some other bit is set then Y is not equal
3079 // to INT_MIN.
3080 if (YKnown.One.intersects(Mask))
3081 return true;
3082 }
3083
3084 // The sum of a non-negative number and a power of two is not zero.
3085 if (XKnown.isNonNegative() &&
3086 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3087 return true;
3088 if (YKnown.isNonNegative() &&
3089 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3090 return true;
3091
3092 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3093}
3094
3095static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3096 unsigned BitWidth, Value *X, Value *Y,
3097 unsigned Depth) {
3098 // (X - (X != 0)) is non zero
3099 // ((X != 0) - X) is non zero
3100 if (matchOpWithOpEqZero(X, Y))
3101 return true;
3102
3103 // TODO: Move this case into isKnownNonEqual().
3104 if (auto *C = dyn_cast<Constant>(X))
3105 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3106 return true;
3107
3108 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3109}
3110
3111static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3112 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3113 bool NUW, unsigned Depth) {
3114 // If X and Y are non-zero then so is X * Y as long as the multiplication
3115 // does not overflow.
3116 if (NSW || NUW)
3117 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3118 isKnownNonZero(Y, DemandedElts, Q, Depth);
3119
3120 // If either X or Y is odd, then if the other is non-zero the result can't
3121 // be zero.
3122 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3123 if (XKnown.One[0])
3124 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3125
3126 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3127 if (YKnown.One[0])
3128 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3129
3130 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3131 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3132 // the lowest known One of X and Y. If they are non-zero, the result
3133 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3134 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3135 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3136 BitWidth;
3137}
3138
3139static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3140 const SimplifyQuery &Q, const KnownBits &KnownVal,
3141 unsigned Depth) {
3142 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3143 switch (I->getOpcode()) {
3144 case Instruction::Shl:
3145 return Lhs.shl(Rhs);
3146 case Instruction::LShr:
3147 return Lhs.lshr(Rhs);
3148 case Instruction::AShr:
3149 return Lhs.ashr(Rhs);
3150 default:
3151 llvm_unreachable("Unknown Shift Opcode");
3152 }
3153 };
3154
3155 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3156 switch (I->getOpcode()) {
3157 case Instruction::Shl:
3158 return Lhs.lshr(Rhs);
3159 case Instruction::LShr:
3160 case Instruction::AShr:
3161 return Lhs.shl(Rhs);
3162 default:
3163 llvm_unreachable("Unknown Shift Opcode");
3164 }
3165 };
3166
3167 if (KnownVal.isUnknown())
3168 return false;
3169
3170 KnownBits KnownCnt =
3171 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3172 APInt MaxShift = KnownCnt.getMaxValue();
3173 unsigned NumBits = KnownVal.getBitWidth();
3174 if (MaxShift.uge(NumBits))
3175 return false;
3176
3177 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3178 return true;
3179
3180 // If all of the bits shifted out are known to be zero, and Val is known
3181 // non-zero then at least one non-zero bit must remain.
3182 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3183 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3184 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3185 return true;
3186
3187 return false;
3188}
3189
3191 const APInt &DemandedElts,
3192 const SimplifyQuery &Q, unsigned Depth) {
3193 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3194 switch (I->getOpcode()) {
3195 case Instruction::Alloca:
3196 // Alloca never returns null, malloc might.
3197 return I->getType()->getPointerAddressSpace() == 0;
3198 case Instruction::GetElementPtr:
3199 if (I->getType()->isPointerTy())
3201 break;
3202 case Instruction::BitCast: {
3203 // We need to be a bit careful here. We can only peek through the bitcast
3204 // if the scalar size of elements in the operand are smaller than and a
3205 // multiple of the size they are casting too. Take three cases:
3206 //
3207 // 1) Unsafe:
3208 // bitcast <2 x i16> %NonZero to <4 x i8>
3209 //
3210 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3211 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3212 // guranteed (imagine just sign bit set in the 2 i16 elements).
3213 //
3214 // 2) Unsafe:
3215 // bitcast <4 x i3> %NonZero to <3 x i4>
3216 //
3217 // Even though the scalar size of the src (`i3`) is smaller than the
3218 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3219 // its possible for the `3 x i4` elements to be zero because there are
3220 // some elements in the destination that don't contain any full src
3221 // element.
3222 //
3223 // 3) Safe:
3224 // bitcast <4 x i8> %NonZero to <2 x i16>
3225 //
3226 // This is always safe as non-zero in the 4 i8 elements implies
3227 // non-zero in the combination of any two adjacent ones. Since i8 is a
3228 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3229 // This all implies the 2 i16 elements are non-zero.
3230 Type *FromTy = I->getOperand(0)->getType();
3231 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3232 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3233 return isKnownNonZero(I->getOperand(0), Q, Depth);
3234 } break;
3235 case Instruction::IntToPtr:
3236 // Note that we have to take special care to avoid looking through
3237 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3238 // as casts that can alter the value, e.g., AddrSpaceCasts.
3239 if (!isa<ScalableVectorType>(I->getType()) &&
3240 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3241 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3242 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3243 break;
3244 case Instruction::PtrToAddr:
3245 // isKnownNonZero() for pointers refers to the address bits being non-zero,
3246 // so we can directly forward.
3247 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3248 case Instruction::PtrToInt:
3249 // For inttoptr, make sure the result size is >= the address size. If the
3250 // address is non-zero, any larger value is also non-zero.
3251 if (Q.DL.getAddressSizeInBits(I->getOperand(0)->getType()) <=
3252 I->getType()->getScalarSizeInBits())
3253 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3254 break;
3255 case Instruction::Trunc:
3256 // nuw/nsw trunc preserves zero/non-zero status of input.
3257 if (auto *TI = dyn_cast<TruncInst>(I))
3258 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3259 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3260 break;
3261
3262 // Iff x - y != 0, then x ^ y != 0
3263 // Therefore we can do the same exact checks
3264 case Instruction::Xor:
3265 case Instruction::Sub:
3266 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3267 I->getOperand(1), Depth);
3268 case Instruction::Or:
3269 // (X | (X != 0)) is non zero
3270 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3271 return true;
3272 // X | Y != 0 if X != Y.
3273 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3274 Depth))
3275 return true;
3276 // X | Y != 0 if X != 0 or Y != 0.
3277 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3278 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3279 case Instruction::SExt:
3280 case Instruction::ZExt:
3281 // ext X != 0 if X != 0.
3282 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3283
3284 case Instruction::Shl: {
3285 // shl nsw/nuw can't remove any non-zero bits.
3287 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3288 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3289
3290 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3291 // if the lowest bit is shifted off the end.
3292 KnownBits Known(BitWidth);
3293 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3294 if (Known.One[0])
3295 return true;
3296
3297 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3298 }
3299 case Instruction::LShr:
3300 case Instruction::AShr: {
3301 // shr exact can only shift out zero bits.
3303 if (BO->isExact())
3304 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3305
3306 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3307 // defined if the sign bit is shifted off the end.
3308 KnownBits Known =
3309 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3310 if (Known.isNegative())
3311 return true;
3312
3313 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3314 }
3315 case Instruction::UDiv:
3316 case Instruction::SDiv: {
3317 // X / Y
3318 // div exact can only produce a zero if the dividend is zero.
3319 if (cast<PossiblyExactOperator>(I)->isExact())
3320 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3321
3322 KnownBits XKnown =
3323 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3324 // If X is fully unknown we won't be able to figure anything out so don't
3325 // both computing knownbits for Y.
3326 if (XKnown.isUnknown())
3327 return false;
3328
3329 KnownBits YKnown =
3330 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3331 if (I->getOpcode() == Instruction::SDiv) {
3332 // For signed division need to compare abs value of the operands.
3333 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3334 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3335 }
3336 // If X u>= Y then div is non zero (0/0 is UB).
3337 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3338 // If X is total unknown or X u< Y we won't be able to prove non-zero
3339 // with compute known bits so just return early.
3340 return XUgeY && *XUgeY;
3341 }
3342 case Instruction::Add: {
3343 // X + Y.
3344
3345 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3346 // non-zero.
3348 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3349 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3350 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3351 }
3352 case Instruction::Mul: {
3354 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3355 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3356 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3357 }
3358 case Instruction::Select: {
3359 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3360
3361 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3362 // then see if the select condition implies the arm is non-zero. For example
3363 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3364 // dominated by `X != 0`.
3365 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3366 Value *Op;
3367 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3368 // Op is trivially non-zero.
3369 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3370 return true;
3371
3372 // The condition of the select dominates the true/false arm. Check if the
3373 // condition implies that a given arm is non-zero.
3374 Value *X;
3375 CmpPredicate Pred;
3376 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3377 return false;
3378
3379 if (!IsTrueArm)
3380 Pred = ICmpInst::getInversePredicate(Pred);
3381
3382 return cmpExcludesZero(Pred, X);
3383 };
3384
3385 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3386 SelectArmIsNonZero(/* IsTrueArm */ false))
3387 return true;
3388 break;
3389 }
3390 case Instruction::PHI: {
3391 auto *PN = cast<PHINode>(I);
3393 return true;
3394
3395 // Check if all incoming values are non-zero using recursion.
3397 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3398 return llvm::all_of(PN->operands(), [&](const Use &U) {
3399 if (U.get() == PN)
3400 return true;
3401 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3402 // Check if the branch on the phi excludes zero.
3403 CmpPredicate Pred;
3404 Value *X;
3405 BasicBlock *TrueSucc, *FalseSucc;
3406 if (match(RecQ.CxtI,
3407 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3408 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3409 // Check for cases of duplicate successors.
3410 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3411 // If we're using the false successor, invert the predicate.
3412 if (FalseSucc == PN->getParent())
3413 Pred = CmpInst::getInversePredicate(Pred);
3414 if (cmpExcludesZero(Pred, X))
3415 return true;
3416 }
3417 }
3418 // Finally recurse on the edge and check it directly.
3419 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3420 });
3421 }
3422 case Instruction::InsertElement: {
3423 if (isa<ScalableVectorType>(I->getType()))
3424 break;
3425
3426 const Value *Vec = I->getOperand(0);
3427 const Value *Elt = I->getOperand(1);
3428 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3429
3430 unsigned NumElts = DemandedElts.getBitWidth();
3431 APInt DemandedVecElts = DemandedElts;
3432 bool SkipElt = false;
3433 // If we know the index we are inserting too, clear it from Vec check.
3434 if (CIdx && CIdx->getValue().ult(NumElts)) {
3435 DemandedVecElts.clearBit(CIdx->getZExtValue());
3436 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3437 }
3438
3439 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3440 // are non-zero.
3441 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3442 (DemandedVecElts.isZero() ||
3443 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3444 }
3445 case Instruction::ExtractElement:
3446 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3447 const Value *Vec = EEI->getVectorOperand();
3448 const Value *Idx = EEI->getIndexOperand();
3449 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3450 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3451 unsigned NumElts = VecTy->getNumElements();
3452 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3453 if (CIdx && CIdx->getValue().ult(NumElts))
3454 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3455 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3456 }
3457 }
3458 break;
3459 case Instruction::ShuffleVector: {
3460 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3461 if (!Shuf)
3462 break;
3463 APInt DemandedLHS, DemandedRHS;
3464 // For undef elements, we don't know anything about the common state of
3465 // the shuffle result.
3466 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3467 break;
3468 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3469 return (DemandedRHS.isZero() ||
3470 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3471 (DemandedLHS.isZero() ||
3472 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3473 }
3474 case Instruction::Freeze:
3475 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3476 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3477 Depth);
3478 case Instruction::Load: {
3479 auto *LI = cast<LoadInst>(I);
3480 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3481 // is never null.
3482 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3483 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3484 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3485 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3486 return true;
3487 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3489 }
3490
3491 // No need to fall through to computeKnownBits as range metadata is already
3492 // handled in isKnownNonZero.
3493 return false;
3494 }
3495 case Instruction::ExtractValue: {
3496 const WithOverflowInst *WO;
3498 switch (WO->getBinaryOp()) {
3499 default:
3500 break;
3501 case Instruction::Add:
3502 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3503 WO->getArgOperand(1),
3504 /*NSW=*/false,
3505 /*NUW=*/false, Depth);
3506 case Instruction::Sub:
3507 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3508 WO->getArgOperand(1), Depth);
3509 case Instruction::Mul:
3510 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3511 WO->getArgOperand(1),
3512 /*NSW=*/false, /*NUW=*/false, Depth);
3513 break;
3514 }
3515 }
3516 break;
3517 }
3518 case Instruction::Call:
3519 case Instruction::Invoke: {
3520 const auto *Call = cast<CallBase>(I);
3521 if (I->getType()->isPointerTy()) {
3522 if (Call->isReturnNonNull())
3523 return true;
3524 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3525 return isKnownNonZero(RP, Q, Depth);
3526 } else {
3527 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3529 if (std::optional<ConstantRange> Range = Call->getRange()) {
3530 const APInt ZeroValue(Range->getBitWidth(), 0);
3531 if (!Range->contains(ZeroValue))
3532 return true;
3533 }
3534 if (const Value *RV = Call->getReturnedArgOperand())
3535 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3536 return true;
3537 }
3538
3539 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3540 switch (II->getIntrinsicID()) {
3541 case Intrinsic::sshl_sat:
3542 case Intrinsic::ushl_sat:
3543 case Intrinsic::abs:
3544 case Intrinsic::bitreverse:
3545 case Intrinsic::bswap:
3546 case Intrinsic::ctpop:
3547 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3548 // NB: We don't do usub_sat here as in any case we can prove its
3549 // non-zero, we will fold it to `sub nuw` in InstCombine.
3550 case Intrinsic::ssub_sat:
3551 // For most types, if x != y then ssub.sat x, y != 0. But
3552 // ssub.sat.i1 0, -1 = 0, because 1 saturates to 0. This means
3553 // isNonZeroSub will do the wrong thing for ssub.sat.i1.
3554 if (BitWidth == 1)
3555 return false;
3556 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3557 II->getArgOperand(1), Depth);
3558 case Intrinsic::sadd_sat:
3559 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3560 II->getArgOperand(1),
3561 /*NSW=*/true, /* NUW=*/false, Depth);
3562 // Vec reverse preserves zero/non-zero status from input vec.
3563 case Intrinsic::vector_reverse:
3564 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3565 Q, Depth);
3566 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3567 case Intrinsic::vector_reduce_or:
3568 case Intrinsic::vector_reduce_umax:
3569 case Intrinsic::vector_reduce_umin:
3570 case Intrinsic::vector_reduce_smax:
3571 case Intrinsic::vector_reduce_smin:
3572 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3573 case Intrinsic::umax:
3574 case Intrinsic::uadd_sat:
3575 // umax(X, (X != 0)) is non zero
3576 // X +usat (X != 0) is non zero
3577 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3578 return true;
3579
3580 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3581 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3582 case Intrinsic::smax: {
3583 // If either arg is strictly positive the result is non-zero. Otherwise
3584 // the result is non-zero if both ops are non-zero.
3585 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3586 const KnownBits &OpKnown) {
3587 if (!OpNonZero.has_value())
3588 OpNonZero = OpKnown.isNonZero() ||
3589 isKnownNonZero(Op, DemandedElts, Q, Depth);
3590 return *OpNonZero;
3591 };
3592 // Avoid re-computing isKnownNonZero.
3593 std::optional<bool> Op0NonZero, Op1NonZero;
3594 KnownBits Op1Known =
3595 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3596 if (Op1Known.isNonNegative() &&
3597 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3598 return true;
3599 KnownBits Op0Known =
3600 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3601 if (Op0Known.isNonNegative() &&
3602 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3603 return true;
3604 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3605 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3606 }
3607 case Intrinsic::smin: {
3608 // If either arg is negative the result is non-zero. Otherwise
3609 // the result is non-zero if both ops are non-zero.
3610 KnownBits Op1Known =
3611 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3612 if (Op1Known.isNegative())
3613 return true;
3614 KnownBits Op0Known =
3615 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3616 if (Op0Known.isNegative())
3617 return true;
3618
3619 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3620 return true;
3621 }
3622 [[fallthrough]];
3623 case Intrinsic::umin:
3624 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3625 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3626 case Intrinsic::cttz:
3627 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3628 .Zero[0];
3629 case Intrinsic::ctlz:
3630 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3631 .isNonNegative();
3632 case Intrinsic::fshr:
3633 case Intrinsic::fshl:
3634 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3635 if (II->getArgOperand(0) == II->getArgOperand(1))
3636 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3637 break;
3638 case Intrinsic::vscale:
3639 return true;
3640 case Intrinsic::experimental_get_vector_length:
3641 return isKnownNonZero(I->getOperand(0), Q, Depth);
3642 default:
3643 break;
3644 }
3645 break;
3646 }
3647
3648 return false;
3649 }
3650 }
3651
3652 KnownBits Known(BitWidth);
3653 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3654 return Known.One != 0;
3655}
3656
3657/// Return true if the given value is known to be non-zero when defined. For
3658/// vectors, return true if every demanded element is known to be non-zero when
3659/// defined. For pointers, if the context instruction and dominator tree are
3660/// specified, perform context-sensitive analysis and return true if the
3661/// pointer couldn't possibly be null at the specified instruction.
3662/// Supports values with integer or pointer type and vectors of integers.
3663bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3664 const SimplifyQuery &Q, unsigned Depth) {
3665 Type *Ty = V->getType();
3666
3667#ifndef NDEBUG
3668 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3669
3670 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3671 assert(
3672 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3673 "DemandedElt width should equal the fixed vector number of elements");
3674 } else {
3675 assert(DemandedElts == APInt(1, 1) &&
3676 "DemandedElt width should be 1 for scalars");
3677 }
3678#endif
3679
3680 if (auto *C = dyn_cast<Constant>(V)) {
3681 if (C->isNullValue())
3682 return false;
3683 if (isa<ConstantInt>(C))
3684 // Must be non-zero due to null test above.
3685 return true;
3686
3687 // For constant vectors, check that all elements are poison or known
3688 // non-zero to determine that the whole vector is known non-zero.
3689 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3690 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3691 if (!DemandedElts[i])
3692 continue;
3693 Constant *Elt = C->getAggregateElement(i);
3694 if (!Elt || Elt->isNullValue())
3695 return false;
3696 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3697 return false;
3698 }
3699 return true;
3700 }
3701
3702 // Constant ptrauth can be null, iff the base pointer can be.
3703 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3704 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3705
3706 // A global variable in address space 0 is non null unless extern weak
3707 // or an absolute symbol reference. Other address spaces may have null as a
3708 // valid address for a global, so we can't assume anything.
3709 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3710 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3711 GV->getType()->getAddressSpace() == 0)
3712 return true;
3713 }
3714
3715 // For constant expressions, fall through to the Operator code below.
3716 if (!isa<ConstantExpr>(V))
3717 return false;
3718 }
3719
3720 if (const auto *A = dyn_cast<Argument>(V))
3721 if (std::optional<ConstantRange> Range = A->getRange()) {
3722 const APInt ZeroValue(Range->getBitWidth(), 0);
3723 if (!Range->contains(ZeroValue))
3724 return true;
3725 }
3726
3727 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3728 return true;
3729
3730 // Some of the tests below are recursive, so bail out if we hit the limit.
3732 return false;
3733
3734 // Check for pointer simplifications.
3735
3736 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3737 // A byval, inalloca may not be null in a non-default addres space. A
3738 // nonnull argument is assumed never 0.
3739 if (const Argument *A = dyn_cast<Argument>(V)) {
3740 if (((A->hasPassPointeeByValueCopyAttr() &&
3741 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3742 A->hasNonNullAttr()))
3743 return true;
3744 }
3745 }
3746
3747 if (const auto *I = dyn_cast<Operator>(V))
3748 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3749 return true;
3750
3751 if (!isa<Constant>(V) &&
3753 return true;
3754
3755 if (const Value *Stripped = stripNullTest(V))
3756 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3757
3758 return false;
3759}
3760
3762 unsigned Depth) {
3763 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3764 APInt DemandedElts =
3765 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3766 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3767}
3768
3769/// If the pair of operators are the same invertible function, return the
3770/// the operands of the function corresponding to each input. Otherwise,
3771/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3772/// every input value to exactly one output value. This is equivalent to
3773/// saying that Op1 and Op2 are equal exactly when the specified pair of
3774/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3775static std::optional<std::pair<Value*, Value*>>
3777 const Operator *Op2) {
3778 if (Op1->getOpcode() != Op2->getOpcode())
3779 return std::nullopt;
3780
3781 auto getOperands = [&](unsigned OpNum) -> auto {
3782 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3783 };
3784
3785 switch (Op1->getOpcode()) {
3786 default:
3787 break;
3788 case Instruction::Or:
3789 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3790 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3791 break;
3792 [[fallthrough]];
3793 case Instruction::Xor:
3794 case Instruction::Add: {
3795 Value *Other;
3796 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3797 return std::make_pair(Op1->getOperand(1), Other);
3798 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3799 return std::make_pair(Op1->getOperand(0), Other);
3800 break;
3801 }
3802 case Instruction::Sub:
3803 if (Op1->getOperand(0) == Op2->getOperand(0))
3804 return getOperands(1);
3805 if (Op1->getOperand(1) == Op2->getOperand(1))
3806 return getOperands(0);
3807 break;
3808 case Instruction::Mul: {
3809 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3810 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3811 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3812 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3813 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3814 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3815 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3816 break;
3817
3818 // Assume operand order has been canonicalized
3819 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3820 isa<ConstantInt>(Op1->getOperand(1)) &&
3821 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3822 return getOperands(0);
3823 break;
3824 }
3825 case Instruction::Shl: {
3826 // Same as multiplies, with the difference that we don't need to check
3827 // for a non-zero multiply. Shifts always multiply by non-zero.
3828 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3829 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3830 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3831 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3832 break;
3833
3834 if (Op1->getOperand(1) == Op2->getOperand(1))
3835 return getOperands(0);
3836 break;
3837 }
3838 case Instruction::AShr:
3839 case Instruction::LShr: {
3840 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3841 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3842 if (!PEO1->isExact() || !PEO2->isExact())
3843 break;
3844
3845 if (Op1->getOperand(1) == Op2->getOperand(1))
3846 return getOperands(0);
3847 break;
3848 }
3849 case Instruction::SExt:
3850 case Instruction::ZExt:
3851 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3852 return getOperands(0);
3853 break;
3854 case Instruction::PHI: {
3855 const PHINode *PN1 = cast<PHINode>(Op1);
3856 const PHINode *PN2 = cast<PHINode>(Op2);
3857
3858 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3859 // are a single invertible function of the start values? Note that repeated
3860 // application of an invertible function is also invertible
3861 BinaryOperator *BO1 = nullptr;
3862 Value *Start1 = nullptr, *Step1 = nullptr;
3863 BinaryOperator *BO2 = nullptr;
3864 Value *Start2 = nullptr, *Step2 = nullptr;
3865 if (PN1->getParent() != PN2->getParent() ||
3866 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3867 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3868 break;
3869
3870 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3871 cast<Operator>(BO2));
3872 if (!Values)
3873 break;
3874
3875 // We have to be careful of mutually defined recurrences here. Ex:
3876 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3877 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3878 // The invertibility of these is complicated, and not worth reasoning
3879 // about (yet?).
3880 if (Values->first != PN1 || Values->second != PN2)
3881 break;
3882
3883 return std::make_pair(Start1, Start2);
3884 }
3885 }
3886 return std::nullopt;
3887}
3888
3889/// Return true if V1 == (binop V2, X), where X is known non-zero.
3890/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3891/// implies V2 != V1.
3892static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3893 const APInt &DemandedElts,
3894 const SimplifyQuery &Q, unsigned Depth) {
3896 if (!BO)
3897 return false;
3898 switch (BO->getOpcode()) {
3899 default:
3900 break;
3901 case Instruction::Or:
3902 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3903 break;
3904 [[fallthrough]];
3905 case Instruction::Xor:
3906 case Instruction::Add:
3907 Value *Op = nullptr;
3908 if (V2 == BO->getOperand(0))
3909 Op = BO->getOperand(1);
3910 else if (V2 == BO->getOperand(1))
3911 Op = BO->getOperand(0);
3912 else
3913 return false;
3914 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3915 }
3916 return false;
3917}
3918
3919/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3920/// the multiplication is nuw or nsw.
3921static bool isNonEqualMul(const Value *V1, const Value *V2,
3922 const APInt &DemandedElts, const SimplifyQuery &Q,
3923 unsigned Depth) {
3924 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3925 const APInt *C;
3926 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3927 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3928 !C->isZero() && !C->isOne() &&
3929 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3930 }
3931 return false;
3932}
3933
3934/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3935/// the shift is nuw or nsw.
3936static bool isNonEqualShl(const Value *V1, const Value *V2,
3937 const APInt &DemandedElts, const SimplifyQuery &Q,
3938 unsigned Depth) {
3939 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3940 const APInt *C;
3941 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3942 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3943 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3944 }
3945 return false;
3946}
3947
3948static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3949 const APInt &DemandedElts, const SimplifyQuery &Q,
3950 unsigned Depth) {
3951 // Check two PHIs are in same block.
3952 if (PN1->getParent() != PN2->getParent())
3953 return false;
3954
3956 bool UsedFullRecursion = false;
3957 for (const BasicBlock *IncomBB : PN1->blocks()) {
3958 if (!VisitedBBs.insert(IncomBB).second)
3959 continue; // Don't reprocess blocks that we have dealt with already.
3960 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3961 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3962 const APInt *C1, *C2;
3963 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3964 continue;
3965
3966 // Only one pair of phi operands is allowed for full recursion.
3967 if (UsedFullRecursion)
3968 return false;
3969
3971 RecQ.CxtI = IncomBB->getTerminator();
3972 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3973 return false;
3974 UsedFullRecursion = true;
3975 }
3976 return true;
3977}
3978
3979static bool isNonEqualSelect(const Value *V1, const Value *V2,
3980 const APInt &DemandedElts, const SimplifyQuery &Q,
3981 unsigned Depth) {
3982 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3983 if (!SI1)
3984 return false;
3985
3986 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3987 const Value *Cond1 = SI1->getCondition();
3988 const Value *Cond2 = SI2->getCondition();
3989 if (Cond1 == Cond2)
3990 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3991 DemandedElts, Q, Depth + 1) &&
3992 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3993 DemandedElts, Q, Depth + 1);
3994 }
3995 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
3996 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
3997}
3998
3999// Check to see if A is both a GEP and is the incoming value for a PHI in the
4000// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
4001// one of them being the recursive GEP A and the other a ptr at same base and at
4002// the same/higher offset than B we are only incrementing the pointer further in
4003// loop if offset of recursive GEP is greater than 0.
4005 const SimplifyQuery &Q) {
4006 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
4007 return false;
4008
4009 auto *GEPA = dyn_cast<GEPOperator>(A);
4010 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
4011 return false;
4012
4013 // Handle 2 incoming PHI values with one being a recursive GEP.
4014 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
4015 if (!PN || PN->getNumIncomingValues() != 2)
4016 return false;
4017
4018 // Search for the recursive GEP as an incoming operand, and record that as
4019 // Step.
4020 Value *Start = nullptr;
4021 Value *Step = const_cast<Value *>(A);
4022 if (PN->getIncomingValue(0) == Step)
4023 Start = PN->getIncomingValue(1);
4024 else if (PN->getIncomingValue(1) == Step)
4025 Start = PN->getIncomingValue(0);
4026 else
4027 return false;
4028
4029 // Other incoming node base should match the B base.
4030 // StartOffset >= OffsetB && StepOffset > 0?
4031 // StartOffset <= OffsetB && StepOffset < 0?
4032 // Is non-equal if above are true.
4033 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
4034 // optimisation to inbounds GEPs only.
4035 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
4036 APInt StartOffset(IndexWidth, 0);
4037 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
4038 APInt StepOffset(IndexWidth, 0);
4039 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
4040
4041 // Check if Base Pointer of Step matches the PHI.
4042 if (Step != PN)
4043 return false;
4044 APInt OffsetB(IndexWidth, 0);
4045 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4046 return Start == B &&
4047 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4048 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4049}
4050
4051static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4052 const SimplifyQuery &Q, unsigned Depth) {
4053 if (!Q.CxtI)
4054 return false;
4055
4056 // Try to infer NonEqual based on information from dominating conditions.
4057 if (Q.DC && Q.DT) {
4058 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4059 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4060 Value *Cond = BI->getCondition();
4061 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4062 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4064 /*LHSIsTrue=*/true, Depth)
4065 .value_or(false))
4066 return true;
4067
4068 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4069 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4071 /*LHSIsTrue=*/false, Depth)
4072 .value_or(false))
4073 return true;
4074 }
4075
4076 return false;
4077 };
4078
4079 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4080 IsKnownNonEqualFromDominatingCondition(V2))
4081 return true;
4082 }
4083
4084 if (!Q.AC)
4085 return false;
4086
4087 // Try to infer NonEqual based on information from assumptions.
4088 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4089 if (!AssumeVH)
4090 continue;
4091 CallInst *I = cast<CallInst>(AssumeVH);
4092
4093 assert(I->getFunction() == Q.CxtI->getFunction() &&
4094 "Got assumption for the wrong function!");
4095 assert(I->getIntrinsicID() == Intrinsic::assume &&
4096 "must be an assume intrinsic");
4097
4098 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4099 /*LHSIsTrue=*/true, Depth)
4100 .value_or(false) &&
4102 return true;
4103 }
4104
4105 return false;
4106}
4107
4108/// Return true if it is known that V1 != V2.
4109static bool isKnownNonEqual(const Value *V1, const Value *V2,
4110 const APInt &DemandedElts, const SimplifyQuery &Q,
4111 unsigned Depth) {
4112 if (V1 == V2)
4113 return false;
4114 if (V1->getType() != V2->getType())
4115 // We can't look through casts yet.
4116 return false;
4117
4119 return false;
4120
4121 // See if we can recurse through (exactly one of) our operands. This
4122 // requires our operation be 1-to-1 and map every input value to exactly
4123 // one output value. Such an operation is invertible.
4124 auto *O1 = dyn_cast<Operator>(V1);
4125 auto *O2 = dyn_cast<Operator>(V2);
4126 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4127 if (auto Values = getInvertibleOperands(O1, O2))
4128 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4129 Depth + 1);
4130
4131 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4132 const PHINode *PN2 = cast<PHINode>(V2);
4133 // FIXME: This is missing a generalization to handle the case where one is
4134 // a PHI and another one isn't.
4135 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4136 return true;
4137 };
4138 }
4139
4140 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4141 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4142 return true;
4143
4144 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4145 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4146 return true;
4147
4148 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4149 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4150 return true;
4151
4152 if (V1->getType()->isIntOrIntVectorTy()) {
4153 // Are any known bits in V1 contradictory to known bits in V2? If V1
4154 // has a known zero where V2 has a known one, they must not be equal.
4155 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4156 if (!Known1.isUnknown()) {
4157 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4158 if (Known1.Zero.intersects(Known2.One) ||
4159 Known2.Zero.intersects(Known1.One))
4160 return true;
4161 }
4162 }
4163
4164 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4165 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4166 return true;
4167
4168 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
4170 return true;
4171
4172 Value *A, *B;
4173 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4174 // Check PtrToInt type matches the pointer size.
4175 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4177 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4178
4179 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4180 return true;
4181
4182 return false;
4183}
4184
4185/// For vector constants, loop over the elements and find the constant with the
4186/// minimum number of sign bits. Return 0 if the value is not a vector constant
4187/// or if any element was not analyzed; otherwise, return the count for the
4188/// element with the minimum number of sign bits.
4190 const APInt &DemandedElts,
4191 unsigned TyBits) {
4192 const auto *CV = dyn_cast<Constant>(V);
4193 if (!CV || !isa<FixedVectorType>(CV->getType()))
4194 return 0;
4195
4196 unsigned MinSignBits = TyBits;
4197 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4198 for (unsigned i = 0; i != NumElts; ++i) {
4199 if (!DemandedElts[i])
4200 continue;
4201 // If we find a non-ConstantInt, bail out.
4202 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4203 if (!Elt)
4204 return 0;
4205
4206 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4207 }
4208
4209 return MinSignBits;
4210}
4211
4212static unsigned ComputeNumSignBitsImpl(const Value *V,
4213 const APInt &DemandedElts,
4214 const SimplifyQuery &Q, unsigned Depth);
4215
4216static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4217 const SimplifyQuery &Q, unsigned Depth) {
4218 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4219 assert(Result > 0 && "At least one sign bit needs to be present!");
4220 return Result;
4221}
4222
4223/// Return the number of times the sign bit of the register is replicated into
4224/// the other bits. We know that at least 1 bit is always equal to the sign bit
4225/// (itself), but other cases can give us information. For example, immediately
4226/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4227/// other, so we return 3. For vectors, return the number of sign bits for the
4228/// vector element with the minimum number of known sign bits of the demanded
4229/// elements in the vector specified by DemandedElts.
4230static unsigned ComputeNumSignBitsImpl(const Value *V,
4231 const APInt &DemandedElts,
4232 const SimplifyQuery &Q, unsigned Depth) {
4233 Type *Ty = V->getType();
4234#ifndef NDEBUG
4235 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4236
4237 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4238 assert(
4239 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4240 "DemandedElt width should equal the fixed vector number of elements");
4241 } else {
4242 assert(DemandedElts == APInt(1, 1) &&
4243 "DemandedElt width should be 1 for scalars");
4244 }
4245#endif
4246
4247 // We return the minimum number of sign bits that are guaranteed to be present
4248 // in V, so for undef we have to conservatively return 1. We don't have the
4249 // same behavior for poison though -- that's a FIXME today.
4250
4251 Type *ScalarTy = Ty->getScalarType();
4252 unsigned TyBits = ScalarTy->isPointerTy() ?
4253 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4254 Q.DL.getTypeSizeInBits(ScalarTy);
4255
4256 unsigned Tmp, Tmp2;
4257 unsigned FirstAnswer = 1;
4258
4259 // Note that ConstantInt is handled by the general computeKnownBits case
4260 // below.
4261
4263 return 1;
4264
4265 if (auto *U = dyn_cast<Operator>(V)) {
4266 switch (Operator::getOpcode(V)) {
4267 default: break;
4268 case Instruction::BitCast: {
4269 Value *Src = U->getOperand(0);
4270 Type *SrcTy = Src->getType();
4271
4272 // Skip if the source type is not an integer or integer vector type
4273 // This ensures we only process integer-like types
4274 if (!SrcTy->isIntOrIntVectorTy())
4275 break;
4276
4277 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4278
4279 // Bitcast 'large element' scalar/vector to 'small element' vector.
4280 if ((SrcBits % TyBits) != 0)
4281 break;
4282
4283 // Only proceed if the destination type is a fixed-size vector
4284 if (isa<FixedVectorType>(Ty)) {
4285 // Fast case - sign splat can be simply split across the small elements.
4286 // This works for both vector and scalar sources
4287 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4288 if (Tmp == SrcBits)
4289 return TyBits;
4290 }
4291 break;
4292 }
4293 case Instruction::SExt:
4294 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4295 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4296 Tmp;
4297
4298 case Instruction::SDiv: {
4299 const APInt *Denominator;
4300 // sdiv X, C -> adds log(C) sign bits.
4301 if (match(U->getOperand(1), m_APInt(Denominator))) {
4302
4303 // Ignore non-positive denominator.
4304 if (!Denominator->isStrictlyPositive())
4305 break;
4306
4307 // Calculate the incoming numerator bits.
4308 unsigned NumBits =
4309 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4310
4311 // Add floor(log(C)) bits to the numerator bits.
4312 return std::min(TyBits, NumBits + Denominator->logBase2());
4313 }
4314 break;
4315 }
4316
4317 case Instruction::SRem: {
4318 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4319
4320 const APInt *Denominator;
4321 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4322 // positive constant. This let us put a lower bound on the number of sign
4323 // bits.
4324 if (match(U->getOperand(1), m_APInt(Denominator))) {
4325
4326 // Ignore non-positive denominator.
4327 if (Denominator->isStrictlyPositive()) {
4328 // Calculate the leading sign bit constraints by examining the
4329 // denominator. Given that the denominator is positive, there are two
4330 // cases:
4331 //
4332 // 1. The numerator is positive. The result range is [0,C) and
4333 // [0,C) u< (1 << ceilLogBase2(C)).
4334 //
4335 // 2. The numerator is negative. Then the result range is (-C,0] and
4336 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4337 //
4338 // Thus a lower bound on the number of sign bits is `TyBits -
4339 // ceilLogBase2(C)`.
4340
4341 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4342 Tmp = std::max(Tmp, ResBits);
4343 }
4344 }
4345 return Tmp;
4346 }
4347
4348 case Instruction::AShr: {
4349 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4350 // ashr X, C -> adds C sign bits. Vectors too.
4351 const APInt *ShAmt;
4352 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4353 if (ShAmt->uge(TyBits))
4354 break; // Bad shift.
4355 unsigned ShAmtLimited = ShAmt->getZExtValue();
4356 Tmp += ShAmtLimited;
4357 if (Tmp > TyBits) Tmp = TyBits;
4358 }
4359 return Tmp;
4360 }
4361 case Instruction::Shl: {
4362 const APInt *ShAmt;
4363 Value *X = nullptr;
4364 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4365 // shl destroys sign bits.
4366 if (ShAmt->uge(TyBits))
4367 break; // Bad shift.
4368 // We can look through a zext (more or less treating it as a sext) if
4369 // all extended bits are shifted out.
4370 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4371 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4372 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4373 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4374 } else
4375 Tmp =
4376 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4377 if (ShAmt->uge(Tmp))
4378 break; // Shifted all sign bits out.
4379 Tmp2 = ShAmt->getZExtValue();
4380 return Tmp - Tmp2;
4381 }
4382 break;
4383 }
4384 case Instruction::And:
4385 case Instruction::Or:
4386 case Instruction::Xor: // NOT is handled here.
4387 // Logical binary ops preserve the number of sign bits at the worst.
4388 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4389 if (Tmp != 1) {
4390 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4391 FirstAnswer = std::min(Tmp, Tmp2);
4392 // We computed what we know about the sign bits as our first
4393 // answer. Now proceed to the generic code that uses
4394 // computeKnownBits, and pick whichever answer is better.
4395 }
4396 break;
4397
4398 case Instruction::Select: {
4399 // If we have a clamp pattern, we know that the number of sign bits will
4400 // be the minimum of the clamp min/max range.
4401 const Value *X;
4402 const APInt *CLow, *CHigh;
4403 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4404 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4405
4406 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4407 if (Tmp == 1)
4408 break;
4409 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4410 return std::min(Tmp, Tmp2);
4411 }
4412
4413 case Instruction::Add:
4414 // Add can have at most one carry bit. Thus we know that the output
4415 // is, at worst, one more bit than the inputs.
4416 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4417 if (Tmp == 1) break;
4418
4419 // Special case decrementing a value (ADD X, -1):
4420 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4421 if (CRHS->isAllOnesValue()) {
4422 KnownBits Known(TyBits);
4423 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4424
4425 // If the input is known to be 0 or 1, the output is 0/-1, which is
4426 // all sign bits set.
4427 if ((Known.Zero | 1).isAllOnes())
4428 return TyBits;
4429
4430 // If we are subtracting one from a positive number, there is no carry
4431 // out of the result.
4432 if (Known.isNonNegative())
4433 return Tmp;
4434 }
4435
4436 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4437 if (Tmp2 == 1)
4438 break;
4439 return std::min(Tmp, Tmp2) - 1;
4440
4441 case Instruction::Sub:
4442 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4443 if (Tmp2 == 1)
4444 break;
4445
4446 // Handle NEG.
4447 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4448 if (CLHS->isNullValue()) {
4449 KnownBits Known(TyBits);
4450 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
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 the input is known to be positive (the sign bit is known clear),
4457 // the output of the NEG has the same number of sign bits as the
4458 // input.
4459 if (Known.isNonNegative())
4460 return Tmp2;
4461
4462 // Otherwise, we treat this like a SUB.
4463 }
4464
4465 // Sub can have at most one carry bit. Thus we know that the output
4466 // is, at worst, one more bit than the inputs.
4467 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4468 if (Tmp == 1)
4469 break;
4470 return std::min(Tmp, Tmp2) - 1;
4471
4472 case Instruction::Mul: {
4473 // The output of the Mul can be at most twice the valid bits in the
4474 // inputs.
4475 unsigned SignBitsOp0 =
4476 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4477 if (SignBitsOp0 == 1)
4478 break;
4479 unsigned SignBitsOp1 =
4480 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4481 if (SignBitsOp1 == 1)
4482 break;
4483 unsigned OutValidBits =
4484 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4485 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4486 }
4487
4488 case Instruction::PHI: {
4489 const PHINode *PN = cast<PHINode>(U);
4490 unsigned NumIncomingValues = PN->getNumIncomingValues();
4491 // Don't analyze large in-degree PHIs.
4492 if (NumIncomingValues > 4) break;
4493 // Unreachable blocks may have zero-operand PHI nodes.
4494 if (NumIncomingValues == 0) break;
4495
4496 // Take the minimum of all incoming values. This can't infinitely loop
4497 // because of our depth threshold.
4499 Tmp = TyBits;
4500 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4501 if (Tmp == 1) return Tmp;
4502 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4503 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4504 DemandedElts, RecQ, Depth + 1));
4505 }
4506 return Tmp;
4507 }
4508
4509 case Instruction::Trunc: {
4510 // If the input contained enough sign bits that some remain after the
4511 // truncation, then we can make use of that. Otherwise we don't know
4512 // anything.
4513 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4514 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4515 if (Tmp > (OperandTyBits - TyBits))
4516 return Tmp - (OperandTyBits - TyBits);
4517
4518 return 1;
4519 }
4520
4521 case Instruction::ExtractElement:
4522 // Look through extract element. At the moment we keep this simple and
4523 // skip tracking the specific element. But at least we might find
4524 // information valid for all elements of the vector (for example if vector
4525 // is sign extended, shifted, etc).
4526 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4527
4528 case Instruction::ShuffleVector: {
4529 // Collect the minimum number of sign bits that are shared by every vector
4530 // element referenced by the shuffle.
4531 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4532 if (!Shuf) {
4533 // FIXME: Add support for shufflevector constant expressions.
4534 return 1;
4535 }
4536 APInt DemandedLHS, DemandedRHS;
4537 // For undef elements, we don't know anything about the common state of
4538 // the shuffle result.
4539 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4540 return 1;
4541 Tmp = std::numeric_limits<unsigned>::max();
4542 if (!!DemandedLHS) {
4543 const Value *LHS = Shuf->getOperand(0);
4544 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4545 }
4546 // If we don't know anything, early out and try computeKnownBits
4547 // fall-back.
4548 if (Tmp == 1)
4549 break;
4550 if (!!DemandedRHS) {
4551 const Value *RHS = Shuf->getOperand(1);
4552 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4553 Tmp = std::min(Tmp, Tmp2);
4554 }
4555 // If we don't know anything, early out and try computeKnownBits
4556 // fall-back.
4557 if (Tmp == 1)
4558 break;
4559 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4560 return Tmp;
4561 }
4562 case Instruction::Call: {
4563 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4564 switch (II->getIntrinsicID()) {
4565 default:
4566 break;
4567 case Intrinsic::abs:
4568 Tmp =
4569 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4570 if (Tmp == 1)
4571 break;
4572
4573 // Absolute value reduces number of sign bits by at most 1.
4574 return Tmp - 1;
4575 case Intrinsic::smin:
4576 case Intrinsic::smax: {
4577 const APInt *CLow, *CHigh;
4578 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4579 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4580 }
4581 }
4582 }
4583 }
4584 }
4585 }
4586
4587 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4588 // use this information.
4589
4590 // If we can examine all elements of a vector constant successfully, we're
4591 // done (we can't do any better than that). If not, keep trying.
4592 if (unsigned VecSignBits =
4593 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4594 return VecSignBits;
4595
4596 KnownBits Known(TyBits);
4597 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4598
4599 // If we know that the sign bit is either zero or one, determine the number of
4600 // identical bits in the top of the input value.
4601 return std::max(FirstAnswer, Known.countMinSignBits());
4602}
4603
4605 const TargetLibraryInfo *TLI) {
4606 const Function *F = CB.getCalledFunction();
4607 if (!F)
4609
4610 if (F->isIntrinsic())
4611 return F->getIntrinsicID();
4612
4613 // We are going to infer semantics of a library function based on mapping it
4614 // to an LLVM intrinsic. Check that the library function is available from
4615 // this callbase and in this environment.
4616 LibFunc Func;
4617 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4618 !CB.onlyReadsMemory())
4620
4621 switch (Func) {
4622 default:
4623 break;
4624 case LibFunc_sin:
4625 case LibFunc_sinf:
4626 case LibFunc_sinl:
4627 return Intrinsic::sin;
4628 case LibFunc_cos:
4629 case LibFunc_cosf:
4630 case LibFunc_cosl:
4631 return Intrinsic::cos;
4632 case LibFunc_tan:
4633 case LibFunc_tanf:
4634 case LibFunc_tanl:
4635 return Intrinsic::tan;
4636 case LibFunc_asin:
4637 case LibFunc_asinf:
4638 case LibFunc_asinl:
4639 return Intrinsic::asin;
4640 case LibFunc_acos:
4641 case LibFunc_acosf:
4642 case LibFunc_acosl:
4643 return Intrinsic::acos;
4644 case LibFunc_atan:
4645 case LibFunc_atanf:
4646 case LibFunc_atanl:
4647 return Intrinsic::atan;
4648 case LibFunc_atan2:
4649 case LibFunc_atan2f:
4650 case LibFunc_atan2l:
4651 return Intrinsic::atan2;
4652 case LibFunc_sinh:
4653 case LibFunc_sinhf:
4654 case LibFunc_sinhl:
4655 return Intrinsic::sinh;
4656 case LibFunc_cosh:
4657 case LibFunc_coshf:
4658 case LibFunc_coshl:
4659 return Intrinsic::cosh;
4660 case LibFunc_tanh:
4661 case LibFunc_tanhf:
4662 case LibFunc_tanhl:
4663 return Intrinsic::tanh;
4664 case LibFunc_exp:
4665 case LibFunc_expf:
4666 case LibFunc_expl:
4667 return Intrinsic::exp;
4668 case LibFunc_exp2:
4669 case LibFunc_exp2f:
4670 case LibFunc_exp2l:
4671 return Intrinsic::exp2;
4672 case LibFunc_exp10:
4673 case LibFunc_exp10f:
4674 case LibFunc_exp10l:
4675 return Intrinsic::exp10;
4676 case LibFunc_log:
4677 case LibFunc_logf:
4678 case LibFunc_logl:
4679 return Intrinsic::log;
4680 case LibFunc_log10:
4681 case LibFunc_log10f:
4682 case LibFunc_log10l:
4683 return Intrinsic::log10;
4684 case LibFunc_log2:
4685 case LibFunc_log2f:
4686 case LibFunc_log2l:
4687 return Intrinsic::log2;
4688 case LibFunc_fabs:
4689 case LibFunc_fabsf:
4690 case LibFunc_fabsl:
4691 return Intrinsic::fabs;
4692 case LibFunc_fmin:
4693 case LibFunc_fminf:
4694 case LibFunc_fminl:
4695 return Intrinsic::minnum;
4696 case LibFunc_fmax:
4697 case LibFunc_fmaxf:
4698 case LibFunc_fmaxl:
4699 return Intrinsic::maxnum;
4700 case LibFunc_copysign:
4701 case LibFunc_copysignf:
4702 case LibFunc_copysignl:
4703 return Intrinsic::copysign;
4704 case LibFunc_floor:
4705 case LibFunc_floorf:
4706 case LibFunc_floorl:
4707 return Intrinsic::floor;
4708 case LibFunc_ceil:
4709 case LibFunc_ceilf:
4710 case LibFunc_ceill:
4711 return Intrinsic::ceil;
4712 case LibFunc_trunc:
4713 case LibFunc_truncf:
4714 case LibFunc_truncl:
4715 return Intrinsic::trunc;
4716 case LibFunc_rint:
4717 case LibFunc_rintf:
4718 case LibFunc_rintl:
4719 return Intrinsic::rint;
4720 case LibFunc_nearbyint:
4721 case LibFunc_nearbyintf:
4722 case LibFunc_nearbyintl:
4723 return Intrinsic::nearbyint;
4724 case LibFunc_round:
4725 case LibFunc_roundf:
4726 case LibFunc_roundl:
4727 return Intrinsic::round;
4728 case LibFunc_roundeven:
4729 case LibFunc_roundevenf:
4730 case LibFunc_roundevenl:
4731 return Intrinsic::roundeven;
4732 case LibFunc_pow:
4733 case LibFunc_powf:
4734 case LibFunc_powl:
4735 return Intrinsic::pow;
4736 case LibFunc_sqrt:
4737 case LibFunc_sqrtf:
4738 case LibFunc_sqrtl:
4739 return Intrinsic::sqrt;
4740 }
4741
4743}
4744
4745/// Given an exploded icmp instruction, return true if the comparison only
4746/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4747/// the result of the comparison is true when the input value is signed.
4749 bool &TrueIfSigned) {
4750 switch (Pred) {
4751 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4752 TrueIfSigned = true;
4753 return RHS.isZero();
4754 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4755 TrueIfSigned = true;
4756 return RHS.isAllOnes();
4757 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4758 TrueIfSigned = false;
4759 return RHS.isAllOnes();
4760 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4761 TrueIfSigned = false;
4762 return RHS.isZero();
4763 case ICmpInst::ICMP_UGT:
4764 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4765 TrueIfSigned = true;
4766 return RHS.isMaxSignedValue();
4767 case ICmpInst::ICMP_UGE:
4768 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4769 TrueIfSigned = true;
4770 return RHS.isMinSignedValue();
4771 case ICmpInst::ICMP_ULT:
4772 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4773 TrueIfSigned = false;
4774 return RHS.isMinSignedValue();
4775 case ICmpInst::ICMP_ULE:
4776 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4777 TrueIfSigned = false;
4778 return RHS.isMaxSignedValue();
4779 default:
4780 return false;
4781 }
4782}
4783
4785 bool CondIsTrue,
4786 const Instruction *CxtI,
4787 KnownFPClass &KnownFromContext,
4788 unsigned Depth = 0) {
4789 Value *A, *B;
4791 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4792 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4793 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4794 Depth + 1);
4795 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4796 Depth + 1);
4797 return;
4798 }
4800 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4801 Depth + 1);
4802 return;
4803 }
4804 CmpPredicate Pred;
4805 Value *LHS;
4806 uint64_t ClassVal = 0;
4807 const APFloat *CRHS;
4808 const APInt *RHS;
4809 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4810 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4811 Pred, *cast<Instruction>(Cond)->getParent()->getParent(), LHS, *CRHS,
4812 LHS != V);
4813 if (CmpVal == V)
4814 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4816 m_Specific(V), m_ConstantInt(ClassVal)))) {
4817 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4818 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4819 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4820 m_APInt(RHS)))) {
4821 bool TrueIfSigned;
4822 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4823 return;
4824 if (TrueIfSigned == CondIsTrue)
4825 KnownFromContext.signBitMustBeOne();
4826 else
4827 KnownFromContext.signBitMustBeZero();
4828 }
4829}
4830
4832 const SimplifyQuery &Q) {
4833 KnownFPClass KnownFromContext;
4834
4835 if (Q.CC && Q.CC->AffectedValues.contains(V))
4837 KnownFromContext);
4838
4839 if (!Q.CxtI)
4840 return KnownFromContext;
4841
4842 if (Q.DC && Q.DT) {
4843 // Handle dominating conditions.
4844 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4845 Value *Cond = BI->getCondition();
4846
4847 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4848 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4849 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4850 KnownFromContext);
4851
4852 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4853 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4854 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4855 KnownFromContext);
4856 }
4857 }
4858
4859 if (!Q.AC)
4860 return KnownFromContext;
4861
4862 // Try to restrict the floating-point classes based on information from
4863 // assumptions.
4864 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4865 if (!AssumeVH)
4866 continue;
4867 CallInst *I = cast<CallInst>(AssumeVH);
4868
4869 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4870 "Got assumption for the wrong function!");
4871 assert(I->getIntrinsicID() == Intrinsic::assume &&
4872 "must be an assume intrinsic");
4873
4874 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4875 continue;
4876
4877 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4878 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4879 }
4880
4881 return KnownFromContext;
4882}
4883
4885 Value *Arm, bool Invert,
4886 const SimplifyQuery &SQ,
4887 unsigned Depth) {
4888
4889 KnownFPClass KnownSrc;
4891 /*CondIsTrue=*/!Invert, SQ.CxtI, KnownSrc,
4892 Depth + 1);
4893 KnownSrc = KnownSrc.unionWith(Known);
4894 if (KnownSrc.isUnknown())
4895 return;
4896
4897 if (isGuaranteedNotToBeUndef(Arm, SQ.AC, SQ.CxtI, SQ.DT, Depth + 1))
4898 Known = KnownSrc;
4899}
4900
4901void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4902 FPClassTest InterestedClasses, KnownFPClass &Known,
4903 const SimplifyQuery &Q, unsigned Depth);
4904
4905static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4906 FPClassTest InterestedClasses,
4907 const SimplifyQuery &Q, unsigned Depth) {
4908 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4909 APInt DemandedElts =
4910 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4911 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4912}
4913
4915 const APInt &DemandedElts,
4916 FPClassTest InterestedClasses,
4917 KnownFPClass &Known,
4918 const SimplifyQuery &Q,
4919 unsigned Depth) {
4920 if ((InterestedClasses &
4922 return;
4923
4924 KnownFPClass KnownSrc;
4925 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4926 KnownSrc, Q, Depth + 1);
4927
4928 // Sign should be preserved
4929 // TODO: Handle cannot be ordered greater than zero
4930 if (KnownSrc.cannotBeOrderedLessThanZero())
4932
4933 Known.propagateNaN(KnownSrc, true);
4934
4935 // Infinity needs a range check.
4936}
4937
4939 switch (IID) {
4940 case Intrinsic::minimum:
4942 case Intrinsic::maximum:
4944 case Intrinsic::minimumnum:
4946 case Intrinsic::maximumnum:
4948 case Intrinsic::minnum:
4950 case Intrinsic::maxnum:
4952 default:
4953 llvm_unreachable("not a floating-point min-max intrinsic");
4954 }
4955}
4956
4957void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4958 FPClassTest InterestedClasses, KnownFPClass &Known,
4959 const SimplifyQuery &Q, unsigned Depth) {
4960 assert(Known.isUnknown() && "should not be called with known information");
4961
4962 if (!DemandedElts) {
4963 // No demanded elts, better to assume we don't know anything.
4964 Known.resetAll();
4965 return;
4966 }
4967
4968 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4969
4970 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4971 Known = KnownFPClass(CFP->getValueAPF());
4972 return;
4973 }
4974
4976 Known.KnownFPClasses = fcPosZero;
4977 Known.SignBit = false;
4978 return;
4979 }
4980
4981 if (isa<PoisonValue>(V)) {
4982 Known.KnownFPClasses = fcNone;
4983 Known.SignBit = false;
4984 return;
4985 }
4986
4987 // Try to handle fixed width vector constants
4988 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
4989 const Constant *CV = dyn_cast<Constant>(V);
4990 if (VFVTy && CV) {
4991 Known.KnownFPClasses = fcNone;
4992 bool SignBitAllZero = true;
4993 bool SignBitAllOne = true;
4994
4995 // For vectors, verify that each element is not NaN.
4996 unsigned NumElts = VFVTy->getNumElements();
4997 for (unsigned i = 0; i != NumElts; ++i) {
4998 if (!DemandedElts[i])
4999 continue;
5000
5001 Constant *Elt = CV->getAggregateElement(i);
5002 if (!Elt) {
5003 Known = KnownFPClass();
5004 return;
5005 }
5006 if (isa<PoisonValue>(Elt))
5007 continue;
5008 auto *CElt = dyn_cast<ConstantFP>(Elt);
5009 if (!CElt) {
5010 Known = KnownFPClass();
5011 return;
5012 }
5013
5014 const APFloat &C = CElt->getValueAPF();
5015 Known.KnownFPClasses |= C.classify();
5016 if (C.isNegative())
5017 SignBitAllZero = false;
5018 else
5019 SignBitAllOne = false;
5020 }
5021 if (SignBitAllOne != SignBitAllZero)
5022 Known.SignBit = SignBitAllOne;
5023 return;
5024 }
5025
5026 FPClassTest KnownNotFromFlags = fcNone;
5027 if (const auto *CB = dyn_cast<CallBase>(V))
5028 KnownNotFromFlags |= CB->getRetNoFPClass();
5029 else if (const auto *Arg = dyn_cast<Argument>(V))
5030 KnownNotFromFlags |= Arg->getNoFPClass();
5031
5032 const Operator *Op = dyn_cast<Operator>(V);
5034 if (FPOp->hasNoNaNs())
5035 KnownNotFromFlags |= fcNan;
5036 if (FPOp->hasNoInfs())
5037 KnownNotFromFlags |= fcInf;
5038 }
5039
5040 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5041 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5042
5043 // We no longer need to find out about these bits from inputs if we can
5044 // assume this from flags/attributes.
5045 InterestedClasses &= ~KnownNotFromFlags;
5046
5047 llvm::scope_exit ClearClassesFromFlags([=, &Known] {
5048 Known.knownNot(KnownNotFromFlags);
5049 if (!Known.SignBit && AssumedClasses.SignBit) {
5050 if (*AssumedClasses.SignBit)
5051 Known.signBitMustBeOne();
5052 else
5053 Known.signBitMustBeZero();
5054 }
5055 });
5056
5057 if (!Op)
5058 return;
5059
5060 // All recursive calls that increase depth must come after this.
5062 return;
5063
5064 const unsigned Opc = Op->getOpcode();
5065 switch (Opc) {
5066 case Instruction::FNeg: {
5067 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5068 Known, Q, Depth + 1);
5069 Known.fneg();
5070 break;
5071 }
5072 case Instruction::Select: {
5073 auto ComputeForArm = [&](Value *Arm, bool Invert) {
5074 KnownFPClass Res;
5075 computeKnownFPClass(Arm, DemandedElts, InterestedClasses, Res, Q,
5076 Depth + 1);
5077 adjustKnownFPClassForSelectArm(Res, Op->getOperand(0), Arm, Invert, Q,
5078 Depth);
5079 return Res;
5080 };
5081 // Only known if known in both the LHS and RHS.
5082 Known =
5083 ComputeForArm(Op->getOperand(1), /*Invert=*/false)
5084 .intersectWith(ComputeForArm(Op->getOperand(2), /*Invert=*/true));
5085 break;
5086 }
5087 case Instruction::Call: {
5088 const CallInst *II = cast<CallInst>(Op);
5089 const Intrinsic::ID IID = II->getIntrinsicID();
5090 switch (IID) {
5091 case Intrinsic::fabs: {
5092 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5093 // If we only care about the sign bit we don't need to inspect the
5094 // operand.
5095 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5096 InterestedClasses, Known, Q, Depth + 1);
5097 }
5098
5099 Known.fabs();
5100 break;
5101 }
5102 case Intrinsic::copysign: {
5103 KnownFPClass KnownSign;
5104
5105 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5106 Known, Q, Depth + 1);
5107 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5108 KnownSign, Q, Depth + 1);
5109 Known.copysign(KnownSign);
5110 break;
5111 }
5112 case Intrinsic::fma:
5113 case Intrinsic::fmuladd: {
5114 if ((InterestedClasses & fcNegative) == fcNone)
5115 break;
5116
5117 if (II->getArgOperand(0) != II->getArgOperand(1) ||
5118 !isGuaranteedNotToBeUndef(II->getArgOperand(0), Q.AC, Q.CxtI, Q.DT,
5119 Depth + 1))
5120 break;
5121
5122 // The multiply cannot be -0 and therefore the add can't be -0
5123 Known.knownNot(fcNegZero);
5124
5125 // x * x + y is non-negative if y is non-negative.
5126 KnownFPClass KnownAddend;
5127 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
5128 KnownAddend, Q, Depth + 1);
5129
5130 if (KnownAddend.cannotBeOrderedLessThanZero())
5131 Known.knownNot(fcNegative);
5132 break;
5133 }
5134 case Intrinsic::sqrt:
5135 case Intrinsic::experimental_constrained_sqrt: {
5136 KnownFPClass KnownSrc;
5137 FPClassTest InterestedSrcs = InterestedClasses;
5138 if (InterestedClasses & fcNan)
5139 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5140
5141 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5142 KnownSrc, Q, Depth + 1);
5143
5145
5146 bool HasNSZ = Q.IIQ.hasNoSignedZeros(II);
5147 if (!HasNSZ) {
5148 const Function *F = II->getFunction();
5149 const fltSemantics &FltSem =
5150 II->getType()->getScalarType()->getFltSemantics();
5151 Mode = F ? F->getDenormalMode(FltSem) : DenormalMode::getDynamic();
5152 }
5153
5154 Known = KnownFPClass::sqrt(KnownSrc, Mode);
5155 if (HasNSZ)
5156 Known.knownNot(fcNegZero);
5157
5158 break;
5159 }
5160 case Intrinsic::sin:
5161 case Intrinsic::cos: {
5162 // Return NaN on infinite inputs.
5163 KnownFPClass KnownSrc;
5164 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5165 KnownSrc, Q, Depth + 1);
5166 Known.knownNot(fcInf);
5167 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
5168 Known.knownNot(fcNan);
5169 break;
5170 }
5171 case Intrinsic::maxnum:
5172 case Intrinsic::minnum:
5173 case Intrinsic::minimum:
5174 case Intrinsic::maximum:
5175 case Intrinsic::minimumnum:
5176 case Intrinsic::maximumnum: {
5177 KnownFPClass KnownLHS, KnownRHS;
5178 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5179 KnownLHS, Q, Depth + 1);
5180 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5181 KnownRHS, Q, Depth + 1);
5182
5183 const Function *F = II->getFunction();
5184
5186 F ? F->getDenormalMode(
5187 II->getType()->getScalarType()->getFltSemantics())
5189
5190 Known = KnownFPClass::minMaxLike(KnownLHS, KnownRHS, getMinMaxKind(IID),
5191 Mode);
5192 break;
5193 }
5194 case Intrinsic::canonicalize: {
5195 KnownFPClass KnownSrc;
5196 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5197 KnownSrc, Q, Depth + 1);
5198
5199 const Function *F = II->getFunction();
5200 DenormalMode DenormMode =
5201 F ? F->getDenormalMode(
5202 II->getType()->getScalarType()->getFltSemantics())
5204 Known = KnownFPClass::canonicalize(KnownSrc, DenormMode);
5205 break;
5206 }
5207 case Intrinsic::vector_reduce_fmax:
5208 case Intrinsic::vector_reduce_fmin:
5209 case Intrinsic::vector_reduce_fmaximum:
5210 case Intrinsic::vector_reduce_fminimum: {
5211 // reduce min/max will choose an element from one of the vector elements,
5212 // so we can infer and class information that is common to all elements.
5213 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5214 InterestedClasses, Q, Depth + 1);
5215 // Can only propagate sign if output is never NaN.
5216 if (!Known.isKnownNeverNaN())
5217 Known.SignBit.reset();
5218 break;
5219 }
5220 // reverse preserves all characteristics of the input vec's element.
5221 case Intrinsic::vector_reverse:
5222 Known = computeKnownFPClass(
5223 II->getArgOperand(0), DemandedElts.reverseBits(),
5224 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5225 break;
5226 case Intrinsic::trunc:
5227 case Intrinsic::floor:
5228 case Intrinsic::ceil:
5229 case Intrinsic::rint:
5230 case Intrinsic::nearbyint:
5231 case Intrinsic::round:
5232 case Intrinsic::roundeven: {
5233 KnownFPClass KnownSrc;
5234 FPClassTest InterestedSrcs = InterestedClasses;
5235 if (InterestedSrcs & fcPosFinite)
5236 InterestedSrcs |= fcPosFinite;
5237 if (InterestedSrcs & fcNegFinite)
5238 InterestedSrcs |= fcNegFinite;
5239 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5240 KnownSrc, Q, Depth + 1);
5241
5243 KnownSrc, IID == Intrinsic::trunc,
5244 V->getType()->getScalarType()->isMultiUnitFPType());
5245 break;
5246 }
5247 case Intrinsic::exp:
5248 case Intrinsic::exp2:
5249 case Intrinsic::exp10:
5250 case Intrinsic::amdgcn_exp2: {
5251 KnownFPClass KnownSrc;
5252 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5253 KnownSrc, Q, Depth + 1);
5254
5255 Known = KnownFPClass::exp(KnownSrc);
5256
5257 Type *EltTy = II->getType()->getScalarType();
5258 if (IID == Intrinsic::amdgcn_exp2 && EltTy->isFloatTy())
5259 Known.knownNot(fcSubnormal);
5260
5261 break;
5262 }
5263 case Intrinsic::fptrunc_round: {
5264 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5265 Q, Depth);
5266 break;
5267 }
5268 case Intrinsic::log:
5269 case Intrinsic::log10:
5270 case Intrinsic::log2:
5271 case Intrinsic::experimental_constrained_log:
5272 case Intrinsic::experimental_constrained_log10:
5273 case Intrinsic::experimental_constrained_log2:
5274 case Intrinsic::amdgcn_log: {
5275 Type *EltTy = II->getType()->getScalarType();
5276
5277 // log(+inf) -> +inf
5278 // log([+-]0.0) -> -inf
5279 // log(-inf) -> nan
5280 // log(-x) -> nan
5281 if ((InterestedClasses & (fcNan | fcInf)) != fcNone) {
5282 FPClassTest InterestedSrcs = InterestedClasses;
5283 if ((InterestedClasses & fcNegInf) != fcNone)
5284 InterestedSrcs |= fcZero | fcSubnormal;
5285 if ((InterestedClasses & fcNan) != fcNone)
5286 InterestedSrcs |= fcNan | fcNegative;
5287
5288 KnownFPClass KnownSrc;
5289 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5290 KnownSrc, Q, Depth + 1);
5291
5292 const Function *F = II->getFunction();
5293 DenormalMode Mode = F ? F->getDenormalMode(EltTy->getFltSemantics())
5295 Known = KnownFPClass::log(KnownSrc, Mode);
5296 }
5297
5298 if (IID == Intrinsic::amdgcn_log && EltTy->isFloatTy())
5299 Known.knownNot(fcSubnormal);
5300 break;
5301 }
5302 case Intrinsic::powi: {
5303 if ((InterestedClasses & fcNegative) == fcNone)
5304 break;
5305
5306 const Value *Exp = II->getArgOperand(1);
5307 Type *ExpTy = Exp->getType();
5308 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5309 KnownBits ExponentKnownBits(BitWidth);
5310 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5311 ExponentKnownBits, Q, Depth + 1);
5312
5313 if (ExponentKnownBits.Zero[0]) { // Is even
5314 Known.knownNot(fcNegative);
5315 break;
5316 }
5317
5318 // Given that exp is an integer, here are the
5319 // ways that pow can return a negative value:
5320 //
5321 // pow(-x, exp) --> negative if exp is odd and x is negative.
5322 // pow(-0, exp) --> -inf if exp is negative odd.
5323 // pow(-0, exp) --> -0 if exp is positive odd.
5324 // pow(-inf, exp) --> -0 if exp is negative odd.
5325 // pow(-inf, exp) --> -inf if exp is positive odd.
5326 KnownFPClass KnownSrc;
5327 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5328 KnownSrc, Q, Depth + 1);
5329 if (KnownSrc.isKnownNever(fcNegative))
5330 Known.knownNot(fcNegative);
5331 break;
5332 }
5333 case Intrinsic::ldexp: {
5334 KnownFPClass KnownSrc;
5335 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5336 KnownSrc, Q, Depth + 1);
5337 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5338
5339 // Sign is preserved, but underflows may produce zeroes.
5340 if (KnownSrc.isKnownNever(fcNegative))
5341 Known.knownNot(fcNegative);
5342 else if (KnownSrc.cannotBeOrderedLessThanZero())
5344
5345 if (KnownSrc.isKnownNever(fcPositive))
5346 Known.knownNot(fcPositive);
5347 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5349
5350 // Can refine inf/zero handling based on the exponent operand.
5351 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5352 if ((InterestedClasses & ExpInfoMask) == fcNone)
5353 break;
5354 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5355 break;
5356
5357 const fltSemantics &Flt =
5358 II->getType()->getScalarType()->getFltSemantics();
5359 unsigned Precision = APFloat::semanticsPrecision(Flt);
5360 const Value *ExpArg = II->getArgOperand(1);
5362 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5363
5364 const int MantissaBits = Precision - 1;
5365 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5366 Known.knownNot(fcSubnormal);
5367
5368 const Function *F = II->getFunction();
5369 const APInt *ConstVal = ExpRange.getSingleElement();
5370 const fltSemantics &FltSem =
5371 II->getType()->getScalarType()->getFltSemantics();
5372 if (ConstVal && ConstVal->isZero()) {
5373 // ldexp(x, 0) -> x, so propagate everything.
5374 Known.propagateCanonicalizingSrc(KnownSrc, F->getDenormalMode(FltSem));
5375 } else if (ExpRange.isAllNegative()) {
5376 // If we know the power is <= 0, can't introduce inf
5377 if (KnownSrc.isKnownNeverPosInfinity())
5378 Known.knownNot(fcPosInf);
5379 if (KnownSrc.isKnownNeverNegInfinity())
5380 Known.knownNot(fcNegInf);
5381 } else if (ExpRange.isAllNonNegative()) {
5382 // If we know the power is >= 0, can't introduce subnormal or zero
5383 if (KnownSrc.isKnownNeverPosSubnormal())
5384 Known.knownNot(fcPosSubnormal);
5385 if (KnownSrc.isKnownNeverNegSubnormal())
5386 Known.knownNot(fcNegSubnormal);
5387 if (F &&
5388 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5389 Known.knownNot(fcPosZero);
5390 if (F &&
5391 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5392 Known.knownNot(fcNegZero);
5393 }
5394
5395 break;
5396 }
5397 case Intrinsic::arithmetic_fence: {
5398 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5399 Known, Q, Depth + 1);
5400 break;
5401 }
5402 case Intrinsic::experimental_constrained_sitofp:
5403 case Intrinsic::experimental_constrained_uitofp:
5404 // Cannot produce nan
5405 Known.knownNot(fcNan);
5406
5407 // sitofp and uitofp turn into +0.0 for zero.
5408 Known.knownNot(fcNegZero);
5409
5410 // Integers cannot be subnormal
5411 Known.knownNot(fcSubnormal);
5412
5413 if (IID == Intrinsic::experimental_constrained_uitofp)
5414 Known.signBitMustBeZero();
5415
5416 // TODO: Copy inf handling from instructions
5417 break;
5418 case Intrinsic::amdgcn_rcp: {
5419 KnownFPClass KnownSrc;
5420 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5421 KnownSrc, Q, Depth + 1);
5422
5423 Known.propagateNaN(KnownSrc);
5424
5425 Type *EltTy = II->getType()->getScalarType();
5426
5427 // f32 denormal always flushed.
5428 if (EltTy->isFloatTy()) {
5429 Known.knownNot(fcSubnormal);
5430 KnownSrc.knownNot(fcSubnormal);
5431 }
5432
5433 if (KnownSrc.isKnownNever(fcNegative))
5434 Known.knownNot(fcNegative);
5435 if (KnownSrc.isKnownNever(fcPositive))
5436 Known.knownNot(fcPositive);
5437
5438 if (const Function *F = II->getFunction()) {
5439 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5440 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5441 Known.knownNot(fcPosInf);
5442 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5443 Known.knownNot(fcNegInf);
5444 }
5445
5446 break;
5447 }
5448 case Intrinsic::amdgcn_rsq: {
5449 KnownFPClass KnownSrc;
5450 // The only negative value that can be returned is -inf for -0 inputs.
5452
5453 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5454 KnownSrc, Q, Depth + 1);
5455
5456 // Negative -> nan
5457 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5458 Known.knownNot(fcNan);
5459 else if (KnownSrc.isKnownNever(fcSNan))
5460 Known.knownNot(fcSNan);
5461
5462 // +inf -> +0
5463 if (KnownSrc.isKnownNeverPosInfinity())
5464 Known.knownNot(fcPosZero);
5465
5466 Type *EltTy = II->getType()->getScalarType();
5467
5468 // f32 denormal always flushed.
5469 if (EltTy->isFloatTy())
5470 Known.knownNot(fcPosSubnormal);
5471
5472 if (const Function *F = II->getFunction()) {
5473 DenormalMode Mode = F->getDenormalMode(EltTy->getFltSemantics());
5474
5475 // -0 -> -inf
5476 if (KnownSrc.isKnownNeverLogicalNegZero(Mode))
5477 Known.knownNot(fcNegInf);
5478
5479 // +0 -> +inf
5480 if (KnownSrc.isKnownNeverLogicalPosZero(Mode))
5481 Known.knownNot(fcPosInf);
5482 }
5483
5484 break;
5485 }
5486 default:
5487 break;
5488 }
5489
5490 break;
5491 }
5492 case Instruction::FAdd:
5493 case Instruction::FSub: {
5494 KnownFPClass KnownLHS, KnownRHS;
5495 bool WantNegative =
5496 Op->getOpcode() == Instruction::FAdd &&
5497 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5498 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5499 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5500
5501 if (!WantNaN && !WantNegative && !WantNegZero)
5502 break;
5503
5504 FPClassTest InterestedSrcs = InterestedClasses;
5505 if (WantNegative)
5506 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5507 if (InterestedClasses & fcNan)
5508 InterestedSrcs |= fcInf;
5509 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5510 KnownRHS, Q, Depth + 1);
5511
5512 // Special case fadd x, x, which is the canonical form of fmul x, 2.
5513 bool SelfAdd = Op->getOperand(0) == Op->getOperand(1) &&
5514 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI,
5515 Q.DT, Depth + 1);
5516 if (SelfAdd)
5517 KnownLHS = KnownRHS;
5518
5519 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5520 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5521 WantNegZero || Opc == Instruction::FSub) {
5522
5523 if (!SelfAdd) {
5524 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5525 // there's no point.
5526 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5527 KnownLHS, Q, Depth + 1);
5528 }
5529
5530 // Adding positive and negative infinity produces NaN.
5531 // TODO: Check sign of infinities.
5532 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5533 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5534 Known.knownNot(fcNan);
5535
5536 // FIXME: Context function should always be passed in separately
5537 const Function *F = cast<Instruction>(Op)->getFunction();
5538
5539 if (Op->getOpcode() == Instruction::FAdd) {
5540 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5541 KnownRHS.cannotBeOrderedLessThanZero())
5543 if (KnownLHS.cannotBeOrderedGreaterThanZero() &&
5546
5547 if (!F)
5548 break;
5549
5550 const fltSemantics &FltSem =
5551 Op->getType()->getScalarType()->getFltSemantics();
5552 DenormalMode Mode = F->getDenormalMode(FltSem);
5553
5554 // Doubling 0 will give the same 0.
5555 if (SelfAdd && KnownRHS.isKnownNeverLogicalPosZero(Mode) &&
5556 (Mode.Output == DenormalMode::IEEE ||
5557 (Mode.Output == DenormalMode::PreserveSign &&
5558 KnownRHS.isKnownNeverPosSubnormal()) ||
5559 (Mode.Output == DenormalMode::PositiveZero &&
5560 KnownRHS.isKnownNeverSubnormal())))
5561 Known.knownNot(fcPosZero);
5562
5563 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5564 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5565 KnownRHS.isKnownNeverLogicalNegZero(Mode)) &&
5566 // Make sure output negative denormal can't flush to -0
5567 (Mode.Output == DenormalMode::IEEE ||
5569 Known.knownNot(fcNegZero);
5570 } else {
5571 if (!F)
5572 break;
5573
5574 const fltSemantics &FltSem =
5575 Op->getType()->getScalarType()->getFltSemantics();
5576 DenormalMode Mode = F->getDenormalMode(FltSem);
5577
5578 // Only fsub -0, +0 can return -0
5579 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5580 KnownRHS.isKnownNeverLogicalPosZero(Mode)) &&
5581 // Make sure output negative denormal can't flush to -0
5582 (Mode.Output == DenormalMode::IEEE ||
5584 Known.knownNot(fcNegZero);
5585 }
5586 }
5587
5588 break;
5589 }
5590 case Instruction::FMul: {
5591 const Function *F = cast<Instruction>(Op)->getFunction();
5593 F ? F->getDenormalMode(
5594 Op->getType()->getScalarType()->getFltSemantics())
5596
5597 // X * X is always non-negative or a NaN.
5598 if (Op->getOperand(0) == Op->getOperand(1) &&
5599 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
5600 KnownFPClass KnownSrc;
5601 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownSrc,
5602 Q, Depth + 1);
5603 Known = KnownFPClass::square(KnownSrc, Mode);
5604 break;
5605 }
5606
5607 KnownFPClass KnownLHS, KnownRHS;
5608
5609 bool CannotBeSubnormal = false;
5610 const APFloat *CRHS;
5611 if (match(Op->getOperand(1), m_APFloat(CRHS))) {
5612 // Match denormal scaling pattern, similar to the case in ldexp. If the
5613 // constant's exponent is sufficiently large, the result cannot be
5614 // subnormal.
5615
5616 // TODO: Should do general ConstantFPRange analysis.
5617 const fltSemantics &Flt =
5618 Op->getType()->getScalarType()->getFltSemantics();
5619 unsigned Precision = APFloat::semanticsPrecision(Flt);
5620 const int MantissaBits = Precision - 1;
5621
5622 int MinKnownExponent = ilogb(*CRHS);
5623 if (MinKnownExponent >= MantissaBits)
5624 CannotBeSubnormal = true;
5625
5626 KnownRHS = KnownFPClass(*CRHS);
5627 } else {
5628 computeKnownFPClass(Op->getOperand(1), DemandedElts, fcAllFlags, KnownRHS,
5629 Q, Depth + 1);
5630 }
5631
5632 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5633 Q, Depth + 1);
5634
5635 Known = KnownFPClass::fmul(KnownLHS, KnownRHS, Mode);
5636 if (CannotBeSubnormal)
5637 Known.knownNot(fcSubnormal);
5638 break;
5639 }
5640 case Instruction::FDiv:
5641 case Instruction::FRem: {
5642 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5643
5644 if (Op->getOperand(0) == Op->getOperand(1) &&
5645 isGuaranteedNotToBeUndef(Op->getOperand(0), Q.AC, Q.CxtI, Q.DT)) {
5646 if (Op->getOpcode() == Instruction::FDiv) {
5647 // X / X is always exactly 1.0 or a NaN.
5649 } else {
5650 // X % X is always exactly [+-]0.0 or a NaN.
5651 Known.KnownFPClasses = fcNan | fcZero;
5652 }
5653
5654 if (!WantNan)
5655 break;
5656
5657 KnownFPClass KnownSrc;
5658 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5659 fcNan | fcInf | fcZero | fcSubnormal, KnownSrc, Q,
5660 Depth + 1);
5661 const Function *F = cast<Instruction>(Op)->getFunction();
5662 const fltSemantics &FltSem =
5663 Op->getType()->getScalarType()->getFltSemantics();
5664
5665 if (KnownSrc.isKnownNeverInfOrNaN() &&
5666 KnownSrc.isKnownNeverLogicalZero(F ? F->getDenormalMode(FltSem)
5668 Known.knownNot(fcNan);
5669 else if (KnownSrc.isKnownNever(fcSNan))
5670 Known.knownNot(fcSNan);
5671
5672 break;
5673 }
5674
5675 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5676 const bool WantPositive =
5677 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5678 if (!WantNan && !WantNegative && !WantPositive)
5679 break;
5680
5681 KnownFPClass KnownLHS, KnownRHS;
5682
5683 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5684 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5685 Depth + 1);
5686
5687 bool KnowSomethingUseful = KnownRHS.isKnownNeverNaN() ||
5688 KnownRHS.isKnownNever(fcNegative) ||
5689 KnownRHS.isKnownNever(fcPositive);
5690
5691 if (KnowSomethingUseful || WantPositive) {
5692 computeKnownFPClass(Op->getOperand(0), DemandedElts, fcAllFlags, KnownLHS,
5693 Q, Depth + 1);
5694 }
5695
5696 const Function *F = cast<Instruction>(Op)->getFunction();
5697 const fltSemantics &FltSem =
5698 Op->getType()->getScalarType()->getFltSemantics();
5699
5700 if (Op->getOpcode() == Instruction::FDiv) {
5701 // Only 0/0, Inf/Inf produce NaN.
5702 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5703 (KnownLHS.isKnownNeverInfinity() ||
5704 KnownRHS.isKnownNeverInfinity()) &&
5705 ((F &&
5706 KnownLHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) ||
5707 (F &&
5708 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))))) {
5709 Known.knownNot(fcNan);
5710 }
5711
5712 // xor sign bit.
5713 // X / -0.0 is -Inf (or NaN).
5714 // +X / +X is +X
5715 if ((KnownLHS.isKnownNever(fcNegative) &&
5716 KnownRHS.isKnownNever(fcNegative)) ||
5717 (KnownLHS.isKnownNever(fcPositive) &&
5718 KnownRHS.isKnownNever(fcPositive)))
5719 Known.knownNot(fcNegative);
5720
5721 if ((KnownLHS.isKnownNever(fcPositive) &&
5722 KnownRHS.isKnownNever(fcNegative)) ||
5723 (KnownLHS.isKnownNever(fcNegative) &&
5724 KnownRHS.isKnownNever(fcPositive)))
5725 Known.knownNot(fcPositive);
5726
5727 // 0 / x => 0 or nan
5728 if (KnownLHS.isKnownAlways(fcZero))
5730
5731 // x / 0 => nan or inf
5732 if (KnownRHS.isKnownAlways(fcZero))
5733 Known.knownNot(fcFinite);
5734 } else {
5735 // Inf REM x and x REM 0 produce NaN.
5736 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5737 KnownLHS.isKnownNeverInfinity() && F &&
5738 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5739 Known.knownNot(fcNan);
5740 }
5741
5742 // The sign for frem is the same as the first operand.
5743 if (KnownLHS.cannotBeOrderedLessThanZero())
5745 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5747
5748 // See if we can be more aggressive about the sign of 0.
5749 if (KnownLHS.isKnownNever(fcNegative))
5750 Known.knownNot(fcNegative);
5751 if (KnownLHS.isKnownNever(fcPositive))
5752 Known.knownNot(fcPositive);
5753 }
5754
5755 break;
5756 }
5757 case Instruction::FPExt: {
5758 KnownFPClass KnownSrc;
5759 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5760 KnownSrc, Q, Depth + 1);
5761
5762 const fltSemantics &DstTy =
5763 Op->getType()->getScalarType()->getFltSemantics();
5764 const fltSemantics &SrcTy =
5765 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5766
5767 Known = KnownFPClass::fpext(KnownSrc, DstTy, SrcTy);
5768 break;
5769 }
5770 case Instruction::FPTrunc: {
5771 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5772 Depth);
5773 break;
5774 }
5775 case Instruction::SIToFP:
5776 case Instruction::UIToFP: {
5777 // Cannot produce nan
5778 Known.knownNot(fcNan);
5779
5780 // Integers cannot be subnormal
5781 Known.knownNot(fcSubnormal);
5782
5783 // sitofp and uitofp turn into +0.0 for zero.
5784 Known.knownNot(fcNegZero);
5785 if (Op->getOpcode() == Instruction::UIToFP)
5786 Known.signBitMustBeZero();
5787
5788 if (InterestedClasses & fcInf) {
5789 // Get width of largest magnitude integer (remove a bit if signed).
5790 // This still works for a signed minimum value because the largest FP
5791 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5792 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5793 if (Op->getOpcode() == Instruction::SIToFP)
5794 --IntSize;
5795
5796 // If the exponent of the largest finite FP value can hold the largest
5797 // integer, the result of the cast must be finite.
5798 Type *FPTy = Op->getType()->getScalarType();
5799 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5800 Known.knownNot(fcInf);
5801 }
5802
5803 break;
5804 }
5805 case Instruction::ExtractElement: {
5806 // Look through extract element. If the index is non-constant or
5807 // out-of-range demand all elements, otherwise just the extracted element.
5808 const Value *Vec = Op->getOperand(0);
5809
5810 APInt DemandedVecElts;
5811 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5812 unsigned NumElts = VecTy->getNumElements();
5813 DemandedVecElts = APInt::getAllOnes(NumElts);
5814 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5815 if (CIdx && CIdx->getValue().ult(NumElts))
5816 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5817 } else {
5818 DemandedVecElts = APInt(1, 1);
5819 }
5820
5821 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5822 Q, Depth + 1);
5823 }
5824 case Instruction::InsertElement: {
5825 if (isa<ScalableVectorType>(Op->getType()))
5826 return;
5827
5828 const Value *Vec = Op->getOperand(0);
5829 const Value *Elt = Op->getOperand(1);
5830 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5831 unsigned NumElts = DemandedElts.getBitWidth();
5832 APInt DemandedVecElts = DemandedElts;
5833 bool NeedsElt = true;
5834 // If we know the index we are inserting to, clear it from Vec check.
5835 if (CIdx && CIdx->getValue().ult(NumElts)) {
5836 DemandedVecElts.clearBit(CIdx->getZExtValue());
5837 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5838 }
5839
5840 // Do we demand the inserted element?
5841 if (NeedsElt) {
5842 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5843 // If we don't know any bits, early out.
5844 if (Known.isUnknown())
5845 break;
5846 } else {
5847 Known.KnownFPClasses = fcNone;
5848 }
5849
5850 // Do we need anymore elements from Vec?
5851 if (!DemandedVecElts.isZero()) {
5852 KnownFPClass Known2;
5853 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5854 Depth + 1);
5855 Known |= Known2;
5856 }
5857
5858 break;
5859 }
5860 case Instruction::ShuffleVector: {
5861 // Handle vector splat idiom
5862 if (Value *Splat = getSplatValue(V)) {
5863 computeKnownFPClass(Splat, Known, InterestedClasses, Q, Depth + 1);
5864 break;
5865 }
5866
5867 // For undef elements, we don't know anything about the common state of
5868 // the shuffle result.
5869 APInt DemandedLHS, DemandedRHS;
5870 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5871 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5872 return;
5873
5874 if (!!DemandedLHS) {
5875 const Value *LHS = Shuf->getOperand(0);
5876 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5877 Depth + 1);
5878
5879 // If we don't know any bits, early out.
5880 if (Known.isUnknown())
5881 break;
5882 } else {
5883 Known.KnownFPClasses = fcNone;
5884 }
5885
5886 if (!!DemandedRHS) {
5887 KnownFPClass Known2;
5888 const Value *RHS = Shuf->getOperand(1);
5889 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5890 Depth + 1);
5891 Known |= Known2;
5892 }
5893
5894 break;
5895 }
5896 case Instruction::ExtractValue: {
5897 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5898 ArrayRef<unsigned> Indices = Extract->getIndices();
5899 const Value *Src = Extract->getAggregateOperand();
5900 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5901 Indices[0] == 0) {
5902 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5903 switch (II->getIntrinsicID()) {
5904 case Intrinsic::frexp: {
5905 Known.knownNot(fcSubnormal);
5906
5907 KnownFPClass KnownSrc;
5908 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5909 InterestedClasses, KnownSrc, Q, Depth + 1);
5910
5911 const Function *F = cast<Instruction>(Op)->getFunction();
5912 const fltSemantics &FltSem =
5913 Op->getType()->getScalarType()->getFltSemantics();
5914
5915 if (KnownSrc.isKnownNever(fcNegative))
5916 Known.knownNot(fcNegative);
5917 else {
5918 if (F &&
5919 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5920 Known.knownNot(fcNegZero);
5921 if (KnownSrc.isKnownNever(fcNegInf))
5922 Known.knownNot(fcNegInf);
5923 }
5924
5925 if (KnownSrc.isKnownNever(fcPositive))
5926 Known.knownNot(fcPositive);
5927 else {
5928 if (F &&
5929 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5930 Known.knownNot(fcPosZero);
5931 if (KnownSrc.isKnownNever(fcPosInf))
5932 Known.knownNot(fcPosInf);
5933 }
5934
5935 Known.propagateNaN(KnownSrc);
5936 return;
5937 }
5938 default:
5939 break;
5940 }
5941 }
5942 }
5943
5944 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
5945 Depth + 1);
5946 break;
5947 }
5948 case Instruction::PHI: {
5949 const PHINode *P = cast<PHINode>(Op);
5950 // Unreachable blocks may have zero-operand PHI nodes.
5951 if (P->getNumIncomingValues() == 0)
5952 break;
5953
5954 // Otherwise take the unions of the known bit sets of the operands,
5955 // taking conservative care to avoid excessive recursion.
5956 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5957
5958 if (Depth < PhiRecursionLimit) {
5959 // Skip if every incoming value references to ourself.
5960 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5961 break;
5962
5963 bool First = true;
5964
5965 for (const Use &U : P->operands()) {
5966 Value *IncValue;
5967 Instruction *CxtI;
5968 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
5969 // Skip direct self references.
5970 if (IncValue == P)
5971 continue;
5972
5973 KnownFPClass KnownSrc;
5974 // Recurse, but cap the recursion to two levels, because we don't want
5975 // to waste time spinning around in loops. We need at least depth 2 to
5976 // detect known sign bits.
5977 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
5979 PhiRecursionLimit);
5980
5981 if (First) {
5982 Known = KnownSrc;
5983 First = false;
5984 } else {
5985 Known |= KnownSrc;
5986 }
5987
5988 if (Known.KnownFPClasses == fcAllFlags)
5989 break;
5990 }
5991 }
5992
5993 break;
5994 }
5995 case Instruction::BitCast: {
5996 const Value *Src;
5997 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
5998 !Src->getType()->isIntOrIntVectorTy())
5999 break;
6000
6001 const Type *Ty = Op->getType()->getScalarType();
6002 KnownBits Bits(Ty->getScalarSizeInBits());
6003 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
6004
6005 // Transfer information from the sign bit.
6006 if (Bits.isNonNegative())
6007 Known.signBitMustBeZero();
6008 else if (Bits.isNegative())
6009 Known.signBitMustBeOne();
6010
6011 if (Ty->isIEEELikeFPTy()) {
6012 // IEEE floats are NaN when all bits of the exponent plus at least one of
6013 // the fraction bits are 1. This means:
6014 // - If we assume unknown bits are 0 and the value is NaN, it will
6015 // always be NaN
6016 // - If we assume unknown bits are 1 and the value is not NaN, it can
6017 // never be NaN
6018 // Note: They do not hold for x86_fp80 format.
6019 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
6020 Known.KnownFPClasses = fcNan;
6021 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
6022 Known.knownNot(fcNan);
6023
6024 // Build KnownBits representing Inf and check if it must be equal or
6025 // unequal to this value.
6026 auto InfKB = KnownBits::makeConstant(
6027 APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());
6028 InfKB.Zero.clearSignBit();
6029 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
6030 assert(!InfResult.value());
6031 Known.knownNot(fcInf);
6032 } else if (Bits == InfKB) {
6033 Known.KnownFPClasses = fcInf;
6034 }
6035
6036 // Build KnownBits representing Zero and check if it must be equal or
6037 // unequal to this value.
6038 auto ZeroKB = KnownBits::makeConstant(
6039 APFloat::getZero(Ty->getFltSemantics()).bitcastToAPInt());
6040 ZeroKB.Zero.clearSignBit();
6041 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
6042 assert(!ZeroResult.value());
6043 Known.knownNot(fcZero);
6044 } else if (Bits == ZeroKB) {
6045 Known.KnownFPClasses = fcZero;
6046 }
6047 }
6048
6049 break;
6050 }
6051 default:
6052 break;
6053 }
6054}
6055
6057 const APInt &DemandedElts,
6058 FPClassTest InterestedClasses,
6059 const SimplifyQuery &SQ,
6060 unsigned Depth) {
6061 KnownFPClass KnownClasses;
6062 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6063 Depth);
6064 return KnownClasses;
6065}
6066
6068 FPClassTest InterestedClasses,
6069 const SimplifyQuery &SQ,
6070 unsigned Depth) {
6071 KnownFPClass Known;
6072 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6073 return Known;
6074}
6075
6077 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6078 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6079 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6080 return computeKnownFPClass(V, InterestedClasses,
6081 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6082 Depth);
6083}
6084
6086llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6087 FastMathFlags FMF, FPClassTest InterestedClasses,
6088 const SimplifyQuery &SQ, unsigned Depth) {
6089 if (FMF.noNaNs())
6090 InterestedClasses &= ~fcNan;
6091 if (FMF.noInfs())
6092 InterestedClasses &= ~fcInf;
6093
6094 KnownFPClass Result =
6095 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6096
6097 if (FMF.noNaNs())
6098 Result.KnownFPClasses &= ~fcNan;
6099 if (FMF.noInfs())
6100 Result.KnownFPClasses &= ~fcInf;
6101 return Result;
6102}
6103
6105 FPClassTest InterestedClasses,
6106 const SimplifyQuery &SQ,
6107 unsigned Depth) {
6108 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6109 APInt DemandedElts =
6110 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6111 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6112 Depth);
6113}
6114
6116 unsigned Depth) {
6118 return Known.isKnownNeverNegZero();
6119}
6120
6127
6129 unsigned Depth) {
6131 return Known.isKnownNeverInfinity();
6132}
6133
6134/// Return true if the floating-point value can never contain a NaN or infinity.
6136 unsigned Depth) {
6138 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6139}
6140
6141/// Return true if the floating-point scalar value is not a NaN or if the
6142/// floating-point vector value has no NaN elements. Return false if a value
6143/// could ever be NaN.
6145 unsigned Depth) {
6147 return Known.isKnownNeverNaN();
6148}
6149
6150/// Return false if we can prove that the specified FP value's sign bit is 0.
6151/// Return true if we can prove that the specified FP value's sign bit is 1.
6152/// Otherwise return std::nullopt.
6153std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6154 const SimplifyQuery &SQ,
6155 unsigned Depth) {
6157 return Known.SignBit;
6158}
6159
6161 auto *User = cast<Instruction>(U.getUser());
6162 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6163 if (FPOp->hasNoSignedZeros())
6164 return true;
6165 }
6166
6167 switch (User->getOpcode()) {
6168 case Instruction::FPToSI:
6169 case Instruction::FPToUI:
6170 return true;
6171 case Instruction::FCmp:
6172 // fcmp treats both positive and negative zero as equal.
6173 return true;
6174 case Instruction::Call:
6175 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6176 switch (II->getIntrinsicID()) {
6177 case Intrinsic::fabs:
6178 return true;
6179 case Intrinsic::copysign:
6180 return U.getOperandNo() == 0;
6181 case Intrinsic::is_fpclass:
6182 case Intrinsic::vp_is_fpclass: {
6183 auto Test =
6184 static_cast<FPClassTest>(
6185 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6188 }
6189 default:
6190 return false;
6191 }
6192 }
6193 return false;
6194 default:
6195 return false;
6196 }
6197}
6198
6200 auto *User = cast<Instruction>(U.getUser());
6201 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6202 if (FPOp->hasNoNaNs())
6203 return true;
6204 }
6205
6206 switch (User->getOpcode()) {
6207 case Instruction::FPToSI:
6208 case Instruction::FPToUI:
6209 return true;
6210 // Proper FP math operations ignore the sign bit of NaN.
6211 case Instruction::FAdd:
6212 case Instruction::FSub:
6213 case Instruction::FMul:
6214 case Instruction::FDiv:
6215 case Instruction::FRem:
6216 case Instruction::FPTrunc:
6217 case Instruction::FPExt:
6218 case Instruction::FCmp:
6219 return true;
6220 // Bitwise FP operations should preserve the sign bit of NaN.
6221 case Instruction::FNeg:
6222 case Instruction::Select:
6223 case Instruction::PHI:
6224 return false;
6225 case Instruction::Ret:
6226 return User->getFunction()->getAttributes().getRetNoFPClass() &
6228 case Instruction::Call:
6229 case Instruction::Invoke: {
6230 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6231 switch (II->getIntrinsicID()) {
6232 case Intrinsic::fabs:
6233 return true;
6234 case Intrinsic::copysign:
6235 return U.getOperandNo() == 0;
6236 // Other proper FP math intrinsics ignore the sign bit of NaN.
6237 case Intrinsic::maxnum:
6238 case Intrinsic::minnum:
6239 case Intrinsic::maximum:
6240 case Intrinsic::minimum:
6241 case Intrinsic::maximumnum:
6242 case Intrinsic::minimumnum:
6243 case Intrinsic::canonicalize:
6244 case Intrinsic::fma:
6245 case Intrinsic::fmuladd:
6246 case Intrinsic::sqrt:
6247 case Intrinsic::pow:
6248 case Intrinsic::powi:
6249 case Intrinsic::fptoui_sat:
6250 case Intrinsic::fptosi_sat:
6251 case Intrinsic::is_fpclass:
6252 case Intrinsic::vp_is_fpclass:
6253 return true;
6254 default:
6255 return false;
6256 }
6257 }
6258
6259 FPClassTest NoFPClass =
6260 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6261 return NoFPClass & FPClassTest::fcNan;
6262 }
6263 default:
6264 return false;
6265 }
6266}
6267
6269
6270 // All byte-wide stores are splatable, even of arbitrary variables.
6271 if (V->getType()->isIntegerTy(8))
6272 return V;
6273
6274 LLVMContext &Ctx = V->getContext();
6275
6276 // Undef don't care.
6277 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6278 if (isa<UndefValue>(V))
6279 return UndefInt8;
6280
6281 // Return poison for zero-sized type.
6282 if (DL.getTypeStoreSize(V->getType()).isZero())
6283 return PoisonValue::get(Type::getInt8Ty(Ctx));
6284
6286 if (!C) {
6287 // Conceptually, we could handle things like:
6288 // %a = zext i8 %X to i16
6289 // %b = shl i16 %a, 8
6290 // %c = or i16 %a, %b
6291 // but until there is an example that actually needs this, it doesn't seem
6292 // worth worrying about.
6293 return nullptr;
6294 }
6295
6296 // Handle 'null' ConstantArrayZero etc.
6297 if (C->isNullValue())
6299
6300 // Constant floating-point values can be handled as integer values if the
6301 // corresponding integer value is "byteable". An important case is 0.0.
6302 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6303 Type *Ty = nullptr;
6304 if (CFP->getType()->isHalfTy())
6305 Ty = Type::getInt16Ty(Ctx);
6306 else if (CFP->getType()->isFloatTy())
6307 Ty = Type::getInt32Ty(Ctx);
6308 else if (CFP->getType()->isDoubleTy())
6309 Ty = Type::getInt64Ty(Ctx);
6310 // Don't handle long double formats, which have strange constraints.
6311 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6312 : nullptr;
6313 }
6314
6315 // We can handle constant integers that are multiple of 8 bits.
6316 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6317 if (CI->getBitWidth() % 8 == 0) {
6318 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6319 if (!CI->getValue().isSplat(8))
6320 return nullptr;
6321 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6322 }
6323 }
6324
6325 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6326 if (CE->getOpcode() == Instruction::IntToPtr) {
6327 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6328 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6330 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6331 return isBytewiseValue(Op, DL);
6332 }
6333 }
6334 }
6335
6336 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6337 if (LHS == RHS)
6338 return LHS;
6339 if (!LHS || !RHS)
6340 return nullptr;
6341 if (LHS == UndefInt8)
6342 return RHS;
6343 if (RHS == UndefInt8)
6344 return LHS;
6345 return nullptr;
6346 };
6347
6349 Value *Val = UndefInt8;
6350 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6351 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6352 return nullptr;
6353 return Val;
6354 }
6355
6357 Value *Val = UndefInt8;
6358 for (Value *Op : C->operands())
6359 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6360 return nullptr;
6361 return Val;
6362 }
6363
6364 // Don't try to handle the handful of other constants.
6365 return nullptr;
6366}
6367
6368// This is the recursive version of BuildSubAggregate. It takes a few different
6369// arguments. Idxs is the index within the nested struct From that we are
6370// looking at now (which is of type IndexedType). IdxSkip is the number of
6371// indices from Idxs that should be left out when inserting into the resulting
6372// struct. To is the result struct built so far, new insertvalue instructions
6373// build on that.
6374static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6376 unsigned IdxSkip,
6377 BasicBlock::iterator InsertBefore) {
6378 StructType *STy = dyn_cast<StructType>(IndexedType);
6379 if (STy) {
6380 // Save the original To argument so we can modify it
6381 Value *OrigTo = To;
6382 // General case, the type indexed by Idxs is a struct
6383 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6384 // Process each struct element recursively
6385 Idxs.push_back(i);
6386 Value *PrevTo = To;
6387 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6388 InsertBefore);
6389 Idxs.pop_back();
6390 if (!To) {
6391 // Couldn't find any inserted value for this index? Cleanup
6392 while (PrevTo != OrigTo) {
6394 PrevTo = Del->getAggregateOperand();
6395 Del->eraseFromParent();
6396 }
6397 // Stop processing elements
6398 break;
6399 }
6400 }
6401 // If we successfully found a value for each of our subaggregates
6402 if (To)
6403 return To;
6404 }
6405 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6406 // the struct's elements had a value that was inserted directly. In the latter
6407 // case, perhaps we can't determine each of the subelements individually, but
6408 // we might be able to find the complete struct somewhere.
6409
6410 // Find the value that is at that particular spot
6411 Value *V = FindInsertedValue(From, Idxs);
6412
6413 if (!V)
6414 return nullptr;
6415
6416 // Insert the value in the new (sub) aggregate
6417 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6418 InsertBefore);
6419}
6420
6421// This helper takes a nested struct and extracts a part of it (which is again a
6422// struct) into a new value. For example, given the struct:
6423// { a, { b, { c, d }, e } }
6424// and the indices "1, 1" this returns
6425// { c, d }.
6426//
6427// It does this by inserting an insertvalue for each element in the resulting
6428// struct, as opposed to just inserting a single struct. This will only work if
6429// each of the elements of the substruct are known (ie, inserted into From by an
6430// insertvalue instruction somewhere).
6431//
6432// All inserted insertvalue instructions are inserted before InsertBefore
6434 BasicBlock::iterator InsertBefore) {
6435 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6436 idx_range);
6437 Value *To = PoisonValue::get(IndexedType);
6438 SmallVector<unsigned, 10> Idxs(idx_range);
6439 unsigned IdxSkip = Idxs.size();
6440
6441 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6442}
6443
6444/// Given an aggregate and a sequence of indices, see if the scalar value
6445/// indexed is already around as a register, for example if it was inserted
6446/// directly into the aggregate.
6447///
6448/// If InsertBefore is not null, this function will duplicate (modified)
6449/// insertvalues when a part of a nested struct is extracted.
6450Value *
6452 std::optional<BasicBlock::iterator> InsertBefore) {
6453 // Nothing to index? Just return V then (this is useful at the end of our
6454 // recursion).
6455 if (idx_range.empty())
6456 return V;
6457 // We have indices, so V should have an indexable type.
6458 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6459 "Not looking at a struct or array?");
6460 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6461 "Invalid indices for type?");
6462
6463 if (Constant *C = dyn_cast<Constant>(V)) {
6464 C = C->getAggregateElement(idx_range[0]);
6465 if (!C) return nullptr;
6466 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6467 }
6468
6470 // Loop the indices for the insertvalue instruction in parallel with the
6471 // requested indices
6472 const unsigned *req_idx = idx_range.begin();
6473 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6474 i != e; ++i, ++req_idx) {
6475 if (req_idx == idx_range.end()) {
6476 // We can't handle this without inserting insertvalues
6477 if (!InsertBefore)
6478 return nullptr;
6479
6480 // The requested index identifies a part of a nested aggregate. Handle
6481 // this specially. For example,
6482 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6483 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6484 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6485 // This can be changed into
6486 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6487 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6488 // which allows the unused 0,0 element from the nested struct to be
6489 // removed.
6490 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6491 *InsertBefore);
6492 }
6493
6494 // This insert value inserts something else than what we are looking for.
6495 // See if the (aggregate) value inserted into has the value we are
6496 // looking for, then.
6497 if (*req_idx != *i)
6498 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6499 InsertBefore);
6500 }
6501 // If we end up here, the indices of the insertvalue match with those
6502 // requested (though possibly only partially). Now we recursively look at
6503 // the inserted value, passing any remaining indices.
6504 return FindInsertedValue(I->getInsertedValueOperand(),
6505 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6506 }
6507
6509 // If we're extracting a value from an aggregate that was extracted from
6510 // something else, we can extract from that something else directly instead.
6511 // However, we will need to chain I's indices with the requested indices.
6512
6513 // Calculate the number of indices required
6514 unsigned size = I->getNumIndices() + idx_range.size();
6515 // Allocate some space to put the new indices in
6517 Idxs.reserve(size);
6518 // Add indices from the extract value instruction
6519 Idxs.append(I->idx_begin(), I->idx_end());
6520
6521 // Add requested indices
6522 Idxs.append(idx_range.begin(), idx_range.end());
6523
6524 assert(Idxs.size() == size
6525 && "Number of indices added not correct?");
6526
6527 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6528 }
6529 // Otherwise, we don't know (such as, extracting from a function return value
6530 // or load instruction)
6531 return nullptr;
6532}
6533
6534// If V refers to an initialized global constant, set Slice either to
6535// its initializer if the size of its elements equals ElementSize, or,
6536// for ElementSize == 8, to its representation as an array of unsiged
6537// char. Return true on success.
6538// Offset is in the unit "nr of ElementSize sized elements".
6541 unsigned ElementSize, uint64_t Offset) {
6542 assert(V && "V should not be null.");
6543 assert((ElementSize % 8) == 0 &&
6544 "ElementSize expected to be a multiple of the size of a byte.");
6545 unsigned ElementSizeInBytes = ElementSize / 8;
6546
6547 // Drill down into the pointer expression V, ignoring any intervening
6548 // casts, and determine the identity of the object it references along
6549 // with the cumulative byte offset into it.
6550 const GlobalVariable *GV =
6552 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6553 // Fail if V is not based on constant global object.
6554 return false;
6555
6556 const DataLayout &DL = GV->getDataLayout();
6557 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6558
6559 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6560 /*AllowNonInbounds*/ true))
6561 // Fail if a constant offset could not be determined.
6562 return false;
6563
6564 uint64_t StartIdx = Off.getLimitedValue();
6565 if (StartIdx == UINT64_MAX)
6566 // Fail if the constant offset is excessive.
6567 return false;
6568
6569 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6570 // elements. Simply bail out if that isn't possible.
6571 if ((StartIdx % ElementSizeInBytes) != 0)
6572 return false;
6573
6574 Offset += StartIdx / ElementSizeInBytes;
6575 ConstantDataArray *Array = nullptr;
6576 ArrayType *ArrayTy = nullptr;
6577
6578 if (GV->getInitializer()->isNullValue()) {
6579 Type *GVTy = GV->getValueType();
6580 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6581 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6582
6583 Slice.Array = nullptr;
6584 Slice.Offset = 0;
6585 // Return an empty Slice for undersized constants to let callers
6586 // transform even undefined library calls into simpler, well-defined
6587 // expressions. This is preferable to making the calls although it
6588 // prevents sanitizers from detecting such calls.
6589 Slice.Length = Length < Offset ? 0 : Length - Offset;
6590 return true;
6591 }
6592
6593 auto *Init = const_cast<Constant *>(GV->getInitializer());
6594 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6595 Type *InitElTy = ArrayInit->getElementType();
6596 if (InitElTy->isIntegerTy(ElementSize)) {
6597 // If Init is an initializer for an array of the expected type
6598 // and size, use it as is.
6599 Array = ArrayInit;
6600 ArrayTy = ArrayInit->getType();
6601 }
6602 }
6603
6604 if (!Array) {
6605 if (ElementSize != 8)
6606 // TODO: Handle conversions to larger integral types.
6607 return false;
6608
6609 // Otherwise extract the portion of the initializer starting
6610 // at Offset as an array of bytes, and reset Offset.
6612 if (!Init)
6613 return false;
6614
6615 Offset = 0;
6617 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6618 }
6619
6620 uint64_t NumElts = ArrayTy->getArrayNumElements();
6621 if (Offset > NumElts)
6622 return false;
6623
6624 Slice.Array = Array;
6625 Slice.Offset = Offset;
6626 Slice.Length = NumElts - Offset;
6627 return true;
6628}
6629
6630/// Extract bytes from the initializer of the constant array V, which need
6631/// not be a nul-terminated string. On success, store the bytes in Str and
6632/// return true. When TrimAtNul is set, Str will contain only the bytes up
6633/// to but not including the first nul. Return false on failure.
6635 bool TrimAtNul) {
6637 if (!getConstantDataArrayInfo(V, Slice, 8))
6638 return false;
6639
6640 if (Slice.Array == nullptr) {
6641 if (TrimAtNul) {
6642 // Return a nul-terminated string even for an empty Slice. This is
6643 // safe because all existing SimplifyLibcalls callers require string
6644 // arguments and the behavior of the functions they fold is undefined
6645 // otherwise. Folding the calls this way is preferable to making
6646 // the undefined library calls, even though it prevents sanitizers
6647 // from reporting such calls.
6648 Str = StringRef();
6649 return true;
6650 }
6651 if (Slice.Length == 1) {
6652 Str = StringRef("", 1);
6653 return true;
6654 }
6655 // We cannot instantiate a StringRef as we do not have an appropriate string
6656 // of 0s at hand.
6657 return false;
6658 }
6659
6660 // Start out with the entire array in the StringRef.
6661 Str = Slice.Array->getAsString();
6662 // Skip over 'offset' bytes.
6663 Str = Str.substr(Slice.Offset);
6664
6665 if (TrimAtNul) {
6666 // Trim off the \0 and anything after it. If the array is not nul
6667 // terminated, we just return the whole end of string. The client may know
6668 // some other way that the string is length-bound.
6669 Str = Str.substr(0, Str.find('\0'));
6670 }
6671 return true;
6672}
6673
6674// These next two are very similar to the above, but also look through PHI
6675// nodes.
6676// TODO: See if we can integrate these two together.
6677
6678/// If we can compute the length of the string pointed to by
6679/// the specified pointer, return 'len+1'. If we can't, return 0.
6682 unsigned CharSize) {
6683 // Look through noop bitcast instructions.
6684 V = V->stripPointerCasts();
6685
6686 // If this is a PHI node, there are two cases: either we have already seen it
6687 // or we haven't.
6688 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6689 if (!PHIs.insert(PN).second)
6690 return ~0ULL; // already in the set.
6691
6692 // If it was new, see if all the input strings are the same length.
6693 uint64_t LenSoFar = ~0ULL;
6694 for (Value *IncValue : PN->incoming_values()) {
6695 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6696 if (Len == 0) return 0; // Unknown length -> unknown.
6697
6698 if (Len == ~0ULL) continue;
6699
6700 if (Len != LenSoFar && LenSoFar != ~0ULL)
6701 return 0; // Disagree -> unknown.
6702 LenSoFar = Len;
6703 }
6704
6705 // Success, all agree.
6706 return LenSoFar;
6707 }
6708
6709 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6710 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6711 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6712 if (Len1 == 0) return 0;
6713 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6714 if (Len2 == 0) return 0;
6715 if (Len1 == ~0ULL) return Len2;
6716 if (Len2 == ~0ULL) return Len1;
6717 if (Len1 != Len2) return 0;
6718 return Len1;
6719 }
6720
6721 // Otherwise, see if we can read the string.
6723 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6724 return 0;
6725
6726 if (Slice.Array == nullptr)
6727 // Zeroinitializer (including an empty one).
6728 return 1;
6729
6730 // Search for the first nul character. Return a conservative result even
6731 // when there is no nul. This is safe since otherwise the string function
6732 // being folded such as strlen is undefined, and can be preferable to
6733 // making the undefined library call.
6734 unsigned NullIndex = 0;
6735 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6736 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6737 break;
6738 }
6739
6740 return NullIndex + 1;
6741}
6742
6743/// If we can compute the length of the string pointed to by
6744/// the specified pointer, return 'len+1'. If we can't, return 0.
6745uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6746 if (!V->getType()->isPointerTy())
6747 return 0;
6748
6750 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6751 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6752 // an empty string as a length.
6753 return Len == ~0ULL ? 1 : Len;
6754}
6755
6756const Value *
6758 bool MustPreserveNullness) {
6759 assert(Call &&
6760 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6761 if (const Value *RV = Call->getReturnedArgOperand())
6762 return RV;
6763 // This can be used only as a aliasing property.
6765 Call, MustPreserveNullness))
6766 return Call->getArgOperand(0);
6767 return nullptr;
6768}
6769
6771 const CallBase *Call, bool MustPreserveNullness) {
6772 switch (Call->getIntrinsicID()) {
6773 case Intrinsic::launder_invariant_group:
6774 case Intrinsic::strip_invariant_group:
6775 case Intrinsic::aarch64_irg:
6776 case Intrinsic::aarch64_tagp:
6777 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6778 // input pointer (and thus preserve null-ness for the purposes of escape
6779 // analysis, which is where the MustPreserveNullness flag comes in to play).
6780 // However, it will not necessarily map ptr addrspace(N) null to ptr
6781 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6782 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6783 // list, no one should be relying on such a strict interpretation of
6784 // MustPreserveNullness (and, at time of writing, they are not), but we
6785 // document this fact out of an abundance of caution.
6786 case Intrinsic::amdgcn_make_buffer_rsrc:
6787 return true;
6788 case Intrinsic::ptrmask:
6789 return !MustPreserveNullness;
6790 case Intrinsic::threadlocal_address:
6791 // The underlying variable changes with thread ID. The Thread ID may change
6792 // at coroutine suspend points.
6793 return !Call->getParent()->getParent()->isPresplitCoroutine();
6794 default:
6795 return false;
6796 }
6797}
6798
6799/// \p PN defines a loop-variant pointer to an object. Check if the
6800/// previous iteration of the loop was referring to the same object as \p PN.
6802 const LoopInfo *LI) {
6803 // Find the loop-defined value.
6804 Loop *L = LI->getLoopFor(PN->getParent());
6805 if (PN->getNumIncomingValues() != 2)
6806 return true;
6807
6808 // Find the value from previous iteration.
6809 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6810 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6811 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6812 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6813 return true;
6814
6815 // If a new pointer is loaded in the loop, the pointer references a different
6816 // object in every iteration. E.g.:
6817 // for (i)
6818 // int *p = a[i];
6819 // ...
6820 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6821 if (!L->isLoopInvariant(Load->getPointerOperand()))
6822 return false;
6823 return true;
6824}
6825
6826const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6827 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6828 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6829 const Value *PtrOp = GEP->getPointerOperand();
6830 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6831 return V;
6832 V = PtrOp;
6833 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6834 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6835 Value *NewV = cast<Operator>(V)->getOperand(0);
6836 if (!NewV->getType()->isPointerTy())
6837 return V;
6838 V = NewV;
6839 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6840 if (GA->isInterposable())
6841 return V;
6842 V = GA->getAliasee();
6843 } else {
6844 if (auto *PHI = dyn_cast<PHINode>(V)) {
6845 // Look through single-arg phi nodes created by LCSSA.
6846 if (PHI->getNumIncomingValues() == 1) {
6847 V = PHI->getIncomingValue(0);
6848 continue;
6849 }
6850 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6851 // CaptureTracking can know about special capturing properties of some
6852 // intrinsics like launder.invariant.group, that can't be expressed with
6853 // the attributes, but have properties like returning aliasing pointer.
6854 // Because some analysis may assume that nocaptured pointer is not
6855 // returned from some special intrinsic (because function would have to
6856 // be marked with returns attribute), it is crucial to use this function
6857 // because it should be in sync with CaptureTracking. Not using it may
6858 // cause weird miscompilations where 2 aliasing pointers are assumed to
6859 // noalias.
6860 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6861 V = RP;
6862 continue;
6863 }
6864 }
6865
6866 return V;
6867 }
6868 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6869 }
6870 return V;
6871}
6872
6875 const LoopInfo *LI, unsigned MaxLookup) {
6878 Worklist.push_back(V);
6879 do {
6880 const Value *P = Worklist.pop_back_val();
6881 P = getUnderlyingObject(P, MaxLookup);
6882
6883 if (!Visited.insert(P).second)
6884 continue;
6885
6886 if (auto *SI = dyn_cast<SelectInst>(P)) {
6887 Worklist.push_back(SI->getTrueValue());
6888 Worklist.push_back(SI->getFalseValue());
6889 continue;
6890 }
6891
6892 if (auto *PN = dyn_cast<PHINode>(P)) {
6893 // If this PHI changes the underlying object in every iteration of the
6894 // loop, don't look through it. Consider:
6895 // int **A;
6896 // for (i) {
6897 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6898 // Curr = A[i];
6899 // *Prev, *Curr;
6900 //
6901 // Prev is tracking Curr one iteration behind so they refer to different
6902 // underlying objects.
6903 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6905 append_range(Worklist, PN->incoming_values());
6906 else
6907 Objects.push_back(P);
6908 continue;
6909 }
6910
6911 Objects.push_back(P);
6912 } while (!Worklist.empty());
6913}
6914
6916 const unsigned MaxVisited = 8;
6917
6920 Worklist.push_back(V);
6921 const Value *Object = nullptr;
6922 // Used as fallback if we can't find a common underlying object through
6923 // recursion.
6924 bool First = true;
6925 const Value *FirstObject = getUnderlyingObject(V);
6926 do {
6927 const Value *P = Worklist.pop_back_val();
6928 P = First ? FirstObject : getUnderlyingObject(P);
6929 First = false;
6930
6931 if (!Visited.insert(P).second)
6932 continue;
6933
6934 if (Visited.size() == MaxVisited)
6935 return FirstObject;
6936
6937 if (auto *SI = dyn_cast<SelectInst>(P)) {
6938 Worklist.push_back(SI->getTrueValue());
6939 Worklist.push_back(SI->getFalseValue());
6940 continue;
6941 }
6942
6943 if (auto *PN = dyn_cast<PHINode>(P)) {
6944 append_range(Worklist, PN->incoming_values());
6945 continue;
6946 }
6947
6948 if (!Object)
6949 Object = P;
6950 else if (Object != P)
6951 return FirstObject;
6952 } while (!Worklist.empty());
6953
6954 return Object ? Object : FirstObject;
6955}
6956
6957/// This is the function that does the work of looking through basic
6958/// ptrtoint+arithmetic+inttoptr sequences.
6959static const Value *getUnderlyingObjectFromInt(const Value *V) {
6960 do {
6961 if (const Operator *U = dyn_cast<Operator>(V)) {
6962 // If we find a ptrtoint, we can transfer control back to the
6963 // regular getUnderlyingObjectFromInt.
6964 if (U->getOpcode() == Instruction::PtrToInt)
6965 return U->getOperand(0);
6966 // If we find an add of a constant, a multiplied value, or a phi, it's
6967 // likely that the other operand will lead us to the base
6968 // object. We don't have to worry about the case where the
6969 // object address is somehow being computed by the multiply,
6970 // because our callers only care when the result is an
6971 // identifiable object.
6972 if (U->getOpcode() != Instruction::Add ||
6973 (!isa<ConstantInt>(U->getOperand(1)) &&
6974 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6975 !isa<PHINode>(U->getOperand(1))))
6976 return V;
6977 V = U->getOperand(0);
6978 } else {
6979 return V;
6980 }
6981 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6982 } while (true);
6983}
6984
6985/// This is a wrapper around getUnderlyingObjects and adds support for basic
6986/// ptrtoint+arithmetic+inttoptr sequences.
6987/// It returns false if unidentified object is found in getUnderlyingObjects.
6989 SmallVectorImpl<Value *> &Objects) {
6991 SmallVector<const Value *, 4> Working(1, V);
6992 do {
6993 V = Working.pop_back_val();
6994
6996 getUnderlyingObjects(V, Objs);
6997
6998 for (const Value *V : Objs) {
6999 if (!Visited.insert(V).second)
7000 continue;
7001 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7002 const Value *O =
7003 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7004 if (O->getType()->isPointerTy()) {
7005 Working.push_back(O);
7006 continue;
7007 }
7008 }
7009 // If getUnderlyingObjects fails to find an identifiable object,
7010 // getUnderlyingObjectsForCodeGen also fails for safety.
7011 if (!isIdentifiedObject(V)) {
7012 Objects.clear();
7013 return false;
7014 }
7015 Objects.push_back(const_cast<Value *>(V));
7016 }
7017 } while (!Working.empty());
7018 return true;
7019}
7020
7022 AllocaInst *Result = nullptr;
7024 SmallVector<Value *, 4> Worklist;
7025
7026 auto AddWork = [&](Value *V) {
7027 if (Visited.insert(V).second)
7028 Worklist.push_back(V);
7029 };
7030
7031 AddWork(V);
7032 do {
7033 V = Worklist.pop_back_val();
7034 assert(Visited.count(V));
7035
7036 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7037 if (Result && Result != AI)
7038 return nullptr;
7039 Result = AI;
7040 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7041 AddWork(CI->getOperand(0));
7042 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7043 for (Value *IncValue : PN->incoming_values())
7044 AddWork(IncValue);
7045 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7046 AddWork(SI->getTrueValue());
7047 AddWork(SI->getFalseValue());
7049 if (OffsetZero && !GEP->hasAllZeroIndices())
7050 return nullptr;
7051 AddWork(GEP->getPointerOperand());
7052 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7053 Value *Returned = CB->getReturnedArgOperand();
7054 if (Returned)
7055 AddWork(Returned);
7056 else
7057 return nullptr;
7058 } else {
7059 return nullptr;
7060 }
7061 } while (!Worklist.empty());
7062
7063 return Result;
7064}
7065
7067 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7068 for (const User *U : V->users()) {
7070 if (!II)
7071 return false;
7072
7073 if (AllowLifetime && II->isLifetimeStartOrEnd())
7074 continue;
7075
7076 if (AllowDroppable && II->isDroppable())
7077 continue;
7078
7079 return false;
7080 }
7081 return true;
7082}
7083
7086 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7087}
7090 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7091}
7092
7094 if (auto *II = dyn_cast<IntrinsicInst>(I))
7095 return isTriviallyVectorizable(II->getIntrinsicID());
7096 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7097 return (!Shuffle || Shuffle->isSelect()) &&
7099}
7100
7102 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7103 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7104 bool IgnoreUBImplyingAttrs) {
7105 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7106 AC, DT, TLI, UseVariableInfo,
7107 IgnoreUBImplyingAttrs);
7108}
7109
7111 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7112 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7113 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7114#ifndef NDEBUG
7115 if (Inst->getOpcode() != Opcode) {
7116 // Check that the operands are actually compatible with the Opcode override.
7117 auto hasEqualReturnAndLeadingOperandTypes =
7118 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7119 if (Inst->getNumOperands() < NumLeadingOperands)
7120 return false;
7121 const Type *ExpectedType = Inst->getType();
7122 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7123 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7124 return false;
7125 return true;
7126 };
7128 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7129 assert(!Instruction::isUnaryOp(Opcode) ||
7130 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7131 }
7132#endif
7133
7134 switch (Opcode) {
7135 default:
7136 return true;
7137 case Instruction::UDiv:
7138 case Instruction::URem: {
7139 // x / y is undefined if y == 0.
7140 const APInt *V;
7141 if (match(Inst->getOperand(1), m_APInt(V)))
7142 return *V != 0;
7143 return false;
7144 }
7145 case Instruction::SDiv:
7146 case Instruction::SRem: {
7147 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7148 const APInt *Numerator, *Denominator;
7149 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7150 return false;
7151 // We cannot hoist this division if the denominator is 0.
7152 if (*Denominator == 0)
7153 return false;
7154 // It's safe to hoist if the denominator is not 0 or -1.
7155 if (!Denominator->isAllOnes())
7156 return true;
7157 // At this point we know that the denominator is -1. It is safe to hoist as
7158 // long we know that the numerator is not INT_MIN.
7159 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7160 return !Numerator->isMinSignedValue();
7161 // The numerator *might* be MinSignedValue.
7162 return false;
7163 }
7164 case Instruction::Load: {
7165 if (!UseVariableInfo)
7166 return false;
7167
7168 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7169 if (!LI)
7170 return false;
7171 if (mustSuppressSpeculation(*LI))
7172 return false;
7173 const DataLayout &DL = LI->getDataLayout();
7175 LI->getType(), LI->getAlign(), DL,
7176 CtxI, AC, DT, TLI);
7177 }
7178 case Instruction::Call: {
7179 auto *CI = dyn_cast<const CallInst>(Inst);
7180 if (!CI)
7181 return false;
7182 const Function *Callee = CI->getCalledFunction();
7183
7184 // The called function could have undefined behavior or side-effects, even
7185 // if marked readnone nounwind.
7186 if (!Callee || !Callee->isSpeculatable())
7187 return false;
7188 // Since the operands may be changed after hoisting, undefined behavior may
7189 // be triggered by some UB-implying attributes.
7190 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7191 }
7192 case Instruction::VAArg:
7193 case Instruction::Alloca:
7194 case Instruction::Invoke:
7195 case Instruction::CallBr:
7196 case Instruction::PHI:
7197 case Instruction::Store:
7198 case Instruction::Ret:
7199 case Instruction::Br:
7200 case Instruction::IndirectBr:
7201 case Instruction::Switch:
7202 case Instruction::Unreachable:
7203 case Instruction::Fence:
7204 case Instruction::AtomicRMW:
7205 case Instruction::AtomicCmpXchg:
7206 case Instruction::LandingPad:
7207 case Instruction::Resume:
7208 case Instruction::CatchSwitch:
7209 case Instruction::CatchPad:
7210 case Instruction::CatchRet:
7211 case Instruction::CleanupPad:
7212 case Instruction::CleanupRet:
7213 return false; // Misc instructions which have effects
7214 }
7215}
7216
7218 if (I.mayReadOrWriteMemory())
7219 // Memory dependency possible
7220 return true;
7222 // Can't move above a maythrow call or infinite loop. Or if an
7223 // inalloca alloca, above a stacksave call.
7224 return true;
7226 // 1) Can't reorder two inf-loop calls, even if readonly
7227 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7228 // safe to speculative execute. (Inverse of above)
7229 return true;
7230 return false;
7231}
7232
7233/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7247
7248/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7251 bool ForSigned,
7252 const SimplifyQuery &SQ) {
7253 ConstantRange CR1 =
7254 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7255 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7258 return CR1.intersectWith(CR2, RangeType);
7259}
7260
7262 const Value *RHS,
7263 const SimplifyQuery &SQ,
7264 bool IsNSW) {
7265 ConstantRange LHSRange =
7266 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7267 ConstantRange RHSRange =
7268 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7269
7270 // mul nsw of two non-negative numbers is also nuw.
7271 if (IsNSW && LHSRange.isAllNonNegative() && RHSRange.isAllNonNegative())
7273
7274 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7275}
7276
7278 const Value *RHS,
7279 const SimplifyQuery &SQ) {
7280 // Multiplying n * m significant bits yields a result of n + m significant
7281 // bits. If the total number of significant bits does not exceed the
7282 // result bit width (minus 1), there is no overflow.
7283 // This means if we have enough leading sign bits in the operands
7284 // we can guarantee that the result does not overflow.
7285 // Ref: "Hacker's Delight" by Henry Warren
7286 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7287
7288 // Note that underestimating the number of sign bits gives a more
7289 // conservative answer.
7290 unsigned SignBits =
7291 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7292
7293 // First handle the easy case: if we have enough sign bits there's
7294 // definitely no overflow.
7295 if (SignBits > BitWidth + 1)
7297
7298 // There are two ambiguous cases where there can be no overflow:
7299 // SignBits == BitWidth + 1 and
7300 // SignBits == BitWidth
7301 // The second case is difficult to check, therefore we only handle the
7302 // first case.
7303 if (SignBits == BitWidth + 1) {
7304 // It overflows only when both arguments are negative and the true
7305 // product is exactly the minimum negative number.
7306 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7307 // For simplicity we just check if at least one side is not negative.
7308 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7309 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7310 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7312 }
7314}
7315
7318 const WithCache<const Value *> &RHS,
7319 const SimplifyQuery &SQ) {
7320 ConstantRange LHSRange =
7321 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7322 ConstantRange RHSRange =
7323 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7324 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7325}
7326
7327static OverflowResult
7330 const AddOperator *Add, const SimplifyQuery &SQ) {
7331 if (Add && Add->hasNoSignedWrap()) {
7333 }
7334
7335 // If LHS and RHS each have at least two sign bits, the addition will look
7336 // like
7337 //
7338 // XX..... +
7339 // YY.....
7340 //
7341 // If the carry into the most significant position is 0, X and Y can't both
7342 // be 1 and therefore the carry out of the addition is also 0.
7343 //
7344 // If the carry into the most significant position is 1, X and Y can't both
7345 // be 0 and therefore the carry out of the addition is also 1.
7346 //
7347 // Since the carry into the most significant position is always equal to
7348 // the carry out of the addition, there is no signed overflow.
7349 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7351
7352 ConstantRange LHSRange =
7353 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7354 ConstantRange RHSRange =
7355 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7356 OverflowResult OR =
7357 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7359 return OR;
7360
7361 // The remaining code needs Add to be available. Early returns if not so.
7362 if (!Add)
7364
7365 // If the sign of Add is the same as at least one of the operands, this add
7366 // CANNOT overflow. If this can be determined from the known bits of the
7367 // operands the above signedAddMayOverflow() check will have already done so.
7368 // The only other way to improve on the known bits is from an assumption, so
7369 // call computeKnownBitsFromContext() directly.
7370 bool LHSOrRHSKnownNonNegative =
7371 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7372 bool LHSOrRHSKnownNegative =
7373 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7374 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7375 KnownBits AddKnown(LHSRange.getBitWidth());
7376 computeKnownBitsFromContext(Add, AddKnown, SQ);
7377 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7378 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7380 }
7381
7383}
7384
7386 const Value *RHS,
7387 const SimplifyQuery &SQ) {
7388 // X - (X % ?)
7389 // The remainder of a value can't have greater magnitude than itself,
7390 // so the subtraction can't overflow.
7391
7392 // X - (X -nuw ?)
7393 // In the minimal case, this would simplify to "?", so there's no subtract
7394 // at all. But if this analysis is used to peek through casts, for example,
7395 // then determining no-overflow may allow other transforms.
7396
7397 // TODO: There are other patterns like this.
7398 // See simplifyICmpWithBinOpOnLHS() for candidates.
7399 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7400 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7401 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7403
7404 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7405 SQ.DL)) {
7406 if (*C)
7409 }
7410
7411 ConstantRange LHSRange =
7412 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7413 ConstantRange RHSRange =
7414 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7415 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7416}
7417
7419 const Value *RHS,
7420 const SimplifyQuery &SQ) {
7421 // X - (X % ?)
7422 // The remainder of a value can't have greater magnitude than itself,
7423 // so the subtraction can't overflow.
7424
7425 // X - (X -nsw ?)
7426 // In the minimal case, this would simplify to "?", so there's no subtract
7427 // at all. But if this analysis is used to peek through casts, for example,
7428 // then determining no-overflow may allow other transforms.
7429 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7430 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7431 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7433
7434 // If LHS and RHS each have at least two sign bits, the subtraction
7435 // cannot overflow.
7436 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7438
7439 ConstantRange LHSRange =
7440 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7441 ConstantRange RHSRange =
7442 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7443 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7444}
7445
7447 const DominatorTree &DT) {
7448 SmallVector<const BranchInst *, 2> GuardingBranches;
7450
7451 for (const User *U : WO->users()) {
7452 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7453 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7454
7455 if (EVI->getIndices()[0] == 0)
7456 Results.push_back(EVI);
7457 else {
7458 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7459
7460 for (const auto *U : EVI->users())
7461 if (const auto *B = dyn_cast<BranchInst>(U)) {
7462 assert(B->isConditional() && "How else is it using an i1?");
7463 GuardingBranches.push_back(B);
7464 }
7465 }
7466 } else {
7467 // We are using the aggregate directly in a way we don't want to analyze
7468 // here (storing it to a global, say).
7469 return false;
7470 }
7471 }
7472
7473 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7474 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7475 if (!NoWrapEdge.isSingleEdge())
7476 return false;
7477
7478 // Check if all users of the add are provably no-wrap.
7479 for (const auto *Result : Results) {
7480 // If the extractvalue itself is not executed on overflow, the we don't
7481 // need to check each use separately, since domination is transitive.
7482 if (DT.dominates(NoWrapEdge, Result->getParent()))
7483 continue;
7484
7485 for (const auto &RU : Result->uses())
7486 if (!DT.dominates(NoWrapEdge, RU))
7487 return false;
7488 }
7489
7490 return true;
7491 };
7492
7493 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7494}
7495
7496/// Shifts return poison if shiftwidth is larger than the bitwidth.
7497static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7498 auto *C = dyn_cast<Constant>(ShiftAmount);
7499 if (!C)
7500 return false;
7501
7502 // Shifts return poison if shiftwidth is larger than the bitwidth.
7504 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7505 unsigned NumElts = FVTy->getNumElements();
7506 for (unsigned i = 0; i < NumElts; ++i)
7507 ShiftAmounts.push_back(C->getAggregateElement(i));
7508 } else if (isa<ScalableVectorType>(C->getType()))
7509 return false; // Can't tell, just return false to be safe
7510 else
7511 ShiftAmounts.push_back(C);
7512
7513 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7514 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7515 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7516 });
7517
7518 return Safe;
7519}
7520
7526
7528 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7529}
7530
7532 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7533}
7534
7536 bool ConsiderFlagsAndMetadata) {
7537
7538 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7539 Op->hasPoisonGeneratingAnnotations())
7540 return true;
7541
7542 unsigned Opcode = Op->getOpcode();
7543
7544 // Check whether opcode is a poison/undef-generating operation
7545 switch (Opcode) {
7546 case Instruction::Shl:
7547 case Instruction::AShr:
7548 case Instruction::LShr:
7549 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7550 case Instruction::FPToSI:
7551 case Instruction::FPToUI:
7552 // fptosi/ui yields poison if the resulting value does not fit in the
7553 // destination type.
7554 return true;
7555 case Instruction::Call:
7556 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7557 switch (II->getIntrinsicID()) {
7558 // TODO: Add more intrinsics.
7559 case Intrinsic::ctlz:
7560 case Intrinsic::cttz:
7561 case Intrinsic::abs:
7562 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7563 return false;
7564 break;
7565 case Intrinsic::sshl_sat:
7566 case Intrinsic::ushl_sat:
7567 if (!includesPoison(Kind) ||
7568 shiftAmountKnownInRange(II->getArgOperand(1)))
7569 return false;
7570 break;
7571 }
7572 }
7573 [[fallthrough]];
7574 case Instruction::CallBr:
7575 case Instruction::Invoke: {
7576 const auto *CB = cast<CallBase>(Op);
7577 return !CB->hasRetAttr(Attribute::NoUndef) &&
7578 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7579 }
7580 case Instruction::InsertElement:
7581 case Instruction::ExtractElement: {
7582 // If index exceeds the length of the vector, it returns poison
7583 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7584 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7585 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7586 if (includesPoison(Kind))
7587 return !Idx ||
7588 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7589 return false;
7590 }
7591 case Instruction::ShuffleVector: {
7593 ? cast<ConstantExpr>(Op)->getShuffleMask()
7594 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7595 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7596 }
7597 case Instruction::FNeg:
7598 case Instruction::PHI:
7599 case Instruction::Select:
7600 case Instruction::ExtractValue:
7601 case Instruction::InsertValue:
7602 case Instruction::Freeze:
7603 case Instruction::ICmp:
7604 case Instruction::FCmp:
7605 case Instruction::GetElementPtr:
7606 return false;
7607 case Instruction::AddrSpaceCast:
7608 return true;
7609 default: {
7610 const auto *CE = dyn_cast<ConstantExpr>(Op);
7611 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7612 return false;
7613 else if (Instruction::isBinaryOp(Opcode))
7614 return false;
7615 // Be conservative and return true.
7616 return true;
7617 }
7618 }
7619}
7620
7622 bool ConsiderFlagsAndMetadata) {
7623 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7624 ConsiderFlagsAndMetadata);
7625}
7626
7627bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7628 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7629 ConsiderFlagsAndMetadata);
7630}
7631
7632static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7633 unsigned Depth) {
7634 if (ValAssumedPoison == V)
7635 return true;
7636
7637 const unsigned MaxDepth = 2;
7638 if (Depth >= MaxDepth)
7639 return false;
7640
7641 if (const auto *I = dyn_cast<Instruction>(V)) {
7642 if (any_of(I->operands(), [=](const Use &Op) {
7643 return propagatesPoison(Op) &&
7644 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7645 }))
7646 return true;
7647
7648 // V = extractvalue V0, idx
7649 // V2 = extractvalue V0, idx2
7650 // V0's elements are all poison or not. (e.g., add_with_overflow)
7651 const WithOverflowInst *II;
7653 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7654 llvm::is_contained(II->args(), ValAssumedPoison)))
7655 return true;
7656 }
7657 return false;
7658}
7659
7660static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7661 unsigned Depth) {
7662 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7663 return true;
7664
7665 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7666 return true;
7667
7668 const unsigned MaxDepth = 2;
7669 if (Depth >= MaxDepth)
7670 return false;
7671
7672 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7673 if (I && !canCreatePoison(cast<Operator>(I))) {
7674 return all_of(I->operands(), [=](const Value *Op) {
7675 return impliesPoison(Op, V, Depth + 1);
7676 });
7677 }
7678 return false;
7679}
7680
7681bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7682 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7683}
7684
7685static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7686
7688 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7689 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7691 return false;
7692
7693 if (isa<MetadataAsValue>(V))
7694 return false;
7695
7696 if (const auto *A = dyn_cast<Argument>(V)) {
7697 if (A->hasAttribute(Attribute::NoUndef) ||
7698 A->hasAttribute(Attribute::Dereferenceable) ||
7699 A->hasAttribute(Attribute::DereferenceableOrNull))
7700 return true;
7701 }
7702
7703 if (auto *C = dyn_cast<Constant>(V)) {
7704 if (isa<PoisonValue>(C))
7705 return !includesPoison(Kind);
7706
7707 if (isa<UndefValue>(C))
7708 return !includesUndef(Kind);
7709
7712 return true;
7713
7714 if (C->getType()->isVectorTy()) {
7715 if (isa<ConstantExpr>(C)) {
7716 // Scalable vectors can use a ConstantExpr to build a splat.
7717 if (Constant *SplatC = C->getSplatValue())
7718 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7719 return true;
7720 } else {
7721 if (includesUndef(Kind) && C->containsUndefElement())
7722 return false;
7723 if (includesPoison(Kind) && C->containsPoisonElement())
7724 return false;
7725 return !C->containsConstantExpression();
7726 }
7727 }
7728 }
7729
7730 // Strip cast operations from a pointer value.
7731 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7732 // inbounds with zero offset. To guarantee that the result isn't poison, the
7733 // stripped pointer is checked as it has to be pointing into an allocated
7734 // object or be null `null` to ensure `inbounds` getelement pointers with a
7735 // zero offset could not produce poison.
7736 // It can strip off addrspacecast that do not change bit representation as
7737 // well. We believe that such addrspacecast is equivalent to no-op.
7738 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7739 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7740 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7741 return true;
7742
7743 auto OpCheck = [&](const Value *V) {
7744 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7745 };
7746
7747 if (auto *Opr = dyn_cast<Operator>(V)) {
7748 // If the value is a freeze instruction, then it can never
7749 // be undef or poison.
7750 if (isa<FreezeInst>(V))
7751 return true;
7752
7753 if (const auto *CB = dyn_cast<CallBase>(V)) {
7754 if (CB->hasRetAttr(Attribute::NoUndef) ||
7755 CB->hasRetAttr(Attribute::Dereferenceable) ||
7756 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7757 return true;
7758 }
7759
7760 if (!::canCreateUndefOrPoison(Opr, Kind,
7761 /*ConsiderFlagsAndMetadata=*/true)) {
7762 if (const auto *PN = dyn_cast<PHINode>(V)) {
7763 unsigned Num = PN->getNumIncomingValues();
7764 bool IsWellDefined = true;
7765 for (unsigned i = 0; i < Num; ++i) {
7766 if (PN == PN->getIncomingValue(i))
7767 continue;
7768 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7769 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7770 DT, Depth + 1, Kind)) {
7771 IsWellDefined = false;
7772 break;
7773 }
7774 }
7775 if (IsWellDefined)
7776 return true;
7777 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7778 : nullptr) {
7779 // For splats we only need to check the value being splatted.
7780 if (OpCheck(Splat))
7781 return true;
7782 } else if (all_of(Opr->operands(), OpCheck))
7783 return true;
7784 }
7785 }
7786
7787 if (auto *I = dyn_cast<LoadInst>(V))
7788 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7789 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7790 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7791 return true;
7792
7794 return true;
7795
7796 // CxtI may be null or a cloned instruction.
7797 if (!CtxI || !CtxI->getParent() || !DT)
7798 return false;
7799
7800 auto *DNode = DT->getNode(CtxI->getParent());
7801 if (!DNode)
7802 // Unreachable block
7803 return false;
7804
7805 // If V is used as a branch condition before reaching CtxI, V cannot be
7806 // undef or poison.
7807 // br V, BB1, BB2
7808 // BB1:
7809 // CtxI ; V cannot be undef or poison here
7810 auto *Dominator = DNode->getIDom();
7811 // This check is purely for compile time reasons: we can skip the IDom walk
7812 // if what we are checking for includes undef and the value is not an integer.
7813 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7814 while (Dominator) {
7815 auto *TI = Dominator->getBlock()->getTerminator();
7816
7817 Value *Cond = nullptr;
7818 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7819 if (BI->isConditional())
7820 Cond = BI->getCondition();
7821 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7822 Cond = SI->getCondition();
7823 }
7824
7825 if (Cond) {
7826 if (Cond == V)
7827 return true;
7828 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7829 // For poison, we can analyze further
7830 auto *Opr = cast<Operator>(Cond);
7831 if (any_of(Opr->operands(), [V](const Use &U) {
7832 return V == U && propagatesPoison(U);
7833 }))
7834 return true;
7835 }
7836 }
7837
7838 Dominator = Dominator->getIDom();
7839 }
7840
7841 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7842 return true;
7843
7844 return false;
7845}
7846
7848 const Instruction *CtxI,
7849 const DominatorTree *DT,
7850 unsigned Depth) {
7851 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7853}
7854
7856 const Instruction *CtxI,
7857 const DominatorTree *DT, unsigned Depth) {
7858 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7860}
7861
7863 const Instruction *CtxI,
7864 const DominatorTree *DT, unsigned Depth) {
7865 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7867}
7868
7869/// Return true if undefined behavior would provably be executed on the path to
7870/// OnPathTo if Root produced a posion result. Note that this doesn't say
7871/// anything about whether OnPathTo is actually executed or whether Root is
7872/// actually poison. This can be used to assess whether a new use of Root can
7873/// be added at a location which is control equivalent with OnPathTo (such as
7874/// immediately before it) without introducing UB which didn't previously
7875/// exist. Note that a false result conveys no information.
7877 Instruction *OnPathTo,
7878 DominatorTree *DT) {
7879 // Basic approach is to assume Root is poison, propagate poison forward
7880 // through all users we can easily track, and then check whether any of those
7881 // users are provable UB and must execute before out exiting block might
7882 // exit.
7883
7884 // The set of all recursive users we've visited (which are assumed to all be
7885 // poison because of said visit)
7888 Worklist.push_back(Root);
7889 while (!Worklist.empty()) {
7890 const Instruction *I = Worklist.pop_back_val();
7891
7892 // If we know this must trigger UB on a path leading our target.
7893 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7894 return true;
7895
7896 // If we can't analyze propagation through this instruction, just skip it
7897 // and transitive users. Safe as false is a conservative result.
7898 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7899 return KnownPoison.contains(U) && propagatesPoison(U);
7900 }))
7901 continue;
7902
7903 if (KnownPoison.insert(I).second)
7904 for (const User *User : I->users())
7905 Worklist.push_back(cast<Instruction>(User));
7906 }
7907
7908 // Might be non-UB, or might have a path we couldn't prove must execute on
7909 // way to exiting bb.
7910 return false;
7911}
7912
7914 const SimplifyQuery &SQ) {
7915 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7916 Add, SQ);
7917}
7918
7921 const WithCache<const Value *> &RHS,
7922 const SimplifyQuery &SQ) {
7923 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7924}
7925
7927 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7928 // of time because it's possible for another thread to interfere with it for an
7929 // arbitrary length of time, but programs aren't allowed to rely on that.
7930
7931 // If there is no successor, then execution can't transfer to it.
7932 if (isa<ReturnInst>(I))
7933 return false;
7935 return false;
7936
7937 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7938 // Instruction::willReturn.
7939 //
7940 // FIXME: Move this check into Instruction::willReturn.
7941 if (isa<CatchPadInst>(I)) {
7942 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7943 default:
7944 // A catchpad may invoke exception object constructors and such, which
7945 // in some languages can be arbitrary code, so be conservative by default.
7946 return false;
7948 // For CoreCLR, it just involves a type test.
7949 return true;
7950 }
7951 }
7952
7953 // An instruction that returns without throwing must transfer control flow
7954 // to a successor.
7955 return !I->mayThrow() && I->willReturn();
7956}
7957
7959 // TODO: This is slightly conservative for invoke instruction since exiting
7960 // via an exception *is* normal control for them.
7961 for (const Instruction &I : *BB)
7963 return false;
7964 return true;
7965}
7966
7973
7976 assert(ScanLimit && "scan limit must be non-zero");
7977 for (const Instruction &I : Range) {
7978 if (--ScanLimit == 0)
7979 return false;
7981 return false;
7982 }
7983 return true;
7984}
7985
7987 const Loop *L) {
7988 // The loop header is guaranteed to be executed for every iteration.
7989 //
7990 // FIXME: Relax this constraint to cover all basic blocks that are
7991 // guaranteed to be executed at every iteration.
7992 if (I->getParent() != L->getHeader()) return false;
7993
7994 for (const Instruction &LI : *L->getHeader()) {
7995 if (&LI == I) return true;
7996 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7997 }
7998 llvm_unreachable("Instruction not contained in its own parent basic block.");
7999}
8000
8002 switch (IID) {
8003 // TODO: Add more intrinsics.
8004 case Intrinsic::sadd_with_overflow:
8005 case Intrinsic::ssub_with_overflow:
8006 case Intrinsic::smul_with_overflow:
8007 case Intrinsic::uadd_with_overflow:
8008 case Intrinsic::usub_with_overflow:
8009 case Intrinsic::umul_with_overflow:
8010 // If an input is a vector containing a poison element, the
8011 // two output vectors (calculated results, overflow bits)'
8012 // corresponding lanes are poison.
8013 return true;
8014 case Intrinsic::ctpop:
8015 case Intrinsic::ctlz:
8016 case Intrinsic::cttz:
8017 case Intrinsic::abs:
8018 case Intrinsic::smax:
8019 case Intrinsic::smin:
8020 case Intrinsic::umax:
8021 case Intrinsic::umin:
8022 case Intrinsic::scmp:
8023 case Intrinsic::is_fpclass:
8024 case Intrinsic::ptrmask:
8025 case Intrinsic::ucmp:
8026 case Intrinsic::bitreverse:
8027 case Intrinsic::bswap:
8028 case Intrinsic::sadd_sat:
8029 case Intrinsic::ssub_sat:
8030 case Intrinsic::sshl_sat:
8031 case Intrinsic::uadd_sat:
8032 case Intrinsic::usub_sat:
8033 case Intrinsic::ushl_sat:
8034 case Intrinsic::smul_fix:
8035 case Intrinsic::smul_fix_sat:
8036 case Intrinsic::umul_fix:
8037 case Intrinsic::umul_fix_sat:
8038 case Intrinsic::pow:
8039 case Intrinsic::powi:
8040 case Intrinsic::sin:
8041 case Intrinsic::sinh:
8042 case Intrinsic::cos:
8043 case Intrinsic::cosh:
8044 case Intrinsic::sincos:
8045 case Intrinsic::sincospi:
8046 case Intrinsic::tan:
8047 case Intrinsic::tanh:
8048 case Intrinsic::asin:
8049 case Intrinsic::acos:
8050 case Intrinsic::atan:
8051 case Intrinsic::atan2:
8052 case Intrinsic::canonicalize:
8053 case Intrinsic::sqrt:
8054 case Intrinsic::exp:
8055 case Intrinsic::exp2:
8056 case Intrinsic::exp10:
8057 case Intrinsic::log:
8058 case Intrinsic::log2:
8059 case Intrinsic::log10:
8060 case Intrinsic::modf:
8061 case Intrinsic::floor:
8062 case Intrinsic::ceil:
8063 case Intrinsic::trunc:
8064 case Intrinsic::rint:
8065 case Intrinsic::nearbyint:
8066 case Intrinsic::round:
8067 case Intrinsic::roundeven:
8068 case Intrinsic::lrint:
8069 case Intrinsic::llrint:
8070 case Intrinsic::fshl:
8071 case Intrinsic::fshr:
8072 return true;
8073 default:
8074 return false;
8075 }
8076}
8077
8078bool llvm::propagatesPoison(const Use &PoisonOp) {
8079 const Operator *I = cast<Operator>(PoisonOp.getUser());
8080 switch (I->getOpcode()) {
8081 case Instruction::Freeze:
8082 case Instruction::PHI:
8083 case Instruction::Invoke:
8084 return false;
8085 case Instruction::Select:
8086 return PoisonOp.getOperandNo() == 0;
8087 case Instruction::Call:
8088 if (auto *II = dyn_cast<IntrinsicInst>(I))
8089 return intrinsicPropagatesPoison(II->getIntrinsicID());
8090 return false;
8091 case Instruction::ICmp:
8092 case Instruction::FCmp:
8093 case Instruction::GetElementPtr:
8094 return true;
8095 default:
8097 return true;
8098
8099 // Be conservative and return false.
8100 return false;
8101 }
8102}
8103
8104/// Enumerates all operands of \p I that are guaranteed to not be undef or
8105/// poison. If the callback \p Handle returns true, stop processing and return
8106/// true. Otherwise, return false.
8107template <typename CallableT>
8109 const CallableT &Handle) {
8110 switch (I->getOpcode()) {
8111 case Instruction::Store:
8112 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8113 return true;
8114 break;
8115
8116 case Instruction::Load:
8117 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8118 return true;
8119 break;
8120
8121 // Since dereferenceable attribute imply noundef, atomic operations
8122 // also implicitly have noundef pointers too
8123 case Instruction::AtomicCmpXchg:
8125 return true;
8126 break;
8127
8128 case Instruction::AtomicRMW:
8129 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8130 return true;
8131 break;
8132
8133 case Instruction::Call:
8134 case Instruction::Invoke: {
8135 const CallBase *CB = cast<CallBase>(I);
8136 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8137 return true;
8138 for (unsigned i = 0; i < CB->arg_size(); ++i)
8139 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8140 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8141 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8142 Handle(CB->getArgOperand(i)))
8143 return true;
8144 break;
8145 }
8146 case Instruction::Ret:
8147 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8148 Handle(I->getOperand(0)))
8149 return true;
8150 break;
8151 case Instruction::Switch:
8152 if (Handle(cast<SwitchInst>(I)->getCondition()))
8153 return true;
8154 break;
8155 case Instruction::Br: {
8156 auto *BR = cast<BranchInst>(I);
8157 if (BR->isConditional() && Handle(BR->getCondition()))
8158 return true;
8159 break;
8160 }
8161 default:
8162 break;
8163 }
8164
8165 return false;
8166}
8167
8168/// Enumerates all operands of \p I that are guaranteed to not be poison.
8169template <typename CallableT>
8171 const CallableT &Handle) {
8172 if (handleGuaranteedWellDefinedOps(I, Handle))
8173 return true;
8174 switch (I->getOpcode()) {
8175 // Divisors of these operations are allowed to be partially undef.
8176 case Instruction::UDiv:
8177 case Instruction::SDiv:
8178 case Instruction::URem:
8179 case Instruction::SRem:
8180 return Handle(I->getOperand(1));
8181 default:
8182 return false;
8183 }
8184}
8185
8187 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8189 I, [&](const Value *V) { return KnownPoison.count(V); });
8190}
8191
8193 bool PoisonOnly) {
8194 // We currently only look for uses of values within the same basic
8195 // block, as that makes it easier to guarantee that the uses will be
8196 // executed given that Inst is executed.
8197 //
8198 // FIXME: Expand this to consider uses beyond the same basic block. To do
8199 // this, look out for the distinction between post-dominance and strong
8200 // post-dominance.
8201 const BasicBlock *BB = nullptr;
8203 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8204 BB = Inst->getParent();
8205 Begin = Inst->getIterator();
8206 Begin++;
8207 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8208 if (Arg->getParent()->isDeclaration())
8209 return false;
8210 BB = &Arg->getParent()->getEntryBlock();
8211 Begin = BB->begin();
8212 } else {
8213 return false;
8214 }
8215
8216 // Limit number of instructions we look at, to avoid scanning through large
8217 // blocks. The current limit is chosen arbitrarily.
8218 unsigned ScanLimit = 32;
8219 BasicBlock::const_iterator End = BB->end();
8220
8221 if (!PoisonOnly) {
8222 // Since undef does not propagate eagerly, be conservative & just check
8223 // whether a value is directly passed to an instruction that must take
8224 // well-defined operands.
8225
8226 for (const auto &I : make_range(Begin, End)) {
8227 if (--ScanLimit == 0)
8228 break;
8229
8230 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8231 return WellDefinedOp == V;
8232 }))
8233 return true;
8234
8236 break;
8237 }
8238 return false;
8239 }
8240
8241 // Set of instructions that we have proved will yield poison if Inst
8242 // does.
8243 SmallPtrSet<const Value *, 16> YieldsPoison;
8245
8246 YieldsPoison.insert(V);
8247 Visited.insert(BB);
8248
8249 while (true) {
8250 for (const auto &I : make_range(Begin, End)) {
8251 if (--ScanLimit == 0)
8252 return false;
8253 if (mustTriggerUB(&I, YieldsPoison))
8254 return true;
8256 return false;
8257
8258 // If an operand is poison and propagates it, mark I as yielding poison.
8259 for (const Use &Op : I.operands()) {
8260 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8261 YieldsPoison.insert(&I);
8262 break;
8263 }
8264 }
8265
8266 // Special handling for select, which returns poison if its operand 0 is
8267 // poison (handled in the loop above) *or* if both its true/false operands
8268 // are poison (handled here).
8269 if (I.getOpcode() == Instruction::Select &&
8270 YieldsPoison.count(I.getOperand(1)) &&
8271 YieldsPoison.count(I.getOperand(2))) {
8272 YieldsPoison.insert(&I);
8273 }
8274 }
8275
8276 BB = BB->getSingleSuccessor();
8277 if (!BB || !Visited.insert(BB).second)
8278 break;
8279
8280 Begin = BB->getFirstNonPHIIt();
8281 End = BB->end();
8282 }
8283 return false;
8284}
8285
8287 return ::programUndefinedIfUndefOrPoison(Inst, false);
8288}
8289
8291 return ::programUndefinedIfUndefOrPoison(Inst, true);
8292}
8293
8294static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8295 if (FMF.noNaNs())
8296 return true;
8297
8298 if (auto *C = dyn_cast<ConstantFP>(V))
8299 return !C->isNaN();
8300
8301 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8302 if (!C->getElementType()->isFloatingPointTy())
8303 return false;
8304 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8305 if (C->getElementAsAPFloat(I).isNaN())
8306 return false;
8307 }
8308 return true;
8309 }
8310
8312 return true;
8313
8314 return false;
8315}
8316
8317static bool isKnownNonZero(const Value *V) {
8318 if (auto *C = dyn_cast<ConstantFP>(V))
8319 return !C->isZero();
8320
8321 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8322 if (!C->getElementType()->isFloatingPointTy())
8323 return false;
8324 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8325 if (C->getElementAsAPFloat(I).isZero())
8326 return false;
8327 }
8328 return true;
8329 }
8330
8331 return false;
8332}
8333
8334/// Match clamp pattern for float types without care about NaNs or signed zeros.
8335/// Given non-min/max outer cmp/select from the clamp pattern this
8336/// function recognizes if it can be substitued by a "canonical" min/max
8337/// pattern.
8339 Value *CmpLHS, Value *CmpRHS,
8340 Value *TrueVal, Value *FalseVal,
8341 Value *&LHS, Value *&RHS) {
8342 // Try to match
8343 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8344 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8345 // and return description of the outer Max/Min.
8346
8347 // First, check if select has inverse order:
8348 if (CmpRHS == FalseVal) {
8349 std::swap(TrueVal, FalseVal);
8350 Pred = CmpInst::getInversePredicate(Pred);
8351 }
8352
8353 // Assume success now. If there's no match, callers should not use these anyway.
8354 LHS = TrueVal;
8355 RHS = FalseVal;
8356
8357 const APFloat *FC1;
8358 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8359 return {SPF_UNKNOWN, SPNB_NA, false};
8360
8361 const APFloat *FC2;
8362 switch (Pred) {
8363 case CmpInst::FCMP_OLT:
8364 case CmpInst::FCMP_OLE:
8365 case CmpInst::FCMP_ULT:
8366 case CmpInst::FCMP_ULE:
8367 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8368 *FC1 < *FC2)
8369 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8370 break;
8371 case CmpInst::FCMP_OGT:
8372 case CmpInst::FCMP_OGE:
8373 case CmpInst::FCMP_UGT:
8374 case CmpInst::FCMP_UGE:
8375 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8376 *FC1 > *FC2)
8377 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8378 break;
8379 default:
8380 break;
8381 }
8382
8383 return {SPF_UNKNOWN, SPNB_NA, false};
8384}
8385
8386/// Recognize variations of:
8387/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8389 Value *CmpLHS, Value *CmpRHS,
8390 Value *TrueVal, Value *FalseVal) {
8391 // Swap the select operands and predicate to match the patterns below.
8392 if (CmpRHS != TrueVal) {
8393 Pred = ICmpInst::getSwappedPredicate(Pred);
8394 std::swap(TrueVal, FalseVal);
8395 }
8396 const APInt *C1;
8397 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8398 const APInt *C2;
8399 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8400 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8401 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8402 return {SPF_SMAX, SPNB_NA, false};
8403
8404 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8405 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8406 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8407 return {SPF_SMIN, SPNB_NA, false};
8408
8409 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8410 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8411 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8412 return {SPF_UMAX, SPNB_NA, false};
8413
8414 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8415 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8416 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8417 return {SPF_UMIN, SPNB_NA, false};
8418 }
8419 return {SPF_UNKNOWN, SPNB_NA, false};
8420}
8421
8422/// Recognize variations of:
8423/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8425 Value *CmpLHS, Value *CmpRHS,
8426 Value *TVal, Value *FVal,
8427 unsigned Depth) {
8428 // TODO: Allow FP min/max with nnan/nsz.
8429 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8430
8431 Value *A = nullptr, *B = nullptr;
8432 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8433 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8434 return {SPF_UNKNOWN, SPNB_NA, false};
8435
8436 Value *C = nullptr, *D = nullptr;
8437 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8438 if (L.Flavor != R.Flavor)
8439 return {SPF_UNKNOWN, SPNB_NA, false};
8440
8441 // We have something like: x Pred y ? min(a, b) : min(c, d).
8442 // Try to match the compare to the min/max operations of the select operands.
8443 // First, make sure we have the right compare predicate.
8444 switch (L.Flavor) {
8445 case SPF_SMIN:
8446 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8447 Pred = ICmpInst::getSwappedPredicate(Pred);
8448 std::swap(CmpLHS, CmpRHS);
8449 }
8450 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8451 break;
8452 return {SPF_UNKNOWN, SPNB_NA, false};
8453 case SPF_SMAX:
8454 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8455 Pred = ICmpInst::getSwappedPredicate(Pred);
8456 std::swap(CmpLHS, CmpRHS);
8457 }
8458 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8459 break;
8460 return {SPF_UNKNOWN, SPNB_NA, false};
8461 case SPF_UMIN:
8462 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8463 Pred = ICmpInst::getSwappedPredicate(Pred);
8464 std::swap(CmpLHS, CmpRHS);
8465 }
8466 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8467 break;
8468 return {SPF_UNKNOWN, SPNB_NA, false};
8469 case SPF_UMAX:
8470 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8471 Pred = ICmpInst::getSwappedPredicate(Pred);
8472 std::swap(CmpLHS, CmpRHS);
8473 }
8474 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8475 break;
8476 return {SPF_UNKNOWN, SPNB_NA, false};
8477 default:
8478 return {SPF_UNKNOWN, SPNB_NA, false};
8479 }
8480
8481 // If there is a common operand in the already matched min/max and the other
8482 // min/max operands match the compare operands (either directly or inverted),
8483 // then this is min/max of the same flavor.
8484
8485 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8486 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8487 if (D == B) {
8488 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8489 match(A, m_Not(m_Specific(CmpRHS)))))
8490 return {L.Flavor, SPNB_NA, false};
8491 }
8492 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8493 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8494 if (C == B) {
8495 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8496 match(A, m_Not(m_Specific(CmpRHS)))))
8497 return {L.Flavor, SPNB_NA, false};
8498 }
8499 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8500 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8501 if (D == A) {
8502 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8503 match(B, m_Not(m_Specific(CmpRHS)))))
8504 return {L.Flavor, SPNB_NA, false};
8505 }
8506 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8507 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8508 if (C == A) {
8509 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8510 match(B, m_Not(m_Specific(CmpRHS)))))
8511 return {L.Flavor, SPNB_NA, false};
8512 }
8513
8514 return {SPF_UNKNOWN, SPNB_NA, false};
8515}
8516
8517/// If the input value is the result of a 'not' op, constant integer, or vector
8518/// splat of a constant integer, return the bitwise-not source value.
8519/// TODO: This could be extended to handle non-splat vector integer constants.
8521 Value *NotV;
8522 if (match(V, m_Not(m_Value(NotV))))
8523 return NotV;
8524
8525 const APInt *C;
8526 if (match(V, m_APInt(C)))
8527 return ConstantInt::get(V->getType(), ~(*C));
8528
8529 return nullptr;
8530}
8531
8532/// Match non-obvious integer minimum and maximum sequences.
8534 Value *CmpLHS, Value *CmpRHS,
8535 Value *TrueVal, Value *FalseVal,
8536 Value *&LHS, Value *&RHS,
8537 unsigned Depth) {
8538 // Assume success. If there's no match, callers should not use these anyway.
8539 LHS = TrueVal;
8540 RHS = FalseVal;
8541
8542 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8544 return SPR;
8545
8546 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8548 return SPR;
8549
8550 // Look through 'not' ops to find disguised min/max.
8551 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8552 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8553 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8554 switch (Pred) {
8555 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8556 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8557 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8558 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8559 default: break;
8560 }
8561 }
8562
8563 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8564 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8565 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8566 switch (Pred) {
8567 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8568 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8569 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8570 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8571 default: break;
8572 }
8573 }
8574
8575 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8576 return {SPF_UNKNOWN, SPNB_NA, false};
8577
8578 const APInt *C1;
8579 if (!match(CmpRHS, m_APInt(C1)))
8580 return {SPF_UNKNOWN, SPNB_NA, false};
8581
8582 // An unsigned min/max can be written with a signed compare.
8583 const APInt *C2;
8584 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8585 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8586 // Is the sign bit set?
8587 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8588 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8589 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8590 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8591
8592 // Is the sign bit clear?
8593 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8594 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8595 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8596 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8597 }
8598
8599 return {SPF_UNKNOWN, SPNB_NA, false};
8600}
8601
8602bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8603 bool AllowPoison) {
8604 assert(X && Y && "Invalid operand");
8605
8606 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8607 if (!match(X, m_Neg(m_Specific(Y))))
8608 return false;
8609
8610 auto *BO = cast<BinaryOperator>(X);
8611 if (NeedNSW && !BO->hasNoSignedWrap())
8612 return false;
8613
8614 auto *Zero = cast<Constant>(BO->getOperand(0));
8615 if (!AllowPoison && !Zero->isNullValue())
8616 return false;
8617
8618 return true;
8619 };
8620
8621 // X = -Y or Y = -X
8622 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8623 return true;
8624
8625 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8626 Value *A, *B;
8627 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8628 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8629 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8631}
8632
8633bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8634 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8635 Value *A, *B, *C;
8636 CmpPredicate Pred1, Pred2;
8637 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8638 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8639 return false;
8640
8641 // They must both have samesign flag or not.
8642 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8643 return false;
8644
8645 if (B == C)
8646 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8647
8648 // Try to infer the relationship from constant ranges.
8649 const APInt *RHSC1, *RHSC2;
8650 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8651 return false;
8652
8653 // Sign bits of two RHSCs should match.
8654 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8655 return false;
8656
8657 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8658 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8659
8660 return CR1.inverse() == CR2;
8661}
8662
8664 SelectPatternNaNBehavior NaNBehavior,
8665 bool Ordered) {
8666 switch (Pred) {
8667 default:
8668 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8669 case ICmpInst::ICMP_UGT:
8670 case ICmpInst::ICMP_UGE:
8671 return {SPF_UMAX, SPNB_NA, false};
8672 case ICmpInst::ICMP_SGT:
8673 case ICmpInst::ICMP_SGE:
8674 return {SPF_SMAX, SPNB_NA, false};
8675 case ICmpInst::ICMP_ULT:
8676 case ICmpInst::ICMP_ULE:
8677 return {SPF_UMIN, SPNB_NA, false};
8678 case ICmpInst::ICMP_SLT:
8679 case ICmpInst::ICMP_SLE:
8680 return {SPF_SMIN, SPNB_NA, false};
8681 case FCmpInst::FCMP_UGT:
8682 case FCmpInst::FCMP_UGE:
8683 case FCmpInst::FCMP_OGT:
8684 case FCmpInst::FCMP_OGE:
8685 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8686 case FCmpInst::FCMP_ULT:
8687 case FCmpInst::FCMP_ULE:
8688 case FCmpInst::FCMP_OLT:
8689 case FCmpInst::FCMP_OLE:
8690 return {SPF_FMINNUM, NaNBehavior, Ordered};
8691 }
8692}
8693
8694std::optional<std::pair<CmpPredicate, Constant *>>
8697 "Only for relational integer predicates.");
8698 if (isa<UndefValue>(C))
8699 return std::nullopt;
8700
8701 Type *Type = C->getType();
8702 bool IsSigned = ICmpInst::isSigned(Pred);
8703
8705 bool WillIncrement =
8706 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8707
8708 // Check if the constant operand can be safely incremented/decremented
8709 // without overflowing/underflowing.
8710 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8711 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8712 };
8713
8714 Constant *SafeReplacementConstant = nullptr;
8715 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8716 // Bail out if the constant can't be safely incremented/decremented.
8717 if (!ConstantIsOk(CI))
8718 return std::nullopt;
8719 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8720 unsigned NumElts = FVTy->getNumElements();
8721 for (unsigned i = 0; i != NumElts; ++i) {
8722 Constant *Elt = C->getAggregateElement(i);
8723 if (!Elt)
8724 return std::nullopt;
8725
8726 if (isa<UndefValue>(Elt))
8727 continue;
8728
8729 // Bail out if we can't determine if this constant is min/max or if we
8730 // know that this constant is min/max.
8731 auto *CI = dyn_cast<ConstantInt>(Elt);
8732 if (!CI || !ConstantIsOk(CI))
8733 return std::nullopt;
8734
8735 if (!SafeReplacementConstant)
8736 SafeReplacementConstant = CI;
8737 }
8738 } else if (isa<VectorType>(C->getType())) {
8739 // Handle scalable splat
8740 Value *SplatC = C->getSplatValue();
8741 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8742 // Bail out if the constant can't be safely incremented/decremented.
8743 if (!CI || !ConstantIsOk(CI))
8744 return std::nullopt;
8745 } else {
8746 // ConstantExpr?
8747 return std::nullopt;
8748 }
8749
8750 // It may not be safe to change a compare predicate in the presence of
8751 // undefined elements, so replace those elements with the first safe constant
8752 // that we found.
8753 // TODO: in case of poison, it is safe; let's replace undefs only.
8754 if (C->containsUndefOrPoisonElement()) {
8755 assert(SafeReplacementConstant && "Replacement constant not set");
8756 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8757 }
8758
8760
8761 // Increment or decrement the constant.
8762 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8763 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8764
8765 return std::make_pair(NewPred, NewC);
8766}
8767
8769 FastMathFlags FMF,
8770 Value *CmpLHS, Value *CmpRHS,
8771 Value *TrueVal, Value *FalseVal,
8772 Value *&LHS, Value *&RHS,
8773 unsigned Depth) {
8774 bool HasMismatchedZeros = false;
8775 if (CmpInst::isFPPredicate(Pred)) {
8776 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8777 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8778 // purpose of identifying min/max. Disregard vector constants with undefined
8779 // elements because those can not be back-propagated for analysis.
8780 Value *OutputZeroVal = nullptr;
8781 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8782 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8783 OutputZeroVal = TrueVal;
8784 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8785 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8786 OutputZeroVal = FalseVal;
8787
8788 if (OutputZeroVal) {
8789 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8790 HasMismatchedZeros = true;
8791 CmpLHS = OutputZeroVal;
8792 }
8793 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8794 HasMismatchedZeros = true;
8795 CmpRHS = OutputZeroVal;
8796 }
8797 }
8798 }
8799
8800 LHS = CmpLHS;
8801 RHS = CmpRHS;
8802
8803 // Signed zero may return inconsistent results between implementations.
8804 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8805 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8806 // Therefore, we behave conservatively and only proceed if at least one of the
8807 // operands is known to not be zero or if we don't care about signed zero.
8808 switch (Pred) {
8809 default: break;
8812 if (!HasMismatchedZeros)
8813 break;
8814 [[fallthrough]];
8817 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8818 !isKnownNonZero(CmpRHS))
8819 return {SPF_UNKNOWN, SPNB_NA, false};
8820 }
8821
8822 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8823 bool Ordered = false;
8824
8825 // When given one NaN and one non-NaN input:
8826 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8827 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8828 // ordered comparison fails), which could be NaN or non-NaN.
8829 // so here we discover exactly what NaN behavior is required/accepted.
8830 if (CmpInst::isFPPredicate(Pred)) {
8831 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8832 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8833
8834 if (LHSSafe && RHSSafe) {
8835 // Both operands are known non-NaN.
8836 NaNBehavior = SPNB_RETURNS_ANY;
8837 Ordered = CmpInst::isOrdered(Pred);
8838 } else if (CmpInst::isOrdered(Pred)) {
8839 // An ordered comparison will return false when given a NaN, so it
8840 // returns the RHS.
8841 Ordered = true;
8842 if (LHSSafe)
8843 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8844 NaNBehavior = SPNB_RETURNS_NAN;
8845 else if (RHSSafe)
8846 NaNBehavior = SPNB_RETURNS_OTHER;
8847 else
8848 // Completely unsafe.
8849 return {SPF_UNKNOWN, SPNB_NA, false};
8850 } else {
8851 Ordered = false;
8852 // An unordered comparison will return true when given a NaN, so it
8853 // returns the LHS.
8854 if (LHSSafe)
8855 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8856 NaNBehavior = SPNB_RETURNS_OTHER;
8857 else if (RHSSafe)
8858 NaNBehavior = SPNB_RETURNS_NAN;
8859 else
8860 // Completely unsafe.
8861 return {SPF_UNKNOWN, SPNB_NA, false};
8862 }
8863 }
8864
8865 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8866 std::swap(CmpLHS, CmpRHS);
8867 Pred = CmpInst::getSwappedPredicate(Pred);
8868 if (NaNBehavior == SPNB_RETURNS_NAN)
8869 NaNBehavior = SPNB_RETURNS_OTHER;
8870 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8871 NaNBehavior = SPNB_RETURNS_NAN;
8872 Ordered = !Ordered;
8873 }
8874
8875 // ([if]cmp X, Y) ? X : Y
8876 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8877 return getSelectPattern(Pred, NaNBehavior, Ordered);
8878
8879 if (isKnownNegation(TrueVal, FalseVal)) {
8880 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8881 // match against either LHS or sext(LHS).
8882 auto MaybeSExtCmpLHS =
8883 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8884 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8885 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8886 if (match(TrueVal, MaybeSExtCmpLHS)) {
8887 // Set the return values. If the compare uses the negated value (-X >s 0),
8888 // swap the return values because the negated value is always 'RHS'.
8889 LHS = TrueVal;
8890 RHS = FalseVal;
8891 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8892 std::swap(LHS, RHS);
8893
8894 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8895 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8896 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8897 return {SPF_ABS, SPNB_NA, false};
8898
8899 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8900 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8901 return {SPF_ABS, SPNB_NA, false};
8902
8903 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8904 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8905 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8906 return {SPF_NABS, SPNB_NA, false};
8907 }
8908 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8909 // Set the return values. If the compare uses the negated value (-X >s 0),
8910 // swap the return values because the negated value is always 'RHS'.
8911 LHS = FalseVal;
8912 RHS = TrueVal;
8913 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8914 std::swap(LHS, RHS);
8915
8916 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8917 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8918 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8919 return {SPF_NABS, SPNB_NA, false};
8920
8921 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8922 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8923 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8924 return {SPF_ABS, SPNB_NA, false};
8925 }
8926 }
8927
8928 if (CmpInst::isIntPredicate(Pred))
8929 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8930
8931 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8932 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8933 // semantics than minNum. Be conservative in such case.
8934 if (NaNBehavior != SPNB_RETURNS_ANY ||
8935 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8936 !isKnownNonZero(CmpRHS)))
8937 return {SPF_UNKNOWN, SPNB_NA, false};
8938
8939 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8940}
8941
8943 Instruction::CastOps *CastOp) {
8944 const DataLayout &DL = CmpI->getDataLayout();
8945
8946 Constant *CastedTo = nullptr;
8947 switch (*CastOp) {
8948 case Instruction::ZExt:
8949 if (CmpI->isUnsigned())
8950 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8951 break;
8952 case Instruction::SExt:
8953 if (CmpI->isSigned())
8954 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8955 break;
8956 case Instruction::Trunc:
8957 Constant *CmpConst;
8958 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8959 CmpConst->getType() == SrcTy) {
8960 // Here we have the following case:
8961 //
8962 // %cond = cmp iN %x, CmpConst
8963 // %tr = trunc iN %x to iK
8964 // %narrowsel = select i1 %cond, iK %t, iK C
8965 //
8966 // We can always move trunc after select operation:
8967 //
8968 // %cond = cmp iN %x, CmpConst
8969 // %widesel = select i1 %cond, iN %x, iN CmpConst
8970 // %tr = trunc iN %widesel to iK
8971 //
8972 // Note that C could be extended in any way because we don't care about
8973 // upper bits after truncation. It can't be abs pattern, because it would
8974 // look like:
8975 //
8976 // select i1 %cond, x, -x.
8977 //
8978 // So only min/max pattern could be matched. Such match requires widened C
8979 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8980 // CmpConst == C is checked below.
8981 CastedTo = CmpConst;
8982 } else {
8983 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8984 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8985 }
8986 break;
8987 case Instruction::FPTrunc:
8988 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8989 break;
8990 case Instruction::FPExt:
8991 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8992 break;
8993 case Instruction::FPToUI:
8994 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8995 break;
8996 case Instruction::FPToSI:
8997 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8998 break;
8999 case Instruction::UIToFP:
9000 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9001 break;
9002 case Instruction::SIToFP:
9003 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9004 break;
9005 default:
9006 break;
9007 }
9008
9009 if (!CastedTo)
9010 return nullptr;
9011
9012 // Make sure the cast doesn't lose any information.
9013 Constant *CastedBack =
9014 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9015 if (CastedBack && CastedBack != C)
9016 return nullptr;
9017
9018 return CastedTo;
9019}
9020
9021/// Helps to match a select pattern in case of a type mismatch.
9022///
9023/// The function processes the case when type of true and false values of a
9024/// select instruction differs from type of the cmp instruction operands because
9025/// of a cast instruction. The function checks if it is legal to move the cast
9026/// operation after "select". If yes, it returns the new second value of
9027/// "select" (with the assumption that cast is moved):
9028/// 1. As operand of cast instruction when both values of "select" are same cast
9029/// instructions.
9030/// 2. As restored constant (by applying reverse cast operation) when the first
9031/// value of the "select" is a cast operation and the second value is a
9032/// constant. It is implemented in lookThroughCastConst().
9033/// 3. As one operand is cast instruction and the other is not. The operands in
9034/// sel(cmp) are in different type integer.
9035/// NOTE: We return only the new second value because the first value could be
9036/// accessed as operand of cast instruction.
9037static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9038 Instruction::CastOps *CastOp) {
9039 auto *Cast1 = dyn_cast<CastInst>(V1);
9040 if (!Cast1)
9041 return nullptr;
9042
9043 *CastOp = Cast1->getOpcode();
9044 Type *SrcTy = Cast1->getSrcTy();
9045 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9046 // If V1 and V2 are both the same cast from the same type, look through V1.
9047 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9048 return Cast2->getOperand(0);
9049 return nullptr;
9050 }
9051
9052 auto *C = dyn_cast<Constant>(V2);
9053 if (C)
9054 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9055
9056 Value *CastedTo = nullptr;
9057 if (*CastOp == Instruction::Trunc) {
9058 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9059 // Here we have the following case:
9060 // %y_ext = sext iK %y to iN
9061 // %cond = cmp iN %x, %y_ext
9062 // %tr = trunc iN %x to iK
9063 // %narrowsel = select i1 %cond, iK %tr, iK %y
9064 //
9065 // We can always move trunc after select operation:
9066 // %y_ext = sext iK %y to iN
9067 // %cond = cmp iN %x, %y_ext
9068 // %widesel = select i1 %cond, iN %x, iN %y_ext
9069 // %tr = trunc iN %widesel to iK
9070 assert(V2->getType() == Cast1->getType() &&
9071 "V2 and Cast1 should be the same type.");
9072 CastedTo = CmpI->getOperand(1);
9073 }
9074 }
9075
9076 return CastedTo;
9077}
9079 Instruction::CastOps *CastOp,
9080 unsigned Depth) {
9082 return {SPF_UNKNOWN, SPNB_NA, false};
9083
9085 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9086
9087 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9088 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9089
9090 Value *TrueVal = SI->getTrueValue();
9091 Value *FalseVal = SI->getFalseValue();
9092
9094 CmpI, TrueVal, FalseVal, LHS, RHS,
9095 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
9096 CastOp, Depth);
9097}
9098
9100 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9101 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9102 CmpInst::Predicate Pred = CmpI->getPredicate();
9103 Value *CmpLHS = CmpI->getOperand(0);
9104 Value *CmpRHS = CmpI->getOperand(1);
9105 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9106 FMF.setNoNaNs();
9107
9108 // Bail out early.
9109 if (CmpI->isEquality())
9110 return {SPF_UNKNOWN, SPNB_NA, false};
9111
9112 // Deal with type mismatches.
9113 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9114 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9115 // If this is a potential fmin/fmax with a cast to integer, then ignore
9116 // -0.0 because there is no corresponding integer value.
9117 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9118 FMF.setNoSignedZeros();
9119 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9120 cast<CastInst>(TrueVal)->getOperand(0), C,
9121 LHS, RHS, Depth);
9122 }
9123 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9124 // If this is a potential fmin/fmax with a cast to integer, then ignore
9125 // -0.0 because there is no corresponding integer value.
9126 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9127 FMF.setNoSignedZeros();
9128 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9129 C, cast<CastInst>(FalseVal)->getOperand(0),
9130 LHS, RHS, Depth);
9131 }
9132 }
9133 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9134 LHS, RHS, Depth);
9135}
9136
9138 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9139 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9140 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9141 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9142 if (SPF == SPF_FMINNUM)
9143 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9144 if (SPF == SPF_FMAXNUM)
9145 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9146 llvm_unreachable("unhandled!");
9147}
9148
9150 switch (SPF) {
9152 return Intrinsic::umin;
9154 return Intrinsic::umax;
9156 return Intrinsic::smin;
9158 return Intrinsic::smax;
9159 default:
9160 llvm_unreachable("Unexpected SPF");
9161 }
9162}
9163
9165 if (SPF == SPF_SMIN) return SPF_SMAX;
9166 if (SPF == SPF_UMIN) return SPF_UMAX;
9167 if (SPF == SPF_SMAX) return SPF_SMIN;
9168 if (SPF == SPF_UMAX) return SPF_UMIN;
9169 llvm_unreachable("unhandled!");
9170}
9171
9173 switch (MinMaxID) {
9174 case Intrinsic::smax: return Intrinsic::smin;
9175 case Intrinsic::smin: return Intrinsic::smax;
9176 case Intrinsic::umax: return Intrinsic::umin;
9177 case Intrinsic::umin: return Intrinsic::umax;
9178 // Please note that next four intrinsics may produce the same result for
9179 // original and inverted case even if X != Y due to NaN is handled specially.
9180 case Intrinsic::maximum: return Intrinsic::minimum;
9181 case Intrinsic::minimum: return Intrinsic::maximum;
9182 case Intrinsic::maxnum: return Intrinsic::minnum;
9183 case Intrinsic::minnum: return Intrinsic::maxnum;
9184 case Intrinsic::maximumnum:
9185 return Intrinsic::minimumnum;
9186 case Intrinsic::minimumnum:
9187 return Intrinsic::maximumnum;
9188 default: llvm_unreachable("Unexpected intrinsic");
9189 }
9190}
9191
9193 switch (SPF) {
9196 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9197 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9198 default: llvm_unreachable("Unexpected flavor");
9199 }
9200}
9201
9202std::pair<Intrinsic::ID, bool>
9204 // Check if VL contains select instructions that can be folded into a min/max
9205 // vector intrinsic and return the intrinsic if it is possible.
9206 // TODO: Support floating point min/max.
9207 bool AllCmpSingleUse = true;
9208 SelectPatternResult SelectPattern;
9209 SelectPattern.Flavor = SPF_UNKNOWN;
9210 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9211 Value *LHS, *RHS;
9212 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9213 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9214 return false;
9215 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9216 SelectPattern.Flavor != CurrentPattern.Flavor)
9217 return false;
9218 SelectPattern = CurrentPattern;
9219 AllCmpSingleUse &=
9221 return true;
9222 })) {
9223 switch (SelectPattern.Flavor) {
9224 case SPF_SMIN:
9225 return {Intrinsic::smin, AllCmpSingleUse};
9226 case SPF_UMIN:
9227 return {Intrinsic::umin, AllCmpSingleUse};
9228 case SPF_SMAX:
9229 return {Intrinsic::smax, AllCmpSingleUse};
9230 case SPF_UMAX:
9231 return {Intrinsic::umax, AllCmpSingleUse};
9232 case SPF_FMAXNUM:
9233 return {Intrinsic::maxnum, AllCmpSingleUse};
9234 case SPF_FMINNUM:
9235 return {Intrinsic::minnum, AllCmpSingleUse};
9236 default:
9237 llvm_unreachable("unexpected select pattern flavor");
9238 }
9239 }
9240 return {Intrinsic::not_intrinsic, false};
9241}
9242
9243template <typename InstTy>
9244static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9245 Value *&Init, Value *&OtherOp) {
9246 // Handle the case of a simple two-predecessor recurrence PHI.
9247 // There's a lot more that could theoretically be done here, but
9248 // this is sufficient to catch some interesting cases.
9249 // TODO: Expand list -- gep, uadd.sat etc.
9250 if (PN->getNumIncomingValues() != 2)
9251 return false;
9252
9253 for (unsigned I = 0; I != 2; ++I) {
9254 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9255 Operation && Operation->getNumOperands() >= 2) {
9256 Value *LHS = Operation->getOperand(0);
9257 Value *RHS = Operation->getOperand(1);
9258 if (LHS != PN && RHS != PN)
9259 continue;
9260
9261 Inst = Operation;
9262 Init = PN->getIncomingValue(!I);
9263 OtherOp = (LHS == PN) ? RHS : LHS;
9264 return true;
9265 }
9266 }
9267 return false;
9268}
9269
9271 Value *&Start, Value *&Step) {
9272 // We try to match a recurrence of the form:
9273 // %iv = [Start, %entry], [%iv.next, %backedge]
9274 // %iv.next = binop %iv, Step
9275 // Or:
9276 // %iv = [Start, %entry], [%iv.next, %backedge]
9277 // %iv.next = binop Step, %iv
9278 return matchTwoInputRecurrence(P, BO, Start, Step);
9279}
9280
9282 Value *&Start, Value *&Step) {
9283 BinaryOperator *BO = nullptr;
9284 P = dyn_cast<PHINode>(I->getOperand(0));
9285 if (!P)
9286 P = dyn_cast<PHINode>(I->getOperand(1));
9287 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9288}
9289
9291 PHINode *&P, Value *&Init,
9292 Value *&OtherOp) {
9293 // Binary intrinsics only supported for now.
9294 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9295 I->getType() != I->getArgOperand(1)->getType())
9296 return false;
9297
9298 IntrinsicInst *II = nullptr;
9299 P = dyn_cast<PHINode>(I->getArgOperand(0));
9300 if (!P)
9301 P = dyn_cast<PHINode>(I->getArgOperand(1));
9302
9303 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9304}
9305
9306/// Return true if "icmp Pred LHS RHS" is always true.
9308 const Value *RHS) {
9309 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9310 return true;
9311
9312 switch (Pred) {
9313 default:
9314 return false;
9315
9316 case CmpInst::ICMP_SLE: {
9317 const APInt *C;
9318
9319 // LHS s<= LHS +_{nsw} C if C >= 0
9320 // LHS s<= LHS | C if C >= 0
9321 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9323 return !C->isNegative();
9324
9325 // LHS s<= smax(LHS, V) for any V
9327 return true;
9328
9329 // smin(RHS, V) s<= RHS for any V
9331 return true;
9332
9333 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9334 const Value *X;
9335 const APInt *CLHS, *CRHS;
9336 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9338 return CLHS->sle(*CRHS);
9339
9340 return false;
9341 }
9342
9343 case CmpInst::ICMP_ULE: {
9344 // LHS u<= LHS +_{nuw} V for any V
9345 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9347 return true;
9348
9349 // LHS u<= LHS | V for any V
9350 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9351 return true;
9352
9353 // LHS u<= umax(LHS, V) for any V
9355 return true;
9356
9357 // RHS >> V u<= RHS for any V
9358 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9359 return true;
9360
9361 // RHS u/ C_ugt_1 u<= RHS
9362 const APInt *C;
9363 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9364 return true;
9365
9366 // RHS & V u<= RHS for any V
9368 return true;
9369
9370 // umin(RHS, V) u<= RHS for any V
9372 return true;
9373
9374 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9375 const Value *X;
9376 const APInt *CLHS, *CRHS;
9377 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9379 return CLHS->ule(*CRHS);
9380
9381 return false;
9382 }
9383 }
9384}
9385
9386/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9387/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9388static std::optional<bool>
9390 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9391 switch (Pred) {
9392 default:
9393 return std::nullopt;
9394
9395 case CmpInst::ICMP_SLT:
9396 case CmpInst::ICMP_SLE:
9397 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9399 return true;
9400 return std::nullopt;
9401
9402 case CmpInst::ICMP_SGT:
9403 case CmpInst::ICMP_SGE:
9404 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9406 return true;
9407 return std::nullopt;
9408
9409 case CmpInst::ICMP_ULT:
9410 case CmpInst::ICMP_ULE:
9411 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9413 return true;
9414 return std::nullopt;
9415
9416 case CmpInst::ICMP_UGT:
9417 case CmpInst::ICMP_UGE:
9418 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9420 return true;
9421 return std::nullopt;
9422 }
9423}
9424
9425/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9426/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9427/// Otherwise, return std::nullopt if we can't infer anything.
9428static std::optional<bool>
9430 CmpPredicate RPred, const ConstantRange &RCR) {
9431 auto CRImpliesPred = [&](ConstantRange CR,
9432 CmpInst::Predicate Pred) -> std::optional<bool> {
9433 // If all true values for lhs and true for rhs, lhs implies rhs
9434 if (CR.icmp(Pred, RCR))
9435 return true;
9436
9437 // If there is no overlap, lhs implies not rhs
9438 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9439 return false;
9440
9441 return std::nullopt;
9442 };
9443 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9444 RPred))
9445 return Res;
9446 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9448 : LPred.dropSameSign();
9450 : RPred.dropSameSign();
9451 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9452 RPred);
9453 }
9454 return std::nullopt;
9455}
9456
9457/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9458/// is true. Return false if LHS implies RHS is false. Otherwise, return
9459/// std::nullopt if we can't infer anything.
9460static std::optional<bool>
9461isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9462 CmpPredicate RPred, const Value *R0, const Value *R1,
9463 const DataLayout &DL, bool LHSIsTrue) {
9464 // The rest of the logic assumes the LHS condition is true. If that's not the
9465 // case, invert the predicate to make it so.
9466 if (!LHSIsTrue)
9467 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9468
9469 // We can have non-canonical operands, so try to normalize any common operand
9470 // to L0/R0.
9471 if (L0 == R1) {
9472 std::swap(R0, R1);
9473 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9474 }
9475 if (R0 == L1) {
9476 std::swap(L0, L1);
9477 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9478 }
9479 if (L1 == R1) {
9480 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9481 if (L0 != R0 || match(L0, m_ImmConstant())) {
9482 std::swap(L0, L1);
9483 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9484 std::swap(R0, R1);
9485 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9486 }
9487 }
9488
9489 // See if we can infer anything if operand-0 matches and we have at least one
9490 // constant.
9491 const APInt *Unused;
9492 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9493 // Potential TODO: We could also further use the constant range of L0/R0 to
9494 // further constraint the constant ranges. At the moment this leads to
9495 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9496 // C1` (see discussion: D58633).
9498 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9499 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9501 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9502 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9503 // Even if L1/R1 are not both constant, we can still sometimes deduce
9504 // relationship from a single constant. For example X u> Y implies X != 0.
9505 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9506 return R;
9507 // If both L1/R1 were exact constant ranges and we didn't get anything
9508 // here, we won't be able to deduce this.
9509 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9510 return std::nullopt;
9511 }
9512
9513 // Can we infer anything when the two compares have matching operands?
9514 if (L0 == R0 && L1 == R1)
9515 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9516
9517 // It only really makes sense in the context of signed comparison for "X - Y
9518 // must be positive if X >= Y and no overflow".
9519 // Take SGT as an example: L0:x > L1:y and C >= 0
9520 // ==> R0:(x -nsw y) < R1:(-C) is false
9521 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9522 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9523 SignedLPred == ICmpInst::ICMP_SGE) &&
9524 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9525 if (match(R1, m_NonPositive()) &&
9526 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9527 return false;
9528 }
9529
9530 // Take SLT as an example: L0:x < L1:y and C <= 0
9531 // ==> R0:(x -nsw y) < R1:(-C) is true
9532 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9533 SignedLPred == ICmpInst::ICMP_SLE) &&
9534 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9535 if (match(R1, m_NonNegative()) &&
9536 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9537 return true;
9538 }
9539
9540 // a - b == NonZero -> a != b
9541 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9542 const APInt *L1C;
9543 Value *A, *B;
9544 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9545 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9546 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9547 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9552 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9553 }
9554
9555 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9556 if (L0 == R0 &&
9557 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9558 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9559 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9560 return CmpPredicate::getMatching(LPred, RPred).has_value();
9561
9562 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9563 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9564
9565 return std::nullopt;
9566}
9567
9568/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9569/// is true. Return false if LHS implies RHS is false. Otherwise, return
9570/// std::nullopt if we can't infer anything.
9571static std::optional<bool>
9573 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9574 const DataLayout &DL, bool LHSIsTrue) {
9575 // The rest of the logic assumes the LHS condition is true. If that's not the
9576 // case, invert the predicate to make it so.
9577 if (!LHSIsTrue)
9578 LPred = FCmpInst::getInversePredicate(LPred);
9579
9580 // We can have non-canonical operands, so try to normalize any common operand
9581 // to L0/R0.
9582 if (L0 == R1) {
9583 std::swap(R0, R1);
9584 RPred = FCmpInst::getSwappedPredicate(RPred);
9585 }
9586 if (R0 == L1) {
9587 std::swap(L0, L1);
9588 LPred = FCmpInst::getSwappedPredicate(LPred);
9589 }
9590 if (L1 == R1) {
9591 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9592 if (L0 != R0 || match(L0, m_ImmConstant())) {
9593 std::swap(L0, L1);
9594 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9595 std::swap(R0, R1);
9596 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9597 }
9598 }
9599
9600 // Can we infer anything when the two compares have matching operands?
9601 if (L0 == R0 && L1 == R1) {
9602 if ((LPred & RPred) == LPred)
9603 return true;
9604 if ((LPred & ~RPred) == LPred)
9605 return false;
9606 }
9607
9608 // See if we can infer anything if operand-0 matches and we have at least one
9609 // constant.
9610 const APFloat *L1C, *R1C;
9611 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9612 if (std::optional<ConstantFPRange> DomCR =
9614 if (std::optional<ConstantFPRange> ImpliedCR =
9616 if (ImpliedCR->contains(*DomCR))
9617 return true;
9618 }
9619 if (std::optional<ConstantFPRange> ImpliedCR =
9621 FCmpInst::getInversePredicate(RPred), *R1C)) {
9622 if (ImpliedCR->contains(*DomCR))
9623 return false;
9624 }
9625 }
9626 }
9627
9628 return std::nullopt;
9629}
9630
9631/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9632/// false. Otherwise, return std::nullopt if we can't infer anything. We
9633/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9634/// instruction.
9635static std::optional<bool>
9637 const Value *RHSOp0, const Value *RHSOp1,
9638 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9639 // The LHS must be an 'or', 'and', or a 'select' instruction.
9640 assert((LHS->getOpcode() == Instruction::And ||
9641 LHS->getOpcode() == Instruction::Or ||
9642 LHS->getOpcode() == Instruction::Select) &&
9643 "Expected LHS to be 'and', 'or', or 'select'.");
9644
9645 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9646
9647 // If the result of an 'or' is false, then we know both legs of the 'or' are
9648 // false. Similarly, if the result of an 'and' is true, then we know both
9649 // legs of the 'and' are true.
9650 const Value *ALHS, *ARHS;
9651 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9652 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9653 // FIXME: Make this non-recursion.
9654 if (std::optional<bool> Implication = isImpliedCondition(
9655 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9656 return Implication;
9657 if (std::optional<bool> Implication = isImpliedCondition(
9658 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9659 return Implication;
9660 return std::nullopt;
9661 }
9662 return std::nullopt;
9663}
9664
9665std::optional<bool>
9667 const Value *RHSOp0, const Value *RHSOp1,
9668 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9669 // Bail out when we hit the limit.
9671 return std::nullopt;
9672
9673 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9674 // example.
9675 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9676 return std::nullopt;
9677
9678 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9679 "Expected integer type only!");
9680
9681 // Match not
9682 if (match(LHS, m_Not(m_Value(LHS))))
9683 LHSIsTrue = !LHSIsTrue;
9684
9685 // Both LHS and RHS are icmps.
9686 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9687 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9688 return isImpliedCondICmps(LHSCmp->getCmpPredicate(),
9689 LHSCmp->getOperand(0), LHSCmp->getOperand(1),
9690 RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
9691 const Value *V;
9692 if (match(LHS, m_NUWTrunc(m_Value(V))))
9694 ConstantInt::get(V->getType(), 0), RHSPred,
9695 RHSOp0, RHSOp1, DL, LHSIsTrue);
9696 } else {
9697 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9698 "Expected floating point type only!");
9699 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9700 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9701 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9702 DL, LHSIsTrue);
9703 }
9704
9705 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9706 /// the RHS to be an icmp.
9707 /// FIXME: Add support for and/or/select on the RHS.
9708 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9709 if ((LHSI->getOpcode() == Instruction::And ||
9710 LHSI->getOpcode() == Instruction::Or ||
9711 LHSI->getOpcode() == Instruction::Select))
9712 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9713 Depth);
9714 }
9715 return std::nullopt;
9716}
9717
9718std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9719 const DataLayout &DL,
9720 bool LHSIsTrue, unsigned Depth) {
9721 // LHS ==> RHS by definition
9722 if (LHS == RHS)
9723 return LHSIsTrue;
9724
9725 // Match not
9726 bool InvertRHS = false;
9727 if (match(RHS, m_Not(m_Value(RHS)))) {
9728 if (LHS == RHS)
9729 return !LHSIsTrue;
9730 InvertRHS = true;
9731 }
9732
9733 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9734 if (auto Implied = isImpliedCondition(
9735 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9736 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9737 return InvertRHS ? !*Implied : *Implied;
9738 return std::nullopt;
9739 }
9740 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9741 if (auto Implied = isImpliedCondition(
9742 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9743 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9744 return InvertRHS ? !*Implied : *Implied;
9745 return std::nullopt;
9746 }
9747
9748 const Value *V;
9749 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9750 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9751 ConstantInt::get(V->getType(), 0), DL,
9752 LHSIsTrue, Depth))
9753 return InvertRHS ? !*Implied : *Implied;
9754 return std::nullopt;
9755 }
9756
9758 return std::nullopt;
9759
9760 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9761 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9762 const Value *RHS1, *RHS2;
9763 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9764 if (std::optional<bool> Imp =
9765 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9766 if (*Imp == true)
9767 return !InvertRHS;
9768 if (std::optional<bool> Imp =
9769 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9770 if (*Imp == true)
9771 return !InvertRHS;
9772 }
9773 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9774 if (std::optional<bool> Imp =
9775 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9776 if (*Imp == false)
9777 return InvertRHS;
9778 if (std::optional<bool> Imp =
9779 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9780 if (*Imp == false)
9781 return InvertRHS;
9782 }
9783
9784 return std::nullopt;
9785}
9786
9787// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9788// condition dominating ContextI or nullptr, if no condition is found.
9789static std::pair<Value *, bool>
9791 if (!ContextI || !ContextI->getParent())
9792 return {nullptr, false};
9793
9794 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9795 // dominator tree (eg, from a SimplifyQuery) instead?
9796 const BasicBlock *ContextBB = ContextI->getParent();
9797 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9798 if (!PredBB)
9799 return {nullptr, false};
9800
9801 // We need a conditional branch in the predecessor.
9802 Value *PredCond;
9803 BasicBlock *TrueBB, *FalseBB;
9804 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9805 return {nullptr, false};
9806
9807 // The branch should get simplified. Don't bother simplifying this condition.
9808 if (TrueBB == FalseBB)
9809 return {nullptr, false};
9810
9811 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9812 "Predecessor block does not point to successor?");
9813
9814 // Is this condition implied by the predecessor condition?
9815 return {PredCond, TrueBB == ContextBB};
9816}
9817
9818std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9819 const Instruction *ContextI,
9820 const DataLayout &DL) {
9821 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9822 auto PredCond = getDomPredecessorCondition(ContextI);
9823 if (PredCond.first)
9824 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9825 return std::nullopt;
9826}
9827
9829 const Value *LHS,
9830 const Value *RHS,
9831 const Instruction *ContextI,
9832 const DataLayout &DL) {
9833 auto PredCond = getDomPredecessorCondition(ContextI);
9834 if (PredCond.first)
9835 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9836 PredCond.second);
9837 return std::nullopt;
9838}
9839
9841 APInt &Upper, const InstrInfoQuery &IIQ,
9842 bool PreferSignedRange) {
9843 unsigned Width = Lower.getBitWidth();
9844 const APInt *C;
9845 switch (BO.getOpcode()) {
9846 case Instruction::Sub:
9847 if (match(BO.getOperand(0), m_APInt(C))) {
9848 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9849 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9850
9851 // If the caller expects a signed compare, then try to use a signed range.
9852 // Otherwise if both no-wraps are set, use the unsigned range because it
9853 // is never larger than the signed range. Example:
9854 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9855 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9856 if (PreferSignedRange && HasNSW && HasNUW)
9857 HasNUW = false;
9858
9859 if (HasNUW) {
9860 // 'sub nuw c, x' produces [0, C].
9861 Upper = *C + 1;
9862 } else if (HasNSW) {
9863 if (C->isNegative()) {
9864 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9866 Upper = *C - APInt::getSignedMaxValue(Width);
9867 } else {
9868 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9869 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9870 Lower = *C - APInt::getSignedMaxValue(Width);
9872 }
9873 }
9874 }
9875 break;
9876 case Instruction::Add:
9877 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9878 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9879 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9880
9881 // If the caller expects a signed compare, then try to use a signed
9882 // range. Otherwise if both no-wraps are set, use the unsigned range
9883 // because it is never larger than the signed range. Example: "add nuw
9884 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9885 if (PreferSignedRange && HasNSW && HasNUW)
9886 HasNUW = false;
9887
9888 if (HasNUW) {
9889 // 'add nuw x, C' produces [C, UINT_MAX].
9890 Lower = *C;
9891 } else if (HasNSW) {
9892 if (C->isNegative()) {
9893 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9895 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9896 } else {
9897 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9898 Lower = APInt::getSignedMinValue(Width) + *C;
9899 Upper = APInt::getSignedMaxValue(Width) + 1;
9900 }
9901 }
9902 }
9903 break;
9904
9905 case Instruction::And:
9906 if (match(BO.getOperand(1), m_APInt(C)))
9907 // 'and x, C' produces [0, C].
9908 Upper = *C + 1;
9909 // X & -X is a power of two or zero. So we can cap the value at max power of
9910 // two.
9911 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9912 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9913 Upper = APInt::getSignedMinValue(Width) + 1;
9914 break;
9915
9916 case Instruction::Or:
9917 if (match(BO.getOperand(1), m_APInt(C)))
9918 // 'or x, C' produces [C, UINT_MAX].
9919 Lower = *C;
9920 break;
9921
9922 case Instruction::AShr:
9923 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9924 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9926 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9927 } else if (match(BO.getOperand(0), m_APInt(C))) {
9928 unsigned ShiftAmount = Width - 1;
9929 if (!C->isZero() && IIQ.isExact(&BO))
9930 ShiftAmount = C->countr_zero();
9931 if (C->isNegative()) {
9932 // 'ashr C, x' produces [C, C >> (Width-1)]
9933 Lower = *C;
9934 Upper = C->ashr(ShiftAmount) + 1;
9935 } else {
9936 // 'ashr C, x' produces [C >> (Width-1), C]
9937 Lower = C->ashr(ShiftAmount);
9938 Upper = *C + 1;
9939 }
9940 }
9941 break;
9942
9943 case Instruction::LShr:
9944 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9945 // 'lshr x, C' produces [0, UINT_MAX >> C].
9946 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9947 } else if (match(BO.getOperand(0), m_APInt(C))) {
9948 // 'lshr C, x' produces [C >> (Width-1), C].
9949 unsigned ShiftAmount = Width - 1;
9950 if (!C->isZero() && IIQ.isExact(&BO))
9951 ShiftAmount = C->countr_zero();
9952 Lower = C->lshr(ShiftAmount);
9953 Upper = *C + 1;
9954 }
9955 break;
9956
9957 case Instruction::Shl:
9958 if (match(BO.getOperand(0), m_APInt(C))) {
9959 if (IIQ.hasNoUnsignedWrap(&BO)) {
9960 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9961 Lower = *C;
9962 Upper = Lower.shl(Lower.countl_zero()) + 1;
9963 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9964 if (C->isNegative()) {
9965 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9966 unsigned ShiftAmount = C->countl_one() - 1;
9967 Lower = C->shl(ShiftAmount);
9968 Upper = *C + 1;
9969 } else {
9970 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9971 unsigned ShiftAmount = C->countl_zero() - 1;
9972 Lower = *C;
9973 Upper = C->shl(ShiftAmount) + 1;
9974 }
9975 } else {
9976 // If lowbit is set, value can never be zero.
9977 if ((*C)[0])
9978 Lower = APInt::getOneBitSet(Width, 0);
9979 // If we are shifting a constant the largest it can be is if the longest
9980 // sequence of consecutive ones is shifted to the highbits (breaking
9981 // ties for which sequence is higher). At the moment we take a liberal
9982 // upper bound on this by just popcounting the constant.
9983 // TODO: There may be a bitwise trick for it longest/highest
9984 // consecutative sequence of ones (naive method is O(Width) loop).
9985 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9986 }
9987 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9988 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9989 }
9990 break;
9991
9992 case Instruction::SDiv:
9993 if (match(BO.getOperand(1), m_APInt(C))) {
9994 APInt IntMin = APInt::getSignedMinValue(Width);
9995 APInt IntMax = APInt::getSignedMaxValue(Width);
9996 if (C->isAllOnes()) {
9997 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9998 // where C != -1 and C != 0 and C != 1
9999 Lower = IntMin + 1;
10000 Upper = IntMax + 1;
10001 } else if (C->countl_zero() < Width - 1) {
10002 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
10003 // where C != -1 and C != 0 and C != 1
10004 Lower = IntMin.sdiv(*C);
10005 Upper = IntMax.sdiv(*C);
10006 if (Lower.sgt(Upper))
10008 Upper = Upper + 1;
10009 assert(Upper != Lower && "Upper part of range has wrapped!");
10010 }
10011 } else if (match(BO.getOperand(0), m_APInt(C))) {
10012 if (C->isMinSignedValue()) {
10013 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10014 Lower = *C;
10015 Upper = Lower.lshr(1) + 1;
10016 } else {
10017 // 'sdiv C, x' produces [-|C|, |C|].
10018 Upper = C->abs() + 1;
10019 Lower = (-Upper) + 1;
10020 }
10021 }
10022 break;
10023
10024 case Instruction::UDiv:
10025 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10026 // 'udiv x, C' produces [0, UINT_MAX / C].
10027 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10028 } else if (match(BO.getOperand(0), m_APInt(C))) {
10029 // 'udiv C, x' produces [0, C].
10030 Upper = *C + 1;
10031 }
10032 break;
10033
10034 case Instruction::SRem:
10035 if (match(BO.getOperand(1), m_APInt(C))) {
10036 // 'srem x, C' produces (-|C|, |C|).
10037 Upper = C->abs();
10038 Lower = (-Upper) + 1;
10039 } else if (match(BO.getOperand(0), m_APInt(C))) {
10040 if (C->isNegative()) {
10041 // 'srem -|C|, x' produces [-|C|, 0].
10042 Upper = 1;
10043 Lower = *C;
10044 } else {
10045 // 'srem |C|, x' produces [0, |C|].
10046 Upper = *C + 1;
10047 }
10048 }
10049 break;
10050
10051 case Instruction::URem:
10052 if (match(BO.getOperand(1), m_APInt(C)))
10053 // 'urem x, C' produces [0, C).
10054 Upper = *C;
10055 else if (match(BO.getOperand(0), m_APInt(C)))
10056 // 'urem C, x' produces [0, C].
10057 Upper = *C + 1;
10058 break;
10059
10060 default:
10061 break;
10062 }
10063}
10064
10066 bool UseInstrInfo) {
10067 unsigned Width = II.getType()->getScalarSizeInBits();
10068 const APInt *C;
10069 switch (II.getIntrinsicID()) {
10070 case Intrinsic::ctlz:
10071 case Intrinsic::cttz: {
10072 APInt Upper(Width, Width);
10073 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10074 Upper += 1;
10075 // Maximum of set/clear bits is the bit width.
10077 }
10078 case Intrinsic::ctpop:
10079 // Maximum of set/clear bits is the bit width.
10081 APInt(Width, Width) + 1);
10082 case Intrinsic::uadd_sat:
10083 // uadd.sat(x, C) produces [C, UINT_MAX].
10084 if (match(II.getOperand(0), m_APInt(C)) ||
10085 match(II.getOperand(1), m_APInt(C)))
10087 break;
10088 case Intrinsic::sadd_sat:
10089 if (match(II.getOperand(0), m_APInt(C)) ||
10090 match(II.getOperand(1), m_APInt(C))) {
10091 if (C->isNegative())
10092 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10094 APInt::getSignedMaxValue(Width) + *C +
10095 1);
10096
10097 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10099 APInt::getSignedMaxValue(Width) + 1);
10100 }
10101 break;
10102 case Intrinsic::usub_sat:
10103 // usub.sat(C, x) produces [0, C].
10104 if (match(II.getOperand(0), m_APInt(C)))
10105 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10106
10107 // usub.sat(x, C) produces [0, UINT_MAX - C].
10108 if (match(II.getOperand(1), m_APInt(C)))
10110 APInt::getMaxValue(Width) - *C + 1);
10111 break;
10112 case Intrinsic::ssub_sat:
10113 if (match(II.getOperand(0), m_APInt(C))) {
10114 if (C->isNegative())
10115 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10117 *C - APInt::getSignedMinValue(Width) +
10118 1);
10119
10120 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10122 APInt::getSignedMaxValue(Width) + 1);
10123 } else if (match(II.getOperand(1), m_APInt(C))) {
10124 if (C->isNegative())
10125 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10127 APInt::getSignedMaxValue(Width) + 1);
10128
10129 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10131 APInt::getSignedMaxValue(Width) - *C +
10132 1);
10133 }
10134 break;
10135 case Intrinsic::umin:
10136 case Intrinsic::umax:
10137 case Intrinsic::smin:
10138 case Intrinsic::smax:
10139 if (!match(II.getOperand(0), m_APInt(C)) &&
10140 !match(II.getOperand(1), m_APInt(C)))
10141 break;
10142
10143 switch (II.getIntrinsicID()) {
10144 case Intrinsic::umin:
10145 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10146 case Intrinsic::umax:
10148 case Intrinsic::smin:
10150 *C + 1);
10151 case Intrinsic::smax:
10153 APInt::getSignedMaxValue(Width) + 1);
10154 default:
10155 llvm_unreachable("Must be min/max intrinsic");
10156 }
10157 break;
10158 case Intrinsic::abs:
10159 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10160 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10161 if (match(II.getOperand(1), m_One()))
10163 APInt::getSignedMaxValue(Width) + 1);
10164
10166 APInt::getSignedMinValue(Width) + 1);
10167 case Intrinsic::vscale:
10168 if (!II.getParent() || !II.getFunction())
10169 break;
10170 return getVScaleRange(II.getFunction(), Width);
10171 default:
10172 break;
10173 }
10174
10175 return ConstantRange::getFull(Width);
10176}
10177
10179 const InstrInfoQuery &IIQ) {
10180 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10181 const Value *LHS = nullptr, *RHS = nullptr;
10183 if (R.Flavor == SPF_UNKNOWN)
10184 return ConstantRange::getFull(BitWidth);
10185
10186 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10187 // If the negation part of the abs (in RHS) has the NSW flag,
10188 // then the result of abs(X) is [0..SIGNED_MAX],
10189 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10190 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10194
10197 }
10198
10199 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10200 // The result of -abs(X) is <= 0.
10202 APInt(BitWidth, 1));
10203 }
10204
10205 const APInt *C;
10206 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10207 return ConstantRange::getFull(BitWidth);
10208
10209 switch (R.Flavor) {
10210 case SPF_UMIN:
10212 case SPF_UMAX:
10214 case SPF_SMIN:
10216 *C + 1);
10217 case SPF_SMAX:
10220 default:
10221 return ConstantRange::getFull(BitWidth);
10222 }
10223}
10224
10226 // The maximum representable value of a half is 65504. For floats the maximum
10227 // value is 3.4e38 which requires roughly 129 bits.
10228 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10229 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10230 return;
10231 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10232 Lower = APInt(BitWidth, -65504, true);
10233 Upper = APInt(BitWidth, 65505);
10234 }
10235
10236 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10237 // For a fptoui the lower limit is left as 0.
10238 Upper = APInt(BitWidth, 65505);
10239 }
10240}
10241
10243 bool UseInstrInfo, AssumptionCache *AC,
10244 const Instruction *CtxI,
10245 const DominatorTree *DT,
10246 unsigned Depth) {
10247 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10248
10250 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10251
10252 if (auto *C = dyn_cast<Constant>(V))
10253 return C->toConstantRange();
10254
10255 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10256 InstrInfoQuery IIQ(UseInstrInfo);
10257 ConstantRange CR = ConstantRange::getFull(BitWidth);
10258 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10259 APInt Lower = APInt(BitWidth, 0);
10260 APInt Upper = APInt(BitWidth, 0);
10261 // TODO: Return ConstantRange.
10262 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10264 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10265 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10266 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10268 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10270 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10271 CR = CRTrue.unionWith(CRFalse);
10273 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10274 APInt Lower = APInt(BitWidth, 0);
10275 APInt Upper = APInt(BitWidth, 0);
10276 // TODO: Return ConstantRange.
10279 } else if (const auto *A = dyn_cast<Argument>(V))
10280 if (std::optional<ConstantRange> Range = A->getRange())
10281 CR = *Range;
10282
10283 if (auto *I = dyn_cast<Instruction>(V)) {
10284 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10286
10287 if (const auto *CB = dyn_cast<CallBase>(V))
10288 if (std::optional<ConstantRange> Range = CB->getRange())
10289 CR = CR.intersectWith(*Range);
10290 }
10291
10292 if (CtxI && AC) {
10293 // Try to restrict the range based on information from assumptions.
10294 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10295 if (!AssumeVH)
10296 continue;
10297 CallInst *I = cast<CallInst>(AssumeVH);
10298 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10299 "Got assumption for the wrong function!");
10300 assert(I->getIntrinsicID() == Intrinsic::assume &&
10301 "must be an assume intrinsic");
10302
10303 if (!isValidAssumeForContext(I, CtxI, DT))
10304 continue;
10305 Value *Arg = I->getArgOperand(0);
10306 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10307 // Currently we just use information from comparisons.
10308 if (!Cmp || Cmp->getOperand(0) != V)
10309 continue;
10310 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10311 ConstantRange RHS =
10312 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10313 UseInstrInfo, AC, I, DT, Depth + 1);
10314 CR = CR.intersectWith(
10315 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10316 }
10317 }
10318
10319 return CR;
10320}
10321
10322static void
10324 function_ref<void(Value *)> InsertAffected) {
10325 assert(V != nullptr);
10326 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10327 InsertAffected(V);
10328 } else if (auto *I = dyn_cast<Instruction>(V)) {
10329 InsertAffected(V);
10330
10331 // Peek through unary operators to find the source of the condition.
10332 Value *Op;
10334 m_Trunc(m_Value(Op))))) {
10336 InsertAffected(Op);
10337 }
10338 }
10339}
10340
10342 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10343 auto AddAffected = [&InsertAffected](Value *V) {
10344 addValueAffectedByCondition(V, InsertAffected);
10345 };
10346
10347 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10348 if (IsAssume) {
10349 AddAffected(LHS);
10350 AddAffected(RHS);
10351 } else if (match(RHS, m_Constant()))
10352 AddAffected(LHS);
10353 };
10354
10355 SmallVector<Value *, 8> Worklist;
10357 Worklist.push_back(Cond);
10358 while (!Worklist.empty()) {
10359 Value *V = Worklist.pop_back_val();
10360 if (!Visited.insert(V).second)
10361 continue;
10362
10363 CmpPredicate Pred;
10364 Value *A, *B, *X;
10365
10366 if (IsAssume) {
10367 AddAffected(V);
10368 if (match(V, m_Not(m_Value(X))))
10369 AddAffected(X);
10370 }
10371
10372 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10373 // assume(A && B) is split to -> assume(A); assume(B);
10374 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10375 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10376 // enough information to be worth handling (intersection of information as
10377 // opposed to union).
10378 if (!IsAssume) {
10379 Worklist.push_back(A);
10380 Worklist.push_back(B);
10381 }
10382 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10383 bool HasRHSC = match(B, m_ConstantInt());
10384 if (ICmpInst::isEquality(Pred)) {
10385 AddAffected(A);
10386 if (IsAssume)
10387 AddAffected(B);
10388 if (HasRHSC) {
10389 Value *Y;
10390 // (X << C) or (X >>_s C) or (X >>_u C).
10391 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10392 AddAffected(X);
10393 // (X & C) or (X | C).
10394 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10395 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10396 AddAffected(X);
10397 AddAffected(Y);
10398 }
10399 // X - Y
10400 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10401 AddAffected(X);
10402 AddAffected(Y);
10403 }
10404 }
10405 } else {
10406 AddCmpOperands(A, B);
10407 if (HasRHSC) {
10408 // Handle (A + C1) u< C2, which is the canonical form of
10409 // A > C3 && A < C4.
10411 AddAffected(X);
10412
10413 if (ICmpInst::isUnsigned(Pred)) {
10414 Value *Y;
10415 // X & Y u> C -> X >u C && Y >u C
10416 // X | Y u< C -> X u< C && Y u< C
10417 // X nuw+ Y u< C -> X u< C && Y u< C
10418 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10419 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10420 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10421 AddAffected(X);
10422 AddAffected(Y);
10423 }
10424 // X nuw- Y u> C -> X u> C
10425 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10426 AddAffected(X);
10427 }
10428 }
10429
10430 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10431 // by computeKnownFPClass().
10433 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10434 InsertAffected(X);
10435 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10436 InsertAffected(X);
10437 }
10438 }
10439
10440 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10441 AddAffected(X);
10442 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10443 AddCmpOperands(A, B);
10444
10445 // fcmp fneg(x), y
10446 // fcmp fabs(x), y
10447 // fcmp fneg(fabs(x)), y
10448 if (match(A, m_FNeg(m_Value(A))))
10449 AddAffected(A);
10450 if (match(A, m_FAbs(m_Value(A))))
10451 AddAffected(A);
10452
10454 m_Value()))) {
10455 // Handle patterns that computeKnownFPClass() support.
10456 AddAffected(A);
10457 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10458 // Assume is checked here as X is already added above for assumes in
10459 // addValueAffectedByCondition
10460 AddAffected(X);
10461 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10462 // Assume is checked here to avoid issues with ephemeral values
10463 Worklist.push_back(X);
10464 }
10465 }
10466}
10467
10469 // (X >> C) or/add (X & mask(C) != 0)
10470 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10471 if (BO->getOpcode() == Instruction::Add ||
10472 BO->getOpcode() == Instruction::Or) {
10473 const Value *X;
10474 const APInt *C1, *C2;
10475 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10479 m_Zero())))) &&
10480 C2->popcount() == C1->getZExtValue())
10481 return X;
10482 }
10483 }
10484 return nullptr;
10485}
10486
10488 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10489}
10490
10493 unsigned MaxCount, bool AllowUndefOrPoison) {
10496 auto Push = [&](const Value *V) -> bool {
10497 Constant *C;
10498 if (match(const_cast<Value *>(V), m_ImmConstant(C))) {
10499 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10500 return false;
10501 // Check existence first to avoid unnecessary allocations.
10502 if (Constants.contains(C))
10503 return true;
10504 if (Constants.size() == MaxCount)
10505 return false;
10506 Constants.insert(C);
10507 return true;
10508 }
10509
10510 if (auto *Inst = dyn_cast<Instruction>(V)) {
10511 if (Visited.insert(Inst).second)
10512 Worklist.push_back(Inst);
10513 return true;
10514 }
10515 return false;
10516 };
10517 if (!Push(V))
10518 return false;
10519 while (!Worklist.empty()) {
10520 const Instruction *CurInst = Worklist.pop_back_val();
10521 switch (CurInst->getOpcode()) {
10522 case Instruction::Select:
10523 if (!Push(CurInst->getOperand(1)))
10524 return false;
10525 if (!Push(CurInst->getOperand(2)))
10526 return false;
10527 break;
10528 case Instruction::PHI:
10529 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10530 // Fast path for recurrence PHI.
10531 if (IncomingValue == CurInst)
10532 continue;
10533 if (!Push(IncomingValue))
10534 return false;
10535 }
10536 break;
10537 default:
10538 return false;
10539 }
10540 }
10541 return true;
10542}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Utilities for dealing with flags related to floating point properties and mode controls.
static Value * getCondition(Instruction *I)
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static SmallVector< VPValue *, 4 > getOperands(ArrayRef< VPValue * > Values, unsigned OperandIndex)
Definition VPlanSLP.cpp:210
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext, unsigned Depth=0)
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, SimplifyQuery &Q, unsigned Depth)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
static unsigned computeNumSignBitsVectorConstant(const Value *V, const APInt &DemandedElts, unsigned TyBits)
For vector constants, loop over the elements and find the constant with the minimum number of sign bi...
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V1 == (binop V2, X), where X is known non-zero.
static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q, unsigned Depth)
Test whether a GEP's result is known to be non-null.
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static constexpr unsigned MaxInstrsToCheckForFree
Maximum number of instructions to check between assume and context instruction.
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
static std::optional< bool > isImpliedCondFCmps(FCmpInst::Predicate LPred, const Value *L0, const Value *L1, FCmpInst::Predicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
UndefPoisonKind
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static bool includesPoison(UndefPoisonKind Kind)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool includesUndef(UndefPoisonKind Kind)
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ?
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1, const APInt &DemandedElts, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Try to detect the lerp pattern: a * (b - c) + c * d where a >= 0, b >= 0, c >= 0, d >= 0,...
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static constexpr KnownFPClass::MinMaxKind getMinMaxKind(Intrinsic::ID IID)
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:290
bool isFinite() const
Definition APFloat.h:1436
bool isNaN() const
Definition APFloat.h:1429
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1120
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1080
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1061
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1982
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1584
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1415
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:1549
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1400
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1679
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1394
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:1339
unsigned ceilLogBase2() const
Definition APInt.h:1773
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1202
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1677
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1497
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1250
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1655
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1405
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1167
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1637
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1607
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1052
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
unsigned logBase2() const
Definition APInt.h:1770
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:828
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:472
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:406
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1151
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1131
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1397
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1458
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
iterator end() const
Definition ArrayRef.h:131
size_t size() const
size - Get the array size.
Definition ArrayRef.h:142
iterator begin() const
Definition ArrayRef.h:130
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:186
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:223
LLVM_ABI bool isSingleEdge() const
Check if this is the only edge between Start and End.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:483
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:470
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:374
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
bool isSigned() const
Definition InstrTypes.h:930
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:827
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:942
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:770
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:789
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:765
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:893
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:776
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:936
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:707
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:598
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:673
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:781
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI std::optional< ConstantFPRange > makeExactFCmpRegion(FCmpInst::Predicate Pred, const APFloat &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:282
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI bool isAllNegative() const
Return true if all values in this range are negative.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition Constants.cpp:76
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:214
unsigned getAddressSizeInBits(unsigned AS) const
The size in bits of an address in for the given AS.
Definition DataLayout.h:507
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:771
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:164
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
This instruction compares its operands according to the predicate given to the constructor.
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:200
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:22
bool noSignedZeros() const
Definition FMF.h:67
bool noInfs() const
Definition FMF.h:66
void setNoSignedZeros(bool B=true)
Definition FMF.h:84
void setNoNaNs(bool B=true)
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:65
const BasicBlock & getEntryBlock() const
Definition Function.h:807
bool hasNoSync() const
Determine if the call can synchroize with other threads.
Definition Function.h:637
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
PointerType * getType() const
Global values are always pointers.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:132
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static LLVM_ABI std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
This instruction inserts a struct field of array element value into an aggregate value.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isUnaryOp() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1078
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A udiv, sdiv, lshr, or ashr instruction, which can be marked as "exact", indicating that no bits are ...
Definition Operator.h:154
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition Operator.h:173
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static LLVM_ABI void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:573
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:723
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:754
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Type * getElementType(unsigned N) const
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:297
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:246
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
bool isFloatTy() const
Return true if this is 'float', a 32-bit IEEE fp type.
Definition Type.h:153
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:295
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:311
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:230
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:270
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition Type.h:255
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:300
bool isFPOrFPVectorTy() const
Return true if this is a FP type or a vector of FP.
Definition Type.h:225
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:106
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
op_range operands()
Definition User.h:293
Value * getOperand(unsigned i) const
Definition User.h:233
unsigned getNumOperands() const
Definition User.h:255
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition Value.h:759
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition WithCache.h:59
PointerType getValue() const
Definition WithCache.h:57
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:168
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:165
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
A range adaptor for a pair of iterators.
CallInst * Call
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3020
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2272
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
auto m_PtrToIntOrAddr(const OpTy &Op)
Matches PtrToInt or PtrToAddr.
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
MatchFunctor< Val, Pattern > match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI)
Returns true, if no instruction between Assume and CtxI may free memory and the function is marked as...
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1737
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1667
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2544
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:229
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:303
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2198
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:420
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:284
gep_type_iterator gep_type_end(const User *GEP)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1516
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI Value * getSplatValue(const Value *V)
Get splat value if the input is a splat vector or return nullptr.
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:337
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:202
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1744
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
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 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:1945
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:872
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ IEEE
IEEE-754 denormal numbers preserved.
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:304
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static LLVM_ABI std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:189
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:258
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h: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:254
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:245
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:277
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:164
KnownBits byteSwap() const
Definition KnownBits.h:522
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:292
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:526
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:175
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:324
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:228
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:314
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:183
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:248
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:350
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:199
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:251
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:60
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:329
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:356
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:283
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:222
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:170
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:209
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 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.
void copysign(const KnownFPClass &Sign)
static KnownFPClass square(const KnownFPClass &Src, DenormalMode Mode=DenormalMode::getDynamic())
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
bool isKnownAlways(FPClassTest Mask) const
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 roundToIntegral(const KnownFPClass &Src, bool IsTrunc, bool IsMultiUnitFPType)
Propagate known class for rounding intrinsics (trunc, floor, ceil, rint, nearbyint,...
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
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
static LLVM_ABI KnownFPClass exp(const KnownFPClass &Src)
Report known values for exp, exp2 and exp10.
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.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
LLVM_ABI void propagateCanonicalizingSrc(const KnownFPClass &Src, DenormalMode Mode)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void signBitMustBeZero()
Assume the sign bit is zero.
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.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's known this can never be interpreted as a negative zero.
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC