LLVM 22.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Constant.h"
44#include "llvm/IR/Constants.h"
47#include "llvm/IR/Dominators.h"
49#include "llvm/IR/Function.h"
51#include "llvm/IR/GlobalAlias.h"
52#include "llvm/IR/GlobalValue.h"
54#include "llvm/IR/InstrTypes.h"
55#include "llvm/IR/Instruction.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/IntrinsicsAArch64.h"
60#include "llvm/IR/IntrinsicsAMDGPU.h"
61#include "llvm/IR/IntrinsicsRISCV.h"
62#include "llvm/IR/IntrinsicsX86.h"
63#include "llvm/IR/LLVMContext.h"
64#include "llvm/IR/Metadata.h"
65#include "llvm/IR/Module.h"
66#include "llvm/IR/Operator.h"
68#include "llvm/IR/Type.h"
69#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
79#include <algorithm>
80#include <cassert>
81#include <cstdint>
82#include <optional>
83#include <utility>
84
85using namespace llvm;
86using namespace llvm::PatternMatch;
87
88// Controls the number of uses of the value searched for possible
89// dominating comparisons.
90static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
91 cl::Hidden, cl::init(20));
92
93/// Maximum number of instructions to check between assume and context
94/// instruction.
95static constexpr unsigned MaxInstrsToCheckForFree = 16;
96
97/// Returns the bitwidth of the given scalar or pointer type. For vector types,
98/// returns the element type's bitwidth.
99static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
100 if (unsigned BitWidth = Ty->getScalarSizeInBits())
101 return BitWidth;
102
103 return DL.getPointerTypeSizeInBits(Ty);
104}
105
106// Given the provided Value and, potentially, a context instruction, return
107// the preferred context instruction (if any).
108static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
109 // If we've been provided with a context instruction, then use that (provided
110 // it has been inserted).
111 if (CxtI && CxtI->getParent())
112 return CxtI;
113
114 // If the value is really an already-inserted instruction, then use that.
115 CxtI = dyn_cast<Instruction>(V);
116 if (CxtI && CxtI->getParent())
117 return CxtI;
118
119 return nullptr;
120}
121
123 const APInt &DemandedElts,
124 APInt &DemandedLHS, APInt &DemandedRHS) {
125 if (isa<ScalableVectorType>(Shuf->getType())) {
126 assert(DemandedElts == APInt(1,1));
127 DemandedLHS = DemandedRHS = DemandedElts;
128 return true;
129 }
130
131 int NumElts =
132 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
133 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
134 DemandedElts, DemandedLHS, DemandedRHS);
135}
136
137static void computeKnownBits(const Value *V, const APInt &DemandedElts,
138 KnownBits &Known, const SimplifyQuery &Q,
139 unsigned Depth);
140
142 const SimplifyQuery &Q, unsigned Depth) {
143 // Since the number of lanes in a scalable vector is unknown at compile time,
144 // we track one bit which is implicitly broadcast to all lanes. This means
145 // that all lanes in a scalable vector are considered demanded.
146 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
147 APInt DemandedElts =
148 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
149 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
150}
151
153 const DataLayout &DL, AssumptionCache *AC,
154 const Instruction *CxtI, const DominatorTree *DT,
155 bool UseInstrInfo, unsigned Depth) {
156 computeKnownBits(V, Known,
157 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
158 Depth);
159}
160
162 AssumptionCache *AC, const Instruction *CxtI,
163 const DominatorTree *DT, bool UseInstrInfo,
164 unsigned Depth) {
165 return computeKnownBits(
166 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
167}
168
169KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
170 const DataLayout &DL, AssumptionCache *AC,
171 const Instruction *CxtI,
172 const DominatorTree *DT, bool UseInstrInfo,
173 unsigned Depth) {
174 return computeKnownBits(
175 V, DemandedElts,
176 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
177}
178
180 const SimplifyQuery &SQ) {
181 // Look for an inverted mask: (X & ~M) op (Y & M).
182 {
183 Value *M;
184 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
186 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
187 return true;
188 }
189
190 // X op (Y & ~X)
193 return true;
194
195 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
196 // for constant Y.
197 Value *Y;
198 if (match(RHS,
200 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
201 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
202 return true;
203
204 // Peek through extends to find a 'not' of the other side:
205 // (ext Y) op ext(~Y)
206 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
208 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
209 return true;
210
211 // Look for: (A & B) op ~(A | B)
212 {
213 Value *A, *B;
214 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
216 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
217 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
218 return true;
219 }
220
221 // Look for: (X << V) op (Y >> (BitWidth - V))
222 // or (X >> V) op (Y << (BitWidth - V))
223 {
224 const Value *V;
225 const APInt *R;
226 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
227 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
228 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
229 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
230 R->uge(LHS->getType()->getScalarSizeInBits()))
231 return true;
232 }
233
234 return false;
235}
236
238 const WithCache<const Value *> &RHSCache,
239 const SimplifyQuery &SQ) {
240 const Value *LHS = LHSCache.getValue();
241 const Value *RHS = RHSCache.getValue();
242
243 assert(LHS->getType() == RHS->getType() &&
244 "LHS and RHS should have the same type");
245 assert(LHS->getType()->isIntOrIntVectorTy() &&
246 "LHS and RHS should be integers");
247
248 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
250 return true;
251
253 RHSCache.getKnownBits(SQ));
254}
255
257 return !I->user_empty() &&
258 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
259}
260
262 return !I->user_empty() && all_of(I->users(), [](const User *U) {
263 CmpPredicate P;
264 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
265 });
266}
267
269 bool OrZero, AssumptionCache *AC,
270 const Instruction *CxtI,
271 const DominatorTree *DT, bool UseInstrInfo,
272 unsigned Depth) {
273 return ::isKnownToBeAPowerOfTwo(
274 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
275 Depth);
276}
277
278static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
279 const SimplifyQuery &Q, unsigned Depth);
280
282 unsigned Depth) {
283 return computeKnownBits(V, SQ, Depth).isNonNegative();
284}
285
287 unsigned Depth) {
288 if (auto *CI = dyn_cast<ConstantInt>(V))
289 return CI->getValue().isStrictlyPositive();
290
291 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
292 // this updated.
293 KnownBits Known = computeKnownBits(V, SQ, Depth);
294 return Known.isNonNegative() &&
295 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
296}
297
299 unsigned Depth) {
300 return computeKnownBits(V, SQ, Depth).isNegative();
301}
302
303static bool isKnownNonEqual(const Value *V1, const Value *V2,
304 const APInt &DemandedElts, const SimplifyQuery &Q,
305 unsigned Depth);
306
307bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
308 const SimplifyQuery &Q, unsigned Depth) {
309 // We don't support looking through casts.
310 if (V1 == V2 || V1->getType() != V2->getType())
311 return false;
312 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
313 APInt DemandedElts =
314 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
315 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
316}
317
318bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
319 const SimplifyQuery &SQ, unsigned Depth) {
320 KnownBits Known(Mask.getBitWidth());
321 computeKnownBits(V, Known, SQ, Depth);
322 return Mask.isSubsetOf(Known.Zero);
323}
324
325static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
326 const SimplifyQuery &Q, unsigned Depth);
327
328static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
329 unsigned Depth = 0) {
330 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
331 APInt DemandedElts =
332 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
333 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
334}
335
336unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
337 AssumptionCache *AC, const Instruction *CxtI,
338 const DominatorTree *DT, bool UseInstrInfo,
339 unsigned Depth) {
340 return ::ComputeNumSignBits(
341 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
342}
343
345 AssumptionCache *AC,
346 const Instruction *CxtI,
347 const DominatorTree *DT,
348 unsigned Depth) {
349 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
350 return V->getType()->getScalarSizeInBits() - SignBits + 1;
351}
352
353/// Try to detect the lerp pattern: a * (b - c) + c * d
354/// where a >= 0, b >= 0, c >= 0, d >= 0, and b >= c.
355///
356/// In that particular case, we can use the following chain of reasoning:
357///
358/// a * (b - c) + c * d <= a' * (b - c) + a' * c = a' * b where a' = max(a, d)
359///
360/// Since that is true for arbitrary a, b, c and d within our constraints, we
361/// can conclude that:
362///
363/// max(a * (b - c) + c * d) <= max(max(a), max(d)) * max(b) = U
364///
365/// Considering that any result of the lerp would be less or equal to U, it
366/// would have at least the number of leading 0s as in U.
367///
368/// While being quite a specific situation, it is fairly common in computer
369/// graphics in the shape of alpha blending.
370///
371/// Modifies given KnownOut in-place with the inferred information.
372static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1,
373 const APInt &DemandedElts,
374 KnownBits &KnownOut,
375 const SimplifyQuery &Q,
376 unsigned Depth) {
377
378 Type *Ty = Op0->getType();
379 const unsigned BitWidth = Ty->getScalarSizeInBits();
380
381 // Only handle scalar types for now
382 if (Ty->isVectorTy())
383 return;
384
385 // Try to match: a * (b - c) + c * d.
386 // When a == 1 => A == nullptr, the same applies to d/D as well.
387 const Value *A = nullptr, *B = nullptr, *C = nullptr, *D = nullptr;
388 const Instruction *SubBC = nullptr;
389
390 const auto MatchSubBC = [&]() {
391 // (b - c) can have two forms that interest us:
392 //
393 // 1. sub nuw %b, %c
394 // 2. xor %c, %b
395 //
396 // For the first case, nuw flag guarantees our requirement b >= c.
397 //
398 // The second case might happen when the analysis can infer that b is a mask
399 // for c and we can transform sub operation into xor (that is usually true
400 // for constant b's). Even though xor is symmetrical, canonicalization
401 // ensures that the constant will be the RHS. We have additional checks
402 // later on to ensure that this xor operation is equivalent to subtraction.
404 m_Xor(m_Value(C), m_Value(B))));
405 };
406
407 const auto MatchASubBC = [&]() {
408 // Cases:
409 // - a * (b - c)
410 // - (b - c) * a
411 // - (b - c) <- a implicitly equals 1
412 return m_CombineOr(m_c_Mul(m_Value(A), MatchSubBC()), MatchSubBC());
413 };
414
415 const auto MatchCD = [&]() {
416 // Cases:
417 // - d * c
418 // - c * d
419 // - c <- d implicitly equals 1
421 };
422
423 const auto Match = [&](const Value *LHS, const Value *RHS) {
424 // We do use m_Specific(C) in MatchCD, so we have to make sure that
425 // it's bound to anything and match(LHS, MatchASubBC()) absolutely
426 // has to evaluate first and return true.
427 //
428 // If Match returns true, it is guaranteed that B != nullptr, C != nullptr.
429 return match(LHS, MatchASubBC()) && match(RHS, MatchCD());
430 };
431
432 if (!Match(Op0, Op1) && !Match(Op1, Op0))
433 return;
434
435 const auto ComputeKnownBitsOrOne = [&](const Value *V) {
436 // For some of the values we use the convention of leaving
437 // it nullptr to signify an implicit constant 1.
438 return V ? computeKnownBits(V, DemandedElts, Q, Depth + 1)
440 };
441
442 // Check that all operands are non-negative
443 const KnownBits KnownA = ComputeKnownBitsOrOne(A);
444 if (!KnownA.isNonNegative())
445 return;
446
447 const KnownBits KnownD = ComputeKnownBitsOrOne(D);
448 if (!KnownD.isNonNegative())
449 return;
450
451 const KnownBits KnownB = computeKnownBits(B, DemandedElts, Q, Depth + 1);
452 if (!KnownB.isNonNegative())
453 return;
454
455 const KnownBits KnownC = computeKnownBits(C, DemandedElts, Q, Depth + 1);
456 if (!KnownC.isNonNegative())
457 return;
458
459 // If we matched subtraction as xor, we need to actually check that xor
460 // is semantically equivalent to subtraction.
461 //
462 // For that to be true, b has to be a mask for c or that b's known
463 // ones cover all known and possible ones of c.
464 if (SubBC->getOpcode() == Instruction::Xor &&
465 !KnownC.getMaxValue().isSubsetOf(KnownB.getMinValue()))
466 return;
467
468 const APInt MaxA = KnownA.getMaxValue();
469 const APInt MaxD = KnownD.getMaxValue();
470 const APInt MaxAD = APIntOps::umax(MaxA, MaxD);
471 const APInt MaxB = KnownB.getMaxValue();
472
473 // We can't infer leading zeros info if the upper-bound estimate wraps.
474 bool Overflow;
475 const APInt UpperBound = MaxAD.umul_ov(MaxB, Overflow);
476
477 if (Overflow)
478 return;
479
480 // If we know that x <= y and both are positive than x has at least the same
481 // number of leading zeros as y.
482 const unsigned MinimumNumberOfLeadingZeros = UpperBound.countl_zero();
483 KnownOut.Zero.setHighBits(MinimumNumberOfLeadingZeros);
484}
485
486static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
487 bool NSW, bool NUW,
488 const APInt &DemandedElts,
489 KnownBits &KnownOut, KnownBits &Known2,
490 const SimplifyQuery &Q, unsigned Depth) {
491 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
492
493 // If one operand is unknown and we have no nowrap information,
494 // the result will be unknown independently of the second operand.
495 if (KnownOut.isUnknown() && !NSW && !NUW)
496 return;
497
498 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
499 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
500
501 if (!Add && NSW && !KnownOut.isNonNegative() &&
503 .value_or(false))
504 KnownOut.makeNonNegative();
505
506 if (Add)
507 // Try to match lerp pattern and combine results
508 computeKnownBitsFromLerpPattern(Op0, Op1, DemandedElts, KnownOut, Q, Depth);
509}
510
511static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
512 bool NUW, const APInt &DemandedElts,
513 KnownBits &Known, KnownBits &Known2,
514 const SimplifyQuery &Q, unsigned Depth) {
515 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
516 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
517
518 bool isKnownNegative = false;
519 bool isKnownNonNegative = false;
520 // If the multiplication is known not to overflow, compute the sign bit.
521 if (NSW) {
522 if (Op0 == Op1) {
523 // The product of a number with itself is non-negative.
524 isKnownNonNegative = true;
525 } else {
526 bool isKnownNonNegativeOp1 = Known.isNonNegative();
527 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
528 bool isKnownNegativeOp1 = Known.isNegative();
529 bool isKnownNegativeOp0 = Known2.isNegative();
530 // The product of two numbers with the same sign is non-negative.
531 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
532 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
533 if (!isKnownNonNegative && NUW) {
534 // mul nuw nsw with a factor > 1 is non-negative.
536 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
537 KnownBits::sgt(Known2, One).value_or(false);
538 }
539
540 // The product of a negative number and a non-negative number is either
541 // negative or zero.
544 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
545 Known2.isNonZero()) ||
546 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
547 }
548 }
549
550 bool SelfMultiply = Op0 == Op1;
551 if (SelfMultiply)
552 SelfMultiply &=
553 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
554 Known = KnownBits::mul(Known, Known2, SelfMultiply);
555
556 if (SelfMultiply) {
557 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
558 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
559 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
560
561 if (OutValidBits < TyBits) {
562 APInt KnownZeroMask =
563 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
564 Known.Zero |= KnownZeroMask;
565 }
566 }
567
568 // Only make use of no-wrap flags if we failed to compute the sign bit
569 // directly. This matters if the multiplication always overflows, in
570 // which case we prefer to follow the result of the direct computation,
571 // though as the program is invoking undefined behaviour we can choose
572 // whatever we like here.
573 if (isKnownNonNegative && !Known.isNegative())
574 Known.makeNonNegative();
575 else if (isKnownNegative && !Known.isNonNegative())
576 Known.makeNegative();
577}
578
580 KnownBits &Known) {
581 unsigned BitWidth = Known.getBitWidth();
582 unsigned NumRanges = Ranges.getNumOperands() / 2;
583 assert(NumRanges >= 1);
584
585 Known.setAllConflict();
586
587 for (unsigned i = 0; i < NumRanges; ++i) {
589 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
591 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
592 ConstantRange Range(Lower->getValue(), Upper->getValue());
593 // BitWidth must equal the Ranges BitWidth for the correct number of high
594 // bits to be set.
595 assert(BitWidth == Range.getBitWidth() &&
596 "Known bit width must match range bit width!");
597
598 // The first CommonPrefixBits of all values in Range are equal.
599 unsigned CommonPrefixBits =
600 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
601 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
602 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
603 Known.One &= UnsignedMax & Mask;
604 Known.Zero &= ~UnsignedMax & Mask;
605 }
606}
607
608static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
612
613 // The instruction defining an assumption's condition itself is always
614 // considered ephemeral to that assumption (even if it has other
615 // non-ephemeral users). See r246696's test case for an example.
616 if (is_contained(I->operands(), E))
617 return true;
618
619 while (!WorkSet.empty()) {
620 const Instruction *V = WorkSet.pop_back_val();
621 if (!Visited.insert(V).second)
622 continue;
623
624 // If all uses of this value are ephemeral, then so is this value.
625 if (all_of(V->users(), [&](const User *U) {
626 return EphValues.count(cast<Instruction>(U));
627 })) {
628 if (V == E)
629 return true;
630
631 if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {
632 EphValues.insert(V);
633
634 if (const User *U = dyn_cast<User>(V)) {
635 for (const Use &U : U->operands()) {
636 if (const auto *I = dyn_cast<Instruction>(U.get()))
637 WorkSet.push_back(I);
638 }
639 }
640 }
641 }
642 }
643
644 return false;
645}
646
647// Is this an intrinsic that cannot be speculated but also cannot trap?
649 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
650 return CI->isAssumeLikeIntrinsic();
651
652 return false;
653}
654
656 const Instruction *CxtI,
657 const DominatorTree *DT,
658 bool AllowEphemerals) {
659 // There are two restrictions on the use of an assume:
660 // 1. The assume must dominate the context (or the control flow must
661 // reach the assume whenever it reaches the context).
662 // 2. The context must not be in the assume's set of ephemeral values
663 // (otherwise we will use the assume to prove that the condition
664 // feeding the assume is trivially true, thus causing the removal of
665 // the assume).
666
667 if (Inv->getParent() == CxtI->getParent()) {
668 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
669 // in the BB.
670 if (Inv->comesBefore(CxtI))
671 return true;
672
673 // Don't let an assume affect itself - this would cause the problems
674 // `isEphemeralValueOf` is trying to prevent, and it would also make
675 // the loop below go out of bounds.
676 if (!AllowEphemerals && Inv == CxtI)
677 return false;
678
679 // The context comes first, but they're both in the same block.
680 // Make sure there is nothing in between that might interrupt
681 // the control flow, not even CxtI itself.
682 // We limit the scan distance between the assume and its context instruction
683 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
684 // it can be adjusted if needed (could be turned into a cl::opt).
685 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
687 return false;
688
689 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
690 }
691
692 // Inv and CxtI are in different blocks.
693 if (DT) {
694 if (DT->dominates(Inv, CxtI))
695 return true;
696 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
697 Inv->getParent()->isEntryBlock()) {
698 // We don't have a DT, but this trivially dominates.
699 return true;
700 }
701
702 return false;
703}
704
706 const Instruction *CtxI) {
707 if (CtxI->getParent() != Assume->getParent() || !Assume->comesBefore(CtxI))
708 return false;
709 // Make sure the current function cannot arrange for another thread to free on
710 // its behalf.
711 if (!CtxI->getFunction()->hasNoSync())
712 return false;
713
714 // Check if there are any calls between the assume and CtxI that may
715 // free memory.
716 for (const auto &[Idx, I] :
717 enumerate(make_range(Assume->getIterator(), CtxI->getIterator()))) {
718 // Limit number of instructions to walk.
719 if (Idx > MaxInstrsToCheckForFree)
720 return false;
721 if (const auto *CB = dyn_cast<CallBase>(&I))
722 if (!CB->hasFnAttr(Attribute::NoFree))
723 return false;
724 }
725 return true;
726}
727
728// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
729// we still have enough information about `RHS` to conclude non-zero. For
730// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
731// so the extra compile time may not be worth it, but possibly a second API
732// should be created for use outside of loops.
733static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
734 // v u> y implies v != 0.
735 if (Pred == ICmpInst::ICMP_UGT)
736 return true;
737
738 // Special-case v != 0 to also handle v != null.
739 if (Pred == ICmpInst::ICMP_NE)
740 return match(RHS, m_Zero());
741
742 // All other predicates - rely on generic ConstantRange handling.
743 const APInt *C;
744 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
745 if (match(RHS, m_APInt(C))) {
747 return !TrueValues.contains(Zero);
748 }
749
751 if (VC == nullptr)
752 return false;
753
754 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
755 ++ElemIdx) {
757 Pred, VC->getElementAsAPInt(ElemIdx));
758 if (TrueValues.contains(Zero))
759 return false;
760 }
761 return true;
762}
763
764static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
765 Value *&ValOut, Instruction *&CtxIOut,
766 const PHINode **PhiOut = nullptr) {
767 ValOut = U->get();
768 if (ValOut == PHI)
769 return;
770 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
771 if (PhiOut)
772 *PhiOut = PHI;
773 Value *V;
774 // If the Use is a select of this phi, compute analysis on other arm to break
775 // recursion.
776 // TODO: Min/Max
777 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
778 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
779 ValOut = V;
780
781 // Same for select, if this phi is 2-operand phi, compute analysis on other
782 // incoming value to break recursion.
783 // TODO: We could handle any number of incoming edges as long as we only have
784 // two unique values.
785 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
786 IncPhi && IncPhi->getNumIncomingValues() == 2) {
787 for (int Idx = 0; Idx < 2; ++Idx) {
788 if (IncPhi->getIncomingValue(Idx) == PHI) {
789 ValOut = IncPhi->getIncomingValue(1 - Idx);
790 if (PhiOut)
791 *PhiOut = IncPhi;
792 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
793 break;
794 }
795 }
796 }
797}
798
799static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
800 // Use of assumptions is context-sensitive. If we don't have a context, we
801 // cannot use them!
802 if (!Q.AC || !Q.CxtI)
803 return false;
804
805 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
806 if (!Elem.Assume)
807 continue;
808
809 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
810 assert(I->getFunction() == Q.CxtI->getFunction() &&
811 "Got assumption for the wrong function!");
812
813 if (Elem.Index != AssumptionCache::ExprResultIdx) {
814 if (!V->getType()->isPointerTy())
815 continue;
817 *I, I->bundle_op_info_begin()[Elem.Index])) {
818 if (RK.WasOn == V &&
819 (RK.AttrKind == Attribute::NonNull ||
820 (RK.AttrKind == Attribute::Dereferenceable &&
822 V->getType()->getPointerAddressSpace()))) &&
824 return true;
825 }
826 continue;
827 }
828
829 // Warning: This loop can end up being somewhat performance sensitive.
830 // We're running this loop for once for each value queried resulting in a
831 // runtime of ~O(#assumes * #values).
832
833 Value *RHS;
834 CmpPredicate Pred;
835 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
836 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
837 continue;
838
839 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
840 return true;
841 }
842
843 return false;
844}
845
847 Value *LHS, Value *RHS, KnownBits &Known,
848 const SimplifyQuery &Q) {
849 if (RHS->getType()->isPointerTy()) {
850 // Handle comparison of pointer to null explicitly, as it will not be
851 // covered by the m_APInt() logic below.
852 if (LHS == V && match(RHS, m_Zero())) {
853 switch (Pred) {
855 Known.setAllZero();
856 break;
859 Known.makeNonNegative();
860 break;
862 Known.makeNegative();
863 break;
864 default:
865 break;
866 }
867 }
868 return;
869 }
870
871 unsigned BitWidth = Known.getBitWidth();
872 auto m_V =
874
875 Value *Y;
876 const APInt *Mask, *C;
877 if (!match(RHS, m_APInt(C)))
878 return;
879
880 uint64_t ShAmt;
881 switch (Pred) {
883 // assume(V = C)
884 if (match(LHS, m_V)) {
885 Known = Known.unionWith(KnownBits::makeConstant(*C));
886 // assume(V & Mask = C)
887 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
888 // For one bits in Mask, we can propagate bits from C to V.
889 Known.One |= *C;
890 if (match(Y, m_APInt(Mask)))
891 Known.Zero |= ~*C & *Mask;
892 // assume(V | Mask = C)
893 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
894 // For zero bits in Mask, we can propagate bits from C to V.
895 Known.Zero |= ~*C;
896 if (match(Y, m_APInt(Mask)))
897 Known.One |= *C & ~*Mask;
898 // assume(V << ShAmt = C)
899 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
900 ShAmt < BitWidth) {
901 // For those bits in C that are known, we can propagate them to known
902 // bits in V shifted to the right by ShAmt.
904 RHSKnown >>= ShAmt;
905 Known = Known.unionWith(RHSKnown);
906 // assume(V >> ShAmt = C)
907 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
908 ShAmt < BitWidth) {
909 // For those bits in RHS that are known, we can propagate them to known
910 // bits in V shifted to the right by C.
912 RHSKnown <<= ShAmt;
913 Known = Known.unionWith(RHSKnown);
914 }
915 break;
916 case ICmpInst::ICMP_NE: {
917 // assume (V & B != 0) where B is a power of 2
918 const APInt *BPow2;
919 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
920 Known.One |= *BPow2;
921 break;
922 }
923 default: {
924 const APInt *Offset = nullptr;
925 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
927 if (Offset)
928 LHSRange = LHSRange.sub(*Offset);
929 Known = Known.unionWith(LHSRange.toKnownBits());
930 }
931 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
932 // X & Y u> C -> X u> C && Y u> C
933 // X nuw- Y u> C -> X u> C
934 if (match(LHS, m_c_And(m_V, m_Value())) ||
935 match(LHS, m_NUWSub(m_V, m_Value())))
936 Known.One.setHighBits(
937 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
938 }
939 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
940 // X | Y u< C -> X u< C && Y u< C
941 // X nuw+ Y u< C -> X u< C && Y u< C
942 if (match(LHS, m_c_Or(m_V, m_Value())) ||
943 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
944 Known.Zero.setHighBits(
945 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
946 }
947 }
948 } break;
949 }
950}
951
952static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
953 KnownBits &Known,
954 const SimplifyQuery &SQ, bool Invert) {
956 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
957 Value *LHS = Cmp->getOperand(0);
958 Value *RHS = Cmp->getOperand(1);
959
960 // Handle icmp pred (trunc V), C
961 if (match(LHS, m_Trunc(m_Specific(V)))) {
962 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
963 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
965 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
966 else
967 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
968 return;
969 }
970
971 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
972}
973
975 KnownBits &Known, const SimplifyQuery &SQ,
976 bool Invert, unsigned Depth) {
977 Value *A, *B;
980 KnownBits Known2(Known.getBitWidth());
981 KnownBits Known3(Known.getBitWidth());
982 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
983 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
984 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
986 Known2 = Known2.unionWith(Known3);
987 else
988 Known2 = Known2.intersectWith(Known3);
989 Known = Known.unionWith(Known2);
990 return;
991 }
992
993 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
994 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
995 return;
996 }
997
998 if (match(Cond, m_Trunc(m_Specific(V)))) {
999 KnownBits DstKnown(1);
1000 if (Invert) {
1001 DstKnown.setAllZero();
1002 } else {
1003 DstKnown.setAllOnes();
1004 }
1006 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
1007 return;
1008 }
1009 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
1010 return;
1011 }
1012
1014 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
1015}
1016
1018 const SimplifyQuery &Q, unsigned Depth) {
1019 // Handle injected condition.
1020 if (Q.CC && Q.CC->AffectedValues.contains(V))
1021 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
1022
1023 if (!Q.CxtI)
1024 return;
1025
1026 if (Q.DC && Q.DT) {
1027 // Handle dominating conditions.
1028 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
1029 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
1030 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
1031 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1032 /*Invert*/ false, Depth);
1033
1034 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
1035 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
1036 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
1037 /*Invert*/ true, Depth);
1038 }
1039
1040 if (Known.hasConflict())
1041 Known.resetAll();
1042 }
1043
1044 if (!Q.AC)
1045 return;
1046
1047 unsigned BitWidth = Known.getBitWidth();
1048
1049 // Note that the patterns below need to be kept in sync with the code
1050 // in AssumptionCache::updateAffectedValues.
1051
1052 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
1053 if (!Elem.Assume)
1054 continue;
1055
1056 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
1057 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
1058 "Got assumption for the wrong function!");
1059
1060 if (Elem.Index != AssumptionCache::ExprResultIdx) {
1061 if (!V->getType()->isPointerTy())
1062 continue;
1064 *I, I->bundle_op_info_begin()[Elem.Index])) {
1065 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
1066 // be the producer of the pointer in the bundle. At the moment, align
1067 // assumptions aren't optimized away.
1068 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
1069 isPowerOf2_64(RK.ArgValue) &&
1070 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
1071 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
1072 }
1073 continue;
1074 }
1075
1076 // Warning: This loop can end up being somewhat performance sensitive.
1077 // We're running this loop for once for each value queried resulting in a
1078 // runtime of ~O(#assumes * #values).
1079
1080 Value *Arg = I->getArgOperand(0);
1081
1082 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
1083 assert(BitWidth == 1 && "assume operand is not i1?");
1084 (void)BitWidth;
1085 Known.setAllOnes();
1086 return;
1087 }
1088 if (match(Arg, m_Not(m_Specific(V))) &&
1090 assert(BitWidth == 1 && "assume operand is not i1?");
1091 (void)BitWidth;
1092 Known.setAllZero();
1093 return;
1094 }
1095 auto *Trunc = dyn_cast<TruncInst>(Arg);
1096 if (Trunc && Trunc->getOperand(0) == V &&
1098 if (Trunc->hasNoUnsignedWrap()) {
1100 return;
1101 }
1102 Known.One.setBit(0);
1103 return;
1104 }
1105
1106 // The remaining tests are all recursive, so bail out if we hit the limit.
1108 continue;
1109
1110 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
1111 if (!Cmp)
1112 continue;
1113
1114 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
1115 continue;
1116
1117 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
1118 }
1119
1120 // Conflicting assumption: Undefined behavior will occur on this execution
1121 // path.
1122 if (Known.hasConflict())
1123 Known.resetAll();
1124}
1125
1126/// Compute known bits from a shift operator, including those with a
1127/// non-constant shift amount. Known is the output of this function. Known2 is a
1128/// pre-allocated temporary with the same bit width as Known and on return
1129/// contains the known bit of the shift value source. KF is an
1130/// operator-specific function that, given the known-bits and a shift amount,
1131/// compute the implied known-bits of the shift operator's result respectively
1132/// for that shift amount. The results from calling KF are conservatively
1133/// combined for all permitted shift amounts.
1135 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
1136 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
1137 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1138 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1139 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1140 // To limit compile-time impact, only query isKnownNonZero() if we know at
1141 // least something about the shift amount.
1142 bool ShAmtNonZero =
1143 Known.isNonZero() ||
1144 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1145 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1146 Known = KF(Known2, Known, ShAmtNonZero);
1147}
1148
1149static KnownBits
1150getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1151 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1152 const SimplifyQuery &Q, unsigned Depth) {
1153 unsigned BitWidth = KnownLHS.getBitWidth();
1154 KnownBits KnownOut(BitWidth);
1155 bool IsAnd = false;
1156 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1157 Value *X = nullptr, *Y = nullptr;
1158
1159 switch (I->getOpcode()) {
1160 case Instruction::And:
1161 KnownOut = KnownLHS & KnownRHS;
1162 IsAnd = true;
1163 // and(x, -x) is common idioms that will clear all but lowest set
1164 // bit. If we have a single known bit in x, we can clear all bits
1165 // above it.
1166 // TODO: instcombine often reassociates independent `and` which can hide
1167 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1168 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1169 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1170 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1171 KnownOut = KnownLHS.blsi();
1172 else
1173 KnownOut = KnownRHS.blsi();
1174 }
1175 break;
1176 case Instruction::Or:
1177 KnownOut = KnownLHS | KnownRHS;
1178 break;
1179 case Instruction::Xor:
1180 KnownOut = KnownLHS ^ KnownRHS;
1181 // xor(x, x-1) is common idioms that will clear all but lowest set
1182 // bit. If we have a single known bit in x, we can clear all bits
1183 // above it.
1184 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1185 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1186 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1187 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1188 if (HasKnownOne &&
1190 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1191 KnownOut = XBits.blsmsk();
1192 }
1193 break;
1194 default:
1195 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1196 }
1197
1198 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1199 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1200 // here we handle the more general case of adding any odd number by
1201 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1202 // TODO: This could be generalized to clearing any bit set in y where the
1203 // following bit is known to be unset in y.
1204 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1208 KnownBits KnownY(BitWidth);
1209 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1210 if (KnownY.countMinTrailingOnes() > 0) {
1211 if (IsAnd)
1212 KnownOut.Zero.setBit(0);
1213 else
1214 KnownOut.One.setBit(0);
1215 }
1216 }
1217 return KnownOut;
1218}
1219
1221 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1222 unsigned Depth,
1223 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1224 KnownBitsFunc) {
1225 APInt DemandedEltsLHS, DemandedEltsRHS;
1227 DemandedElts, DemandedEltsLHS,
1228 DemandedEltsRHS);
1229
1230 const auto ComputeForSingleOpFunc =
1231 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1232 return KnownBitsFunc(
1233 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1234 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1235 };
1236
1237 if (DemandedEltsRHS.isZero())
1238 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1239 if (DemandedEltsLHS.isZero())
1240 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1241
1242 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1243 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1244}
1245
1246// Public so this can be used in `SimplifyDemandedUseBits`.
1248 const KnownBits &KnownLHS,
1249 const KnownBits &KnownRHS,
1250 const SimplifyQuery &SQ,
1251 unsigned Depth) {
1252 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1253 APInt DemandedElts =
1254 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1255
1256 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1257 Depth);
1258}
1259
1261 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1262 // Without vscale_range, we only know that vscale is non-zero.
1263 if (!Attr.isValid())
1265
1266 unsigned AttrMin = Attr.getVScaleRangeMin();
1267 // Minimum is larger than vscale width, result is always poison.
1268 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1269 return ConstantRange::getEmpty(BitWidth);
1270
1271 APInt Min(BitWidth, AttrMin);
1272 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1273 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1275
1276 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1277}
1278
1280 Value *Arm, bool Invert,
1281 const SimplifyQuery &Q, unsigned Depth) {
1282 // If we have a constant arm, we are done.
1283 if (Known.isConstant())
1284 return;
1285
1286 // See what condition implies about the bits of the select arm.
1287 KnownBits CondRes(Known.getBitWidth());
1288 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1289 // If we don't get any information from the condition, no reason to
1290 // proceed.
1291 if (CondRes.isUnknown())
1292 return;
1293
1294 // We can have conflict if the condition is dead. I.e if we have
1295 // (x | 64) < 32 ? (x | 64) : y
1296 // we will have conflict at bit 6 from the condition/the `or`.
1297 // In that case just return. Its not particularly important
1298 // what we do, as this select is going to be simplified soon.
1299 CondRes = CondRes.unionWith(Known);
1300 if (CondRes.hasConflict())
1301 return;
1302
1303 // Finally make sure the information we found is valid. This is relatively
1304 // expensive so it's left for the very end.
1305 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1306 return;
1307
1308 // Finally, we know we get information from the condition and its valid,
1309 // so return it.
1310 Known = CondRes;
1311}
1312
1313// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1314// Returns the input and lower/upper bounds.
1315static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1316 const APInt *&CLow, const APInt *&CHigh) {
1318 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1319 "Input should be a Select!");
1320
1321 const Value *LHS = nullptr, *RHS = nullptr;
1323 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1324 return false;
1325
1326 if (!match(RHS, m_APInt(CLow)))
1327 return false;
1328
1329 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1331 if (getInverseMinMaxFlavor(SPF) != SPF2)
1332 return false;
1333
1334 if (!match(RHS2, m_APInt(CHigh)))
1335 return false;
1336
1337 if (SPF == SPF_SMIN)
1338 std::swap(CLow, CHigh);
1339
1340 In = LHS2;
1341 return CLow->sle(*CHigh);
1342}
1343
1345 const APInt *&CLow,
1346 const APInt *&CHigh) {
1347 assert((II->getIntrinsicID() == Intrinsic::smin ||
1348 II->getIntrinsicID() == Intrinsic::smax) &&
1349 "Must be smin/smax");
1350
1351 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1352 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1353 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1354 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1355 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1356 return false;
1357
1358 if (II->getIntrinsicID() == Intrinsic::smin)
1359 std::swap(CLow, CHigh);
1360 return CLow->sle(*CHigh);
1361}
1362
1364 KnownBits &Known) {
1365 const APInt *CLow, *CHigh;
1366 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1367 Known = Known.unionWith(
1368 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1369}
1370
1372 const APInt &DemandedElts,
1373 KnownBits &Known,
1374 const SimplifyQuery &Q,
1375 unsigned Depth) {
1376 unsigned BitWidth = Known.getBitWidth();
1377
1378 KnownBits Known2(BitWidth);
1379 switch (I->getOpcode()) {
1380 default: break;
1381 case Instruction::Load:
1382 if (MDNode *MD =
1383 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1385 break;
1386 case Instruction::And:
1387 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1388 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1389
1390 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1391 break;
1392 case Instruction::Or:
1393 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1394 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1395
1396 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1397 break;
1398 case Instruction::Xor:
1399 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1400 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1401
1402 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1403 break;
1404 case Instruction::Mul: {
1407 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1408 DemandedElts, Known, Known2, Q, Depth);
1409 break;
1410 }
1411 case Instruction::UDiv: {
1412 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1413 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1414 Known =
1415 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1416 break;
1417 }
1418 case Instruction::SDiv: {
1419 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1420 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1421 Known =
1422 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1423 break;
1424 }
1425 case Instruction::Select: {
1426 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1427 KnownBits Res(Known.getBitWidth());
1428 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1429 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1430 return Res;
1431 };
1432 // Only known if known in both the LHS and RHS.
1433 Known =
1434 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1435 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1436 break;
1437 }
1438 case Instruction::FPTrunc:
1439 case Instruction::FPExt:
1440 case Instruction::FPToUI:
1441 case Instruction::FPToSI:
1442 case Instruction::SIToFP:
1443 case Instruction::UIToFP:
1444 break; // Can't work with floating point.
1445 case Instruction::PtrToInt:
1446 case Instruction::IntToPtr:
1447 // Fall through and handle them the same as zext/trunc.
1448 [[fallthrough]];
1449 case Instruction::ZExt:
1450 case Instruction::Trunc: {
1451 Type *SrcTy = I->getOperand(0)->getType();
1452
1453 unsigned SrcBitWidth;
1454 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1455 // which fall through here.
1456 Type *ScalarTy = SrcTy->getScalarType();
1457 SrcBitWidth = ScalarTy->isPointerTy() ?
1458 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1459 Q.DL.getTypeSizeInBits(ScalarTy);
1460
1461 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1462 Known = Known.anyextOrTrunc(SrcBitWidth);
1463 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1464 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1465 Inst && Inst->hasNonNeg() && !Known.isNegative())
1466 Known.makeNonNegative();
1467 Known = Known.zextOrTrunc(BitWidth);
1468 break;
1469 }
1470 case Instruction::BitCast: {
1471 Type *SrcTy = I->getOperand(0)->getType();
1472 if (SrcTy->isIntOrPtrTy() &&
1473 // TODO: For now, not handling conversions like:
1474 // (bitcast i64 %x to <2 x i32>)
1475 !I->getType()->isVectorTy()) {
1476 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1477 break;
1478 }
1479
1480 const Value *V;
1481 // Handle bitcast from floating point to integer.
1482 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1483 V->getType()->isFPOrFPVectorTy()) {
1484 Type *FPType = V->getType()->getScalarType();
1485 KnownFPClass Result =
1486 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1487 FPClassTest FPClasses = Result.KnownFPClasses;
1488
1489 // TODO: Treat it as zero/poison if the use of I is unreachable.
1490 if (FPClasses == fcNone)
1491 break;
1492
1493 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1494 Known.setAllConflict();
1495
1496 if (FPClasses & fcInf)
1498 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1499
1500 if (FPClasses & fcZero)
1502 APInt::getZero(FPType->getScalarSizeInBits())));
1503
1504 Known.Zero.clearSignBit();
1505 Known.One.clearSignBit();
1506 }
1507
1508 if (Result.SignBit) {
1509 if (*Result.SignBit)
1510 Known.makeNegative();
1511 else
1512 Known.makeNonNegative();
1513 }
1514
1515 break;
1516 }
1517
1518 // Handle cast from vector integer type to scalar or vector integer.
1519 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1520 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1521 !I->getType()->isIntOrIntVectorTy() ||
1522 isa<ScalableVectorType>(I->getType()))
1523 break;
1524
1525 unsigned NumElts = DemandedElts.getBitWidth();
1526 bool IsLE = Q.DL.isLittleEndian();
1527 // Look through a cast from narrow vector elements to wider type.
1528 // Examples: v4i32 -> v2i64, v3i8 -> v24
1529 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1530 if (BitWidth % SubBitWidth == 0) {
1531 // Known bits are automatically intersected across demanded elements of a
1532 // vector. So for example, if a bit is computed as known zero, it must be
1533 // zero across all demanded elements of the vector.
1534 //
1535 // For this bitcast, each demanded element of the output is sub-divided
1536 // across a set of smaller vector elements in the source vector. To get
1537 // the known bits for an entire element of the output, compute the known
1538 // bits for each sub-element sequentially. This is done by shifting the
1539 // one-set-bit demanded elements parameter across the sub-elements for
1540 // consecutive calls to computeKnownBits. We are using the demanded
1541 // elements parameter as a mask operator.
1542 //
1543 // The known bits of each sub-element are then inserted into place
1544 // (dependent on endian) to form the full result of known bits.
1545 unsigned SubScale = BitWidth / SubBitWidth;
1546 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1547 for (unsigned i = 0; i != NumElts; ++i) {
1548 if (DemandedElts[i])
1549 SubDemandedElts.setBit(i * SubScale);
1550 }
1551
1552 KnownBits KnownSrc(SubBitWidth);
1553 for (unsigned i = 0; i != SubScale; ++i) {
1554 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1555 Depth + 1);
1556 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1557 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1558 }
1559 }
1560 // Look through a cast from wider vector elements to narrow type.
1561 // Examples: v2i64 -> v4i32
1562 if (SubBitWidth % BitWidth == 0) {
1563 unsigned SubScale = SubBitWidth / BitWidth;
1564 KnownBits KnownSrc(SubBitWidth);
1565 APInt SubDemandedElts =
1566 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1567 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1568 Depth + 1);
1569
1570 Known.setAllConflict();
1571 for (unsigned i = 0; i != NumElts; ++i) {
1572 if (DemandedElts[i]) {
1573 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1574 unsigned Offset = (Shifts % SubScale) * BitWidth;
1575 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1576 if (Known.isUnknown())
1577 break;
1578 }
1579 }
1580 }
1581 break;
1582 }
1583 case Instruction::SExt: {
1584 // Compute the bits in the result that are not present in the input.
1585 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1586
1587 Known = Known.trunc(SrcBitWidth);
1588 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1589 // If the sign bit of the input is known set or clear, then we know the
1590 // top bits of the result.
1591 Known = Known.sext(BitWidth);
1592 break;
1593 }
1594 case Instruction::Shl: {
1597 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1598 bool ShAmtNonZero) {
1599 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1600 };
1601 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1602 KF);
1603 // Trailing zeros of a right-shifted constant never decrease.
1604 const APInt *C;
1605 if (match(I->getOperand(0), m_APInt(C)))
1606 Known.Zero.setLowBits(C->countr_zero());
1607 break;
1608 }
1609 case Instruction::LShr: {
1610 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1611 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1612 bool ShAmtNonZero) {
1613 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1614 };
1615 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1616 KF);
1617 // Leading zeros of a left-shifted constant never decrease.
1618 const APInt *C;
1619 if (match(I->getOperand(0), m_APInt(C)))
1620 Known.Zero.setHighBits(C->countl_zero());
1621 break;
1622 }
1623 case Instruction::AShr: {
1624 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1625 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1626 bool ShAmtNonZero) {
1627 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1628 };
1629 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1630 KF);
1631 break;
1632 }
1633 case Instruction::Sub: {
1636 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1637 DemandedElts, Known, Known2, Q, Depth);
1638 break;
1639 }
1640 case Instruction::Add: {
1643 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1644 DemandedElts, Known, Known2, Q, Depth);
1645 break;
1646 }
1647 case Instruction::SRem:
1648 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1649 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1650 Known = KnownBits::srem(Known, Known2);
1651 break;
1652
1653 case Instruction::URem:
1654 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1655 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1656 Known = KnownBits::urem(Known, Known2);
1657 break;
1658 case Instruction::Alloca:
1660 break;
1661 case Instruction::GetElementPtr: {
1662 // Analyze all of the subscripts of this getelementptr instruction
1663 // to determine if we can prove known low zero bits.
1664 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1665 // Accumulate the constant indices in a separate variable
1666 // to minimize the number of calls to computeForAddSub.
1667 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1668 APInt AccConstIndices(IndexWidth, 0);
1669
1670 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1671 if (IndexWidth == BitWidth) {
1672 // Note that inbounds does *not* guarantee nsw for the addition, as only
1673 // the offset is signed, while the base address is unsigned.
1674 Known = KnownBits::add(Known, IndexBits);
1675 } else {
1676 // If the index width is smaller than the pointer width, only add the
1677 // value to the low bits.
1678 assert(IndexWidth < BitWidth &&
1679 "Index width can't be larger than pointer width");
1680 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1681 }
1682 };
1683
1685 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1686 // TrailZ can only become smaller, short-circuit if we hit zero.
1687 if (Known.isUnknown())
1688 break;
1689
1690 Value *Index = I->getOperand(i);
1691
1692 // Handle case when index is zero.
1693 Constant *CIndex = dyn_cast<Constant>(Index);
1694 if (CIndex && CIndex->isZeroValue())
1695 continue;
1696
1697 if (StructType *STy = GTI.getStructTypeOrNull()) {
1698 // Handle struct member offset arithmetic.
1699
1700 assert(CIndex &&
1701 "Access to structure field must be known at compile time");
1702
1703 if (CIndex->getType()->isVectorTy())
1704 Index = CIndex->getSplatValue();
1705
1706 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1707 const StructLayout *SL = Q.DL.getStructLayout(STy);
1708 uint64_t Offset = SL->getElementOffset(Idx);
1709 AccConstIndices += Offset;
1710 continue;
1711 }
1712
1713 // Handle array index arithmetic.
1714 Type *IndexedTy = GTI.getIndexedType();
1715 if (!IndexedTy->isSized()) {
1716 Known.resetAll();
1717 break;
1718 }
1719
1720 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1721 uint64_t StrideInBytes = Stride.getKnownMinValue();
1722 if (!Stride.isScalable()) {
1723 // Fast path for constant offset.
1724 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1725 AccConstIndices +=
1726 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1727 continue;
1728 }
1729 }
1730
1731 KnownBits IndexBits =
1732 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1733 KnownBits ScalingFactor(IndexWidth);
1734 // Multiply by current sizeof type.
1735 // &A[i] == A + i * sizeof(*A[i]).
1736 if (Stride.isScalable()) {
1737 // For scalable types the only thing we know about sizeof is
1738 // that this is a multiple of the minimum size.
1739 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1740 } else {
1741 ScalingFactor =
1742 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1743 }
1744 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1745 }
1746 if (!Known.isUnknown() && !AccConstIndices.isZero())
1747 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1748 break;
1749 }
1750 case Instruction::PHI: {
1751 const PHINode *P = cast<PHINode>(I);
1752 BinaryOperator *BO = nullptr;
1753 Value *R = nullptr, *L = nullptr;
1754 if (matchSimpleRecurrence(P, BO, R, L)) {
1755 // Handle the case of a simple two-predecessor recurrence PHI.
1756 // There's a lot more that could theoretically be done here, but
1757 // this is sufficient to catch some interesting cases.
1758 unsigned Opcode = BO->getOpcode();
1759
1760 switch (Opcode) {
1761 // If this is a shift recurrence, we know the bits being shifted in. We
1762 // can combine that with information about the start value of the
1763 // recurrence to conclude facts about the result. If this is a udiv
1764 // recurrence, we know that the result can never exceed either the
1765 // numerator or the start value, whichever is greater.
1766 case Instruction::LShr:
1767 case Instruction::AShr:
1768 case Instruction::Shl:
1769 case Instruction::UDiv:
1770 if (BO->getOperand(0) != I)
1771 break;
1772 [[fallthrough]];
1773
1774 // For a urem recurrence, the result can never exceed the start value. The
1775 // phi could either be the numerator or the denominator.
1776 case Instruction::URem: {
1777 // We have matched a recurrence of the form:
1778 // %iv = [R, %entry], [%iv.next, %backedge]
1779 // %iv.next = shift_op %iv, L
1780
1781 // Recurse with the phi context to avoid concern about whether facts
1782 // inferred hold at original context instruction. TODO: It may be
1783 // correct to use the original context. IF warranted, explore and
1784 // add sufficient tests to cover.
1786 RecQ.CxtI = P;
1787 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1788 switch (Opcode) {
1789 case Instruction::Shl:
1790 // A shl recurrence will only increase the tailing zeros
1791 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1792 break;
1793 case Instruction::LShr:
1794 case Instruction::UDiv:
1795 case Instruction::URem:
1796 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1797 // the start value.
1798 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1799 break;
1800 case Instruction::AShr:
1801 // An ashr recurrence will extend the initial sign bit
1802 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1803 Known.One.setHighBits(Known2.countMinLeadingOnes());
1804 break;
1805 }
1806 break;
1807 }
1808
1809 // Check for operations that have the property that if
1810 // both their operands have low zero bits, the result
1811 // will have low zero bits.
1812 case Instruction::Add:
1813 case Instruction::Sub:
1814 case Instruction::And:
1815 case Instruction::Or:
1816 case Instruction::Mul: {
1817 // Change the context instruction to the "edge" that flows into the
1818 // phi. This is important because that is where the value is actually
1819 // "evaluated" even though it is used later somewhere else. (see also
1820 // D69571).
1822
1823 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1824 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1825 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1826
1827 // Ok, we have a PHI of the form L op= R. Check for low
1828 // zero bits.
1829 RecQ.CxtI = RInst;
1830 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1831
1832 // We need to take the minimum number of known bits
1833 KnownBits Known3(BitWidth);
1834 RecQ.CxtI = LInst;
1835 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1836
1837 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1838 Known3.countMinTrailingZeros()));
1839
1840 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1841 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1842 break;
1843
1844 switch (Opcode) {
1845 // If initial value of recurrence is nonnegative, and we are adding
1846 // a nonnegative number with nsw, the result can only be nonnegative
1847 // or poison value regardless of the number of times we execute the
1848 // add in phi recurrence. If initial value is negative and we are
1849 // adding a negative number with nsw, the result can only be
1850 // negative or poison value. Similar arguments apply to sub and mul.
1851 //
1852 // (add non-negative, non-negative) --> non-negative
1853 // (add negative, negative) --> negative
1854 case Instruction::Add: {
1855 if (Known2.isNonNegative() && Known3.isNonNegative())
1856 Known.makeNonNegative();
1857 else if (Known2.isNegative() && Known3.isNegative())
1858 Known.makeNegative();
1859 break;
1860 }
1861
1862 // (sub nsw non-negative, negative) --> non-negative
1863 // (sub nsw negative, non-negative) --> negative
1864 case Instruction::Sub: {
1865 if (BO->getOperand(0) != I)
1866 break;
1867 if (Known2.isNonNegative() && Known3.isNegative())
1868 Known.makeNonNegative();
1869 else if (Known2.isNegative() && Known3.isNonNegative())
1870 Known.makeNegative();
1871 break;
1872 }
1873
1874 // (mul nsw non-negative, non-negative) --> non-negative
1875 case Instruction::Mul:
1876 if (Known2.isNonNegative() && Known3.isNonNegative())
1877 Known.makeNonNegative();
1878 break;
1879
1880 default:
1881 break;
1882 }
1883 break;
1884 }
1885
1886 default:
1887 break;
1888 }
1889 }
1890
1891 // Unreachable blocks may have zero-operand PHI nodes.
1892 if (P->getNumIncomingValues() == 0)
1893 break;
1894
1895 // Otherwise take the unions of the known bit sets of the operands,
1896 // taking conservative care to avoid excessive recursion.
1897 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1898 // Skip if every incoming value references to ourself.
1899 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1900 break;
1901
1902 Known.setAllConflict();
1903 for (const Use &U : P->operands()) {
1904 Value *IncValue;
1905 const PHINode *CxtPhi;
1906 Instruction *CxtI;
1907 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1908 // Skip direct self references.
1909 if (IncValue == P)
1910 continue;
1911
1912 // Change the context instruction to the "edge" that flows into the
1913 // phi. This is important because that is where the value is actually
1914 // "evaluated" even though it is used later somewhere else. (see also
1915 // D69571).
1917
1918 Known2 = KnownBits(BitWidth);
1919
1920 // Recurse, but cap the recursion to one level, because we don't
1921 // want to waste time spinning around in loops.
1922 // TODO: See if we can base recursion limiter on number of incoming phi
1923 // edges so we don't overly clamp analysis.
1924 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1926
1927 // See if we can further use a conditional branch into the phi
1928 // to help us determine the range of the value.
1929 if (!Known2.isConstant()) {
1930 CmpPredicate Pred;
1931 const APInt *RHSC;
1932 BasicBlock *TrueSucc, *FalseSucc;
1933 // TODO: Use RHS Value and compute range from its known bits.
1934 if (match(RecQ.CxtI,
1935 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1936 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1937 // Check for cases of duplicate successors.
1938 if ((TrueSucc == CxtPhi->getParent()) !=
1939 (FalseSucc == CxtPhi->getParent())) {
1940 // If we're using the false successor, invert the predicate.
1941 if (FalseSucc == CxtPhi->getParent())
1942 Pred = CmpInst::getInversePredicate(Pred);
1943 // Get the knownbits implied by the incoming phi condition.
1944 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1945 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1946 // We can have conflicts here if we are analyzing deadcode (its
1947 // impossible for us reach this BB based the icmp).
1948 if (KnownUnion.hasConflict()) {
1949 // No reason to continue analyzing in a known dead region, so
1950 // just resetAll and break. This will cause us to also exit the
1951 // outer loop.
1952 Known.resetAll();
1953 break;
1954 }
1955 Known2 = KnownUnion;
1956 }
1957 }
1958 }
1959
1960 Known = Known.intersectWith(Known2);
1961 // If all bits have been ruled out, there's no need to check
1962 // more operands.
1963 if (Known.isUnknown())
1964 break;
1965 }
1966 }
1967 break;
1968 }
1969 case Instruction::Call:
1970 case Instruction::Invoke: {
1971 // If range metadata is attached to this call, set known bits from that,
1972 // and then intersect with known bits based on other properties of the
1973 // function.
1974 if (MDNode *MD =
1975 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1977
1978 const auto *CB = cast<CallBase>(I);
1979
1980 if (std::optional<ConstantRange> Range = CB->getRange())
1981 Known = Known.unionWith(Range->toKnownBits());
1982
1983 if (const Value *RV = CB->getReturnedArgOperand()) {
1984 if (RV->getType() == I->getType()) {
1985 computeKnownBits(RV, Known2, Q, Depth + 1);
1986 Known = Known.unionWith(Known2);
1987 // If the function doesn't return properly for all input values
1988 // (e.g. unreachable exits) then there might be conflicts between the
1989 // argument value and the range metadata. Simply discard the known bits
1990 // in case of conflicts.
1991 if (Known.hasConflict())
1992 Known.resetAll();
1993 }
1994 }
1995 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1996 switch (II->getIntrinsicID()) {
1997 default:
1998 break;
1999 case Intrinsic::abs: {
2000 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2001 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
2002 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
2003 break;
2004 }
2005 case Intrinsic::bitreverse:
2006 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2007 Known = Known.unionWith(Known2.reverseBits());
2008 break;
2009 case Intrinsic::bswap:
2010 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2011 Known = Known.unionWith(Known2.byteSwap());
2012 break;
2013 case Intrinsic::ctlz: {
2014 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2015 // If we have a known 1, its position is our upper bound.
2016 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
2017 // If this call is poison for 0 input, the result will be less than 2^n.
2018 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2019 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
2020 unsigned LowBits = llvm::bit_width(PossibleLZ);
2021 Known.Zero.setBitsFrom(LowBits);
2022 break;
2023 }
2024 case Intrinsic::cttz: {
2025 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2026 // If we have a known 1, its position is our upper bound.
2027 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
2028 // If this call is poison for 0 input, the result will be less than 2^n.
2029 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
2030 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
2031 unsigned LowBits = llvm::bit_width(PossibleTZ);
2032 Known.Zero.setBitsFrom(LowBits);
2033 break;
2034 }
2035 case Intrinsic::ctpop: {
2036 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2037 // We can bound the space the count needs. Also, bits known to be zero
2038 // can't contribute to the population.
2039 unsigned BitsPossiblySet = Known2.countMaxPopulation();
2040 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
2041 Known.Zero.setBitsFrom(LowBits);
2042 // TODO: we could bound KnownOne using the lower bound on the number
2043 // of bits which might be set provided by popcnt KnownOne2.
2044 break;
2045 }
2046 case Intrinsic::fshr:
2047 case Intrinsic::fshl: {
2048 const APInt *SA;
2049 if (!match(I->getOperand(2), m_APInt(SA)))
2050 break;
2051
2052 // Normalize to funnel shift left.
2053 uint64_t ShiftAmt = SA->urem(BitWidth);
2054 if (II->getIntrinsicID() == Intrinsic::fshr)
2055 ShiftAmt = BitWidth - ShiftAmt;
2056
2057 KnownBits Known3(BitWidth);
2058 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
2059 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
2060
2061 Known2 <<= ShiftAmt;
2062 Known3 >>= BitWidth - ShiftAmt;
2063 Known = Known2.unionWith(Known3);
2064 break;
2065 }
2066 case Intrinsic::uadd_sat:
2067 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2068 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2069 Known = KnownBits::uadd_sat(Known, Known2);
2070 break;
2071 case Intrinsic::usub_sat:
2072 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2073 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2074 Known = KnownBits::usub_sat(Known, Known2);
2075 break;
2076 case Intrinsic::sadd_sat:
2077 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2078 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2079 Known = KnownBits::sadd_sat(Known, Known2);
2080 break;
2081 case Intrinsic::ssub_sat:
2082 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2083 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2084 Known = KnownBits::ssub_sat(Known, Known2);
2085 break;
2086 // Vec reverse preserves bits from input vec.
2087 case Intrinsic::vector_reverse:
2088 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
2089 Depth + 1);
2090 break;
2091 // for min/max/and/or reduce, any bit common to each element in the
2092 // input vec is set in the output.
2093 case Intrinsic::vector_reduce_and:
2094 case Intrinsic::vector_reduce_or:
2095 case Intrinsic::vector_reduce_umax:
2096 case Intrinsic::vector_reduce_umin:
2097 case Intrinsic::vector_reduce_smax:
2098 case Intrinsic::vector_reduce_smin:
2099 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2100 break;
2101 case Intrinsic::vector_reduce_xor: {
2102 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2103 // The zeros common to all vecs are zero in the output.
2104 // If the number of elements is odd, then the common ones remain. If the
2105 // number of elements is even, then the common ones becomes zeros.
2106 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
2107 // Even, so the ones become zeros.
2108 bool EvenCnt = VecTy->getElementCount().isKnownEven();
2109 if (EvenCnt)
2110 Known.Zero |= Known.One;
2111 // Maybe even element count so need to clear ones.
2112 if (VecTy->isScalableTy() || EvenCnt)
2113 Known.One.clearAllBits();
2114 break;
2115 }
2116 case Intrinsic::umin:
2117 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2118 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2119 Known = KnownBits::umin(Known, Known2);
2120 break;
2121 case Intrinsic::umax:
2122 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2123 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2124 Known = KnownBits::umax(Known, Known2);
2125 break;
2126 case Intrinsic::smin:
2127 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2128 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2129 Known = KnownBits::smin(Known, Known2);
2131 break;
2132 case Intrinsic::smax:
2133 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2134 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2135 Known = KnownBits::smax(Known, Known2);
2137 break;
2138 case Intrinsic::ptrmask: {
2139 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2140
2141 const Value *Mask = I->getOperand(1);
2142 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2143 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2144 // TODO: 1-extend would be more precise.
2145 Known &= Known2.anyextOrTrunc(BitWidth);
2146 break;
2147 }
2148 case Intrinsic::x86_sse2_pmulh_w:
2149 case Intrinsic::x86_avx2_pmulh_w:
2150 case Intrinsic::x86_avx512_pmulh_w_512:
2151 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2152 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2153 Known = KnownBits::mulhs(Known, Known2);
2154 break;
2155 case Intrinsic::x86_sse2_pmulhu_w:
2156 case Intrinsic::x86_avx2_pmulhu_w:
2157 case Intrinsic::x86_avx512_pmulhu_w_512:
2158 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2159 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2160 Known = KnownBits::mulhu(Known, Known2);
2161 break;
2162 case Intrinsic::x86_sse42_crc32_64_64:
2163 Known.Zero.setBitsFrom(32);
2164 break;
2165 case Intrinsic::x86_ssse3_phadd_d_128:
2166 case Intrinsic::x86_ssse3_phadd_w_128:
2167 case Intrinsic::x86_avx2_phadd_d:
2168 case Intrinsic::x86_avx2_phadd_w: {
2170 I, DemandedElts, Q, Depth,
2171 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2172 return KnownBits::add(KnownLHS, KnownRHS);
2173 });
2174 break;
2175 }
2176 case Intrinsic::x86_ssse3_phadd_sw_128:
2177 case Intrinsic::x86_avx2_phadd_sw: {
2179 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2180 break;
2181 }
2182 case Intrinsic::x86_ssse3_phsub_d_128:
2183 case Intrinsic::x86_ssse3_phsub_w_128:
2184 case Intrinsic::x86_avx2_phsub_d:
2185 case Intrinsic::x86_avx2_phsub_w: {
2187 I, DemandedElts, Q, Depth,
2188 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2189 return KnownBits::sub(KnownLHS, KnownRHS);
2190 });
2191 break;
2192 }
2193 case Intrinsic::x86_ssse3_phsub_sw_128:
2194 case Intrinsic::x86_avx2_phsub_sw: {
2196 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2197 break;
2198 }
2199 case Intrinsic::riscv_vsetvli:
2200 case Intrinsic::riscv_vsetvlimax: {
2201 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2202 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2204 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2205 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2206 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2207 uint64_t MaxVLEN =
2208 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2209 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2210
2211 // Result of vsetvli must be not larger than AVL.
2212 if (HasAVL)
2213 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2214 MaxVL = std::min(MaxVL, CI->getZExtValue());
2215
2216 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2217 if (BitWidth > KnownZeroFirstBit)
2218 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2219 break;
2220 }
2221 case Intrinsic::vscale: {
2222 if (!II->getParent() || !II->getFunction())
2223 break;
2224
2225 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2226 break;
2227 }
2228 }
2229 }
2230 break;
2231 }
2232 case Instruction::ShuffleVector: {
2233 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2234 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2235 if (!Shuf) {
2236 Known.resetAll();
2237 return;
2238 }
2239 // For undef elements, we don't know anything about the common state of
2240 // the shuffle result.
2241 APInt DemandedLHS, DemandedRHS;
2242 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2243 Known.resetAll();
2244 return;
2245 }
2246 Known.setAllConflict();
2247 if (!!DemandedLHS) {
2248 const Value *LHS = Shuf->getOperand(0);
2249 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2250 // If we don't know any bits, early out.
2251 if (Known.isUnknown())
2252 break;
2253 }
2254 if (!!DemandedRHS) {
2255 const Value *RHS = Shuf->getOperand(1);
2256 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2257 Known = Known.intersectWith(Known2);
2258 }
2259 break;
2260 }
2261 case Instruction::InsertElement: {
2262 if (isa<ScalableVectorType>(I->getType())) {
2263 Known.resetAll();
2264 return;
2265 }
2266 const Value *Vec = I->getOperand(0);
2267 const Value *Elt = I->getOperand(1);
2268 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2269 unsigned NumElts = DemandedElts.getBitWidth();
2270 APInt DemandedVecElts = DemandedElts;
2271 bool NeedsElt = true;
2272 // If we know the index we are inserting too, clear it from Vec check.
2273 if (CIdx && CIdx->getValue().ult(NumElts)) {
2274 DemandedVecElts.clearBit(CIdx->getZExtValue());
2275 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2276 }
2277
2278 Known.setAllConflict();
2279 if (NeedsElt) {
2280 computeKnownBits(Elt, Known, Q, Depth + 1);
2281 // If we don't know any bits, early out.
2282 if (Known.isUnknown())
2283 break;
2284 }
2285
2286 if (!DemandedVecElts.isZero()) {
2287 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2288 Known = Known.intersectWith(Known2);
2289 }
2290 break;
2291 }
2292 case Instruction::ExtractElement: {
2293 // Look through extract element. If the index is non-constant or
2294 // out-of-range demand all elements, otherwise just the extracted element.
2295 const Value *Vec = I->getOperand(0);
2296 const Value *Idx = I->getOperand(1);
2297 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2298 if (isa<ScalableVectorType>(Vec->getType())) {
2299 // FIXME: there's probably *something* we can do with scalable vectors
2300 Known.resetAll();
2301 break;
2302 }
2303 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2304 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2305 if (CIdx && CIdx->getValue().ult(NumElts))
2306 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2307 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2308 break;
2309 }
2310 case Instruction::ExtractValue:
2311 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2313 if (EVI->getNumIndices() != 1) break;
2314 if (EVI->getIndices()[0] == 0) {
2315 switch (II->getIntrinsicID()) {
2316 default: break;
2317 case Intrinsic::uadd_with_overflow:
2318 case Intrinsic::sadd_with_overflow:
2320 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2321 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2322 break;
2323 case Intrinsic::usub_with_overflow:
2324 case Intrinsic::ssub_with_overflow:
2326 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2327 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2328 break;
2329 case Intrinsic::umul_with_overflow:
2330 case Intrinsic::smul_with_overflow:
2331 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2332 false, DemandedElts, Known, Known2, Q, Depth);
2333 break;
2334 }
2335 }
2336 }
2337 break;
2338 case Instruction::Freeze:
2339 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2340 Depth + 1))
2341 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2342 break;
2343 }
2344}
2345
2346/// Determine which bits of V are known to be either zero or one and return
2347/// them.
2348KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2349 const SimplifyQuery &Q, unsigned Depth) {
2350 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2351 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2352 return Known;
2353}
2354
2355/// Determine which bits of V are known to be either zero or one and return
2356/// them.
2358 unsigned Depth) {
2359 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2360 computeKnownBits(V, Known, Q, Depth);
2361 return Known;
2362}
2363
2364/// Determine which bits of V are known to be either zero or one and return
2365/// them in the Known bit set.
2366///
2367/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2368/// we cannot optimize based on the assumption that it is zero without changing
2369/// it to be an explicit zero. If we don't change it to zero, other code could
2370/// optimized based on the contradictory assumption that it is non-zero.
2371/// Because instcombine aggressively folds operations with undef args anyway,
2372/// this won't lose us code quality.
2373///
2374/// This function is defined on values with integer type, values with pointer
2375/// type, and vectors of integers. In the case
2376/// where V is a vector, known zero, and known one values are the
2377/// same width as the vector element, and the bit is set only if it is true
2378/// for all of the demanded elements in the vector specified by DemandedElts.
2379void computeKnownBits(const Value *V, const APInt &DemandedElts,
2380 KnownBits &Known, const SimplifyQuery &Q,
2381 unsigned Depth) {
2382 if (!DemandedElts) {
2383 // No demanded elts, better to assume we don't know anything.
2384 Known.resetAll();
2385 return;
2386 }
2387
2388 assert(V && "No Value?");
2389 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2390
2391#ifndef NDEBUG
2392 Type *Ty = V->getType();
2393 unsigned BitWidth = Known.getBitWidth();
2394
2395 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2396 "Not integer or pointer type!");
2397
2398 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2399 assert(
2400 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2401 "DemandedElt width should equal the fixed vector number of elements");
2402 } else {
2403 assert(DemandedElts == APInt(1, 1) &&
2404 "DemandedElt width should be 1 for scalars or scalable vectors");
2405 }
2406
2407 Type *ScalarTy = Ty->getScalarType();
2408 if (ScalarTy->isPointerTy()) {
2409 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2410 "V and Known should have same BitWidth");
2411 } else {
2412 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2413 "V and Known should have same BitWidth");
2414 }
2415#endif
2416
2417 const APInt *C;
2418 if (match(V, m_APInt(C))) {
2419 // We know all of the bits for a scalar constant or a splat vector constant!
2420 Known = KnownBits::makeConstant(*C);
2421 return;
2422 }
2423 // Null and aggregate-zero are all-zeros.
2425 Known.setAllZero();
2426 return;
2427 }
2428 // Handle a constant vector by taking the intersection of the known bits of
2429 // each element.
2431 assert(!isa<ScalableVectorType>(V->getType()));
2432 // We know that CDV must be a vector of integers. Take the intersection of
2433 // each element.
2434 Known.setAllConflict();
2435 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2436 if (!DemandedElts[i])
2437 continue;
2438 APInt Elt = CDV->getElementAsAPInt(i);
2439 Known.Zero &= ~Elt;
2440 Known.One &= Elt;
2441 }
2442 if (Known.hasConflict())
2443 Known.resetAll();
2444 return;
2445 }
2446
2447 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2448 assert(!isa<ScalableVectorType>(V->getType()));
2449 // We know that CV must be a vector of integers. Take the intersection of
2450 // each element.
2451 Known.setAllConflict();
2452 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2453 if (!DemandedElts[i])
2454 continue;
2455 Constant *Element = CV->getAggregateElement(i);
2456 if (isa<PoisonValue>(Element))
2457 continue;
2458 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2459 if (!ElementCI) {
2460 Known.resetAll();
2461 return;
2462 }
2463 const APInt &Elt = ElementCI->getValue();
2464 Known.Zero &= ~Elt;
2465 Known.One &= Elt;
2466 }
2467 if (Known.hasConflict())
2468 Known.resetAll();
2469 return;
2470 }
2471
2472 // Start out not knowing anything.
2473 Known.resetAll();
2474
2475 // We can't imply anything about undefs.
2476 if (isa<UndefValue>(V))
2477 return;
2478
2479 // There's no point in looking through other users of ConstantData for
2480 // assumptions. Confirm that we've handled them all.
2481 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2482
2483 if (const auto *A = dyn_cast<Argument>(V))
2484 if (std::optional<ConstantRange> Range = A->getRange())
2485 Known = Range->toKnownBits();
2486
2487 // All recursive calls that increase depth must come after this.
2489 return;
2490
2491 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2492 // the bits of its aliasee.
2493 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2494 if (!GA->isInterposable())
2495 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2496 return;
2497 }
2498
2499 if (const Operator *I = dyn_cast<Operator>(V))
2500 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2501 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2502 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2503 Known = CR->toKnownBits();
2504 }
2505
2506 // Aligned pointers have trailing zeros - refine Known.Zero set
2507 if (isa<PointerType>(V->getType())) {
2508 Align Alignment = V->getPointerAlignment(Q.DL);
2509 Known.Zero.setLowBits(Log2(Alignment));
2510 }
2511
2512 // computeKnownBitsFromContext strictly refines Known.
2513 // Therefore, we run them after computeKnownBitsFromOperator.
2514
2515 // Check whether we can determine known bits from context such as assumes.
2516 computeKnownBitsFromContext(V, Known, Q, Depth);
2517}
2518
2519/// Try to detect a recurrence that the value of the induction variable is
2520/// always a power of two (or zero).
2521static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2522 SimplifyQuery &Q, unsigned Depth) {
2523 BinaryOperator *BO = nullptr;
2524 Value *Start = nullptr, *Step = nullptr;
2525 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2526 return false;
2527
2528 // Initial value must be a power of two.
2529 for (const Use &U : PN->operands()) {
2530 if (U.get() == Start) {
2531 // Initial value comes from a different BB, need to adjust context
2532 // instruction for analysis.
2533 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2534 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2535 return false;
2536 }
2537 }
2538
2539 // Except for Mul, the induction variable must be on the left side of the
2540 // increment expression, otherwise its value can be arbitrary.
2541 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2542 return false;
2543
2544 Q.CxtI = BO->getParent()->getTerminator();
2545 switch (BO->getOpcode()) {
2546 case Instruction::Mul:
2547 // Power of two is closed under multiplication.
2548 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2549 Q.IIQ.hasNoSignedWrap(BO)) &&
2550 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2551 case Instruction::SDiv:
2552 // Start value must not be signmask for signed division, so simply being a
2553 // power of two is not sufficient, and it has to be a constant.
2554 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2555 return false;
2556 [[fallthrough]];
2557 case Instruction::UDiv:
2558 // Divisor must be a power of two.
2559 // If OrZero is false, cannot guarantee induction variable is non-zero after
2560 // division, same for Shr, unless it is exact division.
2561 return (OrZero || Q.IIQ.isExact(BO)) &&
2562 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2563 case Instruction::Shl:
2564 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2565 case Instruction::AShr:
2566 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2567 return false;
2568 [[fallthrough]];
2569 case Instruction::LShr:
2570 return OrZero || Q.IIQ.isExact(BO);
2571 default:
2572 return false;
2573 }
2574}
2575
2576/// Return true if we can infer that \p V is known to be a power of 2 from
2577/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2578static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2579 const Value *Cond,
2580 bool CondIsTrue) {
2581 CmpPredicate Pred;
2582 const APInt *RHSC;
2584 m_APInt(RHSC))))
2585 return false;
2586 if (!CondIsTrue)
2587 Pred = ICmpInst::getInversePredicate(Pred);
2588 // ctpop(V) u< 2
2589 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2590 return true;
2591 // ctpop(V) == 1
2592 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2593}
2594
2595/// Return true if the given value is known to have exactly one
2596/// bit set when defined. For vectors return true if every element is known to
2597/// be a power of two when defined. Supports values with integer or pointer
2598/// types and vectors of integers.
2599bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2600 const SimplifyQuery &Q, unsigned Depth) {
2601 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2602
2603 if (isa<Constant>(V))
2604 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2605
2606 // i1 is by definition a power of 2 or zero.
2607 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2608 return true;
2609
2610 // Try to infer from assumptions.
2611 if (Q.AC && Q.CxtI) {
2612 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2613 if (!AssumeVH)
2614 continue;
2615 CallInst *I = cast<CallInst>(AssumeVH);
2616 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2617 /*CondIsTrue=*/true) &&
2619 return true;
2620 }
2621 }
2622
2623 // Handle dominating conditions.
2624 if (Q.DC && Q.CxtI && Q.DT) {
2625 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2626 Value *Cond = BI->getCondition();
2627
2628 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2630 /*CondIsTrue=*/true) &&
2631 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2632 return true;
2633
2634 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2636 /*CondIsTrue=*/false) &&
2637 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2638 return true;
2639 }
2640 }
2641
2642 auto *I = dyn_cast<Instruction>(V);
2643 if (!I)
2644 return false;
2645
2646 if (Q.CxtI && match(V, m_VScale())) {
2647 const Function *F = Q.CxtI->getFunction();
2648 // The vscale_range indicates vscale is a power-of-two.
2649 return F->hasFnAttribute(Attribute::VScaleRange);
2650 }
2651
2652 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2653 // it is shifted off the end then the result is undefined.
2654 if (match(I, m_Shl(m_One(), m_Value())))
2655 return true;
2656
2657 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2658 // the bottom. If it is shifted off the bottom then the result is undefined.
2659 if (match(I, m_LShr(m_SignMask(), m_Value())))
2660 return true;
2661
2662 // The remaining tests are all recursive, so bail out if we hit the limit.
2664 return false;
2665
2666 switch (I->getOpcode()) {
2667 case Instruction::ZExt:
2668 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2669 case Instruction::Trunc:
2670 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2671 case Instruction::Shl:
2672 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2673 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2674 return false;
2675 case Instruction::LShr:
2676 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2677 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2678 return false;
2679 case Instruction::UDiv:
2681 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2682 return false;
2683 case Instruction::Mul:
2684 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2685 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2686 (OrZero || isKnownNonZero(I, Q, Depth));
2687 case Instruction::And:
2688 // A power of two and'd with anything is a power of two or zero.
2689 if (OrZero &&
2690 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2691 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2692 return true;
2693 // X & (-X) is always a power of two or zero.
2694 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2695 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2696 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2697 return false;
2698 case Instruction::Add: {
2699 // Adding a power-of-two or zero to the same power-of-two or zero yields
2700 // either the original power-of-two, a larger power-of-two or zero.
2702 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2703 Q.IIQ.hasNoSignedWrap(VOBO)) {
2704 if (match(I->getOperand(0),
2705 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2706 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2707 return true;
2708 if (match(I->getOperand(1),
2709 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2710 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2711 return true;
2712
2713 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2714 KnownBits LHSBits(BitWidth);
2715 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2716
2717 KnownBits RHSBits(BitWidth);
2718 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2719 // If i8 V is a power of two or zero:
2720 // ZeroBits: 1 1 1 0 1 1 1 1
2721 // ~ZeroBits: 0 0 0 1 0 0 0 0
2722 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2723 // If OrZero isn't set, we cannot give back a zero result.
2724 // Make sure either the LHS or RHS has a bit set.
2725 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2726 return true;
2727 }
2728
2729 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2730 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2731 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2732 return true;
2733 return false;
2734 }
2735 case Instruction::Select:
2736 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2737 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2738 case Instruction::PHI: {
2739 // A PHI node is power of two if all incoming values are power of two, or if
2740 // it is an induction variable where in each step its value is a power of
2741 // two.
2742 auto *PN = cast<PHINode>(I);
2744
2745 // Check if it is an induction variable and always power of two.
2746 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2747 return true;
2748
2749 // Recursively check all incoming values. Limit recursion to 2 levels, so
2750 // that search complexity is limited to number of operands^2.
2751 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2752 return llvm::all_of(PN->operands(), [&](const Use &U) {
2753 // Value is power of 2 if it is coming from PHI node itself by induction.
2754 if (U.get() == PN)
2755 return true;
2756
2757 // Change the context instruction to the incoming block where it is
2758 // evaluated.
2759 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2760 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2761 });
2762 }
2763 case Instruction::Invoke:
2764 case Instruction::Call: {
2765 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2766 switch (II->getIntrinsicID()) {
2767 case Intrinsic::umax:
2768 case Intrinsic::smax:
2769 case Intrinsic::umin:
2770 case Intrinsic::smin:
2771 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2772 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2773 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2774 // thus dont change pow2/non-pow2 status.
2775 case Intrinsic::bitreverse:
2776 case Intrinsic::bswap:
2777 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2778 case Intrinsic::fshr:
2779 case Intrinsic::fshl:
2780 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2781 if (II->getArgOperand(0) == II->getArgOperand(1))
2782 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2783 break;
2784 default:
2785 break;
2786 }
2787 }
2788 return false;
2789 }
2790 default:
2791 return false;
2792 }
2793}
2794
2795/// Test whether a GEP's result is known to be non-null.
2796///
2797/// Uses properties inherent in a GEP to try to determine whether it is known
2798/// to be non-null.
2799///
2800/// Currently this routine does not support vector GEPs.
2801static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2802 unsigned Depth) {
2803 const Function *F = nullptr;
2804 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2805 F = I->getFunction();
2806
2807 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2808 // may be null iff the base pointer is null and the offset is zero.
2809 if (!GEP->hasNoUnsignedWrap() &&
2810 !(GEP->isInBounds() &&
2811 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2812 return false;
2813
2814 // FIXME: Support vector-GEPs.
2815 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2816
2817 // If the base pointer is non-null, we cannot walk to a null address with an
2818 // inbounds GEP in address space zero.
2819 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2820 return true;
2821
2822 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2823 // If so, then the GEP cannot produce a null pointer, as doing so would
2824 // inherently violate the inbounds contract within address space zero.
2826 GTI != GTE; ++GTI) {
2827 // Struct types are easy -- they must always be indexed by a constant.
2828 if (StructType *STy = GTI.getStructTypeOrNull()) {
2829 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2830 unsigned ElementIdx = OpC->getZExtValue();
2831 const StructLayout *SL = Q.DL.getStructLayout(STy);
2832 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2833 if (ElementOffset > 0)
2834 return true;
2835 continue;
2836 }
2837
2838 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2839 if (GTI.getSequentialElementStride(Q.DL).isZero())
2840 continue;
2841
2842 // Fast path the constant operand case both for efficiency and so we don't
2843 // increment Depth when just zipping down an all-constant GEP.
2844 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2845 if (!OpC->isZero())
2846 return true;
2847 continue;
2848 }
2849
2850 // We post-increment Depth here because while isKnownNonZero increments it
2851 // as well, when we pop back up that increment won't persist. We don't want
2852 // to recurse 10k times just because we have 10k GEP operands. We don't
2853 // bail completely out because we want to handle constant GEPs regardless
2854 // of depth.
2856 continue;
2857
2858 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2859 return true;
2860 }
2861
2862 return false;
2863}
2864
2866 const Instruction *CtxI,
2867 const DominatorTree *DT) {
2868 assert(!isa<Constant>(V) && "Called for constant?");
2869
2870 if (!CtxI || !DT)
2871 return false;
2872
2873 unsigned NumUsesExplored = 0;
2874 for (auto &U : V->uses()) {
2875 // Avoid massive lists
2876 if (NumUsesExplored >= DomConditionsMaxUses)
2877 break;
2878 NumUsesExplored++;
2879
2880 const Instruction *UI = cast<Instruction>(U.getUser());
2881 // If the value is used as an argument to a call or invoke, then argument
2882 // attributes may provide an answer about null-ness.
2883 if (V->getType()->isPointerTy()) {
2884 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2885 if (CB->isArgOperand(&U) &&
2886 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2887 /*AllowUndefOrPoison=*/false) &&
2888 DT->dominates(CB, CtxI))
2889 return true;
2890 }
2891 }
2892
2893 // If the value is used as a load/store, then the pointer must be non null.
2894 if (V == getLoadStorePointerOperand(UI)) {
2897 DT->dominates(UI, CtxI))
2898 return true;
2899 }
2900
2901 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2902 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2903 isValidAssumeForContext(UI, CtxI, DT))
2904 return true;
2905
2906 // Consider only compare instructions uniquely controlling a branch
2907 Value *RHS;
2908 CmpPredicate Pred;
2909 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2910 continue;
2911
2912 bool NonNullIfTrue;
2913 if (cmpExcludesZero(Pred, RHS))
2914 NonNullIfTrue = true;
2916 NonNullIfTrue = false;
2917 else
2918 continue;
2919
2922 for (const auto *CmpU : UI->users()) {
2923 assert(WorkList.empty() && "Should be!");
2924 if (Visited.insert(CmpU).second)
2925 WorkList.push_back(CmpU);
2926
2927 while (!WorkList.empty()) {
2928 auto *Curr = WorkList.pop_back_val();
2929
2930 // If a user is an AND, add all its users to the work list. We only
2931 // propagate "pred != null" condition through AND because it is only
2932 // correct to assume that all conditions of AND are met in true branch.
2933 // TODO: Support similar logic of OR and EQ predicate?
2934 if (NonNullIfTrue)
2935 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2936 for (const auto *CurrU : Curr->users())
2937 if (Visited.insert(CurrU).second)
2938 WorkList.push_back(CurrU);
2939 continue;
2940 }
2941
2942 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2943 assert(BI->isConditional() && "uses a comparison!");
2944
2945 BasicBlock *NonNullSuccessor =
2946 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2947 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2948 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2949 return true;
2950 } else if (NonNullIfTrue && isGuard(Curr) &&
2951 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2952 return true;
2953 }
2954 }
2955 }
2956 }
2957
2958 return false;
2959}
2960
2961/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2962/// ensure that the value it's attached to is never Value? 'RangeType' is
2963/// is the type of the value described by the range.
2964static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2965 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2966 assert(NumRanges >= 1);
2967 for (unsigned i = 0; i < NumRanges; ++i) {
2969 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2971 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2972 ConstantRange Range(Lower->getValue(), Upper->getValue());
2973 if (Range.contains(Value))
2974 return false;
2975 }
2976 return true;
2977}
2978
2979/// Try to detect a recurrence that monotonically increases/decreases from a
2980/// non-zero starting value. These are common as induction variables.
2981static bool isNonZeroRecurrence(const PHINode *PN) {
2982 BinaryOperator *BO = nullptr;
2983 Value *Start = nullptr, *Step = nullptr;
2984 const APInt *StartC, *StepC;
2985 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2986 !match(Start, m_APInt(StartC)) || StartC->isZero())
2987 return false;
2988
2989 switch (BO->getOpcode()) {
2990 case Instruction::Add:
2991 // Starting from non-zero and stepping away from zero can never wrap back
2992 // to zero.
2993 return BO->hasNoUnsignedWrap() ||
2994 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
2995 StartC->isNegative() == StepC->isNegative());
2996 case Instruction::Mul:
2997 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2998 match(Step, m_APInt(StepC)) && !StepC->isZero();
2999 case Instruction::Shl:
3000 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
3001 case Instruction::AShr:
3002 case Instruction::LShr:
3003 return BO->isExact();
3004 default:
3005 return false;
3006 }
3007}
3008
3009static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
3011 m_Specific(Op1), m_Zero()))) ||
3013 m_Specific(Op0), m_Zero())));
3014}
3015
3016static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
3017 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3018 bool NUW, unsigned Depth) {
3019 // (X + (X != 0)) is non zero
3020 if (matchOpWithOpEqZero(X, Y))
3021 return true;
3022
3023 if (NUW)
3024 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3025 isKnownNonZero(X, DemandedElts, Q, Depth);
3026
3027 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3028 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3029
3030 // If X and Y are both non-negative (as signed values) then their sum is not
3031 // zero unless both X and Y are zero.
3032 if (XKnown.isNonNegative() && YKnown.isNonNegative())
3033 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
3034 isKnownNonZero(X, DemandedElts, Q, Depth))
3035 return true;
3036
3037 // If X and Y are both negative (as signed values) then their sum is not
3038 // zero unless both X and Y equal INT_MIN.
3039 if (XKnown.isNegative() && YKnown.isNegative()) {
3041 // The sign bit of X is set. If some other bit is set then X is not equal
3042 // to INT_MIN.
3043 if (XKnown.One.intersects(Mask))
3044 return true;
3045 // The sign bit of Y is set. If some other bit is set then Y is not equal
3046 // to INT_MIN.
3047 if (YKnown.One.intersects(Mask))
3048 return true;
3049 }
3050
3051 // The sum of a non-negative number and a power of two is not zero.
3052 if (XKnown.isNonNegative() &&
3053 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
3054 return true;
3055 if (YKnown.isNonNegative() &&
3056 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
3057 return true;
3058
3059 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
3060}
3061
3062static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
3063 unsigned BitWidth, Value *X, Value *Y,
3064 unsigned Depth) {
3065 // (X - (X != 0)) is non zero
3066 // ((X != 0) - X) is non zero
3067 if (matchOpWithOpEqZero(X, Y))
3068 return true;
3069
3070 // TODO: Move this case into isKnownNonEqual().
3071 if (auto *C = dyn_cast<Constant>(X))
3072 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
3073 return true;
3074
3075 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
3076}
3077
3078static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
3079 unsigned BitWidth, Value *X, Value *Y, bool NSW,
3080 bool NUW, unsigned Depth) {
3081 // If X and Y are non-zero then so is X * Y as long as the multiplication
3082 // does not overflow.
3083 if (NSW || NUW)
3084 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
3085 isKnownNonZero(Y, DemandedElts, Q, Depth);
3086
3087 // If either X or Y is odd, then if the other is non-zero the result can't
3088 // be zero.
3089 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
3090 if (XKnown.One[0])
3091 return isKnownNonZero(Y, DemandedElts, Q, Depth);
3092
3093 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
3094 if (YKnown.One[0])
3095 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
3096
3097 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
3098 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
3099 // the lowest known One of X and Y. If they are non-zero, the result
3100 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
3101 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
3102 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
3103 BitWidth;
3104}
3105
3106static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
3107 const SimplifyQuery &Q, const KnownBits &KnownVal,
3108 unsigned Depth) {
3109 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3110 switch (I->getOpcode()) {
3111 case Instruction::Shl:
3112 return Lhs.shl(Rhs);
3113 case Instruction::LShr:
3114 return Lhs.lshr(Rhs);
3115 case Instruction::AShr:
3116 return Lhs.ashr(Rhs);
3117 default:
3118 llvm_unreachable("Unknown Shift Opcode");
3119 }
3120 };
3121
3122 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
3123 switch (I->getOpcode()) {
3124 case Instruction::Shl:
3125 return Lhs.lshr(Rhs);
3126 case Instruction::LShr:
3127 case Instruction::AShr:
3128 return Lhs.shl(Rhs);
3129 default:
3130 llvm_unreachable("Unknown Shift Opcode");
3131 }
3132 };
3133
3134 if (KnownVal.isUnknown())
3135 return false;
3136
3137 KnownBits KnownCnt =
3138 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3139 APInt MaxShift = KnownCnt.getMaxValue();
3140 unsigned NumBits = KnownVal.getBitWidth();
3141 if (MaxShift.uge(NumBits))
3142 return false;
3143
3144 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3145 return true;
3146
3147 // If all of the bits shifted out are known to be zero, and Val is known
3148 // non-zero then at least one non-zero bit must remain.
3149 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3150 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3151 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3152 return true;
3153
3154 return false;
3155}
3156
3158 const APInt &DemandedElts,
3159 const SimplifyQuery &Q, unsigned Depth) {
3160 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3161 switch (I->getOpcode()) {
3162 case Instruction::Alloca:
3163 // Alloca never returns null, malloc might.
3164 return I->getType()->getPointerAddressSpace() == 0;
3165 case Instruction::GetElementPtr:
3166 if (I->getType()->isPointerTy())
3168 break;
3169 case Instruction::BitCast: {
3170 // We need to be a bit careful here. We can only peek through the bitcast
3171 // if the scalar size of elements in the operand are smaller than and a
3172 // multiple of the size they are casting too. Take three cases:
3173 //
3174 // 1) Unsafe:
3175 // bitcast <2 x i16> %NonZero to <4 x i8>
3176 //
3177 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3178 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3179 // guranteed (imagine just sign bit set in the 2 i16 elements).
3180 //
3181 // 2) Unsafe:
3182 // bitcast <4 x i3> %NonZero to <3 x i4>
3183 //
3184 // Even though the scalar size of the src (`i3`) is smaller than the
3185 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3186 // its possible for the `3 x i4` elements to be zero because there are
3187 // some elements in the destination that don't contain any full src
3188 // element.
3189 //
3190 // 3) Safe:
3191 // bitcast <4 x i8> %NonZero to <2 x i16>
3192 //
3193 // This is always safe as non-zero in the 4 i8 elements implies
3194 // non-zero in the combination of any two adjacent ones. Since i8 is a
3195 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3196 // This all implies the 2 i16 elements are non-zero.
3197 Type *FromTy = I->getOperand(0)->getType();
3198 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3199 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3200 return isKnownNonZero(I->getOperand(0), Q, Depth);
3201 } break;
3202 case Instruction::IntToPtr:
3203 // Note that we have to take special care to avoid looking through
3204 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3205 // as casts that can alter the value, e.g., AddrSpaceCasts.
3206 if (!isa<ScalableVectorType>(I->getType()) &&
3207 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3208 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3209 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3210 break;
3211 case Instruction::PtrToInt:
3212 // Similar to int2ptr above, we can look through ptr2int here if the cast
3213 // is a no-op or an extend and not a truncate.
3214 if (!isa<ScalableVectorType>(I->getType()) &&
3215 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3216 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3217 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3218 break;
3219 case Instruction::Trunc:
3220 // nuw/nsw trunc preserves zero/non-zero status of input.
3221 if (auto *TI = dyn_cast<TruncInst>(I))
3222 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3223 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3224 break;
3225
3226 // Iff x - y != 0, then x ^ y != 0
3227 // Therefore we can do the same exact checks
3228 case Instruction::Xor:
3229 case Instruction::Sub:
3230 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3231 I->getOperand(1), Depth);
3232 case Instruction::Or:
3233 // (X | (X != 0)) is non zero
3234 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3235 return true;
3236 // X | Y != 0 if X != Y.
3237 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3238 Depth))
3239 return true;
3240 // X | Y != 0 if X != 0 or Y != 0.
3241 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3242 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3243 case Instruction::SExt:
3244 case Instruction::ZExt:
3245 // ext X != 0 if X != 0.
3246 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3247
3248 case Instruction::Shl: {
3249 // shl nsw/nuw can't remove any non-zero bits.
3251 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3252 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3253
3254 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3255 // if the lowest bit is shifted off the end.
3256 KnownBits Known(BitWidth);
3257 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3258 if (Known.One[0])
3259 return true;
3260
3261 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3262 }
3263 case Instruction::LShr:
3264 case Instruction::AShr: {
3265 // shr exact can only shift out zero bits.
3267 if (BO->isExact())
3268 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3269
3270 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3271 // defined if the sign bit is shifted off the end.
3272 KnownBits Known =
3273 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3274 if (Known.isNegative())
3275 return true;
3276
3277 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3278 }
3279 case Instruction::UDiv:
3280 case Instruction::SDiv: {
3281 // X / Y
3282 // div exact can only produce a zero if the dividend is zero.
3283 if (cast<PossiblyExactOperator>(I)->isExact())
3284 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3285
3286 KnownBits XKnown =
3287 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3288 // If X is fully unknown we won't be able to figure anything out so don't
3289 // both computing knownbits for Y.
3290 if (XKnown.isUnknown())
3291 return false;
3292
3293 KnownBits YKnown =
3294 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3295 if (I->getOpcode() == Instruction::SDiv) {
3296 // For signed division need to compare abs value of the operands.
3297 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3298 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3299 }
3300 // If X u>= Y then div is non zero (0/0 is UB).
3301 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3302 // If X is total unknown or X u< Y we won't be able to prove non-zero
3303 // with compute known bits so just return early.
3304 return XUgeY && *XUgeY;
3305 }
3306 case Instruction::Add: {
3307 // X + Y.
3308
3309 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3310 // non-zero.
3312 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3313 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3314 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3315 }
3316 case Instruction::Mul: {
3318 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3319 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3320 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3321 }
3322 case Instruction::Select: {
3323 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3324
3325 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3326 // then see if the select condition implies the arm is non-zero. For example
3327 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3328 // dominated by `X != 0`.
3329 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3330 Value *Op;
3331 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3332 // Op is trivially non-zero.
3333 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3334 return true;
3335
3336 // The condition of the select dominates the true/false arm. Check if the
3337 // condition implies that a given arm is non-zero.
3338 Value *X;
3339 CmpPredicate Pred;
3340 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3341 return false;
3342
3343 if (!IsTrueArm)
3344 Pred = ICmpInst::getInversePredicate(Pred);
3345
3346 return cmpExcludesZero(Pred, X);
3347 };
3348
3349 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3350 SelectArmIsNonZero(/* IsTrueArm */ false))
3351 return true;
3352 break;
3353 }
3354 case Instruction::PHI: {
3355 auto *PN = cast<PHINode>(I);
3357 return true;
3358
3359 // Check if all incoming values are non-zero using recursion.
3361 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3362 return llvm::all_of(PN->operands(), [&](const Use &U) {
3363 if (U.get() == PN)
3364 return true;
3365 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3366 // Check if the branch on the phi excludes zero.
3367 CmpPredicate Pred;
3368 Value *X;
3369 BasicBlock *TrueSucc, *FalseSucc;
3370 if (match(RecQ.CxtI,
3371 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3372 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3373 // Check for cases of duplicate successors.
3374 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3375 // If we're using the false successor, invert the predicate.
3376 if (FalseSucc == PN->getParent())
3377 Pred = CmpInst::getInversePredicate(Pred);
3378 if (cmpExcludesZero(Pred, X))
3379 return true;
3380 }
3381 }
3382 // Finally recurse on the edge and check it directly.
3383 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3384 });
3385 }
3386 case Instruction::InsertElement: {
3387 if (isa<ScalableVectorType>(I->getType()))
3388 break;
3389
3390 const Value *Vec = I->getOperand(0);
3391 const Value *Elt = I->getOperand(1);
3392 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3393
3394 unsigned NumElts = DemandedElts.getBitWidth();
3395 APInt DemandedVecElts = DemandedElts;
3396 bool SkipElt = false;
3397 // If we know the index we are inserting too, clear it from Vec check.
3398 if (CIdx && CIdx->getValue().ult(NumElts)) {
3399 DemandedVecElts.clearBit(CIdx->getZExtValue());
3400 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3401 }
3402
3403 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3404 // are non-zero.
3405 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3406 (DemandedVecElts.isZero() ||
3407 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3408 }
3409 case Instruction::ExtractElement:
3410 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3411 const Value *Vec = EEI->getVectorOperand();
3412 const Value *Idx = EEI->getIndexOperand();
3413 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3414 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3415 unsigned NumElts = VecTy->getNumElements();
3416 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3417 if (CIdx && CIdx->getValue().ult(NumElts))
3418 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3419 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3420 }
3421 }
3422 break;
3423 case Instruction::ShuffleVector: {
3424 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3425 if (!Shuf)
3426 break;
3427 APInt DemandedLHS, DemandedRHS;
3428 // For undef elements, we don't know anything about the common state of
3429 // the shuffle result.
3430 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3431 break;
3432 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3433 return (DemandedRHS.isZero() ||
3434 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3435 (DemandedLHS.isZero() ||
3436 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3437 }
3438 case Instruction::Freeze:
3439 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3440 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3441 Depth);
3442 case Instruction::Load: {
3443 auto *LI = cast<LoadInst>(I);
3444 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3445 // is never null.
3446 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3447 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3448 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3449 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3450 return true;
3451 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3453 }
3454
3455 // No need to fall through to computeKnownBits as range metadata is already
3456 // handled in isKnownNonZero.
3457 return false;
3458 }
3459 case Instruction::ExtractValue: {
3460 const WithOverflowInst *WO;
3462 switch (WO->getBinaryOp()) {
3463 default:
3464 break;
3465 case Instruction::Add:
3466 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3467 WO->getArgOperand(1),
3468 /*NSW=*/false,
3469 /*NUW=*/false, Depth);
3470 case Instruction::Sub:
3471 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3472 WO->getArgOperand(1), Depth);
3473 case Instruction::Mul:
3474 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3475 WO->getArgOperand(1),
3476 /*NSW=*/false, /*NUW=*/false, Depth);
3477 break;
3478 }
3479 }
3480 break;
3481 }
3482 case Instruction::Call:
3483 case Instruction::Invoke: {
3484 const auto *Call = cast<CallBase>(I);
3485 if (I->getType()->isPointerTy()) {
3486 if (Call->isReturnNonNull())
3487 return true;
3488 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3489 return isKnownNonZero(RP, Q, Depth);
3490 } else {
3491 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3493 if (std::optional<ConstantRange> Range = Call->getRange()) {
3494 const APInt ZeroValue(Range->getBitWidth(), 0);
3495 if (!Range->contains(ZeroValue))
3496 return true;
3497 }
3498 if (const Value *RV = Call->getReturnedArgOperand())
3499 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3500 return true;
3501 }
3502
3503 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3504 switch (II->getIntrinsicID()) {
3505 case Intrinsic::sshl_sat:
3506 case Intrinsic::ushl_sat:
3507 case Intrinsic::abs:
3508 case Intrinsic::bitreverse:
3509 case Intrinsic::bswap:
3510 case Intrinsic::ctpop:
3511 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3512 // NB: We don't do usub_sat here as in any case we can prove its
3513 // non-zero, we will fold it to `sub nuw` in InstCombine.
3514 case Intrinsic::ssub_sat:
3515 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3516 II->getArgOperand(1), Depth);
3517 case Intrinsic::sadd_sat:
3518 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3519 II->getArgOperand(1),
3520 /*NSW=*/true, /* NUW=*/false, Depth);
3521 // Vec reverse preserves zero/non-zero status from input vec.
3522 case Intrinsic::vector_reverse:
3523 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3524 Q, Depth);
3525 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3526 case Intrinsic::vector_reduce_or:
3527 case Intrinsic::vector_reduce_umax:
3528 case Intrinsic::vector_reduce_umin:
3529 case Intrinsic::vector_reduce_smax:
3530 case Intrinsic::vector_reduce_smin:
3531 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3532 case Intrinsic::umax:
3533 case Intrinsic::uadd_sat:
3534 // umax(X, (X != 0)) is non zero
3535 // X +usat (X != 0) is non zero
3536 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3537 return true;
3538
3539 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3540 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3541 case Intrinsic::smax: {
3542 // If either arg is strictly positive the result is non-zero. Otherwise
3543 // the result is non-zero if both ops are non-zero.
3544 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3545 const KnownBits &OpKnown) {
3546 if (!OpNonZero.has_value())
3547 OpNonZero = OpKnown.isNonZero() ||
3548 isKnownNonZero(Op, DemandedElts, Q, Depth);
3549 return *OpNonZero;
3550 };
3551 // Avoid re-computing isKnownNonZero.
3552 std::optional<bool> Op0NonZero, Op1NonZero;
3553 KnownBits Op1Known =
3554 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3555 if (Op1Known.isNonNegative() &&
3556 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3557 return true;
3558 KnownBits Op0Known =
3559 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3560 if (Op0Known.isNonNegative() &&
3561 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3562 return true;
3563 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3564 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3565 }
3566 case Intrinsic::smin: {
3567 // If either arg is negative the result is non-zero. Otherwise
3568 // the result is non-zero if both ops are non-zero.
3569 KnownBits Op1Known =
3570 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3571 if (Op1Known.isNegative())
3572 return true;
3573 KnownBits Op0Known =
3574 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3575 if (Op0Known.isNegative())
3576 return true;
3577
3578 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3579 return true;
3580 }
3581 [[fallthrough]];
3582 case Intrinsic::umin:
3583 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3584 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3585 case Intrinsic::cttz:
3586 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3587 .Zero[0];
3588 case Intrinsic::ctlz:
3589 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3590 .isNonNegative();
3591 case Intrinsic::fshr:
3592 case Intrinsic::fshl:
3593 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3594 if (II->getArgOperand(0) == II->getArgOperand(1))
3595 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3596 break;
3597 case Intrinsic::vscale:
3598 return true;
3599 case Intrinsic::experimental_get_vector_length:
3600 return isKnownNonZero(I->getOperand(0), Q, Depth);
3601 default:
3602 break;
3603 }
3604 break;
3605 }
3606
3607 return false;
3608 }
3609 }
3610
3611 KnownBits Known(BitWidth);
3612 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3613 return Known.One != 0;
3614}
3615
3616/// Return true if the given value is known to be non-zero when defined. For
3617/// vectors, return true if every demanded element is known to be non-zero when
3618/// defined. For pointers, if the context instruction and dominator tree are
3619/// specified, perform context-sensitive analysis and return true if the
3620/// pointer couldn't possibly be null at the specified instruction.
3621/// Supports values with integer or pointer type and vectors of integers.
3622bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3623 const SimplifyQuery &Q, unsigned Depth) {
3624 Type *Ty = V->getType();
3625
3626#ifndef NDEBUG
3627 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3628
3629 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3630 assert(
3631 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3632 "DemandedElt width should equal the fixed vector number of elements");
3633 } else {
3634 assert(DemandedElts == APInt(1, 1) &&
3635 "DemandedElt width should be 1 for scalars");
3636 }
3637#endif
3638
3639 if (auto *C = dyn_cast<Constant>(V)) {
3640 if (C->isNullValue())
3641 return false;
3642 if (isa<ConstantInt>(C))
3643 // Must be non-zero due to null test above.
3644 return true;
3645
3646 // For constant vectors, check that all elements are poison or known
3647 // non-zero to determine that the whole vector is known non-zero.
3648 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3649 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3650 if (!DemandedElts[i])
3651 continue;
3652 Constant *Elt = C->getAggregateElement(i);
3653 if (!Elt || Elt->isNullValue())
3654 return false;
3655 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3656 return false;
3657 }
3658 return true;
3659 }
3660
3661 // Constant ptrauth can be null, iff the base pointer can be.
3662 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3663 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3664
3665 // A global variable in address space 0 is non null unless extern weak
3666 // or an absolute symbol reference. Other address spaces may have null as a
3667 // valid address for a global, so we can't assume anything.
3668 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3669 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3670 GV->getType()->getAddressSpace() == 0)
3671 return true;
3672 }
3673
3674 // For constant expressions, fall through to the Operator code below.
3675 if (!isa<ConstantExpr>(V))
3676 return false;
3677 }
3678
3679 if (const auto *A = dyn_cast<Argument>(V))
3680 if (std::optional<ConstantRange> Range = A->getRange()) {
3681 const APInt ZeroValue(Range->getBitWidth(), 0);
3682 if (!Range->contains(ZeroValue))
3683 return true;
3684 }
3685
3686 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3687 return true;
3688
3689 // Some of the tests below are recursive, so bail out if we hit the limit.
3691 return false;
3692
3693 // Check for pointer simplifications.
3694
3695 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3696 // A byval, inalloca may not be null in a non-default addres space. A
3697 // nonnull argument is assumed never 0.
3698 if (const Argument *A = dyn_cast<Argument>(V)) {
3699 if (((A->hasPassPointeeByValueCopyAttr() &&
3700 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3701 A->hasNonNullAttr()))
3702 return true;
3703 }
3704 }
3705
3706 if (const auto *I = dyn_cast<Operator>(V))
3707 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3708 return true;
3709
3710 if (!isa<Constant>(V) &&
3712 return true;
3713
3714 if (const Value *Stripped = stripNullTest(V))
3715 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3716
3717 return false;
3718}
3719
3721 unsigned Depth) {
3722 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3723 APInt DemandedElts =
3724 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3725 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3726}
3727
3728/// If the pair of operators are the same invertible function, return the
3729/// the operands of the function corresponding to each input. Otherwise,
3730/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3731/// every input value to exactly one output value. This is equivalent to
3732/// saying that Op1 and Op2 are equal exactly when the specified pair of
3733/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3734static std::optional<std::pair<Value*, Value*>>
3736 const Operator *Op2) {
3737 if (Op1->getOpcode() != Op2->getOpcode())
3738 return std::nullopt;
3739
3740 auto getOperands = [&](unsigned OpNum) -> auto {
3741 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3742 };
3743
3744 switch (Op1->getOpcode()) {
3745 default:
3746 break;
3747 case Instruction::Or:
3748 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3749 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3750 break;
3751 [[fallthrough]];
3752 case Instruction::Xor:
3753 case Instruction::Add: {
3754 Value *Other;
3755 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3756 return std::make_pair(Op1->getOperand(1), Other);
3757 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3758 return std::make_pair(Op1->getOperand(0), Other);
3759 break;
3760 }
3761 case Instruction::Sub:
3762 if (Op1->getOperand(0) == Op2->getOperand(0))
3763 return getOperands(1);
3764 if (Op1->getOperand(1) == Op2->getOperand(1))
3765 return getOperands(0);
3766 break;
3767 case Instruction::Mul: {
3768 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3769 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3770 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3771 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3772 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3773 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3774 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3775 break;
3776
3777 // Assume operand order has been canonicalized
3778 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3779 isa<ConstantInt>(Op1->getOperand(1)) &&
3780 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3781 return getOperands(0);
3782 break;
3783 }
3784 case Instruction::Shl: {
3785 // Same as multiplies, with the difference that we don't need to check
3786 // for a non-zero multiply. Shifts always multiply by non-zero.
3787 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3788 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3789 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3790 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3791 break;
3792
3793 if (Op1->getOperand(1) == Op2->getOperand(1))
3794 return getOperands(0);
3795 break;
3796 }
3797 case Instruction::AShr:
3798 case Instruction::LShr: {
3799 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3800 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3801 if (!PEO1->isExact() || !PEO2->isExact())
3802 break;
3803
3804 if (Op1->getOperand(1) == Op2->getOperand(1))
3805 return getOperands(0);
3806 break;
3807 }
3808 case Instruction::SExt:
3809 case Instruction::ZExt:
3810 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3811 return getOperands(0);
3812 break;
3813 case Instruction::PHI: {
3814 const PHINode *PN1 = cast<PHINode>(Op1);
3815 const PHINode *PN2 = cast<PHINode>(Op2);
3816
3817 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3818 // are a single invertible function of the start values? Note that repeated
3819 // application of an invertible function is also invertible
3820 BinaryOperator *BO1 = nullptr;
3821 Value *Start1 = nullptr, *Step1 = nullptr;
3822 BinaryOperator *BO2 = nullptr;
3823 Value *Start2 = nullptr, *Step2 = nullptr;
3824 if (PN1->getParent() != PN2->getParent() ||
3825 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3826 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3827 break;
3828
3829 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3830 cast<Operator>(BO2));
3831 if (!Values)
3832 break;
3833
3834 // We have to be careful of mutually defined recurrences here. Ex:
3835 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3836 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3837 // The invertibility of these is complicated, and not worth reasoning
3838 // about (yet?).
3839 if (Values->first != PN1 || Values->second != PN2)
3840 break;
3841
3842 return std::make_pair(Start1, Start2);
3843 }
3844 }
3845 return std::nullopt;
3846}
3847
3848/// Return true if V1 == (binop V2, X), where X is known non-zero.
3849/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3850/// implies V2 != V1.
3851static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3852 const APInt &DemandedElts,
3853 const SimplifyQuery &Q, unsigned Depth) {
3855 if (!BO)
3856 return false;
3857 switch (BO->getOpcode()) {
3858 default:
3859 break;
3860 case Instruction::Or:
3861 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3862 break;
3863 [[fallthrough]];
3864 case Instruction::Xor:
3865 case Instruction::Add:
3866 Value *Op = nullptr;
3867 if (V2 == BO->getOperand(0))
3868 Op = BO->getOperand(1);
3869 else if (V2 == BO->getOperand(1))
3870 Op = BO->getOperand(0);
3871 else
3872 return false;
3873 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3874 }
3875 return false;
3876}
3877
3878/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3879/// the multiplication is nuw or nsw.
3880static bool isNonEqualMul(const Value *V1, const Value *V2,
3881 const APInt &DemandedElts, const SimplifyQuery &Q,
3882 unsigned Depth) {
3883 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3884 const APInt *C;
3885 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3886 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3887 !C->isZero() && !C->isOne() &&
3888 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3889 }
3890 return false;
3891}
3892
3893/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3894/// the shift is nuw or nsw.
3895static bool isNonEqualShl(const Value *V1, const Value *V2,
3896 const APInt &DemandedElts, const SimplifyQuery &Q,
3897 unsigned Depth) {
3898 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3899 const APInt *C;
3900 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3901 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3902 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3903 }
3904 return false;
3905}
3906
3907static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3908 const APInt &DemandedElts, const SimplifyQuery &Q,
3909 unsigned Depth) {
3910 // Check two PHIs are in same block.
3911 if (PN1->getParent() != PN2->getParent())
3912 return false;
3913
3915 bool UsedFullRecursion = false;
3916 for (const BasicBlock *IncomBB : PN1->blocks()) {
3917 if (!VisitedBBs.insert(IncomBB).second)
3918 continue; // Don't reprocess blocks that we have dealt with already.
3919 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3920 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3921 const APInt *C1, *C2;
3922 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3923 continue;
3924
3925 // Only one pair of phi operands is allowed for full recursion.
3926 if (UsedFullRecursion)
3927 return false;
3928
3930 RecQ.CxtI = IncomBB->getTerminator();
3931 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3932 return false;
3933 UsedFullRecursion = true;
3934 }
3935 return true;
3936}
3937
3938static bool isNonEqualSelect(const Value *V1, const Value *V2,
3939 const APInt &DemandedElts, const SimplifyQuery &Q,
3940 unsigned Depth) {
3941 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3942 if (!SI1)
3943 return false;
3944
3945 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3946 const Value *Cond1 = SI1->getCondition();
3947 const Value *Cond2 = SI2->getCondition();
3948 if (Cond1 == Cond2)
3949 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3950 DemandedElts, Q, Depth + 1) &&
3951 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3952 DemandedElts, Q, Depth + 1);
3953 }
3954 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
3955 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
3956}
3957
3958// Check to see if A is both a GEP and is the incoming value for a PHI in the
3959// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3960// one of them being the recursive GEP A and the other a ptr at same base and at
3961// the same/higher offset than B we are only incrementing the pointer further in
3962// loop if offset of recursive GEP is greater than 0.
3964 const SimplifyQuery &Q) {
3965 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3966 return false;
3967
3968 auto *GEPA = dyn_cast<GEPOperator>(A);
3969 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3970 return false;
3971
3972 // Handle 2 incoming PHI values with one being a recursive GEP.
3973 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3974 if (!PN || PN->getNumIncomingValues() != 2)
3975 return false;
3976
3977 // Search for the recursive GEP as an incoming operand, and record that as
3978 // Step.
3979 Value *Start = nullptr;
3980 Value *Step = const_cast<Value *>(A);
3981 if (PN->getIncomingValue(0) == Step)
3982 Start = PN->getIncomingValue(1);
3983 else if (PN->getIncomingValue(1) == Step)
3984 Start = PN->getIncomingValue(0);
3985 else
3986 return false;
3987
3988 // Other incoming node base should match the B base.
3989 // StartOffset >= OffsetB && StepOffset > 0?
3990 // StartOffset <= OffsetB && StepOffset < 0?
3991 // Is non-equal if above are true.
3992 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3993 // optimisation to inbounds GEPs only.
3994 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3995 APInt StartOffset(IndexWidth, 0);
3996 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3997 APInt StepOffset(IndexWidth, 0);
3998 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3999
4000 // Check if Base Pointer of Step matches the PHI.
4001 if (Step != PN)
4002 return false;
4003 APInt OffsetB(IndexWidth, 0);
4004 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
4005 return Start == B &&
4006 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
4007 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
4008}
4009
4010static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
4011 const SimplifyQuery &Q, unsigned Depth) {
4012 if (!Q.CxtI)
4013 return false;
4014
4015 // Try to infer NonEqual based on information from dominating conditions.
4016 if (Q.DC && Q.DT) {
4017 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
4018 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4019 Value *Cond = BI->getCondition();
4020 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4021 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
4023 /*LHSIsTrue=*/true, Depth)
4024 .value_or(false))
4025 return true;
4026
4027 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4028 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
4030 /*LHSIsTrue=*/false, Depth)
4031 .value_or(false))
4032 return true;
4033 }
4034
4035 return false;
4036 };
4037
4038 if (IsKnownNonEqualFromDominatingCondition(V1) ||
4039 IsKnownNonEqualFromDominatingCondition(V2))
4040 return true;
4041 }
4042
4043 if (!Q.AC)
4044 return false;
4045
4046 // Try to infer NonEqual based on information from assumptions.
4047 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
4048 if (!AssumeVH)
4049 continue;
4050 CallInst *I = cast<CallInst>(AssumeVH);
4051
4052 assert(I->getFunction() == Q.CxtI->getFunction() &&
4053 "Got assumption for the wrong function!");
4054 assert(I->getIntrinsicID() == Intrinsic::assume &&
4055 "must be an assume intrinsic");
4056
4057 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
4058 /*LHSIsTrue=*/true, Depth)
4059 .value_or(false) &&
4061 return true;
4062 }
4063
4064 return false;
4065}
4066
4067/// Return true if it is known that V1 != V2.
4068static bool isKnownNonEqual(const Value *V1, const Value *V2,
4069 const APInt &DemandedElts, const SimplifyQuery &Q,
4070 unsigned Depth) {
4071 if (V1 == V2)
4072 return false;
4073 if (V1->getType() != V2->getType())
4074 // We can't look through casts yet.
4075 return false;
4076
4078 return false;
4079
4080 // See if we can recurse through (exactly one of) our operands. This
4081 // requires our operation be 1-to-1 and map every input value to exactly
4082 // one output value. Such an operation is invertible.
4083 auto *O1 = dyn_cast<Operator>(V1);
4084 auto *O2 = dyn_cast<Operator>(V2);
4085 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
4086 if (auto Values = getInvertibleOperands(O1, O2))
4087 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
4088 Depth + 1);
4089
4090 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
4091 const PHINode *PN2 = cast<PHINode>(V2);
4092 // FIXME: This is missing a generalization to handle the case where one is
4093 // a PHI and another one isn't.
4094 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
4095 return true;
4096 };
4097 }
4098
4099 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
4100 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
4101 return true;
4102
4103 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
4104 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
4105 return true;
4106
4107 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
4108 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
4109 return true;
4110
4111 if (V1->getType()->isIntOrIntVectorTy()) {
4112 // Are any known bits in V1 contradictory to known bits in V2? If V1
4113 // has a known zero where V2 has a known one, they must not be equal.
4114 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
4115 if (!Known1.isUnknown()) {
4116 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
4117 if (Known1.Zero.intersects(Known2.One) ||
4118 Known2.Zero.intersects(Known1.One))
4119 return true;
4120 }
4121 }
4122
4123 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
4124 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
4125 return true;
4126
4127 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
4129 return true;
4130
4131 Value *A, *B;
4132 // PtrToInts are NonEqual if their Ptrs are NonEqual.
4133 // Check PtrToInt type matches the pointer size.
4134 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
4136 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
4137
4138 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4139 return true;
4140
4141 return false;
4142}
4143
4144/// For vector constants, loop over the elements and find the constant with the
4145/// minimum number of sign bits. Return 0 if the value is not a vector constant
4146/// or if any element was not analyzed; otherwise, return the count for the
4147/// element with the minimum number of sign bits.
4149 const APInt &DemandedElts,
4150 unsigned TyBits) {
4151 const auto *CV = dyn_cast<Constant>(V);
4152 if (!CV || !isa<FixedVectorType>(CV->getType()))
4153 return 0;
4154
4155 unsigned MinSignBits = TyBits;
4156 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4157 for (unsigned i = 0; i != NumElts; ++i) {
4158 if (!DemandedElts[i])
4159 continue;
4160 // If we find a non-ConstantInt, bail out.
4161 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4162 if (!Elt)
4163 return 0;
4164
4165 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4166 }
4167
4168 return MinSignBits;
4169}
4170
4171static unsigned ComputeNumSignBitsImpl(const Value *V,
4172 const APInt &DemandedElts,
4173 const SimplifyQuery &Q, unsigned Depth);
4174
4175static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4176 const SimplifyQuery &Q, unsigned Depth) {
4177 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4178 assert(Result > 0 && "At least one sign bit needs to be present!");
4179 return Result;
4180}
4181
4182/// Return the number of times the sign bit of the register is replicated into
4183/// the other bits. We know that at least 1 bit is always equal to the sign bit
4184/// (itself), but other cases can give us information. For example, immediately
4185/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4186/// other, so we return 3. For vectors, return the number of sign bits for the
4187/// vector element with the minimum number of known sign bits of the demanded
4188/// elements in the vector specified by DemandedElts.
4189static unsigned ComputeNumSignBitsImpl(const Value *V,
4190 const APInt &DemandedElts,
4191 const SimplifyQuery &Q, unsigned Depth) {
4192 Type *Ty = V->getType();
4193#ifndef NDEBUG
4194 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4195
4196 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4197 assert(
4198 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4199 "DemandedElt width should equal the fixed vector number of elements");
4200 } else {
4201 assert(DemandedElts == APInt(1, 1) &&
4202 "DemandedElt width should be 1 for scalars");
4203 }
4204#endif
4205
4206 // We return the minimum number of sign bits that are guaranteed to be present
4207 // in V, so for undef we have to conservatively return 1. We don't have the
4208 // same behavior for poison though -- that's a FIXME today.
4209
4210 Type *ScalarTy = Ty->getScalarType();
4211 unsigned TyBits = ScalarTy->isPointerTy() ?
4212 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4213 Q.DL.getTypeSizeInBits(ScalarTy);
4214
4215 unsigned Tmp, Tmp2;
4216 unsigned FirstAnswer = 1;
4217
4218 // Note that ConstantInt is handled by the general computeKnownBits case
4219 // below.
4220
4222 return 1;
4223
4224 if (auto *U = dyn_cast<Operator>(V)) {
4225 switch (Operator::getOpcode(V)) {
4226 default: break;
4227 case Instruction::BitCast: {
4228 Value *Src = U->getOperand(0);
4229 Type *SrcTy = Src->getType();
4230
4231 // Skip if the source type is not an integer or integer vector type
4232 // This ensures we only process integer-like types
4233 if (!SrcTy->isIntOrIntVectorTy())
4234 break;
4235
4236 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4237
4238 // Bitcast 'large element' scalar/vector to 'small element' vector.
4239 if ((SrcBits % TyBits) != 0)
4240 break;
4241
4242 // Only proceed if the destination type is a fixed-size vector
4243 if (isa<FixedVectorType>(Ty)) {
4244 // Fast case - sign splat can be simply split across the small elements.
4245 // This works for both vector and scalar sources
4246 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4247 if (Tmp == SrcBits)
4248 return TyBits;
4249 }
4250 break;
4251 }
4252 case Instruction::SExt:
4253 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4254 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4255 Tmp;
4256
4257 case Instruction::SDiv: {
4258 const APInt *Denominator;
4259 // sdiv X, C -> adds log(C) sign bits.
4260 if (match(U->getOperand(1), m_APInt(Denominator))) {
4261
4262 // Ignore non-positive denominator.
4263 if (!Denominator->isStrictlyPositive())
4264 break;
4265
4266 // Calculate the incoming numerator bits.
4267 unsigned NumBits =
4268 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4269
4270 // Add floor(log(C)) bits to the numerator bits.
4271 return std::min(TyBits, NumBits + Denominator->logBase2());
4272 }
4273 break;
4274 }
4275
4276 case Instruction::SRem: {
4277 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4278
4279 const APInt *Denominator;
4280 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4281 // positive constant. This let us put a lower bound on the number of sign
4282 // bits.
4283 if (match(U->getOperand(1), m_APInt(Denominator))) {
4284
4285 // Ignore non-positive denominator.
4286 if (Denominator->isStrictlyPositive()) {
4287 // Calculate the leading sign bit constraints by examining the
4288 // denominator. Given that the denominator is positive, there are two
4289 // cases:
4290 //
4291 // 1. The numerator is positive. The result range is [0,C) and
4292 // [0,C) u< (1 << ceilLogBase2(C)).
4293 //
4294 // 2. The numerator is negative. Then the result range is (-C,0] and
4295 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4296 //
4297 // Thus a lower bound on the number of sign bits is `TyBits -
4298 // ceilLogBase2(C)`.
4299
4300 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4301 Tmp = std::max(Tmp, ResBits);
4302 }
4303 }
4304 return Tmp;
4305 }
4306
4307 case Instruction::AShr: {
4308 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4309 // ashr X, C -> adds C sign bits. Vectors too.
4310 const APInt *ShAmt;
4311 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4312 if (ShAmt->uge(TyBits))
4313 break; // Bad shift.
4314 unsigned ShAmtLimited = ShAmt->getZExtValue();
4315 Tmp += ShAmtLimited;
4316 if (Tmp > TyBits) Tmp = TyBits;
4317 }
4318 return Tmp;
4319 }
4320 case Instruction::Shl: {
4321 const APInt *ShAmt;
4322 Value *X = nullptr;
4323 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4324 // shl destroys sign bits.
4325 if (ShAmt->uge(TyBits))
4326 break; // Bad shift.
4327 // We can look through a zext (more or less treating it as a sext) if
4328 // all extended bits are shifted out.
4329 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4330 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4331 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4332 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4333 } else
4334 Tmp =
4335 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4336 if (ShAmt->uge(Tmp))
4337 break; // Shifted all sign bits out.
4338 Tmp2 = ShAmt->getZExtValue();
4339 return Tmp - Tmp2;
4340 }
4341 break;
4342 }
4343 case Instruction::And:
4344 case Instruction::Or:
4345 case Instruction::Xor: // NOT is handled here.
4346 // Logical binary ops preserve the number of sign bits at the worst.
4347 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4348 if (Tmp != 1) {
4349 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4350 FirstAnswer = std::min(Tmp, Tmp2);
4351 // We computed what we know about the sign bits as our first
4352 // answer. Now proceed to the generic code that uses
4353 // computeKnownBits, and pick whichever answer is better.
4354 }
4355 break;
4356
4357 case Instruction::Select: {
4358 // If we have a clamp pattern, we know that the number of sign bits will
4359 // be the minimum of the clamp min/max range.
4360 const Value *X;
4361 const APInt *CLow, *CHigh;
4362 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4363 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4364
4365 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4366 if (Tmp == 1)
4367 break;
4368 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4369 return std::min(Tmp, Tmp2);
4370 }
4371
4372 case Instruction::Add:
4373 // Add can have at most one carry bit. Thus we know that the output
4374 // is, at worst, one more bit than the inputs.
4375 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4376 if (Tmp == 1) break;
4377
4378 // Special case decrementing a value (ADD X, -1):
4379 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4380 if (CRHS->isAllOnesValue()) {
4381 KnownBits Known(TyBits);
4382 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4383
4384 // If the input is known to be 0 or 1, the output is 0/-1, which is
4385 // all sign bits set.
4386 if ((Known.Zero | 1).isAllOnes())
4387 return TyBits;
4388
4389 // If we are subtracting one from a positive number, there is no carry
4390 // out of the result.
4391 if (Known.isNonNegative())
4392 return Tmp;
4393 }
4394
4395 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4396 if (Tmp2 == 1)
4397 break;
4398 return std::min(Tmp, Tmp2) - 1;
4399
4400 case Instruction::Sub:
4401 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4402 if (Tmp2 == 1)
4403 break;
4404
4405 // Handle NEG.
4406 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4407 if (CLHS->isNullValue()) {
4408 KnownBits Known(TyBits);
4409 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4410 // If the input is known to be 0 or 1, the output is 0/-1, which is
4411 // all sign bits set.
4412 if ((Known.Zero | 1).isAllOnes())
4413 return TyBits;
4414
4415 // If the input is known to be positive (the sign bit is known clear),
4416 // the output of the NEG has the same number of sign bits as the
4417 // input.
4418 if (Known.isNonNegative())
4419 return Tmp2;
4420
4421 // Otherwise, we treat this like a SUB.
4422 }
4423
4424 // Sub can have at most one carry bit. Thus we know that the output
4425 // is, at worst, one more bit than the inputs.
4426 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4427 if (Tmp == 1)
4428 break;
4429 return std::min(Tmp, Tmp2) - 1;
4430
4431 case Instruction::Mul: {
4432 // The output of the Mul can be at most twice the valid bits in the
4433 // inputs.
4434 unsigned SignBitsOp0 =
4435 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4436 if (SignBitsOp0 == 1)
4437 break;
4438 unsigned SignBitsOp1 =
4439 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4440 if (SignBitsOp1 == 1)
4441 break;
4442 unsigned OutValidBits =
4443 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4444 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4445 }
4446
4447 case Instruction::PHI: {
4448 const PHINode *PN = cast<PHINode>(U);
4449 unsigned NumIncomingValues = PN->getNumIncomingValues();
4450 // Don't analyze large in-degree PHIs.
4451 if (NumIncomingValues > 4) break;
4452 // Unreachable blocks may have zero-operand PHI nodes.
4453 if (NumIncomingValues == 0) break;
4454
4455 // Take the minimum of all incoming values. This can't infinitely loop
4456 // because of our depth threshold.
4458 Tmp = TyBits;
4459 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4460 if (Tmp == 1) return Tmp;
4461 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4462 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4463 DemandedElts, RecQ, Depth + 1));
4464 }
4465 return Tmp;
4466 }
4467
4468 case Instruction::Trunc: {
4469 // If the input contained enough sign bits that some remain after the
4470 // truncation, then we can make use of that. Otherwise we don't know
4471 // anything.
4472 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4473 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4474 if (Tmp > (OperandTyBits - TyBits))
4475 return Tmp - (OperandTyBits - TyBits);
4476
4477 return 1;
4478 }
4479
4480 case Instruction::ExtractElement:
4481 // Look through extract element. At the moment we keep this simple and
4482 // skip tracking the specific element. But at least we might find
4483 // information valid for all elements of the vector (for example if vector
4484 // is sign extended, shifted, etc).
4485 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4486
4487 case Instruction::ShuffleVector: {
4488 // Collect the minimum number of sign bits that are shared by every vector
4489 // element referenced by the shuffle.
4490 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4491 if (!Shuf) {
4492 // FIXME: Add support for shufflevector constant expressions.
4493 return 1;
4494 }
4495 APInt DemandedLHS, DemandedRHS;
4496 // For undef elements, we don't know anything about the common state of
4497 // the shuffle result.
4498 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4499 return 1;
4500 Tmp = std::numeric_limits<unsigned>::max();
4501 if (!!DemandedLHS) {
4502 const Value *LHS = Shuf->getOperand(0);
4503 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4504 }
4505 // If we don't know anything, early out and try computeKnownBits
4506 // fall-back.
4507 if (Tmp == 1)
4508 break;
4509 if (!!DemandedRHS) {
4510 const Value *RHS = Shuf->getOperand(1);
4511 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4512 Tmp = std::min(Tmp, Tmp2);
4513 }
4514 // If we don't know anything, early out and try computeKnownBits
4515 // fall-back.
4516 if (Tmp == 1)
4517 break;
4518 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4519 return Tmp;
4520 }
4521 case Instruction::Call: {
4522 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4523 switch (II->getIntrinsicID()) {
4524 default:
4525 break;
4526 case Intrinsic::abs:
4527 Tmp =
4528 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4529 if (Tmp == 1)
4530 break;
4531
4532 // Absolute value reduces number of sign bits by at most 1.
4533 return Tmp - 1;
4534 case Intrinsic::smin:
4535 case Intrinsic::smax: {
4536 const APInt *CLow, *CHigh;
4537 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4538 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4539 }
4540 }
4541 }
4542 }
4543 }
4544 }
4545
4546 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4547 // use this information.
4548
4549 // If we can examine all elements of a vector constant successfully, we're
4550 // done (we can't do any better than that). If not, keep trying.
4551 if (unsigned VecSignBits =
4552 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4553 return VecSignBits;
4554
4555 KnownBits Known(TyBits);
4556 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4557
4558 // If we know that the sign bit is either zero or one, determine the number of
4559 // identical bits in the top of the input value.
4560 return std::max(FirstAnswer, Known.countMinSignBits());
4561}
4562
4564 const TargetLibraryInfo *TLI) {
4565 const Function *F = CB.getCalledFunction();
4566 if (!F)
4568
4569 if (F->isIntrinsic())
4570 return F->getIntrinsicID();
4571
4572 // We are going to infer semantics of a library function based on mapping it
4573 // to an LLVM intrinsic. Check that the library function is available from
4574 // this callbase and in this environment.
4575 LibFunc Func;
4576 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4577 !CB.onlyReadsMemory())
4579
4580 switch (Func) {
4581 default:
4582 break;
4583 case LibFunc_sin:
4584 case LibFunc_sinf:
4585 case LibFunc_sinl:
4586 return Intrinsic::sin;
4587 case LibFunc_cos:
4588 case LibFunc_cosf:
4589 case LibFunc_cosl:
4590 return Intrinsic::cos;
4591 case LibFunc_tan:
4592 case LibFunc_tanf:
4593 case LibFunc_tanl:
4594 return Intrinsic::tan;
4595 case LibFunc_asin:
4596 case LibFunc_asinf:
4597 case LibFunc_asinl:
4598 return Intrinsic::asin;
4599 case LibFunc_acos:
4600 case LibFunc_acosf:
4601 case LibFunc_acosl:
4602 return Intrinsic::acos;
4603 case LibFunc_atan:
4604 case LibFunc_atanf:
4605 case LibFunc_atanl:
4606 return Intrinsic::atan;
4607 case LibFunc_atan2:
4608 case LibFunc_atan2f:
4609 case LibFunc_atan2l:
4610 return Intrinsic::atan2;
4611 case LibFunc_sinh:
4612 case LibFunc_sinhf:
4613 case LibFunc_sinhl:
4614 return Intrinsic::sinh;
4615 case LibFunc_cosh:
4616 case LibFunc_coshf:
4617 case LibFunc_coshl:
4618 return Intrinsic::cosh;
4619 case LibFunc_tanh:
4620 case LibFunc_tanhf:
4621 case LibFunc_tanhl:
4622 return Intrinsic::tanh;
4623 case LibFunc_exp:
4624 case LibFunc_expf:
4625 case LibFunc_expl:
4626 return Intrinsic::exp;
4627 case LibFunc_exp2:
4628 case LibFunc_exp2f:
4629 case LibFunc_exp2l:
4630 return Intrinsic::exp2;
4631 case LibFunc_exp10:
4632 case LibFunc_exp10f:
4633 case LibFunc_exp10l:
4634 return Intrinsic::exp10;
4635 case LibFunc_log:
4636 case LibFunc_logf:
4637 case LibFunc_logl:
4638 return Intrinsic::log;
4639 case LibFunc_log10:
4640 case LibFunc_log10f:
4641 case LibFunc_log10l:
4642 return Intrinsic::log10;
4643 case LibFunc_log2:
4644 case LibFunc_log2f:
4645 case LibFunc_log2l:
4646 return Intrinsic::log2;
4647 case LibFunc_fabs:
4648 case LibFunc_fabsf:
4649 case LibFunc_fabsl:
4650 return Intrinsic::fabs;
4651 case LibFunc_fmin:
4652 case LibFunc_fminf:
4653 case LibFunc_fminl:
4654 return Intrinsic::minnum;
4655 case LibFunc_fmax:
4656 case LibFunc_fmaxf:
4657 case LibFunc_fmaxl:
4658 return Intrinsic::maxnum;
4659 case LibFunc_copysign:
4660 case LibFunc_copysignf:
4661 case LibFunc_copysignl:
4662 return Intrinsic::copysign;
4663 case LibFunc_floor:
4664 case LibFunc_floorf:
4665 case LibFunc_floorl:
4666 return Intrinsic::floor;
4667 case LibFunc_ceil:
4668 case LibFunc_ceilf:
4669 case LibFunc_ceill:
4670 return Intrinsic::ceil;
4671 case LibFunc_trunc:
4672 case LibFunc_truncf:
4673 case LibFunc_truncl:
4674 return Intrinsic::trunc;
4675 case LibFunc_rint:
4676 case LibFunc_rintf:
4677 case LibFunc_rintl:
4678 return Intrinsic::rint;
4679 case LibFunc_nearbyint:
4680 case LibFunc_nearbyintf:
4681 case LibFunc_nearbyintl:
4682 return Intrinsic::nearbyint;
4683 case LibFunc_round:
4684 case LibFunc_roundf:
4685 case LibFunc_roundl:
4686 return Intrinsic::round;
4687 case LibFunc_roundeven:
4688 case LibFunc_roundevenf:
4689 case LibFunc_roundevenl:
4690 return Intrinsic::roundeven;
4691 case LibFunc_pow:
4692 case LibFunc_powf:
4693 case LibFunc_powl:
4694 return Intrinsic::pow;
4695 case LibFunc_sqrt:
4696 case LibFunc_sqrtf:
4697 case LibFunc_sqrtl:
4698 return Intrinsic::sqrt;
4699 }
4700
4702}
4703
4704static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4705 Ty = Ty->getScalarType();
4706 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4707 return Mode.Output == DenormalMode::IEEE ||
4709}
4710/// Given an exploded icmp instruction, return true if the comparison only
4711/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4712/// the result of the comparison is true when the input value is signed.
4714 bool &TrueIfSigned) {
4715 switch (Pred) {
4716 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4717 TrueIfSigned = true;
4718 return RHS.isZero();
4719 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4720 TrueIfSigned = true;
4721 return RHS.isAllOnes();
4722 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4723 TrueIfSigned = false;
4724 return RHS.isAllOnes();
4725 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4726 TrueIfSigned = false;
4727 return RHS.isZero();
4728 case ICmpInst::ICMP_UGT:
4729 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4730 TrueIfSigned = true;
4731 return RHS.isMaxSignedValue();
4732 case ICmpInst::ICMP_UGE:
4733 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4734 TrueIfSigned = true;
4735 return RHS.isMinSignedValue();
4736 case ICmpInst::ICMP_ULT:
4737 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4738 TrueIfSigned = false;
4739 return RHS.isMinSignedValue();
4740 case ICmpInst::ICMP_ULE:
4741 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4742 TrueIfSigned = false;
4743 return RHS.isMaxSignedValue();
4744 default:
4745 return false;
4746 }
4747}
4748
4750 bool CondIsTrue,
4751 const Instruction *CxtI,
4752 KnownFPClass &KnownFromContext,
4753 unsigned Depth = 0) {
4754 Value *A, *B;
4756 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4757 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4758 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4759 Depth + 1);
4760 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4761 Depth + 1);
4762 return;
4763 }
4765 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4766 Depth + 1);
4767 return;
4768 }
4769 CmpPredicate Pred;
4770 Value *LHS;
4771 uint64_t ClassVal = 0;
4772 const APFloat *CRHS;
4773 const APInt *RHS;
4774 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4775 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4776 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4777 if (CmpVal == V)
4778 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4780 m_Specific(V), m_ConstantInt(ClassVal)))) {
4781 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4782 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4783 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4784 m_APInt(RHS)))) {
4785 bool TrueIfSigned;
4786 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4787 return;
4788 if (TrueIfSigned == CondIsTrue)
4789 KnownFromContext.signBitMustBeOne();
4790 else
4791 KnownFromContext.signBitMustBeZero();
4792 }
4793}
4794
4796 const SimplifyQuery &Q) {
4797 KnownFPClass KnownFromContext;
4798
4799 if (Q.CC && Q.CC->AffectedValues.contains(V))
4801 KnownFromContext);
4802
4803 if (!Q.CxtI)
4804 return KnownFromContext;
4805
4806 if (Q.DC && Q.DT) {
4807 // Handle dominating conditions.
4808 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4809 Value *Cond = BI->getCondition();
4810
4811 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4812 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4813 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4814 KnownFromContext);
4815
4816 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4817 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4818 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4819 KnownFromContext);
4820 }
4821 }
4822
4823 if (!Q.AC)
4824 return KnownFromContext;
4825
4826 // Try to restrict the floating-point classes based on information from
4827 // assumptions.
4828 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4829 if (!AssumeVH)
4830 continue;
4831 CallInst *I = cast<CallInst>(AssumeVH);
4832
4833 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4834 "Got assumption for the wrong function!");
4835 assert(I->getIntrinsicID() == Intrinsic::assume &&
4836 "must be an assume intrinsic");
4837
4838 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4839 continue;
4840
4841 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4842 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4843 }
4844
4845 return KnownFromContext;
4846}
4847
4848void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4849 FPClassTest InterestedClasses, KnownFPClass &Known,
4850 const SimplifyQuery &Q, unsigned Depth);
4851
4852static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4853 FPClassTest InterestedClasses,
4854 const SimplifyQuery &Q, unsigned Depth) {
4855 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4856 APInt DemandedElts =
4857 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4858 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4859}
4860
4862 const APInt &DemandedElts,
4863 FPClassTest InterestedClasses,
4864 KnownFPClass &Known,
4865 const SimplifyQuery &Q,
4866 unsigned Depth) {
4867 if ((InterestedClasses &
4869 return;
4870
4871 KnownFPClass KnownSrc;
4872 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4873 KnownSrc, Q, Depth + 1);
4874
4875 // Sign should be preserved
4876 // TODO: Handle cannot be ordered greater than zero
4877 if (KnownSrc.cannotBeOrderedLessThanZero())
4879
4880 Known.propagateNaN(KnownSrc, true);
4881
4882 // Infinity needs a range check.
4883}
4884
4885void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4886 FPClassTest InterestedClasses, KnownFPClass &Known,
4887 const SimplifyQuery &Q, unsigned Depth) {
4888 assert(Known.isUnknown() && "should not be called with known information");
4889
4890 if (!DemandedElts) {
4891 // No demanded elts, better to assume we don't know anything.
4892 Known.resetAll();
4893 return;
4894 }
4895
4896 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4897
4898 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4899 Known.KnownFPClasses = CFP->getValueAPF().classify();
4900 Known.SignBit = CFP->isNegative();
4901 return;
4902 }
4903
4905 Known.KnownFPClasses = fcPosZero;
4906 Known.SignBit = false;
4907 return;
4908 }
4909
4910 if (isa<PoisonValue>(V)) {
4911 Known.KnownFPClasses = fcNone;
4912 Known.SignBit = false;
4913 return;
4914 }
4915
4916 // Try to handle fixed width vector constants
4917 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
4918 const Constant *CV = dyn_cast<Constant>(V);
4919 if (VFVTy && CV) {
4920 Known.KnownFPClasses = fcNone;
4921 bool SignBitAllZero = true;
4922 bool SignBitAllOne = true;
4923
4924 // For vectors, verify that each element is not NaN.
4925 unsigned NumElts = VFVTy->getNumElements();
4926 for (unsigned i = 0; i != NumElts; ++i) {
4927 if (!DemandedElts[i])
4928 continue;
4929
4930 Constant *Elt = CV->getAggregateElement(i);
4931 if (!Elt) {
4932 Known = KnownFPClass();
4933 return;
4934 }
4935 if (isa<PoisonValue>(Elt))
4936 continue;
4937 auto *CElt = dyn_cast<ConstantFP>(Elt);
4938 if (!CElt) {
4939 Known = KnownFPClass();
4940 return;
4941 }
4942
4943 const APFloat &C = CElt->getValueAPF();
4944 Known.KnownFPClasses |= C.classify();
4945 if (C.isNegative())
4946 SignBitAllZero = false;
4947 else
4948 SignBitAllOne = false;
4949 }
4950 if (SignBitAllOne != SignBitAllZero)
4951 Known.SignBit = SignBitAllOne;
4952 return;
4953 }
4954
4955 FPClassTest KnownNotFromFlags = fcNone;
4956 if (const auto *CB = dyn_cast<CallBase>(V))
4957 KnownNotFromFlags |= CB->getRetNoFPClass();
4958 else if (const auto *Arg = dyn_cast<Argument>(V))
4959 KnownNotFromFlags |= Arg->getNoFPClass();
4960
4961 const Operator *Op = dyn_cast<Operator>(V);
4963 if (FPOp->hasNoNaNs())
4964 KnownNotFromFlags |= fcNan;
4965 if (FPOp->hasNoInfs())
4966 KnownNotFromFlags |= fcInf;
4967 }
4968
4969 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
4970 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
4971
4972 // We no longer need to find out about these bits from inputs if we can
4973 // assume this from flags/attributes.
4974 InterestedClasses &= ~KnownNotFromFlags;
4975
4976 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
4977 Known.knownNot(KnownNotFromFlags);
4978 if (!Known.SignBit && AssumedClasses.SignBit) {
4979 if (*AssumedClasses.SignBit)
4980 Known.signBitMustBeOne();
4981 else
4982 Known.signBitMustBeZero();
4983 }
4984 });
4985
4986 if (!Op)
4987 return;
4988
4989 // All recursive calls that increase depth must come after this.
4991 return;
4992
4993 const unsigned Opc = Op->getOpcode();
4994 switch (Opc) {
4995 case Instruction::FNeg: {
4996 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4997 Known, Q, Depth + 1);
4998 Known.fneg();
4999 break;
5000 }
5001 case Instruction::Select: {
5002 Value *Cond = Op->getOperand(0);
5003 Value *LHS = Op->getOperand(1);
5004 Value *RHS = Op->getOperand(2);
5005
5006 FPClassTest FilterLHS = fcAllFlags;
5007 FPClassTest FilterRHS = fcAllFlags;
5008
5009 Value *TestedValue = nullptr;
5010 FPClassTest MaskIfTrue = fcAllFlags;
5011 FPClassTest MaskIfFalse = fcAllFlags;
5012 uint64_t ClassVal = 0;
5013 const Function *F = cast<Instruction>(Op)->getFunction();
5014 CmpPredicate Pred;
5015 Value *CmpLHS, *CmpRHS;
5016 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
5017 // If the select filters out a value based on the class, it no longer
5018 // participates in the class of the result
5019
5020 // TODO: In some degenerate cases we can infer something if we try again
5021 // without looking through sign operations.
5022 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
5023 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
5024 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
5025 } else if (match(Cond,
5027 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
5028 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
5029 MaskIfTrue = TestedMask;
5030 MaskIfFalse = ~TestedMask;
5031 }
5032
5033 if (TestedValue == LHS) {
5034 // match !isnan(x) ? x : y
5035 FilterLHS = MaskIfTrue;
5036 } else if (TestedValue == RHS) { // && IsExactClass
5037 // match !isnan(x) ? y : x
5038 FilterRHS = MaskIfFalse;
5039 }
5040
5041 KnownFPClass Known2;
5042 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
5043 Q, Depth + 1);
5044 Known.KnownFPClasses &= FilterLHS;
5045
5046 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
5047 Known2, Q, Depth + 1);
5048 Known2.KnownFPClasses &= FilterRHS;
5049
5050 Known |= Known2;
5051 break;
5052 }
5053 case Instruction::Call: {
5054 const CallInst *II = cast<CallInst>(Op);
5055 const Intrinsic::ID IID = II->getIntrinsicID();
5056 switch (IID) {
5057 case Intrinsic::fabs: {
5058 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5059 // If we only care about the sign bit we don't need to inspect the
5060 // operand.
5061 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5062 InterestedClasses, Known, Q, Depth + 1);
5063 }
5064
5065 Known.fabs();
5066 break;
5067 }
5068 case Intrinsic::copysign: {
5069 KnownFPClass KnownSign;
5070
5071 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5072 Known, Q, Depth + 1);
5073 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5074 KnownSign, Q, Depth + 1);
5075 Known.copysign(KnownSign);
5076 break;
5077 }
5078 case Intrinsic::fma:
5079 case Intrinsic::fmuladd: {
5080 if ((InterestedClasses & fcNegative) == fcNone)
5081 break;
5082
5083 if (II->getArgOperand(0) != II->getArgOperand(1))
5084 break;
5085
5086 // The multiply cannot be -0 and therefore the add can't be -0
5087 Known.knownNot(fcNegZero);
5088
5089 // x * x + y is non-negative if y is non-negative.
5090 KnownFPClass KnownAddend;
5091 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
5092 KnownAddend, Q, Depth + 1);
5093
5094 if (KnownAddend.cannotBeOrderedLessThanZero())
5095 Known.knownNot(fcNegative);
5096 break;
5097 }
5098 case Intrinsic::sqrt:
5099 case Intrinsic::experimental_constrained_sqrt: {
5100 KnownFPClass KnownSrc;
5101 FPClassTest InterestedSrcs = InterestedClasses;
5102 if (InterestedClasses & fcNan)
5103 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5104
5105 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5106 KnownSrc, Q, Depth + 1);
5107
5108 if (KnownSrc.isKnownNeverPosInfinity())
5109 Known.knownNot(fcPosInf);
5110 if (KnownSrc.isKnownNever(fcSNan))
5111 Known.knownNot(fcSNan);
5112
5113 // Any negative value besides -0 returns a nan.
5114 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5115 Known.knownNot(fcNan);
5116
5117 // The only negative value that can be returned is -0 for -0 inputs.
5119
5120 // If the input denormal mode could be PreserveSign, a negative
5121 // subnormal input could produce a negative zero output.
5122 const Function *F = II->getFunction();
5123 const fltSemantics &FltSem =
5124 II->getType()->getScalarType()->getFltSemantics();
5125
5126 if (Q.IIQ.hasNoSignedZeros(II) ||
5127 (F &&
5128 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem))))
5129 Known.knownNot(fcNegZero);
5130
5131 break;
5132 }
5133 case Intrinsic::sin:
5134 case Intrinsic::cos: {
5135 // Return NaN on infinite inputs.
5136 KnownFPClass KnownSrc;
5137 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5138 KnownSrc, Q, Depth + 1);
5139 Known.knownNot(fcInf);
5140 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
5141 Known.knownNot(fcNan);
5142 break;
5143 }
5144 case Intrinsic::maxnum:
5145 case Intrinsic::minnum:
5146 case Intrinsic::minimum:
5147 case Intrinsic::maximum:
5148 case Intrinsic::minimumnum:
5149 case Intrinsic::maximumnum: {
5150 KnownFPClass KnownLHS, KnownRHS;
5151 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5152 KnownLHS, Q, Depth + 1);
5153 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5154 KnownRHS, Q, Depth + 1);
5155
5156 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
5157 Known = KnownLHS | KnownRHS;
5158
5159 // If either operand is not NaN, the result is not NaN.
5160 if (NeverNaN &&
5161 (IID == Intrinsic::minnum || IID == Intrinsic::maxnum ||
5162 IID == Intrinsic::minimumnum || IID == Intrinsic::maximumnum))
5163 Known.knownNot(fcNan);
5164
5165 if (IID == Intrinsic::maxnum || IID == Intrinsic::maximumnum) {
5166 // If at least one operand is known to be positive, the result must be
5167 // positive.
5168 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
5169 KnownLHS.isKnownNeverNaN()) ||
5170 (KnownRHS.cannotBeOrderedLessThanZero() &&
5171 KnownRHS.isKnownNeverNaN()))
5173 } else if (IID == Intrinsic::maximum) {
5174 // If at least one operand is known to be positive, the result must be
5175 // positive.
5176 if (KnownLHS.cannotBeOrderedLessThanZero() ||
5177 KnownRHS.cannotBeOrderedLessThanZero())
5179 } else if (IID == Intrinsic::minnum || IID == Intrinsic::minimumnum) {
5180 // If at least one operand is known to be negative, the result must be
5181 // negative.
5182 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
5183 KnownLHS.isKnownNeverNaN()) ||
5184 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
5185 KnownRHS.isKnownNeverNaN()))
5187 } else if (IID == Intrinsic::minimum) {
5188 // If at least one operand is known to be negative, the result must be
5189 // negative.
5190 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
5193 } else
5194 llvm_unreachable("unhandled intrinsic");
5195
5196 // Fixup zero handling if denormals could be returned as a zero.
5197 //
5198 // As there's no spec for denormal flushing, be conservative with the
5199 // treatment of denormals that could be flushed to zero. For older
5200 // subtargets on AMDGPU the min/max instructions would not flush the
5201 // output and return the original value.
5202 //
5203 if ((Known.KnownFPClasses & fcZero) != fcNone &&
5204 !Known.isKnownNeverSubnormal()) {
5205 const Function *Parent = II->getFunction();
5206 if (!Parent)
5207 break;
5208
5210 II->getType()->getScalarType()->getFltSemantics());
5211 if (Mode != DenormalMode::getIEEE())
5212 Known.KnownFPClasses |= fcZero;
5213 }
5214
5215 if (Known.isKnownNeverNaN()) {
5216 if (KnownLHS.SignBit && KnownRHS.SignBit &&
5217 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5218 if (*KnownLHS.SignBit)
5219 Known.signBitMustBeOne();
5220 else
5221 Known.signBitMustBeZero();
5222 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum ||
5223 IID == Intrinsic::maximumnum ||
5224 IID == Intrinsic::minimumnum) ||
5225 // FIXME: Should be using logical zero versions
5226 ((KnownLHS.isKnownNeverNegZero() ||
5227 KnownRHS.isKnownNeverPosZero()) &&
5228 (KnownLHS.isKnownNeverPosZero() ||
5229 KnownRHS.isKnownNeverNegZero()))) {
5230 // Don't take sign bit from NaN operands.
5231 if (!KnownLHS.isKnownNeverNaN())
5232 KnownLHS.SignBit = std::nullopt;
5233 if (!KnownRHS.isKnownNeverNaN())
5234 KnownRHS.SignBit = std::nullopt;
5235 if ((IID == Intrinsic::maximum || IID == Intrinsic::maximumnum ||
5236 IID == Intrinsic::maxnum) &&
5237 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5238 Known.signBitMustBeZero();
5239 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minimumnum ||
5240 IID == Intrinsic::minnum) &&
5241 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5242 Known.signBitMustBeOne();
5243 }
5244 }
5245 break;
5246 }
5247 case Intrinsic::canonicalize: {
5248 KnownFPClass KnownSrc;
5249 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5250 KnownSrc, Q, Depth + 1);
5251
5252 // This is essentially a stronger form of
5253 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5254 // actually have an IR canonicalization guarantee.
5255
5256 // Canonicalize may flush denormals to zero, so we have to consider the
5257 // denormal mode to preserve known-not-0 knowledge.
5258 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5259
5260 // Stronger version of propagateNaN
5261 // Canonicalize is guaranteed to quiet signaling nans.
5262 if (KnownSrc.isKnownNeverNaN())
5263 Known.knownNot(fcNan);
5264 else
5265 Known.knownNot(fcSNan);
5266
5267 const Function *F = II->getFunction();
5268 if (!F)
5269 break;
5270
5271 // If the parent function flushes denormals, the canonical output cannot
5272 // be a denormal.
5273 const fltSemantics &FPType =
5274 II->getType()->getScalarType()->getFltSemantics();
5275 DenormalMode DenormMode = F->getDenormalMode(FPType);
5276 if (DenormMode == DenormalMode::getIEEE()) {
5277 if (KnownSrc.isKnownNever(fcPosZero))
5278 Known.knownNot(fcPosZero);
5279 if (KnownSrc.isKnownNever(fcNegZero))
5280 Known.knownNot(fcNegZero);
5281 break;
5282 }
5283
5284 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5285 Known.knownNot(fcSubnormal);
5286
5287 if (DenormMode.Input == DenormalMode::PositiveZero ||
5288 (DenormMode.Output == DenormalMode::PositiveZero &&
5289 DenormMode.Input == DenormalMode::IEEE))
5290 Known.knownNot(fcNegZero);
5291
5292 break;
5293 }
5294 case Intrinsic::vector_reduce_fmax:
5295 case Intrinsic::vector_reduce_fmin:
5296 case Intrinsic::vector_reduce_fmaximum:
5297 case Intrinsic::vector_reduce_fminimum: {
5298 // reduce min/max will choose an element from one of the vector elements,
5299 // so we can infer and class information that is common to all elements.
5300 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5301 InterestedClasses, Q, Depth + 1);
5302 // Can only propagate sign if output is never NaN.
5303 if (!Known.isKnownNeverNaN())
5304 Known.SignBit.reset();
5305 break;
5306 }
5307 // reverse preserves all characteristics of the input vec's element.
5308 case Intrinsic::vector_reverse:
5309 Known = computeKnownFPClass(
5310 II->getArgOperand(0), DemandedElts.reverseBits(),
5311 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5312 break;
5313 case Intrinsic::trunc:
5314 case Intrinsic::floor:
5315 case Intrinsic::ceil:
5316 case Intrinsic::rint:
5317 case Intrinsic::nearbyint:
5318 case Intrinsic::round:
5319 case Intrinsic::roundeven: {
5320 KnownFPClass KnownSrc;
5321 FPClassTest InterestedSrcs = InterestedClasses;
5322 if (InterestedSrcs & fcPosFinite)
5323 InterestedSrcs |= fcPosFinite;
5324 if (InterestedSrcs & fcNegFinite)
5325 InterestedSrcs |= fcNegFinite;
5326 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5327 KnownSrc, Q, Depth + 1);
5328
5329 // Integer results cannot be subnormal.
5330 Known.knownNot(fcSubnormal);
5331
5332 Known.propagateNaN(KnownSrc, true);
5333
5334 // Pass through infinities, except PPC_FP128 is a special case for
5335 // intrinsics other than trunc.
5336 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5337 if (KnownSrc.isKnownNeverPosInfinity())
5338 Known.knownNot(fcPosInf);
5339 if (KnownSrc.isKnownNeverNegInfinity())
5340 Known.knownNot(fcNegInf);
5341 }
5342
5343 // Negative round ups to 0 produce -0
5344 if (KnownSrc.isKnownNever(fcPosFinite))
5345 Known.knownNot(fcPosFinite);
5346 if (KnownSrc.isKnownNever(fcNegFinite))
5347 Known.knownNot(fcNegFinite);
5348
5349 break;
5350 }
5351 case Intrinsic::exp:
5352 case Intrinsic::exp2:
5353 case Intrinsic::exp10: {
5354 Known.knownNot(fcNegative);
5355 if ((InterestedClasses & fcNan) == fcNone)
5356 break;
5357
5358 KnownFPClass KnownSrc;
5359 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5360 KnownSrc, Q, Depth + 1);
5361 if (KnownSrc.isKnownNeverNaN()) {
5362 Known.knownNot(fcNan);
5363 Known.signBitMustBeZero();
5364 }
5365
5366 break;
5367 }
5368 case Intrinsic::fptrunc_round: {
5369 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5370 Q, Depth);
5371 break;
5372 }
5373 case Intrinsic::log:
5374 case Intrinsic::log10:
5375 case Intrinsic::log2:
5376 case Intrinsic::experimental_constrained_log:
5377 case Intrinsic::experimental_constrained_log10:
5378 case Intrinsic::experimental_constrained_log2: {
5379 // log(+inf) -> +inf
5380 // log([+-]0.0) -> -inf
5381 // log(-inf) -> nan
5382 // log(-x) -> nan
5383 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5384 break;
5385
5386 FPClassTest InterestedSrcs = InterestedClasses;
5387 if ((InterestedClasses & fcNegInf) != fcNone)
5388 InterestedSrcs |= fcZero | fcSubnormal;
5389 if ((InterestedClasses & fcNan) != fcNone)
5390 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5391
5392 KnownFPClass KnownSrc;
5393 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5394 KnownSrc, Q, Depth + 1);
5395
5396 if (KnownSrc.isKnownNeverPosInfinity())
5397 Known.knownNot(fcPosInf);
5398
5399 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5400 Known.knownNot(fcNan);
5401
5402 const Function *F = II->getFunction();
5403
5404 if (!F)
5405 break;
5406
5407 const fltSemantics &FltSem =
5408 II->getType()->getScalarType()->getFltSemantics();
5409 DenormalMode Mode = F->getDenormalMode(FltSem);
5410
5411 if (KnownSrc.isKnownNeverLogicalZero(Mode))
5412 Known.knownNot(fcNegInf);
5413
5414 break;
5415 }
5416 case Intrinsic::powi: {
5417 if ((InterestedClasses & fcNegative) == fcNone)
5418 break;
5419
5420 const Value *Exp = II->getArgOperand(1);
5421 Type *ExpTy = Exp->getType();
5422 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5423 KnownBits ExponentKnownBits(BitWidth);
5424 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5425 ExponentKnownBits, Q, Depth + 1);
5426
5427 if (ExponentKnownBits.Zero[0]) { // Is even
5428 Known.knownNot(fcNegative);
5429 break;
5430 }
5431
5432 // Given that exp is an integer, here are the
5433 // ways that pow can return a negative value:
5434 //
5435 // pow(-x, exp) --> negative if exp is odd and x is negative.
5436 // pow(-0, exp) --> -inf if exp is negative odd.
5437 // pow(-0, exp) --> -0 if exp is positive odd.
5438 // pow(-inf, exp) --> -0 if exp is negative odd.
5439 // pow(-inf, exp) --> -inf if exp is positive odd.
5440 KnownFPClass KnownSrc;
5441 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5442 KnownSrc, Q, Depth + 1);
5443 if (KnownSrc.isKnownNever(fcNegative))
5444 Known.knownNot(fcNegative);
5445 break;
5446 }
5447 case Intrinsic::ldexp: {
5448 KnownFPClass KnownSrc;
5449 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5450 KnownSrc, Q, Depth + 1);
5451 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5452
5453 // Sign is preserved, but underflows may produce zeroes.
5454 if (KnownSrc.isKnownNever(fcNegative))
5455 Known.knownNot(fcNegative);
5456 else if (KnownSrc.cannotBeOrderedLessThanZero())
5458
5459 if (KnownSrc.isKnownNever(fcPositive))
5460 Known.knownNot(fcPositive);
5461 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5463
5464 // Can refine inf/zero handling based on the exponent operand.
5465 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5466 if ((InterestedClasses & ExpInfoMask) == fcNone)
5467 break;
5468 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5469 break;
5470
5471 const fltSemantics &Flt =
5472 II->getType()->getScalarType()->getFltSemantics();
5473 unsigned Precision = APFloat::semanticsPrecision(Flt);
5474 const Value *ExpArg = II->getArgOperand(1);
5476 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5477
5478 const int MantissaBits = Precision - 1;
5479 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5480 Known.knownNot(fcSubnormal);
5481
5482 const Function *F = II->getFunction();
5483 const APInt *ConstVal = ExpRange.getSingleElement();
5484 const fltSemantics &FltSem =
5485 II->getType()->getScalarType()->getFltSemantics();
5486 if (ConstVal && ConstVal->isZero()) {
5487 // ldexp(x, 0) -> x, so propagate everything.
5488 Known.propagateCanonicalizingSrc(KnownSrc, F->getDenormalMode(FltSem));
5489 } else if (ExpRange.isAllNegative()) {
5490 // If we know the power is <= 0, can't introduce inf
5491 if (KnownSrc.isKnownNeverPosInfinity())
5492 Known.knownNot(fcPosInf);
5493 if (KnownSrc.isKnownNeverNegInfinity())
5494 Known.knownNot(fcNegInf);
5495 } else if (ExpRange.isAllNonNegative()) {
5496 // If we know the power is >= 0, can't introduce subnormal or zero
5497 if (KnownSrc.isKnownNeverPosSubnormal())
5498 Known.knownNot(fcPosSubnormal);
5499 if (KnownSrc.isKnownNeverNegSubnormal())
5500 Known.knownNot(fcNegSubnormal);
5501 if (F &&
5502 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5503 Known.knownNot(fcPosZero);
5504 if (F &&
5505 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5506 Known.knownNot(fcNegZero);
5507 }
5508
5509 break;
5510 }
5511 case Intrinsic::arithmetic_fence: {
5512 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5513 Known, Q, Depth + 1);
5514 break;
5515 }
5516 case Intrinsic::experimental_constrained_sitofp:
5517 case Intrinsic::experimental_constrained_uitofp:
5518 // Cannot produce nan
5519 Known.knownNot(fcNan);
5520
5521 // sitofp and uitofp turn into +0.0 for zero.
5522 Known.knownNot(fcNegZero);
5523
5524 // Integers cannot be subnormal
5525 Known.knownNot(fcSubnormal);
5526
5527 if (IID == Intrinsic::experimental_constrained_uitofp)
5528 Known.signBitMustBeZero();
5529
5530 // TODO: Copy inf handling from instructions
5531 break;
5532 default:
5533 break;
5534 }
5535
5536 break;
5537 }
5538 case Instruction::FAdd:
5539 case Instruction::FSub: {
5540 KnownFPClass KnownLHS, KnownRHS;
5541 bool WantNegative =
5542 Op->getOpcode() == Instruction::FAdd &&
5543 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5544 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5545 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5546
5547 if (!WantNaN && !WantNegative && !WantNegZero)
5548 break;
5549
5550 FPClassTest InterestedSrcs = InterestedClasses;
5551 if (WantNegative)
5552 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5553 if (InterestedClasses & fcNan)
5554 InterestedSrcs |= fcInf;
5555 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5556 KnownRHS, Q, Depth + 1);
5557
5558 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5559 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5560 WantNegZero || Opc == Instruction::FSub) {
5561
5562 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5563 // there's no point.
5564 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5565 KnownLHS, Q, Depth + 1);
5566 // Adding positive and negative infinity produces NaN.
5567 // TODO: Check sign of infinities.
5568 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5569 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5570 Known.knownNot(fcNan);
5571
5572 // FIXME: Context function should always be passed in separately
5573 const Function *F = cast<Instruction>(Op)->getFunction();
5574
5575 if (Op->getOpcode() == Instruction::FAdd) {
5576 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5577 KnownRHS.cannotBeOrderedLessThanZero())
5579 if (!F)
5580 break;
5581
5582 const fltSemantics &FltSem =
5583 Op->getType()->getScalarType()->getFltSemantics();
5584 DenormalMode Mode = F->getDenormalMode(FltSem);
5585
5586 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5587 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5588 KnownRHS.isKnownNeverLogicalNegZero(Mode)) &&
5589 // Make sure output negative denormal can't flush to -0
5590 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5591 Known.knownNot(fcNegZero);
5592 } else {
5593 if (!F)
5594 break;
5595
5596 const fltSemantics &FltSem =
5597 Op->getType()->getScalarType()->getFltSemantics();
5598 DenormalMode Mode = F->getDenormalMode(FltSem);
5599
5600 // Only fsub -0, +0 can return -0
5601 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5602 KnownRHS.isKnownNeverLogicalPosZero(Mode)) &&
5603 // Make sure output negative denormal can't flush to -0
5604 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5605 Known.knownNot(fcNegZero);
5606 }
5607 }
5608
5609 break;
5610 }
5611 case Instruction::FMul: {
5612 // X * X is always non-negative or a NaN.
5613 if (Op->getOperand(0) == Op->getOperand(1))
5614 Known.knownNot(fcNegative);
5615
5616 if ((InterestedClasses & fcNan) != fcNan)
5617 break;
5618
5619 // fcSubnormal is only needed in case of DAZ.
5620 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5621
5622 KnownFPClass KnownLHS, KnownRHS;
5623 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5624 Q, Depth + 1);
5625 if (!KnownRHS.isKnownNeverNaN())
5626 break;
5627
5628 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5629 Q, Depth + 1);
5630 if (!KnownLHS.isKnownNeverNaN())
5631 break;
5632
5633 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5634 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5635 Known.signBitMustBeZero();
5636 else
5637 Known.signBitMustBeOne();
5638 }
5639
5640 // If 0 * +/-inf produces NaN.
5641 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5642 Known.knownNot(fcNan);
5643 break;
5644 }
5645
5646 const Function *F = cast<Instruction>(Op)->getFunction();
5647 if (!F)
5648 break;
5649
5650 Type *OpTy = Op->getType()->getScalarType();
5651 const fltSemantics &FltSem = OpTy->getFltSemantics();
5652 DenormalMode Mode = F->getDenormalMode(FltSem);
5653
5654 if ((KnownRHS.isKnownNeverInfinity() ||
5655 KnownLHS.isKnownNeverLogicalZero(Mode)) &&
5656 (KnownLHS.isKnownNeverInfinity() ||
5657 KnownRHS.isKnownNeverLogicalZero(Mode)))
5658 Known.knownNot(fcNan);
5659
5660 break;
5661 }
5662 case Instruction::FDiv:
5663 case Instruction::FRem: {
5664 if (Op->getOperand(0) == Op->getOperand(1)) {
5665 // TODO: Could filter out snan if we inspect the operand
5666 if (Op->getOpcode() == Instruction::FDiv) {
5667 // X / X is always exactly 1.0 or a NaN.
5669 } else {
5670 // X % X is always exactly [+-]0.0 or a NaN.
5671 Known.KnownFPClasses = fcNan | fcZero;
5672 }
5673
5674 break;
5675 }
5676
5677 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5678 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5679 const bool WantPositive =
5680 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5681 if (!WantNan && !WantNegative && !WantPositive)
5682 break;
5683
5684 KnownFPClass KnownLHS, KnownRHS;
5685
5686 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5687 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5688 Depth + 1);
5689
5690 bool KnowSomethingUseful =
5691 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5692
5693 if (KnowSomethingUseful || WantPositive) {
5694 const FPClassTest InterestedLHS =
5695 WantPositive ? fcAllFlags
5697
5698 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5699 InterestedClasses & InterestedLHS, KnownLHS, Q,
5700 Depth + 1);
5701 }
5702
5703 const Function *F = cast<Instruction>(Op)->getFunction();
5704 const fltSemantics &FltSem =
5705 Op->getType()->getScalarType()->getFltSemantics();
5706
5707 if (Op->getOpcode() == Instruction::FDiv) {
5708 // Only 0/0, Inf/Inf produce NaN.
5709 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5710 (KnownLHS.isKnownNeverInfinity() ||
5711 KnownRHS.isKnownNeverInfinity()) &&
5712 ((F &&
5713 KnownLHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) ||
5714 (F &&
5715 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))))) {
5716 Known.knownNot(fcNan);
5717 }
5718
5719 // X / -0.0 is -Inf (or NaN).
5720 // +X / +X is +X
5721 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5722 Known.knownNot(fcNegative);
5723 } else {
5724 // Inf REM x and x REM 0 produce NaN.
5725 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5726 KnownLHS.isKnownNeverInfinity() && F &&
5727 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5728 Known.knownNot(fcNan);
5729 }
5730
5731 // The sign for frem is the same as the first operand.
5732 if (KnownLHS.cannotBeOrderedLessThanZero())
5734 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5736
5737 // See if we can be more aggressive about the sign of 0.
5738 if (KnownLHS.isKnownNever(fcNegative))
5739 Known.knownNot(fcNegative);
5740 if (KnownLHS.isKnownNever(fcPositive))
5741 Known.knownNot(fcPositive);
5742 }
5743
5744 break;
5745 }
5746 case Instruction::FPExt: {
5747 // Infinity, nan and zero propagate from source.
5748 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5749 Known, Q, Depth + 1);
5750
5751 const fltSemantics &DstTy =
5752 Op->getType()->getScalarType()->getFltSemantics();
5753 const fltSemantics &SrcTy =
5754 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5755
5756 // All subnormal inputs should be in the normal range in the result type.
5757 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5758 if (Known.KnownFPClasses & fcPosSubnormal)
5759 Known.KnownFPClasses |= fcPosNormal;
5760 if (Known.KnownFPClasses & fcNegSubnormal)
5761 Known.KnownFPClasses |= fcNegNormal;
5762 Known.knownNot(fcSubnormal);
5763 }
5764
5765 // Sign bit of a nan isn't guaranteed.
5766 if (!Known.isKnownNeverNaN())
5767 Known.SignBit = std::nullopt;
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 // For undef elements, we don't know anything about the common state of
5862 // the shuffle result.
5863 APInt DemandedLHS, DemandedRHS;
5864 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5865 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5866 return;
5867
5868 if (!!DemandedLHS) {
5869 const Value *LHS = Shuf->getOperand(0);
5870 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5871 Depth + 1);
5872
5873 // If we don't know any bits, early out.
5874 if (Known.isUnknown())
5875 break;
5876 } else {
5877 Known.KnownFPClasses = fcNone;
5878 }
5879
5880 if (!!DemandedRHS) {
5881 KnownFPClass Known2;
5882 const Value *RHS = Shuf->getOperand(1);
5883 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5884 Depth + 1);
5885 Known |= Known2;
5886 }
5887
5888 break;
5889 }
5890 case Instruction::ExtractValue: {
5891 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5892 ArrayRef<unsigned> Indices = Extract->getIndices();
5893 const Value *Src = Extract->getAggregateOperand();
5894 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5895 Indices[0] == 0) {
5896 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5897 switch (II->getIntrinsicID()) {
5898 case Intrinsic::frexp: {
5899 Known.knownNot(fcSubnormal);
5900
5901 KnownFPClass KnownSrc;
5902 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5903 InterestedClasses, KnownSrc, Q, Depth + 1);
5904
5905 const Function *F = cast<Instruction>(Op)->getFunction();
5906 const fltSemantics &FltSem =
5907 Op->getType()->getScalarType()->getFltSemantics();
5908
5909 if (KnownSrc.isKnownNever(fcNegative))
5910 Known.knownNot(fcNegative);
5911 else {
5912 if (F &&
5913 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5914 Known.knownNot(fcNegZero);
5915 if (KnownSrc.isKnownNever(fcNegInf))
5916 Known.knownNot(fcNegInf);
5917 }
5918
5919 if (KnownSrc.isKnownNever(fcPositive))
5920 Known.knownNot(fcPositive);
5921 else {
5922 if (F &&
5923 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5924 Known.knownNot(fcPosZero);
5925 if (KnownSrc.isKnownNever(fcPosInf))
5926 Known.knownNot(fcPosInf);
5927 }
5928
5929 Known.propagateNaN(KnownSrc);
5930 return;
5931 }
5932 default:
5933 break;
5934 }
5935 }
5936 }
5937
5938 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
5939 Depth + 1);
5940 break;
5941 }
5942 case Instruction::PHI: {
5943 const PHINode *P = cast<PHINode>(Op);
5944 // Unreachable blocks may have zero-operand PHI nodes.
5945 if (P->getNumIncomingValues() == 0)
5946 break;
5947
5948 // Otherwise take the unions of the known bit sets of the operands,
5949 // taking conservative care to avoid excessive recursion.
5950 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5951
5952 if (Depth < PhiRecursionLimit) {
5953 // Skip if every incoming value references to ourself.
5954 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5955 break;
5956
5957 bool First = true;
5958
5959 for (const Use &U : P->operands()) {
5960 Value *IncValue;
5961 Instruction *CxtI;
5962 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
5963 // Skip direct self references.
5964 if (IncValue == P)
5965 continue;
5966
5967 KnownFPClass KnownSrc;
5968 // Recurse, but cap the recursion to two levels, because we don't want
5969 // to waste time spinning around in loops. We need at least depth 2 to
5970 // detect known sign bits.
5971 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
5973 PhiRecursionLimit);
5974
5975 if (First) {
5976 Known = KnownSrc;
5977 First = false;
5978 } else {
5979 Known |= KnownSrc;
5980 }
5981
5982 if (Known.KnownFPClasses == fcAllFlags)
5983 break;
5984 }
5985 }
5986
5987 break;
5988 }
5989 case Instruction::BitCast: {
5990 const Value *Src;
5991 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
5992 !Src->getType()->isIntOrIntVectorTy())
5993 break;
5994
5995 const Type *Ty = Op->getType()->getScalarType();
5996 KnownBits Bits(Ty->getScalarSizeInBits());
5997 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
5998
5999 // Transfer information from the sign bit.
6000 if (Bits.isNonNegative())
6001 Known.signBitMustBeZero();
6002 else if (Bits.isNegative())
6003 Known.signBitMustBeOne();
6004
6005 if (Ty->isIEEELikeFPTy()) {
6006 // IEEE floats are NaN when all bits of the exponent plus at least one of
6007 // the fraction bits are 1. This means:
6008 // - If we assume unknown bits are 0 and the value is NaN, it will
6009 // always be NaN
6010 // - If we assume unknown bits are 1 and the value is not NaN, it can
6011 // never be NaN
6012 // Note: They do not hold for x86_fp80 format.
6013 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
6014 Known.KnownFPClasses = fcNan;
6015 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
6016 Known.knownNot(fcNan);
6017
6018 // Build KnownBits representing Inf and check if it must be equal or
6019 // unequal to this value.
6020 auto InfKB = KnownBits::makeConstant(
6021 APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());
6022 InfKB.Zero.clearSignBit();
6023 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
6024 assert(!InfResult.value());
6025 Known.knownNot(fcInf);
6026 } else if (Bits == InfKB) {
6027 Known.KnownFPClasses = fcInf;
6028 }
6029
6030 // Build KnownBits representing Zero and check if it must be equal or
6031 // unequal to this value.
6032 auto ZeroKB = KnownBits::makeConstant(
6033 APFloat::getZero(Ty->getFltSemantics()).bitcastToAPInt());
6034 ZeroKB.Zero.clearSignBit();
6035 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
6036 assert(!ZeroResult.value());
6037 Known.knownNot(fcZero);
6038 } else if (Bits == ZeroKB) {
6039 Known.KnownFPClasses = fcZero;
6040 }
6041 }
6042
6043 break;
6044 }
6045 default:
6046 break;
6047 }
6048}
6049
6051 const APInt &DemandedElts,
6052 FPClassTest InterestedClasses,
6053 const SimplifyQuery &SQ,
6054 unsigned Depth) {
6055 KnownFPClass KnownClasses;
6056 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
6057 Depth);
6058 return KnownClasses;
6059}
6060
6062 FPClassTest InterestedClasses,
6063 const SimplifyQuery &SQ,
6064 unsigned Depth) {
6065 KnownFPClass Known;
6066 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
6067 return Known;
6068}
6069
6071 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
6072 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
6073 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
6074 return computeKnownFPClass(V, InterestedClasses,
6075 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
6076 Depth);
6077}
6078
6080llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
6081 FastMathFlags FMF, FPClassTest InterestedClasses,
6082 const SimplifyQuery &SQ, unsigned Depth) {
6083 if (FMF.noNaNs())
6084 InterestedClasses &= ~fcNan;
6085 if (FMF.noInfs())
6086 InterestedClasses &= ~fcInf;
6087
6088 KnownFPClass Result =
6089 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
6090
6091 if (FMF.noNaNs())
6092 Result.KnownFPClasses &= ~fcNan;
6093 if (FMF.noInfs())
6094 Result.KnownFPClasses &= ~fcInf;
6095 return Result;
6096}
6097
6099 FPClassTest InterestedClasses,
6100 const SimplifyQuery &SQ,
6101 unsigned Depth) {
6102 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
6103 APInt DemandedElts =
6104 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
6105 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
6106 Depth);
6107}
6108
6110 unsigned Depth) {
6112 return Known.isKnownNeverNegZero();
6113}
6114
6121
6123 unsigned Depth) {
6125 return Known.isKnownNeverInfinity();
6126}
6127
6128/// Return true if the floating-point value can never contain a NaN or infinity.
6130 unsigned Depth) {
6132 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
6133}
6134
6135/// Return true if the floating-point scalar value is not a NaN or if the
6136/// floating-point vector value has no NaN elements. Return false if a value
6137/// could ever be NaN.
6139 unsigned Depth) {
6141 return Known.isKnownNeverNaN();
6142}
6143
6144/// Return false if we can prove that the specified FP value's sign bit is 0.
6145/// Return true if we can prove that the specified FP value's sign bit is 1.
6146/// Otherwise return std::nullopt.
6147std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6148 const SimplifyQuery &SQ,
6149 unsigned Depth) {
6151 return Known.SignBit;
6152}
6153
6155 auto *User = cast<Instruction>(U.getUser());
6156 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6157 if (FPOp->hasNoSignedZeros())
6158 return true;
6159 }
6160
6161 switch (User->getOpcode()) {
6162 case Instruction::FPToSI:
6163 case Instruction::FPToUI:
6164 return true;
6165 case Instruction::FCmp:
6166 // fcmp treats both positive and negative zero as equal.
6167 return true;
6168 case Instruction::Call:
6169 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6170 switch (II->getIntrinsicID()) {
6171 case Intrinsic::fabs:
6172 return true;
6173 case Intrinsic::copysign:
6174 return U.getOperandNo() == 0;
6175 case Intrinsic::is_fpclass:
6176 case Intrinsic::vp_is_fpclass: {
6177 auto Test =
6178 static_cast<FPClassTest>(
6179 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6182 }
6183 default:
6184 return false;
6185 }
6186 }
6187 return false;
6188 default:
6189 return false;
6190 }
6191}
6192
6194 auto *User = cast<Instruction>(U.getUser());
6195 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6196 if (FPOp->hasNoNaNs())
6197 return true;
6198 }
6199
6200 switch (User->getOpcode()) {
6201 case Instruction::FPToSI:
6202 case Instruction::FPToUI:
6203 return true;
6204 // Proper FP math operations ignore the sign bit of NaN.
6205 case Instruction::FAdd:
6206 case Instruction::FSub:
6207 case Instruction::FMul:
6208 case Instruction::FDiv:
6209 case Instruction::FRem:
6210 case Instruction::FPTrunc:
6211 case Instruction::FPExt:
6212 case Instruction::FCmp:
6213 return true;
6214 // Bitwise FP operations should preserve the sign bit of NaN.
6215 case Instruction::FNeg:
6216 case Instruction::Select:
6217 case Instruction::PHI:
6218 return false;
6219 case Instruction::Ret:
6220 return User->getFunction()->getAttributes().getRetNoFPClass() &
6222 case Instruction::Call:
6223 case Instruction::Invoke: {
6224 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6225 switch (II->getIntrinsicID()) {
6226 case Intrinsic::fabs:
6227 return true;
6228 case Intrinsic::copysign:
6229 return U.getOperandNo() == 0;
6230 // Other proper FP math intrinsics ignore the sign bit of NaN.
6231 case Intrinsic::maxnum:
6232 case Intrinsic::minnum:
6233 case Intrinsic::maximum:
6234 case Intrinsic::minimum:
6235 case Intrinsic::maximumnum:
6236 case Intrinsic::minimumnum:
6237 case Intrinsic::canonicalize:
6238 case Intrinsic::fma:
6239 case Intrinsic::fmuladd:
6240 case Intrinsic::sqrt:
6241 case Intrinsic::pow:
6242 case Intrinsic::powi:
6243 case Intrinsic::fptoui_sat:
6244 case Intrinsic::fptosi_sat:
6245 case Intrinsic::is_fpclass:
6246 case Intrinsic::vp_is_fpclass:
6247 return true;
6248 default:
6249 return false;
6250 }
6251 }
6252
6253 FPClassTest NoFPClass =
6254 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6255 return NoFPClass & FPClassTest::fcNan;
6256 }
6257 default:
6258 return false;
6259 }
6260}
6261
6263
6264 // All byte-wide stores are splatable, even of arbitrary variables.
6265 if (V->getType()->isIntegerTy(8))
6266 return V;
6267
6268 LLVMContext &Ctx = V->getContext();
6269
6270 // Undef don't care.
6271 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6272 if (isa<UndefValue>(V))
6273 return UndefInt8;
6274
6275 // Return poison for zero-sized type.
6276 if (DL.getTypeStoreSize(V->getType()).isZero())
6277 return PoisonValue::get(Type::getInt8Ty(Ctx));
6278
6280 if (!C) {
6281 // Conceptually, we could handle things like:
6282 // %a = zext i8 %X to i16
6283 // %b = shl i16 %a, 8
6284 // %c = or i16 %a, %b
6285 // but until there is an example that actually needs this, it doesn't seem
6286 // worth worrying about.
6287 return nullptr;
6288 }
6289
6290 // Handle 'null' ConstantArrayZero etc.
6291 if (C->isNullValue())
6293
6294 // Constant floating-point values can be handled as integer values if the
6295 // corresponding integer value is "byteable". An important case is 0.0.
6296 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6297 Type *Ty = nullptr;
6298 if (CFP->getType()->isHalfTy())
6299 Ty = Type::getInt16Ty(Ctx);
6300 else if (CFP->getType()->isFloatTy())
6301 Ty = Type::getInt32Ty(Ctx);
6302 else if (CFP->getType()->isDoubleTy())
6303 Ty = Type::getInt64Ty(Ctx);
6304 // Don't handle long double formats, which have strange constraints.
6305 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6306 : nullptr;
6307 }
6308
6309 // We can handle constant integers that are multiple of 8 bits.
6310 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6311 if (CI->getBitWidth() % 8 == 0) {
6312 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6313 if (!CI->getValue().isSplat(8))
6314 return nullptr;
6315 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6316 }
6317 }
6318
6319 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6320 if (CE->getOpcode() == Instruction::IntToPtr) {
6321 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6322 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6324 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6325 return isBytewiseValue(Op, DL);
6326 }
6327 }
6328 }
6329
6330 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6331 if (LHS == RHS)
6332 return LHS;
6333 if (!LHS || !RHS)
6334 return nullptr;
6335 if (LHS == UndefInt8)
6336 return RHS;
6337 if (RHS == UndefInt8)
6338 return LHS;
6339 return nullptr;
6340 };
6341
6343 Value *Val = UndefInt8;
6344 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6345 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6346 return nullptr;
6347 return Val;
6348 }
6349
6351 Value *Val = UndefInt8;
6352 for (Value *Op : C->operands())
6353 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6354 return nullptr;
6355 return Val;
6356 }
6357
6358 // Don't try to handle the handful of other constants.
6359 return nullptr;
6360}
6361
6362// This is the recursive version of BuildSubAggregate. It takes a few different
6363// arguments. Idxs is the index within the nested struct From that we are
6364// looking at now (which is of type IndexedType). IdxSkip is the number of
6365// indices from Idxs that should be left out when inserting into the resulting
6366// struct. To is the result struct built so far, new insertvalue instructions
6367// build on that.
6368static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6370 unsigned IdxSkip,
6371 BasicBlock::iterator InsertBefore) {
6372 StructType *STy = dyn_cast<StructType>(IndexedType);
6373 if (STy) {
6374 // Save the original To argument so we can modify it
6375 Value *OrigTo = To;
6376 // General case, the type indexed by Idxs is a struct
6377 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6378 // Process each struct element recursively
6379 Idxs.push_back(i);
6380 Value *PrevTo = To;
6381 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6382 InsertBefore);
6383 Idxs.pop_back();
6384 if (!To) {
6385 // Couldn't find any inserted value for this index? Cleanup
6386 while (PrevTo != OrigTo) {
6388 PrevTo = Del->getAggregateOperand();
6389 Del->eraseFromParent();
6390 }
6391 // Stop processing elements
6392 break;
6393 }
6394 }
6395 // If we successfully found a value for each of our subaggregates
6396 if (To)
6397 return To;
6398 }
6399 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6400 // the struct's elements had a value that was inserted directly. In the latter
6401 // case, perhaps we can't determine each of the subelements individually, but
6402 // we might be able to find the complete struct somewhere.
6403
6404 // Find the value that is at that particular spot
6405 Value *V = FindInsertedValue(From, Idxs);
6406
6407 if (!V)
6408 return nullptr;
6409
6410 // Insert the value in the new (sub) aggregate
6411 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6412 InsertBefore);
6413}
6414
6415// This helper takes a nested struct and extracts a part of it (which is again a
6416// struct) into a new value. For example, given the struct:
6417// { a, { b, { c, d }, e } }
6418// and the indices "1, 1" this returns
6419// { c, d }.
6420//
6421// It does this by inserting an insertvalue for each element in the resulting
6422// struct, as opposed to just inserting a single struct. This will only work if
6423// each of the elements of the substruct are known (ie, inserted into From by an
6424// insertvalue instruction somewhere).
6425//
6426// All inserted insertvalue instructions are inserted before InsertBefore
6428 BasicBlock::iterator InsertBefore) {
6429 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6430 idx_range);
6431 Value *To = PoisonValue::get(IndexedType);
6432 SmallVector<unsigned, 10> Idxs(idx_range);
6433 unsigned IdxSkip = Idxs.size();
6434
6435 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6436}
6437
6438/// Given an aggregate and a sequence of indices, see if the scalar value
6439/// indexed is already around as a register, for example if it was inserted
6440/// directly into the aggregate.
6441///
6442/// If InsertBefore is not null, this function will duplicate (modified)
6443/// insertvalues when a part of a nested struct is extracted.
6444Value *
6446 std::optional<BasicBlock::iterator> InsertBefore) {
6447 // Nothing to index? Just return V then (this is useful at the end of our
6448 // recursion).
6449 if (idx_range.empty())
6450 return V;
6451 // We have indices, so V should have an indexable type.
6452 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6453 "Not looking at a struct or array?");
6454 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6455 "Invalid indices for type?");
6456
6457 if (Constant *C = dyn_cast<Constant>(V)) {
6458 C = C->getAggregateElement(idx_range[0]);
6459 if (!C) return nullptr;
6460 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6461 }
6462
6464 // Loop the indices for the insertvalue instruction in parallel with the
6465 // requested indices
6466 const unsigned *req_idx = idx_range.begin();
6467 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6468 i != e; ++i, ++req_idx) {
6469 if (req_idx == idx_range.end()) {
6470 // We can't handle this without inserting insertvalues
6471 if (!InsertBefore)
6472 return nullptr;
6473
6474 // The requested index identifies a part of a nested aggregate. Handle
6475 // this specially. For example,
6476 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6477 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6478 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6479 // This can be changed into
6480 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6481 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6482 // which allows the unused 0,0 element from the nested struct to be
6483 // removed.
6484 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6485 *InsertBefore);
6486 }
6487
6488 // This insert value inserts something else than what we are looking for.
6489 // See if the (aggregate) value inserted into has the value we are
6490 // looking for, then.
6491 if (*req_idx != *i)
6492 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6493 InsertBefore);
6494 }
6495 // If we end up here, the indices of the insertvalue match with those
6496 // requested (though possibly only partially). Now we recursively look at
6497 // the inserted value, passing any remaining indices.
6498 return FindInsertedValue(I->getInsertedValueOperand(),
6499 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6500 }
6501
6503 // If we're extracting a value from an aggregate that was extracted from
6504 // something else, we can extract from that something else directly instead.
6505 // However, we will need to chain I's indices with the requested indices.
6506
6507 // Calculate the number of indices required
6508 unsigned size = I->getNumIndices() + idx_range.size();
6509 // Allocate some space to put the new indices in
6511 Idxs.reserve(size);
6512 // Add indices from the extract value instruction
6513 Idxs.append(I->idx_begin(), I->idx_end());
6514
6515 // Add requested indices
6516 Idxs.append(idx_range.begin(), idx_range.end());
6517
6518 assert(Idxs.size() == size
6519 && "Number of indices added not correct?");
6520
6521 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6522 }
6523 // Otherwise, we don't know (such as, extracting from a function return value
6524 // or load instruction)
6525 return nullptr;
6526}
6527
6528// If V refers to an initialized global constant, set Slice either to
6529// its initializer if the size of its elements equals ElementSize, or,
6530// for ElementSize == 8, to its representation as an array of unsiged
6531// char. Return true on success.
6532// Offset is in the unit "nr of ElementSize sized elements".
6535 unsigned ElementSize, uint64_t Offset) {
6536 assert(V && "V should not be null.");
6537 assert((ElementSize % 8) == 0 &&
6538 "ElementSize expected to be a multiple of the size of a byte.");
6539 unsigned ElementSizeInBytes = ElementSize / 8;
6540
6541 // Drill down into the pointer expression V, ignoring any intervening
6542 // casts, and determine the identity of the object it references along
6543 // with the cumulative byte offset into it.
6544 const GlobalVariable *GV =
6546 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6547 // Fail if V is not based on constant global object.
6548 return false;
6549
6550 const DataLayout &DL = GV->getDataLayout();
6551 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6552
6553 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6554 /*AllowNonInbounds*/ true))
6555 // Fail if a constant offset could not be determined.
6556 return false;
6557
6558 uint64_t StartIdx = Off.getLimitedValue();
6559 if (StartIdx == UINT64_MAX)
6560 // Fail if the constant offset is excessive.
6561 return false;
6562
6563 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6564 // elements. Simply bail out if that isn't possible.
6565 if ((StartIdx % ElementSizeInBytes) != 0)
6566 return false;
6567
6568 Offset += StartIdx / ElementSizeInBytes;
6569 ConstantDataArray *Array = nullptr;
6570 ArrayType *ArrayTy = nullptr;
6571
6572 if (GV->getInitializer()->isNullValue()) {
6573 Type *GVTy = GV->getValueType();
6574 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6575 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6576
6577 Slice.Array = nullptr;
6578 Slice.Offset = 0;
6579 // Return an empty Slice for undersized constants to let callers
6580 // transform even undefined library calls into simpler, well-defined
6581 // expressions. This is preferable to making the calls although it
6582 // prevents sanitizers from detecting such calls.
6583 Slice.Length = Length < Offset ? 0 : Length - Offset;
6584 return true;
6585 }
6586
6587 auto *Init = const_cast<Constant *>(GV->getInitializer());
6588 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6589 Type *InitElTy = ArrayInit->getElementType();
6590 if (InitElTy->isIntegerTy(ElementSize)) {
6591 // If Init is an initializer for an array of the expected type
6592 // and size, use it as is.
6593 Array = ArrayInit;
6594 ArrayTy = ArrayInit->getType();
6595 }
6596 }
6597
6598 if (!Array) {
6599 if (ElementSize != 8)
6600 // TODO: Handle conversions to larger integral types.
6601 return false;
6602
6603 // Otherwise extract the portion of the initializer starting
6604 // at Offset as an array of bytes, and reset Offset.
6606 if (!Init)
6607 return false;
6608
6609 Offset = 0;
6611 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6612 }
6613
6614 uint64_t NumElts = ArrayTy->getArrayNumElements();
6615 if (Offset > NumElts)
6616 return false;
6617
6618 Slice.Array = Array;
6619 Slice.Offset = Offset;
6620 Slice.Length = NumElts - Offset;
6621 return true;
6622}
6623
6624/// Extract bytes from the initializer of the constant array V, which need
6625/// not be a nul-terminated string. On success, store the bytes in Str and
6626/// return true. When TrimAtNul is set, Str will contain only the bytes up
6627/// to but not including the first nul. Return false on failure.
6629 bool TrimAtNul) {
6631 if (!getConstantDataArrayInfo(V, Slice, 8))
6632 return false;
6633
6634 if (Slice.Array == nullptr) {
6635 if (TrimAtNul) {
6636 // Return a nul-terminated string even for an empty Slice. This is
6637 // safe because all existing SimplifyLibcalls callers require string
6638 // arguments and the behavior of the functions they fold is undefined
6639 // otherwise. Folding the calls this way is preferable to making
6640 // the undefined library calls, even though it prevents sanitizers
6641 // from reporting such calls.
6642 Str = StringRef();
6643 return true;
6644 }
6645 if (Slice.Length == 1) {
6646 Str = StringRef("", 1);
6647 return true;
6648 }
6649 // We cannot instantiate a StringRef as we do not have an appropriate string
6650 // of 0s at hand.
6651 return false;
6652 }
6653
6654 // Start out with the entire array in the StringRef.
6655 Str = Slice.Array->getAsString();
6656 // Skip over 'offset' bytes.
6657 Str = Str.substr(Slice.Offset);
6658
6659 if (TrimAtNul) {
6660 // Trim off the \0 and anything after it. If the array is not nul
6661 // terminated, we just return the whole end of string. The client may know
6662 // some other way that the string is length-bound.
6663 Str = Str.substr(0, Str.find('\0'));
6664 }
6665 return true;
6666}
6667
6668// These next two are very similar to the above, but also look through PHI
6669// nodes.
6670// TODO: See if we can integrate these two together.
6671
6672/// If we can compute the length of the string pointed to by
6673/// the specified pointer, return 'len+1'. If we can't, return 0.
6676 unsigned CharSize) {
6677 // Look through noop bitcast instructions.
6678 V = V->stripPointerCasts();
6679
6680 // If this is a PHI node, there are two cases: either we have already seen it
6681 // or we haven't.
6682 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6683 if (!PHIs.insert(PN).second)
6684 return ~0ULL; // already in the set.
6685
6686 // If it was new, see if all the input strings are the same length.
6687 uint64_t LenSoFar = ~0ULL;
6688 for (Value *IncValue : PN->incoming_values()) {
6689 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6690 if (Len == 0) return 0; // Unknown length -> unknown.
6691
6692 if (Len == ~0ULL) continue;
6693
6694 if (Len != LenSoFar && LenSoFar != ~0ULL)
6695 return 0; // Disagree -> unknown.
6696 LenSoFar = Len;
6697 }
6698
6699 // Success, all agree.
6700 return LenSoFar;
6701 }
6702
6703 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6704 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6705 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6706 if (Len1 == 0) return 0;
6707 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6708 if (Len2 == 0) return 0;
6709 if (Len1 == ~0ULL) return Len2;
6710 if (Len2 == ~0ULL) return Len1;
6711 if (Len1 != Len2) return 0;
6712 return Len1;
6713 }
6714
6715 // Otherwise, see if we can read the string.
6717 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6718 return 0;
6719
6720 if (Slice.Array == nullptr)
6721 // Zeroinitializer (including an empty one).
6722 return 1;
6723
6724 // Search for the first nul character. Return a conservative result even
6725 // when there is no nul. This is safe since otherwise the string function
6726 // being folded such as strlen is undefined, and can be preferable to
6727 // making the undefined library call.
6728 unsigned NullIndex = 0;
6729 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6730 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6731 break;
6732 }
6733
6734 return NullIndex + 1;
6735}
6736
6737/// If we can compute the length of the string pointed to by
6738/// the specified pointer, return 'len+1'. If we can't, return 0.
6739uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6740 if (!V->getType()->isPointerTy())
6741 return 0;
6742
6744 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6745 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6746 // an empty string as a length.
6747 return Len == ~0ULL ? 1 : Len;
6748}
6749
6750const Value *
6752 bool MustPreserveNullness) {
6753 assert(Call &&
6754 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6755 if (const Value *RV = Call->getReturnedArgOperand())
6756 return RV;
6757 // This can be used only as a aliasing property.
6759 Call, MustPreserveNullness))
6760 return Call->getArgOperand(0);
6761 return nullptr;
6762}
6763
6765 const CallBase *Call, bool MustPreserveNullness) {
6766 switch (Call->getIntrinsicID()) {
6767 case Intrinsic::launder_invariant_group:
6768 case Intrinsic::strip_invariant_group:
6769 case Intrinsic::aarch64_irg:
6770 case Intrinsic::aarch64_tagp:
6771 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6772 // input pointer (and thus preserve null-ness for the purposes of escape
6773 // analysis, which is where the MustPreserveNullness flag comes in to play).
6774 // However, it will not necessarily map ptr addrspace(N) null to ptr
6775 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6776 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6777 // list, no one should be relying on such a strict interpretation of
6778 // MustPreserveNullness (and, at time of writing, they are not), but we
6779 // document this fact out of an abundance of caution.
6780 case Intrinsic::amdgcn_make_buffer_rsrc:
6781 return true;
6782 case Intrinsic::ptrmask:
6783 return !MustPreserveNullness;
6784 case Intrinsic::threadlocal_address:
6785 // The underlying variable changes with thread ID. The Thread ID may change
6786 // at coroutine suspend points.
6787 return !Call->getParent()->getParent()->isPresplitCoroutine();
6788 default:
6789 return false;
6790 }
6791}
6792
6793/// \p PN defines a loop-variant pointer to an object. Check if the
6794/// previous iteration of the loop was referring to the same object as \p PN.
6796 const LoopInfo *LI) {
6797 // Find the loop-defined value.
6798 Loop *L = LI->getLoopFor(PN->getParent());
6799 if (PN->getNumIncomingValues() != 2)
6800 return true;
6801
6802 // Find the value from previous iteration.
6803 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6804 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6805 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6806 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6807 return true;
6808
6809 // If a new pointer is loaded in the loop, the pointer references a different
6810 // object in every iteration. E.g.:
6811 // for (i)
6812 // int *p = a[i];
6813 // ...
6814 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6815 if (!L->isLoopInvariant(Load->getPointerOperand()))
6816 return false;
6817 return true;
6818}
6819
6820const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6821 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6822 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6823 const Value *PtrOp = GEP->getPointerOperand();
6824 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6825 return V;
6826 V = PtrOp;
6827 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6828 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6829 Value *NewV = cast<Operator>(V)->getOperand(0);
6830 if (!NewV->getType()->isPointerTy())
6831 return V;
6832 V = NewV;
6833 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6834 if (GA->isInterposable())
6835 return V;
6836 V = GA->getAliasee();
6837 } else {
6838 if (auto *PHI = dyn_cast<PHINode>(V)) {
6839 // Look through single-arg phi nodes created by LCSSA.
6840 if (PHI->getNumIncomingValues() == 1) {
6841 V = PHI->getIncomingValue(0);
6842 continue;
6843 }
6844 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6845 // CaptureTracking can know about special capturing properties of some
6846 // intrinsics like launder.invariant.group, that can't be expressed with
6847 // the attributes, but have properties like returning aliasing pointer.
6848 // Because some analysis may assume that nocaptured pointer is not
6849 // returned from some special intrinsic (because function would have to
6850 // be marked with returns attribute), it is crucial to use this function
6851 // because it should be in sync with CaptureTracking. Not using it may
6852 // cause weird miscompilations where 2 aliasing pointers are assumed to
6853 // noalias.
6854 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6855 V = RP;
6856 continue;
6857 }
6858 }
6859
6860 return V;
6861 }
6862 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6863 }
6864 return V;
6865}
6866
6869 const LoopInfo *LI, unsigned MaxLookup) {
6872 Worklist.push_back(V);
6873 do {
6874 const Value *P = Worklist.pop_back_val();
6875 P = getUnderlyingObject(P, MaxLookup);
6876
6877 if (!Visited.insert(P).second)
6878 continue;
6879
6880 if (auto *SI = dyn_cast<SelectInst>(P)) {
6881 Worklist.push_back(SI->getTrueValue());
6882 Worklist.push_back(SI->getFalseValue());
6883 continue;
6884 }
6885
6886 if (auto *PN = dyn_cast<PHINode>(P)) {
6887 // If this PHI changes the underlying object in every iteration of the
6888 // loop, don't look through it. Consider:
6889 // int **A;
6890 // for (i) {
6891 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6892 // Curr = A[i];
6893 // *Prev, *Curr;
6894 //
6895 // Prev is tracking Curr one iteration behind so they refer to different
6896 // underlying objects.
6897 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6899 append_range(Worklist, PN->incoming_values());
6900 else
6901 Objects.push_back(P);
6902 continue;
6903 }
6904
6905 Objects.push_back(P);
6906 } while (!Worklist.empty());
6907}
6908
6910 const unsigned MaxVisited = 8;
6911
6914 Worklist.push_back(V);
6915 const Value *Object = nullptr;
6916 // Used as fallback if we can't find a common underlying object through
6917 // recursion.
6918 bool First = true;
6919 const Value *FirstObject = getUnderlyingObject(V);
6920 do {
6921 const Value *P = Worklist.pop_back_val();
6922 P = First ? FirstObject : getUnderlyingObject(P);
6923 First = false;
6924
6925 if (!Visited.insert(P).second)
6926 continue;
6927
6928 if (Visited.size() == MaxVisited)
6929 return FirstObject;
6930
6931 if (auto *SI = dyn_cast<SelectInst>(P)) {
6932 Worklist.push_back(SI->getTrueValue());
6933 Worklist.push_back(SI->getFalseValue());
6934 continue;
6935 }
6936
6937 if (auto *PN = dyn_cast<PHINode>(P)) {
6938 append_range(Worklist, PN->incoming_values());
6939 continue;
6940 }
6941
6942 if (!Object)
6943 Object = P;
6944 else if (Object != P)
6945 return FirstObject;
6946 } while (!Worklist.empty());
6947
6948 return Object ? Object : FirstObject;
6949}
6950
6951/// This is the function that does the work of looking through basic
6952/// ptrtoint+arithmetic+inttoptr sequences.
6953static const Value *getUnderlyingObjectFromInt(const Value *V) {
6954 do {
6955 if (const Operator *U = dyn_cast<Operator>(V)) {
6956 // If we find a ptrtoint, we can transfer control back to the
6957 // regular getUnderlyingObjectFromInt.
6958 if (U->getOpcode() == Instruction::PtrToInt)
6959 return U->getOperand(0);
6960 // If we find an add of a constant, a multiplied value, or a phi, it's
6961 // likely that the other operand will lead us to the base
6962 // object. We don't have to worry about the case where the
6963 // object address is somehow being computed by the multiply,
6964 // because our callers only care when the result is an
6965 // identifiable object.
6966 if (U->getOpcode() != Instruction::Add ||
6967 (!isa<ConstantInt>(U->getOperand(1)) &&
6968 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6969 !isa<PHINode>(U->getOperand(1))))
6970 return V;
6971 V = U->getOperand(0);
6972 } else {
6973 return V;
6974 }
6975 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6976 } while (true);
6977}
6978
6979/// This is a wrapper around getUnderlyingObjects and adds support for basic
6980/// ptrtoint+arithmetic+inttoptr sequences.
6981/// It returns false if unidentified object is found in getUnderlyingObjects.
6983 SmallVectorImpl<Value *> &Objects) {
6985 SmallVector<const Value *, 4> Working(1, V);
6986 do {
6987 V = Working.pop_back_val();
6988
6990 getUnderlyingObjects(V, Objs);
6991
6992 for (const Value *V : Objs) {
6993 if (!Visited.insert(V).second)
6994 continue;
6995 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6996 const Value *O =
6997 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6998 if (O->getType()->isPointerTy()) {
6999 Working.push_back(O);
7000 continue;
7001 }
7002 }
7003 // If getUnderlyingObjects fails to find an identifiable object,
7004 // getUnderlyingObjectsForCodeGen also fails for safety.
7005 if (!isIdentifiedObject(V)) {
7006 Objects.clear();
7007 return false;
7008 }
7009 Objects.push_back(const_cast<Value *>(V));
7010 }
7011 } while (!Working.empty());
7012 return true;
7013}
7014
7016 AllocaInst *Result = nullptr;
7018 SmallVector<Value *, 4> Worklist;
7019
7020 auto AddWork = [&](Value *V) {
7021 if (Visited.insert(V).second)
7022 Worklist.push_back(V);
7023 };
7024
7025 AddWork(V);
7026 do {
7027 V = Worklist.pop_back_val();
7028 assert(Visited.count(V));
7029
7030 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7031 if (Result && Result != AI)
7032 return nullptr;
7033 Result = AI;
7034 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7035 AddWork(CI->getOperand(0));
7036 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7037 for (Value *IncValue : PN->incoming_values())
7038 AddWork(IncValue);
7039 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7040 AddWork(SI->getTrueValue());
7041 AddWork(SI->getFalseValue());
7043 if (OffsetZero && !GEP->hasAllZeroIndices())
7044 return nullptr;
7045 AddWork(GEP->getPointerOperand());
7046 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7047 Value *Returned = CB->getReturnedArgOperand();
7048 if (Returned)
7049 AddWork(Returned);
7050 else
7051 return nullptr;
7052 } else {
7053 return nullptr;
7054 }
7055 } while (!Worklist.empty());
7056
7057 return Result;
7058}
7059
7061 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7062 for (const User *U : V->users()) {
7064 if (!II)
7065 return false;
7066
7067 if (AllowLifetime && II->isLifetimeStartOrEnd())
7068 continue;
7069
7070 if (AllowDroppable && II->isDroppable())
7071 continue;
7072
7073 return false;
7074 }
7075 return true;
7076}
7077
7080 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7081}
7084 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7085}
7086
7088 if (auto *II = dyn_cast<IntrinsicInst>(I))
7089 return isTriviallyVectorizable(II->getIntrinsicID());
7090 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7091 return (!Shuffle || Shuffle->isSelect()) &&
7093}
7094
7096 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
7097 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
7098 bool IgnoreUBImplyingAttrs) {
7099 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7100 AC, DT, TLI, UseVariableInfo,
7101 IgnoreUBImplyingAttrs);
7102}
7103
7105 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7106 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7107 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
7108#ifndef NDEBUG
7109 if (Inst->getOpcode() != Opcode) {
7110 // Check that the operands are actually compatible with the Opcode override.
7111 auto hasEqualReturnAndLeadingOperandTypes =
7112 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7113 if (Inst->getNumOperands() < NumLeadingOperands)
7114 return false;
7115 const Type *ExpectedType = Inst->getType();
7116 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7117 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7118 return false;
7119 return true;
7120 };
7122 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7123 assert(!Instruction::isUnaryOp(Opcode) ||
7124 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7125 }
7126#endif
7127
7128 switch (Opcode) {
7129 default:
7130 return true;
7131 case Instruction::UDiv:
7132 case Instruction::URem: {
7133 // x / y is undefined if y == 0.
7134 const APInt *V;
7135 if (match(Inst->getOperand(1), m_APInt(V)))
7136 return *V != 0;
7137 return false;
7138 }
7139 case Instruction::SDiv:
7140 case Instruction::SRem: {
7141 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7142 const APInt *Numerator, *Denominator;
7143 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7144 return false;
7145 // We cannot hoist this division if the denominator is 0.
7146 if (*Denominator == 0)
7147 return false;
7148 // It's safe to hoist if the denominator is not 0 or -1.
7149 if (!Denominator->isAllOnes())
7150 return true;
7151 // At this point we know that the denominator is -1. It is safe to hoist as
7152 // long we know that the numerator is not INT_MIN.
7153 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7154 return !Numerator->isMinSignedValue();
7155 // The numerator *might* be MinSignedValue.
7156 return false;
7157 }
7158 case Instruction::Load: {
7159 if (!UseVariableInfo)
7160 return false;
7161
7162 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7163 if (!LI)
7164 return false;
7165 if (mustSuppressSpeculation(*LI))
7166 return false;
7167 const DataLayout &DL = LI->getDataLayout();
7169 LI->getType(), LI->getAlign(), DL,
7170 CtxI, AC, DT, TLI);
7171 }
7172 case Instruction::Call: {
7173 auto *CI = dyn_cast<const CallInst>(Inst);
7174 if (!CI)
7175 return false;
7176 const Function *Callee = CI->getCalledFunction();
7177
7178 // The called function could have undefined behavior or side-effects, even
7179 // if marked readnone nounwind.
7180 if (!Callee || !Callee->isSpeculatable())
7181 return false;
7182 // Since the operands may be changed after hoisting, undefined behavior may
7183 // be triggered by some UB-implying attributes.
7184 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7185 }
7186 case Instruction::VAArg:
7187 case Instruction::Alloca:
7188 case Instruction::Invoke:
7189 case Instruction::CallBr:
7190 case Instruction::PHI:
7191 case Instruction::Store:
7192 case Instruction::Ret:
7193 case Instruction::Br:
7194 case Instruction::IndirectBr:
7195 case Instruction::Switch:
7196 case Instruction::Unreachable:
7197 case Instruction::Fence:
7198 case Instruction::AtomicRMW:
7199 case Instruction::AtomicCmpXchg:
7200 case Instruction::LandingPad:
7201 case Instruction::Resume:
7202 case Instruction::CatchSwitch:
7203 case Instruction::CatchPad:
7204 case Instruction::CatchRet:
7205 case Instruction::CleanupPad:
7206 case Instruction::CleanupRet:
7207 return false; // Misc instructions which have effects
7208 }
7209}
7210
7212 if (I.mayReadOrWriteMemory())
7213 // Memory dependency possible
7214 return true;
7216 // Can't move above a maythrow call or infinite loop. Or if an
7217 // inalloca alloca, above a stacksave call.
7218 return true;
7220 // 1) Can't reorder two inf-loop calls, even if readonly
7221 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7222 // safe to speculative execute. (Inverse of above)
7223 return true;
7224 return false;
7225}
7226
7227/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7241
7242/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7245 bool ForSigned,
7246 const SimplifyQuery &SQ) {
7247 ConstantRange CR1 =
7248 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7249 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7252 return CR1.intersectWith(CR2, RangeType);
7253}
7254
7256 const Value *RHS,
7257 const SimplifyQuery &SQ,
7258 bool IsNSW) {
7259 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7260 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7261
7262 // mul nsw of two non-negative numbers is also nuw.
7263 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
7265
7266 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
7267 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
7268 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7269}
7270
7272 const Value *RHS,
7273 const SimplifyQuery &SQ) {
7274 // Multiplying n * m significant bits yields a result of n + m significant
7275 // bits. If the total number of significant bits does not exceed the
7276 // result bit width (minus 1), there is no overflow.
7277 // This means if we have enough leading sign bits in the operands
7278 // we can guarantee that the result does not overflow.
7279 // Ref: "Hacker's Delight" by Henry Warren
7280 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7281
7282 // Note that underestimating the number of sign bits gives a more
7283 // conservative answer.
7284 unsigned SignBits =
7285 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7286
7287 // First handle the easy case: if we have enough sign bits there's
7288 // definitely no overflow.
7289 if (SignBits > BitWidth + 1)
7291
7292 // There are two ambiguous cases where there can be no overflow:
7293 // SignBits == BitWidth + 1 and
7294 // SignBits == BitWidth
7295 // The second case is difficult to check, therefore we only handle the
7296 // first case.
7297 if (SignBits == BitWidth + 1) {
7298 // It overflows only when both arguments are negative and the true
7299 // product is exactly the minimum negative number.
7300 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7301 // For simplicity we just check if at least one side is not negative.
7302 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7303 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7304 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7306 }
7308}
7309
7312 const WithCache<const Value *> &RHS,
7313 const SimplifyQuery &SQ) {
7314 ConstantRange LHSRange =
7315 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7316 ConstantRange RHSRange =
7317 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7318 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7319}
7320
7321static OverflowResult
7324 const AddOperator *Add, const SimplifyQuery &SQ) {
7325 if (Add && Add->hasNoSignedWrap()) {
7327 }
7328
7329 // If LHS and RHS each have at least two sign bits, the addition will look
7330 // like
7331 //
7332 // XX..... +
7333 // YY.....
7334 //
7335 // If the carry into the most significant position is 0, X and Y can't both
7336 // be 1 and therefore the carry out of the addition is also 0.
7337 //
7338 // If the carry into the most significant position is 1, X and Y can't both
7339 // be 0 and therefore the carry out of the addition is also 1.
7340 //
7341 // Since the carry into the most significant position is always equal to
7342 // the carry out of the addition, there is no signed overflow.
7343 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7345
7346 ConstantRange LHSRange =
7347 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7348 ConstantRange RHSRange =
7349 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7350 OverflowResult OR =
7351 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7353 return OR;
7354
7355 // The remaining code needs Add to be available. Early returns if not so.
7356 if (!Add)
7358
7359 // If the sign of Add is the same as at least one of the operands, this add
7360 // CANNOT overflow. If this can be determined from the known bits of the
7361 // operands the above signedAddMayOverflow() check will have already done so.
7362 // The only other way to improve on the known bits is from an assumption, so
7363 // call computeKnownBitsFromContext() directly.
7364 bool LHSOrRHSKnownNonNegative =
7365 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7366 bool LHSOrRHSKnownNegative =
7367 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7368 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7369 KnownBits AddKnown(LHSRange.getBitWidth());
7370 computeKnownBitsFromContext(Add, AddKnown, SQ);
7371 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7372 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7374 }
7375
7377}
7378
7380 const Value *RHS,
7381 const SimplifyQuery &SQ) {
7382 // X - (X % ?)
7383 // The remainder of a value can't have greater magnitude than itself,
7384 // so the subtraction can't overflow.
7385
7386 // X - (X -nuw ?)
7387 // In the minimal case, this would simplify to "?", so there's no subtract
7388 // at all. But if this analysis is used to peek through casts, for example,
7389 // then determining no-overflow may allow other transforms.
7390
7391 // TODO: There are other patterns like this.
7392 // See simplifyICmpWithBinOpOnLHS() for candidates.
7393 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7394 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7395 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7397
7398 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7399 SQ.DL)) {
7400 if (*C)
7403 }
7404
7405 ConstantRange LHSRange =
7406 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7407 ConstantRange RHSRange =
7408 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7409 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7410}
7411
7413 const Value *RHS,
7414 const SimplifyQuery &SQ) {
7415 // X - (X % ?)
7416 // The remainder of a value can't have greater magnitude than itself,
7417 // so the subtraction can't overflow.
7418
7419 // X - (X -nsw ?)
7420 // In the minimal case, this would simplify to "?", so there's no subtract
7421 // at all. But if this analysis is used to peek through casts, for example,
7422 // then determining no-overflow may allow other transforms.
7423 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7424 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7425 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7427
7428 // If LHS and RHS each have at least two sign bits, the subtraction
7429 // cannot overflow.
7430 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7432
7433 ConstantRange LHSRange =
7434 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7435 ConstantRange RHSRange =
7436 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7437 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7438}
7439
7441 const DominatorTree &DT) {
7442 SmallVector<const BranchInst *, 2> GuardingBranches;
7444
7445 for (const User *U : WO->users()) {
7446 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7447 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7448
7449 if (EVI->getIndices()[0] == 0)
7450 Results.push_back(EVI);
7451 else {
7452 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7453
7454 for (const auto *U : EVI->users())
7455 if (const auto *B = dyn_cast<BranchInst>(U)) {
7456 assert(B->isConditional() && "How else is it using an i1?");
7457 GuardingBranches.push_back(B);
7458 }
7459 }
7460 } else {
7461 // We are using the aggregate directly in a way we don't want to analyze
7462 // here (storing it to a global, say).
7463 return false;
7464 }
7465 }
7466
7467 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7468 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7469 if (!NoWrapEdge.isSingleEdge())
7470 return false;
7471
7472 // Check if all users of the add are provably no-wrap.
7473 for (const auto *Result : Results) {
7474 // If the extractvalue itself is not executed on overflow, the we don't
7475 // need to check each use separately, since domination is transitive.
7476 if (DT.dominates(NoWrapEdge, Result->getParent()))
7477 continue;
7478
7479 for (const auto &RU : Result->uses())
7480 if (!DT.dominates(NoWrapEdge, RU))
7481 return false;
7482 }
7483
7484 return true;
7485 };
7486
7487 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7488}
7489
7490/// Shifts return poison if shiftwidth is larger than the bitwidth.
7491static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7492 auto *C = dyn_cast<Constant>(ShiftAmount);
7493 if (!C)
7494 return false;
7495
7496 // Shifts return poison if shiftwidth is larger than the bitwidth.
7498 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7499 unsigned NumElts = FVTy->getNumElements();
7500 for (unsigned i = 0; i < NumElts; ++i)
7501 ShiftAmounts.push_back(C->getAggregateElement(i));
7502 } else if (isa<ScalableVectorType>(C->getType()))
7503 return false; // Can't tell, just return false to be safe
7504 else
7505 ShiftAmounts.push_back(C);
7506
7507 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7508 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7509 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7510 });
7511
7512 return Safe;
7513}
7514
7520
7522 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7523}
7524
7526 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7527}
7528
7530 bool ConsiderFlagsAndMetadata) {
7531
7532 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7533 Op->hasPoisonGeneratingAnnotations())
7534 return true;
7535
7536 unsigned Opcode = Op->getOpcode();
7537
7538 // Check whether opcode is a poison/undef-generating operation
7539 switch (Opcode) {
7540 case Instruction::Shl:
7541 case Instruction::AShr:
7542 case Instruction::LShr:
7543 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7544 case Instruction::FPToSI:
7545 case Instruction::FPToUI:
7546 // fptosi/ui yields poison if the resulting value does not fit in the
7547 // destination type.
7548 return true;
7549 case Instruction::Call:
7550 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7551 switch (II->getIntrinsicID()) {
7552 // TODO: Add more intrinsics.
7553 case Intrinsic::ctlz:
7554 case Intrinsic::cttz:
7555 case Intrinsic::abs:
7556 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7557 return false;
7558 break;
7559 case Intrinsic::sshl_sat:
7560 case Intrinsic::ushl_sat:
7561 if (!includesPoison(Kind) ||
7562 shiftAmountKnownInRange(II->getArgOperand(1)))
7563 return false;
7564 break;
7565 }
7566 }
7567 [[fallthrough]];
7568 case Instruction::CallBr:
7569 case Instruction::Invoke: {
7570 const auto *CB = cast<CallBase>(Op);
7571 return !CB->hasRetAttr(Attribute::NoUndef) &&
7572 !CB->hasFnAttr(Attribute::NoCreateUndefOrPoison);
7573 }
7574 case Instruction::InsertElement:
7575 case Instruction::ExtractElement: {
7576 // If index exceeds the length of the vector, it returns poison
7577 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7578 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7579 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7580 if (includesPoison(Kind))
7581 return !Idx ||
7582 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7583 return false;
7584 }
7585 case Instruction::ShuffleVector: {
7587 ? cast<ConstantExpr>(Op)->getShuffleMask()
7588 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7589 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7590 }
7591 case Instruction::FNeg:
7592 case Instruction::PHI:
7593 case Instruction::Select:
7594 case Instruction::ExtractValue:
7595 case Instruction::InsertValue:
7596 case Instruction::Freeze:
7597 case Instruction::ICmp:
7598 case Instruction::FCmp:
7599 case Instruction::GetElementPtr:
7600 return false;
7601 case Instruction::AddrSpaceCast:
7602 return true;
7603 default: {
7604 const auto *CE = dyn_cast<ConstantExpr>(Op);
7605 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7606 return false;
7607 else if (Instruction::isBinaryOp(Opcode))
7608 return false;
7609 // Be conservative and return true.
7610 return true;
7611 }
7612 }
7613}
7614
7616 bool ConsiderFlagsAndMetadata) {
7617 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7618 ConsiderFlagsAndMetadata);
7619}
7620
7621bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7622 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7623 ConsiderFlagsAndMetadata);
7624}
7625
7626static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7627 unsigned Depth) {
7628 if (ValAssumedPoison == V)
7629 return true;
7630
7631 const unsigned MaxDepth = 2;
7632 if (Depth >= MaxDepth)
7633 return false;
7634
7635 if (const auto *I = dyn_cast<Instruction>(V)) {
7636 if (any_of(I->operands(), [=](const Use &Op) {
7637 return propagatesPoison(Op) &&
7638 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7639 }))
7640 return true;
7641
7642 // V = extractvalue V0, idx
7643 // V2 = extractvalue V0, idx2
7644 // V0's elements are all poison or not. (e.g., add_with_overflow)
7645 const WithOverflowInst *II;
7647 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7648 llvm::is_contained(II->args(), ValAssumedPoison)))
7649 return true;
7650 }
7651 return false;
7652}
7653
7654static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7655 unsigned Depth) {
7656 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7657 return true;
7658
7659 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7660 return true;
7661
7662 const unsigned MaxDepth = 2;
7663 if (Depth >= MaxDepth)
7664 return false;
7665
7666 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7667 if (I && !canCreatePoison(cast<Operator>(I))) {
7668 return all_of(I->operands(), [=](const Value *Op) {
7669 return impliesPoison(Op, V, Depth + 1);
7670 });
7671 }
7672 return false;
7673}
7674
7675bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7676 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7677}
7678
7679static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7680
7682 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7683 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7685 return false;
7686
7687 if (isa<MetadataAsValue>(V))
7688 return false;
7689
7690 if (const auto *A = dyn_cast<Argument>(V)) {
7691 if (A->hasAttribute(Attribute::NoUndef) ||
7692 A->hasAttribute(Attribute::Dereferenceable) ||
7693 A->hasAttribute(Attribute::DereferenceableOrNull))
7694 return true;
7695 }
7696
7697 if (auto *C = dyn_cast<Constant>(V)) {
7698 if (isa<PoisonValue>(C))
7699 return !includesPoison(Kind);
7700
7701 if (isa<UndefValue>(C))
7702 return !includesUndef(Kind);
7703
7706 return true;
7707
7708 if (C->getType()->isVectorTy()) {
7709 if (isa<ConstantExpr>(C)) {
7710 // Scalable vectors can use a ConstantExpr to build a splat.
7711 if (Constant *SplatC = C->getSplatValue())
7712 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7713 return true;
7714 } else {
7715 if (includesUndef(Kind) && C->containsUndefElement())
7716 return false;
7717 if (includesPoison(Kind) && C->containsPoisonElement())
7718 return false;
7719 return !C->containsConstantExpression();
7720 }
7721 }
7722 }
7723
7724 // Strip cast operations from a pointer value.
7725 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7726 // inbounds with zero offset. To guarantee that the result isn't poison, the
7727 // stripped pointer is checked as it has to be pointing into an allocated
7728 // object or be null `null` to ensure `inbounds` getelement pointers with a
7729 // zero offset could not produce poison.
7730 // It can strip off addrspacecast that do not change bit representation as
7731 // well. We believe that such addrspacecast is equivalent to no-op.
7732 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7733 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7734 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7735 return true;
7736
7737 auto OpCheck = [&](const Value *V) {
7738 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7739 };
7740
7741 if (auto *Opr = dyn_cast<Operator>(V)) {
7742 // If the value is a freeze instruction, then it can never
7743 // be undef or poison.
7744 if (isa<FreezeInst>(V))
7745 return true;
7746
7747 if (const auto *CB = dyn_cast<CallBase>(V)) {
7748 if (CB->hasRetAttr(Attribute::NoUndef) ||
7749 CB->hasRetAttr(Attribute::Dereferenceable) ||
7750 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7751 return true;
7752 }
7753
7754 if (!::canCreateUndefOrPoison(Opr, Kind,
7755 /*ConsiderFlagsAndMetadata=*/true)) {
7756 if (const auto *PN = dyn_cast<PHINode>(V)) {
7757 unsigned Num = PN->getNumIncomingValues();
7758 bool IsWellDefined = true;
7759 for (unsigned i = 0; i < Num; ++i) {
7760 if (PN == PN->getIncomingValue(i))
7761 continue;
7762 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7763 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7764 DT, Depth + 1, Kind)) {
7765 IsWellDefined = false;
7766 break;
7767 }
7768 }
7769 if (IsWellDefined)
7770 return true;
7771 } else if (auto *Splat = isa<ShuffleVectorInst>(Opr) ? getSplatValue(Opr)
7772 : nullptr) {
7773 // For splats we only need to check the value being splatted.
7774 if (OpCheck(Splat))
7775 return true;
7776 } else if (all_of(Opr->operands(), OpCheck))
7777 return true;
7778 }
7779 }
7780
7781 if (auto *I = dyn_cast<LoadInst>(V))
7782 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7783 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7784 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7785 return true;
7786
7788 return true;
7789
7790 // CxtI may be null or a cloned instruction.
7791 if (!CtxI || !CtxI->getParent() || !DT)
7792 return false;
7793
7794 auto *DNode = DT->getNode(CtxI->getParent());
7795 if (!DNode)
7796 // Unreachable block
7797 return false;
7798
7799 // If V is used as a branch condition before reaching CtxI, V cannot be
7800 // undef or poison.
7801 // br V, BB1, BB2
7802 // BB1:
7803 // CtxI ; V cannot be undef or poison here
7804 auto *Dominator = DNode->getIDom();
7805 // This check is purely for compile time reasons: we can skip the IDom walk
7806 // if what we are checking for includes undef and the value is not an integer.
7807 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7808 while (Dominator) {
7809 auto *TI = Dominator->getBlock()->getTerminator();
7810
7811 Value *Cond = nullptr;
7812 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7813 if (BI->isConditional())
7814 Cond = BI->getCondition();
7815 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7816 Cond = SI->getCondition();
7817 }
7818
7819 if (Cond) {
7820 if (Cond == V)
7821 return true;
7822 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7823 // For poison, we can analyze further
7824 auto *Opr = cast<Operator>(Cond);
7825 if (any_of(Opr->operands(), [V](const Use &U) {
7826 return V == U && propagatesPoison(U);
7827 }))
7828 return true;
7829 }
7830 }
7831
7832 Dominator = Dominator->getIDom();
7833 }
7834
7835 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7836 return true;
7837
7838 return false;
7839}
7840
7842 const Instruction *CtxI,
7843 const DominatorTree *DT,
7844 unsigned Depth) {
7845 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7847}
7848
7850 const Instruction *CtxI,
7851 const DominatorTree *DT, unsigned Depth) {
7852 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7854}
7855
7857 const Instruction *CtxI,
7858 const DominatorTree *DT, unsigned Depth) {
7859 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7861}
7862
7863/// Return true if undefined behavior would provably be executed on the path to
7864/// OnPathTo if Root produced a posion result. Note that this doesn't say
7865/// anything about whether OnPathTo is actually executed or whether Root is
7866/// actually poison. This can be used to assess whether a new use of Root can
7867/// be added at a location which is control equivalent with OnPathTo (such as
7868/// immediately before it) without introducing UB which didn't previously
7869/// exist. Note that a false result conveys no information.
7871 Instruction *OnPathTo,
7872 DominatorTree *DT) {
7873 // Basic approach is to assume Root is poison, propagate poison forward
7874 // through all users we can easily track, and then check whether any of those
7875 // users are provable UB and must execute before out exiting block might
7876 // exit.
7877
7878 // The set of all recursive users we've visited (which are assumed to all be
7879 // poison because of said visit)
7882 Worklist.push_back(Root);
7883 while (!Worklist.empty()) {
7884 const Instruction *I = Worklist.pop_back_val();
7885
7886 // If we know this must trigger UB on a path leading our target.
7887 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7888 return true;
7889
7890 // If we can't analyze propagation through this instruction, just skip it
7891 // and transitive users. Safe as false is a conservative result.
7892 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7893 return KnownPoison.contains(U) && propagatesPoison(U);
7894 }))
7895 continue;
7896
7897 if (KnownPoison.insert(I).second)
7898 for (const User *User : I->users())
7899 Worklist.push_back(cast<Instruction>(User));
7900 }
7901
7902 // Might be non-UB, or might have a path we couldn't prove must execute on
7903 // way to exiting bb.
7904 return false;
7905}
7906
7908 const SimplifyQuery &SQ) {
7909 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7910 Add, SQ);
7911}
7912
7915 const WithCache<const Value *> &RHS,
7916 const SimplifyQuery &SQ) {
7917 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7918}
7919
7921 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7922 // of time because it's possible for another thread to interfere with it for an
7923 // arbitrary length of time, but programs aren't allowed to rely on that.
7924
7925 // If there is no successor, then execution can't transfer to it.
7926 if (isa<ReturnInst>(I))
7927 return false;
7929 return false;
7930
7931 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7932 // Instruction::willReturn.
7933 //
7934 // FIXME: Move this check into Instruction::willReturn.
7935 if (isa<CatchPadInst>(I)) {
7936 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7937 default:
7938 // A catchpad may invoke exception object constructors and such, which
7939 // in some languages can be arbitrary code, so be conservative by default.
7940 return false;
7942 // For CoreCLR, it just involves a type test.
7943 return true;
7944 }
7945 }
7946
7947 // An instruction that returns without throwing must transfer control flow
7948 // to a successor.
7949 return !I->mayThrow() && I->willReturn();
7950}
7951
7953 // TODO: This is slightly conservative for invoke instruction since exiting
7954 // via an exception *is* normal control for them.
7955 for (const Instruction &I : *BB)
7957 return false;
7958 return true;
7959}
7960
7967
7970 assert(ScanLimit && "scan limit must be non-zero");
7971 for (const Instruction &I : Range) {
7972 if (--ScanLimit == 0)
7973 return false;
7975 return false;
7976 }
7977 return true;
7978}
7979
7981 const Loop *L) {
7982 // The loop header is guaranteed to be executed for every iteration.
7983 //
7984 // FIXME: Relax this constraint to cover all basic blocks that are
7985 // guaranteed to be executed at every iteration.
7986 if (I->getParent() != L->getHeader()) return false;
7987
7988 for (const Instruction &LI : *L->getHeader()) {
7989 if (&LI == I) return true;
7990 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7991 }
7992 llvm_unreachable("Instruction not contained in its own parent basic block.");
7993}
7994
7996 switch (IID) {
7997 // TODO: Add more intrinsics.
7998 case Intrinsic::sadd_with_overflow:
7999 case Intrinsic::ssub_with_overflow:
8000 case Intrinsic::smul_with_overflow:
8001 case Intrinsic::uadd_with_overflow:
8002 case Intrinsic::usub_with_overflow:
8003 case Intrinsic::umul_with_overflow:
8004 // If an input is a vector containing a poison element, the
8005 // two output vectors (calculated results, overflow bits)'
8006 // corresponding lanes are poison.
8007 return true;
8008 case Intrinsic::ctpop:
8009 case Intrinsic::ctlz:
8010 case Intrinsic::cttz:
8011 case Intrinsic::abs:
8012 case Intrinsic::smax:
8013 case Intrinsic::smin:
8014 case Intrinsic::umax:
8015 case Intrinsic::umin:
8016 case Intrinsic::scmp:
8017 case Intrinsic::is_fpclass:
8018 case Intrinsic::ptrmask:
8019 case Intrinsic::ucmp:
8020 case Intrinsic::bitreverse:
8021 case Intrinsic::bswap:
8022 case Intrinsic::sadd_sat:
8023 case Intrinsic::ssub_sat:
8024 case Intrinsic::sshl_sat:
8025 case Intrinsic::uadd_sat:
8026 case Intrinsic::usub_sat:
8027 case Intrinsic::ushl_sat:
8028 case Intrinsic::smul_fix:
8029 case Intrinsic::smul_fix_sat:
8030 case Intrinsic::umul_fix:
8031 case Intrinsic::umul_fix_sat:
8032 case Intrinsic::pow:
8033 case Intrinsic::powi:
8034 case Intrinsic::sin:
8035 case Intrinsic::sinh:
8036 case Intrinsic::cos:
8037 case Intrinsic::cosh:
8038 case Intrinsic::sincos:
8039 case Intrinsic::sincospi:
8040 case Intrinsic::tan:
8041 case Intrinsic::tanh:
8042 case Intrinsic::asin:
8043 case Intrinsic::acos:
8044 case Intrinsic::atan:
8045 case Intrinsic::atan2:
8046 case Intrinsic::canonicalize:
8047 case Intrinsic::sqrt:
8048 case Intrinsic::exp:
8049 case Intrinsic::exp2:
8050 case Intrinsic::exp10:
8051 case Intrinsic::log:
8052 case Intrinsic::log2:
8053 case Intrinsic::log10:
8054 case Intrinsic::modf:
8055 case Intrinsic::floor:
8056 case Intrinsic::ceil:
8057 case Intrinsic::trunc:
8058 case Intrinsic::rint:
8059 case Intrinsic::nearbyint:
8060 case Intrinsic::round:
8061 case Intrinsic::roundeven:
8062 case Intrinsic::lrint:
8063 case Intrinsic::llrint:
8064 return true;
8065 default:
8066 return false;
8067 }
8068}
8069
8070bool llvm::propagatesPoison(const Use &PoisonOp) {
8071 const Operator *I = cast<Operator>(PoisonOp.getUser());
8072 switch (I->getOpcode()) {
8073 case Instruction::Freeze:
8074 case Instruction::PHI:
8075 case Instruction::Invoke:
8076 return false;
8077 case Instruction::Select:
8078 return PoisonOp.getOperandNo() == 0;
8079 case Instruction::Call:
8080 if (auto *II = dyn_cast<IntrinsicInst>(I))
8081 return intrinsicPropagatesPoison(II->getIntrinsicID());
8082 return false;
8083 case Instruction::ICmp:
8084 case Instruction::FCmp:
8085 case Instruction::GetElementPtr:
8086 return true;
8087 default:
8089 return true;
8090
8091 // Be conservative and return false.
8092 return false;
8093 }
8094}
8095
8096/// Enumerates all operands of \p I that are guaranteed to not be undef or
8097/// poison. If the callback \p Handle returns true, stop processing and return
8098/// true. Otherwise, return false.
8099template <typename CallableT>
8101 const CallableT &Handle) {
8102 switch (I->getOpcode()) {
8103 case Instruction::Store:
8104 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8105 return true;
8106 break;
8107
8108 case Instruction::Load:
8109 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8110 return true;
8111 break;
8112
8113 // Since dereferenceable attribute imply noundef, atomic operations
8114 // also implicitly have noundef pointers too
8115 case Instruction::AtomicCmpXchg:
8117 return true;
8118 break;
8119
8120 case Instruction::AtomicRMW:
8121 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8122 return true;
8123 break;
8124
8125 case Instruction::Call:
8126 case Instruction::Invoke: {
8127 const CallBase *CB = cast<CallBase>(I);
8128 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8129 return true;
8130 for (unsigned i = 0; i < CB->arg_size(); ++i)
8131 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8132 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8133 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8134 Handle(CB->getArgOperand(i)))
8135 return true;
8136 break;
8137 }
8138 case Instruction::Ret:
8139 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8140 Handle(I->getOperand(0)))
8141 return true;
8142 break;
8143 case Instruction::Switch:
8144 if (Handle(cast<SwitchInst>(I)->getCondition()))
8145 return true;
8146 break;
8147 case Instruction::Br: {
8148 auto *BR = cast<BranchInst>(I);
8149 if (BR->isConditional() && Handle(BR->getCondition()))
8150 return true;
8151 break;
8152 }
8153 default:
8154 break;
8155 }
8156
8157 return false;
8158}
8159
8160/// Enumerates all operands of \p I that are guaranteed to not be poison.
8161template <typename CallableT>
8163 const CallableT &Handle) {
8164 if (handleGuaranteedWellDefinedOps(I, Handle))
8165 return true;
8166 switch (I->getOpcode()) {
8167 // Divisors of these operations are allowed to be partially undef.
8168 case Instruction::UDiv:
8169 case Instruction::SDiv:
8170 case Instruction::URem:
8171 case Instruction::SRem:
8172 return Handle(I->getOperand(1));
8173 default:
8174 return false;
8175 }
8176}
8177
8179 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8181 I, [&](const Value *V) { return KnownPoison.count(V); });
8182}
8183
8185 bool PoisonOnly) {
8186 // We currently only look for uses of values within the same basic
8187 // block, as that makes it easier to guarantee that the uses will be
8188 // executed given that Inst is executed.
8189 //
8190 // FIXME: Expand this to consider uses beyond the same basic block. To do
8191 // this, look out for the distinction between post-dominance and strong
8192 // post-dominance.
8193 const BasicBlock *BB = nullptr;
8195 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8196 BB = Inst->getParent();
8197 Begin = Inst->getIterator();
8198 Begin++;
8199 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8200 if (Arg->getParent()->isDeclaration())
8201 return false;
8202 BB = &Arg->getParent()->getEntryBlock();
8203 Begin = BB->begin();
8204 } else {
8205 return false;
8206 }
8207
8208 // Limit number of instructions we look at, to avoid scanning through large
8209 // blocks. The current limit is chosen arbitrarily.
8210 unsigned ScanLimit = 32;
8211 BasicBlock::const_iterator End = BB->end();
8212
8213 if (!PoisonOnly) {
8214 // Since undef does not propagate eagerly, be conservative & just check
8215 // whether a value is directly passed to an instruction that must take
8216 // well-defined operands.
8217
8218 for (const auto &I : make_range(Begin, End)) {
8219 if (--ScanLimit == 0)
8220 break;
8221
8222 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8223 return WellDefinedOp == V;
8224 }))
8225 return true;
8226
8228 break;
8229 }
8230 return false;
8231 }
8232
8233 // Set of instructions that we have proved will yield poison if Inst
8234 // does.
8235 SmallPtrSet<const Value *, 16> YieldsPoison;
8237
8238 YieldsPoison.insert(V);
8239 Visited.insert(BB);
8240
8241 while (true) {
8242 for (const auto &I : make_range(Begin, End)) {
8243 if (--ScanLimit == 0)
8244 return false;
8245 if (mustTriggerUB(&I, YieldsPoison))
8246 return true;
8248 return false;
8249
8250 // If an operand is poison and propagates it, mark I as yielding poison.
8251 for (const Use &Op : I.operands()) {
8252 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8253 YieldsPoison.insert(&I);
8254 break;
8255 }
8256 }
8257
8258 // Special handling for select, which returns poison if its operand 0 is
8259 // poison (handled in the loop above) *or* if both its true/false operands
8260 // are poison (handled here).
8261 if (I.getOpcode() == Instruction::Select &&
8262 YieldsPoison.count(I.getOperand(1)) &&
8263 YieldsPoison.count(I.getOperand(2))) {
8264 YieldsPoison.insert(&I);
8265 }
8266 }
8267
8268 BB = BB->getSingleSuccessor();
8269 if (!BB || !Visited.insert(BB).second)
8270 break;
8271
8272 Begin = BB->getFirstNonPHIIt();
8273 End = BB->end();
8274 }
8275 return false;
8276}
8277
8279 return ::programUndefinedIfUndefOrPoison(Inst, false);
8280}
8281
8283 return ::programUndefinedIfUndefOrPoison(Inst, true);
8284}
8285
8286static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8287 if (FMF.noNaNs())
8288 return true;
8289
8290 if (auto *C = dyn_cast<ConstantFP>(V))
8291 return !C->isNaN();
8292
8293 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8294 if (!C->getElementType()->isFloatingPointTy())
8295 return false;
8296 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8297 if (C->getElementAsAPFloat(I).isNaN())
8298 return false;
8299 }
8300 return true;
8301 }
8302
8304 return true;
8305
8306 return false;
8307}
8308
8309static bool isKnownNonZero(const Value *V) {
8310 if (auto *C = dyn_cast<ConstantFP>(V))
8311 return !C->isZero();
8312
8313 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8314 if (!C->getElementType()->isFloatingPointTy())
8315 return false;
8316 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8317 if (C->getElementAsAPFloat(I).isZero())
8318 return false;
8319 }
8320 return true;
8321 }
8322
8323 return false;
8324}
8325
8326/// Match clamp pattern for float types without care about NaNs or signed zeros.
8327/// Given non-min/max outer cmp/select from the clamp pattern this
8328/// function recognizes if it can be substitued by a "canonical" min/max
8329/// pattern.
8331 Value *CmpLHS, Value *CmpRHS,
8332 Value *TrueVal, Value *FalseVal,
8333 Value *&LHS, Value *&RHS) {
8334 // Try to match
8335 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8336 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8337 // and return description of the outer Max/Min.
8338
8339 // First, check if select has inverse order:
8340 if (CmpRHS == FalseVal) {
8341 std::swap(TrueVal, FalseVal);
8342 Pred = CmpInst::getInversePredicate(Pred);
8343 }
8344
8345 // Assume success now. If there's no match, callers should not use these anyway.
8346 LHS = TrueVal;
8347 RHS = FalseVal;
8348
8349 const APFloat *FC1;
8350 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8351 return {SPF_UNKNOWN, SPNB_NA, false};
8352
8353 const APFloat *FC2;
8354 switch (Pred) {
8355 case CmpInst::FCMP_OLT:
8356 case CmpInst::FCMP_OLE:
8357 case CmpInst::FCMP_ULT:
8358 case CmpInst::FCMP_ULE:
8359 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8360 *FC1 < *FC2)
8361 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8362 break;
8363 case CmpInst::FCMP_OGT:
8364 case CmpInst::FCMP_OGE:
8365 case CmpInst::FCMP_UGT:
8366 case CmpInst::FCMP_UGE:
8367 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8368 *FC1 > *FC2)
8369 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8370 break;
8371 default:
8372 break;
8373 }
8374
8375 return {SPF_UNKNOWN, SPNB_NA, false};
8376}
8377
8378/// Recognize variations of:
8379/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8381 Value *CmpLHS, Value *CmpRHS,
8382 Value *TrueVal, Value *FalseVal) {
8383 // Swap the select operands and predicate to match the patterns below.
8384 if (CmpRHS != TrueVal) {
8385 Pred = ICmpInst::getSwappedPredicate(Pred);
8386 std::swap(TrueVal, FalseVal);
8387 }
8388 const APInt *C1;
8389 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8390 const APInt *C2;
8391 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8392 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8393 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8394 return {SPF_SMAX, SPNB_NA, false};
8395
8396 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8397 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8398 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8399 return {SPF_SMIN, SPNB_NA, false};
8400
8401 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8402 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8403 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8404 return {SPF_UMAX, SPNB_NA, false};
8405
8406 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8407 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8408 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8409 return {SPF_UMIN, SPNB_NA, false};
8410 }
8411 return {SPF_UNKNOWN, SPNB_NA, false};
8412}
8413
8414/// Recognize variations of:
8415/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8417 Value *CmpLHS, Value *CmpRHS,
8418 Value *TVal, Value *FVal,
8419 unsigned Depth) {
8420 // TODO: Allow FP min/max with nnan/nsz.
8421 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8422
8423 Value *A = nullptr, *B = nullptr;
8424 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8425 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8426 return {SPF_UNKNOWN, SPNB_NA, false};
8427
8428 Value *C = nullptr, *D = nullptr;
8429 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8430 if (L.Flavor != R.Flavor)
8431 return {SPF_UNKNOWN, SPNB_NA, false};
8432
8433 // We have something like: x Pred y ? min(a, b) : min(c, d).
8434 // Try to match the compare to the min/max operations of the select operands.
8435 // First, make sure we have the right compare predicate.
8436 switch (L.Flavor) {
8437 case SPF_SMIN:
8438 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8439 Pred = ICmpInst::getSwappedPredicate(Pred);
8440 std::swap(CmpLHS, CmpRHS);
8441 }
8442 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8443 break;
8444 return {SPF_UNKNOWN, SPNB_NA, false};
8445 case SPF_SMAX:
8446 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8447 Pred = ICmpInst::getSwappedPredicate(Pred);
8448 std::swap(CmpLHS, CmpRHS);
8449 }
8450 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8451 break;
8452 return {SPF_UNKNOWN, SPNB_NA, false};
8453 case SPF_UMIN:
8454 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8455 Pred = ICmpInst::getSwappedPredicate(Pred);
8456 std::swap(CmpLHS, CmpRHS);
8457 }
8458 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8459 break;
8460 return {SPF_UNKNOWN, SPNB_NA, false};
8461 case SPF_UMAX:
8462 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8463 Pred = ICmpInst::getSwappedPredicate(Pred);
8464 std::swap(CmpLHS, CmpRHS);
8465 }
8466 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8467 break;
8468 return {SPF_UNKNOWN, SPNB_NA, false};
8469 default:
8470 return {SPF_UNKNOWN, SPNB_NA, false};
8471 }
8472
8473 // If there is a common operand in the already matched min/max and the other
8474 // min/max operands match the compare operands (either directly or inverted),
8475 // then this is min/max of the same flavor.
8476
8477 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8478 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8479 if (D == B) {
8480 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8481 match(A, m_Not(m_Specific(CmpRHS)))))
8482 return {L.Flavor, SPNB_NA, false};
8483 }
8484 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8485 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8486 if (C == B) {
8487 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8488 match(A, m_Not(m_Specific(CmpRHS)))))
8489 return {L.Flavor, SPNB_NA, false};
8490 }
8491 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8492 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8493 if (D == A) {
8494 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8495 match(B, m_Not(m_Specific(CmpRHS)))))
8496 return {L.Flavor, SPNB_NA, false};
8497 }
8498 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8499 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8500 if (C == A) {
8501 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8502 match(B, m_Not(m_Specific(CmpRHS)))))
8503 return {L.Flavor, SPNB_NA, false};
8504 }
8505
8506 return {SPF_UNKNOWN, SPNB_NA, false};
8507}
8508
8509/// If the input value is the result of a 'not' op, constant integer, or vector
8510/// splat of a constant integer, return the bitwise-not source value.
8511/// TODO: This could be extended to handle non-splat vector integer constants.
8513 Value *NotV;
8514 if (match(V, m_Not(m_Value(NotV))))
8515 return NotV;
8516
8517 const APInt *C;
8518 if (match(V, m_APInt(C)))
8519 return ConstantInt::get(V->getType(), ~(*C));
8520
8521 return nullptr;
8522}
8523
8524/// Match non-obvious integer minimum and maximum sequences.
8526 Value *CmpLHS, Value *CmpRHS,
8527 Value *TrueVal, Value *FalseVal,
8528 Value *&LHS, Value *&RHS,
8529 unsigned Depth) {
8530 // Assume success. If there's no match, callers should not use these anyway.
8531 LHS = TrueVal;
8532 RHS = FalseVal;
8533
8534 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8536 return SPR;
8537
8538 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8540 return SPR;
8541
8542 // Look through 'not' ops to find disguised min/max.
8543 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8544 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8545 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8546 switch (Pred) {
8547 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8548 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8549 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8550 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8551 default: break;
8552 }
8553 }
8554
8555 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8556 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8557 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8558 switch (Pred) {
8559 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8560 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8561 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8562 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8563 default: break;
8564 }
8565 }
8566
8567 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8568 return {SPF_UNKNOWN, SPNB_NA, false};
8569
8570 const APInt *C1;
8571 if (!match(CmpRHS, m_APInt(C1)))
8572 return {SPF_UNKNOWN, SPNB_NA, false};
8573
8574 // An unsigned min/max can be written with a signed compare.
8575 const APInt *C2;
8576 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8577 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8578 // Is the sign bit set?
8579 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8580 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8581 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8582 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8583
8584 // Is the sign bit clear?
8585 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8586 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8587 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8588 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8589 }
8590
8591 return {SPF_UNKNOWN, SPNB_NA, false};
8592}
8593
8594bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8595 bool AllowPoison) {
8596 assert(X && Y && "Invalid operand");
8597
8598 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8599 if (!match(X, m_Neg(m_Specific(Y))))
8600 return false;
8601
8602 auto *BO = cast<BinaryOperator>(X);
8603 if (NeedNSW && !BO->hasNoSignedWrap())
8604 return false;
8605
8606 auto *Zero = cast<Constant>(BO->getOperand(0));
8607 if (!AllowPoison && !Zero->isNullValue())
8608 return false;
8609
8610 return true;
8611 };
8612
8613 // X = -Y or Y = -X
8614 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8615 return true;
8616
8617 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8618 Value *A, *B;
8619 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8620 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8621 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8623}
8624
8625bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8626 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8627 Value *A, *B, *C;
8628 CmpPredicate Pred1, Pred2;
8629 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8630 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8631 return false;
8632
8633 // They must both have samesign flag or not.
8634 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8635 return false;
8636
8637 if (B == C)
8638 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8639
8640 // Try to infer the relationship from constant ranges.
8641 const APInt *RHSC1, *RHSC2;
8642 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8643 return false;
8644
8645 // Sign bits of two RHSCs should match.
8646 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8647 return false;
8648
8649 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8650 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8651
8652 return CR1.inverse() == CR2;
8653}
8654
8656 SelectPatternNaNBehavior NaNBehavior,
8657 bool Ordered) {
8658 switch (Pred) {
8659 default:
8660 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8661 case ICmpInst::ICMP_UGT:
8662 case ICmpInst::ICMP_UGE:
8663 return {SPF_UMAX, SPNB_NA, false};
8664 case ICmpInst::ICMP_SGT:
8665 case ICmpInst::ICMP_SGE:
8666 return {SPF_SMAX, SPNB_NA, false};
8667 case ICmpInst::ICMP_ULT:
8668 case ICmpInst::ICMP_ULE:
8669 return {SPF_UMIN, SPNB_NA, false};
8670 case ICmpInst::ICMP_SLT:
8671 case ICmpInst::ICMP_SLE:
8672 return {SPF_SMIN, SPNB_NA, false};
8673 case FCmpInst::FCMP_UGT:
8674 case FCmpInst::FCMP_UGE:
8675 case FCmpInst::FCMP_OGT:
8676 case FCmpInst::FCMP_OGE:
8677 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8678 case FCmpInst::FCMP_ULT:
8679 case FCmpInst::FCMP_ULE:
8680 case FCmpInst::FCMP_OLT:
8681 case FCmpInst::FCMP_OLE:
8682 return {SPF_FMINNUM, NaNBehavior, Ordered};
8683 }
8684}
8685
8686std::optional<std::pair<CmpPredicate, Constant *>>
8689 "Only for relational integer predicates.");
8690 if (isa<UndefValue>(C))
8691 return std::nullopt;
8692
8693 Type *Type = C->getType();
8694 bool IsSigned = ICmpInst::isSigned(Pred);
8695
8697 bool WillIncrement =
8698 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8699
8700 // Check if the constant operand can be safely incremented/decremented
8701 // without overflowing/underflowing.
8702 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8703 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8704 };
8705
8706 Constant *SafeReplacementConstant = nullptr;
8707 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8708 // Bail out if the constant can't be safely incremented/decremented.
8709 if (!ConstantIsOk(CI))
8710 return std::nullopt;
8711 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8712 unsigned NumElts = FVTy->getNumElements();
8713 for (unsigned i = 0; i != NumElts; ++i) {
8714 Constant *Elt = C->getAggregateElement(i);
8715 if (!Elt)
8716 return std::nullopt;
8717
8718 if (isa<UndefValue>(Elt))
8719 continue;
8720
8721 // Bail out if we can't determine if this constant is min/max or if we
8722 // know that this constant is min/max.
8723 auto *CI = dyn_cast<ConstantInt>(Elt);
8724 if (!CI || !ConstantIsOk(CI))
8725 return std::nullopt;
8726
8727 if (!SafeReplacementConstant)
8728 SafeReplacementConstant = CI;
8729 }
8730 } else if (isa<VectorType>(C->getType())) {
8731 // Handle scalable splat
8732 Value *SplatC = C->getSplatValue();
8733 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8734 // Bail out if the constant can't be safely incremented/decremented.
8735 if (!CI || !ConstantIsOk(CI))
8736 return std::nullopt;
8737 } else {
8738 // ConstantExpr?
8739 return std::nullopt;
8740 }
8741
8742 // It may not be safe to change a compare predicate in the presence of
8743 // undefined elements, so replace those elements with the first safe constant
8744 // that we found.
8745 // TODO: in case of poison, it is safe; let's replace undefs only.
8746 if (C->containsUndefOrPoisonElement()) {
8747 assert(SafeReplacementConstant && "Replacement constant not set");
8748 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8749 }
8750
8752
8753 // Increment or decrement the constant.
8754 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8755 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8756
8757 return std::make_pair(NewPred, NewC);
8758}
8759
8761 FastMathFlags FMF,
8762 Value *CmpLHS, Value *CmpRHS,
8763 Value *TrueVal, Value *FalseVal,
8764 Value *&LHS, Value *&RHS,
8765 unsigned Depth) {
8766 bool HasMismatchedZeros = false;
8767 if (CmpInst::isFPPredicate(Pred)) {
8768 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8769 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8770 // purpose of identifying min/max. Disregard vector constants with undefined
8771 // elements because those can not be back-propagated for analysis.
8772 Value *OutputZeroVal = nullptr;
8773 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8774 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8775 OutputZeroVal = TrueVal;
8776 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8777 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8778 OutputZeroVal = FalseVal;
8779
8780 if (OutputZeroVal) {
8781 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8782 HasMismatchedZeros = true;
8783 CmpLHS = OutputZeroVal;
8784 }
8785 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8786 HasMismatchedZeros = true;
8787 CmpRHS = OutputZeroVal;
8788 }
8789 }
8790 }
8791
8792 LHS = CmpLHS;
8793 RHS = CmpRHS;
8794
8795 // Signed zero may return inconsistent results between implementations.
8796 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8797 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8798 // Therefore, we behave conservatively and only proceed if at least one of the
8799 // operands is known to not be zero or if we don't care about signed zero.
8800 switch (Pred) {
8801 default: break;
8804 if (!HasMismatchedZeros)
8805 break;
8806 [[fallthrough]];
8809 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8810 !isKnownNonZero(CmpRHS))
8811 return {SPF_UNKNOWN, SPNB_NA, false};
8812 }
8813
8814 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8815 bool Ordered = false;
8816
8817 // When given one NaN and one non-NaN input:
8818 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8819 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8820 // ordered comparison fails), which could be NaN or non-NaN.
8821 // so here we discover exactly what NaN behavior is required/accepted.
8822 if (CmpInst::isFPPredicate(Pred)) {
8823 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8824 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8825
8826 if (LHSSafe && RHSSafe) {
8827 // Both operands are known non-NaN.
8828 NaNBehavior = SPNB_RETURNS_ANY;
8829 Ordered = CmpInst::isOrdered(Pred);
8830 } else if (CmpInst::isOrdered(Pred)) {
8831 // An ordered comparison will return false when given a NaN, so it
8832 // returns the RHS.
8833 Ordered = true;
8834 if (LHSSafe)
8835 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8836 NaNBehavior = SPNB_RETURNS_NAN;
8837 else if (RHSSafe)
8838 NaNBehavior = SPNB_RETURNS_OTHER;
8839 else
8840 // Completely unsafe.
8841 return {SPF_UNKNOWN, SPNB_NA, false};
8842 } else {
8843 Ordered = false;
8844 // An unordered comparison will return true when given a NaN, so it
8845 // returns the LHS.
8846 if (LHSSafe)
8847 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8848 NaNBehavior = SPNB_RETURNS_OTHER;
8849 else if (RHSSafe)
8850 NaNBehavior = SPNB_RETURNS_NAN;
8851 else
8852 // Completely unsafe.
8853 return {SPF_UNKNOWN, SPNB_NA, false};
8854 }
8855 }
8856
8857 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8858 std::swap(CmpLHS, CmpRHS);
8859 Pred = CmpInst::getSwappedPredicate(Pred);
8860 if (NaNBehavior == SPNB_RETURNS_NAN)
8861 NaNBehavior = SPNB_RETURNS_OTHER;
8862 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8863 NaNBehavior = SPNB_RETURNS_NAN;
8864 Ordered = !Ordered;
8865 }
8866
8867 // ([if]cmp X, Y) ? X : Y
8868 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8869 return getSelectPattern(Pred, NaNBehavior, Ordered);
8870
8871 if (isKnownNegation(TrueVal, FalseVal)) {
8872 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8873 // match against either LHS or sext(LHS).
8874 auto MaybeSExtCmpLHS =
8875 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8876 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8877 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8878 if (match(TrueVal, MaybeSExtCmpLHS)) {
8879 // Set the return values. If the compare uses the negated value (-X >s 0),
8880 // swap the return values because the negated value is always 'RHS'.
8881 LHS = TrueVal;
8882 RHS = FalseVal;
8883 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8884 std::swap(LHS, RHS);
8885
8886 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8887 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8888 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8889 return {SPF_ABS, SPNB_NA, false};
8890
8891 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8892 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8893 return {SPF_ABS, SPNB_NA, false};
8894
8895 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8896 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8897 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8898 return {SPF_NABS, SPNB_NA, false};
8899 }
8900 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8901 // Set the return values. If the compare uses the negated value (-X >s 0),
8902 // swap the return values because the negated value is always 'RHS'.
8903 LHS = FalseVal;
8904 RHS = TrueVal;
8905 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8906 std::swap(LHS, RHS);
8907
8908 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8909 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8910 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8911 return {SPF_NABS, SPNB_NA, false};
8912
8913 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8914 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8915 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8916 return {SPF_ABS, SPNB_NA, false};
8917 }
8918 }
8919
8920 if (CmpInst::isIntPredicate(Pred))
8921 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8922
8923 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8924 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8925 // semantics than minNum. Be conservative in such case.
8926 if (NaNBehavior != SPNB_RETURNS_ANY ||
8927 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8928 !isKnownNonZero(CmpRHS)))
8929 return {SPF_UNKNOWN, SPNB_NA, false};
8930
8931 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8932}
8933
8935 Instruction::CastOps *CastOp) {
8936 const DataLayout &DL = CmpI->getDataLayout();
8937
8938 Constant *CastedTo = nullptr;
8939 switch (*CastOp) {
8940 case Instruction::ZExt:
8941 if (CmpI->isUnsigned())
8942 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8943 break;
8944 case Instruction::SExt:
8945 if (CmpI->isSigned())
8946 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8947 break;
8948 case Instruction::Trunc:
8949 Constant *CmpConst;
8950 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8951 CmpConst->getType() == SrcTy) {
8952 // Here we have the following case:
8953 //
8954 // %cond = cmp iN %x, CmpConst
8955 // %tr = trunc iN %x to iK
8956 // %narrowsel = select i1 %cond, iK %t, iK C
8957 //
8958 // We can always move trunc after select operation:
8959 //
8960 // %cond = cmp iN %x, CmpConst
8961 // %widesel = select i1 %cond, iN %x, iN CmpConst
8962 // %tr = trunc iN %widesel to iK
8963 //
8964 // Note that C could be extended in any way because we don't care about
8965 // upper bits after truncation. It can't be abs pattern, because it would
8966 // look like:
8967 //
8968 // select i1 %cond, x, -x.
8969 //
8970 // So only min/max pattern could be matched. Such match requires widened C
8971 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8972 // CmpConst == C is checked below.
8973 CastedTo = CmpConst;
8974 } else {
8975 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8976 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8977 }
8978 break;
8979 case Instruction::FPTrunc:
8980 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8981 break;
8982 case Instruction::FPExt:
8983 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8984 break;
8985 case Instruction::FPToUI:
8986 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8987 break;
8988 case Instruction::FPToSI:
8989 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8990 break;
8991 case Instruction::UIToFP:
8992 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8993 break;
8994 case Instruction::SIToFP:
8995 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8996 break;
8997 default:
8998 break;
8999 }
9000
9001 if (!CastedTo)
9002 return nullptr;
9003
9004 // Make sure the cast doesn't lose any information.
9005 Constant *CastedBack =
9006 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9007 if (CastedBack && CastedBack != C)
9008 return nullptr;
9009
9010 return CastedTo;
9011}
9012
9013/// Helps to match a select pattern in case of a type mismatch.
9014///
9015/// The function processes the case when type of true and false values of a
9016/// select instruction differs from type of the cmp instruction operands because
9017/// of a cast instruction. The function checks if it is legal to move the cast
9018/// operation after "select". If yes, it returns the new second value of
9019/// "select" (with the assumption that cast is moved):
9020/// 1. As operand of cast instruction when both values of "select" are same cast
9021/// instructions.
9022/// 2. As restored constant (by applying reverse cast operation) when the first
9023/// value of the "select" is a cast operation and the second value is a
9024/// constant. It is implemented in lookThroughCastConst().
9025/// 3. As one operand is cast instruction and the other is not. The operands in
9026/// sel(cmp) are in different type integer.
9027/// NOTE: We return only the new second value because the first value could be
9028/// accessed as operand of cast instruction.
9029static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9030 Instruction::CastOps *CastOp) {
9031 auto *Cast1 = dyn_cast<CastInst>(V1);
9032 if (!Cast1)
9033 return nullptr;
9034
9035 *CastOp = Cast1->getOpcode();
9036 Type *SrcTy = Cast1->getSrcTy();
9037 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9038 // If V1 and V2 are both the same cast from the same type, look through V1.
9039 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9040 return Cast2->getOperand(0);
9041 return nullptr;
9042 }
9043
9044 auto *C = dyn_cast<Constant>(V2);
9045 if (C)
9046 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9047
9048 Value *CastedTo = nullptr;
9049 if (*CastOp == Instruction::Trunc) {
9050 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9051 // Here we have the following case:
9052 // %y_ext = sext iK %y to iN
9053 // %cond = cmp iN %x, %y_ext
9054 // %tr = trunc iN %x to iK
9055 // %narrowsel = select i1 %cond, iK %tr, iK %y
9056 //
9057 // We can always move trunc after select operation:
9058 // %y_ext = sext iK %y to iN
9059 // %cond = cmp iN %x, %y_ext
9060 // %widesel = select i1 %cond, iN %x, iN %y_ext
9061 // %tr = trunc iN %widesel to iK
9062 assert(V2->getType() == Cast1->getType() &&
9063 "V2 and Cast1 should be the same type.");
9064 CastedTo = CmpI->getOperand(1);
9065 }
9066 }
9067
9068 return CastedTo;
9069}
9071 Instruction::CastOps *CastOp,
9072 unsigned Depth) {
9074 return {SPF_UNKNOWN, SPNB_NA, false};
9075
9077 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9078
9079 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9080 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9081
9082 Value *TrueVal = SI->getTrueValue();
9083 Value *FalseVal = SI->getFalseValue();
9084
9086 CmpI, TrueVal, FalseVal, LHS, RHS,
9087 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
9088 CastOp, Depth);
9089}
9090
9092 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9093 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9094 CmpInst::Predicate Pred = CmpI->getPredicate();
9095 Value *CmpLHS = CmpI->getOperand(0);
9096 Value *CmpRHS = CmpI->getOperand(1);
9097 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9098 FMF.setNoNaNs();
9099
9100 // Bail out early.
9101 if (CmpI->isEquality())
9102 return {SPF_UNKNOWN, SPNB_NA, false};
9103
9104 // Deal with type mismatches.
9105 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9106 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9107 // If this is a potential fmin/fmax with a cast to integer, then ignore
9108 // -0.0 because there is no corresponding integer value.
9109 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9110 FMF.setNoSignedZeros();
9111 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9112 cast<CastInst>(TrueVal)->getOperand(0), C,
9113 LHS, RHS, Depth);
9114 }
9115 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9116 // If this is a potential fmin/fmax with a cast to integer, then ignore
9117 // -0.0 because there is no corresponding integer value.
9118 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9119 FMF.setNoSignedZeros();
9120 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9121 C, cast<CastInst>(FalseVal)->getOperand(0),
9122 LHS, RHS, Depth);
9123 }
9124 }
9125 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9126 LHS, RHS, Depth);
9127}
9128
9130 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9131 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9132 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9133 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9134 if (SPF == SPF_FMINNUM)
9135 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9136 if (SPF == SPF_FMAXNUM)
9137 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9138 llvm_unreachable("unhandled!");
9139}
9140
9142 switch (SPF) {
9144 return Intrinsic::umin;
9146 return Intrinsic::umax;
9148 return Intrinsic::smin;
9150 return Intrinsic::smax;
9151 default:
9152 llvm_unreachable("Unexpected SPF");
9153 }
9154}
9155
9157 if (SPF == SPF_SMIN) return SPF_SMAX;
9158 if (SPF == SPF_UMIN) return SPF_UMAX;
9159 if (SPF == SPF_SMAX) return SPF_SMIN;
9160 if (SPF == SPF_UMAX) return SPF_UMIN;
9161 llvm_unreachable("unhandled!");
9162}
9163
9165 switch (MinMaxID) {
9166 case Intrinsic::smax: return Intrinsic::smin;
9167 case Intrinsic::smin: return Intrinsic::smax;
9168 case Intrinsic::umax: return Intrinsic::umin;
9169 case Intrinsic::umin: return Intrinsic::umax;
9170 // Please note that next four intrinsics may produce the same result for
9171 // original and inverted case even if X != Y due to NaN is handled specially.
9172 case Intrinsic::maximum: return Intrinsic::minimum;
9173 case Intrinsic::minimum: return Intrinsic::maximum;
9174 case Intrinsic::maxnum: return Intrinsic::minnum;
9175 case Intrinsic::minnum: return Intrinsic::maxnum;
9176 case Intrinsic::maximumnum:
9177 return Intrinsic::minimumnum;
9178 case Intrinsic::minimumnum:
9179 return Intrinsic::maximumnum;
9180 default: llvm_unreachable("Unexpected intrinsic");
9181 }
9182}
9183
9185 switch (SPF) {
9188 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9189 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9190 default: llvm_unreachable("Unexpected flavor");
9191 }
9192}
9193
9194std::pair<Intrinsic::ID, bool>
9196 // Check if VL contains select instructions that can be folded into a min/max
9197 // vector intrinsic and return the intrinsic if it is possible.
9198 // TODO: Support floating point min/max.
9199 bool AllCmpSingleUse = true;
9200 SelectPatternResult SelectPattern;
9201 SelectPattern.Flavor = SPF_UNKNOWN;
9202 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9203 Value *LHS, *RHS;
9204 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9205 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9206 return false;
9207 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9208 SelectPattern.Flavor != CurrentPattern.Flavor)
9209 return false;
9210 SelectPattern = CurrentPattern;
9211 AllCmpSingleUse &=
9213 return true;
9214 })) {
9215 switch (SelectPattern.Flavor) {
9216 case SPF_SMIN:
9217 return {Intrinsic::smin, AllCmpSingleUse};
9218 case SPF_UMIN:
9219 return {Intrinsic::umin, AllCmpSingleUse};
9220 case SPF_SMAX:
9221 return {Intrinsic::smax, AllCmpSingleUse};
9222 case SPF_UMAX:
9223 return {Intrinsic::umax, AllCmpSingleUse};
9224 case SPF_FMAXNUM:
9225 return {Intrinsic::maxnum, AllCmpSingleUse};
9226 case SPF_FMINNUM:
9227 return {Intrinsic::minnum, AllCmpSingleUse};
9228 default:
9229 llvm_unreachable("unexpected select pattern flavor");
9230 }
9231 }
9232 return {Intrinsic::not_intrinsic, false};
9233}
9234
9235template <typename InstTy>
9236static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9237 Value *&Init, Value *&OtherOp) {
9238 // Handle the case of a simple two-predecessor recurrence PHI.
9239 // There's a lot more that could theoretically be done here, but
9240 // this is sufficient to catch some interesting cases.
9241 // TODO: Expand list -- gep, uadd.sat etc.
9242 if (PN->getNumIncomingValues() != 2)
9243 return false;
9244
9245 for (unsigned I = 0; I != 2; ++I) {
9246 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9247 Operation && Operation->getNumOperands() >= 2) {
9248 Value *LHS = Operation->getOperand(0);
9249 Value *RHS = Operation->getOperand(1);
9250 if (LHS != PN && RHS != PN)
9251 continue;
9252
9253 Inst = Operation;
9254 Init = PN->getIncomingValue(!I);
9255 OtherOp = (LHS == PN) ? RHS : LHS;
9256 return true;
9257 }
9258 }
9259 return false;
9260}
9261
9263 Value *&Start, Value *&Step) {
9264 // We try to match a recurrence of the form:
9265 // %iv = [Start, %entry], [%iv.next, %backedge]
9266 // %iv.next = binop %iv, Step
9267 // Or:
9268 // %iv = [Start, %entry], [%iv.next, %backedge]
9269 // %iv.next = binop Step, %iv
9270 return matchTwoInputRecurrence(P, BO, Start, Step);
9271}
9272
9274 Value *&Start, Value *&Step) {
9275 BinaryOperator *BO = nullptr;
9276 P = dyn_cast<PHINode>(I->getOperand(0));
9277 if (!P)
9278 P = dyn_cast<PHINode>(I->getOperand(1));
9279 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9280}
9281
9283 PHINode *&P, Value *&Init,
9284 Value *&OtherOp) {
9285 // Binary intrinsics only supported for now.
9286 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9287 I->getType() != I->getArgOperand(1)->getType())
9288 return false;
9289
9290 IntrinsicInst *II = nullptr;
9291 P = dyn_cast<PHINode>(I->getArgOperand(0));
9292 if (!P)
9293 P = dyn_cast<PHINode>(I->getArgOperand(1));
9294
9295 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9296}
9297
9298/// Return true if "icmp Pred LHS RHS" is always true.
9300 const Value *RHS) {
9301 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9302 return true;
9303
9304 switch (Pred) {
9305 default:
9306 return false;
9307
9308 case CmpInst::ICMP_SLE: {
9309 const APInt *C;
9310
9311 // LHS s<= LHS +_{nsw} C if C >= 0
9312 // LHS s<= LHS | C if C >= 0
9313 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9315 return !C->isNegative();
9316
9317 // LHS s<= smax(LHS, V) for any V
9319 return true;
9320
9321 // smin(RHS, V) s<= RHS for any V
9323 return true;
9324
9325 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9326 const Value *X;
9327 const APInt *CLHS, *CRHS;
9328 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9330 return CLHS->sle(*CRHS);
9331
9332 return false;
9333 }
9334
9335 case CmpInst::ICMP_ULE: {
9336 // LHS u<= LHS +_{nuw} V for any V
9337 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9339 return true;
9340
9341 // LHS u<= LHS | V for any V
9342 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9343 return true;
9344
9345 // LHS u<= umax(LHS, V) for any V
9347 return true;
9348
9349 // RHS >> V u<= RHS for any V
9350 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9351 return true;
9352
9353 // RHS u/ C_ugt_1 u<= RHS
9354 const APInt *C;
9355 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9356 return true;
9357
9358 // RHS & V u<= RHS for any V
9360 return true;
9361
9362 // umin(RHS, V) u<= RHS for any V
9364 return true;
9365
9366 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9367 const Value *X;
9368 const APInt *CLHS, *CRHS;
9369 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9371 return CLHS->ule(*CRHS);
9372
9373 return false;
9374 }
9375 }
9376}
9377
9378/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9379/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9380static std::optional<bool>
9382 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9383 switch (Pred) {
9384 default:
9385 return std::nullopt;
9386
9387 case CmpInst::ICMP_SLT:
9388 case CmpInst::ICMP_SLE:
9389 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9391 return true;
9392 return std::nullopt;
9393
9394 case CmpInst::ICMP_SGT:
9395 case CmpInst::ICMP_SGE:
9396 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9398 return true;
9399 return std::nullopt;
9400
9401 case CmpInst::ICMP_ULT:
9402 case CmpInst::ICMP_ULE:
9403 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9405 return true;
9406 return std::nullopt;
9407
9408 case CmpInst::ICMP_UGT:
9409 case CmpInst::ICMP_UGE:
9410 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9412 return true;
9413 return std::nullopt;
9414 }
9415}
9416
9417/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9418/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9419/// Otherwise, return std::nullopt if we can't infer anything.
9420static std::optional<bool>
9422 CmpPredicate RPred, const ConstantRange &RCR) {
9423 auto CRImpliesPred = [&](ConstantRange CR,
9424 CmpInst::Predicate Pred) -> std::optional<bool> {
9425 // If all true values for lhs and true for rhs, lhs implies rhs
9426 if (CR.icmp(Pred, RCR))
9427 return true;
9428
9429 // If there is no overlap, lhs implies not rhs
9430 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9431 return false;
9432
9433 return std::nullopt;
9434 };
9435 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9436 RPred))
9437 return Res;
9438 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9440 : LPred.dropSameSign();
9442 : RPred.dropSameSign();
9443 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9444 RPred);
9445 }
9446 return std::nullopt;
9447}
9448
9449/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9450/// is true. Return false if LHS implies RHS is false. Otherwise, return
9451/// std::nullopt if we can't infer anything.
9452static std::optional<bool>
9453isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9454 CmpPredicate RPred, const Value *R0, const Value *R1,
9455 const DataLayout &DL, bool LHSIsTrue) {
9456 // The rest of the logic assumes the LHS condition is true. If that's not the
9457 // case, invert the predicate to make it so.
9458 if (!LHSIsTrue)
9459 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9460
9461 // We can have non-canonical operands, so try to normalize any common operand
9462 // to L0/R0.
9463 if (L0 == R1) {
9464 std::swap(R0, R1);
9465 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9466 }
9467 if (R0 == L1) {
9468 std::swap(L0, L1);
9469 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9470 }
9471 if (L1 == R1) {
9472 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9473 if (L0 != R0 || match(L0, m_ImmConstant())) {
9474 std::swap(L0, L1);
9475 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9476 std::swap(R0, R1);
9477 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9478 }
9479 }
9480
9481 // See if we can infer anything if operand-0 matches and we have at least one
9482 // constant.
9483 const APInt *Unused;
9484 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9485 // Potential TODO: We could also further use the constant range of L0/R0 to
9486 // further constraint the constant ranges. At the moment this leads to
9487 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9488 // C1` (see discussion: D58633).
9490 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9491 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9493 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9494 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9495 // Even if L1/R1 are not both constant, we can still sometimes deduce
9496 // relationship from a single constant. For example X u> Y implies X != 0.
9497 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9498 return R;
9499 // If both L1/R1 were exact constant ranges and we didn't get anything
9500 // here, we won't be able to deduce this.
9501 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9502 return std::nullopt;
9503 }
9504
9505 // Can we infer anything when the two compares have matching operands?
9506 if (L0 == R0 && L1 == R1)
9507 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9508
9509 // It only really makes sense in the context of signed comparison for "X - Y
9510 // must be positive if X >= Y and no overflow".
9511 // Take SGT as an example: L0:x > L1:y and C >= 0
9512 // ==> R0:(x -nsw y) < R1:(-C) is false
9513 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9514 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9515 SignedLPred == ICmpInst::ICMP_SGE) &&
9516 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9517 if (match(R1, m_NonPositive()) &&
9518 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9519 return false;
9520 }
9521
9522 // Take SLT as an example: L0:x < L1:y and C <= 0
9523 // ==> R0:(x -nsw y) < R1:(-C) is true
9524 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9525 SignedLPred == ICmpInst::ICMP_SLE) &&
9526 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9527 if (match(R1, m_NonNegative()) &&
9528 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9529 return true;
9530 }
9531
9532 // a - b == NonZero -> a != b
9533 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9534 const APInt *L1C;
9535 Value *A, *B;
9536 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9537 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9538 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9539 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9540 (match(A, m_PtrToInt(m_Specific(R0))) &&
9541 match(B, m_PtrToInt(m_Specific(R1)))) ||
9542 (match(A, m_PtrToInt(m_Specific(R1))) &&
9543 match(B, m_PtrToInt(m_Specific(R0)))))) {
9544 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9545 }
9546
9547 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9548 if (L0 == R0 &&
9549 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9550 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9551 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9552 return CmpPredicate::getMatching(LPred, RPred).has_value();
9553
9554 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9555 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9556
9557 return std::nullopt;
9558}
9559
9560/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9561/// is true. Return false if LHS implies RHS is false. Otherwise, return
9562/// std::nullopt if we can't infer anything.
9563static std::optional<bool>
9565 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9566 const DataLayout &DL, bool LHSIsTrue) {
9567 // The rest of the logic assumes the LHS condition is true. If that's not the
9568 // case, invert the predicate to make it so.
9569 if (!LHSIsTrue)
9570 LPred = FCmpInst::getInversePredicate(LPred);
9571
9572 // We can have non-canonical operands, so try to normalize any common operand
9573 // to L0/R0.
9574 if (L0 == R1) {
9575 std::swap(R0, R1);
9576 RPred = FCmpInst::getSwappedPredicate(RPred);
9577 }
9578 if (R0 == L1) {
9579 std::swap(L0, L1);
9580 LPred = FCmpInst::getSwappedPredicate(LPred);
9581 }
9582 if (L1 == R1) {
9583 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9584 if (L0 != R0 || match(L0, m_ImmConstant())) {
9585 std::swap(L0, L1);
9586 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9587 std::swap(R0, R1);
9588 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9589 }
9590 }
9591
9592 // Can we infer anything when the two compares have matching operands?
9593 if (L0 == R0 && L1 == R1) {
9594 if ((LPred & RPred) == LPred)
9595 return true;
9596 if ((LPred & ~RPred) == LPred)
9597 return false;
9598 }
9599
9600 // See if we can infer anything if operand-0 matches and we have at least one
9601 // constant.
9602 const APFloat *L1C, *R1C;
9603 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9604 if (std::optional<ConstantFPRange> DomCR =
9606 if (std::optional<ConstantFPRange> ImpliedCR =
9608 if (ImpliedCR->contains(*DomCR))
9609 return true;
9610 }
9611 if (std::optional<ConstantFPRange> ImpliedCR =
9613 FCmpInst::getInversePredicate(RPred), *R1C)) {
9614 if (ImpliedCR->contains(*DomCR))
9615 return false;
9616 }
9617 }
9618 }
9619
9620 return std::nullopt;
9621}
9622
9623/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9624/// false. Otherwise, return std::nullopt if we can't infer anything. We
9625/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9626/// instruction.
9627static std::optional<bool>
9629 const Value *RHSOp0, const Value *RHSOp1,
9630 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9631 // The LHS must be an 'or', 'and', or a 'select' instruction.
9632 assert((LHS->getOpcode() == Instruction::And ||
9633 LHS->getOpcode() == Instruction::Or ||
9634 LHS->getOpcode() == Instruction::Select) &&
9635 "Expected LHS to be 'and', 'or', or 'select'.");
9636
9637 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9638
9639 // If the result of an 'or' is false, then we know both legs of the 'or' are
9640 // false. Similarly, if the result of an 'and' is true, then we know both
9641 // legs of the 'and' are true.
9642 const Value *ALHS, *ARHS;
9643 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9644 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9645 // FIXME: Make this non-recursion.
9646 if (std::optional<bool> Implication = isImpliedCondition(
9647 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9648 return Implication;
9649 if (std::optional<bool> Implication = isImpliedCondition(
9650 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9651 return Implication;
9652 return std::nullopt;
9653 }
9654 return std::nullopt;
9655}
9656
9657std::optional<bool>
9659 const Value *RHSOp0, const Value *RHSOp1,
9660 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9661 // Bail out when we hit the limit.
9663 return std::nullopt;
9664
9665 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9666 // example.
9667 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9668 return std::nullopt;
9669
9670 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9671 "Expected integer type only!");
9672
9673 // Match not
9674 if (match(LHS, m_Not(m_Value(LHS))))
9675 LHSIsTrue = !LHSIsTrue;
9676
9677 // Both LHS and RHS are icmps.
9678 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9679 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9680 return isImpliedCondICmps(LHSCmp->getCmpPredicate(),
9681 LHSCmp->getOperand(0), LHSCmp->getOperand(1),
9682 RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
9683 const Value *V;
9684 if (match(LHS, m_NUWTrunc(m_Value(V))))
9686 ConstantInt::get(V->getType(), 0), RHSPred,
9687 RHSOp0, RHSOp1, DL, LHSIsTrue);
9688 } else {
9689 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9690 "Expected floating point type only!");
9691 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9692 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9693 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9694 DL, LHSIsTrue);
9695 }
9696
9697 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9698 /// the RHS to be an icmp.
9699 /// FIXME: Add support for and/or/select on the RHS.
9700 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9701 if ((LHSI->getOpcode() == Instruction::And ||
9702 LHSI->getOpcode() == Instruction::Or ||
9703 LHSI->getOpcode() == Instruction::Select))
9704 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9705 Depth);
9706 }
9707 return std::nullopt;
9708}
9709
9710std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9711 const DataLayout &DL,
9712 bool LHSIsTrue, unsigned Depth) {
9713 // LHS ==> RHS by definition
9714 if (LHS == RHS)
9715 return LHSIsTrue;
9716
9717 // Match not
9718 bool InvertRHS = false;
9719 if (match(RHS, m_Not(m_Value(RHS)))) {
9720 if (LHS == RHS)
9721 return !LHSIsTrue;
9722 InvertRHS = true;
9723 }
9724
9725 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9726 if (auto Implied = isImpliedCondition(
9727 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9728 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9729 return InvertRHS ? !*Implied : *Implied;
9730 return std::nullopt;
9731 }
9732 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9733 if (auto Implied = isImpliedCondition(
9734 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9735 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9736 return InvertRHS ? !*Implied : *Implied;
9737 return std::nullopt;
9738 }
9739
9740 const Value *V;
9741 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9742 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9743 ConstantInt::get(V->getType(), 0), DL,
9744 LHSIsTrue, Depth))
9745 return InvertRHS ? !*Implied : *Implied;
9746 return std::nullopt;
9747 }
9748
9750 return std::nullopt;
9751
9752 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9753 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9754 const Value *RHS1, *RHS2;
9755 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9756 if (std::optional<bool> Imp =
9757 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9758 if (*Imp == true)
9759 return !InvertRHS;
9760 if (std::optional<bool> Imp =
9761 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9762 if (*Imp == true)
9763 return !InvertRHS;
9764 }
9765 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9766 if (std::optional<bool> Imp =
9767 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9768 if (*Imp == false)
9769 return InvertRHS;
9770 if (std::optional<bool> Imp =
9771 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9772 if (*Imp == false)
9773 return InvertRHS;
9774 }
9775
9776 return std::nullopt;
9777}
9778
9779// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9780// condition dominating ContextI or nullptr, if no condition is found.
9781static std::pair<Value *, bool>
9783 if (!ContextI || !ContextI->getParent())
9784 return {nullptr, false};
9785
9786 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9787 // dominator tree (eg, from a SimplifyQuery) instead?
9788 const BasicBlock *ContextBB = ContextI->getParent();
9789 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9790 if (!PredBB)
9791 return {nullptr, false};
9792
9793 // We need a conditional branch in the predecessor.
9794 Value *PredCond;
9795 BasicBlock *TrueBB, *FalseBB;
9796 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9797 return {nullptr, false};
9798
9799 // The branch should get simplified. Don't bother simplifying this condition.
9800 if (TrueBB == FalseBB)
9801 return {nullptr, false};
9802
9803 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9804 "Predecessor block does not point to successor?");
9805
9806 // Is this condition implied by the predecessor condition?
9807 return {PredCond, TrueBB == ContextBB};
9808}
9809
9810std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9811 const Instruction *ContextI,
9812 const DataLayout &DL) {
9813 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9814 auto PredCond = getDomPredecessorCondition(ContextI);
9815 if (PredCond.first)
9816 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9817 return std::nullopt;
9818}
9819
9821 const Value *LHS,
9822 const Value *RHS,
9823 const Instruction *ContextI,
9824 const DataLayout &DL) {
9825 auto PredCond = getDomPredecessorCondition(ContextI);
9826 if (PredCond.first)
9827 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9828 PredCond.second);
9829 return std::nullopt;
9830}
9831
9833 APInt &Upper, const InstrInfoQuery &IIQ,
9834 bool PreferSignedRange) {
9835 unsigned Width = Lower.getBitWidth();
9836 const APInt *C;
9837 switch (BO.getOpcode()) {
9838 case Instruction::Sub:
9839 if (match(BO.getOperand(0), m_APInt(C))) {
9840 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9841 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9842
9843 // If the caller expects a signed compare, then try to use a signed range.
9844 // Otherwise if both no-wraps are set, use the unsigned range because it
9845 // is never larger than the signed range. Example:
9846 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9847 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9848 if (PreferSignedRange && HasNSW && HasNUW)
9849 HasNUW = false;
9850
9851 if (HasNUW) {
9852 // 'sub nuw c, x' produces [0, C].
9853 Upper = *C + 1;
9854 } else if (HasNSW) {
9855 if (C->isNegative()) {
9856 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9858 Upper = *C - APInt::getSignedMaxValue(Width);
9859 } else {
9860 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9861 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9862 Lower = *C - APInt::getSignedMaxValue(Width);
9864 }
9865 }
9866 }
9867 break;
9868 case Instruction::Add:
9869 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9870 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9871 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9872
9873 // If the caller expects a signed compare, then try to use a signed
9874 // range. Otherwise if both no-wraps are set, use the unsigned range
9875 // because it is never larger than the signed range. Example: "add nuw
9876 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9877 if (PreferSignedRange && HasNSW && HasNUW)
9878 HasNUW = false;
9879
9880 if (HasNUW) {
9881 // 'add nuw x, C' produces [C, UINT_MAX].
9882 Lower = *C;
9883 } else if (HasNSW) {
9884 if (C->isNegative()) {
9885 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9887 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9888 } else {
9889 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9890 Lower = APInt::getSignedMinValue(Width) + *C;
9891 Upper = APInt::getSignedMaxValue(Width) + 1;
9892 }
9893 }
9894 }
9895 break;
9896
9897 case Instruction::And:
9898 if (match(BO.getOperand(1), m_APInt(C)))
9899 // 'and x, C' produces [0, C].
9900 Upper = *C + 1;
9901 // X & -X is a power of two or zero. So we can cap the value at max power of
9902 // two.
9903 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9904 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9905 Upper = APInt::getSignedMinValue(Width) + 1;
9906 break;
9907
9908 case Instruction::Or:
9909 if (match(BO.getOperand(1), m_APInt(C)))
9910 // 'or x, C' produces [C, UINT_MAX].
9911 Lower = *C;
9912 break;
9913
9914 case Instruction::AShr:
9915 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9916 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9918 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9919 } else if (match(BO.getOperand(0), m_APInt(C))) {
9920 unsigned ShiftAmount = Width - 1;
9921 if (!C->isZero() && IIQ.isExact(&BO))
9922 ShiftAmount = C->countr_zero();
9923 if (C->isNegative()) {
9924 // 'ashr C, x' produces [C, C >> (Width-1)]
9925 Lower = *C;
9926 Upper = C->ashr(ShiftAmount) + 1;
9927 } else {
9928 // 'ashr C, x' produces [C >> (Width-1), C]
9929 Lower = C->ashr(ShiftAmount);
9930 Upper = *C + 1;
9931 }
9932 }
9933 break;
9934
9935 case Instruction::LShr:
9936 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9937 // 'lshr x, C' produces [0, UINT_MAX >> C].
9938 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9939 } else if (match(BO.getOperand(0), m_APInt(C))) {
9940 // 'lshr C, x' produces [C >> (Width-1), C].
9941 unsigned ShiftAmount = Width - 1;
9942 if (!C->isZero() && IIQ.isExact(&BO))
9943 ShiftAmount = C->countr_zero();
9944 Lower = C->lshr(ShiftAmount);
9945 Upper = *C + 1;
9946 }
9947 break;
9948
9949 case Instruction::Shl:
9950 if (match(BO.getOperand(0), m_APInt(C))) {
9951 if (IIQ.hasNoUnsignedWrap(&BO)) {
9952 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9953 Lower = *C;
9954 Upper = Lower.shl(Lower.countl_zero()) + 1;
9955 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9956 if (C->isNegative()) {
9957 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9958 unsigned ShiftAmount = C->countl_one() - 1;
9959 Lower = C->shl(ShiftAmount);
9960 Upper = *C + 1;
9961 } else {
9962 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9963 unsigned ShiftAmount = C->countl_zero() - 1;
9964 Lower = *C;
9965 Upper = C->shl(ShiftAmount) + 1;
9966 }
9967 } else {
9968 // If lowbit is set, value can never be zero.
9969 if ((*C)[0])
9970 Lower = APInt::getOneBitSet(Width, 0);
9971 // If we are shifting a constant the largest it can be is if the longest
9972 // sequence of consecutive ones is shifted to the highbits (breaking
9973 // ties for which sequence is higher). At the moment we take a liberal
9974 // upper bound on this by just popcounting the constant.
9975 // TODO: There may be a bitwise trick for it longest/highest
9976 // consecutative sequence of ones (naive method is O(Width) loop).
9977 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9978 }
9979 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9980 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9981 }
9982 break;
9983
9984 case Instruction::SDiv:
9985 if (match(BO.getOperand(1), m_APInt(C))) {
9986 APInt IntMin = APInt::getSignedMinValue(Width);
9987 APInt IntMax = APInt::getSignedMaxValue(Width);
9988 if (C->isAllOnes()) {
9989 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9990 // where C != -1 and C != 0 and C != 1
9991 Lower = IntMin + 1;
9992 Upper = IntMax + 1;
9993 } else if (C->countl_zero() < Width - 1) {
9994 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9995 // where C != -1 and C != 0 and C != 1
9996 Lower = IntMin.sdiv(*C);
9997 Upper = IntMax.sdiv(*C);
9998 if (Lower.sgt(Upper))
10000 Upper = Upper + 1;
10001 assert(Upper != Lower && "Upper part of range has wrapped!");
10002 }
10003 } else if (match(BO.getOperand(0), m_APInt(C))) {
10004 if (C->isMinSignedValue()) {
10005 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
10006 Lower = *C;
10007 Upper = Lower.lshr(1) + 1;
10008 } else {
10009 // 'sdiv C, x' produces [-|C|, |C|].
10010 Upper = C->abs() + 1;
10011 Lower = (-Upper) + 1;
10012 }
10013 }
10014 break;
10015
10016 case Instruction::UDiv:
10017 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
10018 // 'udiv x, C' produces [0, UINT_MAX / C].
10019 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
10020 } else if (match(BO.getOperand(0), m_APInt(C))) {
10021 // 'udiv C, x' produces [0, C].
10022 Upper = *C + 1;
10023 }
10024 break;
10025
10026 case Instruction::SRem:
10027 if (match(BO.getOperand(1), m_APInt(C))) {
10028 // 'srem x, C' produces (-|C|, |C|).
10029 Upper = C->abs();
10030 Lower = (-Upper) + 1;
10031 } else if (match(BO.getOperand(0), m_APInt(C))) {
10032 if (C->isNegative()) {
10033 // 'srem -|C|, x' produces [-|C|, 0].
10034 Upper = 1;
10035 Lower = *C;
10036 } else {
10037 // 'srem |C|, x' produces [0, |C|].
10038 Upper = *C + 1;
10039 }
10040 }
10041 break;
10042
10043 case Instruction::URem:
10044 if (match(BO.getOperand(1), m_APInt(C)))
10045 // 'urem x, C' produces [0, C).
10046 Upper = *C;
10047 else if (match(BO.getOperand(0), m_APInt(C)))
10048 // 'urem C, x' produces [0, C].
10049 Upper = *C + 1;
10050 break;
10051
10052 default:
10053 break;
10054 }
10055}
10056
10058 bool UseInstrInfo) {
10059 unsigned Width = II.getType()->getScalarSizeInBits();
10060 const APInt *C;
10061 switch (II.getIntrinsicID()) {
10062 case Intrinsic::ctlz:
10063 case Intrinsic::cttz: {
10064 APInt Upper(Width, Width);
10065 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
10066 Upper += 1;
10067 // Maximum of set/clear bits is the bit width.
10069 }
10070 case Intrinsic::ctpop:
10071 // Maximum of set/clear bits is the bit width.
10073 APInt(Width, Width) + 1);
10074 case Intrinsic::uadd_sat:
10075 // uadd.sat(x, C) produces [C, UINT_MAX].
10076 if (match(II.getOperand(0), m_APInt(C)) ||
10077 match(II.getOperand(1), m_APInt(C)))
10079 break;
10080 case Intrinsic::sadd_sat:
10081 if (match(II.getOperand(0), m_APInt(C)) ||
10082 match(II.getOperand(1), m_APInt(C))) {
10083 if (C->isNegative())
10084 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10086 APInt::getSignedMaxValue(Width) + *C +
10087 1);
10088
10089 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10091 APInt::getSignedMaxValue(Width) + 1);
10092 }
10093 break;
10094 case Intrinsic::usub_sat:
10095 // usub.sat(C, x) produces [0, C].
10096 if (match(II.getOperand(0), m_APInt(C)))
10097 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10098
10099 // usub.sat(x, C) produces [0, UINT_MAX - C].
10100 if (match(II.getOperand(1), m_APInt(C)))
10102 APInt::getMaxValue(Width) - *C + 1);
10103 break;
10104 case Intrinsic::ssub_sat:
10105 if (match(II.getOperand(0), m_APInt(C))) {
10106 if (C->isNegative())
10107 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10109 *C - APInt::getSignedMinValue(Width) +
10110 1);
10111
10112 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10114 APInt::getSignedMaxValue(Width) + 1);
10115 } else if (match(II.getOperand(1), m_APInt(C))) {
10116 if (C->isNegative())
10117 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10119 APInt::getSignedMaxValue(Width) + 1);
10120
10121 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10123 APInt::getSignedMaxValue(Width) - *C +
10124 1);
10125 }
10126 break;
10127 case Intrinsic::umin:
10128 case Intrinsic::umax:
10129 case Intrinsic::smin:
10130 case Intrinsic::smax:
10131 if (!match(II.getOperand(0), m_APInt(C)) &&
10132 !match(II.getOperand(1), m_APInt(C)))
10133 break;
10134
10135 switch (II.getIntrinsicID()) {
10136 case Intrinsic::umin:
10137 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10138 case Intrinsic::umax:
10140 case Intrinsic::smin:
10142 *C + 1);
10143 case Intrinsic::smax:
10145 APInt::getSignedMaxValue(Width) + 1);
10146 default:
10147 llvm_unreachable("Must be min/max intrinsic");
10148 }
10149 break;
10150 case Intrinsic::abs:
10151 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10152 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10153 if (match(II.getOperand(1), m_One()))
10155 APInt::getSignedMaxValue(Width) + 1);
10156
10158 APInt::getSignedMinValue(Width) + 1);
10159 case Intrinsic::vscale:
10160 if (!II.getParent() || !II.getFunction())
10161 break;
10162 return getVScaleRange(II.getFunction(), Width);
10163 default:
10164 break;
10165 }
10166
10167 return ConstantRange::getFull(Width);
10168}
10169
10171 const InstrInfoQuery &IIQ) {
10172 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10173 const Value *LHS = nullptr, *RHS = nullptr;
10175 if (R.Flavor == SPF_UNKNOWN)
10176 return ConstantRange::getFull(BitWidth);
10177
10178 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10179 // If the negation part of the abs (in RHS) has the NSW flag,
10180 // then the result of abs(X) is [0..SIGNED_MAX],
10181 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10182 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10186
10189 }
10190
10191 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10192 // The result of -abs(X) is <= 0.
10194 APInt(BitWidth, 1));
10195 }
10196
10197 const APInt *C;
10198 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10199 return ConstantRange::getFull(BitWidth);
10200
10201 switch (R.Flavor) {
10202 case SPF_UMIN:
10204 case SPF_UMAX:
10206 case SPF_SMIN:
10208 *C + 1);
10209 case SPF_SMAX:
10212 default:
10213 return ConstantRange::getFull(BitWidth);
10214 }
10215}
10216
10218 // The maximum representable value of a half is 65504. For floats the maximum
10219 // value is 3.4e38 which requires roughly 129 bits.
10220 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10221 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10222 return;
10223 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10224 Lower = APInt(BitWidth, -65504, true);
10225 Upper = APInt(BitWidth, 65505);
10226 }
10227
10228 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10229 // For a fptoui the lower limit is left as 0.
10230 Upper = APInt(BitWidth, 65505);
10231 }
10232}
10233
10235 bool UseInstrInfo, AssumptionCache *AC,
10236 const Instruction *CtxI,
10237 const DominatorTree *DT,
10238 unsigned Depth) {
10239 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10240
10242 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10243
10244 if (auto *C = dyn_cast<Constant>(V))
10245 return C->toConstantRange();
10246
10247 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10248 InstrInfoQuery IIQ(UseInstrInfo);
10249 ConstantRange CR = ConstantRange::getFull(BitWidth);
10250 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10251 APInt Lower = APInt(BitWidth, 0);
10252 APInt Upper = APInt(BitWidth, 0);
10253 // TODO: Return ConstantRange.
10254 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10256 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10257 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10258 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10260 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10262 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10263 CR = CRTrue.unionWith(CRFalse);
10265 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10266 APInt Lower = APInt(BitWidth, 0);
10267 APInt Upper = APInt(BitWidth, 0);
10268 // TODO: Return ConstantRange.
10271 } else if (const auto *A = dyn_cast<Argument>(V))
10272 if (std::optional<ConstantRange> Range = A->getRange())
10273 CR = *Range;
10274
10275 if (auto *I = dyn_cast<Instruction>(V)) {
10276 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10278
10279 if (const auto *CB = dyn_cast<CallBase>(V))
10280 if (std::optional<ConstantRange> Range = CB->getRange())
10281 CR = CR.intersectWith(*Range);
10282 }
10283
10284 if (CtxI && AC) {
10285 // Try to restrict the range based on information from assumptions.
10286 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10287 if (!AssumeVH)
10288 continue;
10289 CallInst *I = cast<CallInst>(AssumeVH);
10290 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10291 "Got assumption for the wrong function!");
10292 assert(I->getIntrinsicID() == Intrinsic::assume &&
10293 "must be an assume intrinsic");
10294
10295 if (!isValidAssumeForContext(I, CtxI, DT))
10296 continue;
10297 Value *Arg = I->getArgOperand(0);
10298 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10299 // Currently we just use information from comparisons.
10300 if (!Cmp || Cmp->getOperand(0) != V)
10301 continue;
10302 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10303 ConstantRange RHS =
10304 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10305 UseInstrInfo, AC, I, DT, Depth + 1);
10306 CR = CR.intersectWith(
10307 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10308 }
10309 }
10310
10311 return CR;
10312}
10313
10314static void
10316 function_ref<void(Value *)> InsertAffected) {
10317 assert(V != nullptr);
10318 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10319 InsertAffected(V);
10320 } else if (auto *I = dyn_cast<Instruction>(V)) {
10321 InsertAffected(V);
10322
10323 // Peek through unary operators to find the source of the condition.
10324 Value *Op;
10327 InsertAffected(Op);
10328 }
10329 }
10330}
10331
10333 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10334 auto AddAffected = [&InsertAffected](Value *V) {
10335 addValueAffectedByCondition(V, InsertAffected);
10336 };
10337
10338 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10339 if (IsAssume) {
10340 AddAffected(LHS);
10341 AddAffected(RHS);
10342 } else if (match(RHS, m_Constant()))
10343 AddAffected(LHS);
10344 };
10345
10346 SmallVector<Value *, 8> Worklist;
10348 Worklist.push_back(Cond);
10349 while (!Worklist.empty()) {
10350 Value *V = Worklist.pop_back_val();
10351 if (!Visited.insert(V).second)
10352 continue;
10353
10354 CmpPredicate Pred;
10355 Value *A, *B, *X;
10356
10357 if (IsAssume) {
10358 AddAffected(V);
10359 if (match(V, m_Not(m_Value(X))))
10360 AddAffected(X);
10361 }
10362
10363 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10364 // assume(A && B) is split to -> assume(A); assume(B);
10365 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10366 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10367 // enough information to be worth handling (intersection of information as
10368 // opposed to union).
10369 if (!IsAssume) {
10370 Worklist.push_back(A);
10371 Worklist.push_back(B);
10372 }
10373 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10374 bool HasRHSC = match(B, m_ConstantInt());
10375 if (ICmpInst::isEquality(Pred)) {
10376 AddAffected(A);
10377 if (IsAssume)
10378 AddAffected(B);
10379 if (HasRHSC) {
10380 Value *Y;
10381 // (X << C) or (X >>_s C) or (X >>_u C).
10382 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10383 AddAffected(X);
10384 // (X & C) or (X | C).
10385 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10386 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10387 AddAffected(X);
10388 AddAffected(Y);
10389 }
10390 // X - Y
10391 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10392 AddAffected(X);
10393 AddAffected(Y);
10394 }
10395 }
10396 } else {
10397 AddCmpOperands(A, B);
10398 if (HasRHSC) {
10399 // Handle (A + C1) u< C2, which is the canonical form of
10400 // A > C3 && A < C4.
10402 AddAffected(X);
10403
10404 if (ICmpInst::isUnsigned(Pred)) {
10405 Value *Y;
10406 // X & Y u> C -> X >u C && Y >u C
10407 // X | Y u< C -> X u< C && Y u< C
10408 // X nuw+ Y u< C -> X u< C && Y u< C
10409 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10410 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10411 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10412 AddAffected(X);
10413 AddAffected(Y);
10414 }
10415 // X nuw- Y u> C -> X u> C
10416 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10417 AddAffected(X);
10418 }
10419 }
10420
10421 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10422 // by computeKnownFPClass().
10424 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10425 InsertAffected(X);
10426 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10427 InsertAffected(X);
10428 }
10429 }
10430
10431 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10432 AddAffected(X);
10433 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10434 AddCmpOperands(A, B);
10435
10436 // fcmp fneg(x), y
10437 // fcmp fabs(x), y
10438 // fcmp fneg(fabs(x)), y
10439 if (match(A, m_FNeg(m_Value(A))))
10440 AddAffected(A);
10441 if (match(A, m_FAbs(m_Value(A))))
10442 AddAffected(A);
10443
10445 m_Value()))) {
10446 // Handle patterns that computeKnownFPClass() support.
10447 AddAffected(A);
10448 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10449 // Assume is checked here as X is already added above for assumes in
10450 // addValueAffectedByCondition
10451 AddAffected(X);
10452 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10453 // Assume is checked here to avoid issues with ephemeral values
10454 Worklist.push_back(X);
10455 }
10456 }
10457}
10458
10460 // (X >> C) or/add (X & mask(C) != 0)
10461 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10462 if (BO->getOpcode() == Instruction::Add ||
10463 BO->getOpcode() == Instruction::Or) {
10464 const Value *X;
10465 const APInt *C1, *C2;
10466 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10470 m_Zero())))) &&
10471 C2->popcount() == C1->getZExtValue())
10472 return X;
10473 }
10474 }
10475 return nullptr;
10476}
10477
10479 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10480}
10481
10484 unsigned MaxCount, bool AllowUndefOrPoison) {
10487 auto Push = [&](const Value *V) -> bool {
10488 if (auto *C = dyn_cast<Constant>(V)) {
10489 if (!AllowUndefOrPoison && !isGuaranteedNotToBeUndefOrPoison(C))
10490 return false;
10491 // Check existence first to avoid unnecessary allocations.
10492 if (Constants.contains(C))
10493 return true;
10494 if (Constants.size() == MaxCount)
10495 return false;
10496 Constants.insert(C);
10497 return true;
10498 }
10499
10500 if (auto *Inst = dyn_cast<Instruction>(V)) {
10501 if (Visited.insert(Inst).second)
10502 Worklist.push_back(Inst);
10503 return true;
10504 }
10505 return false;
10506 };
10507 if (!Push(V))
10508 return false;
10509 while (!Worklist.empty()) {
10510 const Instruction *CurInst = Worklist.pop_back_val();
10511 switch (CurInst->getOpcode()) {
10512 case Instruction::Select:
10513 if (!Push(CurInst->getOperand(1)))
10514 return false;
10515 if (!Push(CurInst->getOperand(2)))
10516 return false;
10517 break;
10518 case Instruction::PHI:
10519 for (Value *IncomingValue : cast<PHINode>(CurInst)->incoming_values()) {
10520 // Fast path for recurrence PHI.
10521 if (IncomingValue == CurInst)
10522 continue;
10523 if (!Push(IncomingValue))
10524 return false;
10525 }
10526 break;
10527 default:
10528 return false;
10529 }
10530 }
10531 return true;
10532}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Utilities for dealing with flags related to floating point properties and mode controls.
static Value * getCondition(Instruction *I)
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static SmallVector< VPValue *, 4 > getOperands(ArrayRef< VPValue * > Values, unsigned OperandIndex)
Definition VPlanSLP.cpp:210
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext, unsigned Depth=0)
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, SimplifyQuery &Q, unsigned Depth)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
static unsigned computeNumSignBitsVectorConstant(const Value *V, const APInt &DemandedElts, unsigned TyBits)
For vector constants, loop over the elements and find the constant with the minimum number of sign bi...
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V1 == (binop V2, X), where X is known non-zero.
static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q, unsigned Depth)
Test whether a GEP's result is known to be non-null.
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static constexpr unsigned MaxInstrsToCheckForFree
Maximum number of instructions to check between assume and context instruction.
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
static std::optional< bool > isImpliedCondFCmps(FCmpInst::Predicate LPred, const Value *L0, const Value *L1, FCmpInst::Predicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
UndefPoisonKind
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static bool includesPoison(UndefPoisonKind Kind)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool includesUndef(UndefPoisonKind Kind)
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ?
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static void computeKnownBitsFromLerpPattern(const Value *Op0, const Value *Op1, const APInt &DemandedElts, KnownBits &KnownOut, const SimplifyQuery &Q, unsigned Depth)
Try to detect the lerp pattern: a * (b - c) + c * d where a >= 0, b >= 0, c >= 0, d >= 0,...
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:290
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:340
bool isFinite() const
Definition APFloat.h:1436
bool isNaN() const
Definition APFloat.h:1429
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1120
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1080
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1061
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt umul_ov(const APInt &RHS, bool &Overflow) const
Definition APInt.cpp:1971
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:235
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1407
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:424
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1541
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1392
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1671
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1386
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:207
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1331
unsigned ceilLogBase2() const
Definition APInt.h:1765
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1202
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:372
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1183
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:381
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1489
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1112
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:210
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:217
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:330
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1250
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1397
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1167
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1629
unsigned countl_zero() const
The APInt version of std::countl_zero.
Definition APInt.h:1599
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:220
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1041
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:357
unsigned logBase2() const
Definition APInt.h:1762
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:828
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:472
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:406
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:335
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1151
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:874
bool isSubsetOf(const APInt &RHS) const
This operation checks that all bits set in this APInt are also set in RHS.
Definition APInt.h:1258
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1131
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:297
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:201
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1389
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1238
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:287
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:852
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1222
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1450
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
iterator end() const
Definition ArrayRef.h:132
size_t size() const
size - Get the array size.
Definition ArrayRef.h:143
iterator begin() const
Definition ArrayRef.h:131
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:138
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:187
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:223
LLVM_ABI bool isSingleEdge() const
Check if this is the only edge between Start and End.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:472
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:374
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
This class is the base class for the comparison instructions.
Definition InstrTypes.h:664
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:676
@ ICMP_SLT
signed less than
Definition InstrTypes.h:705
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:706
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:682
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:691
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:680
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:681
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:700
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:699
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:703
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:690
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:701
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:688
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:683
@ ICMP_NE
not equal
Definition InstrTypes.h:698
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:704
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:702
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:689
bool isSigned() const
Definition InstrTypes.h:930
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:827
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:942
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:770
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:789
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:765
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:893
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:776
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:936
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:702
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:593
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:668
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:776
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:277
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:163
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:63
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:207
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:760
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:165
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
This instruction compares its operands according to the predicate given to the constructor.
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:200
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:22
bool noSignedZeros() const
Definition FMF.h:67
bool noInfs() const
Definition FMF.h:66
void setNoSignedZeros(bool B=true)
Definition FMF.h:84
void setNoNaNs(bool B=true)
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:65
const BasicBlock & getEntryBlock() const
Definition Function.h:807
bool hasNoSync() const
Determine if the call can synchroize with other threads.
Definition Function.h:637
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition Function.cpp:803
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.
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:712
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:743
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:298
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:297
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
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:295
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:296
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:231
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:301
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:107
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:292
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
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:201
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
A range adaptor for a pair of iterators.
CallInst * Call
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define UINT64_MAX
Definition DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3009
const APInt & umax(const APInt &A, const APInt &B)
Determine the larger of two APInts considered to be unsigned.
Definition APInt.h:2264
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
bind_ty< Instruction > m_Instruction(Instruction *&I)
Match an instruction, capturing it if we match.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
BinaryOp_match< LHS, RHS, Instruction::Mul, true > m_c_Mul(const LHS &L, const RHS &R)
Matches a Mul with LHS and RHS in either order.
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
MatchFunctor< Val, Pattern > match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI)
Returns true, if no instruction between Assume and CtxI may free memory and the function is marked as...
@ Offset
Definition DWP.cpp:477
@ Length
Definition DWP.cpp:477
@ 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:1725
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:1655
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.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition ScopeExit.h:59
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:2472
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:2136
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:1732
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:331
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
generic_gep_type_iterator<> gep_type_iterator
LLVM_ABI bool collectPossibleValues(const Value *V, SmallPtrSetImpl< const Constant * > &Constants, unsigned MaxCount, bool AllowUndefOrPoison=true)
Enumerates all possible 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:71
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:1897
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:869
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
constexpr bool outputsAreZero() const
Return true if output denormals should be flushed to 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ IEEE
IEEE-754 denormal numbers preserved.
constexpr bool inputsAreZero() const
Return true if input denormals must be implicitly treated as 0.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getIEEE()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:301
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static LLVM_ABI std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:186
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:255
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:124
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:251
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:242
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:274
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
Definition KnownBits.h:119
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:161
KnownBits byteSwap() const
Definition KnownBits.h:514
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:289
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:518
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:172
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:321
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:225
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:311
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:180
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:245
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:347
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:196
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:248
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
APInt getMinValue() const
Return the minimal unsigned value possible given these KnownBits.
Definition KnownBits.h:129
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:326
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:353
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:280
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:92
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:219
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:167
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:206
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a zero.
bool isUnknown() const
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
LLVM_ABI void propagateCanonicalizingSrc(const KnownFPClass &Src, DenormalMode Mode)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void signBitMustBeZero()
Assume the sign bit is zero.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a negative zero.
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC