LLVM 19.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"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/ScopeExit.h"
21#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/StringRef.h"
32#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"
43#include "llvm/IR/Constants.h"
46#include "llvm/IR/Dominators.h"
48#include "llvm/IR/Function.h"
50#include "llvm/IR/GlobalAlias.h"
51#include "llvm/IR/GlobalValue.h"
53#include "llvm/IR/InstrTypes.h"
54#include "llvm/IR/Instruction.h"
57#include "llvm/IR/Intrinsics.h"
58#include "llvm/IR/IntrinsicsAArch64.h"
59#include "llvm/IR/IntrinsicsAMDGPU.h"
60#include "llvm/IR/IntrinsicsRISCV.h"
61#include "llvm/IR/IntrinsicsX86.h"
62#include "llvm/IR/LLVMContext.h"
63#include "llvm/IR/Metadata.h"
64#include "llvm/IR/Module.h"
65#include "llvm/IR/Operator.h"
67#include "llvm/IR/Type.h"
68#include "llvm/IR/User.h"
69#include "llvm/IR/Value.h"
77#include <algorithm>
78#include <cassert>
79#include <cstdint>
80#include <optional>
81#include <utility>
82
83using namespace llvm;
84using namespace llvm::PatternMatch;
85
86// Controls the number of uses of the value searched for possible
87// dominating comparisons.
88static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
89 cl::Hidden, cl::init(20));
90
91
92/// Returns the bitwidth of the given scalar or pointer type. For vector types,
93/// returns the element type's bitwidth.
94static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
95 if (unsigned BitWidth = Ty->getScalarSizeInBits())
96 return BitWidth;
97
98 return DL.getPointerTypeSizeInBits(Ty);
99}
100
101// Given the provided Value and, potentially, a context instruction, return
102// the preferred context instruction (if any).
103static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
104 // If we've been provided with a context instruction, then use that (provided
105 // it has been inserted).
106 if (CxtI && CxtI->getParent())
107 return CxtI;
108
109 // If the value is really an already-inserted instruction, then use that.
110 CxtI = dyn_cast<Instruction>(V);
111 if (CxtI && CxtI->getParent())
112 return CxtI;
113
114 return nullptr;
115}
116
117static const Instruction *safeCxtI(const Value *V1, const Value *V2, const Instruction *CxtI) {
118 // If we've been provided with a context instruction, then use that (provided
119 // it has been inserted).
120 if (CxtI && CxtI->getParent())
121 return CxtI;
122
123 // If the value is really an already-inserted instruction, then use that.
124 CxtI = dyn_cast<Instruction>(V1);
125 if (CxtI && CxtI->getParent())
126 return CxtI;
127
128 CxtI = dyn_cast<Instruction>(V2);
129 if (CxtI && CxtI->getParent())
130 return CxtI;
131
132 return nullptr;
133}
134
136 const APInt &DemandedElts,
137 APInt &DemandedLHS, APInt &DemandedRHS) {
138 if (isa<ScalableVectorType>(Shuf->getType())) {
139 assert(DemandedElts == APInt(1,1));
140 DemandedLHS = DemandedRHS = DemandedElts;
141 return true;
142 }
143
144 int NumElts =
145 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
146 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
147 DemandedElts, DemandedLHS, DemandedRHS);
148}
149
150static void computeKnownBits(const Value *V, const APInt &DemandedElts,
151 KnownBits &Known, unsigned Depth,
152 const SimplifyQuery &Q);
153
154void llvm::computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
155 const SimplifyQuery &Q) {
156 // Since the number of lanes in a scalable vector is unknown at compile time,
157 // we track one bit which is implicitly broadcast to all lanes. This means
158 // that all lanes in a scalable vector are considered demanded.
159 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
160 APInt DemandedElts =
161 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
162 ::computeKnownBits(V, DemandedElts, Known, Depth, Q);
163}
164
166 const DataLayout &DL, unsigned Depth,
167 AssumptionCache *AC, const Instruction *CxtI,
168 const DominatorTree *DT, bool UseInstrInfo) {
170 V, Known, Depth,
171 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
172}
173
175 unsigned Depth, AssumptionCache *AC,
176 const Instruction *CxtI,
177 const DominatorTree *DT, bool UseInstrInfo) {
178 return computeKnownBits(
179 V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
180}
181
182KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
183 const DataLayout &DL, unsigned Depth,
184 AssumptionCache *AC, const Instruction *CxtI,
185 const DominatorTree *DT, bool UseInstrInfo) {
186 return computeKnownBits(
187 V, DemandedElts, Depth,
188 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
189}
190
191static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS,
192 const SimplifyQuery &SQ) {
193 // Look for an inverted mask: (X & ~M) op (Y & M).
194 {
195 Value *M;
196 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
198 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
199 return true;
200 }
201
202 // X op (Y & ~X)
205 return true;
206
207 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
208 // for constant Y.
209 Value *Y;
210 if (match(RHS,
212 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
213 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
214 return true;
215
216 // Peek through extends to find a 'not' of the other side:
217 // (ext Y) op ext(~Y)
218 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
220 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
221 return true;
222
223 // Look for: (A & B) op ~(A | B)
224 {
225 Value *A, *B;
226 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
228 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
229 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
230 return true;
231 }
232
233 return false;
234}
235
237 const WithCache<const Value *> &RHSCache,
238 const SimplifyQuery &SQ) {
239 const Value *LHS = LHSCache.getValue();
240 const Value *RHS = RHSCache.getValue();
241
242 assert(LHS->getType() == RHS->getType() &&
243 "LHS and RHS should have the same type");
245 "LHS and RHS should be integers");
246
249 return true;
250
252 RHSCache.getKnownBits(SQ));
253}
254
256 return !I->user_empty() && all_of(I->users(), [](const User *U) {
257 ICmpInst::Predicate P;
258 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
259 });
260}
261
262static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
263 const SimplifyQuery &Q);
264
266 bool OrZero, unsigned Depth,
267 AssumptionCache *AC, const Instruction *CxtI,
268 const DominatorTree *DT, bool UseInstrInfo) {
269 return ::isKnownToBeAPowerOfTwo(
270 V, OrZero, Depth,
271 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
272}
273
274static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
275 const SimplifyQuery &Q, unsigned Depth);
276
278 unsigned Depth) {
279 return computeKnownBits(V, Depth, SQ).isNonNegative();
280}
281
283 unsigned Depth) {
284 if (auto *CI = dyn_cast<ConstantInt>(V))
285 return CI->getValue().isStrictlyPositive();
286
287 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
288 // this updated.
289 KnownBits Known = computeKnownBits(V, Depth, SQ);
290 return Known.isNonNegative() &&
291 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
292}
293
295 unsigned Depth) {
296 return computeKnownBits(V, Depth, SQ).isNegative();
297}
298
299static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
300 const SimplifyQuery &Q);
301
302bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
303 const DataLayout &DL, AssumptionCache *AC,
304 const Instruction *CxtI, const DominatorTree *DT,
305 bool UseInstrInfo) {
306 return ::isKnownNonEqual(
307 V1, V2, 0,
308 SimplifyQuery(DL, DT, AC, safeCxtI(V2, V1, CxtI), UseInstrInfo));
309}
310
311bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
312 const SimplifyQuery &SQ, unsigned Depth) {
313 KnownBits Known(Mask.getBitWidth());
314 computeKnownBits(V, Known, Depth, SQ);
315 return Mask.isSubsetOf(Known.Zero);
316}
317
318static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
319 unsigned Depth, const SimplifyQuery &Q);
320
321static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
322 const SimplifyQuery &Q) {
323 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
324 APInt DemandedElts =
325 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
326 return ComputeNumSignBits(V, DemandedElts, Depth, Q);
327}
328
329unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
330 unsigned Depth, AssumptionCache *AC,
331 const Instruction *CxtI,
332 const DominatorTree *DT, bool UseInstrInfo) {
333 return ::ComputeNumSignBits(
334 V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
335}
336
338 unsigned Depth, AssumptionCache *AC,
339 const Instruction *CxtI,
340 const DominatorTree *DT) {
341 unsigned SignBits = ComputeNumSignBits(V, DL, Depth, AC, CxtI, DT);
342 return V->getType()->getScalarSizeInBits() - SignBits + 1;
343}
344
345static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
346 bool NSW, bool NUW,
347 const APInt &DemandedElts,
348 KnownBits &KnownOut, KnownBits &Known2,
349 unsigned Depth, const SimplifyQuery &Q) {
350 computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q);
351
352 // If one operand is unknown and we have no nowrap information,
353 // the result will be unknown independently of the second operand.
354 if (KnownOut.isUnknown() && !NSW && !NUW)
355 return;
356
357 computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
358 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
359}
360
361static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
362 const APInt &DemandedElts, KnownBits &Known,
363 KnownBits &Known2, unsigned Depth,
364 const SimplifyQuery &Q) {
365 computeKnownBits(Op1, DemandedElts, Known, Depth + 1, Q);
366 computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
367
368 bool isKnownNegative = false;
369 bool isKnownNonNegative = false;
370 // If the multiplication is known not to overflow, compute the sign bit.
371 if (NSW) {
372 if (Op0 == Op1) {
373 // The product of a number with itself is non-negative.
374 isKnownNonNegative = true;
375 } else {
376 bool isKnownNonNegativeOp1 = Known.isNonNegative();
377 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
378 bool isKnownNegativeOp1 = Known.isNegative();
379 bool isKnownNegativeOp0 = Known2.isNegative();
380 // The product of two numbers with the same sign is non-negative.
381 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
382 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
383 // The product of a negative number and a non-negative number is either
384 // negative or zero.
387 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
388 Known2.isNonZero()) ||
389 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
390 }
391 }
392
393 bool SelfMultiply = Op0 == Op1;
394 if (SelfMultiply)
395 SelfMultiply &=
396 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
397 Known = KnownBits::mul(Known, Known2, SelfMultiply);
398
399 // Only make use of no-wrap flags if we failed to compute the sign bit
400 // directly. This matters if the multiplication always overflows, in
401 // which case we prefer to follow the result of the direct computation,
402 // though as the program is invoking undefined behaviour we can choose
403 // whatever we like here.
404 if (isKnownNonNegative && !Known.isNegative())
405 Known.makeNonNegative();
406 else if (isKnownNegative && !Known.isNonNegative())
407 Known.makeNegative();
408}
409
411 KnownBits &Known) {
412 unsigned BitWidth = Known.getBitWidth();
413 unsigned NumRanges = Ranges.getNumOperands() / 2;
414 assert(NumRanges >= 1);
415
416 Known.Zero.setAllBits();
417 Known.One.setAllBits();
418
419 for (unsigned i = 0; i < NumRanges; ++i) {
421 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
423 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
424 ConstantRange Range(Lower->getValue(), Upper->getValue());
425
426 // The first CommonPrefixBits of all values in Range are equal.
427 unsigned CommonPrefixBits =
428 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
429 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
430 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
431 Known.One &= UnsignedMax & Mask;
432 Known.Zero &= ~UnsignedMax & Mask;
433 }
434}
435
436static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
440
441 // The instruction defining an assumption's condition itself is always
442 // considered ephemeral to that assumption (even if it has other
443 // non-ephemeral users). See r246696's test case for an example.
444 if (is_contained(I->operands(), E))
445 return true;
446
447 while (!WorkSet.empty()) {
448 const Value *V = WorkSet.pop_back_val();
449 if (!Visited.insert(V).second)
450 continue;
451
452 // If all uses of this value are ephemeral, then so is this value.
453 if (llvm::all_of(V->users(), [&](const User *U) {
454 return EphValues.count(U);
455 })) {
456 if (V == E)
457 return true;
458
459 if (V == I || (isa<Instruction>(V) &&
460 !cast<Instruction>(V)->mayHaveSideEffects() &&
461 !cast<Instruction>(V)->isTerminator())) {
462 EphValues.insert(V);
463 if (const User *U = dyn_cast<User>(V))
464 append_range(WorkSet, U->operands());
465 }
466 }
467 }
468
469 return false;
470}
471
472// Is this an intrinsic that cannot be speculated but also cannot trap?
474 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
475 return CI->isAssumeLikeIntrinsic();
476
477 return false;
478}
479
481 const Instruction *CxtI,
482 const DominatorTree *DT,
483 bool AllowEphemerals) {
484 // There are two restrictions on the use of an assume:
485 // 1. The assume must dominate the context (or the control flow must
486 // reach the assume whenever it reaches the context).
487 // 2. The context must not be in the assume's set of ephemeral values
488 // (otherwise we will use the assume to prove that the condition
489 // feeding the assume is trivially true, thus causing the removal of
490 // the assume).
491
492 if (Inv->getParent() == CxtI->getParent()) {
493 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
494 // in the BB.
495 if (Inv->comesBefore(CxtI))
496 return true;
497
498 // Don't let an assume affect itself - this would cause the problems
499 // `isEphemeralValueOf` is trying to prevent, and it would also make
500 // the loop below go out of bounds.
501 if (!AllowEphemerals && Inv == CxtI)
502 return false;
503
504 // The context comes first, but they're both in the same block.
505 // Make sure there is nothing in between that might interrupt
506 // the control flow, not even CxtI itself.
507 // We limit the scan distance between the assume and its context instruction
508 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
509 // it can be adjusted if needed (could be turned into a cl::opt).
510 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
512 return false;
513
514 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
515 }
516
517 // Inv and CxtI are in different blocks.
518 if (DT) {
519 if (DT->dominates(Inv, CxtI))
520 return true;
521 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) {
522 // We don't have a DT, but this trivially dominates.
523 return true;
524 }
525
526 return false;
527}
528
529// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
530// we still have enough information about `RHS` to conclude non-zero. For
531// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
532// so the extra compile time may not be worth it, but possibly a second API
533// should be created for use outside of loops.
534static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
535 // v u> y implies v != 0.
536 if (Pred == ICmpInst::ICMP_UGT)
537 return true;
538
539 // Special-case v != 0 to also handle v != null.
540 if (Pred == ICmpInst::ICMP_NE)
541 return match(RHS, m_Zero());
542
543 // All other predicates - rely on generic ConstantRange handling.
544 const APInt *C;
546 if (match(RHS, m_APInt(C))) {
548 return !TrueValues.contains(Zero);
549 }
550
551 auto *VC = dyn_cast<ConstantDataVector>(RHS);
552 if (VC == nullptr)
553 return false;
554
555 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
556 ++ElemIdx) {
558 Pred, VC->getElementAsAPInt(ElemIdx));
559 if (TrueValues.contains(Zero))
560 return false;
561 }
562 return true;
563}
564
565static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
566 // Use of assumptions is context-sensitive. If we don't have a context, we
567 // cannot use them!
568 if (!Q.AC || !Q.CxtI)
569 return false;
570
571 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
572 if (!Elem.Assume)
573 continue;
574
575 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
576 assert(I->getFunction() == Q.CxtI->getFunction() &&
577 "Got assumption for the wrong function!");
578
579 if (Elem.Index != AssumptionCache::ExprResultIdx) {
580 if (!V->getType()->isPointerTy())
581 continue;
583 *I, I->bundle_op_info_begin()[Elem.Index])) {
584 if (RK.WasOn == V &&
585 (RK.AttrKind == Attribute::NonNull ||
586 (RK.AttrKind == Attribute::Dereferenceable &&
588 V->getType()->getPointerAddressSpace()))) &&
590 return true;
591 }
592 continue;
593 }
594
595 // Warning: This loop can end up being somewhat performance sensitive.
596 // We're running this loop for once for each value queried resulting in a
597 // runtime of ~O(#assumes * #values).
598
599 Value *RHS;
601 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
602 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
603 return false;
604
605 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
606 return true;
607 }
608
609 return false;
610}
611
613 Value *LHS, Value *RHS, KnownBits &Known,
614 const SimplifyQuery &Q) {
615 if (RHS->getType()->isPointerTy()) {
616 // Handle comparison of pointer to null explicitly, as it will not be
617 // covered by the m_APInt() logic below.
618 if (LHS == V && match(RHS, m_Zero())) {
619 switch (Pred) {
620 case ICmpInst::ICMP_EQ:
621 Known.setAllZero();
622 break;
623 case ICmpInst::ICMP_SGE:
624 case ICmpInst::ICMP_SGT:
625 Known.makeNonNegative();
626 break;
627 case ICmpInst::ICMP_SLT:
628 Known.makeNegative();
629 break;
630 default:
631 break;
632 }
633 }
634 return;
635 }
636
637 unsigned BitWidth = Known.getBitWidth();
638 auto m_V =
640
641 Value *Y;
642 const APInt *Mask, *C;
643 uint64_t ShAmt;
644 switch (Pred) {
645 case ICmpInst::ICMP_EQ:
646 // assume(V = C)
647 if (match(LHS, m_V) && match(RHS, m_APInt(C))) {
648 Known = Known.unionWith(KnownBits::makeConstant(*C));
649 // assume(V & Mask = C)
650 } else if (match(LHS, m_c_And(m_V, m_Value(Y))) &&
651 match(RHS, m_APInt(C))) {
652 // For one bits in Mask, we can propagate bits from C to V.
653 Known.One |= *C;
654 if (match(Y, m_APInt(Mask)))
655 Known.Zero |= ~*C & *Mask;
656 // assume(V | Mask = C)
657 } else if (match(LHS, m_c_Or(m_V, m_Value(Y))) && match(RHS, m_APInt(C))) {
658 // For zero bits in Mask, we can propagate bits from C to V.
659 Known.Zero |= ~*C;
660 if (match(Y, m_APInt(Mask)))
661 Known.One |= *C & ~*Mask;
662 // assume(V ^ Mask = C)
663 } else if (match(LHS, m_Xor(m_V, m_APInt(Mask))) &&
664 match(RHS, m_APInt(C))) {
665 // Equivalent to assume(V == Mask ^ C)
666 Known = Known.unionWith(KnownBits::makeConstant(*C ^ *Mask));
667 // assume(V << ShAmt = C)
668 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
669 match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
670 // For those bits in C that are known, we can propagate them to known
671 // bits in V shifted to the right by ShAmt.
673 RHSKnown.Zero.lshrInPlace(ShAmt);
674 RHSKnown.One.lshrInPlace(ShAmt);
675 Known = Known.unionWith(RHSKnown);
676 // assume(V >> ShAmt = C)
677 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
678 match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
680 // For those bits in RHS that are known, we can propagate them to known
681 // bits in V shifted to the right by C.
682 Known.Zero |= RHSKnown.Zero << ShAmt;
683 Known.One |= RHSKnown.One << ShAmt;
684 }
685 break;
686 case ICmpInst::ICMP_NE: {
687 // assume (V & B != 0) where B is a power of 2
688 const APInt *BPow2;
689 if (match(LHS, m_And(m_V, m_Power2(BPow2))) && match(RHS, m_Zero()))
690 Known.One |= *BPow2;
691 break;
692 }
693 default:
694 if (match(RHS, m_APInt(C))) {
695 const APInt *Offset = nullptr;
696 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
698 if (Offset)
699 LHSRange = LHSRange.sub(*Offset);
700 Known = Known.unionWith(LHSRange.toKnownBits());
701 }
702 // X & Y u> C -> X u> C && Y u> C
703 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) &&
704 match(LHS, m_c_And(m_V, m_Value()))) {
705 Known.One.setHighBits(
706 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
707 }
708 // X | Y u< C -> X u< C && Y u< C
709 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) &&
710 match(LHS, m_c_Or(m_V, m_Value()))) {
711 Known.Zero.setHighBits(
712 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
713 }
714 }
715 break;
716 }
717}
718
719static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
720 KnownBits &Known,
721 const SimplifyQuery &SQ, bool Invert) {
723 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
724 Value *LHS = Cmp->getOperand(0);
725 Value *RHS = Cmp->getOperand(1);
726
727 // Handle icmp pred (trunc V), C
728 if (match(LHS, m_Trunc(m_Specific(V)))) {
730 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
731 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
732 return;
733 }
734
735 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
736}
737
739 KnownBits &Known, unsigned Depth,
740 const SimplifyQuery &SQ, bool Invert) {
741 Value *A, *B;
744 KnownBits Known2(Known.getBitWidth());
745 KnownBits Known3(Known.getBitWidth());
746 computeKnownBitsFromCond(V, A, Known2, Depth + 1, SQ, Invert);
747 computeKnownBitsFromCond(V, B, Known3, Depth + 1, SQ, Invert);
748 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
750 Known2 = Known2.unionWith(Known3);
751 else
752 Known2 = Known2.intersectWith(Known3);
753 Known = Known.unionWith(Known2);
754 }
755
756 if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
757 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
758}
759
761 unsigned Depth, const SimplifyQuery &Q) {
762 if (!Q.CxtI)
763 return;
764
765 if (Q.DC && Q.DT) {
766 // Handle dominating conditions.
767 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
768 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
769 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
770 computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
771 /*Invert*/ false);
772
773 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
774 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
775 computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
776 /*Invert*/ true);
777 }
778
779 if (Known.hasConflict())
780 Known.resetAll();
781 }
782
783 if (!Q.AC)
784 return;
785
786 unsigned BitWidth = Known.getBitWidth();
787
788 // Note that the patterns below need to be kept in sync with the code
789 // in AssumptionCache::updateAffectedValues.
790
791 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
792 if (!Elem.Assume)
793 continue;
794
795 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
796 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
797 "Got assumption for the wrong function!");
798
799 if (Elem.Index != AssumptionCache::ExprResultIdx) {
800 if (!V->getType()->isPointerTy())
801 continue;
803 *I, I->bundle_op_info_begin()[Elem.Index])) {
804 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
805 isPowerOf2_64(RK.ArgValue) &&
807 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
808 }
809 continue;
810 }
811
812 // Warning: This loop can end up being somewhat performance sensitive.
813 // We're running this loop for once for each value queried resulting in a
814 // runtime of ~O(#assumes * #values).
815
816 Value *Arg = I->getArgOperand(0);
817
818 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
819 assert(BitWidth == 1 && "assume operand is not i1?");
820 (void)BitWidth;
821 Known.setAllOnes();
822 return;
823 }
824 if (match(Arg, m_Not(m_Specific(V))) &&
826 assert(BitWidth == 1 && "assume operand is not i1?");
827 (void)BitWidth;
828 Known.setAllZero();
829 return;
830 }
831
832 // The remaining tests are all recursive, so bail out if we hit the limit.
834 continue;
835
836 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
837 if (!Cmp)
838 continue;
839
840 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
841 continue;
842
843 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
844 }
845
846 // Conflicting assumption: Undefined behavior will occur on this execution
847 // path.
848 if (Known.hasConflict())
849 Known.resetAll();
850}
851
852/// Compute known bits from a shift operator, including those with a
853/// non-constant shift amount. Known is the output of this function. Known2 is a
854/// pre-allocated temporary with the same bit width as Known and on return
855/// contains the known bit of the shift value source. KF is an
856/// operator-specific function that, given the known-bits and a shift amount,
857/// compute the implied known-bits of the shift operator's result respectively
858/// for that shift amount. The results from calling KF are conservatively
859/// combined for all permitted shift amounts.
861 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
862 KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q,
863 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
864 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
865 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
866 // To limit compile-time impact, only query isKnownNonZero() if we know at
867 // least something about the shift amount.
868 bool ShAmtNonZero =
869 Known.isNonZero() ||
870 (Known.getMaxValue().ult(Known.getBitWidth()) &&
871 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
872 Known = KF(Known2, Known, ShAmtNonZero);
873}
874
875static KnownBits
876getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
877 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
878 unsigned Depth, const SimplifyQuery &Q) {
879 unsigned BitWidth = KnownLHS.getBitWidth();
880 KnownBits KnownOut(BitWidth);
881 bool IsAnd = false;
882 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
883 Value *X = nullptr, *Y = nullptr;
884
885 switch (I->getOpcode()) {
886 case Instruction::And:
887 KnownOut = KnownLHS & KnownRHS;
888 IsAnd = true;
889 // and(x, -x) is common idioms that will clear all but lowest set
890 // bit. If we have a single known bit in x, we can clear all bits
891 // above it.
892 // TODO: instcombine often reassociates independent `and` which can hide
893 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
894 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
895 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
896 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
897 KnownOut = KnownLHS.blsi();
898 else
899 KnownOut = KnownRHS.blsi();
900 }
901 break;
902 case Instruction::Or:
903 KnownOut = KnownLHS | KnownRHS;
904 break;
905 case Instruction::Xor:
906 KnownOut = KnownLHS ^ KnownRHS;
907 // xor(x, x-1) is common idioms that will clear all but lowest set
908 // bit. If we have a single known bit in x, we can clear all bits
909 // above it.
910 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
911 // -1 but for the purpose of demanded bits (xor(x, x-C) &
912 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
913 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
914 if (HasKnownOne &&
916 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
917 KnownOut = XBits.blsmsk();
918 }
919 break;
920 default:
921 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
922 }
923
924 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
925 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
926 // here we handle the more general case of adding any odd number by
927 // matching the form and/xor/or(x, add(x, y)) where y is odd.
928 // TODO: This could be generalized to clearing any bit set in y where the
929 // following bit is known to be unset in y.
930 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
934 KnownBits KnownY(BitWidth);
935 computeKnownBits(Y, DemandedElts, KnownY, Depth + 1, Q);
936 if (KnownY.countMinTrailingOnes() > 0) {
937 if (IsAnd)
938 KnownOut.Zero.setBit(0);
939 else
940 KnownOut.One.setBit(0);
941 }
942 }
943 return KnownOut;
944}
945
946// Public so this can be used in `SimplifyDemandedUseBits`.
948 const KnownBits &KnownLHS,
949 const KnownBits &KnownRHS,
950 unsigned Depth,
951 const SimplifyQuery &SQ) {
952 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
953 APInt DemandedElts =
954 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
955
956 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, Depth,
957 SQ);
958}
959
961 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
962 // Without vscale_range, we only know that vscale is non-zero.
963 if (!Attr.isValid())
965
966 unsigned AttrMin = Attr.getVScaleRangeMin();
967 // Minimum is larger than vscale width, result is always poison.
968 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
969 return ConstantRange::getEmpty(BitWidth);
970
971 APInt Min(BitWidth, AttrMin);
972 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
973 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
975
976 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
977}
978
980 const APInt &DemandedElts,
981 KnownBits &Known, unsigned Depth,
982 const SimplifyQuery &Q) {
983 unsigned BitWidth = Known.getBitWidth();
984
985 KnownBits Known2(BitWidth);
986 switch (I->getOpcode()) {
987 default: break;
988 case Instruction::Load:
989 if (MDNode *MD =
990 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
992 break;
993 case Instruction::And:
994 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
995 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
996
997 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
998 break;
999 case Instruction::Or:
1000 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1001 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1002
1003 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1004 break;
1005 case Instruction::Xor:
1006 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1007 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1008
1009 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1010 break;
1011 case Instruction::Mul: {
1012 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1013 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, DemandedElts,
1014 Known, Known2, Depth, Q);
1015 break;
1016 }
1017 case Instruction::UDiv: {
1018 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1019 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1020 Known =
1021 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1022 break;
1023 }
1024 case Instruction::SDiv: {
1025 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1026 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1027 Known =
1028 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1029 break;
1030 }
1031 case Instruction::Select: {
1032 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1033 KnownBits Res(Known.getBitWidth());
1034 computeKnownBits(Arm, Res, Depth + 1, Q);
1035 // If we have a constant arm, we are done.
1036 if (Res.isConstant())
1037 return Res;
1038
1039 // See what condition implies about the bits of the two select arms.
1040 KnownBits CondRes(Res.getBitWidth());
1041 computeKnownBitsFromCond(Arm, I->getOperand(0), CondRes, Depth + 1, Q,
1042 Invert);
1043 // If we don't get any information from the condition, no reason to
1044 // proceed.
1045 if (CondRes.isUnknown())
1046 return Res;
1047
1048 // We can have conflict if the condition is dead. I.e if we have
1049 // (x | 64) < 32 ? (x | 64) : y
1050 // we will have conflict at bit 6 from the condition/the `or`.
1051 // In that case just return. Its not particularly important
1052 // what we do, as this select is going to be simplified soon.
1053 CondRes = CondRes.unionWith(Res);
1054 if (CondRes.hasConflict())
1055 return Res;
1056
1057 // Finally make sure the information we found is valid. This is relatively
1058 // expensive so it's left for the very end.
1059 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1060 return Res;
1061
1062 // Finally, we know we get information from the condition and its valid,
1063 // so return it.
1064 return CondRes;
1065 };
1066 // Only known if known in both the LHS and RHS.
1067 Known =
1068 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1069 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1070 break;
1071 }
1072 case Instruction::FPTrunc:
1073 case Instruction::FPExt:
1074 case Instruction::FPToUI:
1075 case Instruction::FPToSI:
1076 case Instruction::SIToFP:
1077 case Instruction::UIToFP:
1078 break; // Can't work with floating point.
1079 case Instruction::PtrToInt:
1080 case Instruction::IntToPtr:
1081 // Fall through and handle them the same as zext/trunc.
1082 [[fallthrough]];
1083 case Instruction::ZExt:
1084 case Instruction::Trunc: {
1085 Type *SrcTy = I->getOperand(0)->getType();
1086
1087 unsigned SrcBitWidth;
1088 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1089 // which fall through here.
1090 Type *ScalarTy = SrcTy->getScalarType();
1091 SrcBitWidth = ScalarTy->isPointerTy() ?
1092 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1093 Q.DL.getTypeSizeInBits(ScalarTy);
1094
1095 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1096 Known = Known.anyextOrTrunc(SrcBitWidth);
1097 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1098 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1099 Inst && Inst->hasNonNeg() && !Known.isNegative())
1100 Known.makeNonNegative();
1101 Known = Known.zextOrTrunc(BitWidth);
1102 break;
1103 }
1104 case Instruction::BitCast: {
1105 Type *SrcTy = I->getOperand(0)->getType();
1106 if (SrcTy->isIntOrPtrTy() &&
1107 // TODO: For now, not handling conversions like:
1108 // (bitcast i64 %x to <2 x i32>)
1109 !I->getType()->isVectorTy()) {
1110 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1111 break;
1112 }
1113
1114 // Handle cast from vector integer type to scalar or vector integer.
1115 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1116 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1117 !I->getType()->isIntOrIntVectorTy() ||
1118 isa<ScalableVectorType>(I->getType()))
1119 break;
1120
1121 // Look through a cast from narrow vector elements to wider type.
1122 // Examples: v4i32 -> v2i64, v3i8 -> v24
1123 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1124 if (BitWidth % SubBitWidth == 0) {
1125 // Known bits are automatically intersected across demanded elements of a
1126 // vector. So for example, if a bit is computed as known zero, it must be
1127 // zero across all demanded elements of the vector.
1128 //
1129 // For this bitcast, each demanded element of the output is sub-divided
1130 // across a set of smaller vector elements in the source vector. To get
1131 // the known bits for an entire element of the output, compute the known
1132 // bits for each sub-element sequentially. This is done by shifting the
1133 // one-set-bit demanded elements parameter across the sub-elements for
1134 // consecutive calls to computeKnownBits. We are using the demanded
1135 // elements parameter as a mask operator.
1136 //
1137 // The known bits of each sub-element are then inserted into place
1138 // (dependent on endian) to form the full result of known bits.
1139 unsigned NumElts = DemandedElts.getBitWidth();
1140 unsigned SubScale = BitWidth / SubBitWidth;
1141 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1142 for (unsigned i = 0; i != NumElts; ++i) {
1143 if (DemandedElts[i])
1144 SubDemandedElts.setBit(i * SubScale);
1145 }
1146
1147 KnownBits KnownSrc(SubBitWidth);
1148 for (unsigned i = 0; i != SubScale; ++i) {
1149 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc,
1150 Depth + 1, Q);
1151 unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i;
1152 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1153 }
1154 }
1155 break;
1156 }
1157 case Instruction::SExt: {
1158 // Compute the bits in the result that are not present in the input.
1159 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1160
1161 Known = Known.trunc(SrcBitWidth);
1162 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1163 // If the sign bit of the input is known set or clear, then we know the
1164 // top bits of the result.
1165 Known = Known.sext(BitWidth);
1166 break;
1167 }
1168 case Instruction::Shl: {
1169 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1170 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1171 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1172 bool ShAmtNonZero) {
1173 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1174 };
1175 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1176 KF);
1177 // Trailing zeros of a right-shifted constant never decrease.
1178 const APInt *C;
1179 if (match(I->getOperand(0), m_APInt(C)))
1180 Known.Zero.setLowBits(C->countr_zero());
1181 break;
1182 }
1183 case Instruction::LShr: {
1184 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1185 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1186 bool ShAmtNonZero) {
1187 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1188 };
1189 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1190 KF);
1191 // Leading zeros of a left-shifted constant never decrease.
1192 const APInt *C;
1193 if (match(I->getOperand(0), m_APInt(C)))
1194 Known.Zero.setHighBits(C->countl_zero());
1195 break;
1196 }
1197 case Instruction::AShr: {
1198 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1199 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1200 bool ShAmtNonZero) {
1201 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1202 };
1203 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1204 KF);
1205 break;
1206 }
1207 case Instruction::Sub: {
1208 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1209 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1210 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1211 DemandedElts, Known, Known2, Depth, Q);
1212 break;
1213 }
1214 case Instruction::Add: {
1215 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1216 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1217 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1218 DemandedElts, Known, Known2, Depth, Q);
1219 break;
1220 }
1221 case Instruction::SRem:
1222 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1223 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1224 Known = KnownBits::srem(Known, Known2);
1225 break;
1226
1227 case Instruction::URem:
1228 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1229 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1230 Known = KnownBits::urem(Known, Known2);
1231 break;
1232 case Instruction::Alloca:
1233 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
1234 break;
1235 case Instruction::GetElementPtr: {
1236 // Analyze all of the subscripts of this getelementptr instruction
1237 // to determine if we can prove known low zero bits.
1238 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1239 // Accumulate the constant indices in a separate variable
1240 // to minimize the number of calls to computeForAddSub.
1241 APInt AccConstIndices(BitWidth, 0, /*IsSigned*/ true);
1242
1244 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1245 // TrailZ can only become smaller, short-circuit if we hit zero.
1246 if (Known.isUnknown())
1247 break;
1248
1249 Value *Index = I->getOperand(i);
1250
1251 // Handle case when index is zero.
1252 Constant *CIndex = dyn_cast<Constant>(Index);
1253 if (CIndex && CIndex->isZeroValue())
1254 continue;
1255
1256 if (StructType *STy = GTI.getStructTypeOrNull()) {
1257 // Handle struct member offset arithmetic.
1258
1259 assert(CIndex &&
1260 "Access to structure field must be known at compile time");
1261
1262 if (CIndex->getType()->isVectorTy())
1263 Index = CIndex->getSplatValue();
1264
1265 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1266 const StructLayout *SL = Q.DL.getStructLayout(STy);
1268 AccConstIndices += Offset;
1269 continue;
1270 }
1271
1272 // Handle array index arithmetic.
1273 Type *IndexedTy = GTI.getIndexedType();
1274 if (!IndexedTy->isSized()) {
1275 Known.resetAll();
1276 break;
1277 }
1278
1279 unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits();
1280 KnownBits IndexBits(IndexBitWidth);
1281 computeKnownBits(Index, IndexBits, Depth + 1, Q);
1282 TypeSize IndexTypeSize = GTI.getSequentialElementStride(Q.DL);
1283 uint64_t TypeSizeInBytes = IndexTypeSize.getKnownMinValue();
1284 KnownBits ScalingFactor(IndexBitWidth);
1285 // Multiply by current sizeof type.
1286 // &A[i] == A + i * sizeof(*A[i]).
1287 if (IndexTypeSize.isScalable()) {
1288 // For scalable types the only thing we know about sizeof is
1289 // that this is a multiple of the minimum size.
1290 ScalingFactor.Zero.setLowBits(llvm::countr_zero(TypeSizeInBytes));
1291 } else if (IndexBits.isConstant()) {
1292 APInt IndexConst = IndexBits.getConstant();
1293 APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes);
1294 IndexConst *= ScalingFactor;
1295 AccConstIndices += IndexConst.sextOrTrunc(BitWidth);
1296 continue;
1297 } else {
1298 ScalingFactor =
1299 KnownBits::makeConstant(APInt(IndexBitWidth, TypeSizeInBytes));
1300 }
1301 IndexBits = KnownBits::mul(IndexBits, ScalingFactor);
1302
1303 // If the offsets have a different width from the pointer, according
1304 // to the language reference we need to sign-extend or truncate them
1305 // to the width of the pointer.
1306 IndexBits = IndexBits.sextOrTrunc(BitWidth);
1307
1308 // Note that inbounds does *not* guarantee nsw for the addition, as only
1309 // the offset is signed, while the base address is unsigned.
1311 /*Add=*/true, /*NSW=*/false, /* NUW=*/false, Known, IndexBits);
1312 }
1313 if (!Known.isUnknown() && !AccConstIndices.isZero()) {
1314 KnownBits Index = KnownBits::makeConstant(AccConstIndices);
1316 /*Add=*/true, /*NSW=*/false, /* NUW=*/false, Known, Index);
1317 }
1318 break;
1319 }
1320 case Instruction::PHI: {
1321 const PHINode *P = cast<PHINode>(I);
1322 BinaryOperator *BO = nullptr;
1323 Value *R = nullptr, *L = nullptr;
1324 if (matchSimpleRecurrence(P, BO, R, L)) {
1325 // Handle the case of a simple two-predecessor recurrence PHI.
1326 // There's a lot more that could theoretically be done here, but
1327 // this is sufficient to catch some interesting cases.
1328 unsigned Opcode = BO->getOpcode();
1329
1330 // If this is a shift recurrence, we know the bits being shifted in.
1331 // We can combine that with information about the start value of the
1332 // recurrence to conclude facts about the result.
1333 if ((Opcode == Instruction::LShr || Opcode == Instruction::AShr ||
1334 Opcode == Instruction::Shl) &&
1335 BO->getOperand(0) == I) {
1336
1337 // We have matched a recurrence of the form:
1338 // %iv = [R, %entry], [%iv.next, %backedge]
1339 // %iv.next = shift_op %iv, L
1340
1341 // Recurse with the phi context to avoid concern about whether facts
1342 // inferred hold at original context instruction. TODO: It may be
1343 // correct to use the original context. IF warranted, explore and
1344 // add sufficient tests to cover.
1345 SimplifyQuery RecQ = Q;
1346 RecQ.CxtI = P;
1347 computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1348 switch (Opcode) {
1349 case Instruction::Shl:
1350 // A shl recurrence will only increase the tailing zeros
1351 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1352 break;
1353 case Instruction::LShr:
1354 // A lshr recurrence will preserve the leading zeros of the
1355 // start value
1356 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1357 break;
1358 case Instruction::AShr:
1359 // An ashr recurrence will extend the initial sign bit
1360 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1361 Known.One.setHighBits(Known2.countMinLeadingOnes());
1362 break;
1363 };
1364 }
1365
1366 // Check for operations that have the property that if
1367 // both their operands have low zero bits, the result
1368 // will have low zero bits.
1369 if (Opcode == Instruction::Add ||
1370 Opcode == Instruction::Sub ||
1371 Opcode == Instruction::And ||
1372 Opcode == Instruction::Or ||
1373 Opcode == Instruction::Mul) {
1374 // Change the context instruction to the "edge" that flows into the
1375 // phi. This is important because that is where the value is actually
1376 // "evaluated" even though it is used later somewhere else. (see also
1377 // D69571).
1378 SimplifyQuery RecQ = Q;
1379
1380 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1381 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1382 Instruction *LInst = P->getIncomingBlock(1-OpNum)->getTerminator();
1383
1384 // Ok, we have a PHI of the form L op= R. Check for low
1385 // zero bits.
1386 RecQ.CxtI = RInst;
1387 computeKnownBits(R, Known2, Depth + 1, RecQ);
1388
1389 // We need to take the minimum number of known bits
1390 KnownBits Known3(BitWidth);
1391 RecQ.CxtI = LInst;
1392 computeKnownBits(L, Known3, Depth + 1, RecQ);
1393
1394 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1395 Known3.countMinTrailingZeros()));
1396
1397 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1398 if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
1399 // If initial value of recurrence is nonnegative, and we are adding
1400 // a nonnegative number with nsw, the result can only be nonnegative
1401 // or poison value regardless of the number of times we execute the
1402 // add in phi recurrence. If initial value is negative and we are
1403 // adding a negative number with nsw, the result can only be
1404 // negative or poison value. Similar arguments apply to sub and mul.
1405 //
1406 // (add non-negative, non-negative) --> non-negative
1407 // (add negative, negative) --> negative
1408 if (Opcode == Instruction::Add) {
1409 if (Known2.isNonNegative() && Known3.isNonNegative())
1410 Known.makeNonNegative();
1411 else if (Known2.isNegative() && Known3.isNegative())
1412 Known.makeNegative();
1413 }
1414
1415 // (sub nsw non-negative, negative) --> non-negative
1416 // (sub nsw negative, non-negative) --> negative
1417 else if (Opcode == Instruction::Sub && BO->getOperand(0) == I) {
1418 if (Known2.isNonNegative() && Known3.isNegative())
1419 Known.makeNonNegative();
1420 else if (Known2.isNegative() && Known3.isNonNegative())
1421 Known.makeNegative();
1422 }
1423
1424 // (mul nsw non-negative, non-negative) --> non-negative
1425 else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
1426 Known3.isNonNegative())
1427 Known.makeNonNegative();
1428 }
1429
1430 break;
1431 }
1432 }
1433
1434 // Unreachable blocks may have zero-operand PHI nodes.
1435 if (P->getNumIncomingValues() == 0)
1436 break;
1437
1438 // Otherwise take the unions of the known bit sets of the operands,
1439 // taking conservative care to avoid excessive recursion.
1440 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1441 // Skip if every incoming value references to ourself.
1442 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1443 break;
1444
1445 Known.Zero.setAllBits();
1446 Known.One.setAllBits();
1447 for (unsigned u = 0, e = P->getNumIncomingValues(); u < e; ++u) {
1448 Value *IncValue = P->getIncomingValue(u);
1449 // Skip direct self references.
1450 if (IncValue == P) continue;
1451
1452 // Change the context instruction to the "edge" that flows into the
1453 // phi. This is important because that is where the value is actually
1454 // "evaluated" even though it is used later somewhere else. (see also
1455 // D69571).
1456 SimplifyQuery RecQ = Q;
1457 RecQ.CxtI = P->getIncomingBlock(u)->getTerminator();
1458
1459 Known2 = KnownBits(BitWidth);
1460
1461 // Recurse, but cap the recursion to one level, because we don't
1462 // want to waste time spinning around in loops.
1463 // TODO: See if we can base recursion limiter on number of incoming phi
1464 // edges so we don't overly clamp analysis.
1465 computeKnownBits(IncValue, Known2, MaxAnalysisRecursionDepth - 1, RecQ);
1466
1467 // See if we can further use a conditional branch into the phi
1468 // to help us determine the range of the value.
1469 if (!Known2.isConstant()) {
1471 const APInt *RHSC;
1472 BasicBlock *TrueSucc, *FalseSucc;
1473 // TODO: Use RHS Value and compute range from its known bits.
1474 if (match(RecQ.CxtI,
1475 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1476 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1477 // Check for cases of duplicate successors.
1478 if ((TrueSucc == P->getParent()) != (FalseSucc == P->getParent())) {
1479 // If we're using the false successor, invert the predicate.
1480 if (FalseSucc == P->getParent())
1481 Pred = CmpInst::getInversePredicate(Pred);
1482 // Get the knownbits implied by the incoming phi condition.
1483 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1484 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1485 // We can have conflicts here if we are analyzing deadcode (its
1486 // impossible for us reach this BB based the icmp).
1487 if (KnownUnion.hasConflict()) {
1488 // No reason to continue analyzing in a known dead region, so
1489 // just resetAll and break. This will cause us to also exit the
1490 // outer loop.
1491 Known.resetAll();
1492 break;
1493 }
1494 Known2 = KnownUnion;
1495 }
1496 }
1497 }
1498
1499 Known = Known.intersectWith(Known2);
1500 // If all bits have been ruled out, there's no need to check
1501 // more operands.
1502 if (Known.isUnknown())
1503 break;
1504 }
1505 }
1506 break;
1507 }
1508 case Instruction::Call:
1509 case Instruction::Invoke: {
1510 // If range metadata is attached to this call, set known bits from that,
1511 // and then intersect with known bits based on other properties of the
1512 // function.
1513 if (MDNode *MD =
1514 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1516
1517 const auto *CB = cast<CallBase>(I);
1518
1519 if (std::optional<ConstantRange> Range = CB->getRange())
1520 Known = Known.unionWith(Range->toKnownBits());
1521
1522 if (const Value *RV = CB->getReturnedArgOperand()) {
1523 if (RV->getType() == I->getType()) {
1524 computeKnownBits(RV, Known2, Depth + 1, Q);
1525 Known = Known.unionWith(Known2);
1526 // If the function doesn't return properly for all input values
1527 // (e.g. unreachable exits) then there might be conflicts between the
1528 // argument value and the range metadata. Simply discard the known bits
1529 // in case of conflicts.
1530 if (Known.hasConflict())
1531 Known.resetAll();
1532 }
1533 }
1534 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1535 switch (II->getIntrinsicID()) {
1536 default: break;
1537 case Intrinsic::abs: {
1538 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1539 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
1540 Known = Known2.abs(IntMinIsPoison);
1541 break;
1542 }
1543 case Intrinsic::bitreverse:
1544 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1545 Known.Zero |= Known2.Zero.reverseBits();
1546 Known.One |= Known2.One.reverseBits();
1547 break;
1548 case Intrinsic::bswap:
1549 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1550 Known.Zero |= Known2.Zero.byteSwap();
1551 Known.One |= Known2.One.byteSwap();
1552 break;
1553 case Intrinsic::ctlz: {
1554 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1555 // If we have a known 1, its position is our upper bound.
1556 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
1557 // If this call is poison for 0 input, the result will be less than 2^n.
1558 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1559 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1560 unsigned LowBits = llvm::bit_width(PossibleLZ);
1561 Known.Zero.setBitsFrom(LowBits);
1562 break;
1563 }
1564 case Intrinsic::cttz: {
1565 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1566 // If we have a known 1, its position is our upper bound.
1567 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
1568 // If this call is poison for 0 input, the result will be less than 2^n.
1569 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1570 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1571 unsigned LowBits = llvm::bit_width(PossibleTZ);
1572 Known.Zero.setBitsFrom(LowBits);
1573 break;
1574 }
1575 case Intrinsic::ctpop: {
1576 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1577 // We can bound the space the count needs. Also, bits known to be zero
1578 // can't contribute to the population.
1579 unsigned BitsPossiblySet = Known2.countMaxPopulation();
1580 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
1581 Known.Zero.setBitsFrom(LowBits);
1582 // TODO: we could bound KnownOne using the lower bound on the number
1583 // of bits which might be set provided by popcnt KnownOne2.
1584 break;
1585 }
1586 case Intrinsic::fshr:
1587 case Intrinsic::fshl: {
1588 const APInt *SA;
1589 if (!match(I->getOperand(2), m_APInt(SA)))
1590 break;
1591
1592 // Normalize to funnel shift left.
1593 uint64_t ShiftAmt = SA->urem(BitWidth);
1594 if (II->getIntrinsicID() == Intrinsic::fshr)
1595 ShiftAmt = BitWidth - ShiftAmt;
1596
1597 KnownBits Known3(BitWidth);
1598 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1599 computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q);
1600
1601 Known.Zero =
1602 Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
1603 Known.One =
1604 Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
1605 break;
1606 }
1607 case Intrinsic::uadd_sat:
1608 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1609 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1610 Known = KnownBits::uadd_sat(Known, Known2);
1611 break;
1612 case Intrinsic::usub_sat:
1613 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1614 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1615 Known = KnownBits::usub_sat(Known, Known2);
1616 break;
1617 case Intrinsic::sadd_sat:
1618 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1619 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1620 Known = KnownBits::sadd_sat(Known, Known2);
1621 break;
1622 case Intrinsic::ssub_sat:
1623 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1624 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1625 Known = KnownBits::ssub_sat(Known, Known2);
1626 break;
1627 // for min/max/and/or reduce, any bit common to each element in the
1628 // input vec is set in the output.
1629 case Intrinsic::vector_reduce_and:
1630 case Intrinsic::vector_reduce_or:
1631 case Intrinsic::vector_reduce_umax:
1632 case Intrinsic::vector_reduce_umin:
1633 case Intrinsic::vector_reduce_smax:
1634 case Intrinsic::vector_reduce_smin:
1635 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1636 break;
1637 case Intrinsic::vector_reduce_xor: {
1638 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1639 // The zeros common to all vecs are zero in the output.
1640 // If the number of elements is odd, then the common ones remain. If the
1641 // number of elements is even, then the common ones becomes zeros.
1642 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
1643 // Even, so the ones become zeros.
1644 bool EvenCnt = VecTy->getElementCount().isKnownEven();
1645 if (EvenCnt)
1646 Known.Zero |= Known.One;
1647 // Maybe even element count so need to clear ones.
1648 if (VecTy->isScalableTy() || EvenCnt)
1649 Known.One.clearAllBits();
1650 break;
1651 }
1652 case Intrinsic::umin:
1653 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1654 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1655 Known = KnownBits::umin(Known, Known2);
1656 break;
1657 case Intrinsic::umax:
1658 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1659 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1660 Known = KnownBits::umax(Known, Known2);
1661 break;
1662 case Intrinsic::smin:
1663 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1664 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1665 Known = KnownBits::smin(Known, Known2);
1666 break;
1667 case Intrinsic::smax:
1668 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1669 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1670 Known = KnownBits::smax(Known, Known2);
1671 break;
1672 case Intrinsic::ptrmask: {
1673 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1674
1675 const Value *Mask = I->getOperand(1);
1676 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
1677 computeKnownBits(Mask, Known2, Depth + 1, Q);
1678 // TODO: 1-extend would be more precise.
1679 Known &= Known2.anyextOrTrunc(BitWidth);
1680 break;
1681 }
1682 case Intrinsic::x86_sse42_crc32_64_64:
1683 Known.Zero.setBitsFrom(32);
1684 break;
1685 case Intrinsic::riscv_vsetvli:
1686 case Intrinsic::riscv_vsetvlimax: {
1687 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
1688 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
1690 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
1691 RISCVII::VLMUL VLMUL = static_cast<RISCVII::VLMUL>(
1692 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
1693 // The Range is [Lower, Upper), so we need to subtract 1 here to get the
1694 // real upper value.
1695 uint64_t MaxVLEN =
1696 (Range.getUpper().getZExtValue() - 1) * RISCV::RVVBitsPerBlock;
1697 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
1698
1699 // Result of vsetvli must be not larger than AVL.
1700 if (HasAVL)
1701 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
1702 MaxVL = std::min(MaxVL, CI->getZExtValue());
1703
1704 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
1705 if (BitWidth > KnownZeroFirstBit)
1706 Known.Zero.setBitsFrom(KnownZeroFirstBit);
1707 break;
1708 }
1709 case Intrinsic::vscale: {
1710 if (!II->getParent() || !II->getFunction())
1711 break;
1712
1713 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
1714 break;
1715 }
1716 }
1717 }
1718 break;
1719 }
1720 case Instruction::ShuffleVector: {
1721 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
1722 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
1723 if (!Shuf) {
1724 Known.resetAll();
1725 return;
1726 }
1727 // For undef elements, we don't know anything about the common state of
1728 // the shuffle result.
1729 APInt DemandedLHS, DemandedRHS;
1730 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
1731 Known.resetAll();
1732 return;
1733 }
1734 Known.One.setAllBits();
1735 Known.Zero.setAllBits();
1736 if (!!DemandedLHS) {
1737 const Value *LHS = Shuf->getOperand(0);
1738 computeKnownBits(LHS, DemandedLHS, Known, Depth + 1, Q);
1739 // If we don't know any bits, early out.
1740 if (Known.isUnknown())
1741 break;
1742 }
1743 if (!!DemandedRHS) {
1744 const Value *RHS = Shuf->getOperand(1);
1745 computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q);
1746 Known = Known.intersectWith(Known2);
1747 }
1748 break;
1749 }
1750 case Instruction::InsertElement: {
1751 if (isa<ScalableVectorType>(I->getType())) {
1752 Known.resetAll();
1753 return;
1754 }
1755 const Value *Vec = I->getOperand(0);
1756 const Value *Elt = I->getOperand(1);
1757 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
1758 unsigned NumElts = DemandedElts.getBitWidth();
1759 APInt DemandedVecElts = DemandedElts;
1760 bool NeedsElt = true;
1761 // If we know the index we are inserting too, clear it from Vec check.
1762 if (CIdx && CIdx->getValue().ult(NumElts)) {
1763 DemandedVecElts.clearBit(CIdx->getZExtValue());
1764 NeedsElt = DemandedElts[CIdx->getZExtValue()];
1765 }
1766
1767 Known.One.setAllBits();
1768 Known.Zero.setAllBits();
1769 if (NeedsElt) {
1770 computeKnownBits(Elt, Known, Depth + 1, Q);
1771 // If we don't know any bits, early out.
1772 if (Known.isUnknown())
1773 break;
1774 }
1775
1776 if (!DemandedVecElts.isZero()) {
1777 computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q);
1778 Known = Known.intersectWith(Known2);
1779 }
1780 break;
1781 }
1782 case Instruction::ExtractElement: {
1783 // Look through extract element. If the index is non-constant or
1784 // out-of-range demand all elements, otherwise just the extracted element.
1785 const Value *Vec = I->getOperand(0);
1786 const Value *Idx = I->getOperand(1);
1787 auto *CIdx = dyn_cast<ConstantInt>(Idx);
1788 if (isa<ScalableVectorType>(Vec->getType())) {
1789 // FIXME: there's probably *something* we can do with scalable vectors
1790 Known.resetAll();
1791 break;
1792 }
1793 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
1794 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
1795 if (CIdx && CIdx->getValue().ult(NumElts))
1796 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
1797 computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q);
1798 break;
1799 }
1800 case Instruction::ExtractValue:
1801 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
1802 const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
1803 if (EVI->getNumIndices() != 1) break;
1804 if (EVI->getIndices()[0] == 0) {
1805 switch (II->getIntrinsicID()) {
1806 default: break;
1807 case Intrinsic::uadd_with_overflow:
1808 case Intrinsic::sadd_with_overflow:
1810 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
1811 /* NUW=*/false, DemandedElts, Known, Known2, Depth, Q);
1812 break;
1813 case Intrinsic::usub_with_overflow:
1814 case Intrinsic::ssub_with_overflow:
1816 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
1817 /* NUW=*/false, DemandedElts, Known, Known2, Depth, Q);
1818 break;
1819 case Intrinsic::umul_with_overflow:
1820 case Intrinsic::smul_with_overflow:
1821 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
1822 DemandedElts, Known, Known2, Depth, Q);
1823 break;
1824 }
1825 }
1826 }
1827 break;
1828 case Instruction::Freeze:
1829 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
1830 Depth + 1))
1831 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1832 break;
1833 }
1834}
1835
1836/// Determine which bits of V are known to be either zero or one and return
1837/// them.
1838KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
1839 unsigned Depth, const SimplifyQuery &Q) {
1840 KnownBits Known(getBitWidth(V->getType(), Q.DL));
1841 ::computeKnownBits(V, DemandedElts, Known, Depth, Q);
1842 return Known;
1843}
1844
1845/// Determine which bits of V are known to be either zero or one and return
1846/// them.
1848 const SimplifyQuery &Q) {
1849 KnownBits Known(getBitWidth(V->getType(), Q.DL));
1850 computeKnownBits(V, Known, Depth, Q);
1851 return Known;
1852}
1853
1854/// Determine which bits of V are known to be either zero or one and return
1855/// them in the Known bit set.
1856///
1857/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
1858/// we cannot optimize based on the assumption that it is zero without changing
1859/// it to be an explicit zero. If we don't change it to zero, other code could
1860/// optimized based on the contradictory assumption that it is non-zero.
1861/// Because instcombine aggressively folds operations with undef args anyway,
1862/// this won't lose us code quality.
1863///
1864/// This function is defined on values with integer type, values with pointer
1865/// type, and vectors of integers. In the case
1866/// where V is a vector, known zero, and known one values are the
1867/// same width as the vector element, and the bit is set only if it is true
1868/// for all of the demanded elements in the vector specified by DemandedElts.
1869void computeKnownBits(const Value *V, const APInt &DemandedElts,
1870 KnownBits &Known, unsigned Depth,
1871 const SimplifyQuery &Q) {
1872 if (!DemandedElts) {
1873 // No demanded elts, better to assume we don't know anything.
1874 Known.resetAll();
1875 return;
1876 }
1877
1878 assert(V && "No Value?");
1879 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
1880
1881#ifndef NDEBUG
1882 Type *Ty = V->getType();
1883 unsigned BitWidth = Known.getBitWidth();
1884
1886 "Not integer or pointer type!");
1887
1888 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
1889 assert(
1890 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
1891 "DemandedElt width should equal the fixed vector number of elements");
1892 } else {
1893 assert(DemandedElts == APInt(1, 1) &&
1894 "DemandedElt width should be 1 for scalars or scalable vectors");
1895 }
1896
1897 Type *ScalarTy = Ty->getScalarType();
1898 if (ScalarTy->isPointerTy()) {
1899 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
1900 "V and Known should have same BitWidth");
1901 } else {
1902 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
1903 "V and Known should have same BitWidth");
1904 }
1905#endif
1906
1907 const APInt *C;
1908 if (match(V, m_APInt(C))) {
1909 // We know all of the bits for a scalar constant or a splat vector constant!
1910 Known = KnownBits::makeConstant(*C);
1911 return;
1912 }
1913 // Null and aggregate-zero are all-zeros.
1914 if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
1915 Known.setAllZero();
1916 return;
1917 }
1918 // Handle a constant vector by taking the intersection of the known bits of
1919 // each element.
1920 if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) {
1921 assert(!isa<ScalableVectorType>(V->getType()));
1922 // We know that CDV must be a vector of integers. Take the intersection of
1923 // each element.
1924 Known.Zero.setAllBits(); Known.One.setAllBits();
1925 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
1926 if (!DemandedElts[i])
1927 continue;
1928 APInt Elt = CDV->getElementAsAPInt(i);
1929 Known.Zero &= ~Elt;
1930 Known.One &= Elt;
1931 }
1932 if (Known.hasConflict())
1933 Known.resetAll();
1934 return;
1935 }
1936
1937 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
1938 assert(!isa<ScalableVectorType>(V->getType()));
1939 // We know that CV must be a vector of integers. Take the intersection of
1940 // each element.
1941 Known.Zero.setAllBits(); Known.One.setAllBits();
1942 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1943 if (!DemandedElts[i])
1944 continue;
1945 Constant *Element = CV->getAggregateElement(i);
1946 if (isa<PoisonValue>(Element))
1947 continue;
1948 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
1949 if (!ElementCI) {
1950 Known.resetAll();
1951 return;
1952 }
1953 const APInt &Elt = ElementCI->getValue();
1954 Known.Zero &= ~Elt;
1955 Known.One &= Elt;
1956 }
1957 if (Known.hasConflict())
1958 Known.resetAll();
1959 return;
1960 }
1961
1962 // Start out not knowing anything.
1963 Known.resetAll();
1964
1965 // We can't imply anything about undefs.
1966 if (isa<UndefValue>(V))
1967 return;
1968
1969 // There's no point in looking through other users of ConstantData for
1970 // assumptions. Confirm that we've handled them all.
1971 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
1972
1973 if (const auto *A = dyn_cast<Argument>(V))
1974 if (std::optional<ConstantRange> Range = A->getRange())
1975 Known = Range->toKnownBits();
1976
1977 // All recursive calls that increase depth must come after this.
1979 return;
1980
1981 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
1982 // the bits of its aliasee.
1983 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
1984 if (!GA->isInterposable())
1985 computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q);
1986 return;
1987 }
1988
1989 if (const Operator *I = dyn_cast<Operator>(V))
1990 computeKnownBitsFromOperator(I, DemandedElts, Known, Depth, Q);
1991 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
1992 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
1993 Known = CR->toKnownBits();
1994 }
1995
1996 // Aligned pointers have trailing zeros - refine Known.Zero set
1997 if (isa<PointerType>(V->getType())) {
1998 Align Alignment = V->getPointerAlignment(Q.DL);
1999 Known.Zero.setLowBits(Log2(Alignment));
2000 }
2001
2002 // computeKnownBitsFromContext strictly refines Known.
2003 // Therefore, we run them after computeKnownBitsFromOperator.
2004
2005 // Check whether we can determine known bits from context such as assumes.
2006 computeKnownBitsFromContext(V, Known, Depth, Q);
2007
2008 assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
2009}
2010
2011/// Try to detect a recurrence that the value of the induction variable is
2012/// always a power of two (or zero).
2013static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2014 unsigned Depth, SimplifyQuery &Q) {
2015 BinaryOperator *BO = nullptr;
2016 Value *Start = nullptr, *Step = nullptr;
2017 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2018 return false;
2019
2020 // Initial value must be a power of two.
2021 for (const Use &U : PN->operands()) {
2022 if (U.get() == Start) {
2023 // Initial value comes from a different BB, need to adjust context
2024 // instruction for analysis.
2025 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2026 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Depth, Q))
2027 return false;
2028 }
2029 }
2030
2031 // Except for Mul, the induction variable must be on the left side of the
2032 // increment expression, otherwise its value can be arbitrary.
2033 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2034 return false;
2035
2036 Q.CxtI = BO->getParent()->getTerminator();
2037 switch (BO->getOpcode()) {
2038 case Instruction::Mul:
2039 // Power of two is closed under multiplication.
2040 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2041 Q.IIQ.hasNoSignedWrap(BO)) &&
2042 isKnownToBeAPowerOfTwo(Step, OrZero, Depth, Q);
2043 case Instruction::SDiv:
2044 // Start value must not be signmask for signed division, so simply being a
2045 // power of two is not sufficient, and it has to be a constant.
2046 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2047 return false;
2048 [[fallthrough]];
2049 case Instruction::UDiv:
2050 // Divisor must be a power of two.
2051 // If OrZero is false, cannot guarantee induction variable is non-zero after
2052 // division, same for Shr, unless it is exact division.
2053 return (OrZero || Q.IIQ.isExact(BO)) &&
2054 isKnownToBeAPowerOfTwo(Step, false, Depth, Q);
2055 case Instruction::Shl:
2056 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2057 case Instruction::AShr:
2058 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2059 return false;
2060 [[fallthrough]];
2061 case Instruction::LShr:
2062 return OrZero || Q.IIQ.isExact(BO);
2063 default:
2064 return false;
2065 }
2066}
2067
2068/// Return true if the given value is known to have exactly one
2069/// bit set when defined. For vectors return true if every element is known to
2070/// be a power of two when defined. Supports values with integer or pointer
2071/// types and vectors of integers.
2072bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
2073 const SimplifyQuery &Q) {
2074 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2075
2076 if (isa<Constant>(V))
2077 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2078
2079 // i1 is by definition a power of 2 or zero.
2080 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2081 return true;
2082
2083 auto *I = dyn_cast<Instruction>(V);
2084 if (!I)
2085 return false;
2086
2087 if (Q.CxtI && match(V, m_VScale())) {
2088 const Function *F = Q.CxtI->getFunction();
2089 // The vscale_range indicates vscale is a power-of-two.
2090 return F->hasFnAttribute(Attribute::VScaleRange);
2091 }
2092
2093 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2094 // it is shifted off the end then the result is undefined.
2095 if (match(I, m_Shl(m_One(), m_Value())))
2096 return true;
2097
2098 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2099 // the bottom. If it is shifted off the bottom then the result is undefined.
2100 if (match(I, m_LShr(m_SignMask(), m_Value())))
2101 return true;
2102
2103 // The remaining tests are all recursive, so bail out if we hit the limit.
2105 return false;
2106
2107 switch (I->getOpcode()) {
2108 case Instruction::ZExt:
2109 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2110 case Instruction::Trunc:
2111 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2112 case Instruction::Shl:
2113 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2114 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2115 return false;
2116 case Instruction::LShr:
2117 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2118 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2119 return false;
2120 case Instruction::UDiv:
2121 if (Q.IIQ.isExact(cast<BinaryOperator>(I)))
2122 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2123 return false;
2124 case Instruction::Mul:
2125 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2126 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q) &&
2127 (OrZero || isKnownNonZero(I, Q, Depth));
2128 case Instruction::And:
2129 // A power of two and'd with anything is a power of two or zero.
2130 if (OrZero &&
2131 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Depth, Q) ||
2132 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Depth, Q)))
2133 return true;
2134 // X & (-X) is always a power of two or zero.
2135 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2136 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2137 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2138 return false;
2139 case Instruction::Add: {
2140 // Adding a power-of-two or zero to the same power-of-two or zero yields
2141 // either the original power-of-two, a larger power-of-two or zero.
2142 const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
2143 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2144 Q.IIQ.hasNoSignedWrap(VOBO)) {
2145 if (match(I->getOperand(0),
2146 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2147 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q))
2148 return true;
2149 if (match(I->getOperand(1),
2150 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2151 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q))
2152 return true;
2153
2154 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2155 KnownBits LHSBits(BitWidth);
2156 computeKnownBits(I->getOperand(0), LHSBits, Depth, Q);
2157
2158 KnownBits RHSBits(BitWidth);
2159 computeKnownBits(I->getOperand(1), RHSBits, Depth, Q);
2160 // If i8 V is a power of two or zero:
2161 // ZeroBits: 1 1 1 0 1 1 1 1
2162 // ~ZeroBits: 0 0 0 1 0 0 0 0
2163 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2164 // If OrZero isn't set, we cannot give back a zero result.
2165 // Make sure either the LHS or RHS has a bit set.
2166 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2167 return true;
2168 }
2169 return false;
2170 }
2171 case Instruction::Select:
2172 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2173 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Depth, Q);
2174 case Instruction::PHI: {
2175 // A PHI node is power of two if all incoming values are power of two, or if
2176 // it is an induction variable where in each step its value is a power of
2177 // two.
2178 auto *PN = cast<PHINode>(I);
2179 SimplifyQuery RecQ = Q;
2180
2181 // Check if it is an induction variable and always power of two.
2182 if (isPowerOfTwoRecurrence(PN, OrZero, Depth, RecQ))
2183 return true;
2184
2185 // Recursively check all incoming values. Limit recursion to 2 levels, so
2186 // that search complexity is limited to number of operands^2.
2187 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2188 return llvm::all_of(PN->operands(), [&](const Use &U) {
2189 // Value is power of 2 if it is coming from PHI node itself by induction.
2190 if (U.get() == PN)
2191 return true;
2192
2193 // Change the context instruction to the incoming block where it is
2194 // evaluated.
2195 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2196 return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ);
2197 });
2198 }
2199 case Instruction::Invoke:
2200 case Instruction::Call: {
2201 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2202 switch (II->getIntrinsicID()) {
2203 case Intrinsic::umax:
2204 case Intrinsic::smax:
2205 case Intrinsic::umin:
2206 case Intrinsic::smin:
2207 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Depth, Q) &&
2208 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2209 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2210 // thus dont change pow2/non-pow2 status.
2211 case Intrinsic::bitreverse:
2212 case Intrinsic::bswap:
2213 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2214 case Intrinsic::fshr:
2215 case Intrinsic::fshl:
2216 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2217 if (II->getArgOperand(0) == II->getArgOperand(1))
2218 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2219 break;
2220 default:
2221 break;
2222 }
2223 }
2224 return false;
2225 }
2226 default:
2227 return false;
2228 }
2229}
2230
2231/// Test whether a GEP's result is known to be non-null.
2232///
2233/// Uses properties inherent in a GEP to try to determine whether it is known
2234/// to be non-null.
2235///
2236/// Currently this routine does not support vector GEPs.
2237static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
2238 const SimplifyQuery &Q) {
2239 const Function *F = nullptr;
2240 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2241 F = I->getFunction();
2242
2243 if (!GEP->isInBounds() ||
2244 NullPointerIsDefined(F, GEP->getPointerAddressSpace()))
2245 return false;
2246
2247 // FIXME: Support vector-GEPs.
2248 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2249
2250 // If the base pointer is non-null, we cannot walk to a null address with an
2251 // inbounds GEP in address space zero.
2252 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2253 return true;
2254
2255 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2256 // If so, then the GEP cannot produce a null pointer, as doing so would
2257 // inherently violate the inbounds contract within address space zero.
2259 GTI != GTE; ++GTI) {
2260 // Struct types are easy -- they must always be indexed by a constant.
2261 if (StructType *STy = GTI.getStructTypeOrNull()) {
2262 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2263 unsigned ElementIdx = OpC->getZExtValue();
2264 const StructLayout *SL = Q.DL.getStructLayout(STy);
2265 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2266 if (ElementOffset > 0)
2267 return true;
2268 continue;
2269 }
2270
2271 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2272 if (GTI.getSequentialElementStride(Q.DL).isZero())
2273 continue;
2274
2275 // Fast path the constant operand case both for efficiency and so we don't
2276 // increment Depth when just zipping down an all-constant GEP.
2277 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2278 if (!OpC->isZero())
2279 return true;
2280 continue;
2281 }
2282
2283 // We post-increment Depth here because while isKnownNonZero increments it
2284 // as well, when we pop back up that increment won't persist. We don't want
2285 // to recurse 10k times just because we have 10k GEP operands. We don't
2286 // bail completely out because we want to handle constant GEPs regardless
2287 // of depth.
2289 continue;
2290
2291 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2292 return true;
2293 }
2294
2295 return false;
2296}
2297
2299 const Instruction *CtxI,
2300 const DominatorTree *DT) {
2301 assert(!isa<Constant>(V) && "Called for constant?");
2302
2303 if (!CtxI || !DT)
2304 return false;
2305
2306 unsigned NumUsesExplored = 0;
2307 for (const auto *U : V->users()) {
2308 // Avoid massive lists
2309 if (NumUsesExplored >= DomConditionsMaxUses)
2310 break;
2311 NumUsesExplored++;
2312
2313 // If the value is used as an argument to a call or invoke, then argument
2314 // attributes may provide an answer about null-ness.
2315 if (const auto *CB = dyn_cast<CallBase>(U))
2316 if (auto *CalledFunc = CB->getCalledFunction())
2317 for (const Argument &Arg : CalledFunc->args())
2318 if (CB->getArgOperand(Arg.getArgNo()) == V &&
2319 Arg.hasNonNullAttr(/* AllowUndefOrPoison */ false) &&
2320 DT->dominates(CB, CtxI))
2321 return true;
2322
2323 // If the value is used as a load/store, then the pointer must be non null.
2324 if (V == getLoadStorePointerOperand(U)) {
2325 const Instruction *I = cast<Instruction>(U);
2326 if (!NullPointerIsDefined(I->getFunction(),
2327 V->getType()->getPointerAddressSpace()) &&
2328 DT->dominates(I, CtxI))
2329 return true;
2330 }
2331
2332 if ((match(U, m_IDiv(m_Value(), m_Specific(V))) ||
2333 match(U, m_IRem(m_Value(), m_Specific(V)))) &&
2334 isValidAssumeForContext(cast<Instruction>(U), CtxI, DT))
2335 return true;
2336
2337 // Consider only compare instructions uniquely controlling a branch
2338 Value *RHS;
2339 CmpInst::Predicate Pred;
2340 if (!match(U, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2341 continue;
2342
2343 bool NonNullIfTrue;
2344 if (cmpExcludesZero(Pred, RHS))
2345 NonNullIfTrue = true;
2347 NonNullIfTrue = false;
2348 else
2349 continue;
2350
2353 for (const auto *CmpU : U->users()) {
2354 assert(WorkList.empty() && "Should be!");
2355 if (Visited.insert(CmpU).second)
2356 WorkList.push_back(CmpU);
2357
2358 while (!WorkList.empty()) {
2359 auto *Curr = WorkList.pop_back_val();
2360
2361 // If a user is an AND, add all its users to the work list. We only
2362 // propagate "pred != null" condition through AND because it is only
2363 // correct to assume that all conditions of AND are met in true branch.
2364 // TODO: Support similar logic of OR and EQ predicate?
2365 if (NonNullIfTrue)
2366 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2367 for (const auto *CurrU : Curr->users())
2368 if (Visited.insert(CurrU).second)
2369 WorkList.push_back(CurrU);
2370 continue;
2371 }
2372
2373 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2374 assert(BI->isConditional() && "uses a comparison!");
2375
2376 BasicBlock *NonNullSuccessor =
2377 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2378 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2379 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2380 return true;
2381 } else if (NonNullIfTrue && isGuard(Curr) &&
2382 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2383 return true;
2384 }
2385 }
2386 }
2387 }
2388
2389 return false;
2390}
2391
2392/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2393/// ensure that the value it's attached to is never Value? 'RangeType' is
2394/// is the type of the value described by the range.
2395static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2396 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2397 assert(NumRanges >= 1);
2398 for (unsigned i = 0; i < NumRanges; ++i) {
2400 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2402 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2403 ConstantRange Range(Lower->getValue(), Upper->getValue());
2404 if (Range.contains(Value))
2405 return false;
2406 }
2407 return true;
2408}
2409
2410/// Try to detect a recurrence that monotonically increases/decreases from a
2411/// non-zero starting value. These are common as induction variables.
2412static bool isNonZeroRecurrence(const PHINode *PN) {
2413 BinaryOperator *BO = nullptr;
2414 Value *Start = nullptr, *Step = nullptr;
2415 const APInt *StartC, *StepC;
2416 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2417 !match(Start, m_APInt(StartC)) || StartC->isZero())
2418 return false;
2419
2420 switch (BO->getOpcode()) {
2421 case Instruction::Add:
2422 // Starting from non-zero and stepping away from zero can never wrap back
2423 // to zero.
2424 return BO->hasNoUnsignedWrap() ||
2425 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
2426 StartC->isNegative() == StepC->isNegative());
2427 case Instruction::Mul:
2428 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2429 match(Step, m_APInt(StepC)) && !StepC->isZero();
2430 case Instruction::Shl:
2431 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
2432 case Instruction::AShr:
2433 case Instruction::LShr:
2434 return BO->isExact();
2435 default:
2436 return false;
2437 }
2438}
2439
2440static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth,
2441 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2442 Value *Y, bool NSW, bool NUW) {
2443 if (NUW)
2444 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2445 isKnownNonZero(X, DemandedElts, Q, Depth);
2446
2447 KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2448 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2449
2450 // If X and Y are both non-negative (as signed values) then their sum is not
2451 // zero unless both X and Y are zero.
2452 if (XKnown.isNonNegative() && YKnown.isNonNegative())
2453 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2454 isKnownNonZero(X, DemandedElts, Q, Depth))
2455 return true;
2456
2457 // If X and Y are both negative (as signed values) then their sum is not
2458 // zero unless both X and Y equal INT_MIN.
2459 if (XKnown.isNegative() && YKnown.isNegative()) {
2461 // The sign bit of X is set. If some other bit is set then X is not equal
2462 // to INT_MIN.
2463 if (XKnown.One.intersects(Mask))
2464 return true;
2465 // The sign bit of Y is set. If some other bit is set then Y is not equal
2466 // to INT_MIN.
2467 if (YKnown.One.intersects(Mask))
2468 return true;
2469 }
2470
2471 // The sum of a non-negative number and a power of two is not zero.
2472 if (XKnown.isNonNegative() &&
2473 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q))
2474 return true;
2475 if (YKnown.isNonNegative() &&
2476 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
2477 return true;
2478
2479 return KnownBits::computeForAddSub(/*Add=*/true, NSW, NUW, XKnown, YKnown)
2480 .isNonZero();
2481}
2482
2483static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth,
2484 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2485 Value *Y) {
2486 // TODO: Move this case into isKnownNonEqual().
2487 if (auto *C = dyn_cast<Constant>(X))
2488 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
2489 return true;
2490
2491 return ::isKnownNonEqual(X, Y, Depth, Q);
2492}
2493
2494static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth,
2495 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2496 Value *Y, bool NSW, bool NUW) {
2497 // If X and Y are non-zero then so is X * Y as long as the multiplication
2498 // does not overflow.
2499 if (NSW || NUW)
2500 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
2501 isKnownNonZero(Y, DemandedElts, Q, Depth);
2502
2503 // If either X or Y is odd, then if the other is non-zero the result can't
2504 // be zero.
2505 KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2506 if (XKnown.One[0])
2507 return isKnownNonZero(Y, DemandedElts, Q, Depth);
2508
2509 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2510 if (YKnown.One[0])
2511 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
2512
2513 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
2514 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
2515 // the lowest known One of X and Y. If they are non-zero, the result
2516 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
2517 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
2518 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
2519 BitWidth;
2520}
2521
2522static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
2523 unsigned Depth, const SimplifyQuery &Q,
2524 const KnownBits &KnownVal) {
2525 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2526 switch (I->getOpcode()) {
2527 case Instruction::Shl:
2528 return Lhs.shl(Rhs);
2529 case Instruction::LShr:
2530 return Lhs.lshr(Rhs);
2531 case Instruction::AShr:
2532 return Lhs.ashr(Rhs);
2533 default:
2534 llvm_unreachable("Unknown Shift Opcode");
2535 }
2536 };
2537
2538 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2539 switch (I->getOpcode()) {
2540 case Instruction::Shl:
2541 return Lhs.lshr(Rhs);
2542 case Instruction::LShr:
2543 case Instruction::AShr:
2544 return Lhs.shl(Rhs);
2545 default:
2546 llvm_unreachable("Unknown Shift Opcode");
2547 }
2548 };
2549
2550 if (KnownVal.isUnknown())
2551 return false;
2552
2553 KnownBits KnownCnt =
2554 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
2555 APInt MaxShift = KnownCnt.getMaxValue();
2556 unsigned NumBits = KnownVal.getBitWidth();
2557 if (MaxShift.uge(NumBits))
2558 return false;
2559
2560 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
2561 return true;
2562
2563 // If all of the bits shifted out are known to be zero, and Val is known
2564 // non-zero then at least one non-zero bit must remain.
2565 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
2566 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
2567 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
2568 return true;
2569
2570 return false;
2571}
2572
2574 const APInt &DemandedElts,
2575 unsigned Depth, const SimplifyQuery &Q) {
2576 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
2577 switch (I->getOpcode()) {
2578 case Instruction::Alloca:
2579 // Alloca never returns null, malloc might.
2580 return I->getType()->getPointerAddressSpace() == 0;
2581 case Instruction::GetElementPtr:
2582 if (I->getType()->isPointerTy())
2583 return isGEPKnownNonNull(cast<GEPOperator>(I), Depth, Q);
2584 break;
2585 case Instruction::BitCast: {
2586 // We need to be a bit careful here. We can only peek through the bitcast
2587 // if the scalar size of elements in the operand are smaller than and a
2588 // multiple of the size they are casting too. Take three cases:
2589 //
2590 // 1) Unsafe:
2591 // bitcast <2 x i16> %NonZero to <4 x i8>
2592 //
2593 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
2594 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
2595 // guranteed (imagine just sign bit set in the 2 i16 elements).
2596 //
2597 // 2) Unsafe:
2598 // bitcast <4 x i3> %NonZero to <3 x i4>
2599 //
2600 // Even though the scalar size of the src (`i3`) is smaller than the
2601 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
2602 // its possible for the `3 x i4` elements to be zero because there are
2603 // some elements in the destination that don't contain any full src
2604 // element.
2605 //
2606 // 3) Safe:
2607 // bitcast <4 x i8> %NonZero to <2 x i16>
2608 //
2609 // This is always safe as non-zero in the 4 i8 elements implies
2610 // non-zero in the combination of any two adjacent ones. Since i8 is a
2611 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
2612 // This all implies the 2 i16 elements are non-zero.
2613 Type *FromTy = I->getOperand(0)->getType();
2614 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
2615 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
2616 return isKnownNonZero(I->getOperand(0), Q, Depth);
2617 } break;
2618 case Instruction::IntToPtr:
2619 // Note that we have to take special care to avoid looking through
2620 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
2621 // as casts that can alter the value, e.g., AddrSpaceCasts.
2622 if (!isa<ScalableVectorType>(I->getType()) &&
2623 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2624 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2625 return isKnownNonZero(I->getOperand(0), Q, Depth);
2626 break;
2627 case Instruction::PtrToInt:
2628 // Similar to int2ptr above, we can look through ptr2int here if the cast
2629 // is a no-op or an extend and not a truncate.
2630 if (!isa<ScalableVectorType>(I->getType()) &&
2631 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2632 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2633 return isKnownNonZero(I->getOperand(0), Q, Depth);
2634 break;
2635 case Instruction::Sub:
2636 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2637 I->getOperand(1));
2638 case Instruction::Or:
2639 // X | Y != 0 if X != 0 or Y != 0.
2640 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
2641 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2642 case Instruction::SExt:
2643 case Instruction::ZExt:
2644 // ext X != 0 if X != 0.
2645 return isKnownNonZero(I->getOperand(0), Q, Depth);
2646
2647 case Instruction::Shl: {
2648 // shl nsw/nuw can't remove any non-zero bits.
2649 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
2650 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
2651 return isKnownNonZero(I->getOperand(0), Q, Depth);
2652
2653 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
2654 // if the lowest bit is shifted off the end.
2655 KnownBits Known(BitWidth);
2656 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth, Q);
2657 if (Known.One[0])
2658 return true;
2659
2660 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
2661 }
2662 case Instruction::LShr:
2663 case Instruction::AShr: {
2664 // shr exact can only shift out zero bits.
2665 const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(I);
2666 if (BO->isExact())
2667 return isKnownNonZero(I->getOperand(0), Q, Depth);
2668
2669 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
2670 // defined if the sign bit is shifted off the end.
2671 KnownBits Known =
2672 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
2673 if (Known.isNegative())
2674 return true;
2675
2676 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
2677 }
2678 case Instruction::UDiv:
2679 case Instruction::SDiv: {
2680 // X / Y
2681 // div exact can only produce a zero if the dividend is zero.
2682 if (cast<PossiblyExactOperator>(I)->isExact())
2683 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2684
2685 std::optional<bool> XUgeY;
2686 KnownBits XKnown =
2687 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
2688 // If X is fully unknown we won't be able to figure anything out so don't
2689 // both computing knownbits for Y.
2690 if (XKnown.isUnknown())
2691 return false;
2692
2693 KnownBits YKnown =
2694 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
2695 if (I->getOpcode() == Instruction::SDiv) {
2696 // For signed division need to compare abs value of the operands.
2697 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
2698 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
2699 }
2700 // If X u>= Y then div is non zero (0/0 is UB).
2701 XUgeY = KnownBits::uge(XKnown, YKnown);
2702 // If X is total unknown or X u< Y we won't be able to prove non-zero
2703 // with compute known bits so just return early.
2704 return XUgeY && *XUgeY;
2705 }
2706 case Instruction::Add: {
2707 // X + Y.
2708
2709 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
2710 // non-zero.
2711 auto *BO = cast<OverflowingBinaryOperator>(I);
2712 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2713 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
2714 Q.IIQ.hasNoUnsignedWrap(BO));
2715 }
2716 case Instruction::Mul: {
2717 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
2718 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2719 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
2720 Q.IIQ.hasNoUnsignedWrap(BO));
2721 }
2722 case Instruction::Select: {
2723 // (C ? X : Y) != 0 if X != 0 and Y != 0.
2724
2725 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
2726 // then see if the select condition implies the arm is non-zero. For example
2727 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
2728 // dominated by `X != 0`.
2729 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
2730 Value *Op;
2731 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
2732 // Op is trivially non-zero.
2733 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
2734 return true;
2735
2736 // The condition of the select dominates the true/false arm. Check if the
2737 // condition implies that a given arm is non-zero.
2738 Value *X;
2739 CmpInst::Predicate Pred;
2740 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
2741 return false;
2742
2743 if (!IsTrueArm)
2744 Pred = ICmpInst::getInversePredicate(Pred);
2745
2746 return cmpExcludesZero(Pred, X);
2747 };
2748
2749 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
2750 SelectArmIsNonZero(/* IsTrueArm */ false))
2751 return true;
2752 break;
2753 }
2754 case Instruction::PHI: {
2755 auto *PN = cast<PHINode>(I);
2757 return true;
2758
2759 // Check if all incoming values are non-zero using recursion.
2760 SimplifyQuery RecQ = Q;
2761 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2762 return llvm::all_of(PN->operands(), [&](const Use &U) {
2763 if (U.get() == PN)
2764 return true;
2765 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2766 // Check if the branch on the phi excludes zero.
2767 ICmpInst::Predicate Pred;
2768 Value *X;
2769 BasicBlock *TrueSucc, *FalseSucc;
2770 if (match(RecQ.CxtI,
2771 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
2772 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
2773 // Check for cases of duplicate successors.
2774 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
2775 // If we're using the false successor, invert the predicate.
2776 if (FalseSucc == PN->getParent())
2777 Pred = CmpInst::getInversePredicate(Pred);
2778 if (cmpExcludesZero(Pred, X))
2779 return true;
2780 }
2781 }
2782 // Finally recurse on the edge and check it directly.
2783 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
2784 });
2785 }
2786 case Instruction::InsertElement: {
2787 if (isa<ScalableVectorType>(I->getType()))
2788 break;
2789
2790 const Value *Vec = I->getOperand(0);
2791 const Value *Elt = I->getOperand(1);
2792 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2793
2794 unsigned NumElts = DemandedElts.getBitWidth();
2795 APInt DemandedVecElts = DemandedElts;
2796 bool SkipElt = false;
2797 // If we know the index we are inserting too, clear it from Vec check.
2798 if (CIdx && CIdx->getValue().ult(NumElts)) {
2799 DemandedVecElts.clearBit(CIdx->getZExtValue());
2800 SkipElt = !DemandedElts[CIdx->getZExtValue()];
2801 }
2802
2803 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
2804 // are non-zero.
2805 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
2806 (DemandedVecElts.isZero() ||
2807 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
2808 }
2809 case Instruction::ExtractElement:
2810 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
2811 const Value *Vec = EEI->getVectorOperand();
2812 const Value *Idx = EEI->getIndexOperand();
2813 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2814 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
2815 unsigned NumElts = VecTy->getNumElements();
2816 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2817 if (CIdx && CIdx->getValue().ult(NumElts))
2818 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2819 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
2820 }
2821 }
2822 break;
2823 case Instruction::ShuffleVector: {
2824 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2825 if (!Shuf)
2826 break;
2827 APInt DemandedLHS, DemandedRHS;
2828 // For undef elements, we don't know anything about the common state of
2829 // the shuffle result.
2830 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
2831 break;
2832 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
2833 return (DemandedRHS.isZero() ||
2834 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
2835 (DemandedLHS.isZero() ||
2836 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
2837 }
2838 case Instruction::Freeze:
2839 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
2840 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2841 Depth);
2842 case Instruction::Load: {
2843 auto *LI = cast<LoadInst>(I);
2844 // A Load tagged with nonnull or dereferenceable with null pointer undefined
2845 // is never null.
2846 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
2847 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
2848 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
2849 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
2850 return true;
2851 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
2853 }
2854
2855 // No need to fall through to computeKnownBits as range metadata is already
2856 // handled in isKnownNonZero.
2857 return false;
2858 }
2859 case Instruction::ExtractValue: {
2860 const WithOverflowInst *WO;
2861 if (match(I, m_ExtractValue<0>(m_WithOverflowInst(WO)))) {
2862 switch (WO->getBinaryOp()) {
2863 default:
2864 break;
2865 case Instruction::Add:
2866 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
2867 WO->getArgOperand(0), WO->getArgOperand(1),
2868 /*NSW=*/false,
2869 /*NUW=*/false);
2870 case Instruction::Sub:
2871 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
2872 WO->getArgOperand(0), WO->getArgOperand(1));
2873 case Instruction::Mul:
2874 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth,
2875 WO->getArgOperand(0), WO->getArgOperand(1),
2876 /*NSW=*/false, /*NUW=*/false);
2877 break;
2878 }
2879 }
2880 break;
2881 }
2882 case Instruction::Call:
2883 case Instruction::Invoke: {
2884 const auto *Call = cast<CallBase>(I);
2885 if (I->getType()->isPointerTy()) {
2886 if (Call->isReturnNonNull())
2887 return true;
2888 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
2889 return isKnownNonZero(RP, Q, Depth);
2890 } else {
2891 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
2893 if (std::optional<ConstantRange> Range = Call->getRange()) {
2894 const APInt ZeroValue(Range->getBitWidth(), 0);
2895 if (!Range->contains(ZeroValue))
2896 return true;
2897 }
2898 if (const Value *RV = Call->getReturnedArgOperand())
2899 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
2900 return true;
2901 }
2902
2903 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2904 switch (II->getIntrinsicID()) {
2905 case Intrinsic::sshl_sat:
2906 case Intrinsic::ushl_sat:
2907 case Intrinsic::abs:
2908 case Intrinsic::bitreverse:
2909 case Intrinsic::bswap:
2910 case Intrinsic::ctpop:
2911 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
2912 // NB: We don't do usub_sat here as in any case we can prove its
2913 // non-zero, we will fold it to `sub nuw` in InstCombine.
2914 case Intrinsic::ssub_sat:
2915 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
2916 II->getArgOperand(0), II->getArgOperand(1));
2917 case Intrinsic::sadd_sat:
2918 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
2919 II->getArgOperand(0), II->getArgOperand(1),
2920 /*NSW=*/true, /* NUW=*/false);
2921 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
2922 case Intrinsic::vector_reduce_or:
2923 case Intrinsic::vector_reduce_umax:
2924 case Intrinsic::vector_reduce_umin:
2925 case Intrinsic::vector_reduce_smax:
2926 case Intrinsic::vector_reduce_smin:
2927 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
2928 case Intrinsic::umax:
2929 case Intrinsic::uadd_sat:
2930 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
2931 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
2932 case Intrinsic::smax: {
2933 // If either arg is strictly positive the result is non-zero. Otherwise
2934 // the result is non-zero if both ops are non-zero.
2935 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
2936 const KnownBits &OpKnown) {
2937 if (!OpNonZero.has_value())
2938 OpNonZero = OpKnown.isNonZero() ||
2939 isKnownNonZero(Op, DemandedElts, Q, Depth);
2940 return *OpNonZero;
2941 };
2942 // Avoid re-computing isKnownNonZero.
2943 std::optional<bool> Op0NonZero, Op1NonZero;
2944 KnownBits Op1Known =
2945 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
2946 if (Op1Known.isNonNegative() &&
2947 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
2948 return true;
2949 KnownBits Op0Known =
2950 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
2951 if (Op0Known.isNonNegative() &&
2952 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
2953 return true;
2954 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
2955 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
2956 }
2957 case Intrinsic::smin: {
2958 // If either arg is negative the result is non-zero. Otherwise
2959 // the result is non-zero if both ops are non-zero.
2960 KnownBits Op1Known =
2961 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
2962 if (Op1Known.isNegative())
2963 return true;
2964 KnownBits Op0Known =
2965 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
2966 if (Op0Known.isNegative())
2967 return true;
2968
2969 if (Op1Known.isNonZero() && Op0Known.isNonZero())
2970 return true;
2971 }
2972 [[fallthrough]];
2973 case Intrinsic::umin:
2974 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
2975 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
2976 case Intrinsic::cttz:
2977 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
2978 .Zero[0];
2979 case Intrinsic::ctlz:
2980 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
2981 .isNonNegative();
2982 case Intrinsic::fshr:
2983 case Intrinsic::fshl:
2984 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
2985 if (II->getArgOperand(0) == II->getArgOperand(1))
2986 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
2987 break;
2988 case Intrinsic::vscale:
2989 return true;
2990 case Intrinsic::experimental_get_vector_length:
2991 return isKnownNonZero(I->getOperand(0), Q, Depth);
2992 default:
2993 break;
2994 }
2995 break;
2996 }
2997
2998 return false;
2999 }
3000 }
3001
3002 KnownBits Known(BitWidth);
3003 computeKnownBits(I, DemandedElts, Known, Depth, Q);
3004 return Known.One != 0;
3005}
3006
3007/// Return true if the given value is known to be non-zero when defined. For
3008/// vectors, return true if every demanded element is known to be non-zero when
3009/// defined. For pointers, if the context instruction and dominator tree are
3010/// specified, perform context-sensitive analysis and return true if the
3011/// pointer couldn't possibly be null at the specified instruction.
3012/// Supports values with integer or pointer type and vectors of integers.
3013bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3014 const SimplifyQuery &Q, unsigned Depth) {
3015 Type *Ty = V->getType();
3016
3017#ifndef NDEBUG
3018 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3019
3020 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3021 assert(
3022 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3023 "DemandedElt width should equal the fixed vector number of elements");
3024 } else {
3025 assert(DemandedElts == APInt(1, 1) &&
3026 "DemandedElt width should be 1 for scalars");
3027 }
3028#endif
3029
3030 if (auto *C = dyn_cast<Constant>(V)) {
3031 if (C->isNullValue())
3032 return false;
3033 if (isa<ConstantInt>(C))
3034 // Must be non-zero due to null test above.
3035 return true;
3036
3037 // For constant vectors, check that all elements are poison or known
3038 // non-zero to determine that the whole vector is known non-zero.
3039 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3040 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3041 if (!DemandedElts[i])
3042 continue;
3043 Constant *Elt = C->getAggregateElement(i);
3044 if (!Elt || Elt->isNullValue())
3045 return false;
3046 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3047 return false;
3048 }
3049 return true;
3050 }
3051
3052 // A global variable in address space 0 is non null unless extern weak
3053 // or an absolute symbol reference. Other address spaces may have null as a
3054 // valid address for a global, so we can't assume anything.
3055 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3056 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3057 GV->getType()->getAddressSpace() == 0)
3058 return true;
3059 }
3060
3061 // For constant expressions, fall through to the Operator code below.
3062 if (!isa<ConstantExpr>(V))
3063 return false;
3064 }
3065
3066 if (const auto *A = dyn_cast<Argument>(V))
3067 if (std::optional<ConstantRange> Range = A->getRange()) {
3068 const APInt ZeroValue(Range->getBitWidth(), 0);
3069 if (!Range->contains(ZeroValue))
3070 return true;
3071 }
3072
3073 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3074 return true;
3075
3076 // Some of the tests below are recursive, so bail out if we hit the limit.
3078 return false;
3079
3080 // Check for pointer simplifications.
3081
3082 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3083 // A byval, inalloca may not be null in a non-default addres space. A
3084 // nonnull argument is assumed never 0.
3085 if (const Argument *A = dyn_cast<Argument>(V)) {
3086 if (((A->hasPassPointeeByValueCopyAttr() &&
3087 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3088 A->hasNonNullAttr()))
3089 return true;
3090 }
3091 }
3092
3093 if (const auto *I = dyn_cast<Operator>(V))
3094 if (isKnownNonZeroFromOperator(I, DemandedElts, Depth, Q))
3095 return true;
3096
3097 if (!isa<Constant>(V) &&
3099 return true;
3100
3101 return false;
3102}
3103
3105 unsigned Depth) {
3106 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3107 APInt DemandedElts =
3108 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3109 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3110}
3111
3112/// If the pair of operators are the same invertible function, return the
3113/// the operands of the function corresponding to each input. Otherwise,
3114/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3115/// every input value to exactly one output value. This is equivalent to
3116/// saying that Op1 and Op2 are equal exactly when the specified pair of
3117/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3118static std::optional<std::pair<Value*, Value*>>
3120 const Operator *Op2) {
3121 if (Op1->getOpcode() != Op2->getOpcode())
3122 return std::nullopt;
3123
3124 auto getOperands = [&](unsigned OpNum) -> auto {
3125 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3126 };
3127
3128 switch (Op1->getOpcode()) {
3129 default:
3130 break;
3131 case Instruction::Or:
3132 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3133 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3134 break;
3135 [[fallthrough]];
3136 case Instruction::Xor:
3137 case Instruction::Add: {
3138 Value *Other;
3139 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3140 return std::make_pair(Op1->getOperand(1), Other);
3141 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3142 return std::make_pair(Op1->getOperand(0), Other);
3143 break;
3144 }
3145 case Instruction::Sub:
3146 if (Op1->getOperand(0) == Op2->getOperand(0))
3147 return getOperands(1);
3148 if (Op1->getOperand(1) == Op2->getOperand(1))
3149 return getOperands(0);
3150 break;
3151 case Instruction::Mul: {
3152 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3153 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3154 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3155 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3156 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3157 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3158 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3159 break;
3160
3161 // Assume operand order has been canonicalized
3162 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3163 isa<ConstantInt>(Op1->getOperand(1)) &&
3164 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3165 return getOperands(0);
3166 break;
3167 }
3168 case Instruction::Shl: {
3169 // Same as multiplies, with the difference that we don't need to check
3170 // for a non-zero multiply. Shifts always multiply by non-zero.
3171 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3172 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3173 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3174 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3175 break;
3176
3177 if (Op1->getOperand(1) == Op2->getOperand(1))
3178 return getOperands(0);
3179 break;
3180 }
3181 case Instruction::AShr:
3182 case Instruction::LShr: {
3183 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3184 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3185 if (!PEO1->isExact() || !PEO2->isExact())
3186 break;
3187
3188 if (Op1->getOperand(1) == Op2->getOperand(1))
3189 return getOperands(0);
3190 break;
3191 }
3192 case Instruction::SExt:
3193 case Instruction::ZExt:
3194 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3195 return getOperands(0);
3196 break;
3197 case Instruction::PHI: {
3198 const PHINode *PN1 = cast<PHINode>(Op1);
3199 const PHINode *PN2 = cast<PHINode>(Op2);
3200
3201 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3202 // are a single invertible function of the start values? Note that repeated
3203 // application of an invertible function is also invertible
3204 BinaryOperator *BO1 = nullptr;
3205 Value *Start1 = nullptr, *Step1 = nullptr;
3206 BinaryOperator *BO2 = nullptr;
3207 Value *Start2 = nullptr, *Step2 = nullptr;
3208 if (PN1->getParent() != PN2->getParent() ||
3209 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3210 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3211 break;
3212
3213 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3214 cast<Operator>(BO2));
3215 if (!Values)
3216 break;
3217
3218 // We have to be careful of mutually defined recurrences here. Ex:
3219 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3220 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3221 // The invertibility of these is complicated, and not worth reasoning
3222 // about (yet?).
3223 if (Values->first != PN1 || Values->second != PN2)
3224 break;
3225
3226 return std::make_pair(Start1, Start2);
3227 }
3228 }
3229 return std::nullopt;
3230}
3231
3232/// Return true if V1 == (binop V2, X), where X is known non-zero.
3233/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3234/// implies V2 != V1.
3235static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3236 unsigned Depth, const SimplifyQuery &Q) {
3237 const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
3238 if (!BO)
3239 return false;
3240 switch (BO->getOpcode()) {
3241 default:
3242 break;
3243 case Instruction::Or:
3244 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3245 break;
3246 [[fallthrough]];
3247 case Instruction::Xor:
3248 case Instruction::Add:
3249 Value *Op = nullptr;
3250 if (V2 == BO->getOperand(0))
3251 Op = BO->getOperand(1);
3252 else if (V2 == BO->getOperand(1))
3253 Op = BO->getOperand(0);
3254 else
3255 return false;
3256 return isKnownNonZero(Op, Q, Depth + 1);
3257 }
3258 return false;
3259}
3260
3261/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3262/// the multiplication is nuw or nsw.
3263static bool isNonEqualMul(const Value *V1, const Value *V2, unsigned Depth,
3264 const SimplifyQuery &Q) {
3265 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3266 const APInt *C;
3267 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3268 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3269 !C->isZero() && !C->isOne() && isKnownNonZero(V1, Q, Depth + 1);
3270 }
3271 return false;
3272}
3273
3274/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3275/// the shift is nuw or nsw.
3276static bool isNonEqualShl(const Value *V1, const Value *V2, unsigned Depth,
3277 const SimplifyQuery &Q) {
3278 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3279 const APInt *C;
3280 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3281 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3282 !C->isZero() && isKnownNonZero(V1, Q, Depth + 1);
3283 }
3284 return false;
3285}
3286
3287static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3288 unsigned Depth, const SimplifyQuery &Q) {
3289 // Check two PHIs are in same block.
3290 if (PN1->getParent() != PN2->getParent())
3291 return false;
3292
3294 bool UsedFullRecursion = false;
3295 for (const BasicBlock *IncomBB : PN1->blocks()) {
3296 if (!VisitedBBs.insert(IncomBB).second)
3297 continue; // Don't reprocess blocks that we have dealt with already.
3298 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3299 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3300 const APInt *C1, *C2;
3301 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3302 continue;
3303
3304 // Only one pair of phi operands is allowed for full recursion.
3305 if (UsedFullRecursion)
3306 return false;
3307
3308 SimplifyQuery RecQ = Q;
3309 RecQ.CxtI = IncomBB->getTerminator();
3310 if (!isKnownNonEqual(IV1, IV2, Depth + 1, RecQ))
3311 return false;
3312 UsedFullRecursion = true;
3313 }
3314 return true;
3315}
3316
3317static bool isNonEqualSelect(const Value *V1, const Value *V2, unsigned Depth,
3318 const SimplifyQuery &Q) {
3319 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3320 if (!SI1)
3321 return false;
3322
3323 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3324 const Value *Cond1 = SI1->getCondition();
3325 const Value *Cond2 = SI2->getCondition();
3326 if (Cond1 == Cond2)
3327 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3328 Depth + 1, Q) &&
3329 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3330 Depth + 1, Q);
3331 }
3332 return isKnownNonEqual(SI1->getTrueValue(), V2, Depth + 1, Q) &&
3333 isKnownNonEqual(SI1->getFalseValue(), V2, Depth + 1, Q);
3334}
3335
3336// Check to see if A is both a GEP and is the incoming value for a PHI in the
3337// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3338// one of them being the recursive GEP A and the other a ptr at same base and at
3339// the same/higher offset than B we are only incrementing the pointer further in
3340// loop if offset of recursive GEP is greater than 0.
3342 const SimplifyQuery &Q) {
3343 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3344 return false;
3345
3346 auto *GEPA = dyn_cast<GEPOperator>(A);
3347 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3348 return false;
3349
3350 // Handle 2 incoming PHI values with one being a recursive GEP.
3351 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3352 if (!PN || PN->getNumIncomingValues() != 2)
3353 return false;
3354
3355 // Search for the recursive GEP as an incoming operand, and record that as
3356 // Step.
3357 Value *Start = nullptr;
3358 Value *Step = const_cast<Value *>(A);
3359 if (PN->getIncomingValue(0) == Step)
3360 Start = PN->getIncomingValue(1);
3361 else if (PN->getIncomingValue(1) == Step)
3362 Start = PN->getIncomingValue(0);
3363 else
3364 return false;
3365
3366 // Other incoming node base should match the B base.
3367 // StartOffset >= OffsetB && StepOffset > 0?
3368 // StartOffset <= OffsetB && StepOffset < 0?
3369 // Is non-equal if above are true.
3370 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3371 // optimisation to inbounds GEPs only.
3372 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3373 APInt StartOffset(IndexWidth, 0);
3374 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3375 APInt StepOffset(IndexWidth, 0);
3376 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3377
3378 // Check if Base Pointer of Step matches the PHI.
3379 if (Step != PN)
3380 return false;
3381 APInt OffsetB(IndexWidth, 0);
3382 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3383 return Start == B &&
3384 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
3385 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
3386}
3387
3388/// Return true if it is known that V1 != V2.
3389static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
3390 const SimplifyQuery &Q) {
3391 if (V1 == V2)
3392 return false;
3393 if (V1->getType() != V2->getType())
3394 // We can't look through casts yet.
3395 return false;
3396
3398 return false;
3399
3400 // See if we can recurse through (exactly one of) our operands. This
3401 // requires our operation be 1-to-1 and map every input value to exactly
3402 // one output value. Such an operation is invertible.
3403 auto *O1 = dyn_cast<Operator>(V1);
3404 auto *O2 = dyn_cast<Operator>(V2);
3405 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3406 if (auto Values = getInvertibleOperands(O1, O2))
3407 return isKnownNonEqual(Values->first, Values->second, Depth + 1, Q);
3408
3409 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3410 const PHINode *PN2 = cast<PHINode>(V2);
3411 // FIXME: This is missing a generalization to handle the case where one is
3412 // a PHI and another one isn't.
3413 if (isNonEqualPHIs(PN1, PN2, Depth, Q))
3414 return true;
3415 };
3416 }
3417
3418 if (isModifyingBinopOfNonZero(V1, V2, Depth, Q) ||
3419 isModifyingBinopOfNonZero(V2, V1, Depth, Q))
3420 return true;
3421
3422 if (isNonEqualMul(V1, V2, Depth, Q) || isNonEqualMul(V2, V1, Depth, Q))
3423 return true;
3424
3425 if (isNonEqualShl(V1, V2, Depth, Q) || isNonEqualShl(V2, V1, Depth, Q))
3426 return true;
3427
3428 if (V1->getType()->isIntOrIntVectorTy()) {
3429 // Are any known bits in V1 contradictory to known bits in V2? If V1
3430 // has a known zero where V2 has a known one, they must not be equal.
3431 KnownBits Known1 = computeKnownBits(V1, Depth, Q);
3432 if (!Known1.isUnknown()) {
3433 KnownBits Known2 = computeKnownBits(V2, Depth, Q);
3434 if (Known1.Zero.intersects(Known2.One) ||
3435 Known2.Zero.intersects(Known1.One))
3436 return true;
3437 }
3438 }
3439
3440 if (isNonEqualSelect(V1, V2, Depth, Q) || isNonEqualSelect(V2, V1, Depth, Q))
3441 return true;
3442
3443 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3445 return true;
3446
3447 Value *A, *B;
3448 // PtrToInts are NonEqual if their Ptrs are NonEqual.
3449 // Check PtrToInt type matches the pointer size.
3450 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
3452 return isKnownNonEqual(A, B, Depth + 1, Q);
3453
3454 return false;
3455}
3456
3457// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
3458// Returns the input and lower/upper bounds.
3459static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
3460 const APInt *&CLow, const APInt *&CHigh) {
3461 assert(isa<Operator>(Select) &&
3462 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
3463 "Input should be a Select!");
3464
3465 const Value *LHS = nullptr, *RHS = nullptr;
3467 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
3468 return false;
3469
3470 if (!match(RHS, m_APInt(CLow)))
3471 return false;
3472
3473 const Value *LHS2 = nullptr, *RHS2 = nullptr;
3475 if (getInverseMinMaxFlavor(SPF) != SPF2)
3476 return false;
3477
3478 if (!match(RHS2, m_APInt(CHigh)))
3479 return false;
3480
3481 if (SPF == SPF_SMIN)
3482 std::swap(CLow, CHigh);
3483
3484 In = LHS2;
3485 return CLow->sle(*CHigh);
3486}
3487
3489 const APInt *&CLow,
3490 const APInt *&CHigh) {
3491 assert((II->getIntrinsicID() == Intrinsic::smin ||
3492 II->getIntrinsicID() == Intrinsic::smax) && "Must be smin/smax");
3493
3495 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
3496 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
3497 !match(II->getArgOperand(1), m_APInt(CLow)) ||
3498 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
3499 return false;
3500
3501 if (II->getIntrinsicID() == Intrinsic::smin)
3502 std::swap(CLow, CHigh);
3503 return CLow->sle(*CHigh);
3504}
3505
3506/// For vector constants, loop over the elements and find the constant with the
3507/// minimum number of sign bits. Return 0 if the value is not a vector constant
3508/// or if any element was not analyzed; otherwise, return the count for the
3509/// element with the minimum number of sign bits.
3511 const APInt &DemandedElts,
3512 unsigned TyBits) {
3513 const auto *CV = dyn_cast<Constant>(V);
3514 if (!CV || !isa<FixedVectorType>(CV->getType()))
3515 return 0;
3516
3517 unsigned MinSignBits = TyBits;
3518 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3519 for (unsigned i = 0; i != NumElts; ++i) {
3520 if (!DemandedElts[i])
3521 continue;
3522 // If we find a non-ConstantInt, bail out.
3523 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3524 if (!Elt)
3525 return 0;
3526
3527 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3528 }
3529
3530 return MinSignBits;
3531}
3532
3533static unsigned ComputeNumSignBitsImpl(const Value *V,
3534 const APInt &DemandedElts,
3535 unsigned Depth, const SimplifyQuery &Q);
3536
3537static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
3538 unsigned Depth, const SimplifyQuery &Q) {
3539 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q);
3540 assert(Result > 0 && "At least one sign bit needs to be present!");
3541 return Result;
3542}
3543
3544/// Return the number of times the sign bit of the register is replicated into
3545/// the other bits. We know that at least 1 bit is always equal to the sign bit
3546/// (itself), but other cases can give us information. For example, immediately
3547/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
3548/// other, so we return 3. For vectors, return the number of sign bits for the
3549/// vector element with the minimum number of known sign bits of the demanded
3550/// elements in the vector specified by DemandedElts.
3551static unsigned ComputeNumSignBitsImpl(const Value *V,
3552 const APInt &DemandedElts,
3553 unsigned Depth, const SimplifyQuery &Q) {
3554 Type *Ty = V->getType();
3555#ifndef NDEBUG
3556 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3557
3558 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3559 assert(
3560 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3561 "DemandedElt width should equal the fixed vector number of elements");
3562 } else {
3563 assert(DemandedElts == APInt(1, 1) &&
3564 "DemandedElt width should be 1 for scalars");
3565 }
3566#endif
3567
3568 // We return the minimum number of sign bits that are guaranteed to be present
3569 // in V, so for undef we have to conservatively return 1. We don't have the
3570 // same behavior for poison though -- that's a FIXME today.
3571
3572 Type *ScalarTy = Ty->getScalarType();
3573 unsigned TyBits = ScalarTy->isPointerTy() ?
3574 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
3575 Q.DL.getTypeSizeInBits(ScalarTy);
3576
3577 unsigned Tmp, Tmp2;
3578 unsigned FirstAnswer = 1;
3579
3580 // Note that ConstantInt is handled by the general computeKnownBits case
3581 // below.
3582
3584 return 1;
3585
3586 if (auto *U = dyn_cast<Operator>(V)) {
3587 switch (Operator::getOpcode(V)) {
3588 default: break;
3589 case Instruction::SExt:
3590 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
3591 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp;
3592
3593 case Instruction::SDiv: {
3594 const APInt *Denominator;
3595 // sdiv X, C -> adds log(C) sign bits.
3596 if (match(U->getOperand(1), m_APInt(Denominator))) {
3597
3598 // Ignore non-positive denominator.
3599 if (!Denominator->isStrictlyPositive())
3600 break;
3601
3602 // Calculate the incoming numerator bits.
3603 unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3604
3605 // Add floor(log(C)) bits to the numerator bits.
3606 return std::min(TyBits, NumBits + Denominator->logBase2());
3607 }
3608 break;
3609 }
3610
3611 case Instruction::SRem: {
3612 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3613
3614 const APInt *Denominator;
3615 // srem X, C -> we know that the result is within [-C+1,C) when C is a
3616 // positive constant. This let us put a lower bound on the number of sign
3617 // bits.
3618 if (match(U->getOperand(1), m_APInt(Denominator))) {
3619
3620 // Ignore non-positive denominator.
3621 if (Denominator->isStrictlyPositive()) {
3622 // Calculate the leading sign bit constraints by examining the
3623 // denominator. Given that the denominator is positive, there are two
3624 // cases:
3625 //
3626 // 1. The numerator is positive. The result range is [0,C) and
3627 // [0,C) u< (1 << ceilLogBase2(C)).
3628 //
3629 // 2. The numerator is negative. Then the result range is (-C,0] and
3630 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
3631 //
3632 // Thus a lower bound on the number of sign bits is `TyBits -
3633 // ceilLogBase2(C)`.
3634
3635 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
3636 Tmp = std::max(Tmp, ResBits);
3637 }
3638 }
3639 return Tmp;
3640 }
3641
3642 case Instruction::AShr: {
3643 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3644 // ashr X, C -> adds C sign bits. Vectors too.
3645 const APInt *ShAmt;
3646 if (match(U->getOperand(1), m_APInt(ShAmt))) {
3647 if (ShAmt->uge(TyBits))
3648 break; // Bad shift.
3649 unsigned ShAmtLimited = ShAmt->getZExtValue();
3650 Tmp += ShAmtLimited;
3651 if (Tmp > TyBits) Tmp = TyBits;
3652 }
3653 return Tmp;
3654 }
3655 case Instruction::Shl: {
3656 const APInt *ShAmt;
3657 if (match(U->getOperand(1), m_APInt(ShAmt))) {
3658 // shl destroys sign bits.
3659 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3660 if (ShAmt->uge(TyBits) || // Bad shift.
3661 ShAmt->uge(Tmp)) break; // Shifted all sign bits out.
3662 Tmp2 = ShAmt->getZExtValue();
3663 return Tmp - Tmp2;
3664 }
3665 break;
3666 }
3667 case Instruction::And:
3668 case Instruction::Or:
3669 case Instruction::Xor: // NOT is handled here.
3670 // Logical binary ops preserve the number of sign bits at the worst.
3671 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3672 if (Tmp != 1) {
3673 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3674 FirstAnswer = std::min(Tmp, Tmp2);
3675 // We computed what we know about the sign bits as our first
3676 // answer. Now proceed to the generic code that uses
3677 // computeKnownBits, and pick whichever answer is better.
3678 }
3679 break;
3680
3681 case Instruction::Select: {
3682 // If we have a clamp pattern, we know that the number of sign bits will
3683 // be the minimum of the clamp min/max range.
3684 const Value *X;
3685 const APInt *CLow, *CHigh;
3686 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
3687 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
3688
3689 Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3690 if (Tmp == 1) break;
3691 Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q);
3692 return std::min(Tmp, Tmp2);
3693 }
3694
3695 case Instruction::Add:
3696 // Add can have at most one carry bit. Thus we know that the output
3697 // is, at worst, one more bit than the inputs.
3698 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3699 if (Tmp == 1) break;
3700
3701 // Special case decrementing a value (ADD X, -1):
3702 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
3703 if (CRHS->isAllOnesValue()) {
3704 KnownBits Known(TyBits);
3705 computeKnownBits(U->getOperand(0), Known, Depth + 1, Q);
3706
3707 // If the input is known to be 0 or 1, the output is 0/-1, which is
3708 // all sign bits set.
3709 if ((Known.Zero | 1).isAllOnes())
3710 return TyBits;
3711
3712 // If we are subtracting one from a positive number, there is no carry
3713 // out of the result.
3714 if (Known.isNonNegative())
3715 return Tmp;
3716 }
3717
3718 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3719 if (Tmp2 == 1) break;
3720 return std::min(Tmp, Tmp2) - 1;
3721
3722 case Instruction::Sub:
3723 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3724 if (Tmp2 == 1) break;
3725
3726 // Handle NEG.
3727 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
3728 if (CLHS->isNullValue()) {
3729 KnownBits Known(TyBits);
3730 computeKnownBits(U->getOperand(1), Known, Depth + 1, Q);
3731 // If the input is known to be 0 or 1, the output is 0/-1, which is
3732 // all sign bits set.
3733 if ((Known.Zero | 1).isAllOnes())
3734 return TyBits;
3735
3736 // If the input is known to be positive (the sign bit is known clear),
3737 // the output of the NEG has the same number of sign bits as the
3738 // input.
3739 if (Known.isNonNegative())
3740 return Tmp2;
3741
3742 // Otherwise, we treat this like a SUB.
3743 }
3744
3745 // Sub can have at most one carry bit. Thus we know that the output
3746 // is, at worst, one more bit than the inputs.
3747 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3748 if (Tmp == 1) break;
3749 return std::min(Tmp, Tmp2) - 1;
3750
3751 case Instruction::Mul: {
3752 // The output of the Mul can be at most twice the valid bits in the
3753 // inputs.
3754 unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3755 if (SignBitsOp0 == 1) break;
3756 unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3757 if (SignBitsOp1 == 1) break;
3758 unsigned OutValidBits =
3759 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
3760 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
3761 }
3762
3763 case Instruction::PHI: {
3764 const PHINode *PN = cast<PHINode>(U);
3765 unsigned NumIncomingValues = PN->getNumIncomingValues();
3766 // Don't analyze large in-degree PHIs.
3767 if (NumIncomingValues > 4) break;
3768 // Unreachable blocks may have zero-operand PHI nodes.
3769 if (NumIncomingValues == 0) break;
3770
3771 // Take the minimum of all incoming values. This can't infinitely loop
3772 // because of our depth threshold.
3773 SimplifyQuery RecQ = Q;
3774 Tmp = TyBits;
3775 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
3776 if (Tmp == 1) return Tmp;
3777 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
3778 Tmp = std::min(
3779 Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, RecQ));
3780 }
3781 return Tmp;
3782 }
3783
3784 case Instruction::Trunc: {
3785 // If the input contained enough sign bits that some remain after the
3786 // truncation, then we can make use of that. Otherwise we don't know
3787 // anything.
3788 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3789 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
3790 if (Tmp > (OperandTyBits - TyBits))
3791 return Tmp - (OperandTyBits - TyBits);
3792
3793 return 1;
3794 }
3795
3796 case Instruction::ExtractElement:
3797 // Look through extract element. At the moment we keep this simple and
3798 // skip tracking the specific element. But at least we might find
3799 // information valid for all elements of the vector (for example if vector
3800 // is sign extended, shifted, etc).
3801 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3802
3803 case Instruction::ShuffleVector: {
3804 // Collect the minimum number of sign bits that are shared by every vector
3805 // element referenced by the shuffle.
3806 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
3807 if (!Shuf) {
3808 // FIXME: Add support for shufflevector constant expressions.
3809 return 1;
3810 }
3811 APInt DemandedLHS, DemandedRHS;
3812 // For undef elements, we don't know anything about the common state of
3813 // the shuffle result.
3814 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3815 return 1;
3816 Tmp = std::numeric_limits<unsigned>::max();
3817 if (!!DemandedLHS) {
3818 const Value *LHS = Shuf->getOperand(0);
3819 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q);
3820 }
3821 // If we don't know anything, early out and try computeKnownBits
3822 // fall-back.
3823 if (Tmp == 1)
3824 break;
3825 if (!!DemandedRHS) {
3826 const Value *RHS = Shuf->getOperand(1);
3827 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q);
3828 Tmp = std::min(Tmp, Tmp2);
3829 }
3830 // If we don't know anything, early out and try computeKnownBits
3831 // fall-back.
3832 if (Tmp == 1)
3833 break;
3834 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
3835 return Tmp;
3836 }
3837 case Instruction::Call: {
3838 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
3839 switch (II->getIntrinsicID()) {
3840 default: break;
3841 case Intrinsic::abs:
3842 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3843 if (Tmp == 1) break;
3844
3845 // Absolute value reduces number of sign bits by at most 1.
3846 return Tmp - 1;
3847 case Intrinsic::smin:
3848 case Intrinsic::smax: {
3849 const APInt *CLow, *CHigh;
3850 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
3851 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
3852 }
3853 }
3854 }
3855 }
3856 }
3857 }
3858
3859 // Finally, if we can prove that the top bits of the result are 0's or 1's,
3860 // use this information.
3861
3862 // If we can examine all elements of a vector constant successfully, we're
3863 // done (we can't do any better than that). If not, keep trying.
3864 if (unsigned VecSignBits =
3865 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
3866 return VecSignBits;
3867
3868 KnownBits Known(TyBits);
3869 computeKnownBits(V, DemandedElts, Known, Depth, Q);
3870
3871 // If we know that the sign bit is either zero or one, determine the number of
3872 // identical bits in the top of the input value.
3873 return std::max(FirstAnswer, Known.countMinSignBits());
3874}
3875
3877 const TargetLibraryInfo *TLI) {
3878 const Function *F = CB.getCalledFunction();
3879 if (!F)
3881
3882 if (F->isIntrinsic())
3883 return F->getIntrinsicID();
3884
3885 // We are going to infer semantics of a library function based on mapping it
3886 // to an LLVM intrinsic. Check that the library function is available from
3887 // this callbase and in this environment.
3888 LibFunc Func;
3889 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
3890 !CB.onlyReadsMemory())
3892
3893 switch (Func) {
3894 default:
3895 break;
3896 case LibFunc_sin:
3897 case LibFunc_sinf:
3898 case LibFunc_sinl:
3899 return Intrinsic::sin;
3900 case LibFunc_cos:
3901 case LibFunc_cosf:
3902 case LibFunc_cosl:
3903 return Intrinsic::cos;
3904 case LibFunc_exp:
3905 case LibFunc_expf:
3906 case LibFunc_expl:
3907 return Intrinsic::exp;
3908 case LibFunc_exp2:
3909 case LibFunc_exp2f:
3910 case LibFunc_exp2l:
3911 return Intrinsic::exp2;
3912 case LibFunc_log:
3913 case LibFunc_logf:
3914 case LibFunc_logl:
3915 return Intrinsic::log;
3916 case LibFunc_log10:
3917 case LibFunc_log10f:
3918 case LibFunc_log10l:
3919 return Intrinsic::log10;
3920 case LibFunc_log2:
3921 case LibFunc_log2f:
3922 case LibFunc_log2l:
3923 return Intrinsic::log2;
3924 case LibFunc_fabs:
3925 case LibFunc_fabsf:
3926 case LibFunc_fabsl:
3927 return Intrinsic::fabs;
3928 case LibFunc_fmin:
3929 case LibFunc_fminf:
3930 case LibFunc_fminl:
3931 return Intrinsic::minnum;
3932 case LibFunc_fmax:
3933 case LibFunc_fmaxf:
3934 case LibFunc_fmaxl:
3935 return Intrinsic::maxnum;
3936 case LibFunc_copysign:
3937 case LibFunc_copysignf:
3938 case LibFunc_copysignl:
3939 return Intrinsic::copysign;
3940 case LibFunc_floor:
3941 case LibFunc_floorf:
3942 case LibFunc_floorl:
3943 return Intrinsic::floor;
3944 case LibFunc_ceil:
3945 case LibFunc_ceilf:
3946 case LibFunc_ceill:
3947 return Intrinsic::ceil;
3948 case LibFunc_trunc:
3949 case LibFunc_truncf:
3950 case LibFunc_truncl:
3951 return Intrinsic::trunc;
3952 case LibFunc_rint:
3953 case LibFunc_rintf:
3954 case LibFunc_rintl:
3955 return Intrinsic::rint;
3956 case LibFunc_nearbyint:
3957 case LibFunc_nearbyintf:
3958 case LibFunc_nearbyintl:
3959 return Intrinsic::nearbyint;
3960 case LibFunc_round:
3961 case LibFunc_roundf:
3962 case LibFunc_roundl:
3963 return Intrinsic::round;
3964 case LibFunc_roundeven:
3965 case LibFunc_roundevenf:
3966 case LibFunc_roundevenl:
3967 return Intrinsic::roundeven;
3968 case LibFunc_pow:
3969 case LibFunc_powf:
3970 case LibFunc_powl:
3971 return Intrinsic::pow;
3972 case LibFunc_sqrt:
3973 case LibFunc_sqrtf:
3974 case LibFunc_sqrtl:
3975 return Intrinsic::sqrt;
3976 }
3977
3979}
3980
3981/// Return true if it's possible to assume IEEE treatment of input denormals in
3982/// \p F for \p Val.
3983static bool inputDenormalIsIEEE(const Function &F, const Type *Ty) {
3984 Ty = Ty->getScalarType();
3985 return F.getDenormalMode(Ty->getFltSemantics()).Input == DenormalMode::IEEE;
3986}
3987
3988static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
3989 Ty = Ty->getScalarType();
3990 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
3991 return Mode.Input == DenormalMode::IEEE ||
3992 Mode.Input == DenormalMode::PositiveZero;
3993}
3994
3995static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
3996 Ty = Ty->getScalarType();
3997 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
3998 return Mode.Output == DenormalMode::IEEE ||
3999 Mode.Output == DenormalMode::PositiveZero;
4000}
4001
4003 return isKnownNeverZero() &&
4005}
4006
4008 Type *Ty) const {
4009 return isKnownNeverNegZero() &&
4011}
4012
4014 Type *Ty) const {
4015 if (!isKnownNeverPosZero())
4016 return false;
4017
4018 // If we know there are no denormals, nothing can be flushed to zero.
4020 return true;
4021
4022 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4023 switch (Mode.Input) {
4024 case DenormalMode::IEEE:
4025 return true;
4027 // Negative subnormal won't flush to +0
4028 return isKnownNeverPosSubnormal();
4030 default:
4031 // Both positive and negative subnormal could flush to +0
4032 return false;
4033 }
4034
4035 llvm_unreachable("covered switch over denormal mode");
4036}
4037
4039 Type *Ty) {
4040 KnownFPClasses = Src.KnownFPClasses;
4041 // If we aren't assuming the source can't be a zero, we don't have to check if
4042 // a denormal input could be flushed.
4043 if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero())
4044 return;
4045
4046 // If we know the input can't be a denormal, it can't be flushed to 0.
4047 if (Src.isKnownNeverSubnormal())
4048 return;
4049
4050 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4051
4052 if (!Src.isKnownNeverPosSubnormal() && Mode != DenormalMode::getIEEE())
4054
4055 if (!Src.isKnownNeverNegSubnormal() && Mode != DenormalMode::getIEEE()) {
4056 if (Mode != DenormalMode::getPositiveZero())
4058
4059 if (Mode.Input == DenormalMode::PositiveZero ||
4060 Mode.Output == DenormalMode::PositiveZero ||
4061 Mode.Input == DenormalMode::Dynamic ||
4062 Mode.Output == DenormalMode::Dynamic)
4064 }
4065}
4066
4068 const Function &F, Type *Ty) {
4069 propagateDenormal(Src, F, Ty);
4070 propagateNaN(Src, /*PreserveSign=*/true);
4071}
4072
4073/// Given an exploded icmp instruction, return true if the comparison only
4074/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4075/// the result of the comparison is true when the input value is signed.
4077 bool &TrueIfSigned) {
4078 switch (Pred) {
4079 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4080 TrueIfSigned = true;
4081 return RHS.isZero();
4082 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4083 TrueIfSigned = true;
4084 return RHS.isAllOnes();
4085 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4086 TrueIfSigned = false;
4087 return RHS.isAllOnes();
4088 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4089 TrueIfSigned = false;
4090 return RHS.isZero();
4091 case ICmpInst::ICMP_UGT:
4092 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4093 TrueIfSigned = true;
4094 return RHS.isMaxSignedValue();
4095 case ICmpInst::ICMP_UGE:
4096 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4097 TrueIfSigned = true;
4098 return RHS.isMinSignedValue();
4099 case ICmpInst::ICMP_ULT:
4100 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4101 TrueIfSigned = false;
4102 return RHS.isMinSignedValue();
4103 case ICmpInst::ICMP_ULE:
4104 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4105 TrueIfSigned = false;
4106 return RHS.isMaxSignedValue();
4107 default:
4108 return false;
4109 }
4110}
4111
4112/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
4113/// same result as an fcmp with the given operands.
4114std::pair<Value *, FPClassTest> llvm::fcmpToClassTest(FCmpInst::Predicate Pred,
4115 const Function &F,
4116 Value *LHS, Value *RHS,
4117 bool LookThroughSrc) {
4118 const APFloat *ConstRHS;
4119 if (!match(RHS, m_APFloatAllowUndef(ConstRHS)))
4120 return {nullptr, fcAllFlags};
4121
4122 return fcmpToClassTest(Pred, F, LHS, ConstRHS, LookThroughSrc);
4123}
4124
4125std::pair<Value *, FPClassTest>
4127 const APFloat *ConstRHS, bool LookThroughSrc) {
4128
4129 auto [Src, ClassIfTrue, ClassIfFalse] =
4130 fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4131 if (Src && ClassIfTrue == ~ClassIfFalse)
4132 return {Src, ClassIfTrue};
4133 return {nullptr, fcAllFlags};
4134}
4135
4136/// Return the return value for fcmpImpliesClass for a compare that produces an
4137/// exact class test.
4138static std::tuple<Value *, FPClassTest, FPClassTest> exactClass(Value *V,
4139 FPClassTest M) {
4140 return {V, M, ~M};
4141}
4142
4143std::tuple<Value *, FPClassTest, FPClassTest>
4145 FPClassTest RHSClass, bool LookThroughSrc) {
4146 assert(RHSClass != fcNone);
4147 Value *Src = LHS;
4148
4149 if (Pred == FCmpInst::FCMP_TRUE)
4150 return exactClass(Src, fcAllFlags);
4151
4152 if (Pred == FCmpInst::FCMP_FALSE)
4153 return exactClass(Src, fcNone);
4154
4155 const FPClassTest OrigClass = RHSClass;
4156
4157 const bool IsNegativeRHS = (RHSClass & fcNegative) == RHSClass;
4158 const bool IsPositiveRHS = (RHSClass & fcPositive) == RHSClass;
4159 const bool IsNaN = (RHSClass & ~fcNan) == fcNone;
4160
4161 if (IsNaN) {
4162 // fcmp o__ x, nan -> false
4163 // fcmp u__ x, nan -> true
4164 return exactClass(Src, CmpInst::isOrdered(Pred) ? fcNone : fcAllFlags);
4165 }
4166
4167 // fcmp ord x, zero|normal|subnormal|inf -> ~fcNan
4168 if (Pred == FCmpInst::FCMP_ORD)
4169 return exactClass(Src, ~fcNan);
4170
4171 // fcmp uno x, zero|normal|subnormal|inf -> fcNan
4172 if (Pred == FCmpInst::FCMP_UNO)
4173 return exactClass(Src, fcNan);
4174
4175 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4176 if (IsFabs)
4177 RHSClass = llvm::inverse_fabs(RHSClass);
4178
4179 const bool IsZero = (OrigClass & fcZero) == OrigClass;
4180 if (IsZero) {
4181 assert(Pred != FCmpInst::FCMP_ORD && Pred != FCmpInst::FCMP_UNO);
4182 // Compares with fcNone are only exactly equal to fcZero if input denormals
4183 // are not flushed.
4184 // TODO: Handle DAZ by expanding masks to cover subnormal cases.
4185 if (!inputDenormalIsIEEE(F, LHS->getType()))
4186 return {nullptr, fcAllFlags, fcAllFlags};
4187
4188 switch (Pred) {
4189 case FCmpInst::FCMP_OEQ: // Match x == 0.0
4190 return exactClass(Src, fcZero);
4191 case FCmpInst::FCMP_UEQ: // Match isnan(x) || (x == 0.0)
4192 return exactClass(Src, fcZero | fcNan);
4193 case FCmpInst::FCMP_UNE: // Match (x != 0.0)
4194 return exactClass(Src, ~fcZero);
4195 case FCmpInst::FCMP_ONE: // Match !isnan(x) && x != 0.0
4196 return exactClass(Src, ~fcNan & ~fcZero);
4197 case FCmpInst::FCMP_ORD:
4198 // Canonical form of ord/uno is with a zero. We could also handle
4199 // non-canonical other non-NaN constants or LHS == RHS.
4200 return exactClass(Src, ~fcNan);
4201 case FCmpInst::FCMP_UNO:
4202 return exactClass(Src, fcNan);
4203 case FCmpInst::FCMP_OGT: // x > 0
4205 case FCmpInst::FCMP_UGT: // isnan(x) || x > 0
4207 case FCmpInst::FCMP_OGE: // x >= 0
4208 return exactClass(Src, fcPositive | fcNegZero);
4209 case FCmpInst::FCMP_UGE: // isnan(x) || x >= 0
4210 return exactClass(Src, fcPositive | fcNegZero | fcNan);
4211 case FCmpInst::FCMP_OLT: // x < 0
4213 case FCmpInst::FCMP_ULT: // isnan(x) || x < 0
4215 case FCmpInst::FCMP_OLE: // x <= 0
4216 return exactClass(Src, fcNegative | fcPosZero);
4217 case FCmpInst::FCMP_ULE: // isnan(x) || x <= 0
4218 return exactClass(Src, fcNegative | fcPosZero | fcNan);
4219 default:
4220 llvm_unreachable("all compare types are handled");
4221 }
4222
4223 return {nullptr, fcAllFlags, fcAllFlags};
4224 }
4225
4226 const bool IsDenormalRHS = (OrigClass & fcSubnormal) == OrigClass;
4227
4228 const bool IsInf = (OrigClass & fcInf) == OrigClass;
4229 if (IsInf) {
4230 FPClassTest Mask = fcAllFlags;
4231
4232 switch (Pred) {
4233 case FCmpInst::FCMP_OEQ:
4234 case FCmpInst::FCMP_UNE: {
4235 // Match __builtin_isinf patterns
4236 //
4237 // fcmp oeq x, +inf -> is_fpclass x, fcPosInf
4238 // fcmp oeq fabs(x), +inf -> is_fpclass x, fcInf
4239 // fcmp oeq x, -inf -> is_fpclass x, fcNegInf
4240 // fcmp oeq fabs(x), -inf -> is_fpclass x, 0 -> false
4241 //
4242 // fcmp une x, +inf -> is_fpclass x, ~fcPosInf
4243 // fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf
4244 // fcmp une x, -inf -> is_fpclass x, ~fcNegInf
4245 // fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true
4246 if (IsNegativeRHS) {
4247 Mask = fcNegInf;
4248 if (IsFabs)
4249 Mask = fcNone;
4250 } else {
4251 Mask = fcPosInf;
4252 if (IsFabs)
4253 Mask |= fcNegInf;
4254 }
4255 break;
4256 }
4257 case FCmpInst::FCMP_ONE:
4258 case FCmpInst::FCMP_UEQ: {
4259 // Match __builtin_isinf patterns
4260 // fcmp one x, -inf -> is_fpclass x, fcNegInf
4261 // fcmp one fabs(x), -inf -> is_fpclass x, ~fcNegInf & ~fcNan
4262 // fcmp one x, +inf -> is_fpclass x, ~fcNegInf & ~fcNan
4263 // fcmp one fabs(x), +inf -> is_fpclass x, ~fcInf & fcNan
4264 //
4265 // fcmp ueq x, +inf -> is_fpclass x, fcPosInf|fcNan
4266 // fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan
4267 // fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan
4268 // fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan
4269 if (IsNegativeRHS) {
4270 Mask = ~fcNegInf & ~fcNan;
4271 if (IsFabs)
4272 Mask = ~fcNan;
4273 } else {
4274 Mask = ~fcPosInf & ~fcNan;
4275 if (IsFabs)
4276 Mask &= ~fcNegInf;
4277 }
4278
4279 break;
4280 }
4281 case FCmpInst::FCMP_OLT:
4282 case FCmpInst::FCMP_UGE: {
4283 if (IsNegativeRHS) {
4284 // No value is ordered and less than negative infinity.
4285 // All values are unordered with or at least negative infinity.
4286 // fcmp olt x, -inf -> false
4287 // fcmp uge x, -inf -> true
4288 Mask = fcNone;
4289 break;
4290 }
4291
4292 // fcmp olt fabs(x), +inf -> fcFinite
4293 // fcmp uge fabs(x), +inf -> ~fcFinite
4294 // fcmp olt x, +inf -> fcFinite|fcNegInf
4295 // fcmp uge x, +inf -> ~(fcFinite|fcNegInf)
4296 Mask = fcFinite;
4297 if (!IsFabs)
4298 Mask |= fcNegInf;
4299 break;
4300 }
4301 case FCmpInst::FCMP_OGE:
4302 case FCmpInst::FCMP_ULT: {
4303 if (IsNegativeRHS) {
4304 // fcmp oge x, -inf -> ~fcNan
4305 // fcmp oge fabs(x), -inf -> ~fcNan
4306 // fcmp ult x, -inf -> fcNan
4307 // fcmp ult fabs(x), -inf -> fcNan
4308 Mask = ~fcNan;
4309 break;
4310 }
4311
4312 // fcmp oge fabs(x), +inf -> fcInf
4313 // fcmp oge x, +inf -> fcPosInf
4314 // fcmp ult fabs(x), +inf -> ~fcInf
4315 // fcmp ult x, +inf -> ~fcPosInf
4316 Mask = fcPosInf;
4317 if (IsFabs)
4318 Mask |= fcNegInf;
4319 break;
4320 }
4321 case FCmpInst::FCMP_OGT:
4322 case FCmpInst::FCMP_ULE: {
4323 if (IsNegativeRHS) {
4324 // fcmp ogt x, -inf -> fcmp one x, -inf
4325 // fcmp ogt fabs(x), -inf -> fcmp ord x, x
4326 // fcmp ule x, -inf -> fcmp ueq x, -inf
4327 // fcmp ule fabs(x), -inf -> fcmp uno x, x
4328 Mask = IsFabs ? ~fcNan : ~(fcNegInf | fcNan);
4329 break;
4330 }
4331
4332 // No value is ordered and greater than infinity.
4333 Mask = fcNone;
4334 break;
4335 }
4336 case FCmpInst::FCMP_OLE:
4337 case FCmpInst::FCMP_UGT: {
4338 if (IsNegativeRHS) {
4339 Mask = IsFabs ? fcNone : fcNegInf;
4340 break;
4341 }
4342
4343 // fcmp ole x, +inf -> fcmp ord x, x
4344 // fcmp ole fabs(x), +inf -> fcmp ord x, x
4345 // fcmp ole x, -inf -> fcmp oeq x, -inf
4346 // fcmp ole fabs(x), -inf -> false
4347 Mask = ~fcNan;
4348 break;
4349 }
4350 default:
4351 llvm_unreachable("all compare types are handled");
4352 }
4353
4354 // Invert the comparison for the unordered cases.
4355 if (FCmpInst::isUnordered(Pred))
4356 Mask = ~Mask;
4357
4358 return exactClass(Src, Mask);
4359 }
4360
4361 if (Pred == FCmpInst::FCMP_OEQ)
4362 return {Src, RHSClass, fcAllFlags};
4363
4364 if (Pred == FCmpInst::FCMP_UEQ) {
4365 FPClassTest Class = RHSClass | fcNan;
4366 return {Src, Class, ~fcNan};
4367 }
4368
4369 if (Pred == FCmpInst::FCMP_ONE)
4370 return {Src, ~fcNan, RHSClass | fcNan};
4371
4372 if (Pred == FCmpInst::FCMP_UNE)
4373 return {Src, fcAllFlags, RHSClass};
4374
4375 assert((RHSClass == fcNone || RHSClass == fcPosNormal ||
4376 RHSClass == fcNegNormal || RHSClass == fcNormal ||
4377 RHSClass == fcPosSubnormal || RHSClass == fcNegSubnormal ||
4378 RHSClass == fcSubnormal) &&
4379 "should have been recognized as an exact class test");
4380
4381 if (IsNegativeRHS) {
4382 // TODO: Handle fneg(fabs)
4383 if (IsFabs) {
4384 // fabs(x) o> -k -> fcmp ord x, x
4385 // fabs(x) u> -k -> true
4386 // fabs(x) o< -k -> false
4387 // fabs(x) u< -k -> fcmp uno x, x
4388 switch (Pred) {
4389 case FCmpInst::FCMP_OGT:
4390 case FCmpInst::FCMP_OGE:
4391 return {Src, ~fcNan, fcNan};
4392 case FCmpInst::FCMP_UGT:
4393 case FCmpInst::FCMP_UGE:
4394 return {Src, fcAllFlags, fcNone};
4395 case FCmpInst::FCMP_OLT:
4396 case FCmpInst::FCMP_OLE:
4397 return {Src, fcNone, fcAllFlags};
4398 case FCmpInst::FCMP_ULT:
4399 case FCmpInst::FCMP_ULE:
4400 return {Src, fcNan, ~fcNan};
4401 default:
4402 break;
4403 }
4404
4405 return {nullptr, fcAllFlags, fcAllFlags};
4406 }
4407
4408 FPClassTest ClassesLE = fcNegInf | fcNegNormal;
4410
4411 if (IsDenormalRHS)
4412 ClassesLE |= fcNegSubnormal;
4413 else
4414 ClassesGE |= fcNegNormal;
4415
4416 switch (Pred) {
4417 case FCmpInst::FCMP_OGT:
4418 case FCmpInst::FCMP_OGE:
4419 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4420 case FCmpInst::FCMP_UGT:
4421 case FCmpInst::FCMP_UGE:
4422 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4423 case FCmpInst::FCMP_OLT:
4424 case FCmpInst::FCMP_OLE:
4425 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4426 case FCmpInst::FCMP_ULT:
4427 case FCmpInst::FCMP_ULE:
4428 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4429 default:
4430 break;
4431 }
4432 } else if (IsPositiveRHS) {
4433 FPClassTest ClassesGE = fcPosNormal | fcPosInf;
4435 if (IsDenormalRHS)
4436 ClassesGE |= fcPosSubnormal;
4437 else
4438 ClassesLE |= fcPosNormal;
4439
4440 if (IsFabs) {
4441 ClassesGE = llvm::inverse_fabs(ClassesGE);
4442 ClassesLE = llvm::inverse_fabs(ClassesLE);
4443 }
4444
4445 switch (Pred) {
4446 case FCmpInst::FCMP_OGT:
4447 case FCmpInst::FCMP_OGE:
4448 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4449 case FCmpInst::FCMP_UGT:
4450 case FCmpInst::FCMP_UGE:
4451 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4452 case FCmpInst::FCMP_OLT:
4453 case FCmpInst::FCMP_OLE:
4454 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4455 case FCmpInst::FCMP_ULT:
4456 case FCmpInst::FCMP_ULE:
4457 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4458 default:
4459 break;
4460 }
4461 }
4462
4463 return {nullptr, fcAllFlags, fcAllFlags};
4464}
4465
4466std::tuple<Value *, FPClassTest, FPClassTest>
4468 const APFloat &ConstRHS, bool LookThroughSrc) {
4469 // We can refine checks against smallest normal / largest denormal to an
4470 // exact class test.
4471 if (!ConstRHS.isNegative() && ConstRHS.isSmallestNormalized()) {
4472 Value *Src = LHS;
4473 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4474
4475 FPClassTest Mask;
4476 // Match pattern that's used in __builtin_isnormal.
4477 switch (Pred) {
4478 case FCmpInst::FCMP_OLT:
4479 case FCmpInst::FCMP_UGE: {
4480 // fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero
4481 // fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero
4482 // fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf
4483 // fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero)
4484 Mask = fcZero | fcSubnormal;
4485 if (!IsFabs)
4486 Mask |= fcNegNormal | fcNegInf;
4487
4488 break;
4489 }
4490 case FCmpInst::FCMP_OGE:
4491 case FCmpInst::FCMP_ULT: {
4492 // fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf
4493 // fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal
4494 // fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf)
4495 // fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal)
4496 Mask = fcPosInf | fcPosNormal;
4497 if (IsFabs)
4498 Mask |= fcNegInf | fcNegNormal;
4499 break;
4500 }
4501 default:
4502 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(),
4503 LookThroughSrc);
4504 }
4505
4506 // Invert the comparison for the unordered cases.
4507 if (FCmpInst::isUnordered(Pred))
4508 Mask = ~Mask;
4509
4510 return exactClass(Src, Mask);
4511 }
4512
4513 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(), LookThroughSrc);
4514}
4515
4516std::tuple<Value *, FPClassTest, FPClassTest>
4518 Value *RHS, bool LookThroughSrc) {
4519 const APFloat *ConstRHS;
4520 if (!match(RHS, m_APFloatAllowUndef(ConstRHS)))
4521 return {nullptr, fcAllFlags, fcAllFlags};
4522
4523 // TODO: Just call computeKnownFPClass for RHS to handle non-constants.
4524 return fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4525}
4526
4528 bool CondIsTrue,
4529 const Instruction *CxtI,
4530 KnownFPClass &KnownFromContext) {
4531 CmpInst::Predicate Pred;
4532 Value *LHS;
4533 uint64_t ClassVal = 0;
4534 const APFloat *CRHS;
4535 const APInt *RHS;
4536 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4537 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4538 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4539 if (CmpVal == V)
4540 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4541 } else if (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(
4542 m_Value(LHS), m_ConstantInt(ClassVal)))) {
4543 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4544 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4545 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Value(LHS)),
4546 m_APInt(RHS)))) {
4547 bool TrueIfSigned;
4548 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4549 return;
4550 if (TrueIfSigned == CondIsTrue)
4551 KnownFromContext.signBitMustBeOne();
4552 else
4553 KnownFromContext.signBitMustBeZero();
4554 }
4555}
4556
4558 const SimplifyQuery &Q) {
4559 KnownFPClass KnownFromContext;
4560
4561 if (!Q.CxtI)
4562 return KnownFromContext;
4563
4564 if (Q.DC && Q.DT) {
4565 // Handle dominating conditions.
4566 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4567 Value *Cond = BI->getCondition();
4568
4569 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4570 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4571 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4572 KnownFromContext);
4573
4574 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4575 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4576 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4577 KnownFromContext);
4578 }
4579 }
4580
4581 if (!Q.AC)
4582 return KnownFromContext;
4583
4584 // Try to restrict the floating-point classes based on information from
4585 // assumptions.
4586 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4587 if (!AssumeVH)
4588 continue;
4589 CallInst *I = cast<CallInst>(AssumeVH);
4590
4591 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4592 "Got assumption for the wrong function!");
4593 assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
4594 "must be an assume intrinsic");
4595
4596 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4597 continue;
4598
4599 computeKnownFPClassFromCond(V, I->getArgOperand(0), /*CondIsTrue=*/true,
4600 Q.CxtI, KnownFromContext);
4601 }
4602
4603 return KnownFromContext;
4604}
4605
4606void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4607 FPClassTest InterestedClasses, KnownFPClass &Known,
4608 unsigned Depth, const SimplifyQuery &Q);
4609
4610static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4611 FPClassTest InterestedClasses, unsigned Depth,
4612 const SimplifyQuery &Q) {
4613 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4614 APInt DemandedElts =
4615 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4616 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Depth, Q);
4617}
4618
4620 const APInt &DemandedElts,
4621 FPClassTest InterestedClasses,
4622 KnownFPClass &Known, unsigned Depth,
4623 const SimplifyQuery &Q) {
4624 if ((InterestedClasses &
4626 return;
4627
4628 KnownFPClass KnownSrc;
4629 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4630 KnownSrc, Depth + 1, Q);
4631
4632 // Sign should be preserved
4633 // TODO: Handle cannot be ordered greater than zero
4634 if (KnownSrc.cannotBeOrderedLessThanZero())
4636
4637 Known.propagateNaN(KnownSrc, true);
4638
4639 // Infinity needs a range check.
4640}
4641
4642void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4643 FPClassTest InterestedClasses, KnownFPClass &Known,
4644 unsigned Depth, const SimplifyQuery &Q) {
4645 assert(Known.isUnknown() && "should not be called with known information");
4646
4647 if (!DemandedElts) {
4648 // No demanded elts, better to assume we don't know anything.
4649 Known.resetAll();
4650 return;
4651 }
4652
4653 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4654
4655 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4656 Known.KnownFPClasses = CFP->getValueAPF().classify();
4657 Known.SignBit = CFP->isNegative();
4658 return;
4659 }
4660
4661 if (isa<ConstantAggregateZero>(V)) {
4662 Known.KnownFPClasses = fcPosZero;
4663 Known.SignBit = false;
4664 return;
4665 }
4666
4667 if (isa<PoisonValue>(V)) {
4668 Known.KnownFPClasses = fcNone;
4669 Known.SignBit = false;
4670 return;
4671 }
4672
4673 // Try to handle fixed width vector constants
4674 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
4675 const Constant *CV = dyn_cast<Constant>(V);
4676 if (VFVTy && CV) {
4677 Known.KnownFPClasses = fcNone;
4678 bool SignBitAllZero = true;
4679 bool SignBitAllOne = true;
4680
4681 // For vectors, verify that each element is not NaN.
4682 unsigned NumElts = VFVTy->getNumElements();
4683 for (unsigned i = 0; i != NumElts; ++i) {
4684 if (!DemandedElts[i])
4685 continue;
4686
4687 Constant *Elt = CV->getAggregateElement(i);
4688 if (!Elt) {
4689 Known = KnownFPClass();
4690 return;
4691 }
4692 if (isa<UndefValue>(Elt))
4693 continue;
4694 auto *CElt = dyn_cast<ConstantFP>(Elt);
4695 if (!CElt) {
4696 Known = KnownFPClass();
4697 return;
4698 }
4699
4700 const APFloat &C = CElt->getValueAPF();
4701 Known.KnownFPClasses |= C.classify();
4702 if (C.isNegative())
4703 SignBitAllZero = false;
4704 else
4705 SignBitAllOne = false;
4706 }
4707 if (SignBitAllOne != SignBitAllZero)
4708 Known.SignBit = SignBitAllOne;
4709 return;
4710 }
4711
4712 FPClassTest KnownNotFromFlags = fcNone;
4713 if (const auto *CB = dyn_cast<CallBase>(V))
4714 KnownNotFromFlags |= CB->getRetNoFPClass();
4715 else if (const auto *Arg = dyn_cast<Argument>(V))
4716 KnownNotFromFlags |= Arg->getNoFPClass();
4717
4718 const Operator *Op = dyn_cast<Operator>(V);
4719 if (const FPMathOperator *FPOp = dyn_cast_or_null<FPMathOperator>(Op)) {
4720 if (FPOp->hasNoNaNs())
4721 KnownNotFromFlags |= fcNan;
4722 if (FPOp->hasNoInfs())
4723 KnownNotFromFlags |= fcInf;
4724 }
4725
4726 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
4727 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
4728
4729 // We no longer need to find out about these bits from inputs if we can
4730 // assume this from flags/attributes.
4731 InterestedClasses &= ~KnownNotFromFlags;
4732
4733 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
4734 Known.knownNot(KnownNotFromFlags);
4735 if (!Known.SignBit && AssumedClasses.SignBit) {
4736 if (*AssumedClasses.SignBit)
4737 Known.signBitMustBeOne();
4738 else
4739 Known.signBitMustBeZero();
4740 }
4741 });
4742
4743 if (!Op)
4744 return;
4745
4746 // All recursive calls that increase depth must come after this.
4748 return;
4749
4750 const unsigned Opc = Op->getOpcode();
4751 switch (Opc) {
4752 case Instruction::FNeg: {
4753 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4754 Known, Depth + 1, Q);
4755 Known.fneg();
4756 break;
4757 }
4758 case Instruction::Select: {
4759 Value *Cond = Op->getOperand(0);
4760 Value *LHS = Op->getOperand(1);
4761 Value *RHS = Op->getOperand(2);
4762
4763 FPClassTest FilterLHS = fcAllFlags;
4764 FPClassTest FilterRHS = fcAllFlags;
4765
4766 Value *TestedValue = nullptr;
4767 FPClassTest MaskIfTrue = fcAllFlags;
4768 FPClassTest MaskIfFalse = fcAllFlags;
4769 uint64_t ClassVal = 0;
4770 const Function *F = cast<Instruction>(Op)->getFunction();
4771 CmpInst::Predicate Pred;
4772 Value *CmpLHS, *CmpRHS;
4773 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
4774 // If the select filters out a value based on the class, it no longer
4775 // participates in the class of the result
4776
4777 // TODO: In some degenerate cases we can infer something if we try again
4778 // without looking through sign operations.
4779 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
4780 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
4781 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
4782 } else if (match(Cond,
4783 m_Intrinsic<Intrinsic::is_fpclass>(
4784 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
4785 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
4786 MaskIfTrue = TestedMask;
4787 MaskIfFalse = ~TestedMask;
4788 }
4789
4790 if (TestedValue == LHS) {
4791 // match !isnan(x) ? x : y
4792 FilterLHS = MaskIfTrue;
4793 } else if (TestedValue == RHS) { // && IsExactClass
4794 // match !isnan(x) ? y : x
4795 FilterRHS = MaskIfFalse;
4796 }
4797
4798 KnownFPClass Known2;
4799 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
4800 Depth + 1, Q);
4801 Known.KnownFPClasses &= FilterLHS;
4802
4803 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
4804 Known2, Depth + 1, Q);
4805 Known2.KnownFPClasses &= FilterRHS;
4806
4807 Known |= Known2;
4808 break;
4809 }
4810 case Instruction::Call: {
4811 const CallInst *II = cast<CallInst>(Op);
4812 const Intrinsic::ID IID = II->getIntrinsicID();
4813 switch (IID) {
4814 case Intrinsic::fabs: {
4815 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
4816 // If we only care about the sign bit we don't need to inspect the
4817 // operand.
4818 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
4819 InterestedClasses, Known, Depth + 1, Q);
4820 }
4821
4822 Known.fabs();
4823 break;
4824 }
4825 case Intrinsic::copysign: {
4826 KnownFPClass KnownSign;
4827
4828 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4829 Known, Depth + 1, Q);
4830 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4831 KnownSign, Depth + 1, Q);
4832 Known.copysign(KnownSign);
4833 break;
4834 }
4835 case Intrinsic::fma:
4836 case Intrinsic::fmuladd: {
4837 if ((InterestedClasses & fcNegative) == fcNone)
4838 break;
4839
4840 if (II->getArgOperand(0) != II->getArgOperand(1))
4841 break;
4842
4843 // The multiply cannot be -0 and therefore the add can't be -0
4844 Known.knownNot(fcNegZero);
4845
4846 // x * x + y is non-negative if y is non-negative.
4847 KnownFPClass KnownAddend;
4848 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
4849 KnownAddend, Depth + 1, Q);
4850
4851 if (KnownAddend.cannotBeOrderedLessThanZero())
4852 Known.knownNot(fcNegative);
4853 break;
4854 }
4855 case Intrinsic::sqrt:
4856 case Intrinsic::experimental_constrained_sqrt: {
4857 KnownFPClass KnownSrc;
4858 FPClassTest InterestedSrcs = InterestedClasses;
4859 if (InterestedClasses & fcNan)
4860 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
4861
4862 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
4863 KnownSrc, Depth + 1, Q);
4864
4865 if (KnownSrc.isKnownNeverPosInfinity())
4866 Known.knownNot(fcPosInf);
4867 if (KnownSrc.isKnownNever(fcSNan))
4868 Known.knownNot(fcSNan);
4869
4870 // Any negative value besides -0 returns a nan.
4871 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
4872 Known.knownNot(fcNan);
4873
4874 // The only negative value that can be returned is -0 for -0 inputs.
4876
4877 // If the input denormal mode could be PreserveSign, a negative
4878 // subnormal input could produce a negative zero output.
4879 const Function *F = II->getFunction();
4880 if (Q.IIQ.hasNoSignedZeros(II) ||
4881 (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))) {
4882 Known.knownNot(fcNegZero);
4883 if (KnownSrc.isKnownNeverNaN())
4884 Known.signBitMustBeZero();
4885 }
4886
4887 break;
4888 }
4889 case Intrinsic::sin:
4890 case Intrinsic::cos: {
4891 // Return NaN on infinite inputs.
4892 KnownFPClass KnownSrc;
4893 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4894 KnownSrc, Depth + 1, Q);
4895 Known.knownNot(fcInf);
4896 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
4897 Known.knownNot(fcNan);
4898 break;
4899 }
4900 case Intrinsic::maxnum:
4901 case Intrinsic::minnum:
4902 case Intrinsic::minimum:
4903 case Intrinsic::maximum: {
4904 KnownFPClass KnownLHS, KnownRHS;
4905 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4906 KnownLHS, Depth + 1, Q);
4907 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4908 KnownRHS, Depth + 1, Q);
4909
4910 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
4911 Known = KnownLHS | KnownRHS;
4912
4913 // If either operand is not NaN, the result is not NaN.
4914 if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum))
4915 Known.knownNot(fcNan);
4916
4917 if (IID == Intrinsic::maxnum) {
4918 // If at least one operand is known to be positive, the result must be
4919 // positive.
4920 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
4921 KnownLHS.isKnownNeverNaN()) ||
4922 (KnownRHS.cannotBeOrderedLessThanZero() &&
4923 KnownRHS.isKnownNeverNaN()))
4925 } else if (IID == Intrinsic::maximum) {
4926 // If at least one operand is known to be positive, the result must be
4927 // positive.
4928 if (KnownLHS.cannotBeOrderedLessThanZero() ||
4929 KnownRHS.cannotBeOrderedLessThanZero())
4931 } else if (IID == Intrinsic::minnum) {
4932 // If at least one operand is known to be negative, the result must be
4933 // negative.
4934 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
4935 KnownLHS.isKnownNeverNaN()) ||
4936 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
4937 KnownRHS.isKnownNeverNaN()))
4939 } else {
4940 // If at least one operand is known to be negative, the result must be
4941 // negative.
4942 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
4945 }
4946
4947 // Fixup zero handling if denormals could be returned as a zero.
4948 //
4949 // As there's no spec for denormal flushing, be conservative with the
4950 // treatment of denormals that could be flushed to zero. For older
4951 // subtargets on AMDGPU the min/max instructions would not flush the
4952 // output and return the original value.
4953 //
4954 if ((Known.KnownFPClasses & fcZero) != fcNone &&
4955 !Known.isKnownNeverSubnormal()) {
4956 const Function *Parent = II->getFunction();
4957 if (!Parent)
4958 break;
4959
4960 DenormalMode Mode = Parent->getDenormalMode(
4962 if (Mode != DenormalMode::getIEEE())
4963 Known.KnownFPClasses |= fcZero;
4964 }
4965
4966 if (Known.isKnownNeverNaN()) {
4967 if (KnownLHS.SignBit && KnownRHS.SignBit &&
4968 *KnownLHS.SignBit == *KnownRHS.SignBit) {
4969 if (*KnownLHS.SignBit)
4970 Known.signBitMustBeOne();
4971 else
4972 Known.signBitMustBeZero();
4973 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum) ||
4974 ((KnownLHS.isKnownNeverNegZero() ||
4975 KnownRHS.isKnownNeverPosZero()) &&
4976 (KnownLHS.isKnownNeverPosZero() ||
4977 KnownRHS.isKnownNeverNegZero()))) {
4978 if ((IID == Intrinsic::maximum || IID == Intrinsic::maxnum) &&
4979 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
4980 Known.signBitMustBeZero();
4981 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minnum) &&
4982 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
4983 Known.signBitMustBeOne();
4984 }
4985 }
4986 break;
4987 }
4988 case Intrinsic::canonicalize: {
4989 KnownFPClass KnownSrc;
4990 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4991 KnownSrc, Depth + 1, Q);
4992
4993 // This is essentially a stronger form of
4994 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
4995 // actually have an IR canonicalization guarantee.
4996
4997 // Canonicalize may flush denormals to zero, so we have to consider the
4998 // denormal mode to preserve known-not-0 knowledge.
4999 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5000
5001 // Stronger version of propagateNaN
5002 // Canonicalize is guaranteed to quiet signaling nans.
5003 if (KnownSrc.isKnownNeverNaN())
5004 Known.knownNot(fcNan);
5005 else
5006 Known.knownNot(fcSNan);
5007
5008 const Function *F = II->getFunction();
5009 if (!F)
5010 break;
5011
5012 // If the parent function flushes denormals, the canonical output cannot
5013 // be a denormal.
5014 const fltSemantics &FPType =
5016 DenormalMode DenormMode = F->getDenormalMode(FPType);
5017 if (DenormMode == DenormalMode::getIEEE()) {
5018 if (KnownSrc.isKnownNever(fcPosZero))
5019 Known.knownNot(fcPosZero);
5020 if (KnownSrc.isKnownNever(fcNegZero))
5021 Known.knownNot(fcNegZero);
5022 break;
5023 }
5024
5025 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5026 Known.knownNot(fcSubnormal);
5027
5028 if (DenormMode.Input == DenormalMode::PositiveZero ||
5029 (DenormMode.Output == DenormalMode::PositiveZero &&
5030 DenormMode.Input == DenormalMode::IEEE))
5031 Known.knownNot(fcNegZero);
5032
5033 break;
5034 }
5035 case Intrinsic::trunc:
5036 case Intrinsic::floor:
5037 case Intrinsic::ceil:
5038 case Intrinsic::rint:
5039 case Intrinsic::nearbyint:
5040 case Intrinsic::round:
5041 case Intrinsic::roundeven: {
5042 KnownFPClass KnownSrc;
5043 FPClassTest InterestedSrcs = InterestedClasses;
5044 if (InterestedSrcs & fcPosFinite)
5045 InterestedSrcs |= fcPosFinite;
5046 if (InterestedSrcs & fcNegFinite)
5047 InterestedSrcs |= fcNegFinite;
5048 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5049 KnownSrc, Depth + 1, Q);
5050
5051 // Integer results cannot be subnormal.
5052 Known.knownNot(fcSubnormal);
5053
5054 Known.propagateNaN(KnownSrc, true);
5055
5056 // Pass through infinities, except PPC_FP128 is a special case for
5057 // intrinsics other than trunc.
5058 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5059 if (KnownSrc.isKnownNeverPosInfinity())
5060 Known.knownNot(fcPosInf);
5061 if (KnownSrc.isKnownNeverNegInfinity())
5062 Known.knownNot(fcNegInf);
5063 }
5064
5065 // Negative round ups to 0 produce -0
5066 if (KnownSrc.isKnownNever(fcPosFinite))
5067 Known.knownNot(fcPosFinite);
5068 if (KnownSrc.isKnownNever(fcNegFinite))
5069 Known.knownNot(fcNegFinite);
5070
5071 break;
5072 }
5073 case Intrinsic::exp:
5074 case Intrinsic::exp2:
5075 case Intrinsic::exp10: {
5076 Known.knownNot(fcNegative);
5077 if ((InterestedClasses & fcNan) == fcNone)
5078 break;
5079
5080 KnownFPClass KnownSrc;
5081 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5082 KnownSrc, Depth + 1, Q);
5083 if (KnownSrc.isKnownNeverNaN()) {
5084 Known.knownNot(fcNan);
5085 Known.signBitMustBeZero();
5086 }
5087
5088 break;
5089 }
5090 case Intrinsic::fptrunc_round: {
5091 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5092 Depth, Q);
5093 break;
5094 }
5095 case Intrinsic::log:
5096 case Intrinsic::log10:
5097 case Intrinsic::log2:
5098 case Intrinsic::experimental_constrained_log:
5099 case Intrinsic::experimental_constrained_log10:
5100 case Intrinsic::experimental_constrained_log2: {
5101 // log(+inf) -> +inf
5102 // log([+-]0.0) -> -inf
5103 // log(-inf) -> nan
5104 // log(-x) -> nan
5105 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5106 break;
5107
5108 FPClassTest InterestedSrcs = InterestedClasses;
5109 if ((InterestedClasses & fcNegInf) != fcNone)
5110 InterestedSrcs |= fcZero | fcSubnormal;
5111 if ((InterestedClasses & fcNan) != fcNone)
5112 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5113
5114 KnownFPClass KnownSrc;
5115 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5116 KnownSrc, Depth + 1, Q);
5117
5118 if (KnownSrc.isKnownNeverPosInfinity())
5119 Known.knownNot(fcPosInf);
5120
5121 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5122 Known.knownNot(fcNan);
5123
5124 const Function *F = II->getFunction();
5125 if (F && KnownSrc.isKnownNeverLogicalZero(*F, II->getType()))
5126 Known.knownNot(fcNegInf);
5127
5128 break;
5129 }
5130 case Intrinsic::powi: {
5131 if ((InterestedClasses & fcNegative) == fcNone)
5132 break;
5133
5134 const Value *Exp = II->getArgOperand(1);
5135 Type *ExpTy = Exp->getType();
5136 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5137 KnownBits ExponentKnownBits(BitWidth);
5138 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5139 ExponentKnownBits, Depth + 1, Q);
5140
5141 if (ExponentKnownBits.Zero[0]) { // Is even
5142 Known.knownNot(fcNegative);
5143 break;
5144 }
5145
5146 // Given that exp is an integer, here are the
5147 // ways that pow can return a negative value:
5148 //
5149 // pow(-x, exp) --> negative if exp is odd and x is negative.
5150 // pow(-0, exp) --> -inf if exp is negative odd.
5151 // pow(-0, exp) --> -0 if exp is positive odd.
5152 // pow(-inf, exp) --> -0 if exp is negative odd.
5153 // pow(-inf, exp) --> -inf if exp is positive odd.
5154 KnownFPClass KnownSrc;
5155 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5156 KnownSrc, Depth + 1, Q);
5157 if (KnownSrc.isKnownNever(fcNegative))
5158 Known.knownNot(fcNegative);
5159 break;
5160 }
5161 case Intrinsic::ldexp: {
5162 KnownFPClass KnownSrc;
5163 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5164 KnownSrc, Depth + 1, Q);
5165 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5166
5167 // Sign is preserved, but underflows may produce zeroes.
5168 if (KnownSrc.isKnownNever(fcNegative))
5169 Known.knownNot(fcNegative);
5170 else if (KnownSrc.cannotBeOrderedLessThanZero())
5172
5173 if (KnownSrc.isKnownNever(fcPositive))
5174 Known.knownNot(fcPositive);
5175 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5177
5178 // Can refine inf/zero handling based on the exponent operand.
5179 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5180 if ((InterestedClasses & ExpInfoMask) == fcNone)
5181 break;
5182 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5183 break;
5184
5185 const fltSemantics &Flt =
5187 unsigned Precision = APFloat::semanticsPrecision(Flt);
5188 const Value *ExpArg = II->getArgOperand(1);
5190 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5191
5192 const int MantissaBits = Precision - 1;
5193 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5194 Known.knownNot(fcSubnormal);
5195
5196 const Function *F = II->getFunction();
5197 const APInt *ConstVal = ExpRange.getSingleElement();
5198 if (ConstVal && ConstVal->isZero()) {
5199 // ldexp(x, 0) -> x, so propagate everything.
5200 Known.propagateCanonicalizingSrc(KnownSrc, *F, II->getType());
5201 } else if (ExpRange.isAllNegative()) {
5202 // If we know the power is <= 0, can't introduce inf
5203 if (KnownSrc.isKnownNeverPosInfinity())
5204 Known.knownNot(fcPosInf);
5205 if (KnownSrc.isKnownNeverNegInfinity())
5206 Known.knownNot(fcNegInf);
5207 } else if (ExpRange.isAllNonNegative()) {
5208 // If we know the power is >= 0, can't introduce subnormal or zero
5209 if (KnownSrc.isKnownNeverPosSubnormal())
5210 Known.knownNot(fcPosSubnormal);
5211 if (KnownSrc.isKnownNeverNegSubnormal())
5212 Known.knownNot(fcNegSubnormal);
5213 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, II->getType()))
5214 Known.knownNot(fcPosZero);
5215 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))
5216 Known.knownNot(fcNegZero);
5217 }
5218
5219 break;
5220 }
5221 case Intrinsic::arithmetic_fence: {
5222 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5223 Known, Depth + 1, Q);
5224 break;
5225 }
5226 case Intrinsic::experimental_constrained_sitofp:
5227 case Intrinsic::experimental_constrained_uitofp:
5228 // Cannot produce nan
5229 Known.knownNot(fcNan);
5230
5231 // sitofp and uitofp turn into +0.0 for zero.
5232 Known.knownNot(fcNegZero);
5233
5234 // Integers cannot be subnormal
5235 Known.knownNot(fcSubnormal);
5236
5237 if (IID == Intrinsic::experimental_constrained_uitofp)
5238 Known.signBitMustBeZero();
5239
5240 // TODO: Copy inf handling from instructions
5241 break;
5242 default:
5243 break;
5244 }
5245
5246 break;
5247 }
5248 case Instruction::FAdd:
5249 case Instruction::FSub: {
5250 KnownFPClass KnownLHS, KnownRHS;
5251 bool WantNegative =
5252 Op->getOpcode() == Instruction::FAdd &&
5253 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5254 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5255 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5256
5257 if (!WantNaN && !WantNegative && !WantNegZero)
5258 break;
5259
5260 FPClassTest InterestedSrcs = InterestedClasses;
5261 if (WantNegative)
5262 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5263 if (InterestedClasses & fcNan)
5264 InterestedSrcs |= fcInf;
5265 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5266 KnownRHS, Depth + 1, Q);
5267
5268 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5269 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5270 WantNegZero || Opc == Instruction::FSub) {
5271
5272 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5273 // there's no point.
5274 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5275 KnownLHS, Depth + 1, Q);
5276 // Adding positive and negative infinity produces NaN.
5277 // TODO: Check sign of infinities.
5278 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5279 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5280 Known.knownNot(fcNan);
5281
5282 // FIXME: Context function should always be passed in separately
5283 const Function *F = cast<Instruction>(Op)->getFunction();
5284
5285 if (Op->getOpcode() == Instruction::FAdd) {
5286 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5287 KnownRHS.cannotBeOrderedLessThanZero())
5289 if (!F)
5290 break;
5291
5292 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5293 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5294 KnownRHS.isKnownNeverLogicalNegZero(*F, Op->getType())) &&
5295 // Make sure output negative denormal can't flush to -0
5296 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5297 Known.knownNot(fcNegZero);
5298 } else {
5299 if (!F)
5300 break;
5301
5302 // Only fsub -0, +0 can return -0
5303 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5304 KnownRHS.isKnownNeverLogicalPosZero(*F, Op->getType())) &&
5305 // Make sure output negative denormal can't flush to -0
5306 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5307 Known.knownNot(fcNegZero);
5308 }
5309 }
5310
5311 break;
5312 }
5313 case Instruction::FMul: {
5314 // X * X is always non-negative or a NaN.
5315 if (Op->getOperand(0) == Op->getOperand(1))
5316 Known.knownNot(fcNegative);
5317
5318 if ((InterestedClasses & fcNan) != fcNan)
5319 break;
5320
5321 // fcSubnormal is only needed in case of DAZ.
5322 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5323
5324 KnownFPClass KnownLHS, KnownRHS;
5325 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5326 Depth + 1, Q);
5327 if (!KnownRHS.isKnownNeverNaN())
5328 break;
5329
5330 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5331 Depth + 1, Q);
5332 if (!KnownLHS.isKnownNeverNaN())
5333 break;
5334
5335 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5336 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5337 Known.signBitMustBeZero();
5338 else
5339 Known.signBitMustBeOne();
5340 }
5341
5342 // If 0 * +/-inf produces NaN.
5343 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5344 Known.knownNot(fcNan);
5345 break;
5346 }
5347
5348 const Function *F = cast<Instruction>(Op)->getFunction();
5349 if (!F)
5350 break;
5351
5352 if ((KnownRHS.isKnownNeverInfinity() ||
5353 KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) &&
5354 (KnownLHS.isKnownNeverInfinity() ||
5355 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))
5356 Known.knownNot(fcNan);
5357
5358 break;
5359 }
5360 case Instruction::FDiv:
5361 case Instruction::FRem: {
5362 if (Op->getOperand(0) == Op->getOperand(1)) {
5363 // TODO: Could filter out snan if we inspect the operand
5364 if (Op->getOpcode() == Instruction::FDiv) {
5365 // X / X is always exactly 1.0 or a NaN.
5367 } else {
5368 // X % X is always exactly [+-]0.0 or a NaN.
5369 Known.KnownFPClasses = fcNan | fcZero;
5370 }
5371
5372 break;
5373 }
5374
5375 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5376 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5377 const bool WantPositive =
5378 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5379 if (!WantNan && !WantNegative && !WantPositive)
5380 break;
5381
5382 KnownFPClass KnownLHS, KnownRHS;
5383
5384 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5385 fcNan | fcInf | fcZero | fcNegative, KnownRHS,
5386 Depth + 1, Q);
5387
5388 bool KnowSomethingUseful =
5389 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5390
5391 if (KnowSomethingUseful || WantPositive) {
5392 const FPClassTest InterestedLHS =
5393 WantPositive ? fcAllFlags
5395
5396 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5397 InterestedClasses & InterestedLHS, KnownLHS,
5398 Depth + 1, Q);
5399 }
5400
5401 const Function *F = cast<Instruction>(Op)->getFunction();
5402
5403 if (Op->getOpcode() == Instruction::FDiv) {
5404 // Only 0/0, Inf/Inf produce NaN.
5405 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5406 (KnownLHS.isKnownNeverInfinity() ||
5407 KnownRHS.isKnownNeverInfinity()) &&
5408 ((F && KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) ||
5409 (F && KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))) {
5410 Known.knownNot(fcNan);
5411 }
5412
5413 // X / -0.0 is -Inf (or NaN).
5414 // +X / +X is +X
5415 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5416 Known.knownNot(fcNegative);
5417 } else {
5418 // Inf REM x and x REM 0 produce NaN.
5419 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5420 KnownLHS.isKnownNeverInfinity() && F &&
5421 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())) {
5422 Known.knownNot(fcNan);
5423 }
5424
5425 // The sign for frem is the same as the first operand.
5426 if (KnownLHS.cannotBeOrderedLessThanZero())
5428 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5430
5431 // See if we can be more aggressive about the sign of 0.
5432 if (KnownLHS.isKnownNever(fcNegative))
5433 Known.knownNot(fcNegative);
5434 if (KnownLHS.isKnownNever(fcPositive))
5435 Known.knownNot(fcPositive);
5436 }
5437
5438 break;
5439 }
5440 case Instruction::FPExt: {
5441 // Infinity, nan and zero propagate from source.
5442 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5443 Known, Depth + 1, Q);
5444
5445 const fltSemantics &DstTy =
5446 Op->getType()->getScalarType()->getFltSemantics();
5447 const fltSemantics &SrcTy =
5448 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5449
5450 // All subnormal inputs should be in the normal range in the result type.
5451 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5452 if (Known.KnownFPClasses & fcPosSubnormal)
5453 Known.KnownFPClasses |= fcPosNormal;
5454 if (Known.KnownFPClasses & fcNegSubnormal)
5455 Known.KnownFPClasses |= fcNegNormal;
5456 Known.knownNot(fcSubnormal);
5457 }
5458
5459 // Sign bit of a nan isn't guaranteed.
5460 if (!Known.isKnownNeverNaN())
5461 Known.SignBit = std::nullopt;
5462 break;
5463 }
5464 case Instruction::FPTrunc: {
5465 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5466 Depth, Q);
5467 break;
5468 }
5469 case Instruction::SIToFP:
5470 case Instruction::UIToFP: {
5471 // Cannot produce nan
5472 Known.knownNot(fcNan);
5473
5474 // Integers cannot be subnormal
5475 Known.knownNot(fcSubnormal);
5476
5477 // sitofp and uitofp turn into +0.0 for zero.
5478 Known.knownNot(fcNegZero);
5479 if (Op->getOpcode() == Instruction::UIToFP)
5480 Known.signBitMustBeZero();
5481
5482 if (InterestedClasses & fcInf) {
5483 // Get width of largest magnitude integer (remove a bit if signed).
5484 // This still works for a signed minimum value because the largest FP
5485 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5486 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5487 if (Op->getOpcode() == Instruction::SIToFP)
5488 --IntSize;
5489
5490 // If the exponent of the largest finite FP value can hold the largest
5491 // integer, the result of the cast must be finite.
5492 Type *FPTy = Op->getType()->getScalarType();
5493 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5494 Known.knownNot(fcInf);
5495 }
5496
5497 break;
5498 }
5499 case Instruction::ExtractElement: {
5500 // Look through extract element. If the index is non-constant or
5501 // out-of-range demand all elements, otherwise just the extracted element.
5502 const Value *Vec = Op->getOperand(0);
5503 const Value *Idx = Op->getOperand(1);
5504 auto *CIdx = dyn_cast<ConstantInt>(Idx);
5505
5506 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5507 unsigned NumElts = VecTy->getNumElements();
5508 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
5509 if (CIdx && CIdx->getValue().ult(NumElts))
5510 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5511 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5512 Depth + 1, Q);
5513 }
5514
5515 break;
5516 }
5517 case Instruction::InsertElement: {
5518 if (isa<ScalableVectorType>(Op->getType()))
5519 return;
5520
5521 const Value *Vec = Op->getOperand(0);
5522 const Value *Elt = Op->getOperand(1);
5523 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5524 unsigned NumElts = DemandedElts.getBitWidth();
5525 APInt DemandedVecElts = DemandedElts;
5526 bool NeedsElt = true;
5527 // If we know the index we are inserting to, clear it from Vec check.
5528 if (CIdx && CIdx->getValue().ult(NumElts)) {
5529 DemandedVecElts.clearBit(CIdx->getZExtValue());
5530 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5531 }
5532
5533 // Do we demand the inserted element?
5534 if (NeedsElt) {
5535 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1, Q);
5536 // If we don't know any bits, early out.
5537 if (Known.isUnknown())
5538 break;
5539 } else {
5540 Known.KnownFPClasses = fcNone;
5541 }
5542
5543 // Do we need anymore elements from Vec?
5544 if (!DemandedVecElts.isZero()) {
5545 KnownFPClass Known2;
5546 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
5547 Depth + 1, Q);
5548 Known |= Known2;
5549 }
5550
5551 break;
5552 }
5553 case Instruction::ShuffleVector: {
5554 // For undef elements, we don't know anything about the common state of
5555 // the shuffle result.
5556 APInt DemandedLHS, DemandedRHS;
5557 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5558 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5559 return;
5560
5561 if (!!DemandedLHS) {
5562 const Value *LHS = Shuf->getOperand(0);
5563 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
5564 Depth + 1, Q);
5565
5566 // If we don't know any bits, early out.
5567 if (Known.isUnknown())
5568 break;
5569 } else {
5570 Known.KnownFPClasses = fcNone;
5571 }
5572
5573 if (!!DemandedRHS) {
5574 KnownFPClass Known2;
5575 const Value *RHS = Shuf->getOperand(1);
5576 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
5577 Depth + 1, Q);
5578 Known |= Known2;
5579 }
5580
5581 break;
5582 }
5583 case Instruction::ExtractValue: {
5584 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5585 ArrayRef<unsigned> Indices = Extract->getIndices();
5586 const Value *Src = Extract->getAggregateOperand();
5587 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5588 Indices[0] == 0) {
5589 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5590 switch (II->getIntrinsicID()) {
5591 case Intrinsic::frexp: {
5592 Known.knownNot(fcSubnormal);
5593
5594 KnownFPClass KnownSrc;
5595 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5596 InterestedClasses, KnownSrc, Depth + 1, Q);
5597
5598 const Function *F = cast<Instruction>(Op)->getFunction();
5599
5600 if (KnownSrc.isKnownNever(fcNegative))
5601 Known.knownNot(fcNegative);
5602 else {
5603 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, Op->getType()))
5604 Known.knownNot(fcNegZero);
5605 if (KnownSrc.isKnownNever(fcNegInf))
5606 Known.knownNot(fcNegInf);
5607 }
5608
5609 if (KnownSrc.isKnownNever(fcPositive))
5610 Known.knownNot(fcPositive);
5611 else {
5612 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, Op->getType()))
5613 Known.knownNot(fcPosZero);
5614 if (KnownSrc.isKnownNever(fcPosInf))
5615 Known.knownNot(fcPosInf);
5616 }
5617
5618 Known.propagateNaN(KnownSrc);
5619 return;
5620 }
5621 default:
5622 break;
5623 }
5624 }
5625 }
5626
5627 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1,
5628 Q);
5629 break;
5630 }
5631 case Instruction::PHI: {
5632 const PHINode *P = cast<PHINode>(Op);
5633 // Unreachable blocks may have zero-operand PHI nodes.
5634 if (P->getNumIncomingValues() == 0)
5635 break;
5636
5637 // Otherwise take the unions of the known bit sets of the operands,
5638 // taking conservative care to avoid excessive recursion.
5639 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5640
5641 if (Depth < PhiRecursionLimit) {
5642 // Skip if every incoming value references to ourself.
5643 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5644 break;
5645
5646 bool First = true;
5647
5648 for (const Use &U : P->operands()) {
5649 Value *IncValue = U.get();
5650 // Skip direct self references.
5651 if (IncValue == P)
5652 continue;
5653
5654 KnownFPClass KnownSrc;
5655 // Recurse, but cap the recursion to two levels, because we don't want
5656 // to waste time spinning around in loops. We need at least depth 2 to
5657 // detect known sign bits.
5659 IncValue, DemandedElts, InterestedClasses, KnownSrc,
5660 PhiRecursionLimit,
5661 Q.getWithInstruction(P->getIncomingBlock(U)->getTerminator()));
5662
5663 if (First) {
5664 Known = KnownSrc;
5665 First = false;
5666 } else {
5667 Known |= KnownSrc;
5668 }
5669
5670 if (Known.KnownFPClasses == fcAllFlags)
5671 break;
5672 }
5673 }
5674
5675 break;
5676 }
5677 default:
5678 break;
5679 }
5680}
5681
5683 const APInt &DemandedElts,
5684 FPClassTest InterestedClasses,
5685 unsigned Depth,
5686 const SimplifyQuery &SQ) {
5687 KnownFPClass KnownClasses;
5688 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, Depth,
5689 SQ);
5690 return KnownClasses;
5691}
5692
5694 FPClassTest InterestedClasses,
5695 unsigned Depth,
5696 const SimplifyQuery &SQ) {
5697 KnownFPClass Known;
5698 ::computeKnownFPClass(V, Known, InterestedClasses, Depth, SQ);
5699 return Known;
5700}
5701
5703
5704 // All byte-wide stores are splatable, even of arbitrary variables.
5705 if (V->getType()->isIntegerTy(8))
5706 return V;
5707
5708 LLVMContext &Ctx = V->getContext();
5709
5710 // Undef don't care.
5711 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
5712 if (isa<UndefValue>(V))
5713 return UndefInt8;
5714
5715 // Return Undef for zero-sized type.
5716 if (DL.getTypeStoreSize(V->getType()).isZero())
5717 return UndefInt8;
5718
5719 Constant *C = dyn_cast<Constant>(V);
5720 if (!C) {
5721 // Conceptually, we could handle things like:
5722 // %a = zext i8 %X to i16
5723 // %b = shl i16 %a, 8
5724 // %c = or i16 %a, %b
5725 // but until there is an example that actually needs this, it doesn't seem
5726 // worth worrying about.
5727 return nullptr;
5728 }
5729
5730 // Handle 'null' ConstantArrayZero etc.
5731 if (C->isNullValue())
5733
5734 // Constant floating-point values can be handled as integer values if the
5735 // corresponding integer value is "byteable". An important case is 0.0.
5736 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
5737 Type *Ty = nullptr;
5738 if (CFP->getType()->isHalfTy())
5739 Ty = Type::getInt16Ty(Ctx);
5740 else if (CFP->getType()->isFloatTy())
5741 Ty = Type::getInt32Ty(Ctx);
5742 else if (CFP->getType()->isDoubleTy())
5743 Ty = Type::getInt64Ty(Ctx);
5744 // Don't handle long double formats, which have strange constraints.
5745 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
5746 : nullptr;
5747 }
5748
5749 // We can handle constant integers that are multiple of 8 bits.
5750 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
5751 if (CI->getBitWidth() % 8 == 0) {
5752 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
5753 if (!CI->getValue().isSplat(8))
5754 return nullptr;
5755 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
5756 }
5757 }
5758
5759 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
5760 if (CE->getOpcode() == Instruction::IntToPtr) {
5761 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
5762 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
5764 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
5765 return isBytewiseValue(Op, DL);
5766 }
5767 }
5768 }
5769
5770 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
5771 if (LHS == RHS)
5772 return LHS;
5773 if (!LHS || !RHS)
5774 return nullptr;
5775 if (LHS == UndefInt8)
5776 return RHS;
5777 if (RHS == UndefInt8)
5778 return LHS;
5779 return nullptr;
5780 };
5781
5782 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
5783 Value *Val = UndefInt8;
5784 for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
5785 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
5786 return nullptr;
5787 return Val;
5788 }
5789
5790 if (isa<ConstantAggregate>(C)) {
5791 Value *Val = UndefInt8;
5792 for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
5793 if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I), DL))))
5794 return nullptr;
5795 return Val;
5796 }
5797
5798 // Don't try to handle the handful of other constants.
5799 return nullptr;
5800}
5801
5802// This is the recursive version of BuildSubAggregate. It takes a few different
5803// arguments. Idxs is the index within the nested struct From that we are
5804// looking at now (which is of type IndexedType). IdxSkip is the number of
5805// indices from Idxs that should be left out when inserting into the resulting
5806// struct. To is the result struct built so far, new insertvalue instructions
5807// build on that.
5808static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
5810 unsigned IdxSkip,
5811 BasicBlock::iterator InsertBefore) {
5812 StructType *STy = dyn_cast<StructType>(IndexedType);
5813 if (STy) {
5814 // Save the original To argument so we can modify it
5815 Value *OrigTo = To;
5816 // General case, the type indexed by Idxs is a struct
5817 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
5818 // Process each struct element recursively
5819 Idxs.push_back(i);
5820 Value *PrevTo = To;
5821 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
5822 InsertBefore);
5823 Idxs.pop_back();
5824 if (!To) {
5825 // Couldn't find any inserted value for this index? Cleanup
5826 while (PrevTo != OrigTo) {
5827 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
5828 PrevTo = Del->getAggregateOperand();
5829 Del->eraseFromParent();
5830 }
5831 // Stop processing elements
5832 break;
5833 }
5834 }
5835 // If we successfully found a value for each of our subaggregates
5836 if (To)
5837 return To;
5838 }
5839 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
5840 // the struct's elements had a value that was inserted directly. In the latter
5841 // case, perhaps we can't determine each of the subelements individually, but
5842 // we might be able to find the complete struct somewhere.
5843
5844 // Find the value that is at that particular spot
5845 Value *V = FindInsertedValue(From, Idxs);
5846
5847 if (!V)
5848 return nullptr;
5849
5850 // Insert the value in the new (sub) aggregate
5851 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
5852 InsertBefore);
5853}
5854
5855// This helper takes a nested struct and extracts a part of it (which is again a
5856// struct) into a new value. For example, given the struct:
5857// { a, { b, { c, d }, e } }
5858// and the indices "1, 1" this returns
5859// { c, d }.
5860//
5861// It does this by inserting an insertvalue for each element in the resulting
5862// struct, as opposed to just inserting a single struct. This will only work if
5863// each of the elements of the substruct are known (ie, inserted into From by an
5864// insertvalue instruction somewhere).
5865//
5866// All inserted insertvalue instructions are inserted before InsertBefore
5868 BasicBlock::iterator InsertBefore) {
5869 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
5870 idx_range);
5871 Value *To = PoisonValue::get(IndexedType);
5872 SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
5873 unsigned IdxSkip = Idxs.size();
5874
5875 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
5876}
5877
5878/// Given an aggregate and a sequence of indices, see if the scalar value
5879/// indexed is already around as a register, for example if it was inserted
5880/// directly into the aggregate.
5881///
5882/// If InsertBefore is not null, this function will duplicate (modified)
5883/// insertvalues when a part of a nested struct is extracted.
5884Value *
5886 std::optional<BasicBlock::iterator> InsertBefore) {
5887 // Nothing to index? Just return V then (this is useful at the end of our
5888 // recursion).
5889 if (idx_range.empty())
5890 return V;
5891 // We have indices, so V should have an indexable type.
5892 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
5893 "Not looking at a struct or array?");
5894 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
5895 "Invalid indices for type?");
5896
5897 if (Constant *C = dyn_cast<Constant>(V)) {
5898 C = C->getAggregateElement(idx_range[0]);
5899 if (!C) return nullptr;
5900 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
5901 }
5902
5903 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
5904 // Loop the indices for the insertvalue instruction in parallel with the
5905 // requested indices
5906 const unsigned *req_idx = idx_range.begin();
5907 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
5908 i != e; ++i, ++req_idx) {
5909 if (req_idx == idx_range.end()) {
5910 // We can't handle this without inserting insertvalues
5911 if (!InsertBefore)
5912 return nullptr;
5913
5914 // The requested index identifies a part of a nested aggregate. Handle
5915 // this specially. For example,
5916 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
5917 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
5918 // %C = extractvalue {i32, { i32, i32 } } %B, 1
5919 // This can be changed into
5920 // %A = insertvalue {i32, i32 } undef, i32 10, 0
5921 // %C = insertvalue {i32, i32 } %A, i32 11, 1
5922 // which allows the unused 0,0 element from the nested struct to be
5923 // removed.
5924 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
5925 *InsertBefore);
5926 }
5927
5928 // This insert value inserts something else than what we are looking for.
5929 // See if the (aggregate) value inserted into has the value we are
5930 // looking for, then.
5931 if (*req_idx != *i)
5932 return FindInsertedValue(I->getAggregateOperand(), idx_range,
5933 InsertBefore);
5934 }
5935 // If we end up here, the indices of the insertvalue match with those
5936 // requested (though possibly only partially). Now we recursively look at
5937 // the inserted value, passing any remaining indices.
5938 return FindInsertedValue(I->getInsertedValueOperand(),
5939 ArrayRef(req_idx, idx_range.end()), InsertBefore);
5940 }
5941
5942 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
5943 // If we're extracting a value from an aggregate that was extracted from
5944 // something else, we can extract from that something else directly instead.
5945 // However, we will need to chain I's indices with the requested indices.
5946
5947 // Calculate the number of indices required
5948 unsigned size = I->getNumIndices() + idx_range.size();
5949 // Allocate some space to put the new indices in
5951 Idxs.reserve(size);
5952 // Add indices from the extract value instruction
5953 Idxs.append(I->idx_begin(), I->idx_end());
5954
5955 // Add requested indices
5956 Idxs.append(idx_range.begin(), idx_range.end());
5957
5958 assert(Idxs.size() == size
5959 && "Number of indices added not correct?");
5960
5961 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
5962 }
5963 // Otherwise, we don't know (such as, extracting from a function return value
5964 // or load instruction)
5965 return nullptr;
5966}
5967
5969 unsigned CharSize) {
5970 // Make sure the GEP has exactly three arguments.
5971 if (GEP->getNumOperands() != 3)
5972 return false;
5973
5974 // Make sure the index-ee is a pointer to array of \p CharSize integers.
5975 // CharSize.
5976 ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
5977 if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
5978 return false;
5979
5980 // Check to make sure that the first operand of the GEP is an integer and
5981 // has value 0 so that we are sure we're indexing into the initializer.
5982 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
5983 if (!FirstIdx || !FirstIdx->isZero())
5984 return false;
5985
5986 return true;
5987}
5988
5989// If V refers to an initialized global constant, set Slice either to
5990// its initializer if the size of its elements equals ElementSize, or,
5991// for ElementSize == 8, to its representation as an array of unsiged
5992// char. Return true on success.
5993// Offset is in the unit "nr of ElementSize sized elements".
5996 unsigned ElementSize, uint64_t Offset) {
5997 assert(V && "V should not be null.");
5998 assert((ElementSize % 8) == 0 &&
5999 "ElementSize expected to be a multiple of the size of a byte.");
6000 unsigned ElementSizeInBytes = ElementSize / 8;
6001
6002 // Drill down into the pointer expression V, ignoring any intervening
6003 // casts, and determine the identity of the object it references along
6004 // with the cumulative byte offset into it.
6005 const GlobalVariable *GV =
6006 dyn_cast<GlobalVariable>(getUnderlyingObject(V));
6007 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6008 // Fail if V is not based on constant global object.
6009 return false;
6010
6011 const DataLayout &DL = GV->getParent()->getDataLayout();
6012 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6013
6014 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6015 /*AllowNonInbounds*/ true))
6016 // Fail if a constant offset could not be determined.
6017 return false;
6018
6019 uint64_t StartIdx = Off.getLimitedValue();
6020 if (StartIdx == UINT64_MAX)
6021 // Fail if the constant offset is excessive.
6022 return false;
6023
6024 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6025 // elements. Simply bail out if that isn't possible.
6026 if ((StartIdx % ElementSizeInBytes) != 0)
6027 return false;
6028
6029 Offset += StartIdx / ElementSizeInBytes;
6030 ConstantDataArray *Array = nullptr;
6031 ArrayType *ArrayTy = nullptr;
6032
6033 if (GV->getInitializer()->isNullValue()) {
6034 Type *GVTy = GV->getValueType();
6035 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6036 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6037
6038 Slice.Array = nullptr;
6039 Slice.Offset = 0;
6040 // Return an empty Slice for undersized constants to let callers
6041 // transform even undefined library calls into simpler, well-defined
6042 // expressions. This is preferable to making the calls although it
6043 // prevents sanitizers from detecting such calls.
6044 Slice.Length = Length < Offset ? 0 : Length - Offset;
6045 return true;
6046 }
6047
6048 auto *Init = const_cast<Constant *>(GV->getInitializer());
6049 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6050 Type *InitElTy = ArrayInit->getElementType();
6051 if (InitElTy->isIntegerTy(ElementSize)) {
6052 // If Init is an initializer for an array of the expected type
6053 // and size, use it as is.
6054 Array = ArrayInit;
6055 ArrayTy = ArrayInit->getType();
6056 }
6057 }
6058
6059 if (!Array) {
6060 if (ElementSize != 8)
6061 // TODO: Handle conversions to larger integral types.
6062 return false;
6063
6064 // Otherwise extract the portion of the initializer starting
6065 // at Offset as an array of bytes, and reset Offset.
6067 if (!Init)
6068 return false;
6069
6070 Offset = 0;
6071 Array = dyn_cast<ConstantDataArray>(Init);
6072 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6073 }
6074
6075 uint64_t NumElts = ArrayTy->getArrayNumElements();
6076 if (Offset > NumElts)
6077 return false;
6078
6079 Slice.Array = Array;
6080 Slice.Offset = Offset;
6081 Slice.Length = NumElts - Offset;
6082 return true;
6083}
6084
6085/// Extract bytes from the initializer of the constant array V, which need
6086/// not be a nul-terminated string. On success, store the bytes in Str and
6087/// return true. When TrimAtNul is set, Str will contain only the bytes up
6088/// to but not including the first nul. Return false on failure.
6090 bool TrimAtNul) {
6092 if (!getConstantDataArrayInfo(V, Slice, 8))
6093 return false;
6094
6095 if (Slice.Array == nullptr) {
6096 if (TrimAtNul) {
6097 // Return a nul-terminated string even for an empty Slice. This is
6098 // safe because all existing SimplifyLibcalls callers require string
6099 // arguments and the behavior of the functions they fold is undefined
6100 // otherwise. Folding the calls this way is preferable to making
6101 // the undefined library calls, even though it prevents sanitizers
6102 // from reporting such calls.
6103 Str = StringRef();
6104 return true;
6105 }
6106 if (Slice.Length == 1) {
6107 Str = StringRef("", 1);
6108 return true;
6109 }
6110 // We cannot instantiate a StringRef as we do not have an appropriate string
6111 // of 0s at hand.
6112 return false;
6113 }
6114
6115 // Start out with the entire array in the StringRef.
6116 Str = Slice.Array->getAsString();
6117 // Skip over 'offset' bytes.
6118 Str = Str.substr(Slice.Offset);
6119
6120 if (TrimAtNul) {
6121 // Trim off the \0 and anything after it. If the array is not nul
6122 // terminated, we just return the whole end of string. The client may know
6123 // some other way that the string is length-bound.
6124 Str = Str.substr(0, Str.find('\0'));
6125 }
6126 return true;
6127}
6128
6129// These next two are very similar to the above, but also look through PHI
6130// nodes.
6131// TODO: See if we can integrate these two together.
6132
6133/// If we can compute the length of the string pointed to by
6134/// the specified pointer, return 'len+1'. If we can't, return 0.
6137 unsigned CharSize) {
6138 // Look through noop bitcast instructions.
6139 V = V->stripPointerCasts();
6140
6141 // If this is a PHI node, there are two cases: either we have already seen it
6142 // or we haven't.
6143 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6144 if (!PHIs.insert(PN).second)
6145 return ~0ULL; // already in the set.
6146
6147 // If it was new, see if all the input strings are the same length.
6148 uint64_t LenSoFar = ~0ULL;
6149 for (Value *IncValue : PN->incoming_values()) {
6150 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6151 if (Len == 0) return 0; // Unknown length -> unknown.
6152
6153 if (Len == ~0ULL) continue;
6154
6155 if (Len != LenSoFar && LenSoFar != ~0ULL)
6156 return 0; // Disagree -> unknown.
6157 LenSoFar = Len;
6158 }
6159
6160 // Success, all agree.
6161 return LenSoFar;
6162 }
6163
6164 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6165 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6166 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6167 if (Len1 == 0) return 0;
6168 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6169 if (Len2 == 0) return 0;
6170 if (Len1 == ~0ULL) return Len2;
6171 if (Len2 == ~0ULL) return Len1;
6172 if (Len1 != Len2) return 0;
6173 return Len1;
6174 }
6175
6176 // Otherwise, see if we can read the string.
6178 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6179 return 0;
6180
6181 if (Slice.Array == nullptr)
6182 // Zeroinitializer (including an empty one).
6183 return 1;
6184
6185 // Search for the first nul character. Return a conservative result even
6186 // when there is no nul. This is safe since otherwise the string function
6187 // being folded such as strlen is undefined, and can be preferable to
6188 // making the undefined library call.
6189 unsigned NullIndex = 0;
6190 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6191 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6192 break;
6193 }
6194
6195 return NullIndex + 1;
6196}
6197
6198/// If we can compute the length of the string pointed to by
6199/// the specified pointer, return 'len+1'. If we can't, return 0.
6200uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6201 if (!V->getType()->isPointerTy())
6202 return 0;
6203
6205 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6206 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6207 // an empty string as a length.
6208 return Len == ~0ULL ? 1 : Len;
6209}
6210
6211const Value *
6213 bool MustPreserveNullness) {
6214 assert(Call &&
6215 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6216 if (const Value *RV = Call->getReturnedArgOperand())
6217 return RV;
6218 // This can be used only as a aliasing property.
6220 Call, MustPreserveNullness))
6221 return Call->getArgOperand(0);
6222 return nullptr;
6223}
6224
6226 const CallBase *Call, bool MustPreserveNullness) {
6227 switch (Call->getIntrinsicID()) {
6228 case Intrinsic::launder_invariant_group:
6229 case Intrinsic::strip_invariant_group:
6230 case Intrinsic::aarch64_irg:
6231 case Intrinsic::aarch64_tagp:
6232 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6233 // input pointer (and thus preserve null-ness for the purposes of escape
6234 // analysis, which is where the MustPreserveNullness flag comes in to play).
6235 // However, it will not necessarily map ptr addrspace(N) null to ptr
6236 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6237 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6238 // list, no one should be relying on such a strict interpretation of
6239 // MustPreserveNullness (and, at time of writing, they are not), but we
6240 // document this fact out of an abundance of caution.
6241 case Intrinsic::amdgcn_make_buffer_rsrc:
6242 return true;
6243 case Intrinsic::ptrmask:
6244 return !MustPreserveNullness;
6245 default:
6246 return false;
6247 }
6248}
6249
6250/// \p PN defines a loop-variant pointer to an object. Check if the
6251/// previous iteration of the loop was referring to the same object as \p PN.
6253 const LoopInfo *LI) {
6254 // Find the loop-defined value.
6255 Loop *L = LI->getLoopFor(PN->getParent());
6256 if (PN->getNumIncomingValues() != 2)
6257 return true;
6258
6259 // Find the value from previous iteration.
6260 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6261 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6262 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6263 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6264 return true;
6265
6266 // If a new pointer is loaded in the loop, the pointer references a different
6267 // object in every iteration. E.g.:
6268 // for (i)
6269 // int *p = a[i];
6270 // ...
6271 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6272 if (!L->isLoopInvariant(Load->getPointerOperand()))
6273 return false;
6274 return true;
6275}
6276
6277const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6278 if (!V->getType()->isPointerTy())
6279 return V;
6280 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6281 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6282 V = GEP->getPointerOperand();
6283 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6284 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6285 V = cast<Operator>(V)->getOperand(0);
6286 if (!V->getType()->isPointerTy())
6287 return V;
6288 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6289 if (GA->isInterposable())
6290 return V;
6291 V = GA->getAliasee();
6292 } else {
6293 if (auto *PHI = dyn_cast<PHINode>(V)) {
6294 // Look through single-arg phi nodes created by LCSSA.
6295 if (PHI->getNumIncomingValues() == 1) {
6296 V = PHI->getIncomingValue(0);
6297 continue;
6298 }
6299 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6300 // CaptureTracking can know about special capturing properties of some
6301 // intrinsics like launder.invariant.group, that can't be expressed with
6302 // the attributes, but have properties like returning aliasing pointer.
6303 // Because some analysis may assume that nocaptured pointer is not
6304 // returned from some special intrinsic (because function would have to
6305 // be marked with returns attribute), it is crucial to use this function
6306 // because it should be in sync with CaptureTracking. Not using it may
6307 // cause weird miscompilations where 2 aliasing pointers are assumed to
6308 // noalias.
6309 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6310 V = RP;
6311 continue;
6312 }
6313 }
6314
6315 return V;
6316 }
6317 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6318 }
6319 return V;
6320}
6321
6324 LoopInfo *LI, unsigned MaxLookup) {
6327 Worklist.push_back(V);
6328 do {
6329 const Value *P = Worklist.pop_back_val();
6330 P = getUnderlyingObject(P, MaxLookup);
6331
6332 if (!Visited.insert(P).second)
6333 continue;
6334
6335 if (auto *SI = dyn_cast<SelectInst>(P)) {
6336 Worklist.push_back(SI->getTrueValue());
6337 Worklist.push_back(SI->getFalseValue());
6338 continue;
6339 }
6340
6341 if (auto *PN = dyn_cast<PHINode>(P)) {
6342 // If this PHI changes the underlying object in every iteration of the
6343 // loop, don't look through it. Consider:
6344 // int **A;
6345 // for (i) {
6346 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6347 // Curr = A[i];
6348 // *Prev, *Curr;
6349 //
6350 // Prev is tracking Curr one iteration behind so they refer to different
6351 // underlying objects.
6352 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6354 append_range(Worklist, PN->incoming_values());
6355 else
6356 Objects.push_back(P);
6357 continue;
6358 }
6359
6360 Objects.push_back(P);
6361 } while (!Worklist.empty());
6362}
6363
6364/// This is the function that does the work of looking through basic
6365/// ptrtoint+arithmetic+inttoptr sequences.
6366static const Value *getUnderlyingObjectFromInt(const Value *V) {
6367 do {
6368 if (const Operator *U = dyn_cast<Operator>(V)) {
6369 // If we find a ptrtoint, we can transfer control back to the
6370 // regular getUnderlyingObjectFromInt.
6371 if (U->getOpcode() == Instruction::PtrToInt)
6372 return U->getOperand(0);
6373 // If we find an add of a constant, a multiplied value, or a phi, it's
6374 // likely that the other operand will lead us to the base
6375 // object. We don't have to worry about the case where the
6376 // object address is somehow being computed by the multiply,
6377 // because our callers only care when the result is an
6378 // identifiable object.
6379 if (U->getOpcode() != Instruction::Add ||
6380 (!isa<ConstantInt>(U->getOperand(1)) &&
6381 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6382 !isa<PHINode>(U->getOperand(1))))
6383 return V;
6384 V = U->getOperand(0);
6385 } else {
6386 return V;
6387 }
6388 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6389 } while (true);
6390}
6391
6392/// This is a wrapper around getUnderlyingObjects and adds support for basic
6393/// ptrtoint+arithmetic+inttoptr sequences.
6394/// It returns false if unidentified object is found in getUnderlyingObjects.
6396 SmallVectorImpl<Value *> &Objects) {
6398 SmallVector<const Value *, 4> Working(1, V);
6399 do {
6400 V = Working.pop_back_val();
6401
6403 getUnderlyingObjects(V, Objs);
6404
6405 for (const Value *V : Objs) {
6406 if (!Visited.insert(V).second)
6407 continue;
6408 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6409 const Value *O =
6410 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6411 if (O->getType()->isPointerTy()) {
6412 Working.push_back(O);
6413 continue;
6414 }
6415 }
6416 // If getUnderlyingObjects fails to find an identifiable object,
6417 // getUnderlyingObjectsForCodeGen also fails for safety.
6418 if (!isIdentifiedObject(V)) {
6419 Objects.clear();
6420 return false;
6421 }
6422 Objects.push_back(const_cast<Value *>(V));
6423 }
6424 } while (!Working.empty());
6425 return true;
6426}
6427
6429 AllocaInst *Result = nullptr;
6431 SmallVector<Value *, 4> Worklist;
6432
6433 auto AddWork = [&](Value *V) {
6434 if (Visited.insert(V).second)
6435 Worklist.push_back(V);
6436 };
6437
6438 AddWork(V);
6439 do {
6440 V = Worklist.pop_back_val();
6441 assert(Visited.count(V));
6442
6443 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6444 if (Result && Result != AI)
6445 return nullptr;
6446 Result = AI;
6447 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
6448 AddWork(CI->getOperand(0));
6449 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
6450 for (Value *IncValue : PN->incoming_values())
6451 AddWork(IncValue);
6452 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
6453 AddWork(SI->getTrueValue());
6454 AddWork(SI->getFalseValue());
6455 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
6456 if (OffsetZero && !GEP->hasAllZeroIndices())
6457 return nullptr;
6458 AddWork(GEP->getPointerOperand());
6459 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
6460 Value *Returned = CB->getReturnedArgOperand();
6461 if (Returned)
6462 AddWork(Returned);
6463 else
6464 return nullptr;
6465 } else {
6466 return nullptr;
6467 }
6468 } while (!Worklist.empty());
6469
6470 return Result;
6471}
6472
6474 const Value *V, bool AllowLifetime, bool AllowDroppable) {
6475 for (const User *U : V->users()) {
6476 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
6477 if (!II)
6478 return false;
6479
6480 if (AllowLifetime && II->isLifetimeStartOrEnd())
6481 continue;
6482
6483 if (AllowDroppable && II->isDroppable())
6484 continue;
6485
6486 return false;
6487 }
6488 return true;
6489}
6490
6493 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
6494}
6497 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
6498}
6499
6501 if (!LI.isUnordered())
6502 return true;
6503 const Function &F = *LI.getFunction();
6504 // Speculative load may create a race that did not exist in the source.
6505 return F.hasFnAttribute(Attribute::SanitizeThread) ||
6506 // Speculative load may load data from dirty regions.
6507 F.hasFnAttribute(Attribute::SanitizeAddress) ||
6508 F.hasFnAttribute(Attribute::SanitizeHWAddress);
6509}
6510
6512 const Instruction *CtxI,
6513 AssumptionCache *AC,
6514 const DominatorTree *DT,
6515 const TargetLibraryInfo *TLI) {
6516 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
6517 AC, DT, TLI);
6518}
6519
6521 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
6522 AssumptionCache *AC, const DominatorTree *DT,
6523 const TargetLibraryInfo *TLI) {
6524#ifndef NDEBUG
6525 if (Inst->getOpcode() != Opcode) {
6526 // Check that the operands are actually compatible with the Opcode override.
6527 auto hasEqualReturnAndLeadingOperandTypes =
6528 [](const Instruction *Inst, unsigned NumLeadingOperands) {
6529 if (Inst->getNumOperands() < NumLeadingOperands)
6530 return false;
6531 const Type *ExpectedType = Inst->getType();
6532 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
6533 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
6534 return false;
6535 return true;
6536 };
6538 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
6539 assert(!Instruction::isUnaryOp(Opcode) ||
6540 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
6541 }
6542#endif
6543
6544 switch (Opcode) {
6545 default:
6546 return true;
6547 case Instruction::UDiv:
6548 case Instruction::URem: {
6549 // x / y is undefined if y == 0.
6550 const APInt *V;
6551 if (match(Inst->getOperand(1), m_APInt(V)))
6552 return *V != 0;
6553 return false;
6554 }
6555 case Instruction::SDiv:
6556 case Instruction::SRem: {
6557 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
6558 const APInt *Numerator, *Denominator;
6559 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
6560 return false;
6561 // We cannot hoist this division if the denominator is 0.
6562 if (*Denominator == 0)
6563 return false;
6564 // It's safe to hoist if the denominator is not 0 or -1.
6565 if (!Denominator->isAllOnes())
6566 return true;
6567 // At this point we know that the denominator is -1. It is safe to hoist as
6568 // long we know that the numerator is not INT_MIN.
6569 if (match(Inst->getOperand(0), m_APInt(Numerator)))
6570 return !Numerator->isMinSignedValue();
6571 // The numerator *might* be MinSignedValue.
6572 return false;
6573 }
6574 case Instruction::Load: {
6575 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
6576 if (!LI)
6577 return false;
6578 if (mustSuppressSpeculation(*LI))
6579 return false;
6580 const DataLayout &DL = LI->getModule()->getDataLayout();
6582 LI->getType(), LI->getAlign(), DL,
6583 CtxI, AC, DT, TLI);
6584 }
6585 case Instruction::Call: {
6586 auto *CI = dyn_cast<const CallInst>(Inst);
6587 if (!CI)
6588 return false;
6589 const Function *Callee = CI->getCalledFunction();
6590
6591 // The called function could have undefined behavior or side-effects, even
6592 // if marked readnone nounwind.
6593 return Callee && Callee->isSpeculatable();
6594 }
6595 case Instruction::VAArg:
6596 case Instruction::Alloca:
6597 case Instruction::Invoke:
6598 case Instruction::CallBr:
6599 case Instruction::PHI:
6600 case Instruction::Store:
6601 case Instruction::Ret:
6602 case Instruction::Br:
6603 case Instruction::IndirectBr:
6604 case Instruction::Switch:
6605 case Instruction::Unreachable:
6606 case Instruction::Fence:
6607 case Instruction::AtomicRMW:
6608 case Instruction::AtomicCmpXchg:
6609 case Instruction::LandingPad:
6610 case Instruction::Resume:
6611 case Instruction::CatchSwitch:
6612 case Instruction::CatchPad:
6613 case Instruction::CatchRet:
6614 case Instruction::CleanupPad:
6615 case Instruction::CleanupRet:
6616 return false; // Misc instructions which have effects
6617 }
6618}
6619
6621 if (I.mayReadOrWriteMemory())
6622 // Memory dependency possible
6623 return true;
6625 // Can't move above a maythrow call or infinite loop. Or if an
6626 // inalloca alloca, above a stacksave call.
6627 return true;
6629 // 1) Can't reorder two inf-loop calls, even if readonly
6630 // 2) Also can't reorder an inf-loop call below a instruction which isn't
6631 // safe to speculative execute. (Inverse of above)
6632 return true;
6633 return false;
6634}
6635
6636/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
6638 switch (OR) {
6647 }
6648 llvm_unreachable("Unknown OverflowResult");
6649}
6650
6651/// Combine constant ranges from computeConstantRange() and computeKnownBits().
6654 bool ForSigned,
6655 const SimplifyQuery &SQ) {
6656 ConstantRange CR1 =
6657 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
6658 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
6661 return CR1.intersectWith(CR2, RangeType);
6662}
6663
6665 const Value *RHS,
6666 const SimplifyQuery &SQ) {
6667 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
6668 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
6669 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
6670 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
6671 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
6672}
6673
6675 const Value *RHS,
6676 const SimplifyQuery &SQ) {
6677 // Multiplying n * m significant bits yields a result of n + m significant
6678 // bits. If the total number of significant bits does not exceed the
6679 // result bit width (minus 1), there is no overflow.
6680 // This means if we have enough leading sign bits in the operands
6681 // we can guarantee that the result does not overflow.
6682 // Ref: "Hacker's Delight" by Henry Warren
6683 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
6684
6685 // Note that underestimating the number of sign bits gives a more
6686 // conservative answer.
6687 unsigned SignBits =
6689
6690 // First handle the easy case: if we have enough sign bits there's
6691 // definitely no overflow.
6692 if (SignBits > BitWidth + 1)
6694
6695 // There are two ambiguous cases where there can be no overflow:
6696 // SignBits == BitWidth + 1 and
6697 // SignBits == BitWidth
6698 // The second case is difficult to check, therefore we only handle the
6699 // first case.
6700 if (SignBits == BitWidth + 1) {
6701 // It overflows only when both arguments are negative and the true
6702 // product is exactly the minimum negative number.
6703 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
6704 // For simplicity we just check if at least one side is not negative.
6705 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
6706 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
6707 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
6709 }
6711}
6712
6715 const WithCache<const Value *> &RHS,
6716 const SimplifyQuery &SQ) {
6717 ConstantRange LHSRange =
6718 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
6719 ConstantRange RHSRange =
6720 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
6721 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
6722}
6723
6724static OverflowResult
6726 const WithCache<const Value *> &RHS,
6727 const AddOperator *Add, const SimplifyQuery &SQ) {
6728 if (Add && Add->hasNoSignedWrap()) {
6730 }
6731
6732 // If LHS and RHS each have at least two sign bits, the addition will look
6733 // like
6734 //
6735 // XX..... +
6736 // YY.....
6737 //
6738 // If the carry into the most significant position is 0, X and Y can't both
6739 // be 1 and therefore the carry out of the addition is also 0.
6740 //
6741 // If the carry into the most significant position is 1, X and Y can't both
6742 // be 0 and therefore the carry out of the addition is also 1.
6743 //
6744 // Since the carry into the most significant position is always equal to
6745 // the carry out of the addition, there is no signed overflow.
6746 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
6747 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
6749
6750 ConstantRange LHSRange =
6751 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
6752 ConstantRange RHSRange =
6753 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
6754 OverflowResult OR =
6755 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
6757 return OR;
6758
6759 // The remaining code needs Add to be available. Early returns if not so.
6760 if (!Add)
6762
6763 // If the sign of Add is the same as at least one of the operands, this add
6764 // CANNOT overflow. If this can be determined from the known bits of the
6765 // operands the above signedAddMayOverflow() check will have already done so.
6766 // The only other way to improve on the known bits is from an assumption, so
6767 // call computeKnownBitsFromContext() directly.
6768 bool LHSOrRHSKnownNonNegative =
6769 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
6770 bool LHSOrRHSKnownNegative =
6771 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
6772 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
6773 KnownBits AddKnown(LHSRange.getBitWidth());
6774 computeKnownBitsFromContext(Add, AddKnown, /*Depth=*/0, SQ);
6775 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
6776 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
6778 }
6779
6781}
6782
6784 const Value *RHS,
6785 const SimplifyQuery &SQ) {
6786 // X - (X % ?)
6787 // The remainder of a value can't have greater magnitude than itself,
6788 // so the subtraction can't overflow.
6789
6790 // X - (X -nuw ?)
6791 // In the minimal case, this would simplify to "?", so there's no subtract
6792 // at all. But if this analysis is used to peek through casts, for example,
6793 // then determining no-overflow may allow other transforms.
6794
6795 // TODO: There are other patterns like this.
6796 // See simplifyICmpWithBinOpOnLHS() for candidates.
6797 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
6799 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
6801
6802 // Checking for conditions implied by dominating conditions may be expensive.
6803 // Limit it to usub_with_overflow calls for now.
6804 if (match(SQ.CxtI,
6805 m_Intrinsic<Intrinsic::usub_with_overflow>(m_Value(), m_Value())))
6807 SQ.DL)) {
6808 if (*C)
6811 }
6812 ConstantRange LHSRange =
6813 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
6814 ConstantRange RHSRange =
6815 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
6816 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
6817}
6818
6820 const Value *RHS,
6821 const SimplifyQuery &SQ) {
6822 // X - (X % ?)
6823 // The remainder of a value can't have greater magnitude than itself,
6824 // so the subtraction can't overflow.
6825
6826 // X - (X -nsw ?)
6827 // In the minimal case, this would simplify to "?", so there's no subtract
6828 // at all. But if this analysis is used to peek through casts, for example,
6829 // then determining no-overflow may allow other transforms.
6830 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
6832 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
6834
6835 // If LHS and RHS each have at least two sign bits, the subtraction
6836 // cannot overflow.
6837 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
6838 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
6840
6841 ConstantRange LHSRange =
6842 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
6843 ConstantRange RHSRange =
6844 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
6845 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
6846}
6847
6849 const DominatorTree &DT) {
6850 SmallVector<const BranchInst *, 2> GuardingBranches;
6852
6853 for (const User *U : WO->users()) {
6854 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
6855 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
6856
6857 if (EVI->getIndices()[0] == 0)
6858 Results.push_back(EVI);
6859 else {
6860 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
6861
6862 for (const auto *U : EVI->users())
6863 if (const auto *B = dyn_cast<BranchInst>(U)) {
6864 assert(B->isConditional() && "How else is it using an i1?");
6865 GuardingBranches.push_back(B);
6866 }
6867 }
6868 } else {
6869 // We are using the aggregate directly in a way we don't want to analyze
6870 // here (storing it to a global, say).
6871 return false;
6872 }
6873 }
6874
6875 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
6876 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
6877 if (!NoWrapEdge.isSingleEdge())
6878 return false;
6879
6880 // Check if all users of the add are provably no-wrap.
6881 for (const auto *Result : Results) {
6882 // If the extractvalue itself is not executed on overflow, the we don't
6883 // need to check each use separately, since domination is transitive.
6884 if (DT.dominates(NoWrapEdge, Result->getParent()))
6885 continue;
6886
6887 for (const auto &RU : Result->uses())
6888 if (!DT.dominates(NoWrapEdge, RU))
6889 return false;
6890 }
6891
6892 return true;
6893 };
6894
6895 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
6896}
6897
6898/// Shifts return poison if shiftwidth is larger than the bitwidth.
6899static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
6900 auto *C = dyn_cast<Constant>(ShiftAmount);
6901 if (!C)
6902 return false;
6903
6904 // Shifts return poison if shiftwidth is larger than the bitwidth.
6906 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
6907 unsigned NumElts = FVTy->getNumElements();
6908 for (unsigned i = 0; i < NumElts; ++i)
6909 ShiftAmounts.push_back(C->getAggregateElement(i));
6910 } else if (isa<ScalableVectorType>(C->getType()))
6911 return false; // Can't tell, just return false to be safe
6912 else
6913 ShiftAmounts.push_back(C);
6914
6915 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
6916 auto *CI = dyn_cast_or_null<ConstantInt>(C);
6917 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
6918 });
6919
6920 return Safe;
6921}
6922
6924 PoisonOnly = (1 << 0),
6925 UndefOnly = (1 << 1),
6927};
6928
6930 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
6931}
6932
6934 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
6935}
6936
6938 bool ConsiderFlagsAndMetadata) {
6939
6940 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
6941 Op->hasPoisonGeneratingFlagsOrMetadata())
6942 return true;
6943
6944 unsigned Opcode = Op->getOpcode();
6945
6946 // Check whether opcode is a poison/undef-generating operation
6947 switch (Opcode) {
6948 case Instruction::Shl:
6949 case Instruction::AShr:
6950 case Instruction::LShr:
6951 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
6952 case Instruction::FPToSI:
6953 case Instruction::FPToUI:
6954 // fptosi/ui yields poison if the resulting value does not fit in the
6955 // destination type.
6956 return true;
6957 case Instruction::Call:
6958 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
6959 switch (II->getIntrinsicID()) {
6960 // TODO: Add more intrinsics.
6961 case Intrinsic::ctlz:
6962 case Intrinsic::cttz:
6963 case Intrinsic::abs:
6964 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
6965 return false;
6966 break;
6967 case Intrinsic::ctpop:
6968 case Intrinsic::bswap:
6969 case Intrinsic::bitreverse:
6970 case Intrinsic::fshl:
6971 case Intrinsic::fshr:
6972 case Intrinsic::smax:
6973 case Intrinsic::smin:
6974 case Intrinsic::umax:
6975 case Intrinsic::umin:
6976 case Intrinsic::ptrmask:
6977 case Intrinsic::fptoui_sat:
6978 case Intrinsic::fptosi_sat:
6979 case Intrinsic::sadd_with_overflow:
6980 case Intrinsic::ssub_with_overflow:
6981 case Intrinsic::smul_with_overflow:
6982 case Intrinsic::uadd_with_overflow:
6983 case Intrinsic::usub_with_overflow:
6984 case Intrinsic::umul_with_overflow:
6985 case Intrinsic::sadd_sat:
6986 case Intrinsic::uadd_sat:
6987 case Intrinsic::ssub_sat:
6988 case Intrinsic::usub_sat:
6989 return false;
6990 case Intrinsic::sshl_sat:
6991 case Intrinsic::ushl_sat:
6992 return includesPoison(Kind) &&
6993 !shiftAmountKnownInRange(II->getArgOperand(1));
6994 case Intrinsic::fma:
6995 case Intrinsic::fmuladd:
6996 case Intrinsic::sqrt:
6997 case Intrinsic::powi:
6998 case Intrinsic::sin:
6999 case Intrinsic::cos:
7000 case Intrinsic::pow:
7001 case Intrinsic::log:
7002 case Intrinsic::log10:
7003 case Intrinsic::log2:
7004 case Intrinsic::exp:
7005 case Intrinsic::exp2:
7006 case Intrinsic::exp10:
7007 case Intrinsic::fabs:
7008 case Intrinsic::copysign:
7009 case Intrinsic::floor:
7010 case Intrinsic::ceil:
7011 case Intrinsic::trunc:
7012 case Intrinsic::rint:
7013 case Intrinsic::nearbyint:
7014 case Intrinsic::round:
7015 case Intrinsic::roundeven:
7016 case Intrinsic::fptrunc_round:
7017 case Intrinsic::canonicalize:
7018 case Intrinsic::arithmetic_fence:
7019 case Intrinsic::minnum:
7020 case Intrinsic::maxnum:
7021 case Intrinsic::minimum:
7022 case Intrinsic::maximum:
7023 case Intrinsic::is_fpclass:
7024 case Intrinsic::ldexp:
7025 case Intrinsic::frexp:
7026 return false;
7027 case Intrinsic::lround:
7028 case Intrinsic::llround:
7029 case Intrinsic::lrint:
7030 case Intrinsic::llrint:
7031 // If the value doesn't fit an unspecified value is returned (but this
7032 // is not poison).
7033 return false;
7034 }
7035 }
7036 [[fallthrough]];
7037 case Instruction::CallBr:
7038 case Instruction::Invoke: {
7039 const auto *CB = cast<CallBase>(Op);
7040 return !CB->hasRetAttr(Attribute::NoUndef);
7041 }
7042 case Instruction::InsertElement:
7043 case Instruction::ExtractElement: {
7044 // If index exceeds the length of the vector, it returns poison
7045 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7046 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7047 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7048 if (includesPoison(Kind))
7049 return !Idx ||
7050 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7051 return false;
7052 }
7053 case Instruction::ShuffleVector: {
7054 ArrayRef<int> Mask = isa<ConstantExpr>(Op)
7055 ? cast<ConstantExpr>(Op)->getShuffleMask()
7056 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7057 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7058 }
7059 case Instruction::FNeg:
7060 case Instruction::PHI:
7061 case Instruction::Select:
7062 case Instruction::URem:
7063 case Instruction::SRem:
7064 case Instruction::ExtractValue:
7065 case Instruction::InsertValue:
7066 case Instruction::Freeze:
7067 case Instruction::ICmp:
7068 case Instruction::FCmp:
7069 case Instruction::FAdd:
7070 case Instruction::FSub:
7071 case Instruction::FMul:
7072 case Instruction::FDiv:
7073 case Instruction::FRem:
7074 return false;
7075 case Instruction::GetElementPtr:
7076 // inbounds is handled above
7077 // TODO: what about inrange on constexpr?
7078 return false;
7079 default: {
7080 const auto *CE = dyn_cast<ConstantExpr>(Op);
7081 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7082 return false;
7083 else if (Instruction::isBinaryOp(Opcode))
7084 return false;
7085 // Be conservative and return true.
7086 return true;
7087 }
7088 }
7089}
7090
7092 bool ConsiderFlagsAndMetadata) {
7093 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7094 ConsiderFlagsAndMetadata);
7095}
7096
7097bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7098 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7099 ConsiderFlagsAndMetadata);
7100}
7101
7102static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7103 unsigned Depth) {
7104 if (ValAssumedPoison == V)
7105 return true;
7106
7107 const unsigned MaxDepth = 2;
7108 if (Depth >= MaxDepth)
7109 return false;
7110
7111 if (const auto *I = dyn_cast<Instruction>(V)) {
7112 if (any_of(I->operands(), [=](const Use &Op) {
7113 return propagatesPoison(Op) &&
7114 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7115 }))
7116 return true;
7117
7118 // V = extractvalue V0, idx
7119 // V2 = extractvalue V0, idx2
7120 // V0's elements are all poison or not. (e.g., add_with_overflow)
7121 const WithOverflowInst *II;
7123 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7124 llvm::is_contained(II->args(), ValAssumedPoison)))
7125 return true;
7126 }
7127 return false;
7128}
7129
7130static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7131 unsigned Depth) {
7132 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7133 return true;
7134
7135 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7136 return true;
7137
7138 const unsigned MaxDepth = 2;
7139 if (Depth >= MaxDepth)
7140 return false;
7141
7142 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7143 if (I && !canCreatePoison(cast<Operator>(I))) {
7144 return all_of(I->operands(), [=](const Value *Op) {
7145 return impliesPoison(Op, V, Depth + 1);
7146 });
7147 }
7148 return false;
7149}
7150
7151bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7152 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7153}
7154
7155static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7156
7158 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7159 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7161 return false;
7162
7163 if (isa<MetadataAsValue>(V))
7164 return false;
7165
7166 if (const auto *A = dyn_cast<Argument>(V)) {
7167 if (A->hasAttribute(Attribute::NoUndef) ||
7168 A->hasAttribute(Attribute::Dereferenceable) ||
7169 A->hasAttribute(Attribute::DereferenceableOrNull))
7170 return true;
7171 }
7172
7173 if (auto *C = dyn_cast<Constant>(V)) {
7174 if (isa<PoisonValue>(C))
7175 return !includesPoison(Kind);
7176
7177 if (isa<UndefValue>(C))
7178 return !includesUndef(Kind);
7179
7180 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(V) ||
7181 isa<ConstantPointerNull>(C) || isa<Function>(C))
7182 return true;
7183
7184 if (C->getType()->isVectorTy() && !isa<ConstantExpr>(C))
7185 return (!includesUndef(Kind) ? !C->containsPoisonElement()
7186 : !C->containsUndefOrPoisonElement()) &&
7187 !C->containsConstantExpression();
7188 }
7189
7190 // Strip cast operations from a pointer value.
7191 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7192 // inbounds with zero offset. To guarantee that the result isn't poison, the
7193 // stripped pointer is checked as it has to be pointing into an allocated
7194 // object or be null `null` to ensure `inbounds` getelement pointers with a
7195 // zero offset could not produce poison.
7196 // It can strip off addrspacecast that do not change bit representation as
7197 // well. We believe that such addrspacecast is equivalent to no-op.
7198 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7199 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7200 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7201 return true;
7202
7203 auto OpCheck = [&](const Value *V) {
7204 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7205 };
7206
7207 if (auto *Opr = dyn_cast<Operator>(V)) {
7208 // If the value is a freeze instruction, then it can never
7209 // be undef or poison.
7210 if (isa<FreezeInst>(V))
7211 return true;
7212
7213 if (const auto *CB = dyn_cast<CallBase>(V)) {
7214 if (CB->hasRetAttr(Attribute::NoUndef) ||
7215 CB->hasRetAttr(Attribute::Dereferenceable) ||
7216 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7217 return true;
7218 }
7219
7220 if (const auto *PN = dyn_cast<PHINode>(V)) {
7221 unsigned Num = PN->getNumIncomingValues();
7222 bool IsWellDefined = true;
7223 for (unsigned i = 0; i < Num; ++i) {
7224 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7225 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7226 DT, Depth + 1, Kind)) {
7227 IsWellDefined = false;
7228 break;
7229 }
7230 }
7231 if (IsWellDefined)
7232 return true;
7233 } else if (!::canCreateUndefOrPoison(Opr, Kind,
7234 /*ConsiderFlagsAndMetadata*/ true) &&
7235 all_of(Opr->operands(), OpCheck))
7236 return true;
7237 }
7238
7239 if (auto *I = dyn_cast<LoadInst>(V))
7240 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7241 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7242 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7243 return true;
7244
7246 return true;
7247
7248 // CxtI may be null or a cloned instruction.
7249 if (!CtxI || !CtxI->getParent() || !DT)
7250 return false;
7251
7252 auto *DNode = DT->getNode(CtxI->getParent());
7253 if (!DNode)
7254 // Unreachable block
7255 return false;
7256
7257 // If V is used as a branch condition before reaching CtxI, V cannot be
7258 // undef or poison.
7259 // br V, BB1, BB2
7260 // BB1:
7261 // CtxI ; V cannot be undef or poison here
7262 auto *Dominator = DNode->getIDom();
7263 while (Dominator) {
7264 auto *TI = Dominator->getBlock()->getTerminator();
7265
7266 Value *Cond = nullptr;
7267 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7268 if (BI->isConditional())
7269 Cond = BI->getCondition();
7270 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7271 Cond = SI->getCondition();
7272 }
7273
7274 if (Cond) {
7275 if (Cond == V)
7276 return true;
7277 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7278 // For poison, we can analyze further
7279 auto *Opr = cast<Operator>(Cond);
7280 if (any_of(Opr->operands(),
7281 [V](const Use &U) { return V == U && propagatesPoison(U); }))
7282 return true;
7283 }
7284 }
7285
7286 Dominator = Dominator->getIDom();
7287 }
7288
7289 if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
7290 return true;
7291
7292 return false;
7293}
7294
7296 const Instruction *CtxI,
7297 const DominatorTree *DT,
7298 unsigned Depth) {
7299 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7300 UndefPoisonKind::UndefOrPoison);
7301}
7302
7304 const Instruction *CtxI,
7305 const DominatorTree *DT, unsigned Depth) {
7306 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7307 UndefPoisonKind::PoisonOnly);
7308}
7309
7311 const Instruction *CtxI,
7312 const DominatorTree *DT, unsigned Depth) {
7313 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7314 UndefPoisonKind::UndefOnly);
7315}
7316
7317/// Return true if undefined behavior would provably be executed on the path to
7318/// OnPathTo if Root produced a posion result. Note that this doesn't say
7319/// anything about whether OnPathTo is actually executed or whether Root is
7320/// actually poison. This can be used to assess whether a new use of Root can
7321/// be added at a location which is control equivalent with OnPathTo (such as
7322/// immediately before it) without introducing UB which didn't previously
7323/// exist. Note that a false result conveys no information.
7325 Instruction *OnPathTo,
7326 DominatorTree *DT) {
7327 // Basic approach is to assume Root is poison, propagate poison forward
7328 // through all users we can easily track, and then check whether any of those
7329 // users are provable UB and must execute before out exiting block might
7330 // exit.
7331
7332 // The set of all recursive users we've visited (which are assumed to all be
7333 // poison because of said visit)
7334 SmallSet<const Value *, 16> KnownPoison;
7336 Worklist.push_back(Root);
7337 while (!Worklist.empty()) {
7338 const Instruction *I = Worklist.pop_back_val();
7339
7340 // If we know this must trigger UB on a path leading our target.
7341 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7342 return true;
7343
7344 // If we can't analyze propagation through this instruction, just skip it
7345 // and transitive users. Safe as false is a conservative result.
7346 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7347 return KnownPoison.contains(U) && propagatesPoison(U);
7348 }))
7349 continue;
7350
7351 if (KnownPoison.insert(I).second)
7352 for (const User *User : I->users())
7353 Worklist.push_back(cast<Instruction>(User));
7354 }
7355
7356 // Might be non-UB, or might have a path we couldn't prove must execute on
7357 // way to exiting bb.
7358 return false;
7359}
7360
7362 const SimplifyQuery &SQ) {
7363 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7364 Add, SQ);
7365}
7366
7369 const WithCache<const Value *> &RHS,
7370 const SimplifyQuery &SQ) {
7371 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7372}
7373
7375 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7376 // of time because it's possible for another thread to interfere with it for an
7377 // arbitrary length of time, but programs aren't allowed to rely on that.
7378
7379 // If there is no successor, then execution can't transfer to it.
7380 if (isa<ReturnInst>(I))
7381 return false;
7382 if (isa<UnreachableInst>(I))
7383 return false;
7384
7385 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7386 // Instruction::willReturn.
7387 //
7388 // FIXME: Move this check into Instruction::willReturn.
7389 if (isa<CatchPadInst>(I)) {
7390 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7391 default:
7392 // A catchpad may invoke exception object constructors and such, which
7393 // in some languages can be arbitrary code, so be conservative by default.
7394 return false;
7396 // For CoreCLR, it just involves a type test.
7397 return true;
7398 }
7399 }
7400
7401 // An instruction that returns without throwing must transfer control flow
7402 // to a successor.
7403 return !I->mayThrow() && I->willReturn();
7404}
7405
7407 // TODO: This is slightly conservative for invoke instruction since exiting
7408 // via an exception *is* normal control for them.
7409 for (const Instruction &I : *BB)
7411 return false;
7412 return true;
7413}
7414
7417 unsigned ScanLimit) {
7419 ScanLimit);
7420}
7421
7423 iterator_range<BasicBlock::const_iterator> Range, unsigned ScanLimit) {
7424 assert(ScanLimit && "scan limit must be non-zero");
7425 for (const Instruction &I : Range) {
7426 if (isa<DbgInfoIntrinsic>(I))
7427 continue;
7428 if (--ScanLimit == 0)
7429 return false;
7431 return false;
7432 }
7433 return true;
7434}
7435
7437 const Loop *L) {
7438 // The loop header is guaranteed to be executed for every iteration.
7439 //
7440 // FIXME: Relax this constraint to cover all basic blocks that are
7441 // guaranteed to be executed at every iteration.
7442 if (I->getParent() != L->getHeader()) return false;
7443
7444 for (const Instruction &LI : *L->getHeader()) {
7445 if (&LI == I) return true;
7446 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7447 }
7448 llvm_unreachable("Instruction not contained in its own parent basic block.");
7449}
7450
7451bool llvm::propagatesPoison(const Use &PoisonOp) {
7452 const Operator *I = cast<Operator>(PoisonOp.getUser());
7453 switch (I->getOpcode()) {
7454 case Instruction::Freeze:
7455 case Instruction::PHI:
7456 case Instruction::Invoke:
7457 return false;
7458 case Instruction::Select:
7459 return PoisonOp.getOperandNo() == 0;
7460 case Instruction::Call:
7461 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
7462 switch (II->getIntrinsicID()) {
7463 // TODO: Add more intrinsics.
7464 case Intrinsic::sadd_with_overflow:
7465 case Intrinsic::ssub_with_overflow:
7466 case Intrinsic::smul_with_overflow:
7467 case Intrinsic::uadd_with_overflow:
7468 case Intrinsic::usub_with_overflow:
7469 case Intrinsic::umul_with_overflow:
7470 // If an input is a vector containing a poison element, the
7471 // two output vectors (calculated results, overflow bits)'
7472 // corresponding lanes are poison.
7473 return true;
7474 case Intrinsic::ctpop:
7475 case Intrinsic::ctlz:
7476 case Intrinsic::cttz:
7477 case Intrinsic::abs:
7478 case Intrinsic::smax:
7479 case Intrinsic::smin:
7480 case Intrinsic::umax:
7481 case Intrinsic::umin:
7482 case Intrinsic::bitreverse:
7483 case Intrinsic::bswap:
7484 case Intrinsic::sadd_sat:
7485 case Intrinsic::ssub_sat:
7486 case Intrinsic::sshl_sat:
7487 case Intrinsic::uadd_sat:
7488 case Intrinsic::usub_sat:
7489 case Intrinsic::ushl_sat:
7490 return true;
7491 }
7492 }
7493 return false;
7494 case Instruction::ICmp:
7495 case Instruction::FCmp:
7496 case Instruction::GetElementPtr:
7497 return true;
7498 default:
7499 if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I))
7500 return true;
7501
7502 // Be conservative and return false.
7503 return false;
7504 }
7505}
7506
7507/// Enumerates all operands of \p I that are guaranteed to not be undef or
7508/// poison. If the callback \p Handle returns true, stop processing and return
7509/// true. Otherwise, return false.
7510template <typename CallableT>
7512 const CallableT &Handle) {
7513 switch (I->getOpcode()) {
7514 case Instruction::Store:
7515 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
7516 return true;
7517 break;
7518
7519 case Instruction::Load:
7520 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
7521 return true;
7522 break;
7523
7524 // Since dereferenceable attribute imply noundef, atomic operations
7525 // also implicitly have noundef pointers too
7526 case Instruction::AtomicCmpXchg:
7527 if (Handle(cast<AtomicCmpXchgInst>(I)->getPointerOperand()))
7528 return true;
7529 break;
7530
7531 case Instruction::AtomicRMW:
7532 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
7533 return true;
7534 break;
7535
7536 case Instruction::Call:
7537 case Instruction::Invoke: {
7538 const CallBase *CB = cast<CallBase>(I);
7539 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
7540 return true;
7541 for (unsigned i = 0; i < CB->arg_size(); ++i)
7542 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
7543 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
7544 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
7545 Handle(CB->getArgOperand(i)))
7546 return true;
7547 break;
7548 }
7549 case Instruction::Ret:
7550 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
7551 Handle(I->getOperand(0)))
7552 return true;
7553 break;
7554 case Instruction::Switch:
7555 if (Handle(cast<SwitchInst>(I)->getCondition()))
7556 return true;
7557 break;
7558 case Instruction::Br: {
7559 auto *BR = cast<BranchInst>(I);
7560 if (BR->isConditional() && Handle(BR->getCondition()))
7561 return true;
7562 break;
7563 }
7564 default:
7565 break;
7566 }
7567
7568 return false;
7569}
7570
7573 handleGuaranteedWellDefinedOps(I, [&](const Value *V) {
7574 Operands.push_back(V);
7575 return false;
7576 });
7577}
7578
7579/// Enumerates all operands of \p I that are guaranteed to not be poison.
7580template <typename CallableT>
7582 const CallableT &Handle) {
7583 if (handleGuaranteedWellDefinedOps(I, Handle))
7584 return true;
7585 switch (I->getOpcode()) {
7586 // Divisors of these operations are allowed to be partially undef.
7587 case Instruction::UDiv:
7588 case Instruction::SDiv:
7589 case Instruction::URem:
7590 case Instruction::SRem:
7591 return Handle(I->getOperand(1));
7592 default:
7593 return false;
7594 }
7595}
7596
7599 handleGuaranteedNonPoisonOps(I, [&](const Value *V) {
7600 Operands.push_back(V);
7601 return false;
7602 });
7603}
7604
7606 const SmallPtrSetImpl<const Value *> &KnownPoison) {
7608 I, [&](const Value *V) { return KnownPoison.count(V); });
7609}
7610
7612 bool PoisonOnly) {
7613 // We currently only look for uses of values within the same basic
7614 // block, as that makes it easier to guarantee that the uses will be
7615 // executed given that Inst is executed.
7616 //
7617 // FIXME: Expand this to consider uses beyond the same basic block. To do
7618 // this, look out for the distinction between post-dominance and strong
7619 // post-dominance.
7620 const BasicBlock *BB = nullptr;
7622 if (const auto *Inst = dyn_cast<Instruction>(V)) {
7623 BB = Inst->getParent();
7624 Begin = Inst->getIterator();
7625 Begin++;
7626 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
7627 if (Arg->getParent()->isDeclaration())
7628 return false;
7629 BB = &Arg->getParent()->getEntryBlock();
7630 Begin = BB->begin();
7631 } else {
7632 return false;
7633 }
7634
7635 // Limit number of instructions we look at, to avoid scanning through large
7636 // blocks. The current limit is chosen arbitrarily.
7637 unsigned ScanLimit = 32;
7639
7640 if (!PoisonOnly) {
7641 // Since undef does not propagate eagerly, be conservative & just check
7642 // whether a value is directly passed to an instruction that must take
7643 // well-defined operands.
7644
7645 for (const auto &I : make_range(Begin, End)) {
7646 if (isa<DbgInfoIntrinsic>(I))
7647 continue;
7648 if (--ScanLimit == 0)
7649 break;
7650
7651 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
7652 return WellDefinedOp == V;
7653 }))
7654 return true;
7655
7657 break;
7658 }
7659 return false;
7660 }
7661
7662 // Set of instructions that we have proved will yield poison if Inst
7663 // does.
7664 SmallSet<const Value *, 16> YieldsPoison;
7666
7667 YieldsPoison.insert(V);
7668 Visited.insert(BB);
7669
7670 while (true) {
7671 for (const auto &I : make_range(Begin, End)) {
7672 if (isa<DbgInfoIntrinsic>(I))
7673 continue;
7674 if (--ScanLimit == 0)
7675 return false;
7676 if (mustTriggerUB(&I, YieldsPoison))
7677 return true;
7679 return false;
7680
7681 // If an operand is poison and propagates it, mark I as yielding poison.
7682 for (const Use &Op : I.operands()) {
7683 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
7684 YieldsPoison.insert(&I);
7685 break;
7686 }
7687 }
7688
7689 // Special handling for select, which returns poison if its operand 0 is
7690 // poison (handled in the loop above) *or* if both its true/false operands
7691 // are poison (handled here).
7692 if (I.getOpcode() == Instruction::Select &&
7693 YieldsPoison.count(I.getOperand(1)) &&
7694 YieldsPoison.count(I.getOperand(2))) {
7695 YieldsPoison.insert(&I);
7696 }
7697 }
7698
7699 BB = BB->getSingleSuccessor();
7700 if (!BB || !Visited.insert(BB).second)
7701 break;
7702
7703 Begin = BB->getFirstNonPHI()->getIterator();
7704 End = BB->end();
7705 }
7706 return false;
7707}
7708
7710 return ::programUndefinedIfUndefOrPoison(Inst, false);
7711}
7712
7714 return ::programUndefinedIfUndefOrPoison(Inst, true);
7715}
7716
7717static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
7718 if (FMF.noNaNs())
7719 return true;
7720
7721 if (auto *C = dyn_cast<ConstantFP>(V))
7722 return !C->isNaN();
7723
7724 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
7725 if (!C->getElementType()->isFloatingPointTy())
7726 return false;
7727 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
7728 if (C->getElementAsAPFloat(I).isNaN())
7729 return false;
7730 }
7731 return true;
7732 }
7733
7734 if (isa<ConstantAggregateZero>(V))
7735 return true;
7736
7737 return false;
7738}
7739
7740static bool isKnownNonZero(const Value *V) {
7741 if (auto *C = dyn_cast<ConstantFP>(V))
7742 return !C->isZero();
7743
7744 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
7745 if (!C->getElementType()->isFloatingPointTy())
7746 return false;
7747 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
7748 if (C->getElementAsAPFloat(I).isZero())
7749 return false;
7750 }
7751 return true;
7752 }
7753
7754 return false;
7755}
7756
7757/// Match clamp pattern for float types without care about NaNs or signed zeros.
7758/// Given non-min/max outer cmp/select from the clamp pattern this
7759/// function recognizes if it can be substitued by a "canonical" min/max
7760/// pattern.
7762 Value *CmpLHS, Value *CmpRHS,
7763 Value *TrueVal, Value *FalseVal,
7764 Value *&LHS, Value *&RHS) {
7765 // Try to match
7766 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
7767 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
7768 // and return description of the outer Max/Min.
7769
7770 // First, check if select has inverse order:
7771 if (CmpRHS == FalseVal) {
7772 std::swap(TrueVal, FalseVal);
7773 Pred = CmpInst::getInversePredicate(Pred);
7774 }
7775
7776 // Assume success now. If there's no match, callers should not use these anyway.
7777 LHS = TrueVal;
7778 RHS = FalseVal;
7779
7780 const APFloat *FC1;
7781 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
7782 return {SPF_UNKNOWN, SPNB_NA, false};
7783
7784 const APFloat *FC2;
7785 switch (Pred) {
7786 case CmpInst::FCMP_OLT:
7787 case CmpInst::FCMP_OLE:
7788 case CmpInst::FCMP_ULT:
7789 case CmpInst::FCMP_ULE:
7790 if (match(FalseVal,
7792 m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
7793 *FC1 < *FC2)
7794 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
7795 break;
7796 case CmpInst::FCMP_OGT:
7797 case CmpInst::FCMP_OGE:
7798 case CmpInst::FCMP_UGT:
7799 case CmpInst::FCMP_UGE:
7800 if (match(FalseVal,
7802 m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
7803 *FC1 > *FC2)
7804 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
7805 break;
7806 default:
7807 break;
7808 }
7809
7810 return {SPF_UNKNOWN, SPNB_NA, false};
7811}
7812
7813/// Recognize variations of:
7814/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
7816 Value *CmpLHS, Value *CmpRHS,
7817 Value *TrueVal, Value *FalseVal) {
7818 // Swap the select operands and predicate to match the patterns below.
7819 if (CmpRHS != TrueVal) {
7820 Pred = ICmpInst::getSwappedPredicate(Pred);
7821 std::swap(TrueVal, FalseVal);
7822 }
7823 const APInt *C1;
7824 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
7825 const APInt *C2;
7826 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
7827 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
7828 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
7829 return {SPF_SMAX, SPNB_NA, false};
7830
7831 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
7832 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
7833 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
7834 return {SPF_SMIN, SPNB_NA, false};
7835
7836 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
7837 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
7838 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
7839 return {SPF_UMAX, SPNB_NA, false};
7840
7841 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
7842 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
7843 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
7844 return {SPF_UMIN, SPNB_NA, false};
7845 }
7846 return {SPF_UNKNOWN, SPNB_NA, false};
7847}
7848
7849/// Recognize variations of:
7850/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
7852 Value *CmpLHS, Value *CmpRHS,
7853 Value *TVal, Value *FVal,
7854 unsigned Depth) {
7855 // TODO: Allow FP min/max with nnan/nsz.
7856 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
7857
7858 Value *A = nullptr, *B = nullptr;
7859 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
7860 if (!SelectPatternResult::isMinOrMax(L.Flavor))
7861 return {SPF_UNKNOWN, SPNB_NA, false};
7862
7863 Value *C = nullptr, *D = nullptr;
7864 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
7865 if (L.Flavor != R.Flavor)
7866 return {SPF_UNKNOWN, SPNB_NA, false};
7867
7868 // We have something like: x Pred y ? min(a, b) : min(c, d).
7869 // Try to match the compare to the min/max operations of the select operands.
7870 // First, make sure we have the right compare predicate.
7871 switch (L.Flavor) {
7872 case SPF_SMIN:
7873 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
7874 Pred = ICmpInst::getSwappedPredicate(Pred);
7875 std::swap(CmpLHS, CmpRHS);
7876 }
7877 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
7878 break;
7879 return {SPF_UNKNOWN, SPNB_NA, false};
7880 case SPF_SMAX:
7881 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
7882 Pred = ICmpInst::getSwappedPredicate(Pred);
7883 std::swap(CmpLHS, CmpRHS);
7884 }
7885 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
7886 break;
7887 return {SPF_UNKNOWN, SPNB_NA, false};
7888 case SPF_UMIN:
7889 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
7890 Pred = ICmpInst::getSwappedPredicate(Pred);
7891 std::swap(CmpLHS, CmpRHS);
7892 }
7893 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
7894 break;
7895 return {SPF_UNKNOWN, SPNB_NA, false};
7896 case SPF_UMAX:
7897 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
7898 Pred = ICmpInst::getSwappedPredicate(Pred);
7899 std::swap(CmpLHS, CmpRHS);
7900 }
7901 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
7902 break;
7903 return {SPF_UNKNOWN, SPNB_NA, false};
7904 default:
7905 return {SPF_UNKNOWN, SPNB_NA, false};
7906 }
7907
7908 // If there is a common operand in the already matched min/max and the other
7909 // min/max operands match the compare operands (either directly or inverted),
7910 // then this is min/max of the same flavor.
7911
7912 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
7913 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
7914 if (D == B) {
7915 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
7916 match(A, m_Not(m_Specific(CmpRHS)))))
7917 return {L.Flavor, SPNB_NA, false};
7918 }
7919 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
7920 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
7921 if (C == B) {
7922 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
7923 match(A, m_Not(m_Specific(CmpRHS)))))
7924 return {L.Flavor, SPNB_NA, false};
7925 }
7926 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
7927 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
7928 if (D == A) {
7929 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
7930 match(B, m_Not(m_Specific(CmpRHS)))))
7931 return {L.Flavor, SPNB_NA, false};
7932 }
7933 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
7934 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
7935 if (C == A) {
7936 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
7937 match(B, m_Not(m_Specific(CmpRHS)))))
7938 return {L.Flavor, SPNB_NA, false};
7939 }
7940
7941 return {SPF_UNKNOWN, SPNB_NA, false};
7942}
7943
7944/// If the input value is the result of a 'not' op, constant integer, or vector
7945/// splat of a constant integer, return the bitwise-not source value.
7946/// TODO: This could be extended to handle non-splat vector integer constants.
7948 Value *NotV;
7949 if (match(V, m_Not(m_Value(NotV))))
7950 return NotV;
7951
7952 const APInt *C;
7953 if (match(V, m_APInt(C)))
7954 return ConstantInt::get(V->getType(), ~(*C));
7955
7956 return nullptr;
7957}
7958
7959/// Match non-obvious integer minimum and maximum sequences.
7961 Value *CmpLHS, Value *CmpRHS,
7962 Value *TrueVal, Value *FalseVal,
7963 Value *&LHS, Value *&RHS,
7964 unsigned Depth) {
7965 // Assume success. If there's no match, callers should not use these anyway.
7966 LHS = TrueVal;
7967 RHS = FalseVal;
7968
7969 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
7971 return SPR;
7972
7973 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
7975 return SPR;
7976
7977 // Look through 'not' ops to find disguised min/max.
7978 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
7979 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
7980 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
7981 switch (Pred) {
7982 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
7983 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
7984 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
7985 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
7986 default: break;
7987 }
7988 }
7989
7990 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
7991 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
7992 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
7993 switch (Pred) {
7994 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
7995 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
7996 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
7997 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
7998 default: break;
7999 }
8000 }
8001
8002 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8003 return {SPF_UNKNOWN, SPNB_NA, false};
8004
8005 const APInt *C1;
8006 if (!match(CmpRHS, m_APInt(C1)))
8007 return {SPF_UNKNOWN, SPNB_NA, false};
8008
8009 // An unsigned min/max can be written with a signed compare.
8010 const APInt *C2;
8011 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8012 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8013 // Is the sign bit set?
8014 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8015 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8016 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8017 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8018
8019 // Is the sign bit clear?
8020 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8021 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8022 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8023 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8024 }
8025
8026 return {SPF_UNKNOWN, SPNB_NA, false};
8027}
8028
8029bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW) {
8030 assert(X && Y && "Invalid operand");
8031
8032 // X = sub (0, Y) || X = sub nsw (0, Y)
8033 if ((!NeedNSW && match(X, m_Sub(m_ZeroInt(), m_Specific(Y)))) ||
8034 (NeedNSW && match(X, m_NSWNeg(m_Specific(Y)))))
8035 return true;
8036
8037 // Y = sub (0, X) || Y = sub nsw (0, X)
8038 if ((!NeedNSW && match(Y, m_Sub(m_ZeroInt(), m_Specific(X)))) ||
8039 (NeedNSW && match(Y, m_NSWNeg(m_Specific(X)))))
8040 return true;
8041
8042 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8043 Value *A, *B;
8044 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8045 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8046 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8048}
8049
8051 FastMathFlags FMF,
8052 Value *CmpLHS, Value *CmpRHS,
8053 Value *TrueVal, Value *FalseVal,
8054 Value *&LHS, Value *&RHS,
8055 unsigned Depth) {
8056 bool HasMismatchedZeros = false;
8057 if (CmpInst::isFPPredicate(Pred)) {
8058 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8059 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8060 // purpose of identifying min/max. Disregard vector constants with undefined
8061 // elements because those can not be back-propagated for analysis.
8062 Value *OutputZeroVal = nullptr;
8063 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8064 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8065 OutputZeroVal = TrueVal;
8066 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8067 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8068 OutputZeroVal = FalseVal;
8069
8070 if (OutputZeroVal) {
8071 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8072 HasMismatchedZeros = true;
8073 CmpLHS = OutputZeroVal;
8074 }
8075 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8076 HasMismatchedZeros = true;
8077 CmpRHS = OutputZeroVal;
8078 }
8079 }
8080 }
8081
8082 LHS = CmpLHS;
8083 RHS = CmpRHS;
8084
8085 // Signed zero may return inconsistent results between implementations.
8086 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8087 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8088 // Therefore, we behave conservatively and only proceed if at least one of the
8089 // operands is known to not be zero or if we don't care about signed zero.
8090 switch (Pred) {
8091 default: break;
8094 if (!HasMismatchedZeros)
8095 break;
8096 [[fallthrough]];
8099 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8100 !isKnownNonZero(CmpRHS))
8101 return {SPF_UNKNOWN, SPNB_NA, false};
8102 }
8103
8104 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8105 bool Ordered = false;
8106
8107 // When given one NaN and one non-NaN input:
8108 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8109 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8110 // ordered comparison fails), which could be NaN or non-NaN.
8111 // so here we discover exactly what NaN behavior is required/accepted.
8112 if (CmpInst::isFPPredicate(Pred)) {
8113 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8114 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8115
8116 if (LHSSafe && RHSSafe) {
8117 // Both operands are known non-NaN.
8118 NaNBehavior = SPNB_RETURNS_ANY;
8119 } else if (CmpInst::isOrdered(Pred)) {
8120 // An ordered comparison will return false when given a NaN, so it
8121 // returns the RHS.
8122 Ordered = true;
8123 if (LHSSafe)
8124 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8125 NaNBehavior = SPNB_RETURNS_NAN;
8126 else if (RHSSafe)
8127 NaNBehavior = SPNB_RETURNS_OTHER;
8128 else
8129 // Completely unsafe.
8130 return {SPF_UNKNOWN, SPNB_NA, false};
8131 } else {
8132 Ordered = false;
8133 // An unordered comparison will return true when given a NaN, so it
8134 // returns the LHS.
8135 if (LHSSafe)
8136 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8137 NaNBehavior = SPNB_RETURNS_OTHER;
8138 else if (RHSSafe)
8139 NaNBehavior = SPNB_RETURNS_NAN;
8140 else
8141 // Completely unsafe.
8142 return {SPF_UNKNOWN, SPNB_NA, false};
8143 }
8144 }
8145
8146 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8147 std::swap(CmpLHS, CmpRHS);
8148 Pred = CmpInst::getSwappedPredicate(Pred);
8149 if (NaNBehavior == SPNB_RETURNS_NAN)
8150 NaNBehavior = SPNB_RETURNS_OTHER;
8151 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8152 NaNBehavior = SPNB_RETURNS_NAN;
8153 Ordered = !Ordered;
8154 }
8155
8156 // ([if]cmp X, Y) ? X : Y
8157 if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
8158 switch (Pred) {
8159 default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8160 case ICmpInst::ICMP_UGT:
8161 case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false};
8162 case ICmpInst::ICMP_SGT:
8163 case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false};
8164 case ICmpInst::ICMP_ULT:
8165 case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false};
8166 case ICmpInst::ICMP_SLT:
8167 case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false};
8168 case FCmpInst::FCMP_UGT:
8169 case FCmpInst::FCMP_UGE:
8170 case FCmpInst::FCMP_OGT:
8171 case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered};
8172 case FCmpInst::FCMP_ULT:
8173 case FCmpInst::FCMP_ULE:
8174 case FCmpInst::FCMP_OLT:
8175 case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered};
8176 }
8177 }
8178
8179 if (isKnownNegation(TrueVal, FalseVal)) {
8180 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8181 // match against either LHS or sext(LHS).
8182 auto MaybeSExtCmpLHS =
8183 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8184 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8185 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8186 if (match(TrueVal, MaybeSExtCmpLHS)) {
8187 // Set the return values. If the compare uses the negated value (-X >s 0),
8188 // swap the return values because the negated value is always 'RHS'.
8189 LHS = TrueVal;
8190 RHS = FalseVal;
8191 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8192 std::swap(LHS, RHS);
8193
8194 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8195 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8196 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8197 return {SPF_ABS, SPNB_NA, false};
8198
8199 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8200 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8201 return {SPF_ABS, SPNB_NA, false};
8202
8203 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8204 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8205 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8206 return {SPF_NABS, SPNB_NA, false};
8207 }
8208 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8209 // Set the return values. If the compare uses the negated value (-X >s 0),
8210 // swap the return values because the negated value is always 'RHS'.
8211 LHS = FalseVal;
8212 RHS = TrueVal;
8213 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8214 std::swap(LHS, RHS);
8215
8216 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8217 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8218 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8219 return {SPF_NABS, SPNB_NA, false};
8220
8221 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8222 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8223 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8224 return {SPF_ABS, SPNB_NA, false};
8225 }
8226 }
8227
8228 if (CmpInst::isIntPredicate(Pred))
8229 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8230
8231 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8232 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8233 // semantics than minNum. Be conservative in such case.
8234 if (NaNBehavior != SPNB_RETURNS_ANY ||
8235 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8236 !isKnownNonZero(CmpRHS)))
8237 return {SPF_UNKNOWN, SPNB_NA, false};
8238
8239 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8240}
8241
8242/// Helps to match a select pattern in case of a type mismatch.
8243///
8244/// The function processes the case when type of true and false values of a
8245/// select instruction differs from type of the cmp instruction operands because
8246/// of a cast instruction. The function checks if it is legal to move the cast
8247/// operation after "select". If yes, it returns the new second value of
8248/// "select" (with the assumption that cast is moved):
8249/// 1. As operand of cast instruction when both values of "select" are same cast
8250/// instructions.
8251/// 2. As restored constant (by applying reverse cast operation) when the first
8252/// value of the "select" is a cast operation and the second value is a
8253/// constant.
8254/// NOTE: We return only the new second value because the first value could be
8255/// accessed as operand of cast instruction.
8256static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8257 Instruction::CastOps *CastOp) {
8258 auto *Cast1 = dyn_cast<CastInst>(V1);
8259 if (!Cast1)
8260 return nullptr;
8261
8262 *CastOp = Cast1->getOpcode();
8263 Type *SrcTy = Cast1->getSrcTy();
8264 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8265 // If V1 and V2 are both the same cast from the same type, look through V1.
8266 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
8267 return Cast2->getOperand(0);
8268 return nullptr;
8269 }
8270
8271 auto *C = dyn_cast<Constant>(V2);
8272 if (!C)
8273 return nullptr;
8274
8275 const DataLayout &DL = CmpI->getModule()->getDataLayout();
8276 Constant *CastedTo = nullptr;
8277 switch (*CastOp) {
8278 case Instruction::ZExt:
8279 if (CmpI->isUnsigned())
8280 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8281 break;
8282 case Instruction::SExt:
8283 if (CmpI->isSigned())
8284 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8285 break;
8286 case Instruction::Trunc:
8287 Constant *CmpConst;
8288 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8289 CmpConst->getType() == SrcTy) {
8290 // Here we have the following case:
8291 //
8292 // %cond = cmp iN %x, CmpConst
8293 // %tr = trunc iN %x to iK
8294 // %narrowsel = select i1 %cond, iK %t, iK C
8295 //
8296 // We can always move trunc after select operation:
8297 //
8298 // %cond = cmp iN %x, CmpConst
8299 // %widesel = select i1 %cond, iN %x, iN CmpConst
8300 // %tr = trunc iN %widesel to iK
8301 //
8302 // Note that C could be extended in any way because we don't care about
8303 // upper bits after truncation. It can't be abs pattern, because it would
8304 // look like:
8305 //
8306 // select i1 %cond, x, -x.
8307 //
8308 // So only min/max pattern could be matched. Such match requires widened C
8309 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8310 // CmpConst == C is checked below.
8311 CastedTo = CmpConst;
8312 } else {
8313 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8314 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8315 }
8316 break;
8317 case Instruction::FPTrunc:
8318 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8319 break;
8320 case Instruction::FPExt:
8321 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8322 break;
8323 case Instruction::FPToUI:
8324 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8325 break;
8326 case Instruction::FPToSI:
8327 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8328 break;
8329 case Instruction::UIToFP:
8330 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8331 break;
8332 case Instruction::SIToFP:
8333 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8334 break;
8335 default:
8336 break;
8337 }
8338
8339 if (!CastedTo)
8340 return nullptr;
8341
8342 // Make sure the cast doesn't lose any information.
8343 Constant *CastedBack =
8344 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8345 if (CastedBack && CastedBack != C)
8346 return nullptr;
8347
8348 return CastedTo;
8349}
8350
8352 Instruction::CastOps *CastOp,
8353 unsigned Depth) {
8355 return {SPF_UNKNOWN, SPNB_NA, false};
8356
8357 SelectInst *SI = dyn_cast<SelectInst>(V);
8358 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
8359
8360 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
8361 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
8362
8363 Value *TrueVal = SI->getTrueValue();
8364 Value *FalseVal = SI->getFalseValue();
8365
8366 return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
8367 CastOp, Depth);
8368}
8369
8371 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
8372 Instruction::CastOps *CastOp, unsigned Depth) {
8373 CmpInst::Predicate Pred = CmpI->getPredicate();
8374 Value *CmpLHS = CmpI->getOperand(0);
8375 Value *CmpRHS = CmpI->getOperand(1);
8376 FastMathFlags FMF;
8377 if (isa<FPMathOperator>(CmpI))
8378 FMF = CmpI->getFastMathFlags();
8379
8380 // Bail out early.
8381 if (CmpI->isEquality())
8382 return {SPF_UNKNOWN, SPNB_NA, false};
8383
8384 // Deal with type mismatches.
8385 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
8386 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
8387 // If this is a potential fmin/fmax with a cast to integer, then ignore
8388 // -0.0 because there is no corresponding integer value.
8389 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
8390 FMF.setNoSignedZeros();
8391 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
8392 cast<CastInst>(TrueVal)->getOperand(0), C,
8393 LHS, RHS, Depth);
8394 }
8395 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
8396 // If this is a potential fmin/fmax with a cast to integer, then ignore
8397 // -0.0 because there is no corresponding integer value.
8398 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
8399 FMF.setNoSignedZeros();
8400 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
8401 C, cast<CastInst>(FalseVal)->getOperand(0),
8402 LHS, RHS, Depth);
8403 }
8404 }
8405 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
8406 LHS, RHS, Depth);
8407}
8408
8410 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
8411 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
8412 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
8413 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
8414 if (SPF == SPF_FMINNUM)
8415 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
8416 if (SPF == SPF_FMAXNUM)
8417 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
8418 llvm_unreachable("unhandled!");
8419}
8420
8422 if (SPF == SPF_SMIN) return SPF_SMAX;
8423 if (SPF == SPF_UMIN) return SPF_UMAX;
8424 if (SPF == SPF_SMAX) return SPF_SMIN;
8425 if (SPF == SPF_UMAX) return SPF_UMIN;
8426 llvm_unreachable("unhandled!");
8427}
8428
8430 switch (MinMaxID) {
8431 case Intrinsic::smax: return Intrinsic::smin;
8432 case Intrinsic::smin: return Intrinsic::smax;
8433 case Intrinsic::umax: return Intrinsic::umin;
8434 case Intrinsic::umin: return Intrinsic::umax;
8435 // Please note that next four intrinsics may produce the same result for
8436 // original and inverted case even if X != Y due to NaN is handled specially.
8437 case Intrinsic::maximum: return Intrinsic::minimum;
8438 case Intrinsic::minimum: return Intrinsic::maximum;
8439 case Intrinsic::maxnum: return Intrinsic::minnum;
8440 case Intrinsic::minnum: return Intrinsic::maxnum;
8441 default: llvm_unreachable("Unexpected intrinsic");
8442 }
8443}
8444
8446 switch (SPF) {
8449 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
8450 case SPF_UMIN: return APInt::getMinValue(BitWidth);
8451 default: llvm_unreachable("Unexpected flavor");
8452 }
8453}
8454
8455std::pair<Intrinsic::ID, bool>
8457 // Check if VL contains select instructions that can be folded into a min/max
8458 // vector intrinsic and return the intrinsic if it is possible.
8459 // TODO: Support floating point min/max.
8460 bool AllCmpSingleUse = true;
8461 SelectPatternResult SelectPattern;
8462 SelectPattern.Flavor = SPF_UNKNOWN;
8463 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
8464 Value *LHS, *RHS;
8465 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
8466 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor) ||
8467 CurrentPattern.Flavor == SPF_FMINNUM ||
8468 CurrentPattern.Flavor == SPF_FMAXNUM ||
8469 !I->getType()->isIntOrIntVectorTy())
8470 return false;
8471 if (SelectPattern.Flavor != SPF_UNKNOWN &&
8472 SelectPattern.Flavor != CurrentPattern.Flavor)
8473 return false;
8474 SelectPattern = CurrentPattern;
8475 AllCmpSingleUse &=
8477 return true;
8478 })) {
8479 switch (SelectPattern.Flavor) {
8480 case SPF_SMIN:
8481 return {Intrinsic::smin, AllCmpSingleUse};
8482 case SPF_UMIN:
8483 return {Intrinsic::umin, AllCmpSingleUse};
8484 case SPF_SMAX:
8485 return {Intrinsic::smax, AllCmpSingleUse};
8486 case SPF_UMAX:
8487 return {Intrinsic::umax, AllCmpSingleUse};
8488 default:
8489 llvm_unreachable("unexpected select pattern flavor");
8490 }
8491 }
8492 return {Intrinsic::not_intrinsic, false};
8493}
8494
8496 Value *&Start, Value *&Step) {
8497 // Handle the case of a simple two-predecessor recurrence PHI.
8498 // There's a lot more that could theoretically be done here, but
8499 // this is sufficient to catch some interesting cases.
8500 if (P->getNumIncomingValues() != 2)
8501 return false;
8502
8503 for (unsigned i = 0; i != 2; ++i) {
8504 Value *L = P->getIncomingValue(i);
8505 Value *R = P->getIncomingValue(!i);
8506 auto *LU = dyn_cast<BinaryOperator>(L);
8507 if (!LU)
8508 continue;
8509 unsigned Opcode = LU->getOpcode();
8510
8511 switch (Opcode) {
8512 default:
8513 continue;
8514 // TODO: Expand list -- xor, div, gep, uaddo, etc..
8515 case Instruction::LShr:
8516 case Instruction::AShr:
8517 case Instruction::Shl:
8518 case Instruction::Add:
8519 case Instruction::Sub:
8520 case Instruction::And:
8521 case Instruction::Or:
8522 case Instruction::Mul:
8523 case Instruction::FMul: {
8524 Value *LL = LU->getOperand(0);
8525 Value *LR = LU->getOperand(1);
8526 // Find a recurrence.
8527 if (LL == P)
8528 L = LR;
8529 else if (LR == P)
8530 L = LL;
8531 else
8532 continue; // Check for recurrence with L and R flipped.
8533
8534 break; // Match!
8535 }
8536 };
8537
8538 // We have matched a recurrence of the form:
8539 // %iv = [R, %entry], [%iv.next, %backedge]
8540 // %iv.next = binop %iv, L
8541 // OR
8542 // %iv = [R, %entry], [%iv.next, %backedge]
8543 // %iv.next = binop L, %iv
8544 BO = LU;
8545 Start = R;
8546 Step = L;
8547 return true;
8548 }
8549 return false;
8550}
8551
8553 Value *&Start, Value *&Step) {
8554 BinaryOperator *BO = nullptr;
8555 P = dyn_cast<PHINode>(I->getOperand(0));
8556 if (!P)
8557 P = dyn_cast<PHINode>(I->getOperand(1));
8558 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
8559}
8560
8561/// Return true if "icmp Pred LHS RHS" is always true.
8562static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
8563 const Value *RHS) {
8564 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
8565 return true;
8566
8567 switch (Pred) {
8568 default:
8569 return false;
8570
8571 case CmpInst::ICMP_SLE: {
8572 const APInt *C;
8573
8574 // LHS s<= LHS +_{nsw} C if C >= 0
8575 // LHS s<= LHS | C if C >= 0
8576 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
8578 return !C->isNegative();
8579
8580 // LHS s<= smax(LHS, V) for any V
8582 return true;
8583
8584 // smin(RHS, V) s<= RHS for any V
8586 return true;
8587
8588 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
8589 const Value *X;
8590 const APInt *CLHS, *CRHS;
8591 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
8593 return CLHS->sle(*CRHS);
8594
8595 return false;
8596 }
8597
8598 case CmpInst::ICMP_ULE: {
8599 // LHS u<= LHS +_{nuw} V for any V
8600 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
8601 cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
8602 return true;
8603
8604 // LHS u<= LHS | V for any V
8605 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
8606 return true;
8607
8608 // LHS u<= umax(LHS, V) for any V
8610 return true;
8611
8612 // RHS >> V u<= RHS for any V
8613 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
8614 return true;
8615
8616 // RHS u/ C_ugt_1 u<= RHS
8617 const APInt *C;
8618 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
8619 return true;
8620
8621 // RHS & V u<= RHS for any V
8623 return true;
8624
8625 // umin(RHS, V) u<= RHS for any V
8627 return true;
8628
8629 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
8630 const Value *X;
8631 const APInt *CLHS, *CRHS;
8632 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
8634 return CLHS->ule(*CRHS);
8635
8636 return false;
8637 }
8638 }
8639}
8640
8641/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
8642/// ALHS ARHS" is true. Otherwise, return std::nullopt.
8643static std::optional<bool>
8645 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
8646 switch (Pred) {
8647 default:
8648 return std::nullopt;
8649
8650 case CmpInst::ICMP_SLT:
8651 case CmpInst::ICMP_SLE:
8652 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
8654 return true;
8655 return std::nullopt;
8656
8657 case CmpInst::ICMP_SGT:
8658 case CmpInst::ICMP_SGE:
8659 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
8661 return true;
8662 return std::nullopt;
8663
8664 case CmpInst::ICMP_ULT:
8665 case CmpInst::ICMP_ULE:
8666 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
8668 return true;
8669 return std::nullopt;
8670
8671 case CmpInst::ICMP_UGT:
8672 case CmpInst::ICMP_UGE:
8673 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
8675 return true;
8676 return std::nullopt;
8677 }
8678}
8679
8680/// Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true.
8681/// Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false.
8682/// Otherwise, return std::nullopt if we can't infer anything.
8683static std::optional<bool>
8685 CmpInst::Predicate RPred) {
8686 if (CmpInst::isImpliedTrueByMatchingCmp(LPred, RPred))
8687 return true;
8688 if (CmpInst::isImpliedFalseByMatchingCmp(LPred, RPred))
8689 return false;
8690
8691 return std::nullopt;
8692}
8693
8694/// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true.
8695/// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false.
8696/// Otherwise, return std::nullopt if we can't infer anything.
8698 CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred,
8699 const APInt &RC) {
8702 ConstantRange Intersection = DomCR.intersectWith(CR);
8703 ConstantRange Difference = DomCR.difference(CR);
8704 if (Intersection.isEmptySet())
8705 return false;
8706 if (Difference.isEmptySet())
8707 return true;
8708 return std::nullopt;
8709}
8710
8711/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
8712/// is true. Return false if LHS implies RHS is false. Otherwise, return
8713/// std::nullopt if we can't infer anything.
8714static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
8715 CmpInst::Predicate RPred,
8716 const Value *R0, const Value *R1,
8717 const DataLayout &DL,
8718 bool LHSIsTrue) {
8719 Value *L0 = LHS->getOperand(0);
8720 Value *L1 = LHS->getOperand(1);
8721
8722 // The rest of the logic assumes the LHS condition is true. If that's not the
8723 // case, invert the predicate to make it so.
8724 CmpInst::Predicate LPred =
8725 LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
8726
8727 // We can have non-canonical operands, so try to normalize any common operand
8728 // to L0/R0.
8729 if (L0 == R1) {
8730 std::swap(R0, R1);
8731 RPred = ICmpInst::getSwappedPredicate(RPred);
8732 }
8733 if (R0 == L1) {
8734 std::swap(L0, L1);
8735 LPred = ICmpInst::getSwappedPredicate(LPred);
8736 }
8737 if (L1 == R1) {
8738 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
8739 if (L0 != R0 || match(L0, m_ImmConstant())) {
8740 std::swap(L0, L1);
8741 LPred = ICmpInst::getSwappedPredicate(LPred);
8742 std::swap(R0, R1);
8743 RPred = ICmpInst::getSwappedPredicate(RPred);
8744 }
8745 }
8746
8747 // Can we infer anything when the 0-operands match and the 1-operands are
8748 // constants (not necessarily matching)?
8749 const APInt *LC, *RC;
8750 if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
8751 return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
8752
8753 // Can we infer anything when the two compares have matching operands?
8754 if (L0 == R0 && L1 == R1)
8755 return isImpliedCondMatchingOperands(LPred, RPred);
8756
8757 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
8758 if (L0 == R0 &&
8759 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
8760 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
8761 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
8762 return LPred == RPred;
8763
8764 if (LPred == RPred)
8765 return isImpliedCondOperands(LPred, L0, L1, R0, R1);
8766
8767 return std::nullopt;
8768}
8769
8770/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
8771/// false. Otherwise, return std::nullopt if we can't infer anything. We
8772/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
8773/// instruction.
8774static std::optional<bool>
8776 const Value *RHSOp0, const Value *RHSOp1,
8777 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
8778 // The LHS must be an 'or', 'and', or a 'select' instruction.
8779 assert((LHS->getOpcode() == Instruction::And ||
8780 LHS->getOpcode() == Instruction::Or ||
8781 LHS->getOpcode() == Instruction::Select) &&
8782 "Expected LHS to be 'and', 'or', or 'select'.");
8783
8784 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
8785
8786 // If the result of an 'or' is false, then we know both legs of the 'or' are
8787 // false. Similarly, if the result of an 'and' is true, then we know both
8788 // legs of the 'and' are true.
8789 const Value *ALHS, *ARHS;
8790 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
8791 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
8792 // FIXME: Make this non-recursion.
8793 if (std::optional<bool> Implication = isImpliedCondition(
8794 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
8795 return Implication;
8796 if (std::optional<bool> Implication = isImpliedCondition(
8797 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
8798 return Implication;
8799 return std::nullopt;
8800 }
8801 return std::nullopt;
8802}
8803
8804std::optional<bool>
8806 const Value *RHSOp0, const Value *RHSOp1,
8807 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
8808 // Bail out when we hit the limit.
8810 return std::nullopt;
8811
8812 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
8813 // example.
8814 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
8815 return std::nullopt;
8816
8818 "Expected integer type only!");
8819
8820 // Match not
8821 if (match(LHS, m_Not(m_Value(LHS))))
8822 LHSIsTrue = !LHSIsTrue;
8823
8824 // Both LHS and RHS are icmps.
8825 const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
8826 if (LHSCmp)
8827 return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
8828
8829 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
8830 /// the RHS to be an icmp.
8831 /// FIXME: Add support for and/or/select on the RHS.
8832 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
8833 if ((LHSI->getOpcode() == Instruction::And ||
8834 LHSI->getOpcode() == Instruction::Or ||
8835 LHSI->getOpcode() == Instruction::Select))
8836 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
8837 Depth);
8838 }
8839 return std::nullopt;
8840}
8841
8842std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
8843 const DataLayout &DL,
8844 bool LHSIsTrue, unsigned Depth) {
8845 // LHS ==> RHS by definition
8846 if (LHS == RHS)
8847 return LHSIsTrue;
8848
8849 // Match not
8850 bool InvertRHS = false;
8851 if (match(RHS, m_Not(m_Value(RHS)))) {
8852 if (LHS == RHS)
8853 return !LHSIsTrue;
8854 InvertRHS = true;
8855 }
8856
8857 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
8858 if (auto Implied = isImpliedCondition(
8859 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
8860 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
8861 return InvertRHS ? !*Implied : *Implied;
8862 return std::nullopt;
8863 }
8864
8866 return std::nullopt;
8867
8868 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
8869 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
8870 const Value *RHS1, *RHS2;
8871 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
8872 if (std::optional<bool> Imp =
8873 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
8874 if (*Imp == true)
8875 return !InvertRHS;
8876 if (std::optional<bool> Imp =
8877 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
8878 if (*Imp == true)
8879 return !InvertRHS;
8880 }
8881 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
8882 if (std::optional<bool> Imp =
8883 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
8884 if (*Imp == false)
8885 return InvertRHS;
8886 if (std::optional<bool> Imp =
8887 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
8888 if (*Imp == false)
8889 return InvertRHS;
8890 }
8891
8892 return std::nullopt;
8893}
8894
8895// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
8896// condition dominating ContextI or nullptr, if no condition is found.
8897static std::pair<Value *, bool>
8899 if (!ContextI || !ContextI->getParent())
8900 return {nullptr, false};
8901
8902 // TODO: This is a poor/cheap way to determine dominance. Should we use a
8903 // dominator tree (eg, from a SimplifyQuery) instead?
8904 const BasicBlock *ContextBB = ContextI->getParent();
8905 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
8906 if (!PredBB)
8907 return {nullptr, false};
8908
8909 // We need a conditional branch in the predecessor.
8910 Value *PredCond;
8911 BasicBlock *TrueBB, *FalseBB;
8912 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
8913 return {nullptr, false};
8914
8915 // The branch should get simplified. Don't bother simplifying this condition.
8916 if (TrueBB == FalseBB)
8917 return {nullptr, false};
8918
8919 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
8920 "Predecessor block does not point to successor?");
8921
8922 // Is this condition implied by the predecessor condition?
8923 return {PredCond, TrueBB == ContextBB};
8924}
8925
8926std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
8927 const Instruction *ContextI,
8928 const DataLayout &DL) {
8929 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
8930 auto PredCond = getDomPredecessorCondition(ContextI);
8931 if (PredCond.first)
8932 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
8933 return std::nullopt;
8934}
8935
8937 const Value *LHS,
8938 const Value *RHS,
8939 const Instruction *ContextI,
8940 const DataLayout &DL) {
8941 auto PredCond = getDomPredecessorCondition(ContextI);
8942 if (PredCond.first)
8943 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
8944 PredCond.second);
8945 return std::nullopt;
8946}
8947
8949 APInt &Upper, const InstrInfoQuery &IIQ,
8950 bool PreferSignedRange) {
8951 unsigned Width = Lower.getBitWidth();
8952 const APInt *C;
8953 switch (BO.getOpcode()) {
8954 case Instruction::Add:
8955 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
8956 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
8957 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
8958
8959 // If the caller expects a signed compare, then try to use a signed range.
8960 // Otherwise if both no-wraps are set, use the unsigned range because it
8961 // is never larger than the signed range. Example:
8962 // "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
8963 if (PreferSignedRange && HasNSW && HasNUW)
8964 HasNUW = false;
8965
8966 if (HasNUW) {
8967 // 'add nuw x, C' produces [C, UINT_MAX].
8968 Lower = *C;
8969 } else if (HasNSW) {
8970 if (C->isNegative()) {
8971 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
8973 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
8974 } else {
8975 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
8976 Lower = APInt::getSignedMinValue(Width) + *C;
8977 Upper = APInt::getSignedMaxValue(Width) + 1;
8978 }
8979 }
8980 }
8981 break;
8982
8983 case Instruction::And:
8984 if (match(BO.getOperand(1), m_APInt(C)))
8985 // 'and x, C' produces [0, C].
8986 Upper = *C + 1;
8987 // X & -X is a power of two or zero. So we can cap the value at max power of
8988 // two.
8989 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
8990 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
8991 Upper = APInt::getSignedMinValue(Width) + 1;
8992 break;
8993
8994 case Instruction::Or:
8995 if (match(BO.getOperand(1), m_APInt(C)))
8996 // 'or x, C' produces [C, UINT_MAX].
8997 Lower = *C;
8998 break;
8999
9000 case Instruction::AShr:
9001 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9002 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9004 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9005 } else if (match(BO.getOperand(0), m_APInt(C))) {
9006 unsigned ShiftAmount = Width - 1;
9007 if (!C->isZero() && IIQ.isExact(&BO))
9008 ShiftAmount = C->countr_zero();
9009 if (C->isNegative()) {
9010 // 'ashr C, x' produces [C, C >> (Width-1)]
9011 Lower = *C;
9012 Upper = C->ashr(ShiftAmount) + 1;
9013 } else {
9014 // 'ashr C, x' produces [C >> (Width-1), C]
9015 Lower = C->ashr(ShiftAmount);
9016 Upper = *C + 1;
9017 }
9018 }
9019 break;
9020
9021 case Instruction::LShr:
9022 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9023 // 'lshr x, C' produces [0, UINT_MAX >> C].
9024 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9025 } else if (match(BO.getOperand(0), m_APInt(C))) {
9026 // 'lshr C, x' produces [C >> (Width-1), C].
9027 unsigned ShiftAmount = Width - 1;
9028 if (!C->isZero() && IIQ.isExact(&BO))
9029 ShiftAmount = C->countr_zero();
9030 Lower = C->lshr(ShiftAmount);
9031 Upper = *C + 1;
9032 }
9033 break;
9034
9035 case Instruction::Shl:
9036 if (match(BO.getOperand(0), m_APInt(C))) {
9037 if (IIQ.hasNoUnsignedWrap(&BO)) {
9038 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9039 Lower = *C;
9040 Upper = Lower.shl(Lower.countl_zero()) + 1;
9041 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9042 if (C->isNegative()) {
9043 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9044 unsigned ShiftAmount = C->countl_one() - 1;
9045 Lower = C->shl(ShiftAmount);
9046 Upper = *C + 1;
9047 } else {
9048 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9049 unsigned ShiftAmount = C->countl_zero() - 1;
9050 Lower = *C;
9051 Upper = C->shl(ShiftAmount) + 1;
9052 }
9053 } else {
9054 // If lowbit is set, value can never be zero.
9055 if ((*C)[0])
9056 Lower = APInt::getOneBitSet(Width, 0);
9057 // If we are shifting a constant the largest it can be is if the longest
9058 // sequence of consecutive ones is shifted to the highbits (breaking
9059 // ties for which sequence is higher). At the moment we take a liberal
9060 // upper bound on this by just popcounting the constant.
9061 // TODO: There may be a bitwise trick for it longest/highest
9062 // consecutative sequence of ones (naive method is O(Width) loop).
9063 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9064 }
9065 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9066 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9067 }
9068 break;
9069
9070 case Instruction::SDiv:
9071 if (match(BO.getOperand(1), m_APInt(C))) {
9072 APInt IntMin = APInt::getSignedMinValue(Width);
9073 APInt IntMax = APInt::getSignedMaxValue(Width);
9074 if (C->isAllOnes()) {
9075 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9076 // where C != -1 and C != 0 and C != 1
9077 Lower = IntMin + 1;
9078 Upper = IntMax + 1;
9079 } else if (C->countl_zero() < Width - 1) {
9080 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9081 // where C != -1 and C != 0 and C != 1
9082 Lower = IntMin.sdiv(*C);
9083 Upper = IntMax.sdiv(*C);
9084 if (Lower.sgt(Upper))
9086 Upper = Upper + 1;
9087 assert(Upper != Lower && "Upper part of range has wrapped!");
9088 }
9089 } else if (match(BO.getOperand(0), m_APInt(C))) {
9090 if (C->isMinSignedValue()) {
9091 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9092 Lower = *C;
9093 Upper = Lower.lshr(1) + 1;
9094 } else {
9095 // 'sdiv C, x' produces [-|C|, |C|].
9096 Upper = C->abs() + 1;
9097 Lower = (-Upper) + 1;
9098 }
9099 }
9100 break;
9101
9102 case Instruction::UDiv:
9103 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9104 // 'udiv x, C' produces [0, UINT_MAX / C].
9105 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9106 } else if (match(BO.getOperand(0), m_APInt(C))) {
9107 // 'udiv C, x' produces [0, C].
9108 Upper = *C + 1;
9109 }
9110 break;
9111
9112 case Instruction::SRem:
9113 if (match(BO.getOperand(1), m_APInt(C))) {
9114 // 'srem x, C' produces (-|C|, |C|).
9115 Upper = C->abs();
9116 Lower = (-Upper) + 1;
9117 } else if (match(BO.getOperand(0), m_APInt(C))) {
9118 if (C->isNegative()) {
9119 // 'srem -|C|, x' produces [-|C|, 0].
9120 Upper = 1;
9121 Lower = *C;
9122 } else {
9123 // 'srem |C|, x' produces [0, |C|].
9124 Upper = *C + 1;
9125 }
9126 }
9127 break;
9128
9129 case Instruction::URem:
9130 if (match(BO.getOperand(1), m_APInt(C)))
9131 // 'urem x, C' produces [0, C).
9132 Upper = *C;
9133 else if (match(BO.getOperand(0), m_APInt(C)))
9134 // 'urem C, x' produces [0, C].
9135 Upper = *C + 1;
9136 break;
9137
9138 default:
9139 break;
9140 }
9141}
9142
9144 unsigned Width = II.getType()->getScalarSizeInBits();
9145 const APInt *C;
9146 switch (II.getIntrinsicID()) {
9147 case Intrinsic::ctpop:
9148 case Intrinsic::ctlz:
9149 case Intrinsic::cttz:
9150 // Maximum of set/clear bits is the bit width.
9152 APInt(Width, Width + 1));
9153 case Intrinsic::uadd_sat:
9154 // uadd.sat(x, C) produces [C, UINT_MAX].
9155 if (match(II.getOperand(0), m_APInt(C)) ||
9156 match(II.getOperand(1), m_APInt(C)))
9158 break;
9159 case Intrinsic::sadd_sat:
9160 if (match(II.getOperand(0), m_APInt(C)) ||
9161 match(II.getOperand(1), m_APInt(C))) {
9162 if (C->isNegative())
9163 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9165 APInt::getSignedMaxValue(Width) + *C +
9166 1);
9167
9168 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9170 APInt::getSignedMaxValue(Width) + 1);
9171 }
9172 break;
9173 case Intrinsic::usub_sat:
9174 // usub.sat(C, x) produces [0, C].
9175 if (match(II.getOperand(0), m_APInt(C)))
9176 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9177
9178 // usub.sat(x, C) produces [0, UINT_MAX - C].
9179 if (match(II.getOperand(1), m_APInt(C)))
9181 APInt::getMaxValue(Width) - *C + 1);
9182 break;
9183 case Intrinsic::ssub_sat:
9184 if (match(II.getOperand(0), m_APInt(C))) {
9185 if (C->isNegative())
9186 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9188 *C - APInt::getSignedMinValue(Width) +
9189 1);
9190
9191 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9193 APInt::getSignedMaxValue(Width) + 1);
9194 } else if (match(II.getOperand(1), m_APInt(C))) {
9195 if (C->isNegative())
9196 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9198 APInt::getSignedMaxValue(Width) + 1);
9199
9200 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9202 APInt::getSignedMaxValue(Width) - *C +
9203 1);
9204 }
9205 break;
9206 case Intrinsic::umin:
9207 case Intrinsic::umax:
9208 case Intrinsic::smin:
9209 case Intrinsic::smax:
9210 if (!match(II.getOperand(0), m_APInt(C)) &&
9211 !match(II.getOperand(1), m_APInt(C)))
9212 break;
9213
9214 switch (II.getIntrinsicID()) {
9215 case Intrinsic::umin:
9216 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9217 case Intrinsic::umax:
9219 case Intrinsic::smin:
9221 *C + 1);
9222 case Intrinsic::smax:
9224 APInt::getSignedMaxValue(Width) + 1);
9225 default:
9226 llvm_unreachable("Must be min/max intrinsic");
9227 }
9228 break;
9229 case Intrinsic::abs:
9230 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
9231 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9232 if (match(II.getOperand(1), m_One()))
9234 APInt::getSignedMaxValue(Width) + 1);
9235
9237 APInt::getSignedMinValue(Width) + 1);
9238 case Intrinsic::vscale:
9239 if (!II.getParent() || !II.getFunction())
9240 break;
9241 return getVScaleRange(II.getFunction(), Width);
9242 default:
9243 break;
9244 }
9245
9246 return ConstantRange::getFull(Width);
9247}
9248
9250 const InstrInfoQuery &IIQ) {
9251 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
9252 const Value *LHS = nullptr, *RHS = nullptr;
9254 if (R.Flavor == SPF_UNKNOWN)
9255 return ConstantRange::getFull(BitWidth);
9256
9257 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
9258 // If the negation part of the abs (in RHS) has the NSW flag,
9259 // then the result of abs(X) is [0..SIGNED_MAX],
9260 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9261 if (match(RHS, m_Neg(m_Specific(LHS))) &&
9262 IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
9265
9268 }
9269
9270 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
9271 // The result of -abs(X) is <= 0.
9273 APInt(BitWidth, 1));
9274 }
9275
9276 const APInt *C;
9277 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
9278 return ConstantRange::getFull(BitWidth);
9279
9280 switch (R.Flavor) {
9281 case SPF_UMIN:
9283 case SPF_UMAX:
9285 case SPF_SMIN:
9287 *C + 1);
9288 case SPF_SMAX:
9291 default:
9292 return ConstantRange::getFull(BitWidth);
9293 }
9294}
9295
9297 // The maximum representable value of a half is 65504. For floats the maximum
9298 // value is 3.4e38 which requires roughly 129 bits.
9299 unsigned BitWidth = I->getType()->getScalarSizeInBits();
9300 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
9301 return;
9302 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
9303 Lower = APInt(BitWidth, -65504);
9304 Upper = APInt(BitWidth, 65505);
9305 }
9306
9307 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
9308 // For a fptoui the lower limit is left as 0.
9309 Upper = APInt(BitWidth, 65505);
9310 }
9311}
9312
9314 bool UseInstrInfo, AssumptionCache *AC,
9315 const Instruction *CtxI,
9316 const DominatorTree *DT,
9317 unsigned Depth) {
9318 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
9319
9321 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
9322
9323 const APInt *C;
9324 if (match(V, m_APInt(C)))
9325 return ConstantRange(*C);
9326 unsigned BitWidth = V->getType()->getScalarSizeInBits();
9327
9328 if (auto *VC = dyn_cast<ConstantDataVector>(V)) {
9329 ConstantRange CR = ConstantRange::getEmpty(BitWidth);
9330 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
9331 ++ElemIdx)
9332 CR = CR.unionWith(VC->getElementAsAPInt(ElemIdx));
9333 return CR;
9334 }
9335
9336 InstrInfoQuery IIQ(UseInstrInfo);
9337 ConstantRange CR = ConstantRange::getFull(BitWidth);
9338 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
9339 APInt Lower = APInt(BitWidth, 0);
9340 APInt Upper = APInt(BitWidth, 0);
9341 // TODO: Return ConstantRange.
9342 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
9344 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
9345 CR = getRangeForIntrinsic(*II);
9346 else if (auto *SI = dyn_cast<SelectInst>(V)) {
9348 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
9350 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
9351 CR = CRTrue.unionWith(CRFalse);
9352 CR = CR.intersectWith(getRangeForSelectPattern(*SI, IIQ));
9353 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
9354 APInt Lower = APInt(BitWidth, 0);
9355 APInt Upper = APInt(BitWidth, 0);
9356 // TODO: Return ConstantRange.
9357 setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
9359 } else if (const auto *A = dyn_cast<Argument>(V))
9360 if (std::optional<ConstantRange> Range = A->getRange())
9361 CR = *Range;
9362
9363 if (auto *I = dyn_cast<Instruction>(V)) {
9364 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
9366
9367 if (const auto *CB = dyn_cast<CallBase>(V))
9368 if (std::optional<ConstantRange> Range = CB->getRange())
9369 CR = CR.intersectWith(*Range);
9370 }
9371
9372 if (CtxI && AC) {
9373 // Try to restrict the range based on information from assumptions.
9374 for (auto &AssumeVH : AC->assumptionsFor(V)) {
9375 if (!AssumeVH)
9376 continue;
9377 CallInst *I = cast<CallInst>(AssumeVH);
9378 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
9379 "Got assumption for the wrong function!");
9380 assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
9381 "must be an assume intrinsic");
9382
9383 if (!isValidAssumeForContext(I, CtxI, DT))
9384 continue;
9385 Value *Arg = I->getArgOperand(0);
9386 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
9387 // Currently we just use information from comparisons.
9388 if (!Cmp || Cmp->getOperand(0) != V)
9389 continue;
9390 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
9392 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
9393 UseInstrInfo, AC, I, DT, Depth + 1);
9394 CR = CR.intersectWith(
9395 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
9396 }
9397 }
9398
9399 return CR;
9400}
9401
9402static void
9404 function_ref<void(Value *)> InsertAffected) {
9405 assert(V != nullptr);
9406 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
9407 InsertAffected(V);
9408 } else if (auto *I = dyn_cast<Instruction>(V)) {
9409 InsertAffected(V);
9410
9411 // Peek through unary operators to find the source of the condition.
9412 Value *Op;
9414 if (isa<Instruction>(Op) || isa<Argument>(Op))
9415 InsertAffected(Op);
9416 }
9417 }
9418}
9419
9421 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
9422 auto AddAffected = [&InsertAffected](Value *V) {
9423 addValueAffectedByCondition(V, InsertAffected);
9424 };
9425
9426 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
9427 if (IsAssume) {
9428 AddAffected(LHS);
9429 AddAffected(RHS);
9430 } else if (match(RHS, m_Constant()))
9431 AddAffected(LHS);
9432 };
9433
9434 SmallVector<Value *, 8> Worklist;
9436 Worklist.push_back(Cond);
9437 while (!Worklist.empty()) {
9438 Value *V = Worklist.pop_back_val();
9439 if (!Visited.insert(V).second)
9440 continue;
9441
9442 CmpInst::Predicate Pred;
9443 Value *A, *B, *X;
9444
9445 if (IsAssume) {
9446 AddAffected(V);
9447 if (match(V, m_Not(m_Value(X))))
9448 AddAffected(X);
9449 }
9450
9451 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
9452 // assume(A && B) is split to -> assume(A); assume(B);
9453 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
9454 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
9455 // enough information to be worth handling (intersection of information as
9456 // opposed to union).
9457 if (!IsAssume) {
9458 Worklist.push_back(A);
9459 Worklist.push_back(B);
9460 }
9461 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
9462 AddCmpOperands(A, B);
9463
9464 if (ICmpInst::isEquality(Pred)) {
9465 if (match(B, m_ConstantInt())) {
9466 Value *Y;
9467 // (X & C) or (X | C) or (X ^ C).
9468 // (X << C) or (X >>_s C) or (X >>_u C).
9471 AddAffected(X);
9472 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
9473 match(A, m_Or(m_Value(X), m_Value(Y)))) {
9474 AddAffected(X);
9475 AddAffected(Y);
9476 }
9477 }
9478 } else {
9479 if (match(B, m_ConstantInt())) {
9480 // Handle (A + C1) u< C2, which is the canonical form of
9481 // A > C3 && A < C4.
9483 AddAffected(X);
9484
9485 Value *Y;
9486 // X & Y u> C -> X >u C && Y >u C
9487 // X | Y u< C -> X u< C && Y u< C
9488 if (ICmpInst::isUnsigned(Pred) &&
9489 (match(A, m_And(m_Value(X), m_Value(Y))) ||
9490 match(A, m_Or(m_Value(X), m_Value(Y))))) {
9491 AddAffected(X);
9492 AddAffected(Y);
9493 }
9494 }
9495
9496 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
9497 // by computeKnownFPClass().
9499 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
9500 InsertAffected(X);
9501 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
9502 InsertAffected(X);
9503 }
9504 }
9505 } else if (match(Cond, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
9506 AddCmpOperands(A, B);
9507
9508 // fcmp fneg(x), y
9509 // fcmp fabs(x), y
9510 // fcmp fneg(fabs(x)), y
9511 if (match(A, m_FNeg(m_Value(A))))
9512 AddAffected(A);
9513 if (match(A, m_FAbs(m_Value(A))))
9514 AddAffected(A);
9515
9516 } else if (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
9517 m_Value()))) {
9518 // Handle patterns that computeKnownFPClass() support.
9519 AddAffected(A);
9520 }
9521 }
9522}
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
amdgpu 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...
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1291
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Hexagon Common GEP
static MaybeAlign getAlign(Value *Ptr)
Definition: IRBuilder.cpp:530
static const unsigned MaxDepth
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
This file contains the declarations for metadata subclasses.
Module.h This file contains the declarations for the Module class.
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static bool mayHaveSideEffects(MachineInstr &MI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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 SmallSet class.
This file defines the SmallVector 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:191
static SmallVector< VPValue *, 4 > getOperands(ArrayRef< VPValue * > Values, unsigned OperandIndex)
Definition: VPlanSLP.cpp:154
static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
static std::optional< bool > isImpliedCondICmps(const ICmpInst *LHS, CmpInst::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.
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 isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW)
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 isNonZeroShift(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, const KnownBits &KnownVal)
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 bool inputDenormalIsIEEE(const Function &F, const Type *Ty)
Return true if it's possible to assume IEEE treatment of input denormals in F for Val.
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, unsigned Depth, const SimplifyQuery &Q)
Return true if V1 == (binop V2, X), where X is known non-zero.
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 std::tuple< Value *, FPClassTest, FPClassTest > exactClass(Value *V, FPClassTest M)
Return the return value for fcmpImpliesClass for a compare that produces an exact class test.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpInst::Predicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
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 bool isKnownNonZero(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if the given value is known to be non-zero when defined.
static bool isNonEqualMul(const Value *V1, const Value *V2, unsigned Depth, const SimplifyQuery &Q)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
UndefPoisonKind
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 bool includesUndef(UndefPoisonKind Kind)
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, unsigned Depth, SimplifyQuery &Q)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, FastMathFlags FMF, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, 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 bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, 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 ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext)
static std::optional< bool > isImpliedCondCommonOperandWithConstants(CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred, const APInt &RC)
Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true.
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth, const SimplifyQuery &Q)
Return true if it is known that V1 != V2.
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 void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth, const SimplifyQuery &Q)
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 computeKnownBits(const Value *V, const APInt &DemandedElts, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
Determine which bits of V are known to be either zero or one and return them in the Known bit set.
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
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 bool isNonEqualSelect(const Value *V1, const Value *V2, unsigned Depth, const SimplifyQuery &Q)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, unsigned Depth, const SimplifyQuery &SQ, bool Invert)
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II)
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) ? (l) : ((v) > (h) ? (h) : (v)))
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 bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, unsigned Depth, const SimplifyQuery &Q)
static bool isNonEqualShl(const Value *V1, const Value *V2, unsigned Depth, const SimplifyQuery &Q)
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 isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, const SimplifyQuery &Q)
Test whether a GEP's result is known to be non-null.
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y)
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 void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q, 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 cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, unsigned Depth, const SimplifyQuery &Q)
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 Pred ALHS ARHS" is true.
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return the number of times the sign bit of the register is replicated into the other bits.
static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW)
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth, const SimplifyQuery &Q)
Return true if the given value is known to have exactly one bit set when defined.
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondMatchingOperands(CmpInst::Predicate LPred, CmpInst::Predicate RPred)
Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true.
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
bool isNegative() const
Definition: APFloat.h:1295
bool isFinite() const
Definition: APFloat.h:1300
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1006
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5183
bool isSmallestNormalized() const
Definition: APFloat.h:1315
Class for arbitrary precision integers.
Definition: APInt.h:76
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1543
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:212
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1385
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:401
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1491
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1370
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1364
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:184
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1308
unsigned ceilLogBase2() const
Definition: APInt.h:1706
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1179
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:349
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1160
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:358
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1636
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1439
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1089
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:187
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:194
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:307
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:1227
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1614
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1375
APInt reverseBits() const
Definition: APInt.cpp:737
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1144
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1578
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:197
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1010
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:334
unsigned logBase2() const
Definition: APInt.h:1703
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:805
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1297
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:449
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:383
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1128
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:851
APInt byteSwap() const
Definition: APInt.cpp:715
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1108
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:274
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:178
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1367
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1215
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:264
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:217
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:836
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:829
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1199
an instruction to allocate memory on the stack
Definition: Instructions.h:59
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
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:154
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
iterator begin() const
Definition: ArrayRef.h:153
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
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:195
Class to represent array types.
Definition: DerivedTypes.h:371
Type * getElementType() const
Definition: DerivedTypes.h:384
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.
std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
Definition: Attributes.cpp:417
unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
Definition: Attributes.cpp:411
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:193
bool isSingleEdge() const
Check if this is the only edge between Start and End.
Definition: Dominators.cpp:51
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
iterator end()
Definition: BasicBlock.h:443
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:430
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:166
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:360
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:452
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:482
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:206
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:165
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:221
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition: InstrTypes.h:486
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1467
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1715
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Definition: InstrTypes.h:2055
Value * getCalledOperand() const
Definition: InstrTypes.h:1708
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1660
Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1651
unsigned arg_size() const
Definition: InstrTypes.h:1658
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:574
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:956
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:966
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:969
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:983
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:995
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:996
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:972
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:981
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:970
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:971
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:990
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:989
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:993
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:980
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:974
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:977
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:991
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:978
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:973
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:975
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:994
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:982
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:992
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:979
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:968
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:976
bool isSigned() const
Definition: InstrTypes.h:1238
static 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:1140
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:1287
bool isFPPredicate() const
Definition: InstrTypes.h:1095
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:1102
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:1078
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
static bool isImpliedTrueByMatchingCmp(Predicate Pred1, Predicate Pred2)
Determine if Pred1 implies Pred2 is true when two compares have matching operands.
bool isIntPredicate() const
Definition: InstrTypes.h:1096
static bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
static bool isImpliedFalseByMatchingCmp(Predicate Pred1, Predicate Pred2)
Determine if Pred1 implies Pred2 is false when two compares have matching operands.
bool isUnsigned() const
Definition: InstrTypes.h:1244
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:692
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition: Constants.h:583
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition: Constants.h:658
uint64_t getElementAsInteger(unsigned i) const
If this is a sequential container of integers (of any size), return the specified element in the low ...
Definition: Constants.cpp:3005
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:766
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2140
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2098
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:268
This is the shared class of boolean and integer constants.
Definition: Constants.h:80
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:849
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:205
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:154
This class represents a range of values.
Definition: ConstantRange.h:47
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 ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
bool isAllNegative() const
Return true if all values in this range are negative.
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
KnownBits toKnownBits() const
Return known bits for values in this range.
ConstantRange difference(const ConstantRange &CR) const
Subtract the specified range from this range (aka relative complement of the sets).
bool isEmptySet() const
Return true if this set contains no members.
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static 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...
ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static 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...
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
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.
Definition: ConstantRange.h:84
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
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:41
Constant * getSplatValue(bool AllowUndefs=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1699
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:370
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:432
bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition: Constants.cpp:76
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:238
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:720
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
Definition: DataLayout.cpp:774
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:763
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:672
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
NodeT * getBlock() 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:162
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
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:20
bool noSignedZeros() const
Definition: FMF.h:68
void setNoSignedZeros(bool B=true)
Definition: FMF.h:85
bool noNaNs() const
Definition: FMF.h:66
const BasicBlock & getEntryBlock() const
Definition: Function.h:783
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition: Function.cpp:746
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:973
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
Type * getValueType() const
Definition: GlobalValue.h:296
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.
bool isEquality() const
Return true if this predicate is either EQ or NE.
This instruction inserts a struct field of array element value into an aggregate value.
Value * getAggregateOperand()
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr, BasicBlock::iterator InsertBefore)
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
bool isLifetimeStartOrEnd() const LLVM_READONLY
Return true if the instruction is a llvm.lifetime.start or llvm.lifetime.end marker.
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
Definition: Instruction.cpp:80
bool isBinaryOp() const
Definition: Instruction.h:257
const BasicBlock * getParent() const
Definition: Instruction.h:152
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:84
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:252
bool isUnaryOp() const
Definition: Instruction.h:256
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:47
Intrinsic::ID getIntrinsicID() const
Return the intrinsic ID of this intrinsic.
Definition: IntrinsicInst.h:54
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
An instruction for reading from memory.
Definition: Instructions.h:184
Value * getPointerOperand()
Definition: Instructions.h:280
bool isUnordered() const
Definition: Instructions.h:274
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:236
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:44
Metadata node.
Definition: Metadata.h:1067
const DataLayout & getDataLayout() const
Get the data layout for the module's target platform.
Definition: Module.h:293
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:31
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:41
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:75
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 PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1827
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed.
Definition: Operator.h:150
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition: Operator.h:169
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 void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:321
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:360
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:342
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:427
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:166
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:179
bool empty() const
Definition: SmallVector.h:94
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void reserve(size_type N)
Definition: SmallVector.h:676
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:696
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:622
TypeSize getElementOffset(unsigned Idx) const
Definition: DataLayout.h:651
Class to represent struct types.
Definition: DerivedTypes.h:216
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:341
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:342
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
unsigned getIntegerBitWidth() const
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:265
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:234
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:255
uint64_t getArrayNumElements() const
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:302
static IntegerType * getInt16Ty(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:262
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition: Type.h:243
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:228
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:348
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1808
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:72
unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition: Use.cpp:31
op_range operands()
Definition: User.h:242
Value * getOperand(unsigned i) const
Definition: User.h:169
unsigned getNumOperands() const
Definition: User.h:191
bool isDroppable() const
A droppable user is a user for which uses can be dropped without affecting correctness and should be ...
Definition: User.cpp:115
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition: Value.h:736
iterator_range< user_iterator > users()
Definition: Value.h:421
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition: WithCache.h:58
PointerType getValue() const
Definition: WithCache.h:56
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:187
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:168
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
self_iterator getIterator()
Definition: ilist_node.h:109
A range adaptor for a pair of iterators.
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.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:477
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > m_UnordFMin(const LHS &L, const RHS &R)
Match an 'unordered' floating point minimum function.
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
apfloat_match m_APFloatAllowUndef(const APFloat *&Res)
Match APFloat while allowing undefs in splat vector constants.
Definition: PatternMatch.h:317
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition: PatternMatch.h:613
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:568
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.
Definition: PatternMatch.h:160
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.
Definition: PatternMatch.h:601
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)
Definition: PatternMatch.h:49
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:713
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:821
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
OverflowingBinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWNeg(const ValTy &V)
Matches a 'Neg' as 'sub nsw 0, V'.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:163
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:541
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
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)
CmpClass_match< LHS, RHS, FCmpInst, FCmpInst::Predicate > m_FCmp(FCmpInst::Predicate &Pred, const LHS &L, const RHS &R)
CastOperator_match< OpTy, Instruction::Trunc > m_Trunc(const OpTy &Op)
Matches Trunc.
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
Definition: PatternMatch.h:771
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()...
Definition: PatternMatch.h:839
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:548
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
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.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:800
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.
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)
CmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate, true > m_c_ICmp(ICmpInst::Predicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
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".
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.
MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > m_UnordFMax(const LHS &L, const RHS &R)
Match an 'unordered' floating point maximum function.
VScaleVal_match m_VScale()
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)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:294
MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty > m_OrdFMax(const LHS &L, const RHS &R)
Match an 'ordered' floating point maximum function.
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.
Definition: PatternMatch.h:92
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)
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.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:311
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty > m_OrdFMin(const LHS &L, const RHS &R)
Match an 'ordered' floating point minimum function.
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
Definition: PatternMatch.h:184
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
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.
Definition: PatternMatch.h:561
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".
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:234
static unsigned decodeVSEW(unsigned VSEW)
unsigned getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:450
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
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...
Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
@ Offset
Definition: DWP.cpp:456
@ Length
Definition: DWP.cpp:456
OverflowResult
@ 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.
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:1722
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:1680
bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &DL, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
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...
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false)
Return true if the two given values are negation.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
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.
const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
void getGuaranteedNonPoisonOps(const Instruction *I, SmallVectorImpl< const Value * > &Ops)
Insert operands of I into Ops such that I will trigger undefined behavior if I is executed and that o...
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
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.
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:201
bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
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.
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
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:317
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:2073
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value,...
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined.
bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:269
gep_type_iterator gep_type_end(const User *GEP)
CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
void computeKnownBitsFromContext(const Value *V, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
Merge bits known from context-dependent facts into Known.
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:319
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.
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
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.
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:215
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...
bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, const Instruction *CtxI, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
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,...
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:1729
KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, unsigned Depth, const SimplifyQuery &SQ)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
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:313
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
Definition: GuardUtils.cpp:18
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition: bit.h:281
SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
Definition: ValueTracking.h:47
void getGuaranteedWellDefinedOps(const Instruction *I, SmallVectorImpl< const Value * > &Ops)
Insert operands of I into Ops such that I will trigger undefined behavior if I is executed and that o...
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, Value *RHS, bool LookThroughSrc=true)
Compute the possible floating-point classes that LHS could be based on fcmp \Pred LHS,...
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.
bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
bool programUndefinedIfPoison(const Instruction *Inst)
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...
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:2032
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...
FPClassTest inverse_fabs(FPClassTest Mask)
Return the test mask which returns true after fabs is applied to the value.
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'.
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
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...
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
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
bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, LoopInfo *LI=nullptr, unsigned MaxLookup=6)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
bool isKnownNegative(const Value *V, const SimplifyQuery &DL, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given values are known to be non-equal when defined.
@ Add
Sum of integers.
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.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
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.
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Return true if the instruction does not have any effects besides calculating the result and does not ...
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, 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...
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
gep_type_iterator gep_type_begin(const User *GEP)
std::pair< Value *, FPClassTest > fcmpToClassTest(CmpInst::Predicate Pred, const Function &F, Value *LHS, Value *RHS, bool LookThroughSrc=true)
Returns a pair of values, which if passed to llvm.is.fpclass, returns the same result as an fcmp with...
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...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1879
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits.
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:208
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...
bool isGEPBasedOnPointerToString(const GEPOperator *GEP, unsigned CharSize=8)
Returns true if the GEP is based on a pointer to a string (array of.
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.
KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, unsigned Depth, const SimplifyQuery &SQ)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
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...
bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
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...
bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Get the upper bound on bit size for this Value Op as a signed integer.
bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
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.
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:860
static unsigned int semanticsPrecision(const fltSemantics &)
Definition: APFloat.cpp:292
static bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition: APFloat.cpp:317
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
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.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ Dynamic
Denormals have unknown treatment.
@ IEEE
IEEE-754 denormal numbers preserved.
static constexpr DenormalMode getPositiveZero()
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...
Definition: SimplifyQuery.h:24
bool isExact(const BinaryOperator *Op) const
Definition: SimplifyQuery.h:47
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
Definition: SimplifyQuery.h:29
bool hasNoSignedZeros(const InstT *Op) const
Definition: SimplifyQuery.h:53
bool hasNoSignedWrap(const InstT *Op) const
Definition: SimplifyQuery.h:41
bool hasNoUnsignedWrap(const InstT *Op) const
Definition: SimplifyQuery.h:35
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:297
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
Definition: KnownBits.cpp:752
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition: KnownBits.h:182
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:251
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:208
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:104
KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
Definition: KnownBits.cpp:1089
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:120
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:761
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition: KnownBits.h:247
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:238
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:422
static KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
Definition: KnownBits.cpp:755
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1018
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:63
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:270
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...
Definition: KnownBits.cpp:1100
void makeNegative()
Make this value negative.
Definition: KnownBits.h:115
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:157
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:47
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:285
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:89
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:40
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:184
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:50
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:71
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:317
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:364
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:107
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:307
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:176
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition: KnownBits.h:241
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:192
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:244
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:141
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:221
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1037
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:976
static 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:57
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:917
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:328
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:101
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:276
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition: KnownBits.h:95
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:215
static KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
Definition: KnownBits.cpp:758
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:765
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:163
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:544
static std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
Definition: KnownBits.cpp:506
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
Definition: KnownBits.cpp:279
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:202
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition: KnownBits.h:202
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition: KnownBits.h:57
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)
bool isKnownNeverZero() const
Return true if it's known this can never be a zero.
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
bool isKnownNeverLogicalNegZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a negative zero.
bool isKnownNeverLogicalPosZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a positive zero.
void propagateCanonicalizingSrc(const KnownFPClass &Src, const Function &F, Type *Ty)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void propagateDenormal(const KnownFPClass &Src, const Function &F, Type *Ty)
Propagate knowledge from a source value that could be a denormal or 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.
bool isKnownNeverLogicalZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a 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.
void signBitMustBeZero()
Assume the sign bit is zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
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
Definition: SimplifyQuery.h:61
const Instruction * CxtI
Definition: SimplifyQuery.h:65
const DominatorTree * DT
Definition: SimplifyQuery.h:63
SimplifyQuery getWithInstruction(const Instruction *I) const
Definition: SimplifyQuery.h:96
AssumptionCache * AC
Definition: SimplifyQuery.h:64
const DomConditionCache * DC
Definition: SimplifyQuery.h:66
const InstrInfoQuery IIQ
Definition: SimplifyQuery.h:71