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::Trunc:
2636 // nuw/nsw trunc preserves zero/non-zero status of input.
2637 if (auto *TI = dyn_cast<TruncInst>(I))
2638 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
2639 return isKnownNonZero(TI->getOperand(0), Q, Depth);
2640 break;
2641
2642 case Instruction::Sub:
2643 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2644 I->getOperand(1));
2645 case Instruction::Or:
2646 // X | Y != 0 if X != 0 or Y != 0.
2647 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
2648 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2649 case Instruction::SExt:
2650 case Instruction::ZExt:
2651 // ext X != 0 if X != 0.
2652 return isKnownNonZero(I->getOperand(0), Q, Depth);
2653
2654 case Instruction::Shl: {
2655 // shl nsw/nuw can't remove any non-zero bits.
2656 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
2657 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
2658 return isKnownNonZero(I->getOperand(0), Q, Depth);
2659
2660 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
2661 // if the lowest bit is shifted off the end.
2662 KnownBits Known(BitWidth);
2663 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth, Q);
2664 if (Known.One[0])
2665 return true;
2666
2667 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
2668 }
2669 case Instruction::LShr:
2670 case Instruction::AShr: {
2671 // shr exact can only shift out zero bits.
2672 const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(I);
2673 if (BO->isExact())
2674 return isKnownNonZero(I->getOperand(0), Q, Depth);
2675
2676 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
2677 // defined if the sign bit is shifted off the end.
2678 KnownBits Known =
2679 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
2680 if (Known.isNegative())
2681 return true;
2682
2683 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
2684 }
2685 case Instruction::UDiv:
2686 case Instruction::SDiv: {
2687 // X / Y
2688 // div exact can only produce a zero if the dividend is zero.
2689 if (cast<PossiblyExactOperator>(I)->isExact())
2690 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2691
2692 KnownBits XKnown =
2693 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
2694 // If X is fully unknown we won't be able to figure anything out so don't
2695 // both computing knownbits for Y.
2696 if (XKnown.isUnknown())
2697 return false;
2698
2699 KnownBits YKnown =
2700 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
2701 if (I->getOpcode() == Instruction::SDiv) {
2702 // For signed division need to compare abs value of the operands.
2703 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
2704 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
2705 }
2706 // If X u>= Y then div is non zero (0/0 is UB).
2707 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
2708 // If X is total unknown or X u< Y we won't be able to prove non-zero
2709 // with compute known bits so just return early.
2710 return XUgeY && *XUgeY;
2711 }
2712 case Instruction::Add: {
2713 // X + Y.
2714
2715 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
2716 // non-zero.
2717 auto *BO = cast<OverflowingBinaryOperator>(I);
2718 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2719 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
2720 Q.IIQ.hasNoUnsignedWrap(BO));
2721 }
2722 case Instruction::Mul: {
2723 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
2724 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2725 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
2726 Q.IIQ.hasNoUnsignedWrap(BO));
2727 }
2728 case Instruction::Select: {
2729 // (C ? X : Y) != 0 if X != 0 and Y != 0.
2730
2731 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
2732 // then see if the select condition implies the arm is non-zero. For example
2733 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
2734 // dominated by `X != 0`.
2735 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
2736 Value *Op;
2737 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
2738 // Op is trivially non-zero.
2739 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
2740 return true;
2741
2742 // The condition of the select dominates the true/false arm. Check if the
2743 // condition implies that a given arm is non-zero.
2744 Value *X;
2745 CmpInst::Predicate Pred;
2746 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
2747 return false;
2748
2749 if (!IsTrueArm)
2750 Pred = ICmpInst::getInversePredicate(Pred);
2751
2752 return cmpExcludesZero(Pred, X);
2753 };
2754
2755 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
2756 SelectArmIsNonZero(/* IsTrueArm */ false))
2757 return true;
2758 break;
2759 }
2760 case Instruction::PHI: {
2761 auto *PN = cast<PHINode>(I);
2763 return true;
2764
2765 // Check if all incoming values are non-zero using recursion.
2766 SimplifyQuery RecQ = Q;
2767 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2768 return llvm::all_of(PN->operands(), [&](const Use &U) {
2769 if (U.get() == PN)
2770 return true;
2771 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2772 // Check if the branch on the phi excludes zero.
2773 ICmpInst::Predicate Pred;
2774 Value *X;
2775 BasicBlock *TrueSucc, *FalseSucc;
2776 if (match(RecQ.CxtI,
2777 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
2778 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
2779 // Check for cases of duplicate successors.
2780 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
2781 // If we're using the false successor, invert the predicate.
2782 if (FalseSucc == PN->getParent())
2783 Pred = CmpInst::getInversePredicate(Pred);
2784 if (cmpExcludesZero(Pred, X))
2785 return true;
2786 }
2787 }
2788 // Finally recurse on the edge and check it directly.
2789 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
2790 });
2791 }
2792 case Instruction::InsertElement: {
2793 if (isa<ScalableVectorType>(I->getType()))
2794 break;
2795
2796 const Value *Vec = I->getOperand(0);
2797 const Value *Elt = I->getOperand(1);
2798 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2799
2800 unsigned NumElts = DemandedElts.getBitWidth();
2801 APInt DemandedVecElts = DemandedElts;
2802 bool SkipElt = false;
2803 // If we know the index we are inserting too, clear it from Vec check.
2804 if (CIdx && CIdx->getValue().ult(NumElts)) {
2805 DemandedVecElts.clearBit(CIdx->getZExtValue());
2806 SkipElt = !DemandedElts[CIdx->getZExtValue()];
2807 }
2808
2809 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
2810 // are non-zero.
2811 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
2812 (DemandedVecElts.isZero() ||
2813 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
2814 }
2815 case Instruction::ExtractElement:
2816 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
2817 const Value *Vec = EEI->getVectorOperand();
2818 const Value *Idx = EEI->getIndexOperand();
2819 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2820 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
2821 unsigned NumElts = VecTy->getNumElements();
2822 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2823 if (CIdx && CIdx->getValue().ult(NumElts))
2824 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2825 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
2826 }
2827 }
2828 break;
2829 case Instruction::ShuffleVector: {
2830 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2831 if (!Shuf)
2832 break;
2833 APInt DemandedLHS, DemandedRHS;
2834 // For undef elements, we don't know anything about the common state of
2835 // the shuffle result.
2836 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
2837 break;
2838 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
2839 return (DemandedRHS.isZero() ||
2840 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
2841 (DemandedLHS.isZero() ||
2842 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
2843 }
2844 case Instruction::Freeze:
2845 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
2846 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2847 Depth);
2848 case Instruction::Load: {
2849 auto *LI = cast<LoadInst>(I);
2850 // A Load tagged with nonnull or dereferenceable with null pointer undefined
2851 // is never null.
2852 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
2853 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
2854 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
2855 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
2856 return true;
2857 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
2859 }
2860
2861 // No need to fall through to computeKnownBits as range metadata is already
2862 // handled in isKnownNonZero.
2863 return false;
2864 }
2865 case Instruction::ExtractValue: {
2866 const WithOverflowInst *WO;
2867 if (match(I, m_ExtractValue<0>(m_WithOverflowInst(WO)))) {
2868 switch (WO->getBinaryOp()) {
2869 default:
2870 break;
2871 case Instruction::Add:
2872 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
2873 WO->getArgOperand(0), WO->getArgOperand(1),
2874 /*NSW=*/false,
2875 /*NUW=*/false);
2876 case Instruction::Sub:
2877 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
2878 WO->getArgOperand(0), WO->getArgOperand(1));
2879 case Instruction::Mul:
2880 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth,
2881 WO->getArgOperand(0), WO->getArgOperand(1),
2882 /*NSW=*/false, /*NUW=*/false);
2883 break;
2884 }
2885 }
2886 break;
2887 }
2888 case Instruction::Call:
2889 case Instruction::Invoke: {
2890 const auto *Call = cast<CallBase>(I);
2891 if (I->getType()->isPointerTy()) {
2892 if (Call->isReturnNonNull())
2893 return true;
2894 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
2895 return isKnownNonZero(RP, Q, Depth);
2896 } else {
2897 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
2899 if (std::optional<ConstantRange> Range = Call->getRange()) {
2900 const APInt ZeroValue(Range->getBitWidth(), 0);
2901 if (!Range->contains(ZeroValue))
2902 return true;
2903 }
2904 if (const Value *RV = Call->getReturnedArgOperand())
2905 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
2906 return true;
2907 }
2908
2909 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2910 switch (II->getIntrinsicID()) {
2911 case Intrinsic::sshl_sat:
2912 case Intrinsic::ushl_sat:
2913 case Intrinsic::abs:
2914 case Intrinsic::bitreverse:
2915 case Intrinsic::bswap:
2916 case Intrinsic::ctpop:
2917 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
2918 // NB: We don't do usub_sat here as in any case we can prove its
2919 // non-zero, we will fold it to `sub nuw` in InstCombine.
2920 case Intrinsic::ssub_sat:
2921 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
2922 II->getArgOperand(0), II->getArgOperand(1));
2923 case Intrinsic::sadd_sat:
2924 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
2925 II->getArgOperand(0), II->getArgOperand(1),
2926 /*NSW=*/true, /* NUW=*/false);
2927 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
2928 case Intrinsic::vector_reduce_or:
2929 case Intrinsic::vector_reduce_umax:
2930 case Intrinsic::vector_reduce_umin:
2931 case Intrinsic::vector_reduce_smax:
2932 case Intrinsic::vector_reduce_smin:
2933 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
2934 case Intrinsic::umax:
2935 case Intrinsic::uadd_sat:
2936 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
2937 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
2938 case Intrinsic::smax: {
2939 // If either arg is strictly positive the result is non-zero. Otherwise
2940 // the result is non-zero if both ops are non-zero.
2941 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
2942 const KnownBits &OpKnown) {
2943 if (!OpNonZero.has_value())
2944 OpNonZero = OpKnown.isNonZero() ||
2945 isKnownNonZero(Op, DemandedElts, Q, Depth);
2946 return *OpNonZero;
2947 };
2948 // Avoid re-computing isKnownNonZero.
2949 std::optional<bool> Op0NonZero, Op1NonZero;
2950 KnownBits Op1Known =
2951 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
2952 if (Op1Known.isNonNegative() &&
2953 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
2954 return true;
2955 KnownBits Op0Known =
2956 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
2957 if (Op0Known.isNonNegative() &&
2958 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
2959 return true;
2960 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
2961 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
2962 }
2963 case Intrinsic::smin: {
2964 // If either arg is negative the result is non-zero. Otherwise
2965 // the result is non-zero if both ops are non-zero.
2966 KnownBits Op1Known =
2967 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
2968 if (Op1Known.isNegative())
2969 return true;
2970 KnownBits Op0Known =
2971 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
2972 if (Op0Known.isNegative())
2973 return true;
2974
2975 if (Op1Known.isNonZero() && Op0Known.isNonZero())
2976 return true;
2977 }
2978 [[fallthrough]];
2979 case Intrinsic::umin:
2980 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
2981 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
2982 case Intrinsic::cttz:
2983 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
2984 .Zero[0];
2985 case Intrinsic::ctlz:
2986 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
2987 .isNonNegative();
2988 case Intrinsic::fshr:
2989 case Intrinsic::fshl:
2990 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
2991 if (II->getArgOperand(0) == II->getArgOperand(1))
2992 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
2993 break;
2994 case Intrinsic::vscale:
2995 return true;
2996 case Intrinsic::experimental_get_vector_length:
2997 return isKnownNonZero(I->getOperand(0), Q, Depth);
2998 default:
2999 break;
3000 }
3001 break;
3002 }
3003
3004 return false;
3005 }
3006 }
3007
3008 KnownBits Known(BitWidth);
3009 computeKnownBits(I, DemandedElts, Known, Depth, Q);
3010 return Known.One != 0;
3011}
3012
3013/// Return true if the given value is known to be non-zero when defined. For
3014/// vectors, return true if every demanded element is known to be non-zero when
3015/// defined. For pointers, if the context instruction and dominator tree are
3016/// specified, perform context-sensitive analysis and return true if the
3017/// pointer couldn't possibly be null at the specified instruction.
3018/// Supports values with integer or pointer type and vectors of integers.
3019bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3020 const SimplifyQuery &Q, unsigned Depth) {
3021 Type *Ty = V->getType();
3022
3023#ifndef NDEBUG
3024 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3025
3026 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3027 assert(
3028 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3029 "DemandedElt width should equal the fixed vector number of elements");
3030 } else {
3031 assert(DemandedElts == APInt(1, 1) &&
3032 "DemandedElt width should be 1 for scalars");
3033 }
3034#endif
3035
3036 if (auto *C = dyn_cast<Constant>(V)) {
3037 if (C->isNullValue())
3038 return false;
3039 if (isa<ConstantInt>(C))
3040 // Must be non-zero due to null test above.
3041 return true;
3042
3043 // For constant vectors, check that all elements are poison or known
3044 // non-zero to determine that the whole vector is known non-zero.
3045 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3046 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3047 if (!DemandedElts[i])
3048 continue;
3049 Constant *Elt = C->getAggregateElement(i);
3050 if (!Elt || Elt->isNullValue())
3051 return false;
3052 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3053 return false;
3054 }
3055 return true;
3056 }
3057
3058 // A global variable in address space 0 is non null unless extern weak
3059 // or an absolute symbol reference. Other address spaces may have null as a
3060 // valid address for a global, so we can't assume anything.
3061 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3062 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3063 GV->getType()->getAddressSpace() == 0)
3064 return true;
3065 }
3066
3067 // For constant expressions, fall through to the Operator code below.
3068 if (!isa<ConstantExpr>(V))
3069 return false;
3070 }
3071
3072 if (const auto *A = dyn_cast<Argument>(V))
3073 if (std::optional<ConstantRange> Range = A->getRange()) {
3074 const APInt ZeroValue(Range->getBitWidth(), 0);
3075 if (!Range->contains(ZeroValue))
3076 return true;
3077 }
3078
3079 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3080 return true;
3081
3082 // Some of the tests below are recursive, so bail out if we hit the limit.
3084 return false;
3085
3086 // Check for pointer simplifications.
3087
3088 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3089 // A byval, inalloca may not be null in a non-default addres space. A
3090 // nonnull argument is assumed never 0.
3091 if (const Argument *A = dyn_cast<Argument>(V)) {
3092 if (((A->hasPassPointeeByValueCopyAttr() &&
3093 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3094 A->hasNonNullAttr()))
3095 return true;
3096 }
3097 }
3098
3099 if (const auto *I = dyn_cast<Operator>(V))
3100 if (isKnownNonZeroFromOperator(I, DemandedElts, Depth, Q))
3101 return true;
3102
3103 if (!isa<Constant>(V) &&
3105 return true;
3106
3107 return false;
3108}
3109
3111 unsigned Depth) {
3112 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3113 APInt DemandedElts =
3114 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3115 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3116}
3117
3118/// If the pair of operators are the same invertible function, return the
3119/// the operands of the function corresponding to each input. Otherwise,
3120/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3121/// every input value to exactly one output value. This is equivalent to
3122/// saying that Op1 and Op2 are equal exactly when the specified pair of
3123/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3124static std::optional<std::pair<Value*, Value*>>
3126 const Operator *Op2) {
3127 if (Op1->getOpcode() != Op2->getOpcode())
3128 return std::nullopt;
3129
3130 auto getOperands = [&](unsigned OpNum) -> auto {
3131 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3132 };
3133
3134 switch (Op1->getOpcode()) {
3135 default:
3136 break;
3137 case Instruction::Or:
3138 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3139 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3140 break;
3141 [[fallthrough]];
3142 case Instruction::Xor:
3143 case Instruction::Add: {
3144 Value *Other;
3145 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3146 return std::make_pair(Op1->getOperand(1), Other);
3147 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3148 return std::make_pair(Op1->getOperand(0), Other);
3149 break;
3150 }
3151 case Instruction::Sub:
3152 if (Op1->getOperand(0) == Op2->getOperand(0))
3153 return getOperands(1);
3154 if (Op1->getOperand(1) == Op2->getOperand(1))
3155 return getOperands(0);
3156 break;
3157 case Instruction::Mul: {
3158 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3159 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3160 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3161 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3162 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3163 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3164 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3165 break;
3166
3167 // Assume operand order has been canonicalized
3168 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3169 isa<ConstantInt>(Op1->getOperand(1)) &&
3170 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3171 return getOperands(0);
3172 break;
3173 }
3174 case Instruction::Shl: {
3175 // Same as multiplies, with the difference that we don't need to check
3176 // for a non-zero multiply. Shifts always multiply by non-zero.
3177 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3178 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3179 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3180 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3181 break;
3182
3183 if (Op1->getOperand(1) == Op2->getOperand(1))
3184 return getOperands(0);
3185 break;
3186 }
3187 case Instruction::AShr:
3188 case Instruction::LShr: {
3189 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3190 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3191 if (!PEO1->isExact() || !PEO2->isExact())
3192 break;
3193
3194 if (Op1->getOperand(1) == Op2->getOperand(1))
3195 return getOperands(0);
3196 break;
3197 }
3198 case Instruction::SExt:
3199 case Instruction::ZExt:
3200 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3201 return getOperands(0);
3202 break;
3203 case Instruction::PHI: {
3204 const PHINode *PN1 = cast<PHINode>(Op1);
3205 const PHINode *PN2 = cast<PHINode>(Op2);
3206
3207 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3208 // are a single invertible function of the start values? Note that repeated
3209 // application of an invertible function is also invertible
3210 BinaryOperator *BO1 = nullptr;
3211 Value *Start1 = nullptr, *Step1 = nullptr;
3212 BinaryOperator *BO2 = nullptr;
3213 Value *Start2 = nullptr, *Step2 = nullptr;
3214 if (PN1->getParent() != PN2->getParent() ||
3215 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3216 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3217 break;
3218
3219 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3220 cast<Operator>(BO2));
3221 if (!Values)
3222 break;
3223
3224 // We have to be careful of mutually defined recurrences here. Ex:
3225 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3226 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3227 // The invertibility of these is complicated, and not worth reasoning
3228 // about (yet?).
3229 if (Values->first != PN1 || Values->second != PN2)
3230 break;
3231
3232 return std::make_pair(Start1, Start2);
3233 }
3234 }
3235 return std::nullopt;
3236}
3237
3238/// Return true if V1 == (binop V2, X), where X is known non-zero.
3239/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3240/// implies V2 != V1.
3241static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3242 unsigned Depth, const SimplifyQuery &Q) {
3243 const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
3244 if (!BO)
3245 return false;
3246 switch (BO->getOpcode()) {
3247 default:
3248 break;
3249 case Instruction::Or:
3250 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3251 break;
3252 [[fallthrough]];
3253 case Instruction::Xor:
3254 case Instruction::Add:
3255 Value *Op = nullptr;
3256 if (V2 == BO->getOperand(0))
3257 Op = BO->getOperand(1);
3258 else if (V2 == BO->getOperand(1))
3259 Op = BO->getOperand(0);
3260 else
3261 return false;
3262 return isKnownNonZero(Op, Q, Depth + 1);
3263 }
3264 return false;
3265}
3266
3267/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3268/// the multiplication is nuw or nsw.
3269static bool isNonEqualMul(const Value *V1, const Value *V2, unsigned Depth,
3270 const SimplifyQuery &Q) {
3271 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3272 const APInt *C;
3273 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3274 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3275 !C->isZero() && !C->isOne() && isKnownNonZero(V1, Q, Depth + 1);
3276 }
3277 return false;
3278}
3279
3280/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3281/// the shift is nuw or nsw.
3282static bool isNonEqualShl(const Value *V1, const Value *V2, unsigned Depth,
3283 const SimplifyQuery &Q) {
3284 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3285 const APInt *C;
3286 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3287 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3288 !C->isZero() && isKnownNonZero(V1, Q, Depth + 1);
3289 }
3290 return false;
3291}
3292
3293static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3294 unsigned Depth, const SimplifyQuery &Q) {
3295 // Check two PHIs are in same block.
3296 if (PN1->getParent() != PN2->getParent())
3297 return false;
3298
3300 bool UsedFullRecursion = false;
3301 for (const BasicBlock *IncomBB : PN1->blocks()) {
3302 if (!VisitedBBs.insert(IncomBB).second)
3303 continue; // Don't reprocess blocks that we have dealt with already.
3304 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3305 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3306 const APInt *C1, *C2;
3307 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3308 continue;
3309
3310 // Only one pair of phi operands is allowed for full recursion.
3311 if (UsedFullRecursion)
3312 return false;
3313
3314 SimplifyQuery RecQ = Q;
3315 RecQ.CxtI = IncomBB->getTerminator();
3316 if (!isKnownNonEqual(IV1, IV2, Depth + 1, RecQ))
3317 return false;
3318 UsedFullRecursion = true;
3319 }
3320 return true;
3321}
3322
3323static bool isNonEqualSelect(const Value *V1, const Value *V2, unsigned Depth,
3324 const SimplifyQuery &Q) {
3325 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3326 if (!SI1)
3327 return false;
3328
3329 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3330 const Value *Cond1 = SI1->getCondition();
3331 const Value *Cond2 = SI2->getCondition();
3332 if (Cond1 == Cond2)
3333 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3334 Depth + 1, Q) &&
3335 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3336 Depth + 1, Q);
3337 }
3338 return isKnownNonEqual(SI1->getTrueValue(), V2, Depth + 1, Q) &&
3339 isKnownNonEqual(SI1->getFalseValue(), V2, Depth + 1, Q);
3340}
3341
3342// Check to see if A is both a GEP and is the incoming value for a PHI in the
3343// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3344// one of them being the recursive GEP A and the other a ptr at same base and at
3345// the same/higher offset than B we are only incrementing the pointer further in
3346// loop if offset of recursive GEP is greater than 0.
3348 const SimplifyQuery &Q) {
3349 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3350 return false;
3351
3352 auto *GEPA = dyn_cast<GEPOperator>(A);
3353 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3354 return false;
3355
3356 // Handle 2 incoming PHI values with one being a recursive GEP.
3357 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3358 if (!PN || PN->getNumIncomingValues() != 2)
3359 return false;
3360
3361 // Search for the recursive GEP as an incoming operand, and record that as
3362 // Step.
3363 Value *Start = nullptr;
3364 Value *Step = const_cast<Value *>(A);
3365 if (PN->getIncomingValue(0) == Step)
3366 Start = PN->getIncomingValue(1);
3367 else if (PN->getIncomingValue(1) == Step)
3368 Start = PN->getIncomingValue(0);
3369 else
3370 return false;
3371
3372 // Other incoming node base should match the B base.
3373 // StartOffset >= OffsetB && StepOffset > 0?
3374 // StartOffset <= OffsetB && StepOffset < 0?
3375 // Is non-equal if above are true.
3376 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3377 // optimisation to inbounds GEPs only.
3378 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3379 APInt StartOffset(IndexWidth, 0);
3380 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3381 APInt StepOffset(IndexWidth, 0);
3382 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3383
3384 // Check if Base Pointer of Step matches the PHI.
3385 if (Step != PN)
3386 return false;
3387 APInt OffsetB(IndexWidth, 0);
3388 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3389 return Start == B &&
3390 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
3391 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
3392}
3393
3394/// Return true if it is known that V1 != V2.
3395static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
3396 const SimplifyQuery &Q) {
3397 if (V1 == V2)
3398 return false;
3399 if (V1->getType() != V2->getType())
3400 // We can't look through casts yet.
3401 return false;
3402
3404 return false;
3405
3406 // See if we can recurse through (exactly one of) our operands. This
3407 // requires our operation be 1-to-1 and map every input value to exactly
3408 // one output value. Such an operation is invertible.
3409 auto *O1 = dyn_cast<Operator>(V1);
3410 auto *O2 = dyn_cast<Operator>(V2);
3411 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3412 if (auto Values = getInvertibleOperands(O1, O2))
3413 return isKnownNonEqual(Values->first, Values->second, Depth + 1, Q);
3414
3415 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3416 const PHINode *PN2 = cast<PHINode>(V2);
3417 // FIXME: This is missing a generalization to handle the case where one is
3418 // a PHI and another one isn't.
3419 if (isNonEqualPHIs(PN1, PN2, Depth, Q))
3420 return true;
3421 };
3422 }
3423
3424 if (isModifyingBinopOfNonZero(V1, V2, Depth, Q) ||
3425 isModifyingBinopOfNonZero(V2, V1, Depth, Q))
3426 return true;
3427
3428 if (isNonEqualMul(V1, V2, Depth, Q) || isNonEqualMul(V2, V1, Depth, Q))
3429 return true;
3430
3431 if (isNonEqualShl(V1, V2, Depth, Q) || isNonEqualShl(V2, V1, Depth, Q))
3432 return true;
3433
3434 if (V1->getType()->isIntOrIntVectorTy()) {
3435 // Are any known bits in V1 contradictory to known bits in V2? If V1
3436 // has a known zero where V2 has a known one, they must not be equal.
3437 KnownBits Known1 = computeKnownBits(V1, Depth, Q);
3438 if (!Known1.isUnknown()) {
3439 KnownBits Known2 = computeKnownBits(V2, Depth, Q);
3440 if (Known1.Zero.intersects(Known2.One) ||
3441 Known2.Zero.intersects(Known1.One))
3442 return true;
3443 }
3444 }
3445
3446 if (isNonEqualSelect(V1, V2, Depth, Q) || isNonEqualSelect(V2, V1, Depth, Q))
3447 return true;
3448
3449 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3451 return true;
3452
3453 Value *A, *B;
3454 // PtrToInts are NonEqual if their Ptrs are NonEqual.
3455 // Check PtrToInt type matches the pointer size.
3456 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
3458 return isKnownNonEqual(A, B, Depth + 1, Q);
3459
3460 return false;
3461}
3462
3463// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
3464// Returns the input and lower/upper bounds.
3465static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
3466 const APInt *&CLow, const APInt *&CHigh) {
3467 assert(isa<Operator>(Select) &&
3468 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
3469 "Input should be a Select!");
3470
3471 const Value *LHS = nullptr, *RHS = nullptr;
3473 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
3474 return false;
3475
3476 if (!match(RHS, m_APInt(CLow)))
3477 return false;
3478
3479 const Value *LHS2 = nullptr, *RHS2 = nullptr;
3481 if (getInverseMinMaxFlavor(SPF) != SPF2)
3482 return false;
3483
3484 if (!match(RHS2, m_APInt(CHigh)))
3485 return false;
3486
3487 if (SPF == SPF_SMIN)
3488 std::swap(CLow, CHigh);
3489
3490 In = LHS2;
3491 return CLow->sle(*CHigh);
3492}
3493
3495 const APInt *&CLow,
3496 const APInt *&CHigh) {
3497 assert((II->getIntrinsicID() == Intrinsic::smin ||
3498 II->getIntrinsicID() == Intrinsic::smax) && "Must be smin/smax");
3499
3501 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
3502 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
3503 !match(II->getArgOperand(1), m_APInt(CLow)) ||
3504 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
3505 return false;
3506
3507 if (II->getIntrinsicID() == Intrinsic::smin)
3508 std::swap(CLow, CHigh);
3509 return CLow->sle(*CHigh);
3510}
3511
3512/// For vector constants, loop over the elements and find the constant with the
3513/// minimum number of sign bits. Return 0 if the value is not a vector constant
3514/// or if any element was not analyzed; otherwise, return the count for the
3515/// element with the minimum number of sign bits.
3517 const APInt &DemandedElts,
3518 unsigned TyBits) {
3519 const auto *CV = dyn_cast<Constant>(V);
3520 if (!CV || !isa<FixedVectorType>(CV->getType()))
3521 return 0;
3522
3523 unsigned MinSignBits = TyBits;
3524 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3525 for (unsigned i = 0; i != NumElts; ++i) {
3526 if (!DemandedElts[i])
3527 continue;
3528 // If we find a non-ConstantInt, bail out.
3529 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3530 if (!Elt)
3531 return 0;
3532
3533 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3534 }
3535
3536 return MinSignBits;
3537}
3538
3539static unsigned ComputeNumSignBitsImpl(const Value *V,
3540 const APInt &DemandedElts,
3541 unsigned Depth, const SimplifyQuery &Q);
3542
3543static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
3544 unsigned Depth, const SimplifyQuery &Q) {
3545 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q);
3546 assert(Result > 0 && "At least one sign bit needs to be present!");
3547 return Result;
3548}
3549
3550/// Return the number of times the sign bit of the register is replicated into
3551/// the other bits. We know that at least 1 bit is always equal to the sign bit
3552/// (itself), but other cases can give us information. For example, immediately
3553/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
3554/// other, so we return 3. For vectors, return the number of sign bits for the
3555/// vector element with the minimum number of known sign bits of the demanded
3556/// elements in the vector specified by DemandedElts.
3557static unsigned ComputeNumSignBitsImpl(const Value *V,
3558 const APInt &DemandedElts,
3559 unsigned Depth, const SimplifyQuery &Q) {
3560 Type *Ty = V->getType();
3561#ifndef NDEBUG
3562 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3563
3564 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3565 assert(
3566 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3567 "DemandedElt width should equal the fixed vector number of elements");
3568 } else {
3569 assert(DemandedElts == APInt(1, 1) &&
3570 "DemandedElt width should be 1 for scalars");
3571 }
3572#endif
3573
3574 // We return the minimum number of sign bits that are guaranteed to be present
3575 // in V, so for undef we have to conservatively return 1. We don't have the
3576 // same behavior for poison though -- that's a FIXME today.
3577
3578 Type *ScalarTy = Ty->getScalarType();
3579 unsigned TyBits = ScalarTy->isPointerTy() ?
3580 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
3581 Q.DL.getTypeSizeInBits(ScalarTy);
3582
3583 unsigned Tmp, Tmp2;
3584 unsigned FirstAnswer = 1;
3585
3586 // Note that ConstantInt is handled by the general computeKnownBits case
3587 // below.
3588
3590 return 1;
3591
3592 if (auto *U = dyn_cast<Operator>(V)) {
3593 switch (Operator::getOpcode(V)) {
3594 default: break;
3595 case Instruction::SExt:
3596 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
3597 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp;
3598
3599 case Instruction::SDiv: {
3600 const APInt *Denominator;
3601 // sdiv X, C -> adds log(C) sign bits.
3602 if (match(U->getOperand(1), m_APInt(Denominator))) {
3603
3604 // Ignore non-positive denominator.
3605 if (!Denominator->isStrictlyPositive())
3606 break;
3607
3608 // Calculate the incoming numerator bits.
3609 unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3610
3611 // Add floor(log(C)) bits to the numerator bits.
3612 return std::min(TyBits, NumBits + Denominator->logBase2());
3613 }
3614 break;
3615 }
3616
3617 case Instruction::SRem: {
3618 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3619
3620 const APInt *Denominator;
3621 // srem X, C -> we know that the result is within [-C+1,C) when C is a
3622 // positive constant. This let us put a lower bound on the number of sign
3623 // bits.
3624 if (match(U->getOperand(1), m_APInt(Denominator))) {
3625
3626 // Ignore non-positive denominator.
3627 if (Denominator->isStrictlyPositive()) {
3628 // Calculate the leading sign bit constraints by examining the
3629 // denominator. Given that the denominator is positive, there are two
3630 // cases:
3631 //
3632 // 1. The numerator is positive. The result range is [0,C) and
3633 // [0,C) u< (1 << ceilLogBase2(C)).
3634 //
3635 // 2. The numerator is negative. Then the result range is (-C,0] and
3636 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
3637 //
3638 // Thus a lower bound on the number of sign bits is `TyBits -
3639 // ceilLogBase2(C)`.
3640
3641 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
3642 Tmp = std::max(Tmp, ResBits);
3643 }
3644 }
3645 return Tmp;
3646 }
3647
3648 case Instruction::AShr: {
3649 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3650 // ashr X, C -> adds C sign bits. Vectors too.
3651 const APInt *ShAmt;
3652 if (match(U->getOperand(1), m_APInt(ShAmt))) {
3653 if (ShAmt->uge(TyBits))
3654 break; // Bad shift.
3655 unsigned ShAmtLimited = ShAmt->getZExtValue();
3656 Tmp += ShAmtLimited;
3657 if (Tmp > TyBits) Tmp = TyBits;
3658 }
3659 return Tmp;
3660 }
3661 case Instruction::Shl: {
3662 const APInt *ShAmt;
3663 if (match(U->getOperand(1), m_APInt(ShAmt))) {
3664 // shl destroys sign bits.
3665 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3666 if (ShAmt->uge(TyBits) || // Bad shift.
3667 ShAmt->uge(Tmp)) break; // Shifted all sign bits out.
3668 Tmp2 = ShAmt->getZExtValue();
3669 return Tmp - Tmp2;
3670 }
3671 break;
3672 }
3673 case Instruction::And:
3674 case Instruction::Or:
3675 case Instruction::Xor: // NOT is handled here.
3676 // Logical binary ops preserve the number of sign bits at the worst.
3677 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3678 if (Tmp != 1) {
3679 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3680 FirstAnswer = std::min(Tmp, Tmp2);
3681 // We computed what we know about the sign bits as our first
3682 // answer. Now proceed to the generic code that uses
3683 // computeKnownBits, and pick whichever answer is better.
3684 }
3685 break;
3686
3687 case Instruction::Select: {
3688 // If we have a clamp pattern, we know that the number of sign bits will
3689 // be the minimum of the clamp min/max range.
3690 const Value *X;
3691 const APInt *CLow, *CHigh;
3692 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
3693 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
3694
3695 Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3696 if (Tmp == 1) break;
3697 Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q);
3698 return std::min(Tmp, Tmp2);
3699 }
3700
3701 case Instruction::Add:
3702 // Add can have at most one carry bit. Thus we know that the output
3703 // is, at worst, one more bit than the inputs.
3704 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3705 if (Tmp == 1) break;
3706
3707 // Special case decrementing a value (ADD X, -1):
3708 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
3709 if (CRHS->isAllOnesValue()) {
3710 KnownBits Known(TyBits);
3711 computeKnownBits(U->getOperand(0), Known, Depth + 1, Q);
3712
3713 // If the input is known to be 0 or 1, the output is 0/-1, which is
3714 // all sign bits set.
3715 if ((Known.Zero | 1).isAllOnes())
3716 return TyBits;
3717
3718 // If we are subtracting one from a positive number, there is no carry
3719 // out of the result.
3720 if (Known.isNonNegative())
3721 return Tmp;
3722 }
3723
3724 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3725 if (Tmp2 == 1) break;
3726 return std::min(Tmp, Tmp2) - 1;
3727
3728 case Instruction::Sub:
3729 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3730 if (Tmp2 == 1) break;
3731
3732 // Handle NEG.
3733 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
3734 if (CLHS->isNullValue()) {
3735 KnownBits Known(TyBits);
3736 computeKnownBits(U->getOperand(1), Known, Depth + 1, Q);
3737 // If the input is known to be 0 or 1, the output is 0/-1, which is
3738 // all sign bits set.
3739 if ((Known.Zero | 1).isAllOnes())
3740 return TyBits;
3741
3742 // If the input is known to be positive (the sign bit is known clear),
3743 // the output of the NEG has the same number of sign bits as the
3744 // input.
3745 if (Known.isNonNegative())
3746 return Tmp2;
3747
3748 // Otherwise, we treat this like a SUB.
3749 }
3750
3751 // Sub can have at most one carry bit. Thus we know that the output
3752 // is, at worst, one more bit than the inputs.
3753 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3754 if (Tmp == 1) break;
3755 return std::min(Tmp, Tmp2) - 1;
3756
3757 case Instruction::Mul: {
3758 // The output of the Mul can be at most twice the valid bits in the
3759 // inputs.
3760 unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3761 if (SignBitsOp0 == 1) break;
3762 unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3763 if (SignBitsOp1 == 1) break;
3764 unsigned OutValidBits =
3765 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
3766 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
3767 }
3768
3769 case Instruction::PHI: {
3770 const PHINode *PN = cast<PHINode>(U);
3771 unsigned NumIncomingValues = PN->getNumIncomingValues();
3772 // Don't analyze large in-degree PHIs.
3773 if (NumIncomingValues > 4) break;
3774 // Unreachable blocks may have zero-operand PHI nodes.
3775 if (NumIncomingValues == 0) break;
3776
3777 // Take the minimum of all incoming values. This can't infinitely loop
3778 // because of our depth threshold.
3779 SimplifyQuery RecQ = Q;
3780 Tmp = TyBits;
3781 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
3782 if (Tmp == 1) return Tmp;
3783 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
3784 Tmp = std::min(
3785 Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, RecQ));
3786 }
3787 return Tmp;
3788 }
3789
3790 case Instruction::Trunc: {
3791 // If the input contained enough sign bits that some remain after the
3792 // truncation, then we can make use of that. Otherwise we don't know
3793 // anything.
3794 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3795 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
3796 if (Tmp > (OperandTyBits - TyBits))
3797 return Tmp - (OperandTyBits - TyBits);
3798
3799 return 1;
3800 }
3801
3802 case Instruction::ExtractElement:
3803 // Look through extract element. At the moment we keep this simple and
3804 // skip tracking the specific element. But at least we might find
3805 // information valid for all elements of the vector (for example if vector
3806 // is sign extended, shifted, etc).
3807 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3808
3809 case Instruction::ShuffleVector: {
3810 // Collect the minimum number of sign bits that are shared by every vector
3811 // element referenced by the shuffle.
3812 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
3813 if (!Shuf) {
3814 // FIXME: Add support for shufflevector constant expressions.
3815 return 1;
3816 }
3817 APInt DemandedLHS, DemandedRHS;
3818 // For undef elements, we don't know anything about the common state of
3819 // the shuffle result.
3820 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3821 return 1;
3822 Tmp = std::numeric_limits<unsigned>::max();
3823 if (!!DemandedLHS) {
3824 const Value *LHS = Shuf->getOperand(0);
3825 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q);
3826 }
3827 // If we don't know anything, early out and try computeKnownBits
3828 // fall-back.
3829 if (Tmp == 1)
3830 break;
3831 if (!!DemandedRHS) {
3832 const Value *RHS = Shuf->getOperand(1);
3833 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q);
3834 Tmp = std::min(Tmp, Tmp2);
3835 }
3836 // If we don't know anything, early out and try computeKnownBits
3837 // fall-back.
3838 if (Tmp == 1)
3839 break;
3840 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
3841 return Tmp;
3842 }
3843 case Instruction::Call: {
3844 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
3845 switch (II->getIntrinsicID()) {
3846 default: break;
3847 case Intrinsic::abs:
3848 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3849 if (Tmp == 1) break;
3850
3851 // Absolute value reduces number of sign bits by at most 1.
3852 return Tmp - 1;
3853 case Intrinsic::smin:
3854 case Intrinsic::smax: {
3855 const APInt *CLow, *CHigh;
3856 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
3857 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
3858 }
3859 }
3860 }
3861 }
3862 }
3863 }
3864
3865 // Finally, if we can prove that the top bits of the result are 0's or 1's,
3866 // use this information.
3867
3868 // If we can examine all elements of a vector constant successfully, we're
3869 // done (we can't do any better than that). If not, keep trying.
3870 if (unsigned VecSignBits =
3871 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
3872 return VecSignBits;
3873
3874 KnownBits Known(TyBits);
3875 computeKnownBits(V, DemandedElts, Known, Depth, Q);
3876
3877 // If we know that the sign bit is either zero or one, determine the number of
3878 // identical bits in the top of the input value.
3879 return std::max(FirstAnswer, Known.countMinSignBits());
3880}
3881
3883 const TargetLibraryInfo *TLI) {
3884 const Function *F = CB.getCalledFunction();
3885 if (!F)
3887
3888 if (F->isIntrinsic())
3889 return F->getIntrinsicID();
3890
3891 // We are going to infer semantics of a library function based on mapping it
3892 // to an LLVM intrinsic. Check that the library function is available from
3893 // this callbase and in this environment.
3894 LibFunc Func;
3895 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
3896 !CB.onlyReadsMemory())
3898
3899 switch (Func) {
3900 default:
3901 break;
3902 case LibFunc_sin:
3903 case LibFunc_sinf:
3904 case LibFunc_sinl:
3905 return Intrinsic::sin;
3906 case LibFunc_cos:
3907 case LibFunc_cosf:
3908 case LibFunc_cosl:
3909 return Intrinsic::cos;
3910 case LibFunc_exp:
3911 case LibFunc_expf:
3912 case LibFunc_expl:
3913 return Intrinsic::exp;
3914 case LibFunc_exp2:
3915 case LibFunc_exp2f:
3916 case LibFunc_exp2l:
3917 return Intrinsic::exp2;
3918 case LibFunc_log:
3919 case LibFunc_logf:
3920 case LibFunc_logl:
3921 return Intrinsic::log;
3922 case LibFunc_log10:
3923 case LibFunc_log10f:
3924 case LibFunc_log10l:
3925 return Intrinsic::log10;
3926 case LibFunc_log2:
3927 case LibFunc_log2f:
3928 case LibFunc_log2l:
3929 return Intrinsic::log2;
3930 case LibFunc_fabs:
3931 case LibFunc_fabsf:
3932 case LibFunc_fabsl:
3933 return Intrinsic::fabs;
3934 case LibFunc_fmin:
3935 case LibFunc_fminf:
3936 case LibFunc_fminl:
3937 return Intrinsic::minnum;
3938 case LibFunc_fmax:
3939 case LibFunc_fmaxf:
3940 case LibFunc_fmaxl:
3941 return Intrinsic::maxnum;
3942 case LibFunc_copysign:
3943 case LibFunc_copysignf:
3944 case LibFunc_copysignl:
3945 return Intrinsic::copysign;
3946 case LibFunc_floor:
3947 case LibFunc_floorf:
3948 case LibFunc_floorl:
3949 return Intrinsic::floor;
3950 case LibFunc_ceil:
3951 case LibFunc_ceilf:
3952 case LibFunc_ceill:
3953 return Intrinsic::ceil;
3954 case LibFunc_trunc:
3955 case LibFunc_truncf:
3956 case LibFunc_truncl:
3957 return Intrinsic::trunc;
3958 case LibFunc_rint:
3959 case LibFunc_rintf:
3960 case LibFunc_rintl:
3961 return Intrinsic::rint;
3962 case LibFunc_nearbyint:
3963 case LibFunc_nearbyintf:
3964 case LibFunc_nearbyintl:
3965 return Intrinsic::nearbyint;
3966 case LibFunc_round:
3967 case LibFunc_roundf:
3968 case LibFunc_roundl:
3969 return Intrinsic::round;
3970 case LibFunc_roundeven:
3971 case LibFunc_roundevenf:
3972 case LibFunc_roundevenl:
3973 return Intrinsic::roundeven;
3974 case LibFunc_pow:
3975 case LibFunc_powf:
3976 case LibFunc_powl:
3977 return Intrinsic::pow;
3978 case LibFunc_sqrt:
3979 case LibFunc_sqrtf:
3980 case LibFunc_sqrtl:
3981 return Intrinsic::sqrt;
3982 }
3983
3985}
3986
3987/// Return true if it's possible to assume IEEE treatment of input denormals in
3988/// \p F for \p Val.
3989static bool inputDenormalIsIEEE(const Function &F, const Type *Ty) {
3990 Ty = Ty->getScalarType();
3991 return F.getDenormalMode(Ty->getFltSemantics()).Input == DenormalMode::IEEE;
3992}
3993
3994static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
3995 Ty = Ty->getScalarType();
3996 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
3997 return Mode.Input == DenormalMode::IEEE ||
3998 Mode.Input == DenormalMode::PositiveZero;
3999}
4000
4001static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4002 Ty = Ty->getScalarType();
4003 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4004 return Mode.Output == DenormalMode::IEEE ||
4005 Mode.Output == DenormalMode::PositiveZero;
4006}
4007
4009 return isKnownNeverZero() &&
4011}
4012
4014 Type *Ty) const {
4015 return isKnownNeverNegZero() &&
4017}
4018
4020 Type *Ty) const {
4021 if (!isKnownNeverPosZero())
4022 return false;
4023
4024 // If we know there are no denormals, nothing can be flushed to zero.
4026 return true;
4027
4028 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4029 switch (Mode.Input) {
4030 case DenormalMode::IEEE:
4031 return true;
4033 // Negative subnormal won't flush to +0
4034 return isKnownNeverPosSubnormal();
4036 default:
4037 // Both positive and negative subnormal could flush to +0
4038 return false;
4039 }
4040
4041 llvm_unreachable("covered switch over denormal mode");
4042}
4043
4045 Type *Ty) {
4046 KnownFPClasses = Src.KnownFPClasses;
4047 // If we aren't assuming the source can't be a zero, we don't have to check if
4048 // a denormal input could be flushed.
4049 if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero())
4050 return;
4051
4052 // If we know the input can't be a denormal, it can't be flushed to 0.
4053 if (Src.isKnownNeverSubnormal())
4054 return;
4055
4056 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4057
4058 if (!Src.isKnownNeverPosSubnormal() && Mode != DenormalMode::getIEEE())
4060
4061 if (!Src.isKnownNeverNegSubnormal() && Mode != DenormalMode::getIEEE()) {
4062 if (Mode != DenormalMode::getPositiveZero())
4064
4065 if (Mode.Input == DenormalMode::PositiveZero ||
4066 Mode.Output == DenormalMode::PositiveZero ||
4067 Mode.Input == DenormalMode::Dynamic ||
4068 Mode.Output == DenormalMode::Dynamic)
4070 }
4071}
4072
4074 const Function &F, Type *Ty) {
4075 propagateDenormal(Src, F, Ty);
4076 propagateNaN(Src, /*PreserveSign=*/true);
4077}
4078
4079/// Given an exploded icmp instruction, return true if the comparison only
4080/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4081/// the result of the comparison is true when the input value is signed.
4083 bool &TrueIfSigned) {
4084 switch (Pred) {
4085 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4086 TrueIfSigned = true;
4087 return RHS.isZero();
4088 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4089 TrueIfSigned = true;
4090 return RHS.isAllOnes();
4091 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4092 TrueIfSigned = false;
4093 return RHS.isAllOnes();
4094 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4095 TrueIfSigned = false;
4096 return RHS.isZero();
4097 case ICmpInst::ICMP_UGT:
4098 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4099 TrueIfSigned = true;
4100 return RHS.isMaxSignedValue();
4101 case ICmpInst::ICMP_UGE:
4102 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4103 TrueIfSigned = true;
4104 return RHS.isMinSignedValue();
4105 case ICmpInst::ICMP_ULT:
4106 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4107 TrueIfSigned = false;
4108 return RHS.isMinSignedValue();
4109 case ICmpInst::ICMP_ULE:
4110 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4111 TrueIfSigned = false;
4112 return RHS.isMaxSignedValue();
4113 default:
4114 return false;
4115 }
4116}
4117
4118/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
4119/// same result as an fcmp with the given operands.
4120std::pair<Value *, FPClassTest> llvm::fcmpToClassTest(FCmpInst::Predicate Pred,
4121 const Function &F,
4122 Value *LHS, Value *RHS,
4123 bool LookThroughSrc) {
4124 const APFloat *ConstRHS;
4125 if (!match(RHS, m_APFloatAllowPoison(ConstRHS)))
4126 return {nullptr, fcAllFlags};
4127
4128 return fcmpToClassTest(Pred, F, LHS, ConstRHS, LookThroughSrc);
4129}
4130
4131std::pair<Value *, FPClassTest>
4133 const APFloat *ConstRHS, bool LookThroughSrc) {
4134
4135 auto [Src, ClassIfTrue, ClassIfFalse] =
4136 fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4137 if (Src && ClassIfTrue == ~ClassIfFalse)
4138 return {Src, ClassIfTrue};
4139 return {nullptr, fcAllFlags};
4140}
4141
4142/// Return the return value for fcmpImpliesClass for a compare that produces an
4143/// exact class test.
4144static std::tuple<Value *, FPClassTest, FPClassTest> exactClass(Value *V,
4145 FPClassTest M) {
4146 return {V, M, ~M};
4147}
4148
4149std::tuple<Value *, FPClassTest, FPClassTest>
4151 FPClassTest RHSClass, bool LookThroughSrc) {
4152 assert(RHSClass != fcNone);
4153 Value *Src = LHS;
4154
4155 if (Pred == FCmpInst::FCMP_TRUE)
4156 return exactClass(Src, fcAllFlags);
4157
4158 if (Pred == FCmpInst::FCMP_FALSE)
4159 return exactClass(Src, fcNone);
4160
4161 const FPClassTest OrigClass = RHSClass;
4162
4163 const bool IsNegativeRHS = (RHSClass & fcNegative) == RHSClass;
4164 const bool IsPositiveRHS = (RHSClass & fcPositive) == RHSClass;
4165 const bool IsNaN = (RHSClass & ~fcNan) == fcNone;
4166
4167 if (IsNaN) {
4168 // fcmp o__ x, nan -> false
4169 // fcmp u__ x, nan -> true
4170 return exactClass(Src, CmpInst::isOrdered(Pred) ? fcNone : fcAllFlags);
4171 }
4172
4173 // fcmp ord x, zero|normal|subnormal|inf -> ~fcNan
4174 if (Pred == FCmpInst::FCMP_ORD)
4175 return exactClass(Src, ~fcNan);
4176
4177 // fcmp uno x, zero|normal|subnormal|inf -> fcNan
4178 if (Pred == FCmpInst::FCMP_UNO)
4179 return exactClass(Src, fcNan);
4180
4181 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4182 if (IsFabs)
4183 RHSClass = llvm::inverse_fabs(RHSClass);
4184
4185 const bool IsZero = (OrigClass & fcZero) == OrigClass;
4186 if (IsZero) {
4187 assert(Pred != FCmpInst::FCMP_ORD && Pred != FCmpInst::FCMP_UNO);
4188 // Compares with fcNone are only exactly equal to fcZero if input denormals
4189 // are not flushed.
4190 // TODO: Handle DAZ by expanding masks to cover subnormal cases.
4191 if (!inputDenormalIsIEEE(F, LHS->getType()))
4192 return {nullptr, fcAllFlags, fcAllFlags};
4193
4194 switch (Pred) {
4195 case FCmpInst::FCMP_OEQ: // Match x == 0.0
4196 return exactClass(Src, fcZero);
4197 case FCmpInst::FCMP_UEQ: // Match isnan(x) || (x == 0.0)
4198 return exactClass(Src, fcZero | fcNan);
4199 case FCmpInst::FCMP_UNE: // Match (x != 0.0)
4200 return exactClass(Src, ~fcZero);
4201 case FCmpInst::FCMP_ONE: // Match !isnan(x) && x != 0.0
4202 return exactClass(Src, ~fcNan & ~fcZero);
4203 case FCmpInst::FCMP_ORD:
4204 // Canonical form of ord/uno is with a zero. We could also handle
4205 // non-canonical other non-NaN constants or LHS == RHS.
4206 return exactClass(Src, ~fcNan);
4207 case FCmpInst::FCMP_UNO:
4208 return exactClass(Src, fcNan);
4209 case FCmpInst::FCMP_OGT: // x > 0
4211 case FCmpInst::FCMP_UGT: // isnan(x) || x > 0
4213 case FCmpInst::FCMP_OGE: // x >= 0
4214 return exactClass(Src, fcPositive | fcNegZero);
4215 case FCmpInst::FCMP_UGE: // isnan(x) || x >= 0
4216 return exactClass(Src, fcPositive | fcNegZero | fcNan);
4217 case FCmpInst::FCMP_OLT: // x < 0
4219 case FCmpInst::FCMP_ULT: // isnan(x) || x < 0
4221 case FCmpInst::FCMP_OLE: // x <= 0
4222 return exactClass(Src, fcNegative | fcPosZero);
4223 case FCmpInst::FCMP_ULE: // isnan(x) || x <= 0
4224 return exactClass(Src, fcNegative | fcPosZero | fcNan);
4225 default:
4226 llvm_unreachable("all compare types are handled");
4227 }
4228
4229 return {nullptr, fcAllFlags, fcAllFlags};
4230 }
4231
4232 const bool IsDenormalRHS = (OrigClass & fcSubnormal) == OrigClass;
4233
4234 const bool IsInf = (OrigClass & fcInf) == OrigClass;
4235 if (IsInf) {
4236 FPClassTest Mask = fcAllFlags;
4237
4238 switch (Pred) {
4239 case FCmpInst::FCMP_OEQ:
4240 case FCmpInst::FCMP_UNE: {
4241 // Match __builtin_isinf patterns
4242 //
4243 // fcmp oeq x, +inf -> is_fpclass x, fcPosInf
4244 // fcmp oeq fabs(x), +inf -> is_fpclass x, fcInf
4245 // fcmp oeq x, -inf -> is_fpclass x, fcNegInf
4246 // fcmp oeq fabs(x), -inf -> is_fpclass x, 0 -> false
4247 //
4248 // fcmp une x, +inf -> is_fpclass x, ~fcPosInf
4249 // fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf
4250 // fcmp une x, -inf -> is_fpclass x, ~fcNegInf
4251 // fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true
4252 if (IsNegativeRHS) {
4253 Mask = fcNegInf;
4254 if (IsFabs)
4255 Mask = fcNone;
4256 } else {
4257 Mask = fcPosInf;
4258 if (IsFabs)
4259 Mask |= fcNegInf;
4260 }
4261 break;
4262 }
4263 case FCmpInst::FCMP_ONE:
4264 case FCmpInst::FCMP_UEQ: {
4265 // Match __builtin_isinf patterns
4266 // fcmp one x, -inf -> is_fpclass x, fcNegInf
4267 // fcmp one fabs(x), -inf -> is_fpclass x, ~fcNegInf & ~fcNan
4268 // fcmp one x, +inf -> is_fpclass x, ~fcNegInf & ~fcNan
4269 // fcmp one fabs(x), +inf -> is_fpclass x, ~fcInf & fcNan
4270 //
4271 // fcmp ueq x, +inf -> is_fpclass x, fcPosInf|fcNan
4272 // fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan
4273 // fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan
4274 // fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan
4275 if (IsNegativeRHS) {
4276 Mask = ~fcNegInf & ~fcNan;
4277 if (IsFabs)
4278 Mask = ~fcNan;
4279 } else {
4280 Mask = ~fcPosInf & ~fcNan;
4281 if (IsFabs)
4282 Mask &= ~fcNegInf;
4283 }
4284
4285 break;
4286 }
4287 case FCmpInst::FCMP_OLT:
4288 case FCmpInst::FCMP_UGE: {
4289 if (IsNegativeRHS) {
4290 // No value is ordered and less than negative infinity.
4291 // All values are unordered with or at least negative infinity.
4292 // fcmp olt x, -inf -> false
4293 // fcmp uge x, -inf -> true
4294 Mask = fcNone;
4295 break;
4296 }
4297
4298 // fcmp olt fabs(x), +inf -> fcFinite
4299 // fcmp uge fabs(x), +inf -> ~fcFinite
4300 // fcmp olt x, +inf -> fcFinite|fcNegInf
4301 // fcmp uge x, +inf -> ~(fcFinite|fcNegInf)
4302 Mask = fcFinite;
4303 if (!IsFabs)
4304 Mask |= fcNegInf;
4305 break;
4306 }
4307 case FCmpInst::FCMP_OGE:
4308 case FCmpInst::FCMP_ULT: {
4309 if (IsNegativeRHS) {
4310 // fcmp oge x, -inf -> ~fcNan
4311 // fcmp oge fabs(x), -inf -> ~fcNan
4312 // fcmp ult x, -inf -> fcNan
4313 // fcmp ult fabs(x), -inf -> fcNan
4314 Mask = ~fcNan;
4315 break;
4316 }
4317
4318 // fcmp oge fabs(x), +inf -> fcInf
4319 // fcmp oge x, +inf -> fcPosInf
4320 // fcmp ult fabs(x), +inf -> ~fcInf
4321 // fcmp ult x, +inf -> ~fcPosInf
4322 Mask = fcPosInf;
4323 if (IsFabs)
4324 Mask |= fcNegInf;
4325 break;
4326 }
4327 case FCmpInst::FCMP_OGT:
4328 case FCmpInst::FCMP_ULE: {
4329 if (IsNegativeRHS) {
4330 // fcmp ogt x, -inf -> fcmp one x, -inf
4331 // fcmp ogt fabs(x), -inf -> fcmp ord x, x
4332 // fcmp ule x, -inf -> fcmp ueq x, -inf
4333 // fcmp ule fabs(x), -inf -> fcmp uno x, x
4334 Mask = IsFabs ? ~fcNan : ~(fcNegInf | fcNan);
4335 break;
4336 }
4337
4338 // No value is ordered and greater than infinity.
4339 Mask = fcNone;
4340 break;
4341 }
4342 case FCmpInst::FCMP_OLE:
4343 case FCmpInst::FCMP_UGT: {
4344 if (IsNegativeRHS) {
4345 Mask = IsFabs ? fcNone : fcNegInf;
4346 break;
4347 }
4348
4349 // fcmp ole x, +inf -> fcmp ord x, x
4350 // fcmp ole fabs(x), +inf -> fcmp ord x, x
4351 // fcmp ole x, -inf -> fcmp oeq x, -inf
4352 // fcmp ole fabs(x), -inf -> false
4353 Mask = ~fcNan;
4354 break;
4355 }
4356 default:
4357 llvm_unreachable("all compare types are handled");
4358 }
4359
4360 // Invert the comparison for the unordered cases.
4361 if (FCmpInst::isUnordered(Pred))
4362 Mask = ~Mask;
4363
4364 return exactClass(Src, Mask);
4365 }
4366
4367 if (Pred == FCmpInst::FCMP_OEQ)
4368 return {Src, RHSClass, fcAllFlags};
4369
4370 if (Pred == FCmpInst::FCMP_UEQ) {
4371 FPClassTest Class = RHSClass | fcNan;
4372 return {Src, Class, ~fcNan};
4373 }
4374
4375 if (Pred == FCmpInst::FCMP_ONE)
4376 return {Src, ~fcNan, RHSClass | fcNan};
4377
4378 if (Pred == FCmpInst::FCMP_UNE)
4379 return {Src, fcAllFlags, RHSClass};
4380
4381 assert((RHSClass == fcNone || RHSClass == fcPosNormal ||
4382 RHSClass == fcNegNormal || RHSClass == fcNormal ||
4383 RHSClass == fcPosSubnormal || RHSClass == fcNegSubnormal ||
4384 RHSClass == fcSubnormal) &&
4385 "should have been recognized as an exact class test");
4386
4387 if (IsNegativeRHS) {
4388 // TODO: Handle fneg(fabs)
4389 if (IsFabs) {
4390 // fabs(x) o> -k -> fcmp ord x, x
4391 // fabs(x) u> -k -> true
4392 // fabs(x) o< -k -> false
4393 // fabs(x) u< -k -> fcmp uno x, x
4394 switch (Pred) {
4395 case FCmpInst::FCMP_OGT:
4396 case FCmpInst::FCMP_OGE:
4397 return {Src, ~fcNan, fcNan};
4398 case FCmpInst::FCMP_UGT:
4399 case FCmpInst::FCMP_UGE:
4400 return {Src, fcAllFlags, fcNone};
4401 case FCmpInst::FCMP_OLT:
4402 case FCmpInst::FCMP_OLE:
4403 return {Src, fcNone, fcAllFlags};
4404 case FCmpInst::FCMP_ULT:
4405 case FCmpInst::FCMP_ULE:
4406 return {Src, fcNan, ~fcNan};
4407 default:
4408 break;
4409 }
4410
4411 return {nullptr, fcAllFlags, fcAllFlags};
4412 }
4413
4414 FPClassTest ClassesLE = fcNegInf | fcNegNormal;
4416
4417 if (IsDenormalRHS)
4418 ClassesLE |= fcNegSubnormal;
4419 else
4420 ClassesGE |= fcNegNormal;
4421
4422 switch (Pred) {
4423 case FCmpInst::FCMP_OGT:
4424 case FCmpInst::FCMP_OGE:
4425 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4426 case FCmpInst::FCMP_UGT:
4427 case FCmpInst::FCMP_UGE:
4428 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4429 case FCmpInst::FCMP_OLT:
4430 case FCmpInst::FCMP_OLE:
4431 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4432 case FCmpInst::FCMP_ULT:
4433 case FCmpInst::FCMP_ULE:
4434 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4435 default:
4436 break;
4437 }
4438 } else if (IsPositiveRHS) {
4439 FPClassTest ClassesGE = fcPosNormal | fcPosInf;
4441 if (IsDenormalRHS)
4442 ClassesGE |= fcPosSubnormal;
4443 else
4444 ClassesLE |= fcPosNormal;
4445
4446 if (IsFabs) {
4447 ClassesGE = llvm::inverse_fabs(ClassesGE);
4448 ClassesLE = llvm::inverse_fabs(ClassesLE);
4449 }
4450
4451 switch (Pred) {
4452 case FCmpInst::FCMP_OGT:
4453 case FCmpInst::FCMP_OGE:
4454 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4455 case FCmpInst::FCMP_UGT:
4456 case FCmpInst::FCMP_UGE:
4457 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4458 case FCmpInst::FCMP_OLT:
4459 case FCmpInst::FCMP_OLE:
4460 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4461 case FCmpInst::FCMP_ULT:
4462 case FCmpInst::FCMP_ULE:
4463 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4464 default:
4465 break;
4466 }
4467 }
4468
4469 return {nullptr, fcAllFlags, fcAllFlags};
4470}
4471
4472std::tuple<Value *, FPClassTest, FPClassTest>
4474 const APFloat &ConstRHS, bool LookThroughSrc) {
4475 // We can refine checks against smallest normal / largest denormal to an
4476 // exact class test.
4477 if (!ConstRHS.isNegative() && ConstRHS.isSmallestNormalized()) {
4478 Value *Src = LHS;
4479 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4480
4481 FPClassTest Mask;
4482 // Match pattern that's used in __builtin_isnormal.
4483 switch (Pred) {
4484 case FCmpInst::FCMP_OLT:
4485 case FCmpInst::FCMP_UGE: {
4486 // fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero
4487 // fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero
4488 // fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf
4489 // fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero)
4490 Mask = fcZero | fcSubnormal;
4491 if (!IsFabs)
4492 Mask |= fcNegNormal | fcNegInf;
4493
4494 break;
4495 }
4496 case FCmpInst::FCMP_OGE:
4497 case FCmpInst::FCMP_ULT: {
4498 // fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf
4499 // fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal
4500 // fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf)
4501 // fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal)
4502 Mask = fcPosInf | fcPosNormal;
4503 if (IsFabs)
4504 Mask |= fcNegInf | fcNegNormal;
4505 break;
4506 }
4507 default:
4508 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(),
4509 LookThroughSrc);
4510 }
4511
4512 // Invert the comparison for the unordered cases.
4513 if (FCmpInst::isUnordered(Pred))
4514 Mask = ~Mask;
4515
4516 return exactClass(Src, Mask);
4517 }
4518
4519 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(), LookThroughSrc);
4520}
4521
4522std::tuple<Value *, FPClassTest, FPClassTest>
4524 Value *RHS, bool LookThroughSrc) {
4525 const APFloat *ConstRHS;
4526 if (!match(RHS, m_APFloatAllowPoison(ConstRHS)))
4527 return {nullptr, fcAllFlags, fcAllFlags};
4528
4529 // TODO: Just call computeKnownFPClass for RHS to handle non-constants.
4530 return fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4531}
4532
4534 bool CondIsTrue,
4535 const Instruction *CxtI,
4536 KnownFPClass &KnownFromContext) {
4537 CmpInst::Predicate Pred;
4538 Value *LHS;
4539 uint64_t ClassVal = 0;
4540 const APFloat *CRHS;
4541 const APInt *RHS;
4542 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4543 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4544 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4545 if (CmpVal == V)
4546 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4547 } else if (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(
4548 m_Value(LHS), m_ConstantInt(ClassVal)))) {
4549 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4550 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4551 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Value(LHS)),
4552 m_APInt(RHS)))) {
4553 bool TrueIfSigned;
4554 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4555 return;
4556 if (TrueIfSigned == CondIsTrue)
4557 KnownFromContext.signBitMustBeOne();
4558 else
4559 KnownFromContext.signBitMustBeZero();
4560 }
4561}
4562
4564 const SimplifyQuery &Q) {
4565 KnownFPClass KnownFromContext;
4566
4567 if (!Q.CxtI)
4568 return KnownFromContext;
4569
4570 if (Q.DC && Q.DT) {
4571 // Handle dominating conditions.
4572 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4573 Value *Cond = BI->getCondition();
4574
4575 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4576 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4577 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4578 KnownFromContext);
4579
4580 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4581 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4582 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4583 KnownFromContext);
4584 }
4585 }
4586
4587 if (!Q.AC)
4588 return KnownFromContext;
4589
4590 // Try to restrict the floating-point classes based on information from
4591 // assumptions.
4592 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4593 if (!AssumeVH)
4594 continue;
4595 CallInst *I = cast<CallInst>(AssumeVH);
4596
4597 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4598 "Got assumption for the wrong function!");
4599 assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
4600 "must be an assume intrinsic");
4601
4602 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4603 continue;
4604
4605 computeKnownFPClassFromCond(V, I->getArgOperand(0), /*CondIsTrue=*/true,
4606 Q.CxtI, KnownFromContext);
4607 }
4608
4609 return KnownFromContext;
4610}
4611
4612void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4613 FPClassTest InterestedClasses, KnownFPClass &Known,
4614 unsigned Depth, const SimplifyQuery &Q);
4615
4616static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4617 FPClassTest InterestedClasses, unsigned Depth,
4618 const SimplifyQuery &Q) {
4619 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4620 APInt DemandedElts =
4621 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4622 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Depth, Q);
4623}
4624
4626 const APInt &DemandedElts,
4627 FPClassTest InterestedClasses,
4628 KnownFPClass &Known, unsigned Depth,
4629 const SimplifyQuery &Q) {
4630 if ((InterestedClasses &
4632 return;
4633
4634 KnownFPClass KnownSrc;
4635 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4636 KnownSrc, Depth + 1, Q);
4637
4638 // Sign should be preserved
4639 // TODO: Handle cannot be ordered greater than zero
4640 if (KnownSrc.cannotBeOrderedLessThanZero())
4642
4643 Known.propagateNaN(KnownSrc, true);
4644
4645 // Infinity needs a range check.
4646}
4647
4648void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4649 FPClassTest InterestedClasses, KnownFPClass &Known,
4650 unsigned Depth, const SimplifyQuery &Q) {
4651 assert(Known.isUnknown() && "should not be called with known information");
4652
4653 if (!DemandedElts) {
4654 // No demanded elts, better to assume we don't know anything.
4655 Known.resetAll();
4656 return;
4657 }
4658
4659 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4660
4661 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4662 Known.KnownFPClasses = CFP->getValueAPF().classify();
4663 Known.SignBit = CFP->isNegative();
4664 return;
4665 }
4666
4667 if (isa<ConstantAggregateZero>(V)) {
4668 Known.KnownFPClasses = fcPosZero;
4669 Known.SignBit = false;
4670 return;
4671 }
4672
4673 if (isa<PoisonValue>(V)) {
4674 Known.KnownFPClasses = fcNone;
4675 Known.SignBit = false;
4676 return;
4677 }
4678
4679 // Try to handle fixed width vector constants
4680 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
4681 const Constant *CV = dyn_cast<Constant>(V);
4682 if (VFVTy && CV) {
4683 Known.KnownFPClasses = fcNone;
4684 bool SignBitAllZero = true;
4685 bool SignBitAllOne = true;
4686
4687 // For vectors, verify that each element is not NaN.
4688 unsigned NumElts = VFVTy->getNumElements();
4689 for (unsigned i = 0; i != NumElts; ++i) {
4690 if (!DemandedElts[i])
4691 continue;
4692
4693 Constant *Elt = CV->getAggregateElement(i);
4694 if (!Elt) {
4695 Known = KnownFPClass();
4696 return;
4697 }
4698 if (isa<UndefValue>(Elt))
4699 continue;
4700 auto *CElt = dyn_cast<ConstantFP>(Elt);
4701 if (!CElt) {
4702 Known = KnownFPClass();
4703 return;
4704 }
4705
4706 const APFloat &C = CElt->getValueAPF();
4707 Known.KnownFPClasses |= C.classify();
4708 if (C.isNegative())
4709 SignBitAllZero = false;
4710 else
4711 SignBitAllOne = false;
4712 }
4713 if (SignBitAllOne != SignBitAllZero)
4714 Known.SignBit = SignBitAllOne;
4715 return;
4716 }
4717
4718 FPClassTest KnownNotFromFlags = fcNone;
4719 if (const auto *CB = dyn_cast<CallBase>(V))
4720 KnownNotFromFlags |= CB->getRetNoFPClass();
4721 else if (const auto *Arg = dyn_cast<Argument>(V))
4722 KnownNotFromFlags |= Arg->getNoFPClass();
4723
4724 const Operator *Op = dyn_cast<Operator>(V);
4725 if (const FPMathOperator *FPOp = dyn_cast_or_null<FPMathOperator>(Op)) {
4726 if (FPOp->hasNoNaNs())
4727 KnownNotFromFlags |= fcNan;
4728 if (FPOp->hasNoInfs())
4729 KnownNotFromFlags |= fcInf;
4730 }
4731
4732 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
4733 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
4734
4735 // We no longer need to find out about these bits from inputs if we can
4736 // assume this from flags/attributes.
4737 InterestedClasses &= ~KnownNotFromFlags;
4738
4739 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
4740 Known.knownNot(KnownNotFromFlags);
4741 if (!Known.SignBit && AssumedClasses.SignBit) {
4742 if (*AssumedClasses.SignBit)
4743 Known.signBitMustBeOne();
4744 else
4745 Known.signBitMustBeZero();
4746 }
4747 });
4748
4749 if (!Op)
4750 return;
4751
4752 // All recursive calls that increase depth must come after this.
4754 return;
4755
4756 const unsigned Opc = Op->getOpcode();
4757 switch (Opc) {
4758 case Instruction::FNeg: {
4759 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4760 Known, Depth + 1, Q);
4761 Known.fneg();
4762 break;
4763 }
4764 case Instruction::Select: {
4765 Value *Cond = Op->getOperand(0);
4766 Value *LHS = Op->getOperand(1);
4767 Value *RHS = Op->getOperand(2);
4768
4769 FPClassTest FilterLHS = fcAllFlags;
4770 FPClassTest FilterRHS = fcAllFlags;
4771
4772 Value *TestedValue = nullptr;
4773 FPClassTest MaskIfTrue = fcAllFlags;
4774 FPClassTest MaskIfFalse = fcAllFlags;
4775 uint64_t ClassVal = 0;
4776 const Function *F = cast<Instruction>(Op)->getFunction();
4777 CmpInst::Predicate Pred;
4778 Value *CmpLHS, *CmpRHS;
4779 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
4780 // If the select filters out a value based on the class, it no longer
4781 // participates in the class of the result
4782
4783 // TODO: In some degenerate cases we can infer something if we try again
4784 // without looking through sign operations.
4785 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
4786 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
4787 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
4788 } else if (match(Cond,
4789 m_Intrinsic<Intrinsic::is_fpclass>(
4790 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
4791 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
4792 MaskIfTrue = TestedMask;
4793 MaskIfFalse = ~TestedMask;
4794 }
4795
4796 if (TestedValue == LHS) {
4797 // match !isnan(x) ? x : y
4798 FilterLHS = MaskIfTrue;
4799 } else if (TestedValue == RHS) { // && IsExactClass
4800 // match !isnan(x) ? y : x
4801 FilterRHS = MaskIfFalse;
4802 }
4803
4804 KnownFPClass Known2;
4805 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
4806 Depth + 1, Q);
4807 Known.KnownFPClasses &= FilterLHS;
4808
4809 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
4810 Known2, Depth + 1, Q);
4811 Known2.KnownFPClasses &= FilterRHS;
4812
4813 Known |= Known2;
4814 break;
4815 }
4816 case Instruction::Call: {
4817 const CallInst *II = cast<CallInst>(Op);
4818 const Intrinsic::ID IID = II->getIntrinsicID();
4819 switch (IID) {
4820 case Intrinsic::fabs: {
4821 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
4822 // If we only care about the sign bit we don't need to inspect the
4823 // operand.
4824 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
4825 InterestedClasses, Known, Depth + 1, Q);
4826 }
4827
4828 Known.fabs();
4829 break;
4830 }
4831 case Intrinsic::copysign: {
4832 KnownFPClass KnownSign;
4833
4834 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4835 Known, Depth + 1, Q);
4836 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4837 KnownSign, Depth + 1, Q);
4838 Known.copysign(KnownSign);
4839 break;
4840 }
4841 case Intrinsic::fma:
4842 case Intrinsic::fmuladd: {
4843 if ((InterestedClasses & fcNegative) == fcNone)
4844 break;
4845
4846 if (II->getArgOperand(0) != II->getArgOperand(1))
4847 break;
4848
4849 // The multiply cannot be -0 and therefore the add can't be -0
4850 Known.knownNot(fcNegZero);
4851
4852 // x * x + y is non-negative if y is non-negative.
4853 KnownFPClass KnownAddend;
4854 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
4855 KnownAddend, Depth + 1, Q);
4856
4857 if (KnownAddend.cannotBeOrderedLessThanZero())
4858 Known.knownNot(fcNegative);
4859 break;
4860 }
4861 case Intrinsic::sqrt:
4862 case Intrinsic::experimental_constrained_sqrt: {
4863 KnownFPClass KnownSrc;
4864 FPClassTest InterestedSrcs = InterestedClasses;
4865 if (InterestedClasses & fcNan)
4866 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
4867
4868 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
4869 KnownSrc, Depth + 1, Q);
4870
4871 if (KnownSrc.isKnownNeverPosInfinity())
4872 Known.knownNot(fcPosInf);
4873 if (KnownSrc.isKnownNever(fcSNan))
4874 Known.knownNot(fcSNan);
4875
4876 // Any negative value besides -0 returns a nan.
4877 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
4878 Known.knownNot(fcNan);
4879
4880 // The only negative value that can be returned is -0 for -0 inputs.
4882
4883 // If the input denormal mode could be PreserveSign, a negative
4884 // subnormal input could produce a negative zero output.
4885 const Function *F = II->getFunction();
4886 if (Q.IIQ.hasNoSignedZeros(II) ||
4887 (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))) {
4888 Known.knownNot(fcNegZero);
4889 if (KnownSrc.isKnownNeverNaN())
4890 Known.signBitMustBeZero();
4891 }
4892
4893 break;
4894 }
4895 case Intrinsic::sin:
4896 case Intrinsic::cos: {
4897 // Return NaN on infinite inputs.
4898 KnownFPClass KnownSrc;
4899 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4900 KnownSrc, Depth + 1, Q);
4901 Known.knownNot(fcInf);
4902 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
4903 Known.knownNot(fcNan);
4904 break;
4905 }
4906 case Intrinsic::maxnum:
4907 case Intrinsic::minnum:
4908 case Intrinsic::minimum:
4909 case Intrinsic::maximum: {
4910 KnownFPClass KnownLHS, KnownRHS;
4911 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4912 KnownLHS, Depth + 1, Q);
4913 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4914 KnownRHS, Depth + 1, Q);
4915
4916 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
4917 Known = KnownLHS | KnownRHS;
4918
4919 // If either operand is not NaN, the result is not NaN.
4920 if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum))
4921 Known.knownNot(fcNan);
4922
4923 if (IID == Intrinsic::maxnum) {
4924 // If at least one operand is known to be positive, the result must be
4925 // positive.
4926 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
4927 KnownLHS.isKnownNeverNaN()) ||
4928 (KnownRHS.cannotBeOrderedLessThanZero() &&
4929 KnownRHS.isKnownNeverNaN()))
4931 } else if (IID == Intrinsic::maximum) {
4932 // If at least one operand is known to be positive, the result must be
4933 // positive.
4934 if (KnownLHS.cannotBeOrderedLessThanZero() ||
4935 KnownRHS.cannotBeOrderedLessThanZero())
4937 } else if (IID == Intrinsic::minnum) {
4938 // If at least one operand is known to be negative, the result must be
4939 // negative.
4940 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
4941 KnownLHS.isKnownNeverNaN()) ||
4942 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
4943 KnownRHS.isKnownNeverNaN()))
4945 } else {
4946 // If at least one operand is known to be negative, the result must be
4947 // negative.
4948 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
4951 }
4952
4953 // Fixup zero handling if denormals could be returned as a zero.
4954 //
4955 // As there's no spec for denormal flushing, be conservative with the
4956 // treatment of denormals that could be flushed to zero. For older
4957 // subtargets on AMDGPU the min/max instructions would not flush the
4958 // output and return the original value.
4959 //
4960 if ((Known.KnownFPClasses & fcZero) != fcNone &&
4961 !Known.isKnownNeverSubnormal()) {
4962 const Function *Parent = II->getFunction();
4963 if (!Parent)
4964 break;
4965
4966 DenormalMode Mode = Parent->getDenormalMode(
4968 if (Mode != DenormalMode::getIEEE())
4969 Known.KnownFPClasses |= fcZero;
4970 }
4971
4972 if (Known.isKnownNeverNaN()) {
4973 if (KnownLHS.SignBit && KnownRHS.SignBit &&
4974 *KnownLHS.SignBit == *KnownRHS.SignBit) {
4975 if (*KnownLHS.SignBit)
4976 Known.signBitMustBeOne();
4977 else
4978 Known.signBitMustBeZero();
4979 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum) ||
4980 ((KnownLHS.isKnownNeverNegZero() ||
4981 KnownRHS.isKnownNeverPosZero()) &&
4982 (KnownLHS.isKnownNeverPosZero() ||
4983 KnownRHS.isKnownNeverNegZero()))) {
4984 if ((IID == Intrinsic::maximum || IID == Intrinsic::maxnum) &&
4985 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
4986 Known.signBitMustBeZero();
4987 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minnum) &&
4988 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
4989 Known.signBitMustBeOne();
4990 }
4991 }
4992 break;
4993 }
4994 case Intrinsic::canonicalize: {
4995 KnownFPClass KnownSrc;
4996 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4997 KnownSrc, Depth + 1, Q);
4998
4999 // This is essentially a stronger form of
5000 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5001 // actually have an IR canonicalization guarantee.
5002
5003 // Canonicalize may flush denormals to zero, so we have to consider the
5004 // denormal mode to preserve known-not-0 knowledge.
5005 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5006
5007 // Stronger version of propagateNaN
5008 // Canonicalize is guaranteed to quiet signaling nans.
5009 if (KnownSrc.isKnownNeverNaN())
5010 Known.knownNot(fcNan);
5011 else
5012 Known.knownNot(fcSNan);
5013
5014 const Function *F = II->getFunction();
5015 if (!F)
5016 break;
5017
5018 // If the parent function flushes denormals, the canonical output cannot
5019 // be a denormal.
5020 const fltSemantics &FPType =
5022 DenormalMode DenormMode = F->getDenormalMode(FPType);
5023 if (DenormMode == DenormalMode::getIEEE()) {
5024 if (KnownSrc.isKnownNever(fcPosZero))
5025 Known.knownNot(fcPosZero);
5026 if (KnownSrc.isKnownNever(fcNegZero))
5027 Known.knownNot(fcNegZero);
5028 break;
5029 }
5030
5031 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5032 Known.knownNot(fcSubnormal);
5033
5034 if (DenormMode.Input == DenormalMode::PositiveZero ||
5035 (DenormMode.Output == DenormalMode::PositiveZero &&
5036 DenormMode.Input == DenormalMode::IEEE))
5037 Known.knownNot(fcNegZero);
5038
5039 break;
5040 }
5041 case Intrinsic::vector_reduce_fmax:
5042 case Intrinsic::vector_reduce_fmin:
5043 case Intrinsic::vector_reduce_fmaximum:
5044 case Intrinsic::vector_reduce_fminimum: {
5045 // reduce min/max will choose an element from one of the vector elements,
5046 // so we can infer and class information that is common to all elements.
5048 InterestedClasses, Depth + 1, Q);
5049 // Can only propagate sign if output is never NaN.
5050 if (!Known.isKnownNeverNaN())
5051 Known.SignBit.reset();
5052 break;
5053 }
5054 case Intrinsic::trunc:
5055 case Intrinsic::floor:
5056 case Intrinsic::ceil:
5057 case Intrinsic::rint:
5058 case Intrinsic::nearbyint:
5059 case Intrinsic::round:
5060 case Intrinsic::roundeven: {
5061 KnownFPClass KnownSrc;
5062 FPClassTest InterestedSrcs = InterestedClasses;
5063 if (InterestedSrcs & fcPosFinite)
5064 InterestedSrcs |= fcPosFinite;
5065 if (InterestedSrcs & fcNegFinite)
5066 InterestedSrcs |= fcNegFinite;
5067 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5068 KnownSrc, Depth + 1, Q);
5069
5070 // Integer results cannot be subnormal.
5071 Known.knownNot(fcSubnormal);
5072
5073 Known.propagateNaN(KnownSrc, true);
5074
5075 // Pass through infinities, except PPC_FP128 is a special case for
5076 // intrinsics other than trunc.
5077 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5078 if (KnownSrc.isKnownNeverPosInfinity())
5079 Known.knownNot(fcPosInf);
5080 if (KnownSrc.isKnownNeverNegInfinity())
5081 Known.knownNot(fcNegInf);
5082 }
5083
5084 // Negative round ups to 0 produce -0
5085 if (KnownSrc.isKnownNever(fcPosFinite))
5086 Known.knownNot(fcPosFinite);
5087 if (KnownSrc.isKnownNever(fcNegFinite))
5088 Known.knownNot(fcNegFinite);
5089
5090 break;
5091 }
5092 case Intrinsic::exp:
5093 case Intrinsic::exp2:
5094 case Intrinsic::exp10: {
5095 Known.knownNot(fcNegative);
5096 if ((InterestedClasses & fcNan) == fcNone)
5097 break;
5098
5099 KnownFPClass KnownSrc;
5100 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5101 KnownSrc, Depth + 1, Q);
5102 if (KnownSrc.isKnownNeverNaN()) {
5103 Known.knownNot(fcNan);
5104 Known.signBitMustBeZero();
5105 }
5106
5107 break;
5108 }
5109 case Intrinsic::fptrunc_round: {
5110 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5111 Depth, Q);
5112 break;
5113 }
5114 case Intrinsic::log:
5115 case Intrinsic::log10:
5116 case Intrinsic::log2:
5117 case Intrinsic::experimental_constrained_log:
5118 case Intrinsic::experimental_constrained_log10:
5119 case Intrinsic::experimental_constrained_log2: {
5120 // log(+inf) -> +inf
5121 // log([+-]0.0) -> -inf
5122 // log(-inf) -> nan
5123 // log(-x) -> nan
5124 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5125 break;
5126
5127 FPClassTest InterestedSrcs = InterestedClasses;
5128 if ((InterestedClasses & fcNegInf) != fcNone)
5129 InterestedSrcs |= fcZero | fcSubnormal;
5130 if ((InterestedClasses & fcNan) != fcNone)
5131 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5132
5133 KnownFPClass KnownSrc;
5134 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5135 KnownSrc, Depth + 1, Q);
5136
5137 if (KnownSrc.isKnownNeverPosInfinity())
5138 Known.knownNot(fcPosInf);
5139
5140 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5141 Known.knownNot(fcNan);
5142
5143 const Function *F = II->getFunction();
5144 if (F && KnownSrc.isKnownNeverLogicalZero(*F, II->getType()))
5145 Known.knownNot(fcNegInf);
5146
5147 break;
5148 }
5149 case Intrinsic::powi: {
5150 if ((InterestedClasses & fcNegative) == fcNone)
5151 break;
5152
5153 const Value *Exp = II->getArgOperand(1);
5154 Type *ExpTy = Exp->getType();
5155 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5156 KnownBits ExponentKnownBits(BitWidth);
5157 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5158 ExponentKnownBits, Depth + 1, Q);
5159
5160 if (ExponentKnownBits.Zero[0]) { // Is even
5161 Known.knownNot(fcNegative);
5162 break;
5163 }
5164
5165 // Given that exp is an integer, here are the
5166 // ways that pow can return a negative value:
5167 //
5168 // pow(-x, exp) --> negative if exp is odd and x is negative.
5169 // pow(-0, exp) --> -inf if exp is negative odd.
5170 // pow(-0, exp) --> -0 if exp is positive odd.
5171 // pow(-inf, exp) --> -0 if exp is negative odd.
5172 // pow(-inf, exp) --> -inf if exp is positive odd.
5173 KnownFPClass KnownSrc;
5174 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5175 KnownSrc, Depth + 1, Q);
5176 if (KnownSrc.isKnownNever(fcNegative))
5177 Known.knownNot(fcNegative);
5178 break;
5179 }
5180 case Intrinsic::ldexp: {
5181 KnownFPClass KnownSrc;
5182 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5183 KnownSrc, Depth + 1, Q);
5184 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5185
5186 // Sign is preserved, but underflows may produce zeroes.
5187 if (KnownSrc.isKnownNever(fcNegative))
5188 Known.knownNot(fcNegative);
5189 else if (KnownSrc.cannotBeOrderedLessThanZero())
5191
5192 if (KnownSrc.isKnownNever(fcPositive))
5193 Known.knownNot(fcPositive);
5194 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5196
5197 // Can refine inf/zero handling based on the exponent operand.
5198 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5199 if ((InterestedClasses & ExpInfoMask) == fcNone)
5200 break;
5201 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5202 break;
5203
5204 const fltSemantics &Flt =
5206 unsigned Precision = APFloat::semanticsPrecision(Flt);
5207 const Value *ExpArg = II->getArgOperand(1);
5209 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5210
5211 const int MantissaBits = Precision - 1;
5212 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5213 Known.knownNot(fcSubnormal);
5214
5215 const Function *F = II->getFunction();
5216 const APInt *ConstVal = ExpRange.getSingleElement();
5217 if (ConstVal && ConstVal->isZero()) {
5218 // ldexp(x, 0) -> x, so propagate everything.
5219 Known.propagateCanonicalizingSrc(KnownSrc, *F, II->getType());
5220 } else if (ExpRange.isAllNegative()) {
5221 // If we know the power is <= 0, can't introduce inf
5222 if (KnownSrc.isKnownNeverPosInfinity())
5223 Known.knownNot(fcPosInf);
5224 if (KnownSrc.isKnownNeverNegInfinity())
5225 Known.knownNot(fcNegInf);
5226 } else if (ExpRange.isAllNonNegative()) {
5227 // If we know the power is >= 0, can't introduce subnormal or zero
5228 if (KnownSrc.isKnownNeverPosSubnormal())
5229 Known.knownNot(fcPosSubnormal);
5230 if (KnownSrc.isKnownNeverNegSubnormal())
5231 Known.knownNot(fcNegSubnormal);
5232 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, II->getType()))
5233 Known.knownNot(fcPosZero);
5234 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))
5235 Known.knownNot(fcNegZero);
5236 }
5237
5238 break;
5239 }
5240 case Intrinsic::arithmetic_fence: {
5241 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5242 Known, Depth + 1, Q);
5243 break;
5244 }
5245 case Intrinsic::experimental_constrained_sitofp:
5246 case Intrinsic::experimental_constrained_uitofp:
5247 // Cannot produce nan
5248 Known.knownNot(fcNan);
5249
5250 // sitofp and uitofp turn into +0.0 for zero.
5251 Known.knownNot(fcNegZero);
5252
5253 // Integers cannot be subnormal
5254 Known.knownNot(fcSubnormal);
5255
5256 if (IID == Intrinsic::experimental_constrained_uitofp)
5257 Known.signBitMustBeZero();
5258
5259 // TODO: Copy inf handling from instructions
5260 break;
5261 default:
5262 break;
5263 }
5264
5265 break;
5266 }
5267 case Instruction::FAdd:
5268 case Instruction::FSub: {
5269 KnownFPClass KnownLHS, KnownRHS;
5270 bool WantNegative =
5271 Op->getOpcode() == Instruction::FAdd &&
5272 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5273 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5274 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5275
5276 if (!WantNaN && !WantNegative && !WantNegZero)
5277 break;
5278
5279 FPClassTest InterestedSrcs = InterestedClasses;
5280 if (WantNegative)
5281 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5282 if (InterestedClasses & fcNan)
5283 InterestedSrcs |= fcInf;
5284 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5285 KnownRHS, Depth + 1, Q);
5286
5287 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5288 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5289 WantNegZero || Opc == Instruction::FSub) {
5290
5291 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5292 // there's no point.
5293 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5294 KnownLHS, Depth + 1, Q);
5295 // Adding positive and negative infinity produces NaN.
5296 // TODO: Check sign of infinities.
5297 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5298 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5299 Known.knownNot(fcNan);
5300
5301 // FIXME: Context function should always be passed in separately
5302 const Function *F = cast<Instruction>(Op)->getFunction();
5303
5304 if (Op->getOpcode() == Instruction::FAdd) {
5305 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5306 KnownRHS.cannotBeOrderedLessThanZero())
5308 if (!F)
5309 break;
5310
5311 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5312 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5313 KnownRHS.isKnownNeverLogicalNegZero(*F, Op->getType())) &&
5314 // Make sure output negative denormal can't flush to -0
5315 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5316 Known.knownNot(fcNegZero);
5317 } else {
5318 if (!F)
5319 break;
5320
5321 // Only fsub -0, +0 can return -0
5322 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5323 KnownRHS.isKnownNeverLogicalPosZero(*F, Op->getType())) &&
5324 // Make sure output negative denormal can't flush to -0
5325 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5326 Known.knownNot(fcNegZero);
5327 }
5328 }
5329
5330 break;
5331 }
5332 case Instruction::FMul: {
5333 // X * X is always non-negative or a NaN.
5334 if (Op->getOperand(0) == Op->getOperand(1))
5335 Known.knownNot(fcNegative);
5336
5337 if ((InterestedClasses & fcNan) != fcNan)
5338 break;
5339
5340 // fcSubnormal is only needed in case of DAZ.
5341 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5342
5343 KnownFPClass KnownLHS, KnownRHS;
5344 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5345 Depth + 1, Q);
5346 if (!KnownRHS.isKnownNeverNaN())
5347 break;
5348
5349 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5350 Depth + 1, Q);
5351 if (!KnownLHS.isKnownNeverNaN())
5352 break;
5353
5354 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5355 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5356 Known.signBitMustBeZero();
5357 else
5358 Known.signBitMustBeOne();
5359 }
5360
5361 // If 0 * +/-inf produces NaN.
5362 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5363 Known.knownNot(fcNan);
5364 break;
5365 }
5366
5367 const Function *F = cast<Instruction>(Op)->getFunction();
5368 if (!F)
5369 break;
5370
5371 if ((KnownRHS.isKnownNeverInfinity() ||
5372 KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) &&
5373 (KnownLHS.isKnownNeverInfinity() ||
5374 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))
5375 Known.knownNot(fcNan);
5376
5377 break;
5378 }
5379 case Instruction::FDiv:
5380 case Instruction::FRem: {
5381 if (Op->getOperand(0) == Op->getOperand(1)) {
5382 // TODO: Could filter out snan if we inspect the operand
5383 if (Op->getOpcode() == Instruction::FDiv) {
5384 // X / X is always exactly 1.0 or a NaN.
5386 } else {
5387 // X % X is always exactly [+-]0.0 or a NaN.
5388 Known.KnownFPClasses = fcNan | fcZero;
5389 }
5390
5391 break;
5392 }
5393
5394 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5395 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5396 const bool WantPositive =
5397 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5398 if (!WantNan && !WantNegative && !WantPositive)
5399 break;
5400
5401 KnownFPClass KnownLHS, KnownRHS;
5402
5403 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5404 fcNan | fcInf | fcZero | fcNegative, KnownRHS,
5405 Depth + 1, Q);
5406
5407 bool KnowSomethingUseful =
5408 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5409
5410 if (KnowSomethingUseful || WantPositive) {
5411 const FPClassTest InterestedLHS =
5412 WantPositive ? fcAllFlags
5414
5415 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5416 InterestedClasses & InterestedLHS, KnownLHS,
5417 Depth + 1, Q);
5418 }
5419
5420 const Function *F = cast<Instruction>(Op)->getFunction();
5421
5422 if (Op->getOpcode() == Instruction::FDiv) {
5423 // Only 0/0, Inf/Inf produce NaN.
5424 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5425 (KnownLHS.isKnownNeverInfinity() ||
5426 KnownRHS.isKnownNeverInfinity()) &&
5427 ((F && KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) ||
5428 (F && KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))) {
5429 Known.knownNot(fcNan);
5430 }
5431
5432 // X / -0.0 is -Inf (or NaN).
5433 // +X / +X is +X
5434 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5435 Known.knownNot(fcNegative);
5436 } else {
5437 // Inf REM x and x REM 0 produce NaN.
5438 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5439 KnownLHS.isKnownNeverInfinity() && F &&
5440 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())) {
5441 Known.knownNot(fcNan);
5442 }
5443
5444 // The sign for frem is the same as the first operand.
5445 if (KnownLHS.cannotBeOrderedLessThanZero())
5447 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5449
5450 // See if we can be more aggressive about the sign of 0.
5451 if (KnownLHS.isKnownNever(fcNegative))
5452 Known.knownNot(fcNegative);
5453 if (KnownLHS.isKnownNever(fcPositive))
5454 Known.knownNot(fcPositive);
5455 }
5456
5457 break;
5458 }
5459 case Instruction::FPExt: {
5460 // Infinity, nan and zero propagate from source.
5461 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5462 Known, Depth + 1, Q);
5463
5464 const fltSemantics &DstTy =
5465 Op->getType()->getScalarType()->getFltSemantics();
5466 const fltSemantics &SrcTy =
5467 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5468
5469 // All subnormal inputs should be in the normal range in the result type.
5470 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5471 if (Known.KnownFPClasses & fcPosSubnormal)
5472 Known.KnownFPClasses |= fcPosNormal;
5473 if (Known.KnownFPClasses & fcNegSubnormal)
5474 Known.KnownFPClasses |= fcNegNormal;
5475 Known.knownNot(fcSubnormal);
5476 }
5477
5478 // Sign bit of a nan isn't guaranteed.
5479 if (!Known.isKnownNeverNaN())
5480 Known.SignBit = std::nullopt;
5481 break;
5482 }
5483 case Instruction::FPTrunc: {
5484 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5485 Depth, Q);
5486 break;
5487 }
5488 case Instruction::SIToFP:
5489 case Instruction::UIToFP: {
5490 // Cannot produce nan
5491 Known.knownNot(fcNan);
5492
5493 // Integers cannot be subnormal
5494 Known.knownNot(fcSubnormal);
5495
5496 // sitofp and uitofp turn into +0.0 for zero.
5497 Known.knownNot(fcNegZero);
5498 if (Op->getOpcode() == Instruction::UIToFP)
5499 Known.signBitMustBeZero();
5500
5501 if (InterestedClasses & fcInf) {
5502 // Get width of largest magnitude integer (remove a bit if signed).
5503 // This still works for a signed minimum value because the largest FP
5504 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5505 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5506 if (Op->getOpcode() == Instruction::SIToFP)
5507 --IntSize;
5508
5509 // If the exponent of the largest finite FP value can hold the largest
5510 // integer, the result of the cast must be finite.
5511 Type *FPTy = Op->getType()->getScalarType();
5512 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5513 Known.knownNot(fcInf);
5514 }
5515
5516 break;
5517 }
5518 case Instruction::ExtractElement: {
5519 // Look through extract element. If the index is non-constant or
5520 // out-of-range demand all elements, otherwise just the extracted element.
5521 const Value *Vec = Op->getOperand(0);
5522 const Value *Idx = Op->getOperand(1);
5523 auto *CIdx = dyn_cast<ConstantInt>(Idx);
5524
5525 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5526 unsigned NumElts = VecTy->getNumElements();
5527 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
5528 if (CIdx && CIdx->getValue().ult(NumElts))
5529 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5530 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5531 Depth + 1, Q);
5532 }
5533
5534 break;
5535 }
5536 case Instruction::InsertElement: {
5537 if (isa<ScalableVectorType>(Op->getType()))
5538 return;
5539
5540 const Value *Vec = Op->getOperand(0);
5541 const Value *Elt = Op->getOperand(1);
5542 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5543 unsigned NumElts = DemandedElts.getBitWidth();
5544 APInt DemandedVecElts = DemandedElts;
5545 bool NeedsElt = true;
5546 // If we know the index we are inserting to, clear it from Vec check.
5547 if (CIdx && CIdx->getValue().ult(NumElts)) {
5548 DemandedVecElts.clearBit(CIdx->getZExtValue());
5549 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5550 }
5551
5552 // Do we demand the inserted element?
5553 if (NeedsElt) {
5554 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1, Q);
5555 // If we don't know any bits, early out.
5556 if (Known.isUnknown())
5557 break;
5558 } else {
5559 Known.KnownFPClasses = fcNone;
5560 }
5561
5562 // Do we need anymore elements from Vec?
5563 if (!DemandedVecElts.isZero()) {
5564 KnownFPClass Known2;
5565 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
5566 Depth + 1, Q);
5567 Known |= Known2;
5568 }
5569
5570 break;
5571 }
5572 case Instruction::ShuffleVector: {
5573 // For undef elements, we don't know anything about the common state of
5574 // the shuffle result.
5575 APInt DemandedLHS, DemandedRHS;
5576 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5577 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5578 return;
5579
5580 if (!!DemandedLHS) {
5581 const Value *LHS = Shuf->getOperand(0);
5582 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
5583 Depth + 1, Q);
5584
5585 // If we don't know any bits, early out.
5586 if (Known.isUnknown())
5587 break;
5588 } else {
5589 Known.KnownFPClasses = fcNone;
5590 }
5591
5592 if (!!DemandedRHS) {
5593 KnownFPClass Known2;
5594 const Value *RHS = Shuf->getOperand(1);
5595 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
5596 Depth + 1, Q);
5597 Known |= Known2;
5598 }
5599
5600 break;
5601 }
5602 case Instruction::ExtractValue: {
5603 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5604 ArrayRef<unsigned> Indices = Extract->getIndices();
5605 const Value *Src = Extract->getAggregateOperand();
5606 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5607 Indices[0] == 0) {
5608 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5609 switch (II->getIntrinsicID()) {
5610 case Intrinsic::frexp: {
5611 Known.knownNot(fcSubnormal);
5612
5613 KnownFPClass KnownSrc;
5614 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5615 InterestedClasses, KnownSrc, Depth + 1, Q);
5616
5617 const Function *F = cast<Instruction>(Op)->getFunction();
5618
5619 if (KnownSrc.isKnownNever(fcNegative))
5620 Known.knownNot(fcNegative);
5621 else {
5622 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, Op->getType()))
5623 Known.knownNot(fcNegZero);
5624 if (KnownSrc.isKnownNever(fcNegInf))
5625 Known.knownNot(fcNegInf);
5626 }
5627
5628 if (KnownSrc.isKnownNever(fcPositive))
5629 Known.knownNot(fcPositive);
5630 else {
5631 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, Op->getType()))
5632 Known.knownNot(fcPosZero);
5633 if (KnownSrc.isKnownNever(fcPosInf))
5634 Known.knownNot(fcPosInf);
5635 }
5636
5637 Known.propagateNaN(KnownSrc);
5638 return;
5639 }
5640 default:
5641 break;
5642 }
5643 }
5644 }
5645
5646 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1,
5647 Q);
5648 break;
5649 }
5650 case Instruction::PHI: {
5651 const PHINode *P = cast<PHINode>(Op);
5652 // Unreachable blocks may have zero-operand PHI nodes.
5653 if (P->getNumIncomingValues() == 0)
5654 break;
5655
5656 // Otherwise take the unions of the known bit sets of the operands,
5657 // taking conservative care to avoid excessive recursion.
5658 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5659
5660 if (Depth < PhiRecursionLimit) {
5661 // Skip if every incoming value references to ourself.
5662 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5663 break;
5664
5665 bool First = true;
5666
5667 for (const Use &U : P->operands()) {
5668 Value *IncValue = U.get();
5669 // Skip direct self references.
5670 if (IncValue == P)
5671 continue;
5672
5673 KnownFPClass KnownSrc;
5674 // Recurse, but cap the recursion to two levels, because we don't want
5675 // to waste time spinning around in loops. We need at least depth 2 to
5676 // detect known sign bits.
5678 IncValue, DemandedElts, InterestedClasses, KnownSrc,
5679 PhiRecursionLimit,
5680 Q.getWithInstruction(P->getIncomingBlock(U)->getTerminator()));
5681
5682 if (First) {
5683 Known = KnownSrc;
5684 First = false;
5685 } else {
5686 Known |= KnownSrc;
5687 }
5688
5689 if (Known.KnownFPClasses == fcAllFlags)
5690 break;
5691 }
5692 }
5693
5694 break;
5695 }
5696 default:
5697 break;
5698 }
5699}
5700
5702 const APInt &DemandedElts,
5703 FPClassTest InterestedClasses,
5704 unsigned Depth,
5705 const SimplifyQuery &SQ) {
5706 KnownFPClass KnownClasses;
5707 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, Depth,
5708 SQ);
5709 return KnownClasses;
5710}
5711
5713 FPClassTest InterestedClasses,
5714 unsigned Depth,
5715 const SimplifyQuery &SQ) {
5716 KnownFPClass Known;
5717 ::computeKnownFPClass(V, Known, InterestedClasses, Depth, SQ);
5718 return Known;
5719}
5720
5722
5723 // All byte-wide stores are splatable, even of arbitrary variables.
5724 if (V->getType()->isIntegerTy(8))
5725 return V;
5726
5727 LLVMContext &Ctx = V->getContext();
5728
5729 // Undef don't care.
5730 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
5731 if (isa<UndefValue>(V))
5732 return UndefInt8;
5733
5734 // Return Undef for zero-sized type.
5735 if (DL.getTypeStoreSize(V->getType()).isZero())
5736 return UndefInt8;
5737
5738 Constant *C = dyn_cast<Constant>(V);
5739 if (!C) {
5740 // Conceptually, we could handle things like:
5741 // %a = zext i8 %X to i16
5742 // %b = shl i16 %a, 8
5743 // %c = or i16 %a, %b
5744 // but until there is an example that actually needs this, it doesn't seem
5745 // worth worrying about.
5746 return nullptr;
5747 }
5748
5749 // Handle 'null' ConstantArrayZero etc.
5750 if (C->isNullValue())
5752
5753 // Constant floating-point values can be handled as integer values if the
5754 // corresponding integer value is "byteable". An important case is 0.0.
5755 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
5756 Type *Ty = nullptr;
5757 if (CFP->getType()->isHalfTy())
5758 Ty = Type::getInt16Ty(Ctx);
5759 else if (CFP->getType()->isFloatTy())
5760 Ty = Type::getInt32Ty(Ctx);
5761 else if (CFP->getType()->isDoubleTy())
5762 Ty = Type::getInt64Ty(Ctx);
5763 // Don't handle long double formats, which have strange constraints.
5764 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
5765 : nullptr;
5766 }
5767
5768 // We can handle constant integers that are multiple of 8 bits.
5769 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
5770 if (CI->getBitWidth() % 8 == 0) {
5771 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
5772 if (!CI->getValue().isSplat(8))
5773 return nullptr;
5774 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
5775 }
5776 }
5777
5778 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
5779 if (CE->getOpcode() == Instruction::IntToPtr) {
5780 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
5781 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
5783 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
5784 return isBytewiseValue(Op, DL);
5785 }
5786 }
5787 }
5788
5789 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
5790 if (LHS == RHS)
5791 return LHS;
5792 if (!LHS || !RHS)
5793 return nullptr;
5794 if (LHS == UndefInt8)
5795 return RHS;
5796 if (RHS == UndefInt8)
5797 return LHS;
5798 return nullptr;
5799 };
5800
5801 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
5802 Value *Val = UndefInt8;
5803 for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
5804 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
5805 return nullptr;
5806 return Val;
5807 }
5808
5809 if (isa<ConstantAggregate>(C)) {
5810 Value *Val = UndefInt8;
5811 for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
5812 if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I), DL))))
5813 return nullptr;
5814 return Val;
5815 }
5816
5817 // Don't try to handle the handful of other constants.
5818 return nullptr;
5819}
5820
5821// This is the recursive version of BuildSubAggregate. It takes a few different
5822// arguments. Idxs is the index within the nested struct From that we are
5823// looking at now (which is of type IndexedType). IdxSkip is the number of
5824// indices from Idxs that should be left out when inserting into the resulting
5825// struct. To is the result struct built so far, new insertvalue instructions
5826// build on that.
5827static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
5829 unsigned IdxSkip,
5830 BasicBlock::iterator InsertBefore) {
5831 StructType *STy = dyn_cast<StructType>(IndexedType);
5832 if (STy) {
5833 // Save the original To argument so we can modify it
5834 Value *OrigTo = To;
5835 // General case, the type indexed by Idxs is a struct
5836 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
5837 // Process each struct element recursively
5838 Idxs.push_back(i);
5839 Value *PrevTo = To;
5840 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
5841 InsertBefore);
5842 Idxs.pop_back();
5843 if (!To) {
5844 // Couldn't find any inserted value for this index? Cleanup
5845 while (PrevTo != OrigTo) {
5846 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
5847 PrevTo = Del->getAggregateOperand();
5848 Del->eraseFromParent();
5849 }
5850 // Stop processing elements
5851 break;
5852 }
5853 }
5854 // If we successfully found a value for each of our subaggregates
5855 if (To)
5856 return To;
5857 }
5858 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
5859 // the struct's elements had a value that was inserted directly. In the latter
5860 // case, perhaps we can't determine each of the subelements individually, but
5861 // we might be able to find the complete struct somewhere.
5862
5863 // Find the value that is at that particular spot
5864 Value *V = FindInsertedValue(From, Idxs);
5865
5866 if (!V)
5867 return nullptr;
5868
5869 // Insert the value in the new (sub) aggregate
5870 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
5871 InsertBefore);
5872}
5873
5874// This helper takes a nested struct and extracts a part of it (which is again a
5875// struct) into a new value. For example, given the struct:
5876// { a, { b, { c, d }, e } }
5877// and the indices "1, 1" this returns
5878// { c, d }.
5879//
5880// It does this by inserting an insertvalue for each element in the resulting
5881// struct, as opposed to just inserting a single struct. This will only work if
5882// each of the elements of the substruct are known (ie, inserted into From by an
5883// insertvalue instruction somewhere).
5884//
5885// All inserted insertvalue instructions are inserted before InsertBefore
5887 BasicBlock::iterator InsertBefore) {
5888 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
5889 idx_range);
5890 Value *To = PoisonValue::get(IndexedType);
5891 SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
5892 unsigned IdxSkip = Idxs.size();
5893
5894 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
5895}
5896
5897/// Given an aggregate and a sequence of indices, see if the scalar value
5898/// indexed is already around as a register, for example if it was inserted
5899/// directly into the aggregate.
5900///
5901/// If InsertBefore is not null, this function will duplicate (modified)
5902/// insertvalues when a part of a nested struct is extracted.
5903Value *
5905 std::optional<BasicBlock::iterator> InsertBefore) {
5906 // Nothing to index? Just return V then (this is useful at the end of our
5907 // recursion).
5908 if (idx_range.empty())
5909 return V;
5910 // We have indices, so V should have an indexable type.
5911 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
5912 "Not looking at a struct or array?");
5913 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
5914 "Invalid indices for type?");
5915
5916 if (Constant *C = dyn_cast<Constant>(V)) {
5917 C = C->getAggregateElement(idx_range[0]);
5918 if (!C) return nullptr;
5919 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
5920 }
5921
5922 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
5923 // Loop the indices for the insertvalue instruction in parallel with the
5924 // requested indices
5925 const unsigned *req_idx = idx_range.begin();
5926 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
5927 i != e; ++i, ++req_idx) {
5928 if (req_idx == idx_range.end()) {
5929 // We can't handle this without inserting insertvalues
5930 if (!InsertBefore)
5931 return nullptr;
5932
5933 // The requested index identifies a part of a nested aggregate. Handle
5934 // this specially. For example,
5935 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
5936 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
5937 // %C = extractvalue {i32, { i32, i32 } } %B, 1
5938 // This can be changed into
5939 // %A = insertvalue {i32, i32 } undef, i32 10, 0
5940 // %C = insertvalue {i32, i32 } %A, i32 11, 1
5941 // which allows the unused 0,0 element from the nested struct to be
5942 // removed.
5943 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
5944 *InsertBefore);
5945 }
5946
5947 // This insert value inserts something else than what we are looking for.
5948 // See if the (aggregate) value inserted into has the value we are
5949 // looking for, then.
5950 if (*req_idx != *i)
5951 return FindInsertedValue(I->getAggregateOperand(), idx_range,
5952 InsertBefore);
5953 }
5954 // If we end up here, the indices of the insertvalue match with those
5955 // requested (though possibly only partially). Now we recursively look at
5956 // the inserted value, passing any remaining indices.
5957 return FindInsertedValue(I->getInsertedValueOperand(),
5958 ArrayRef(req_idx, idx_range.end()), InsertBefore);
5959 }
5960
5961 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
5962 // If we're extracting a value from an aggregate that was extracted from
5963 // something else, we can extract from that something else directly instead.
5964 // However, we will need to chain I's indices with the requested indices.
5965
5966 // Calculate the number of indices required
5967 unsigned size = I->getNumIndices() + idx_range.size();
5968 // Allocate some space to put the new indices in
5970 Idxs.reserve(size);
5971 // Add indices from the extract value instruction
5972 Idxs.append(I->idx_begin(), I->idx_end());
5973
5974 // Add requested indices
5975 Idxs.append(idx_range.begin(), idx_range.end());
5976
5977 assert(Idxs.size() == size
5978 && "Number of indices added not correct?");
5979
5980 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
5981 }
5982 // Otherwise, we don't know (such as, extracting from a function return value
5983 // or load instruction)
5984 return nullptr;
5985}
5986
5988 unsigned CharSize) {
5989 // Make sure the GEP has exactly three arguments.
5990 if (GEP->getNumOperands() != 3)
5991 return false;
5992
5993 // Make sure the index-ee is a pointer to array of \p CharSize integers.
5994 // CharSize.
5995 ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
5996 if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
5997 return false;
5998
5999 // Check to make sure that the first operand of the GEP is an integer and
6000 // has value 0 so that we are sure we're indexing into the initializer.
6001 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
6002 if (!FirstIdx || !FirstIdx->isZero())
6003 return false;
6004
6005 return true;
6006}
6007
6008// If V refers to an initialized global constant, set Slice either to
6009// its initializer if the size of its elements equals ElementSize, or,
6010// for ElementSize == 8, to its representation as an array of unsiged
6011// char. Return true on success.
6012// Offset is in the unit "nr of ElementSize sized elements".
6015 unsigned ElementSize, uint64_t Offset) {
6016 assert(V && "V should not be null.");
6017 assert((ElementSize % 8) == 0 &&
6018 "ElementSize expected to be a multiple of the size of a byte.");
6019 unsigned ElementSizeInBytes = ElementSize / 8;
6020
6021 // Drill down into the pointer expression V, ignoring any intervening
6022 // casts, and determine the identity of the object it references along
6023 // with the cumulative byte offset into it.
6024 const GlobalVariable *GV =
6025 dyn_cast<GlobalVariable>(getUnderlyingObject(V));
6026 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6027 // Fail if V is not based on constant global object.
6028 return false;
6029
6030 const DataLayout &DL = GV->getParent()->getDataLayout();
6031 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6032
6033 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6034 /*AllowNonInbounds*/ true))
6035 // Fail if a constant offset could not be determined.
6036 return false;
6037
6038 uint64_t StartIdx = Off.getLimitedValue();
6039 if (StartIdx == UINT64_MAX)
6040 // Fail if the constant offset is excessive.
6041 return false;
6042
6043 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6044 // elements. Simply bail out if that isn't possible.
6045 if ((StartIdx % ElementSizeInBytes) != 0)
6046 return false;
6047
6048 Offset += StartIdx / ElementSizeInBytes;
6049 ConstantDataArray *Array = nullptr;
6050 ArrayType *ArrayTy = nullptr;
6051
6052 if (GV->getInitializer()->isNullValue()) {
6053 Type *GVTy = GV->getValueType();
6054 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6055 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6056
6057 Slice.Array = nullptr;
6058 Slice.Offset = 0;
6059 // Return an empty Slice for undersized constants to let callers
6060 // transform even undefined library calls into simpler, well-defined
6061 // expressions. This is preferable to making the calls although it
6062 // prevents sanitizers from detecting such calls.
6063 Slice.Length = Length < Offset ? 0 : Length - Offset;
6064 return true;
6065 }
6066
6067 auto *Init = const_cast<Constant *>(GV->getInitializer());
6068 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6069 Type *InitElTy = ArrayInit->getElementType();
6070 if (InitElTy->isIntegerTy(ElementSize)) {
6071 // If Init is an initializer for an array of the expected type
6072 // and size, use it as is.
6073 Array = ArrayInit;
6074 ArrayTy = ArrayInit->getType();
6075 }
6076 }
6077
6078 if (!Array) {
6079 if (ElementSize != 8)
6080 // TODO: Handle conversions to larger integral types.
6081 return false;
6082
6083 // Otherwise extract the portion of the initializer starting
6084 // at Offset as an array of bytes, and reset Offset.
6086 if (!Init)
6087 return false;
6088
6089 Offset = 0;
6090 Array = dyn_cast<ConstantDataArray>(Init);
6091 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6092 }
6093
6094 uint64_t NumElts = ArrayTy->getArrayNumElements();
6095 if (Offset > NumElts)
6096 return false;
6097
6098 Slice.Array = Array;
6099 Slice.Offset = Offset;
6100 Slice.Length = NumElts - Offset;
6101 return true;
6102}
6103
6104/// Extract bytes from the initializer of the constant array V, which need
6105/// not be a nul-terminated string. On success, store the bytes in Str and
6106/// return true. When TrimAtNul is set, Str will contain only the bytes up
6107/// to but not including the first nul. Return false on failure.
6109 bool TrimAtNul) {
6111 if (!getConstantDataArrayInfo(V, Slice, 8))
6112 return false;
6113
6114 if (Slice.Array == nullptr) {
6115 if (TrimAtNul) {
6116 // Return a nul-terminated string even for an empty Slice. This is
6117 // safe because all existing SimplifyLibcalls callers require string
6118 // arguments and the behavior of the functions they fold is undefined
6119 // otherwise. Folding the calls this way is preferable to making
6120 // the undefined library calls, even though it prevents sanitizers
6121 // from reporting such calls.
6122 Str = StringRef();
6123 return true;
6124 }
6125 if (Slice.Length == 1) {
6126 Str = StringRef("", 1);
6127 return true;
6128 }
6129 // We cannot instantiate a StringRef as we do not have an appropriate string
6130 // of 0s at hand.
6131 return false;
6132 }
6133
6134 // Start out with the entire array in the StringRef.
6135 Str = Slice.Array->getAsString();
6136 // Skip over 'offset' bytes.
6137 Str = Str.substr(Slice.Offset);
6138
6139 if (TrimAtNul) {
6140 // Trim off the \0 and anything after it. If the array is not nul
6141 // terminated, we just return the whole end of string. The client may know
6142 // some other way that the string is length-bound.
6143 Str = Str.substr(0, Str.find('\0'));
6144 }
6145 return true;
6146}
6147
6148// These next two are very similar to the above, but also look through PHI
6149// nodes.
6150// TODO: See if we can integrate these two together.
6151
6152/// If we can compute the length of the string pointed to by
6153/// the specified pointer, return 'len+1'. If we can't, return 0.
6156 unsigned CharSize) {
6157 // Look through noop bitcast instructions.
6158 V = V->stripPointerCasts();
6159
6160 // If this is a PHI node, there are two cases: either we have already seen it
6161 // or we haven't.
6162 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6163 if (!PHIs.insert(PN).second)
6164 return ~0ULL; // already in the set.
6165
6166 // If it was new, see if all the input strings are the same length.
6167 uint64_t LenSoFar = ~0ULL;
6168 for (Value *IncValue : PN->incoming_values()) {
6169 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6170 if (Len == 0) return 0; // Unknown length -> unknown.
6171
6172 if (Len == ~0ULL) continue;
6173
6174 if (Len != LenSoFar && LenSoFar != ~0ULL)
6175 return 0; // Disagree -> unknown.
6176 LenSoFar = Len;
6177 }
6178
6179 // Success, all agree.
6180 return LenSoFar;
6181 }
6182
6183 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6184 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6185 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6186 if (Len1 == 0) return 0;
6187 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6188 if (Len2 == 0) return 0;
6189 if (Len1 == ~0ULL) return Len2;
6190 if (Len2 == ~0ULL) return Len1;
6191 if (Len1 != Len2) return 0;
6192 return Len1;
6193 }
6194
6195 // Otherwise, see if we can read the string.
6197 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6198 return 0;
6199
6200 if (Slice.Array == nullptr)
6201 // Zeroinitializer (including an empty one).
6202 return 1;
6203
6204 // Search for the first nul character. Return a conservative result even
6205 // when there is no nul. This is safe since otherwise the string function
6206 // being folded such as strlen is undefined, and can be preferable to
6207 // making the undefined library call.
6208 unsigned NullIndex = 0;
6209 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6210 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6211 break;
6212 }
6213
6214 return NullIndex + 1;
6215}
6216
6217/// If we can compute the length of the string pointed to by
6218/// the specified pointer, return 'len+1'. If we can't, return 0.
6219uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6220 if (!V->getType()->isPointerTy())
6221 return 0;
6222
6224 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6225 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6226 // an empty string as a length.
6227 return Len == ~0ULL ? 1 : Len;
6228}
6229
6230const Value *
6232 bool MustPreserveNullness) {
6233 assert(Call &&
6234 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6235 if (const Value *RV = Call->getReturnedArgOperand())
6236 return RV;
6237 // This can be used only as a aliasing property.
6239 Call, MustPreserveNullness))
6240 return Call->getArgOperand(0);
6241 return nullptr;
6242}
6243
6245 const CallBase *Call, bool MustPreserveNullness) {
6246 switch (Call->getIntrinsicID()) {
6247 case Intrinsic::launder_invariant_group:
6248 case Intrinsic::strip_invariant_group:
6249 case Intrinsic::aarch64_irg:
6250 case Intrinsic::aarch64_tagp:
6251 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6252 // input pointer (and thus preserve null-ness for the purposes of escape
6253 // analysis, which is where the MustPreserveNullness flag comes in to play).
6254 // However, it will not necessarily map ptr addrspace(N) null to ptr
6255 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6256 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6257 // list, no one should be relying on such a strict interpretation of
6258 // MustPreserveNullness (and, at time of writing, they are not), but we
6259 // document this fact out of an abundance of caution.
6260 case Intrinsic::amdgcn_make_buffer_rsrc:
6261 return true;
6262 case Intrinsic::ptrmask:
6263 return !MustPreserveNullness;
6264 case Intrinsic::threadlocal_address:
6265 // The underlying variable changes with thread ID. The Thread ID may change
6266 // at coroutine suspend points.
6267 return !Call->getParent()->getParent()->isPresplitCoroutine();
6268 default:
6269 return false;
6270 }
6271}
6272
6273/// \p PN defines a loop-variant pointer to an object. Check if the
6274/// previous iteration of the loop was referring to the same object as \p PN.
6276 const LoopInfo *LI) {
6277 // Find the loop-defined value.
6278 Loop *L = LI->getLoopFor(PN->getParent());
6279 if (PN->getNumIncomingValues() != 2)
6280 return true;
6281
6282 // Find the value from previous iteration.
6283 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6284 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6285 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6286 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6287 return true;
6288
6289 // If a new pointer is loaded in the loop, the pointer references a different
6290 // object in every iteration. E.g.:
6291 // for (i)
6292 // int *p = a[i];
6293 // ...
6294 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6295 if (!L->isLoopInvariant(Load->getPointerOperand()))
6296 return false;
6297 return true;
6298}
6299
6300const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6301 if (!V->getType()->isPointerTy())
6302 return V;
6303 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6304 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6305 V = GEP->getPointerOperand();
6306 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6307 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6308 V = cast<Operator>(V)->getOperand(0);
6309 if (!V->getType()->isPointerTy())
6310 return V;
6311 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6312 if (GA->isInterposable())
6313 return V;
6314 V = GA->getAliasee();
6315 } else {
6316 if (auto *PHI = dyn_cast<PHINode>(V)) {
6317 // Look through single-arg phi nodes created by LCSSA.
6318 if (PHI->getNumIncomingValues() == 1) {
6319 V = PHI->getIncomingValue(0);
6320 continue;
6321 }
6322 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6323 // CaptureTracking can know about special capturing properties of some
6324 // intrinsics like launder.invariant.group, that can't be expressed with
6325 // the attributes, but have properties like returning aliasing pointer.
6326 // Because some analysis may assume that nocaptured pointer is not
6327 // returned from some special intrinsic (because function would have to
6328 // be marked with returns attribute), it is crucial to use this function
6329 // because it should be in sync with CaptureTracking. Not using it may
6330 // cause weird miscompilations where 2 aliasing pointers are assumed to
6331 // noalias.
6332 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6333 V = RP;
6334 continue;
6335 }
6336 }
6337
6338 return V;
6339 }
6340 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6341 }
6342 return V;
6343}
6344
6347 LoopInfo *LI, unsigned MaxLookup) {
6350 Worklist.push_back(V);
6351 do {
6352 const Value *P = Worklist.pop_back_val();
6353 P = getUnderlyingObject(P, MaxLookup);
6354
6355 if (!Visited.insert(P).second)
6356 continue;
6357
6358 if (auto *SI = dyn_cast<SelectInst>(P)) {
6359 Worklist.push_back(SI->getTrueValue());
6360 Worklist.push_back(SI->getFalseValue());
6361 continue;
6362 }
6363
6364 if (auto *PN = dyn_cast<PHINode>(P)) {
6365 // If this PHI changes the underlying object in every iteration of the
6366 // loop, don't look through it. Consider:
6367 // int **A;
6368 // for (i) {
6369 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6370 // Curr = A[i];
6371 // *Prev, *Curr;
6372 //
6373 // Prev is tracking Curr one iteration behind so they refer to different
6374 // underlying objects.
6375 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6377 append_range(Worklist, PN->incoming_values());
6378 else
6379 Objects.push_back(P);
6380 continue;
6381 }
6382
6383 Objects.push_back(P);
6384 } while (!Worklist.empty());
6385}
6386
6387/// This is the function that does the work of looking through basic
6388/// ptrtoint+arithmetic+inttoptr sequences.
6389static const Value *getUnderlyingObjectFromInt(const Value *V) {
6390 do {
6391 if (const Operator *U = dyn_cast<Operator>(V)) {
6392 // If we find a ptrtoint, we can transfer control back to the
6393 // regular getUnderlyingObjectFromInt.
6394 if (U->getOpcode() == Instruction::PtrToInt)
6395 return U->getOperand(0);
6396 // If we find an add of a constant, a multiplied value, or a phi, it's
6397 // likely that the other operand will lead us to the base
6398 // object. We don't have to worry about the case where the
6399 // object address is somehow being computed by the multiply,
6400 // because our callers only care when the result is an
6401 // identifiable object.
6402 if (U->getOpcode() != Instruction::Add ||
6403 (!isa<ConstantInt>(U->getOperand(1)) &&
6404 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6405 !isa<PHINode>(U->getOperand(1))))
6406 return V;
6407 V = U->getOperand(0);
6408 } else {
6409 return V;
6410 }
6411 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6412 } while (true);
6413}
6414
6415/// This is a wrapper around getUnderlyingObjects and adds support for basic
6416/// ptrtoint+arithmetic+inttoptr sequences.
6417/// It returns false if unidentified object is found in getUnderlyingObjects.
6419 SmallVectorImpl<Value *> &Objects) {
6421 SmallVector<const Value *, 4> Working(1, V);
6422 do {
6423 V = Working.pop_back_val();
6424
6426 getUnderlyingObjects(V, Objs);
6427
6428 for (const Value *V : Objs) {
6429 if (!Visited.insert(V).second)
6430 continue;
6431 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6432 const Value *O =
6433 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6434 if (O->getType()->isPointerTy()) {
6435 Working.push_back(O);
6436 continue;
6437 }
6438 }
6439 // If getUnderlyingObjects fails to find an identifiable object,
6440 // getUnderlyingObjectsForCodeGen also fails for safety.
6441 if (!isIdentifiedObject(V)) {
6442 Objects.clear();
6443 return false;
6444 }
6445 Objects.push_back(const_cast<Value *>(V));
6446 }
6447 } while (!Working.empty());
6448 return true;
6449}
6450
6452 AllocaInst *Result = nullptr;
6454 SmallVector<Value *, 4> Worklist;
6455
6456 auto AddWork = [&](Value *V) {
6457 if (Visited.insert(V).second)
6458 Worklist.push_back(V);
6459 };
6460
6461 AddWork(V);
6462 do {
6463 V = Worklist.pop_back_val();
6464 assert(Visited.count(V));
6465
6466 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6467 if (Result && Result != AI)
6468 return nullptr;
6469 Result = AI;
6470 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
6471 AddWork(CI->getOperand(0));
6472 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
6473 for (Value *IncValue : PN->incoming_values())
6474 AddWork(IncValue);
6475 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
6476 AddWork(SI->getTrueValue());
6477 AddWork(SI->getFalseValue());
6478 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
6479 if (OffsetZero && !GEP->hasAllZeroIndices())
6480 return nullptr;
6481 AddWork(GEP->getPointerOperand());
6482 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
6483 Value *Returned = CB->getReturnedArgOperand();
6484 if (Returned)
6485 AddWork(Returned);
6486 else
6487 return nullptr;
6488 } else {
6489 return nullptr;
6490 }
6491 } while (!Worklist.empty());
6492
6493 return Result;
6494}
6495
6497 const Value *V, bool AllowLifetime, bool AllowDroppable) {
6498 for (const User *U : V->users()) {
6499 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
6500 if (!II)
6501 return false;
6502
6503 if (AllowLifetime && II->isLifetimeStartOrEnd())
6504 continue;
6505
6506 if (AllowDroppable && II->isDroppable())
6507 continue;
6508
6509 return false;
6510 }
6511 return true;
6512}
6513
6516 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
6517}
6520 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
6521}
6522
6524 if (!LI.isUnordered())
6525 return true;
6526 const Function &F = *LI.getFunction();
6527 // Speculative load may create a race that did not exist in the source.
6528 return F.hasFnAttribute(Attribute::SanitizeThread) ||
6529 // Speculative load may load data from dirty regions.
6530 F.hasFnAttribute(Attribute::SanitizeAddress) ||
6531 F.hasFnAttribute(Attribute::SanitizeHWAddress);
6532}
6533
6535 const Instruction *CtxI,
6536 AssumptionCache *AC,
6537 const DominatorTree *DT,
6538 const TargetLibraryInfo *TLI) {
6539 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
6540 AC, DT, TLI);
6541}
6542
6544 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
6545 AssumptionCache *AC, const DominatorTree *DT,
6546 const TargetLibraryInfo *TLI) {
6547#ifndef NDEBUG
6548 if (Inst->getOpcode() != Opcode) {
6549 // Check that the operands are actually compatible with the Opcode override.
6550 auto hasEqualReturnAndLeadingOperandTypes =
6551 [](const Instruction *Inst, unsigned NumLeadingOperands) {
6552 if (Inst->getNumOperands() < NumLeadingOperands)
6553 return false;
6554 const Type *ExpectedType = Inst->getType();
6555 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
6556 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
6557 return false;
6558 return true;
6559 };
6561 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
6562 assert(!Instruction::isUnaryOp(Opcode) ||
6563 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
6564 }
6565#endif
6566
6567 switch (Opcode) {
6568 default:
6569 return true;
6570 case Instruction::UDiv:
6571 case Instruction::URem: {
6572 // x / y is undefined if y == 0.
6573 const APInt *V;
6574 if (match(Inst->getOperand(1), m_APInt(V)))
6575 return *V != 0;
6576 return false;
6577 }
6578 case Instruction::SDiv:
6579 case Instruction::SRem: {
6580 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
6581 const APInt *Numerator, *Denominator;
6582 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
6583 return false;
6584 // We cannot hoist this division if the denominator is 0.
6585 if (*Denominator == 0)
6586 return false;
6587 // It's safe to hoist if the denominator is not 0 or -1.
6588 if (!Denominator->isAllOnes())
6589 return true;
6590 // At this point we know that the denominator is -1. It is safe to hoist as
6591 // long we know that the numerator is not INT_MIN.
6592 if (match(Inst->getOperand(0), m_APInt(Numerator)))
6593 return !Numerator->isMinSignedValue();
6594 // The numerator *might* be MinSignedValue.
6595 return false;
6596 }
6597 case Instruction::Load: {
6598 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
6599 if (!LI)
6600 return false;
6601 if (mustSuppressSpeculation(*LI))
6602 return false;
6603 const DataLayout &DL = LI->getModule()->getDataLayout();
6605 LI->getType(), LI->getAlign(), DL,
6606 CtxI, AC, DT, TLI);
6607 }
6608 case Instruction::Call: {
6609 auto *CI = dyn_cast<const CallInst>(Inst);
6610 if (!CI)
6611 return false;
6612 const Function *Callee = CI->getCalledFunction();
6613
6614 // The called function could have undefined behavior or side-effects, even
6615 // if marked readnone nounwind.
6616 return Callee && Callee->isSpeculatable();
6617 }
6618 case Instruction::VAArg:
6619 case Instruction::Alloca:
6620 case Instruction::Invoke:
6621 case Instruction::CallBr:
6622 case Instruction::PHI:
6623 case Instruction::Store:
6624 case Instruction::Ret:
6625 case Instruction::Br:
6626 case Instruction::IndirectBr:
6627 case Instruction::Switch:
6628 case Instruction::Unreachable:
6629 case Instruction::Fence:
6630 case Instruction::AtomicRMW:
6631 case Instruction::AtomicCmpXchg:
6632 case Instruction::LandingPad:
6633 case Instruction::Resume:
6634 case Instruction::CatchSwitch:
6635 case Instruction::CatchPad:
6636 case Instruction::CatchRet:
6637 case Instruction::CleanupPad:
6638 case Instruction::CleanupRet:
6639 return false; // Misc instructions which have effects
6640 }
6641}
6642
6644 if (I.mayReadOrWriteMemory())
6645 // Memory dependency possible
6646 return true;
6648 // Can't move above a maythrow call or infinite loop. Or if an
6649 // inalloca alloca, above a stacksave call.
6650 return true;
6652 // 1) Can't reorder two inf-loop calls, even if readonly
6653 // 2) Also can't reorder an inf-loop call below a instruction which isn't
6654 // safe to speculative execute. (Inverse of above)
6655 return true;
6656 return false;
6657}
6658
6659/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
6661 switch (OR) {
6670 }
6671 llvm_unreachable("Unknown OverflowResult");
6672}
6673
6674/// Combine constant ranges from computeConstantRange() and computeKnownBits().
6677 bool ForSigned,
6678 const SimplifyQuery &SQ) {
6679 ConstantRange CR1 =
6680 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
6681 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
6684 return CR1.intersectWith(CR2, RangeType);
6685}
6686
6688 const Value *RHS,
6689 const SimplifyQuery &SQ) {
6690 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
6691 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
6692 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
6693 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
6694 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
6695}
6696
6698 const Value *RHS,
6699 const SimplifyQuery &SQ) {
6700 // Multiplying n * m significant bits yields a result of n + m significant
6701 // bits. If the total number of significant bits does not exceed the
6702 // result bit width (minus 1), there is no overflow.
6703 // This means if we have enough leading sign bits in the operands
6704 // we can guarantee that the result does not overflow.
6705 // Ref: "Hacker's Delight" by Henry Warren
6706 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
6707
6708 // Note that underestimating the number of sign bits gives a more
6709 // conservative answer.
6710 unsigned SignBits =
6712
6713 // First handle the easy case: if we have enough sign bits there's
6714 // definitely no overflow.
6715 if (SignBits > BitWidth + 1)
6717
6718 // There are two ambiguous cases where there can be no overflow:
6719 // SignBits == BitWidth + 1 and
6720 // SignBits == BitWidth
6721 // The second case is difficult to check, therefore we only handle the
6722 // first case.
6723 if (SignBits == BitWidth + 1) {
6724 // It overflows only when both arguments are negative and the true
6725 // product is exactly the minimum negative number.
6726 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
6727 // For simplicity we just check if at least one side is not negative.
6728 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
6729 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
6730 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
6732 }
6734}
6735
6738 const WithCache<const Value *> &RHS,
6739 const SimplifyQuery &SQ) {
6740 ConstantRange LHSRange =
6741 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
6742 ConstantRange RHSRange =
6743 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
6744 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
6745}
6746
6747static OverflowResult
6749 const WithCache<const Value *> &RHS,
6750 const AddOperator *Add, const SimplifyQuery &SQ) {
6751 if (Add && Add->hasNoSignedWrap()) {
6753 }
6754
6755 // If LHS and RHS each have at least two sign bits, the addition will look
6756 // like
6757 //
6758 // XX..... +
6759 // YY.....
6760 //
6761 // If the carry into the most significant position is 0, X and Y can't both
6762 // be 1 and therefore the carry out of the addition is also 0.
6763 //
6764 // If the carry into the most significant position is 1, X and Y can't both
6765 // be 0 and therefore the carry out of the addition is also 1.
6766 //
6767 // Since the carry into the most significant position is always equal to
6768 // the carry out of the addition, there is no signed overflow.
6769 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
6770 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
6772
6773 ConstantRange LHSRange =
6774 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
6775 ConstantRange RHSRange =
6776 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
6777 OverflowResult OR =
6778 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
6780 return OR;
6781
6782 // The remaining code needs Add to be available. Early returns if not so.
6783 if (!Add)
6785
6786 // If the sign of Add is the same as at least one of the operands, this add
6787 // CANNOT overflow. If this can be determined from the known bits of the
6788 // operands the above signedAddMayOverflow() check will have already done so.
6789 // The only other way to improve on the known bits is from an assumption, so
6790 // call computeKnownBitsFromContext() directly.
6791 bool LHSOrRHSKnownNonNegative =
6792 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
6793 bool LHSOrRHSKnownNegative =
6794 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
6795 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
6796 KnownBits AddKnown(LHSRange.getBitWidth());
6797 computeKnownBitsFromContext(Add, AddKnown, /*Depth=*/0, SQ);
6798 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
6799 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
6801 }
6802
6804}
6805
6807 const Value *RHS,
6808 const SimplifyQuery &SQ) {
6809 // X - (X % ?)
6810 // The remainder of a value can't have greater magnitude than itself,
6811 // so the subtraction can't overflow.
6812
6813 // X - (X -nuw ?)
6814 // In the minimal case, this would simplify to "?", so there's no subtract
6815 // at all. But if this analysis is used to peek through casts, for example,
6816 // then determining no-overflow may allow other transforms.
6817
6818 // TODO: There are other patterns like this.
6819 // See simplifyICmpWithBinOpOnLHS() for candidates.
6820 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
6822 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
6824
6825 // Checking for conditions implied by dominating conditions may be expensive.
6826 // Limit it to usub_with_overflow calls for now.
6827 if (match(SQ.CxtI,
6828 m_Intrinsic<Intrinsic::usub_with_overflow>(m_Value(), m_Value())))
6830 SQ.DL)) {
6831 if (*C)
6834 }
6835 ConstantRange LHSRange =
6836 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
6837 ConstantRange RHSRange =
6838 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
6839 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
6840}
6841
6843 const Value *RHS,
6844 const SimplifyQuery &SQ) {
6845 // X - (X % ?)
6846 // The remainder of a value can't have greater magnitude than itself,
6847 // so the subtraction can't overflow.
6848
6849 // X - (X -nsw ?)
6850 // In the minimal case, this would simplify to "?", so there's no subtract
6851 // at all. But if this analysis is used to peek through casts, for example,
6852 // then determining no-overflow may allow other transforms.
6853 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
6855 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
6857
6858 // If LHS and RHS each have at least two sign bits, the subtraction
6859 // cannot overflow.
6860 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
6861 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
6863
6864 ConstantRange LHSRange =
6865 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
6866 ConstantRange RHSRange =
6867 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
6868 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
6869}
6870
6872 const DominatorTree &DT) {
6873 SmallVector<const BranchInst *, 2> GuardingBranches;
6875
6876 for (const User *U : WO->users()) {
6877 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
6878 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
6879
6880 if (EVI->getIndices()[0] == 0)
6881 Results.push_back(EVI);
6882 else {
6883 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
6884
6885 for (const auto *U : EVI->users())
6886 if (const auto *B = dyn_cast<BranchInst>(U)) {
6887 assert(B->isConditional() && "How else is it using an i1?");
6888 GuardingBranches.push_back(B);
6889 }
6890 }
6891 } else {
6892 // We are using the aggregate directly in a way we don't want to analyze
6893 // here (storing it to a global, say).
6894 return false;
6895 }
6896 }
6897
6898 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
6899 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
6900 if (!NoWrapEdge.isSingleEdge())
6901 return false;
6902
6903 // Check if all users of the add are provably no-wrap.
6904 for (const auto *Result : Results) {
6905 // If the extractvalue itself is not executed on overflow, the we don't
6906 // need to check each use separately, since domination is transitive.
6907 if (DT.dominates(NoWrapEdge, Result->getParent()))
6908 continue;
6909
6910 for (const auto &RU : Result->uses())
6911 if (!DT.dominates(NoWrapEdge, RU))
6912 return false;
6913 }
6914
6915 return true;
6916 };
6917
6918 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
6919}
6920
6921/// Shifts return poison if shiftwidth is larger than the bitwidth.
6922static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
6923 auto *C = dyn_cast<Constant>(ShiftAmount);
6924 if (!C)
6925 return false;
6926
6927 // Shifts return poison if shiftwidth is larger than the bitwidth.
6929 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
6930 unsigned NumElts = FVTy->getNumElements();
6931 for (unsigned i = 0; i < NumElts; ++i)
6932 ShiftAmounts.push_back(C->getAggregateElement(i));
6933 } else if (isa<ScalableVectorType>(C->getType()))
6934 return false; // Can't tell, just return false to be safe
6935 else
6936 ShiftAmounts.push_back(C);
6937
6938 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
6939 auto *CI = dyn_cast_or_null<ConstantInt>(C);
6940 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
6941 });
6942
6943 return Safe;
6944}
6945
6947 PoisonOnly = (1 << 0),
6948 UndefOnly = (1 << 1),
6950};
6951
6953 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
6954}
6955
6957 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
6958}
6959
6961 bool ConsiderFlagsAndMetadata) {
6962
6963 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
6964 Op->hasPoisonGeneratingAnnotations())
6965 return true;
6966
6967 unsigned Opcode = Op->getOpcode();
6968
6969 // Check whether opcode is a poison/undef-generating operation
6970 switch (Opcode) {
6971 case Instruction::Shl:
6972 case Instruction::AShr:
6973 case Instruction::LShr:
6974 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
6975 case Instruction::FPToSI:
6976 case Instruction::FPToUI:
6977 // fptosi/ui yields poison if the resulting value does not fit in the
6978 // destination type.
6979 return true;
6980 case Instruction::Call:
6981 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
6982 switch (II->getIntrinsicID()) {
6983 // TODO: Add more intrinsics.
6984 case Intrinsic::ctlz:
6985 case Intrinsic::cttz:
6986 case Intrinsic::abs:
6987 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
6988 return false;
6989 break;
6990 case Intrinsic::ctpop:
6991 case Intrinsic::bswap:
6992 case Intrinsic::bitreverse:
6993 case Intrinsic::fshl:
6994 case Intrinsic::fshr:
6995 case Intrinsic::smax:
6996 case Intrinsic::smin:
6997 case Intrinsic::umax:
6998 case Intrinsic::umin:
6999 case Intrinsic::ptrmask:
7000 case Intrinsic::fptoui_sat:
7001 case Intrinsic::fptosi_sat:
7002 case Intrinsic::sadd_with_overflow:
7003 case Intrinsic::ssub_with_overflow:
7004 case Intrinsic::smul_with_overflow:
7005 case Intrinsic::uadd_with_overflow:
7006 case Intrinsic::usub_with_overflow:
7007 case Intrinsic::umul_with_overflow:
7008 case Intrinsic::sadd_sat:
7009 case Intrinsic::uadd_sat:
7010 case Intrinsic::ssub_sat:
7011 case Intrinsic::usub_sat:
7012 return false;
7013 case Intrinsic::sshl_sat:
7014 case Intrinsic::ushl_sat:
7015 return includesPoison(Kind) &&
7016 !shiftAmountKnownInRange(II->getArgOperand(1));
7017 case Intrinsic::fma:
7018 case Intrinsic::fmuladd:
7019 case Intrinsic::sqrt:
7020 case Intrinsic::powi:
7021 case Intrinsic::sin:
7022 case Intrinsic::cos:
7023 case Intrinsic::pow:
7024 case Intrinsic::log:
7025 case Intrinsic::log10:
7026 case Intrinsic::log2:
7027 case Intrinsic::exp:
7028 case Intrinsic::exp2:
7029 case Intrinsic::exp10:
7030 case Intrinsic::fabs:
7031 case Intrinsic::copysign:
7032 case Intrinsic::floor:
7033 case Intrinsic::ceil:
7034 case Intrinsic::trunc:
7035 case Intrinsic::rint:
7036 case Intrinsic::nearbyint:
7037 case Intrinsic::round:
7038 case Intrinsic::roundeven:
7039 case Intrinsic::fptrunc_round:
7040 case Intrinsic::canonicalize:
7041 case Intrinsic::arithmetic_fence:
7042 case Intrinsic::minnum:
7043 case Intrinsic::maxnum:
7044 case Intrinsic::minimum:
7045 case Intrinsic::maximum:
7046 case Intrinsic::is_fpclass:
7047 case Intrinsic::ldexp:
7048 case Intrinsic::frexp:
7049 return false;
7050 case Intrinsic::lround:
7051 case Intrinsic::llround:
7052 case Intrinsic::lrint:
7053 case Intrinsic::llrint:
7054 // If the value doesn't fit an unspecified value is returned (but this
7055 // is not poison).
7056 return false;
7057 }
7058 }
7059 [[fallthrough]];
7060 case Instruction::CallBr:
7061 case Instruction::Invoke: {
7062 const auto *CB = cast<CallBase>(Op);
7063 return !CB->hasRetAttr(Attribute::NoUndef);
7064 }
7065 case Instruction::InsertElement:
7066 case Instruction::ExtractElement: {
7067 // If index exceeds the length of the vector, it returns poison
7068 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7069 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7070 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7071 if (includesPoison(Kind))
7072 return !Idx ||
7073 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7074 return false;
7075 }
7076 case Instruction::ShuffleVector: {
7077 ArrayRef<int> Mask = isa<ConstantExpr>(Op)
7078 ? cast<ConstantExpr>(Op)->getShuffleMask()
7079 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7080 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7081 }
7082 case Instruction::FNeg:
7083 case Instruction::PHI:
7084 case Instruction::Select:
7085 case Instruction::URem:
7086 case Instruction::SRem:
7087 case Instruction::ExtractValue:
7088 case Instruction::InsertValue:
7089 case Instruction::Freeze:
7090 case Instruction::ICmp:
7091 case Instruction::FCmp:
7092 case Instruction::FAdd:
7093 case Instruction::FSub:
7094 case Instruction::FMul:
7095 case Instruction::FDiv:
7096 case Instruction::FRem:
7097 return false;
7098 case Instruction::GetElementPtr:
7099 // inbounds is handled above
7100 // TODO: what about inrange on constexpr?
7101 return false;
7102 default: {
7103 const auto *CE = dyn_cast<ConstantExpr>(Op);
7104 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7105 return false;
7106 else if (Instruction::isBinaryOp(Opcode))
7107 return false;
7108 // Be conservative and return true.
7109 return true;
7110 }
7111 }
7112}
7113
7115 bool ConsiderFlagsAndMetadata) {
7116 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7117 ConsiderFlagsAndMetadata);
7118}
7119
7120bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7121 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7122 ConsiderFlagsAndMetadata);
7123}
7124
7125static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7126 unsigned Depth) {
7127 if (ValAssumedPoison == V)
7128 return true;
7129
7130 const unsigned MaxDepth = 2;
7131 if (Depth >= MaxDepth)
7132 return false;
7133
7134 if (const auto *I = dyn_cast<Instruction>(V)) {
7135 if (any_of(I->operands(), [=](const Use &Op) {
7136 return propagatesPoison(Op) &&
7137 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7138 }))
7139 return true;
7140
7141 // V = extractvalue V0, idx
7142 // V2 = extractvalue V0, idx2
7143 // V0's elements are all poison or not. (e.g., add_with_overflow)
7144 const WithOverflowInst *II;
7146 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7147 llvm::is_contained(II->args(), ValAssumedPoison)))
7148 return true;
7149 }
7150 return false;
7151}
7152
7153static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7154 unsigned Depth) {
7155 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7156 return true;
7157
7158 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7159 return true;
7160
7161 const unsigned MaxDepth = 2;
7162 if (Depth >= MaxDepth)
7163 return false;
7164
7165 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7166 if (I && !canCreatePoison(cast<Operator>(I))) {
7167 return all_of(I->operands(), [=](const Value *Op) {
7168 return impliesPoison(Op, V, Depth + 1);
7169 });
7170 }
7171 return false;
7172}
7173
7174bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7175 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7176}
7177
7178static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7179
7181 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7182 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7184 return false;
7185
7186 if (isa<MetadataAsValue>(V))
7187 return false;
7188
7189 if (const auto *A = dyn_cast<Argument>(V)) {
7190 if (A->hasAttribute(Attribute::NoUndef) ||
7191 A->hasAttribute(Attribute::Dereferenceable) ||
7192 A->hasAttribute(Attribute::DereferenceableOrNull))
7193 return true;
7194 }
7195
7196 if (auto *C = dyn_cast<Constant>(V)) {
7197 if (isa<PoisonValue>(C))
7198 return !includesPoison(Kind);
7199
7200 if (isa<UndefValue>(C))
7201 return !includesUndef(Kind);
7202
7203 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(V) ||
7204 isa<ConstantPointerNull>(C) || isa<Function>(C))
7205 return true;
7206
7207 if (C->getType()->isVectorTy() && !isa<ConstantExpr>(C))
7208 return (!includesUndef(Kind) ? !C->containsPoisonElement()
7209 : !C->containsUndefOrPoisonElement()) &&
7210 !C->containsConstantExpression();
7211 }
7212
7213 // Strip cast operations from a pointer value.
7214 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7215 // inbounds with zero offset. To guarantee that the result isn't poison, the
7216 // stripped pointer is checked as it has to be pointing into an allocated
7217 // object or be null `null` to ensure `inbounds` getelement pointers with a
7218 // zero offset could not produce poison.
7219 // It can strip off addrspacecast that do not change bit representation as
7220 // well. We believe that such addrspacecast is equivalent to no-op.
7221 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7222 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7223 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7224 return true;
7225
7226 auto OpCheck = [&](const Value *V) {
7227 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7228 };
7229
7230 if (auto *Opr = dyn_cast<Operator>(V)) {
7231 // If the value is a freeze instruction, then it can never
7232 // be undef or poison.
7233 if (isa<FreezeInst>(V))
7234 return true;
7235
7236 if (const auto *CB = dyn_cast<CallBase>(V)) {
7237 if (CB->hasRetAttr(Attribute::NoUndef) ||
7238 CB->hasRetAttr(Attribute::Dereferenceable) ||
7239 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7240 return true;
7241 }
7242
7243 if (const auto *PN = dyn_cast<PHINode>(V)) {
7244 unsigned Num = PN->getNumIncomingValues();
7245 bool IsWellDefined = true;
7246 for (unsigned i = 0; i < Num; ++i) {
7247 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7248 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7249 DT, Depth + 1, Kind)) {
7250 IsWellDefined = false;
7251 break;
7252 }
7253 }
7254 if (IsWellDefined)
7255 return true;
7256 } else if (!::canCreateUndefOrPoison(Opr, Kind,
7257 /*ConsiderFlagsAndMetadata*/ true) &&
7258 all_of(Opr->operands(), OpCheck))
7259 return true;
7260 }
7261
7262 if (auto *I = dyn_cast<LoadInst>(V))
7263 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7264 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7265 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7266 return true;
7267
7269 return true;
7270
7271 // CxtI may be null or a cloned instruction.
7272 if (!CtxI || !CtxI->getParent() || !DT)
7273 return false;
7274
7275 auto *DNode = DT->getNode(CtxI->getParent());
7276 if (!DNode)
7277 // Unreachable block
7278 return false;
7279
7280 // If V is used as a branch condition before reaching CtxI, V cannot be
7281 // undef or poison.
7282 // br V, BB1, BB2
7283 // BB1:
7284 // CtxI ; V cannot be undef or poison here
7285 auto *Dominator = DNode->getIDom();
7286 while (Dominator) {
7287 auto *TI = Dominator->getBlock()->getTerminator();
7288
7289 Value *Cond = nullptr;
7290 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7291 if (BI->isConditional())
7292 Cond = BI->getCondition();
7293 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7294 Cond = SI->getCondition();
7295 }
7296
7297 if (Cond) {
7298 if (Cond == V)
7299 return true;
7300 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7301 // For poison, we can analyze further
7302 auto *Opr = cast<Operator>(Cond);
7303 if (any_of(Opr->operands(),
7304 [V](const Use &U) { return V == U && propagatesPoison(U); }))
7305 return true;
7306 }
7307 }
7308
7309 Dominator = Dominator->getIDom();
7310 }
7311
7312 if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
7313 return true;
7314
7315 return false;
7316}
7317
7319 const Instruction *CtxI,
7320 const DominatorTree *DT,
7321 unsigned Depth) {
7322 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7323 UndefPoisonKind::UndefOrPoison);
7324}
7325
7327 const Instruction *CtxI,
7328 const DominatorTree *DT, unsigned Depth) {
7329 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7330 UndefPoisonKind::PoisonOnly);
7331}
7332
7334 const Instruction *CtxI,
7335 const DominatorTree *DT, unsigned Depth) {
7336 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7337 UndefPoisonKind::UndefOnly);
7338}
7339
7340/// Return true if undefined behavior would provably be executed on the path to
7341/// OnPathTo if Root produced a posion result. Note that this doesn't say
7342/// anything about whether OnPathTo is actually executed or whether Root is
7343/// actually poison. This can be used to assess whether a new use of Root can
7344/// be added at a location which is control equivalent with OnPathTo (such as
7345/// immediately before it) without introducing UB which didn't previously
7346/// exist. Note that a false result conveys no information.
7348 Instruction *OnPathTo,
7349 DominatorTree *DT) {
7350 // Basic approach is to assume Root is poison, propagate poison forward
7351 // through all users we can easily track, and then check whether any of those
7352 // users are provable UB and must execute before out exiting block might
7353 // exit.
7354
7355 // The set of all recursive users we've visited (which are assumed to all be
7356 // poison because of said visit)
7357 SmallSet<const Value *, 16> KnownPoison;
7359 Worklist.push_back(Root);
7360 while (!Worklist.empty()) {
7361 const Instruction *I = Worklist.pop_back_val();
7362
7363 // If we know this must trigger UB on a path leading our target.
7364 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7365 return true;
7366
7367 // If we can't analyze propagation through this instruction, just skip it
7368 // and transitive users. Safe as false is a conservative result.
7369 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7370 return KnownPoison.contains(U) && propagatesPoison(U);
7371 }))
7372 continue;
7373
7374 if (KnownPoison.insert(I).second)
7375 for (const User *User : I->users())
7376 Worklist.push_back(cast<Instruction>(User));
7377 }
7378
7379 // Might be non-UB, or might have a path we couldn't prove must execute on
7380 // way to exiting bb.
7381 return false;
7382}
7383
7385 const SimplifyQuery &SQ) {
7386 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7387 Add, SQ);
7388}
7389
7392 const WithCache<const Value *> &RHS,
7393 const SimplifyQuery &SQ) {
7394 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7395}
7396
7398 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7399 // of time because it's possible for another thread to interfere with it for an
7400 // arbitrary length of time, but programs aren't allowed to rely on that.
7401
7402 // If there is no successor, then execution can't transfer to it.
7403 if (isa<ReturnInst>(I))
7404 return false;
7405 if (isa<UnreachableInst>(I))
7406 return false;
7407
7408 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7409 // Instruction::willReturn.
7410 //
7411 // FIXME: Move this check into Instruction::willReturn.
7412 if (isa<CatchPadInst>(I)) {
7413 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7414 default:
7415 // A catchpad may invoke exception object constructors and such, which
7416 // in some languages can be arbitrary code, so be conservative by default.
7417 return false;
7419 // For CoreCLR, it just involves a type test.
7420 return true;
7421 }
7422 }
7423
7424 // An instruction that returns without throwing must transfer control flow
7425 // to a successor.
7426 return !I->mayThrow() && I->willReturn();
7427}
7428
7430 // TODO: This is slightly conservative for invoke instruction since exiting
7431 // via an exception *is* normal control for them.
7432 for (const Instruction &I : *BB)
7434 return false;
7435 return true;
7436}
7437
7440 unsigned ScanLimit) {
7442 ScanLimit);
7443}
7444
7446 iterator_range<BasicBlock::const_iterator> Range, unsigned ScanLimit) {
7447 assert(ScanLimit && "scan limit must be non-zero");
7448 for (const Instruction &I : Range) {
7449 if (isa<DbgInfoIntrinsic>(I))
7450 continue;
7451 if (--ScanLimit == 0)
7452 return false;
7454 return false;
7455 }
7456 return true;
7457}
7458
7460 const Loop *L) {
7461 // The loop header is guaranteed to be executed for every iteration.
7462 //
7463 // FIXME: Relax this constraint to cover all basic blocks that are
7464 // guaranteed to be executed at every iteration.
7465 if (I->getParent() != L->getHeader()) return false;
7466
7467 for (const Instruction &LI : *L->getHeader()) {
7468 if (&LI == I) return true;
7469 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7470 }
7471 llvm_unreachable("Instruction not contained in its own parent basic block.");
7472}
7473
7474bool llvm::propagatesPoison(const Use &PoisonOp) {
7475 const Operator *I = cast<Operator>(PoisonOp.getUser());
7476 switch (I->getOpcode()) {
7477 case Instruction::Freeze:
7478 case Instruction::PHI:
7479 case Instruction::Invoke:
7480 return false;
7481 case Instruction::Select:
7482 return PoisonOp.getOperandNo() == 0;
7483 case Instruction::Call:
7484 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
7485 switch (II->getIntrinsicID()) {
7486 // TODO: Add more intrinsics.
7487 case Intrinsic::sadd_with_overflow:
7488 case Intrinsic::ssub_with_overflow:
7489 case Intrinsic::smul_with_overflow:
7490 case Intrinsic::uadd_with_overflow:
7491 case Intrinsic::usub_with_overflow:
7492 case Intrinsic::umul_with_overflow:
7493 // If an input is a vector containing a poison element, the
7494 // two output vectors (calculated results, overflow bits)'
7495 // corresponding lanes are poison.
7496 return true;
7497 case Intrinsic::ctpop:
7498 case Intrinsic::ctlz:
7499 case Intrinsic::cttz:
7500 case Intrinsic::abs:
7501 case Intrinsic::smax:
7502 case Intrinsic::smin:
7503 case Intrinsic::umax:
7504 case Intrinsic::umin:
7505 case Intrinsic::bitreverse:
7506 case Intrinsic::bswap:
7507 case Intrinsic::sadd_sat:
7508 case Intrinsic::ssub_sat:
7509 case Intrinsic::sshl_sat:
7510 case Intrinsic::uadd_sat:
7511 case Intrinsic::usub_sat:
7512 case Intrinsic::ushl_sat:
7513 return true;
7514 }
7515 }
7516 return false;
7517 case Instruction::ICmp:
7518 case Instruction::FCmp:
7519 case Instruction::GetElementPtr:
7520 return true;
7521 default:
7522 if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I))
7523 return true;
7524
7525 // Be conservative and return false.
7526 return false;
7527 }
7528}
7529
7530/// Enumerates all operands of \p I that are guaranteed to not be undef or
7531/// poison. If the callback \p Handle returns true, stop processing and return
7532/// true. Otherwise, return false.
7533template <typename CallableT>
7535 const CallableT &Handle) {
7536 switch (I->getOpcode()) {
7537 case Instruction::Store:
7538 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
7539 return true;
7540 break;
7541
7542 case Instruction::Load:
7543 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
7544 return true;
7545 break;
7546
7547 // Since dereferenceable attribute imply noundef, atomic operations
7548 // also implicitly have noundef pointers too
7549 case Instruction::AtomicCmpXchg:
7550 if (Handle(cast<AtomicCmpXchgInst>(I)->getPointerOperand()))
7551 return true;
7552 break;
7553
7554 case Instruction::AtomicRMW:
7555 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
7556 return true;
7557 break;
7558
7559 case Instruction::Call:
7560 case Instruction::Invoke: {
7561 const CallBase *CB = cast<CallBase>(I);
7562 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
7563 return true;
7564 for (unsigned i = 0; i < CB->arg_size(); ++i)
7565 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
7566 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
7567 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
7568 Handle(CB->getArgOperand(i)))
7569 return true;
7570 break;
7571 }
7572 case Instruction::Ret:
7573 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
7574 Handle(I->getOperand(0)))
7575 return true;
7576 break;
7577 case Instruction::Switch:
7578 if (Handle(cast<SwitchInst>(I)->getCondition()))
7579 return true;
7580 break;
7581 case Instruction::Br: {
7582 auto *BR = cast<BranchInst>(I);
7583 if (BR->isConditional() && Handle(BR->getCondition()))
7584 return true;
7585 break;
7586 }
7587 default:
7588 break;
7589 }
7590
7591 return false;
7592}
7593
7596 handleGuaranteedWellDefinedOps(I, [&](const Value *V) {
7597 Operands.push_back(V);
7598 return false;
7599 });
7600}
7601
7602/// Enumerates all operands of \p I that are guaranteed to not be poison.
7603template <typename CallableT>
7605 const CallableT &Handle) {
7606 if (handleGuaranteedWellDefinedOps(I, Handle))
7607 return true;
7608 switch (I->getOpcode()) {
7609 // Divisors of these operations are allowed to be partially undef.
7610 case Instruction::UDiv:
7611 case Instruction::SDiv:
7612 case Instruction::URem:
7613 case Instruction::SRem:
7614 return Handle(I->getOperand(1));
7615 default:
7616 return false;
7617 }
7618}
7619
7622 handleGuaranteedNonPoisonOps(I, [&](const Value *V) {
7623 Operands.push_back(V);
7624 return false;
7625 });
7626}
7627
7629 const SmallPtrSetImpl<const Value *> &KnownPoison) {
7631 I, [&](const Value *V) { return KnownPoison.count(V); });
7632}
7633
7635 bool PoisonOnly) {
7636 // We currently only look for uses of values within the same basic
7637 // block, as that makes it easier to guarantee that the uses will be
7638 // executed given that Inst is executed.
7639 //
7640 // FIXME: Expand this to consider uses beyond the same basic block. To do
7641 // this, look out for the distinction between post-dominance and strong
7642 // post-dominance.
7643 const BasicBlock *BB = nullptr;
7645 if (const auto *Inst = dyn_cast<Instruction>(V)) {
7646 BB = Inst->getParent();
7647 Begin = Inst->getIterator();
7648 Begin++;
7649 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
7650 if (Arg->getParent()->isDeclaration())
7651 return false;
7652 BB = &Arg->getParent()->getEntryBlock();
7653 Begin = BB->begin();
7654 } else {
7655 return false;
7656 }
7657
7658 // Limit number of instructions we look at, to avoid scanning through large
7659 // blocks. The current limit is chosen arbitrarily.
7660 unsigned ScanLimit = 32;
7662
7663 if (!PoisonOnly) {
7664 // Since undef does not propagate eagerly, be conservative & just check
7665 // whether a value is directly passed to an instruction that must take
7666 // well-defined operands.
7667
7668 for (const auto &I : make_range(Begin, End)) {
7669 if (isa<DbgInfoIntrinsic>(I))
7670 continue;
7671 if (--ScanLimit == 0)
7672 break;
7673
7674 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
7675 return WellDefinedOp == V;
7676 }))
7677 return true;
7678
7680 break;
7681 }
7682 return false;
7683 }
7684
7685 // Set of instructions that we have proved will yield poison if Inst
7686 // does.
7687 SmallSet<const Value *, 16> YieldsPoison;
7689
7690 YieldsPoison.insert(V);
7691 Visited.insert(BB);
7692
7693 while (true) {
7694 for (const auto &I : make_range(Begin, End)) {
7695 if (isa<DbgInfoIntrinsic>(I))
7696 continue;
7697 if (--ScanLimit == 0)
7698 return false;
7699 if (mustTriggerUB(&I, YieldsPoison))
7700 return true;
7702 return false;
7703
7704 // If an operand is poison and propagates it, mark I as yielding poison.
7705 for (const Use &Op : I.operands()) {
7706 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
7707 YieldsPoison.insert(&I);
7708 break;
7709 }
7710 }
7711
7712 // Special handling for select, which returns poison if its operand 0 is
7713 // poison (handled in the loop above) *or* if both its true/false operands
7714 // are poison (handled here).
7715 if (I.getOpcode() == Instruction::Select &&
7716 YieldsPoison.count(I.getOperand(1)) &&
7717 YieldsPoison.count(I.getOperand(2))) {
7718 YieldsPoison.insert(&I);
7719 }
7720 }
7721
7722 BB = BB->getSingleSuccessor();
7723 if (!BB || !Visited.insert(BB).second)
7724 break;
7725
7726 Begin = BB->getFirstNonPHI()->getIterator();
7727 End = BB->end();
7728 }
7729 return false;
7730}
7731
7733 return ::programUndefinedIfUndefOrPoison(Inst, false);
7734}
7735
7737 return ::programUndefinedIfUndefOrPoison(Inst, true);
7738}
7739
7740static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
7741 if (FMF.noNaNs())
7742 return true;
7743
7744 if (auto *C = dyn_cast<ConstantFP>(V))
7745 return !C->isNaN();
7746
7747 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
7748 if (!C->getElementType()->isFloatingPointTy())
7749 return false;
7750 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
7751 if (C->getElementAsAPFloat(I).isNaN())
7752 return false;
7753 }
7754 return true;
7755 }
7756
7757 if (isa<ConstantAggregateZero>(V))
7758 return true;
7759
7760 return false;
7761}
7762
7763static bool isKnownNonZero(const Value *V) {
7764 if (auto *C = dyn_cast<ConstantFP>(V))
7765 return !C->isZero();
7766
7767 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
7768 if (!C->getElementType()->isFloatingPointTy())
7769 return false;
7770 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
7771 if (C->getElementAsAPFloat(I).isZero())
7772 return false;
7773 }
7774 return true;
7775 }
7776
7777 return false;
7778}
7779
7780/// Match clamp pattern for float types without care about NaNs or signed zeros.
7781/// Given non-min/max outer cmp/select from the clamp pattern this
7782/// function recognizes if it can be substitued by a "canonical" min/max
7783/// pattern.
7785 Value *CmpLHS, Value *CmpRHS,
7786 Value *TrueVal, Value *FalseVal,
7787 Value *&LHS, Value *&RHS) {
7788 // Try to match
7789 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
7790 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
7791 // and return description of the outer Max/Min.
7792
7793 // First, check if select has inverse order:
7794 if (CmpRHS == FalseVal) {
7795 std::swap(TrueVal, FalseVal);
7796 Pred = CmpInst::getInversePredicate(Pred);
7797 }
7798
7799 // Assume success now. If there's no match, callers should not use these anyway.
7800 LHS = TrueVal;
7801 RHS = FalseVal;
7802
7803 const APFloat *FC1;
7804 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
7805 return {SPF_UNKNOWN, SPNB_NA, false};
7806
7807 const APFloat *FC2;
7808 switch (Pred) {
7809 case CmpInst::FCMP_OLT:
7810 case CmpInst::FCMP_OLE:
7811 case CmpInst::FCMP_ULT:
7812 case CmpInst::FCMP_ULE:
7813 if (match(FalseVal,
7815 m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
7816 *FC1 < *FC2)
7817 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
7818 break;
7819 case CmpInst::FCMP_OGT:
7820 case CmpInst::FCMP_OGE:
7821 case CmpInst::FCMP_UGT:
7822 case CmpInst::FCMP_UGE:
7823 if (match(FalseVal,
7825 m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
7826 *FC1 > *FC2)
7827 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
7828 break;
7829 default:
7830 break;
7831 }
7832
7833 return {SPF_UNKNOWN, SPNB_NA, false};
7834}
7835
7836/// Recognize variations of:
7837/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
7839 Value *CmpLHS, Value *CmpRHS,
7840 Value *TrueVal, Value *FalseVal) {
7841 // Swap the select operands and predicate to match the patterns below.
7842 if (CmpRHS != TrueVal) {
7843 Pred = ICmpInst::getSwappedPredicate(Pred);
7844 std::swap(TrueVal, FalseVal);
7845 }
7846 const APInt *C1;
7847 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
7848 const APInt *C2;
7849 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
7850 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
7851 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
7852 return {SPF_SMAX, SPNB_NA, false};
7853
7854 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
7855 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
7856 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
7857 return {SPF_SMIN, SPNB_NA, false};
7858
7859 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
7860 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
7861 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
7862 return {SPF_UMAX, SPNB_NA, false};
7863
7864 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
7865 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
7866 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
7867 return {SPF_UMIN, SPNB_NA, false};
7868 }
7869 return {SPF_UNKNOWN, SPNB_NA, false};
7870}
7871
7872/// Recognize variations of:
7873/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
7875 Value *CmpLHS, Value *CmpRHS,
7876 Value *TVal, Value *FVal,
7877 unsigned Depth) {
7878 // TODO: Allow FP min/max with nnan/nsz.
7879 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
7880
7881 Value *A = nullptr, *B = nullptr;
7882 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
7883 if (!SelectPatternResult::isMinOrMax(L.Flavor))
7884 return {SPF_UNKNOWN, SPNB_NA, false};
7885
7886 Value *C = nullptr, *D = nullptr;
7887 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
7888 if (L.Flavor != R.Flavor)
7889 return {SPF_UNKNOWN, SPNB_NA, false};
7890
7891 // We have something like: x Pred y ? min(a, b) : min(c, d).
7892 // Try to match the compare to the min/max operations of the select operands.
7893 // First, make sure we have the right compare predicate.
7894 switch (L.Flavor) {
7895 case SPF_SMIN:
7896 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
7897 Pred = ICmpInst::getSwappedPredicate(Pred);
7898 std::swap(CmpLHS, CmpRHS);
7899 }
7900 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
7901 break;
7902 return {SPF_UNKNOWN, SPNB_NA, false};
7903 case SPF_SMAX:
7904 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
7905 Pred = ICmpInst::getSwappedPredicate(Pred);
7906 std::swap(CmpLHS, CmpRHS);
7907 }
7908 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
7909 break;
7910 return {SPF_UNKNOWN, SPNB_NA, false};
7911 case SPF_UMIN:
7912 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
7913 Pred = ICmpInst::getSwappedPredicate(Pred);
7914 std::swap(CmpLHS, CmpRHS);
7915 }
7916 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
7917 break;
7918 return {SPF_UNKNOWN, SPNB_NA, false};
7919 case SPF_UMAX:
7920 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
7921 Pred = ICmpInst::getSwappedPredicate(Pred);
7922 std::swap(CmpLHS, CmpRHS);
7923 }
7924 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
7925 break;
7926 return {SPF_UNKNOWN, SPNB_NA, false};
7927 default:
7928 return {SPF_UNKNOWN, SPNB_NA, false};
7929 }
7930
7931 // If there is a common operand in the already matched min/max and the other
7932 // min/max operands match the compare operands (either directly or inverted),
7933 // then this is min/max of the same flavor.
7934
7935 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
7936 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
7937 if (D == B) {
7938 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
7939 match(A, m_Not(m_Specific(CmpRHS)))))
7940 return {L.Flavor, SPNB_NA, false};
7941 }
7942 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
7943 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
7944 if (C == B) {
7945 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
7946 match(A, m_Not(m_Specific(CmpRHS)))))
7947 return {L.Flavor, SPNB_NA, false};
7948 }
7949 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
7950 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
7951 if (D == A) {
7952 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
7953 match(B, m_Not(m_Specific(CmpRHS)))))
7954 return {L.Flavor, SPNB_NA, false};
7955 }
7956 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
7957 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
7958 if (C == A) {
7959 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
7960 match(B, m_Not(m_Specific(CmpRHS)))))
7961 return {L.Flavor, SPNB_NA, false};
7962 }
7963
7964 return {SPF_UNKNOWN, SPNB_NA, false};
7965}
7966
7967/// If the input value is the result of a 'not' op, constant integer, or vector
7968/// splat of a constant integer, return the bitwise-not source value.
7969/// TODO: This could be extended to handle non-splat vector integer constants.
7971 Value *NotV;
7972 if (match(V, m_Not(m_Value(NotV))))
7973 return NotV;
7974
7975 const APInt *C;
7976 if (match(V, m_APInt(C)))
7977 return ConstantInt::get(V->getType(), ~(*C));
7978
7979 return nullptr;
7980}
7981
7982/// Match non-obvious integer minimum and maximum sequences.
7984 Value *CmpLHS, Value *CmpRHS,
7985 Value *TrueVal, Value *FalseVal,
7986 Value *&LHS, Value *&RHS,
7987 unsigned Depth) {
7988 // Assume success. If there's no match, callers should not use these anyway.
7989 LHS = TrueVal;
7990 RHS = FalseVal;
7991
7992 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
7994 return SPR;
7995
7996 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
7998 return SPR;
7999
8000 // Look through 'not' ops to find disguised min/max.
8001 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8002 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8003 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8004 switch (Pred) {
8005 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8006 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8007 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8008 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8009 default: break;
8010 }
8011 }
8012
8013 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8014 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8015 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8016 switch (Pred) {
8017 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8018 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8019 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8020 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8021 default: break;
8022 }
8023 }
8024
8025 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8026 return {SPF_UNKNOWN, SPNB_NA, false};
8027
8028 const APInt *C1;
8029 if (!match(CmpRHS, m_APInt(C1)))
8030 return {SPF_UNKNOWN, SPNB_NA, false};
8031
8032 // An unsigned min/max can be written with a signed compare.
8033 const APInt *C2;
8034 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8035 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8036 // Is the sign bit set?
8037 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8038 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8039 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8040 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8041
8042 // Is the sign bit clear?
8043 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8044 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8045 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8046 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8047 }
8048
8049 return {SPF_UNKNOWN, SPNB_NA, false};
8050}
8051
8052bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8053 bool AllowPoison) {
8054 assert(X && Y && "Invalid operand");
8055
8056 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8057 if (!match(X, m_Neg(m_Specific(Y))))
8058 return false;
8059
8060 auto *BO = cast<BinaryOperator>(X);
8061 if (NeedNSW && !BO->hasNoSignedWrap())
8062 return false;
8063
8064 auto *Zero = cast<Constant>(BO->getOperand(0));
8065 if (!AllowPoison && !Zero->isNullValue())
8066 return false;
8067
8068 return true;
8069 };
8070
8071 // X = -Y or Y = -X
8072 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8073 return true;
8074
8075 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8076 Value *A, *B;
8077 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8078 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8079 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8081}
8082
8084 FastMathFlags FMF,
8085 Value *CmpLHS, Value *CmpRHS,
8086 Value *TrueVal, Value *FalseVal,
8087 Value *&LHS, Value *&RHS,
8088 unsigned Depth) {
8089 bool HasMismatchedZeros = false;
8090 if (CmpInst::isFPPredicate(Pred)) {
8091 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8092 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8093 // purpose of identifying min/max. Disregard vector constants with undefined
8094 // elements because those can not be back-propagated for analysis.
8095 Value *OutputZeroVal = nullptr;
8096 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8097 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8098 OutputZeroVal = TrueVal;
8099 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8100 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8101 OutputZeroVal = FalseVal;
8102
8103 if (OutputZeroVal) {
8104 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8105 HasMismatchedZeros = true;
8106 CmpLHS = OutputZeroVal;
8107 }
8108 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8109 HasMismatchedZeros = true;
8110 CmpRHS = OutputZeroVal;
8111 }
8112 }
8113 }
8114
8115 LHS = CmpLHS;
8116 RHS = CmpRHS;
8117
8118 // Signed zero may return inconsistent results between implementations.
8119 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8120 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8121 // Therefore, we behave conservatively and only proceed if at least one of the
8122 // operands is known to not be zero or if we don't care about signed zero.
8123 switch (Pred) {
8124 default: break;
8127 if (!HasMismatchedZeros)
8128 break;
8129 [[fallthrough]];
8132 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8133 !isKnownNonZero(CmpRHS))
8134 return {SPF_UNKNOWN, SPNB_NA, false};
8135 }
8136
8137 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8138 bool Ordered = false;
8139
8140 // When given one NaN and one non-NaN input:
8141 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8142 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8143 // ordered comparison fails), which could be NaN or non-NaN.
8144 // so here we discover exactly what NaN behavior is required/accepted.
8145 if (CmpInst::isFPPredicate(Pred)) {
8146 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8147 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8148
8149 if (LHSSafe && RHSSafe) {
8150 // Both operands are known non-NaN.
8151 NaNBehavior = SPNB_RETURNS_ANY;
8152 } else if (CmpInst::isOrdered(Pred)) {
8153 // An ordered comparison will return false when given a NaN, so it
8154 // returns the RHS.
8155 Ordered = true;
8156 if (LHSSafe)
8157 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8158 NaNBehavior = SPNB_RETURNS_NAN;
8159 else if (RHSSafe)
8160 NaNBehavior = SPNB_RETURNS_OTHER;
8161 else
8162 // Completely unsafe.
8163 return {SPF_UNKNOWN, SPNB_NA, false};
8164 } else {
8165 Ordered = false;
8166 // An unordered comparison will return true when given a NaN, so it
8167 // returns the LHS.
8168 if (LHSSafe)
8169 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8170 NaNBehavior = SPNB_RETURNS_OTHER;
8171 else if (RHSSafe)
8172 NaNBehavior = SPNB_RETURNS_NAN;
8173 else
8174 // Completely unsafe.
8175 return {SPF_UNKNOWN, SPNB_NA, false};
8176 }
8177 }
8178
8179 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8180 std::swap(CmpLHS, CmpRHS);
8181 Pred = CmpInst::getSwappedPredicate(Pred);
8182 if (NaNBehavior == SPNB_RETURNS_NAN)
8183 NaNBehavior = SPNB_RETURNS_OTHER;
8184 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8185 NaNBehavior = SPNB_RETURNS_NAN;
8186 Ordered = !Ordered;
8187 }
8188
8189 // ([if]cmp X, Y) ? X : Y
8190 if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
8191 switch (Pred) {
8192 default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8193 case ICmpInst::ICMP_UGT:
8194 case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false};
8195 case ICmpInst::ICMP_SGT:
8196 case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false};
8197 case ICmpInst::ICMP_ULT:
8198 case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false};
8199 case ICmpInst::ICMP_SLT:
8200 case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false};
8201 case FCmpInst::FCMP_UGT:
8202 case FCmpInst::FCMP_UGE:
8203 case FCmpInst::FCMP_OGT:
8204 case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered};
8205 case FCmpInst::FCMP_ULT:
8206 case FCmpInst::FCMP_ULE:
8207 case FCmpInst::FCMP_OLT:
8208 case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered};
8209 }
8210 }
8211
8212 if (isKnownNegation(TrueVal, FalseVal)) {
8213 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8214 // match against either LHS or sext(LHS).
8215 auto MaybeSExtCmpLHS =
8216 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8217 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8218 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8219 if (match(TrueVal, MaybeSExtCmpLHS)) {
8220 // Set the return values. If the compare uses the negated value (-X >s 0),
8221 // swap the return values because the negated value is always 'RHS'.
8222 LHS = TrueVal;
8223 RHS = FalseVal;
8224 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8225 std::swap(LHS, RHS);
8226
8227 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8228 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8229 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8230 return {SPF_ABS, SPNB_NA, false};
8231
8232 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8233 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8234 return {SPF_ABS, SPNB_NA, false};
8235
8236 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8237 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8238 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8239 return {SPF_NABS, SPNB_NA, false};
8240 }
8241 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8242 // Set the return values. If the compare uses the negated value (-X >s 0),
8243 // swap the return values because the negated value is always 'RHS'.
8244 LHS = FalseVal;
8245 RHS = TrueVal;
8246 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8247 std::swap(LHS, RHS);
8248
8249 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8250 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8251 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8252 return {SPF_NABS, SPNB_NA, false};
8253
8254 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8255 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8256 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8257 return {SPF_ABS, SPNB_NA, false};
8258 }
8259 }
8260
8261 if (CmpInst::isIntPredicate(Pred))
8262 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8263
8264 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8265 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8266 // semantics than minNum. Be conservative in such case.
8267 if (NaNBehavior != SPNB_RETURNS_ANY ||
8268 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8269 !isKnownNonZero(CmpRHS)))
8270 return {SPF_UNKNOWN, SPNB_NA, false};
8271
8272 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8273}
8274
8275/// Helps to match a select pattern in case of a type mismatch.
8276///
8277/// The function processes the case when type of true and false values of a
8278/// select instruction differs from type of the cmp instruction operands because
8279/// of a cast instruction. The function checks if it is legal to move the cast
8280/// operation after "select". If yes, it returns the new second value of
8281/// "select" (with the assumption that cast is moved):
8282/// 1. As operand of cast instruction when both values of "select" are same cast
8283/// instructions.
8284/// 2. As restored constant (by applying reverse cast operation) when the first
8285/// value of the "select" is a cast operation and the second value is a
8286/// constant.
8287/// NOTE: We return only the new second value because the first value could be
8288/// accessed as operand of cast instruction.
8289static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8290 Instruction::CastOps *CastOp) {
8291 auto *Cast1 = dyn_cast<CastInst>(V1);
8292 if (!Cast1)
8293 return nullptr;
8294
8295 *CastOp = Cast1->getOpcode();
8296 Type *SrcTy = Cast1->getSrcTy();
8297 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8298 // If V1 and V2 are both the same cast from the same type, look through V1.
8299 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
8300 return Cast2->getOperand(0);
8301 return nullptr;
8302 }
8303
8304 auto *C = dyn_cast<Constant>(V2);
8305 if (!C)
8306 return nullptr;
8307
8308 const DataLayout &DL = CmpI->getModule()->getDataLayout();
8309 Constant *CastedTo = nullptr;
8310 switch (*CastOp) {
8311 case Instruction::ZExt:
8312 if (CmpI->isUnsigned())
8313 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8314 break;
8315 case Instruction::SExt:
8316 if (CmpI->isSigned())
8317 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8318 break;
8319 case Instruction::Trunc:
8320 Constant *CmpConst;
8321 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8322 CmpConst->getType() == SrcTy) {
8323 // Here we have the following case:
8324 //
8325 // %cond = cmp iN %x, CmpConst
8326 // %tr = trunc iN %x to iK
8327 // %narrowsel = select i1 %cond, iK %t, iK C
8328 //
8329 // We can always move trunc after select operation:
8330 //
8331 // %cond = cmp iN %x, CmpConst
8332 // %widesel = select i1 %cond, iN %x, iN CmpConst
8333 // %tr = trunc iN %widesel to iK
8334 //
8335 // Note that C could be extended in any way because we don't care about
8336 // upper bits after truncation. It can't be abs pattern, because it would
8337 // look like:
8338 //
8339 // select i1 %cond, x, -x.
8340 //
8341 // So only min/max pattern could be matched. Such match requires widened C
8342 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8343 // CmpConst == C is checked below.
8344 CastedTo = CmpConst;
8345 } else {
8346 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8347 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8348 }
8349 break;
8350 case Instruction::FPTrunc:
8351 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8352 break;
8353 case Instruction::FPExt:
8354 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8355 break;
8356 case Instruction::FPToUI:
8357 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8358 break;
8359 case Instruction::FPToSI:
8360 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8361 break;
8362 case Instruction::UIToFP:
8363 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8364 break;
8365 case Instruction::SIToFP:
8366 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8367 break;
8368 default:
8369 break;
8370 }
8371
8372 if (!CastedTo)
8373 return nullptr;
8374
8375 // Make sure the cast doesn't lose any information.
8376 Constant *CastedBack =
8377 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8378 if (CastedBack && CastedBack != C)
8379 return nullptr;
8380
8381 return CastedTo;
8382}
8383
8385 Instruction::CastOps *CastOp,
8386 unsigned Depth) {
8388 return {SPF_UNKNOWN, SPNB_NA, false};
8389
8390 SelectInst *SI = dyn_cast<SelectInst>(V);
8391 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
8392
8393 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
8394 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
8395
8396 Value *TrueVal = SI->getTrueValue();
8397 Value *FalseVal = SI->getFalseValue();
8398
8399 return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
8400 CastOp, Depth);
8401}
8402
8404 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
8405 Instruction::CastOps *CastOp, unsigned Depth) {
8406 CmpInst::Predicate Pred = CmpI->getPredicate();
8407 Value *CmpLHS = CmpI->getOperand(0);
8408 Value *CmpRHS = CmpI->getOperand(1);
8409 FastMathFlags FMF;
8410 if (isa<FPMathOperator>(CmpI))
8411 FMF = CmpI->getFastMathFlags();
8412
8413 // Bail out early.
8414 if (CmpI->isEquality())
8415 return {SPF_UNKNOWN, SPNB_NA, false};
8416
8417 // Deal with type mismatches.
8418 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
8419 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
8420 // If this is a potential fmin/fmax with a cast to integer, then ignore
8421 // -0.0 because there is no corresponding integer value.
8422 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
8423 FMF.setNoSignedZeros();
8424 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
8425 cast<CastInst>(TrueVal)->getOperand(0), C,
8426 LHS, RHS, Depth);
8427 }
8428 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
8429 // If this is a potential fmin/fmax with a cast to integer, then ignore
8430 // -0.0 because there is no corresponding integer value.
8431 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
8432 FMF.setNoSignedZeros();
8433 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
8434 C, cast<CastInst>(FalseVal)->getOperand(0),
8435 LHS, RHS, Depth);
8436 }
8437 }
8438 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
8439 LHS, RHS, Depth);
8440}
8441
8443 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
8444 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
8445 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
8446 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
8447 if (SPF == SPF_FMINNUM)
8448 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
8449 if (SPF == SPF_FMAXNUM)
8450 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
8451 llvm_unreachable("unhandled!");
8452}
8453
8455 if (SPF == SPF_SMIN) return SPF_SMAX;
8456 if (SPF == SPF_UMIN) return SPF_UMAX;
8457 if (SPF == SPF_SMAX) return SPF_SMIN;
8458 if (SPF == SPF_UMAX) return SPF_UMIN;
8459 llvm_unreachable("unhandled!");
8460}
8461
8463 switch (MinMaxID) {
8464 case Intrinsic::smax: return Intrinsic::smin;
8465 case Intrinsic::smin: return Intrinsic::smax;
8466 case Intrinsic::umax: return Intrinsic::umin;
8467 case Intrinsic::umin: return Intrinsic::umax;
8468 // Please note that next four intrinsics may produce the same result for
8469 // original and inverted case even if X != Y due to NaN is handled specially.
8470 case Intrinsic::maximum: return Intrinsic::minimum;
8471 case Intrinsic::minimum: return Intrinsic::maximum;
8472 case Intrinsic::maxnum: return Intrinsic::minnum;
8473 case Intrinsic::minnum: return Intrinsic::maxnum;
8474 default: llvm_unreachable("Unexpected intrinsic");
8475 }
8476}
8477
8479 switch (SPF) {
8482 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
8483 case SPF_UMIN: return APInt::getMinValue(BitWidth);
8484 default: llvm_unreachable("Unexpected flavor");
8485 }
8486}
8487
8488std::pair<Intrinsic::ID, bool>
8490 // Check if VL contains select instructions that can be folded into a min/max
8491 // vector intrinsic and return the intrinsic if it is possible.
8492 // TODO: Support floating point min/max.
8493 bool AllCmpSingleUse = true;
8494 SelectPatternResult SelectPattern;
8495 SelectPattern.Flavor = SPF_UNKNOWN;
8496 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
8497 Value *LHS, *RHS;
8498 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
8499 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor) ||
8500 CurrentPattern.Flavor == SPF_FMINNUM ||
8501 CurrentPattern.Flavor == SPF_FMAXNUM ||
8502 !I->getType()->isIntOrIntVectorTy())
8503 return false;
8504 if (SelectPattern.Flavor != SPF_UNKNOWN &&
8505 SelectPattern.Flavor != CurrentPattern.Flavor)
8506 return false;
8507 SelectPattern = CurrentPattern;
8508 AllCmpSingleUse &=
8510 return true;
8511 })) {
8512 switch (SelectPattern.Flavor) {
8513 case SPF_SMIN:
8514 return {Intrinsic::smin, AllCmpSingleUse};
8515 case SPF_UMIN:
8516 return {Intrinsic::umin, AllCmpSingleUse};
8517 case SPF_SMAX:
8518 return {Intrinsic::smax, AllCmpSingleUse};
8519 case SPF_UMAX:
8520 return {Intrinsic::umax, AllCmpSingleUse};
8521 default:
8522 llvm_unreachable("unexpected select pattern flavor");
8523 }
8524 }
8525 return {Intrinsic::not_intrinsic, false};
8526}
8527
8529 Value *&Start, Value *&Step) {
8530 // Handle the case of a simple two-predecessor recurrence PHI.
8531 // There's a lot more that could theoretically be done here, but
8532 // this is sufficient to catch some interesting cases.
8533 if (P->getNumIncomingValues() != 2)
8534 return false;
8535
8536 for (unsigned i = 0; i != 2; ++i) {
8537 Value *L = P->getIncomingValue(i);
8538 Value *R = P->getIncomingValue(!i);
8539 auto *LU = dyn_cast<BinaryOperator>(L);
8540 if (!LU)
8541 continue;
8542 unsigned Opcode = LU->getOpcode();
8543
8544 switch (Opcode) {
8545 default:
8546 continue;
8547 // TODO: Expand list -- xor, div, gep, uaddo, etc..
8548 case Instruction::LShr:
8549 case Instruction::AShr:
8550 case Instruction::Shl:
8551 case Instruction::Add:
8552 case Instruction::Sub:
8553 case Instruction::And:
8554 case Instruction::Or:
8555 case Instruction::Mul:
8556 case Instruction::FMul: {
8557 Value *LL = LU->getOperand(0);
8558 Value *LR = LU->getOperand(1);
8559 // Find a recurrence.
8560 if (LL == P)
8561 L = LR;
8562 else if (LR == P)
8563 L = LL;
8564 else
8565 continue; // Check for recurrence with L and R flipped.
8566
8567 break; // Match!
8568 }
8569 };
8570
8571 // We have matched a recurrence of the form:
8572 // %iv = [R, %entry], [%iv.next, %backedge]
8573 // %iv.next = binop %iv, L
8574 // OR
8575 // %iv = [R, %entry], [%iv.next, %backedge]
8576 // %iv.next = binop L, %iv
8577 BO = LU;
8578 Start = R;
8579 Step = L;
8580 return true;
8581 }
8582 return false;
8583}
8584
8586 Value *&Start, Value *&Step) {
8587 BinaryOperator *BO = nullptr;
8588 P = dyn_cast<PHINode>(I->getOperand(0));
8589 if (!P)
8590 P = dyn_cast<PHINode>(I->getOperand(1));
8591 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
8592}
8593
8594/// Return true if "icmp Pred LHS RHS" is always true.
8595static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
8596 const Value *RHS) {
8597 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
8598 return true;
8599
8600 switch (Pred) {
8601 default:
8602 return false;
8603
8604 case CmpInst::ICMP_SLE: {
8605 const APInt *C;
8606
8607 // LHS s<= LHS +_{nsw} C if C >= 0
8608 // LHS s<= LHS | C if C >= 0
8609 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
8611 return !C->isNegative();
8612
8613 // LHS s<= smax(LHS, V) for any V
8615 return true;
8616
8617 // smin(RHS, V) s<= RHS for any V
8619 return true;
8620
8621 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
8622 const Value *X;
8623 const APInt *CLHS, *CRHS;
8624 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
8626 return CLHS->sle(*CRHS);
8627
8628 return false;
8629 }
8630
8631 case CmpInst::ICMP_ULE: {
8632 // LHS u<= LHS +_{nuw} V for any V
8633 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
8634 cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
8635 return true;
8636
8637 // LHS u<= LHS | V for any V
8638 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
8639 return true;
8640
8641 // LHS u<= umax(LHS, V) for any V
8643 return true;
8644
8645 // RHS >> V u<= RHS for any V
8646 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
8647 return true;
8648
8649 // RHS u/ C_ugt_1 u<= RHS
8650 const APInt *C;
8651 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
8652 return true;
8653
8654 // RHS & V u<= RHS for any V
8656 return true;
8657
8658 // umin(RHS, V) u<= RHS for any V
8660 return true;
8661
8662 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
8663 const Value *X;
8664 const APInt *CLHS, *CRHS;
8665 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
8667 return CLHS->ule(*CRHS);
8668
8669 return false;
8670 }
8671 }
8672}
8673
8674/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
8675/// ALHS ARHS" is true. Otherwise, return std::nullopt.
8676static std::optional<bool>
8678 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
8679 switch (Pred) {
8680 default:
8681 return std::nullopt;
8682
8683 case CmpInst::ICMP_SLT:
8684 case CmpInst::ICMP_SLE:
8685 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
8687 return true;
8688 return std::nullopt;
8689
8690 case CmpInst::ICMP_SGT:
8691 case CmpInst::ICMP_SGE:
8692 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
8694 return true;
8695 return std::nullopt;
8696
8697 case CmpInst::ICMP_ULT:
8698 case CmpInst::ICMP_ULE:
8699 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
8701 return true;
8702 return std::nullopt;
8703
8704 case CmpInst::ICMP_UGT:
8705 case CmpInst::ICMP_UGE:
8706 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
8708 return true;
8709 return std::nullopt;
8710 }
8711}
8712
8713/// Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true.
8714/// Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false.
8715/// Otherwise, return std::nullopt if we can't infer anything.
8716static std::optional<bool>
8718 CmpInst::Predicate RPred) {
8719 if (CmpInst::isImpliedTrueByMatchingCmp(LPred, RPred))
8720 return true;
8721 if (CmpInst::isImpliedFalseByMatchingCmp(LPred, RPred))
8722 return false;
8723
8724 return std::nullopt;
8725}
8726
8727/// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true.
8728/// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false.
8729/// Otherwise, return std::nullopt if we can't infer anything.
8731 CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred,
8732 const APInt &RC) {
8735 ConstantRange Intersection = DomCR.intersectWith(CR);
8736 ConstantRange Difference = DomCR.difference(CR);
8737 if (Intersection.isEmptySet())
8738 return false;
8739 if (Difference.isEmptySet())
8740 return true;
8741 return std::nullopt;
8742}
8743
8744/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
8745/// is true. Return false if LHS implies RHS is false. Otherwise, return
8746/// std::nullopt if we can't infer anything.
8747static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
8748 CmpInst::Predicate RPred,
8749 const Value *R0, const Value *R1,
8750 const DataLayout &DL,
8751 bool LHSIsTrue) {
8752 Value *L0 = LHS->getOperand(0);
8753 Value *L1 = LHS->getOperand(1);
8754
8755 // The rest of the logic assumes the LHS condition is true. If that's not the
8756 // case, invert the predicate to make it so.
8757 CmpInst::Predicate LPred =
8758 LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
8759
8760 // We can have non-canonical operands, so try to normalize any common operand
8761 // to L0/R0.
8762 if (L0 == R1) {
8763 std::swap(R0, R1);
8764 RPred = ICmpInst::getSwappedPredicate(RPred);
8765 }
8766 if (R0 == L1) {
8767 std::swap(L0, L1);
8768 LPred = ICmpInst::getSwappedPredicate(LPred);
8769 }
8770 if (L1 == R1) {
8771 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
8772 if (L0 != R0 || match(L0, m_ImmConstant())) {
8773 std::swap(L0, L1);
8774 LPred = ICmpInst::getSwappedPredicate(LPred);
8775 std::swap(R0, R1);
8776 RPred = ICmpInst::getSwappedPredicate(RPred);
8777 }
8778 }
8779
8780 // Can we infer anything when the 0-operands match and the 1-operands are
8781 // constants (not necessarily matching)?
8782 const APInt *LC, *RC;
8783 if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
8784 return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
8785
8786 // Can we infer anything when the two compares have matching operands?
8787 if (L0 == R0 && L1 == R1)
8788 return isImpliedCondMatchingOperands(LPred, RPred);
8789
8790 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
8791 if (L0 == R0 &&
8792 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
8793 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
8794 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
8795 return LPred == RPred;
8796
8797 if (LPred == RPred)
8798 return isImpliedCondOperands(LPred, L0, L1, R0, R1);
8799
8800 return std::nullopt;
8801}
8802
8803/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
8804/// false. Otherwise, return std::nullopt if we can't infer anything. We
8805/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
8806/// instruction.
8807static std::optional<bool>
8809 const Value *RHSOp0, const Value *RHSOp1,
8810 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
8811 // The LHS must be an 'or', 'and', or a 'select' instruction.
8812 assert((LHS->getOpcode() == Instruction::And ||
8813 LHS->getOpcode() == Instruction::Or ||
8814 LHS->getOpcode() == Instruction::Select) &&
8815 "Expected LHS to be 'and', 'or', or 'select'.");
8816
8817 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
8818
8819 // If the result of an 'or' is false, then we know both legs of the 'or' are
8820 // false. Similarly, if the result of an 'and' is true, then we know both
8821 // legs of the 'and' are true.
8822 const Value *ALHS, *ARHS;
8823 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
8824 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
8825 // FIXME: Make this non-recursion.
8826 if (std::optional<bool> Implication = isImpliedCondition(
8827 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
8828 return Implication;
8829 if (std::optional<bool> Implication = isImpliedCondition(
8830 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
8831 return Implication;
8832 return std::nullopt;
8833 }
8834 return std::nullopt;
8835}
8836
8837std::optional<bool>
8839 const Value *RHSOp0, const Value *RHSOp1,
8840 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
8841 // Bail out when we hit the limit.
8843 return std::nullopt;
8844
8845 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
8846 // example.
8847 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
8848 return std::nullopt;
8849
8851 "Expected integer type only!");
8852
8853 // Match not
8854 if (match(LHS, m_Not(m_Value(LHS))))
8855 LHSIsTrue = !LHSIsTrue;
8856
8857 // Both LHS and RHS are icmps.
8858 const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
8859 if (LHSCmp)
8860 return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
8861
8862 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
8863 /// the RHS to be an icmp.
8864 /// FIXME: Add support for and/or/select on the RHS.
8865 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
8866 if ((LHSI->getOpcode() == Instruction::And ||
8867 LHSI->getOpcode() == Instruction::Or ||
8868 LHSI->getOpcode() == Instruction::Select))
8869 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
8870 Depth);
8871 }
8872 return std::nullopt;
8873}
8874
8875std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
8876 const DataLayout &DL,
8877 bool LHSIsTrue, unsigned Depth) {
8878 // LHS ==> RHS by definition
8879 if (LHS == RHS)
8880 return LHSIsTrue;
8881
8882 // Match not
8883 bool InvertRHS = false;
8884 if (match(RHS, m_Not(m_Value(RHS)))) {
8885 if (LHS == RHS)
8886 return !LHSIsTrue;
8887 InvertRHS = true;
8888 }
8889
8890 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
8891 if (auto Implied = isImpliedCondition(
8892 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
8893 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
8894 return InvertRHS ? !*Implied : *Implied;
8895 return std::nullopt;
8896 }
8897
8899 return std::nullopt;
8900
8901 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
8902 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
8903 const Value *RHS1, *RHS2;
8904 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
8905 if (std::optional<bool> Imp =
8906 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
8907 if (*Imp == true)
8908 return !InvertRHS;
8909 if (std::optional<bool> Imp =
8910 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
8911 if (*Imp == true)
8912 return !InvertRHS;
8913 }
8914 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
8915 if (std::optional<bool> Imp =
8916 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
8917 if (*Imp == false)
8918 return InvertRHS;
8919 if (std::optional<bool> Imp =
8920 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
8921 if (*Imp == false)
8922 return InvertRHS;
8923 }
8924
8925 return std::nullopt;
8926}
8927
8928// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
8929// condition dominating ContextI or nullptr, if no condition is found.
8930static std::pair<Value *, bool>
8932 if (!ContextI || !ContextI->getParent())
8933 return {nullptr, false};
8934
8935 // TODO: This is a poor/cheap way to determine dominance. Should we use a
8936 // dominator tree (eg, from a SimplifyQuery) instead?
8937 const BasicBlock *ContextBB = ContextI->getParent();
8938 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
8939 if (!PredBB)
8940 return {nullptr, false};
8941
8942 // We need a conditional branch in the predecessor.
8943 Value *PredCond;
8944 BasicBlock *TrueBB, *FalseBB;
8945 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
8946 return {nullptr, false};
8947
8948 // The branch should get simplified. Don't bother simplifying this condition.
8949 if (TrueBB == FalseBB)
8950 return {nullptr, false};
8951
8952 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
8953 "Predecessor block does not point to successor?");
8954
8955 // Is this condition implied by the predecessor condition?
8956 return {PredCond, TrueBB == ContextBB};
8957}
8958
8959std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
8960 const Instruction *ContextI,
8961 const DataLayout &DL) {
8962 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
8963 auto PredCond = getDomPredecessorCondition(ContextI);
8964 if (PredCond.first)
8965 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
8966 return std::nullopt;
8967}
8968
8970 const Value *LHS,
8971 const Value *RHS,
8972 const Instruction *ContextI,
8973 const DataLayout &DL) {
8974 auto PredCond = getDomPredecessorCondition(ContextI);
8975 if (PredCond.first)
8976 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
8977 PredCond.second);
8978 return std::nullopt;
8979}
8980
8982 APInt &Upper, const InstrInfoQuery &IIQ,
8983 bool PreferSignedRange) {
8984 unsigned Width = Lower.getBitWidth();
8985 const APInt *C;
8986 switch (BO.getOpcode()) {
8987 case Instruction::Add:
8988 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
8989 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
8990 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
8991
8992 // If the caller expects a signed compare, then try to use a signed range.
8993 // Otherwise if both no-wraps are set, use the unsigned range because it
8994 // is never larger than the signed range. Example:
8995 // "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
8996 if (PreferSignedRange && HasNSW && HasNUW)
8997 HasNUW = false;
8998
8999 if (HasNUW) {
9000 // 'add nuw x, C' produces [C, UINT_MAX].
9001 Lower = *C;
9002 } else if (HasNSW) {
9003 if (C->isNegative()) {
9004 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9006 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9007 } else {
9008 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9009 Lower = APInt::getSignedMinValue(Width) + *C;
9010 Upper = APInt::getSignedMaxValue(Width) + 1;
9011 }
9012 }
9013 }
9014 break;
9015
9016 case Instruction::And:
9017 if (match(BO.getOperand(1), m_APInt(C)))
9018 // 'and x, C' produces [0, C].
9019 Upper = *C + 1;
9020 // X & -X is a power of two or zero. So we can cap the value at max power of
9021 // two.
9022 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9023 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9024 Upper = APInt::getSignedMinValue(Width) + 1;
9025 break;
9026
9027 case Instruction::Or:
9028 if (match(BO.getOperand(1), m_APInt(C)))
9029 // 'or x, C' produces [C, UINT_MAX].
9030 Lower = *C;
9031 break;
9032
9033 case Instruction::AShr:
9034 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9035 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9037 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9038 } else if (match(BO.getOperand(0), m_APInt(C))) {
9039 unsigned ShiftAmount = Width - 1;
9040 if (!C->isZero() && IIQ.isExact(&BO))
9041 ShiftAmount = C->countr_zero();
9042 if (C->isNegative()) {
9043 // 'ashr C, x' produces [C, C >> (Width-1)]
9044 Lower = *C;
9045 Upper = C->ashr(ShiftAmount) + 1;
9046 } else {
9047 // 'ashr C, x' produces [C >> (Width-1), C]
9048 Lower = C->ashr(ShiftAmount);
9049 Upper = *C + 1;
9050 }
9051 }
9052 break;
9053
9054 case Instruction::LShr:
9055 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9056 // 'lshr x, C' produces [0, UINT_MAX >> C].
9057 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9058 } else if (match(BO.getOperand(0), m_APInt(C))) {
9059 // 'lshr C, x' produces [C >> (Width-1), C].
9060 unsigned ShiftAmount = Width - 1;
9061 if (!C->isZero() && IIQ.isExact(&BO))
9062 ShiftAmount = C->countr_zero();
9063 Lower = C->lshr(ShiftAmount);
9064 Upper = *C + 1;
9065 }
9066 break;
9067
9068 case Instruction::Shl:
9069 if (match(BO.getOperand(0), m_APInt(C))) {
9070 if (IIQ.hasNoUnsignedWrap(&BO)) {
9071 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9072 Lower = *C;
9073 Upper = Lower.shl(Lower.countl_zero()) + 1;
9074 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9075 if (C->isNegative()) {
9076 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9077 unsigned ShiftAmount = C->countl_one() - 1;
9078 Lower = C->shl(ShiftAmount);
9079 Upper = *C + 1;
9080 } else {
9081 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9082 unsigned ShiftAmount = C->countl_zero() - 1;
9083 Lower = *C;
9084 Upper = C->shl(ShiftAmount) + 1;
9085 }
9086 } else {
9087 // If lowbit is set, value can never be zero.
9088 if ((*C)[0])
9089 Lower = APInt::getOneBitSet(Width, 0);
9090 // If we are shifting a constant the largest it can be is if the longest
9091 // sequence of consecutive ones is shifted to the highbits (breaking
9092 // ties for which sequence is higher). At the moment we take a liberal
9093 // upper bound on this by just popcounting the constant.
9094 // TODO: There may be a bitwise trick for it longest/highest
9095 // consecutative sequence of ones (naive method is O(Width) loop).
9096 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9097 }
9098 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9099 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9100 }
9101 break;
9102
9103 case Instruction::SDiv:
9104 if (match(BO.getOperand(1), m_APInt(C))) {
9105 APInt IntMin = APInt::getSignedMinValue(Width);
9106 APInt IntMax = APInt::getSignedMaxValue(Width);
9107 if (C->isAllOnes()) {
9108 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9109 // where C != -1 and C != 0 and C != 1
9110 Lower = IntMin + 1;
9111 Upper = IntMax + 1;
9112 } else if (C->countl_zero() < Width - 1) {
9113 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9114 // where C != -1 and C != 0 and C != 1
9115 Lower = IntMin.sdiv(*C);
9116 Upper = IntMax.sdiv(*C);
9117 if (Lower.sgt(Upper))
9119 Upper = Upper + 1;
9120 assert(Upper != Lower && "Upper part of range has wrapped!");
9121 }
9122 } else if (match(BO.getOperand(0), m_APInt(C))) {
9123 if (C->isMinSignedValue()) {
9124 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9125 Lower = *C;
9126 Upper = Lower.lshr(1) + 1;
9127 } else {
9128 // 'sdiv C, x' produces [-|C|, |C|].
9129 Upper = C->abs() + 1;
9130 Lower = (-Upper) + 1;
9131 }
9132 }
9133 break;
9134
9135 case Instruction::UDiv:
9136 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9137 // 'udiv x, C' produces [0, UINT_MAX / C].
9138 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9139 } else if (match(BO.getOperand(0), m_APInt(C))) {
9140 // 'udiv C, x' produces [0, C].
9141 Upper = *C + 1;
9142 }
9143 break;
9144
9145 case Instruction::SRem:
9146 if (match(BO.getOperand(1), m_APInt(C))) {
9147 // 'srem x, C' produces (-|C|, |C|).
9148 Upper = C->abs();
9149 Lower = (-Upper) + 1;
9150 } else if (match(BO.getOperand(0), m_APInt(C))) {
9151 if (C->isNegative()) {
9152 // 'srem -|C|, x' produces [-|C|, 0].
9153 Upper = 1;
9154 Lower = *C;
9155 } else {
9156 // 'srem |C|, x' produces [0, |C|].
9157 Upper = *C + 1;
9158 }
9159 }
9160 break;
9161
9162 case Instruction::URem:
9163 if (match(BO.getOperand(1), m_APInt(C)))
9164 // 'urem x, C' produces [0, C).
9165 Upper = *C;
9166 else if (match(BO.getOperand(0), m_APInt(C)))
9167 // 'urem C, x' produces [0, C].
9168 Upper = *C + 1;
9169 break;
9170
9171 default:
9172 break;
9173 }
9174}
9175
9177 unsigned Width = II.getType()->getScalarSizeInBits();
9178 const APInt *C;
9179 switch (II.getIntrinsicID()) {
9180 case Intrinsic::ctpop:
9181 case Intrinsic::ctlz:
9182 case Intrinsic::cttz:
9183 // Maximum of set/clear bits is the bit width.
9185 APInt(Width, Width + 1));
9186 case Intrinsic::uadd_sat:
9187 // uadd.sat(x, C) produces [C, UINT_MAX].
9188 if (match(II.getOperand(0), m_APInt(C)) ||
9189 match(II.getOperand(1), m_APInt(C)))
9191 break;
9192 case Intrinsic::sadd_sat:
9193 if (match(II.getOperand(0), m_APInt(C)) ||
9194 match(II.getOperand(1), m_APInt(C))) {
9195 if (C->isNegative())
9196 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9198 APInt::getSignedMaxValue(Width) + *C +
9199 1);
9200
9201 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9203 APInt::getSignedMaxValue(Width) + 1);
9204 }
9205 break;
9206 case Intrinsic::usub_sat:
9207 // usub.sat(C, x) produces [0, C].
9208 if (match(II.getOperand(0), m_APInt(C)))
9209 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9210
9211 // usub.sat(x, C) produces [0, UINT_MAX - C].
9212 if (match(II.getOperand(1), m_APInt(C)))
9214 APInt::getMaxValue(Width) - *C + 1);
9215 break;
9216 case Intrinsic::ssub_sat:
9217 if (match(II.getOperand(0), m_APInt(C))) {
9218 if (C->isNegative())
9219 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9221 *C - APInt::getSignedMinValue(Width) +
9222 1);
9223
9224 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9226 APInt::getSignedMaxValue(Width) + 1);
9227 } else if (match(II.getOperand(1), m_APInt(C))) {
9228 if (C->isNegative())
9229 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9231 APInt::getSignedMaxValue(Width) + 1);
9232
9233 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9235 APInt::getSignedMaxValue(Width) - *C +
9236 1);
9237 }
9238 break;
9239 case Intrinsic::umin:
9240 case Intrinsic::umax:
9241 case Intrinsic::smin:
9242 case Intrinsic::smax:
9243 if (!match(II.getOperand(0), m_APInt(C)) &&
9244 !match(II.getOperand(1), m_APInt(C)))
9245 break;
9246
9247 switch (II.getIntrinsicID()) {
9248 case Intrinsic::umin:
9249 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9250 case Intrinsic::umax:
9252 case Intrinsic::smin:
9254 *C + 1);
9255 case Intrinsic::smax:
9257 APInt::getSignedMaxValue(Width) + 1);
9258 default:
9259 llvm_unreachable("Must be min/max intrinsic");
9260 }
9261 break;
9262 case Intrinsic::abs:
9263 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
9264 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9265 if (match(II.getOperand(1), m_One()))
9267 APInt::getSignedMaxValue(Width) + 1);
9268
9270 APInt::getSignedMinValue(Width) + 1);
9271 case Intrinsic::vscale:
9272 if (!II.getParent() || !II.getFunction())
9273 break;
9274 return getVScaleRange(II.getFunction(), Width);
9275 default:
9276 break;
9277 }
9278
9279 return ConstantRange::getFull(Width);
9280}
9281
9283 const InstrInfoQuery &IIQ) {
9284 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
9285 const Value *LHS = nullptr, *RHS = nullptr;
9287 if (R.Flavor == SPF_UNKNOWN)
9288 return ConstantRange::getFull(BitWidth);
9289
9290 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
9291 // If the negation part of the abs (in RHS) has the NSW flag,
9292 // then the result of abs(X) is [0..SIGNED_MAX],
9293 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9294 if (match(RHS, m_Neg(m_Specific(LHS))) &&
9295 IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
9298
9301 }
9302
9303 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
9304 // The result of -abs(X) is <= 0.
9306 APInt(BitWidth, 1));
9307 }
9308
9309 const APInt *C;
9310 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
9311 return ConstantRange::getFull(BitWidth);
9312
9313 switch (R.Flavor) {
9314 case SPF_UMIN:
9316 case SPF_UMAX:
9318 case SPF_SMIN:
9320 *C + 1);
9321 case SPF_SMAX:
9324 default:
9325 return ConstantRange::getFull(BitWidth);
9326 }
9327}
9328
9330 // The maximum representable value of a half is 65504. For floats the maximum
9331 // value is 3.4e38 which requires roughly 129 bits.
9332 unsigned BitWidth = I->getType()->getScalarSizeInBits();
9333 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
9334 return;
9335 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
9336 Lower = APInt(BitWidth, -65504);
9337 Upper = APInt(BitWidth, 65505);
9338 }
9339
9340 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
9341 // For a fptoui the lower limit is left as 0.
9342 Upper = APInt(BitWidth, 65505);
9343 }
9344}
9345
9347 bool UseInstrInfo, AssumptionCache *AC,
9348 const Instruction *CtxI,
9349 const DominatorTree *DT,
9350 unsigned Depth) {
9351 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
9352
9354 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
9355
9356 const APInt *C;
9357 if (match(V, m_APInt(C)))
9358 return ConstantRange(*C);
9359 unsigned BitWidth = V->getType()->getScalarSizeInBits();
9360
9361 if (auto *VC = dyn_cast<ConstantDataVector>(V)) {
9362 ConstantRange CR = ConstantRange::getEmpty(BitWidth);
9363 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
9364 ++ElemIdx)
9365 CR = CR.unionWith(VC->getElementAsAPInt(ElemIdx));
9366 return CR;
9367 }
9368
9369 InstrInfoQuery IIQ(UseInstrInfo);
9370 ConstantRange CR = ConstantRange::getFull(BitWidth);
9371 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
9372 APInt Lower = APInt(BitWidth, 0);
9373 APInt Upper = APInt(BitWidth, 0);
9374 // TODO: Return ConstantRange.
9375 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
9377 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
9378 CR = getRangeForIntrinsic(*II);
9379 else if (auto *SI = dyn_cast<SelectInst>(V)) {
9381 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
9383 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
9384 CR = CRTrue.unionWith(CRFalse);
9385 CR = CR.intersectWith(getRangeForSelectPattern(*SI, IIQ));
9386 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
9387 APInt Lower = APInt(BitWidth, 0);
9388 APInt Upper = APInt(BitWidth, 0);
9389 // TODO: Return ConstantRange.
9390 setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
9392 } else if (const auto *A = dyn_cast<Argument>(V))
9393 if (std::optional<ConstantRange> Range = A->getRange())
9394 CR = *Range;
9395
9396 if (auto *I = dyn_cast<Instruction>(V)) {
9397 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
9399
9400 if (const auto *CB = dyn_cast<CallBase>(V))
9401 if (std::optional<ConstantRange> Range = CB->getRange())
9402 CR = CR.intersectWith(*Range);
9403 }
9404
9405 if (CtxI && AC) {
9406 // Try to restrict the range based on information from assumptions.
9407 for (auto &AssumeVH : AC->assumptionsFor(V)) {
9408 if (!AssumeVH)
9409 continue;
9410 CallInst *I = cast<CallInst>(AssumeVH);
9411 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
9412 "Got assumption for the wrong function!");
9413 assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
9414 "must be an assume intrinsic");
9415
9416 if (!isValidAssumeForContext(I, CtxI, DT))
9417 continue;
9418 Value *Arg = I->getArgOperand(0);
9419 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
9420 // Currently we just use information from comparisons.
9421 if (!Cmp || Cmp->getOperand(0) != V)
9422 continue;
9423 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
9425 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
9426 UseInstrInfo, AC, I, DT, Depth + 1);
9427 CR = CR.intersectWith(
9428 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
9429 }
9430 }
9431
9432 return CR;
9433}
9434
9435static void
9437 function_ref<void(Value *)> InsertAffected) {
9438 assert(V != nullptr);
9439 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
9440 InsertAffected(V);
9441 } else if (auto *I = dyn_cast<Instruction>(V)) {
9442 InsertAffected(V);
9443
9444 // Peek through unary operators to find the source of the condition.
9445 Value *Op;
9447 if (isa<Instruction>(Op) || isa<Argument>(Op))
9448 InsertAffected(Op);
9449 }
9450 }
9451}
9452
9454 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
9455 auto AddAffected = [&InsertAffected](Value *V) {
9456 addValueAffectedByCondition(V, InsertAffected);
9457 };
9458
9459 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
9460 if (IsAssume) {
9461 AddAffected(LHS);
9462 AddAffected(RHS);
9463 } else if (match(RHS, m_Constant()))
9464 AddAffected(LHS);
9465 };
9466
9467 SmallVector<Value *, 8> Worklist;
9469 Worklist.push_back(Cond);
9470 while (!Worklist.empty()) {
9471 Value *V = Worklist.pop_back_val();
9472 if (!Visited.insert(V).second)
9473 continue;
9474
9475 CmpInst::Predicate Pred;
9476 Value *A, *B, *X;
9477
9478 if (IsAssume) {
9479 AddAffected(V);
9480 if (match(V, m_Not(m_Value(X))))
9481 AddAffected(X);
9482 }
9483
9484 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
9485 // assume(A && B) is split to -> assume(A); assume(B);
9486 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
9487 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
9488 // enough information to be worth handling (intersection of information as
9489 // opposed to union).
9490 if (!IsAssume) {
9491 Worklist.push_back(A);
9492 Worklist.push_back(B);
9493 }
9494 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
9495 AddCmpOperands(A, B);
9496
9497 if (ICmpInst::isEquality(Pred)) {
9498 if (match(B, m_ConstantInt())) {
9499 Value *Y;
9500 // (X & C) or (X | C) or (X ^ C).
9501 // (X << C) or (X >>_s C) or (X >>_u C).
9504 AddAffected(X);
9505 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
9506 match(A, m_Or(m_Value(X), m_Value(Y)))) {
9507 AddAffected(X);
9508 AddAffected(Y);
9509 }
9510 }
9511 } else {
9512 if (match(B, m_ConstantInt())) {
9513 // Handle (A + C1) u< C2, which is the canonical form of
9514 // A > C3 && A < C4.
9516 AddAffected(X);
9517
9518 Value *Y;
9519 // X & Y u> C -> X >u C && Y >u C
9520 // X | Y u< C -> X u< C && Y u< C
9521 if (ICmpInst::isUnsigned(Pred) &&
9522 (match(A, m_And(m_Value(X), m_Value(Y))) ||
9523 match(A, m_Or(m_Value(X), m_Value(Y))))) {
9524 AddAffected(X);
9525 AddAffected(Y);
9526 }
9527 }
9528
9529 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
9530 // by computeKnownFPClass().
9532 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
9533 InsertAffected(X);
9534 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
9535 InsertAffected(X);
9536 }
9537 }
9538 } else if (match(Cond, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
9539 AddCmpOperands(A, B);
9540
9541 // fcmp fneg(x), y
9542 // fcmp fabs(x), y
9543 // fcmp fneg(fabs(x)), y
9544 if (match(A, m_FNeg(m_Value(A))))
9545 AddAffected(A);
9546 if (match(A, m_FAbs(m_Value(A))))
9547 AddAffected(A);
9548
9549 } else if (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
9550 m_Value()))) {
9551 // Handle patterns that computeKnownFPClass() support.
9552 AddAffected(A);
9553 }
9554 }
9555}
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:513
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1494
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1742
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:2087
Value * getCalledOperand() const
Definition: InstrTypes.h:1735
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1687
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:1678
unsigned arg_size() const
Definition: InstrTypes.h:1685
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:601
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:983
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:993
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:996
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:1010
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:1022
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:1023
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:999
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:1008
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:997
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:998
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:1017
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:1016
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:1020
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:1007
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:1001
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:1004
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:1018
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:1005
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:1000
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:1002
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:1021
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:1009
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:1019
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:1006
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:995
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:1003
bool isSigned() const
Definition: InstrTypes.h:1265
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:1167
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:1314
bool isFPPredicate() const
Definition: InstrTypes.h:1122
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:1129
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:1105
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:1123
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:1271
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 AllowPoison=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:201
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:787
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition: Function.cpp:742
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:83
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:87
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:76
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:151
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition: Operator.h:170
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:485
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)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition: PatternMatch.h:625
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:580
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:165
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:613
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:725
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:836
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:553
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:783
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:854
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:560
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:815
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.
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
Definition: PatternMatch.h:322
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:299
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:316
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:189
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:573
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:239
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...
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, pointer casts or llvm.threadlocal....
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:280
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:330
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:324
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:48
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:2047
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 isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
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:764
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:1101
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:773
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:434
static KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
Definition: KnownBits.cpp:767
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1030
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:1112
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:376
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:1049
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:988
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:929
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:770
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:777
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:556
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:518
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:291
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