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()));
259 });
260}
261
263 return !I->user_empty() && all_of(I->users(), [](const User *U) {
264 ICmpInst::Predicate P;
265 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
266 });
267}
268
269static bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
270 const SimplifyQuery &Q);
271
273 bool OrZero, unsigned Depth,
274 AssumptionCache *AC, const Instruction *CxtI,
275 const DominatorTree *DT, bool UseInstrInfo) {
276 return ::isKnownToBeAPowerOfTwo(
277 V, OrZero, Depth,
278 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
279}
280
281static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
282 const SimplifyQuery &Q, unsigned Depth);
283
285 unsigned Depth) {
286 return computeKnownBits(V, Depth, SQ).isNonNegative();
287}
288
290 unsigned Depth) {
291 if (auto *CI = dyn_cast<ConstantInt>(V))
292 return CI->getValue().isStrictlyPositive();
293
294 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
295 // this updated.
296 KnownBits Known = computeKnownBits(V, Depth, SQ);
297 return Known.isNonNegative() &&
298 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
299}
300
302 unsigned Depth) {
303 return computeKnownBits(V, Depth, SQ).isNegative();
304}
305
306static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
307 const SimplifyQuery &Q);
308
309bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
310 const DataLayout &DL, AssumptionCache *AC,
311 const Instruction *CxtI, const DominatorTree *DT,
312 bool UseInstrInfo) {
313 return ::isKnownNonEqual(
314 V1, V2, 0,
315 SimplifyQuery(DL, DT, AC, safeCxtI(V2, V1, CxtI), UseInstrInfo));
316}
317
318bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
319 const SimplifyQuery &SQ, unsigned Depth) {
320 KnownBits Known(Mask.getBitWidth());
321 computeKnownBits(V, Known, Depth, SQ);
322 return Mask.isSubsetOf(Known.Zero);
323}
324
325static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
326 unsigned Depth, const SimplifyQuery &Q);
327
328static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
329 const SimplifyQuery &Q) {
330 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
331 APInt DemandedElts =
332 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
333 return ComputeNumSignBits(V, DemandedElts, Depth, Q);
334}
335
336unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
337 unsigned Depth, AssumptionCache *AC,
338 const Instruction *CxtI,
339 const DominatorTree *DT, bool UseInstrInfo) {
340 return ::ComputeNumSignBits(
341 V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
342}
343
345 unsigned Depth, AssumptionCache *AC,
346 const Instruction *CxtI,
347 const DominatorTree *DT) {
348 unsigned SignBits = ComputeNumSignBits(V, DL, Depth, AC, CxtI, DT);
349 return V->getType()->getScalarSizeInBits() - SignBits + 1;
350}
351
352static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
353 bool NSW, bool NUW,
354 const APInt &DemandedElts,
355 KnownBits &KnownOut, KnownBits &Known2,
356 unsigned Depth, const SimplifyQuery &Q) {
357 computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q);
358
359 // If one operand is unknown and we have no nowrap information,
360 // the result will be unknown independently of the second operand.
361 if (KnownOut.isUnknown() && !NSW && !NUW)
362 return;
363
364 computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
365 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
366}
367
368static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
369 const APInt &DemandedElts, KnownBits &Known,
370 KnownBits &Known2, unsigned Depth,
371 const SimplifyQuery &Q) {
372 computeKnownBits(Op1, DemandedElts, Known, Depth + 1, Q);
373 computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
374
375 bool isKnownNegative = false;
376 bool isKnownNonNegative = false;
377 // If the multiplication is known not to overflow, compute the sign bit.
378 if (NSW) {
379 if (Op0 == Op1) {
380 // The product of a number with itself is non-negative.
381 isKnownNonNegative = true;
382 } else {
383 bool isKnownNonNegativeOp1 = Known.isNonNegative();
384 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
385 bool isKnownNegativeOp1 = Known.isNegative();
386 bool isKnownNegativeOp0 = Known2.isNegative();
387 // The product of two numbers with the same sign is non-negative.
388 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
389 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
390 // The product of a negative number and a non-negative number is either
391 // negative or zero.
394 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
395 Known2.isNonZero()) ||
396 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
397 }
398 }
399
400 bool SelfMultiply = Op0 == Op1;
401 if (SelfMultiply)
402 SelfMultiply &=
403 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
404 Known = KnownBits::mul(Known, Known2, SelfMultiply);
405
406 // Only make use of no-wrap flags if we failed to compute the sign bit
407 // directly. This matters if the multiplication always overflows, in
408 // which case we prefer to follow the result of the direct computation,
409 // though as the program is invoking undefined behaviour we can choose
410 // whatever we like here.
411 if (isKnownNonNegative && !Known.isNegative())
412 Known.makeNonNegative();
413 else if (isKnownNegative && !Known.isNonNegative())
414 Known.makeNegative();
415}
416
418 KnownBits &Known) {
419 unsigned BitWidth = Known.getBitWidth();
420 unsigned NumRanges = Ranges.getNumOperands() / 2;
421 assert(NumRanges >= 1);
422
423 Known.Zero.setAllBits();
424 Known.One.setAllBits();
425
426 for (unsigned i = 0; i < NumRanges; ++i) {
428 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
430 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
431 ConstantRange Range(Lower->getValue(), Upper->getValue());
432
433 // The first CommonPrefixBits of all values in Range are equal.
434 unsigned CommonPrefixBits =
435 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
436 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
437 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
438 Known.One &= UnsignedMax & Mask;
439 Known.Zero &= ~UnsignedMax & Mask;
440 }
441}
442
443static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
447
448 // The instruction defining an assumption's condition itself is always
449 // considered ephemeral to that assumption (even if it has other
450 // non-ephemeral users). See r246696's test case for an example.
451 if (is_contained(I->operands(), E))
452 return true;
453
454 while (!WorkSet.empty()) {
455 const Value *V = WorkSet.pop_back_val();
456 if (!Visited.insert(V).second)
457 continue;
458
459 // If all uses of this value are ephemeral, then so is this value.
460 if (llvm::all_of(V->users(), [&](const User *U) {
461 return EphValues.count(U);
462 })) {
463 if (V == E)
464 return true;
465
466 if (V == I || (isa<Instruction>(V) &&
467 !cast<Instruction>(V)->mayHaveSideEffects() &&
468 !cast<Instruction>(V)->isTerminator())) {
469 EphValues.insert(V);
470 if (const User *U = dyn_cast<User>(V))
471 append_range(WorkSet, U->operands());
472 }
473 }
474 }
475
476 return false;
477}
478
479// Is this an intrinsic that cannot be speculated but also cannot trap?
481 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
482 return CI->isAssumeLikeIntrinsic();
483
484 return false;
485}
486
488 const Instruction *CxtI,
489 const DominatorTree *DT,
490 bool AllowEphemerals) {
491 // There are two restrictions on the use of an assume:
492 // 1. The assume must dominate the context (or the control flow must
493 // reach the assume whenever it reaches the context).
494 // 2. The context must not be in the assume's set of ephemeral values
495 // (otherwise we will use the assume to prove that the condition
496 // feeding the assume is trivially true, thus causing the removal of
497 // the assume).
498
499 if (Inv->getParent() == CxtI->getParent()) {
500 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
501 // in the BB.
502 if (Inv->comesBefore(CxtI))
503 return true;
504
505 // Don't let an assume affect itself - this would cause the problems
506 // `isEphemeralValueOf` is trying to prevent, and it would also make
507 // the loop below go out of bounds.
508 if (!AllowEphemerals && Inv == CxtI)
509 return false;
510
511 // The context comes first, but they're both in the same block.
512 // Make sure there is nothing in between that might interrupt
513 // the control flow, not even CxtI itself.
514 // We limit the scan distance between the assume and its context instruction
515 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
516 // it can be adjusted if needed (could be turned into a cl::opt).
517 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
519 return false;
520
521 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
522 }
523
524 // Inv and CxtI are in different blocks.
525 if (DT) {
526 if (DT->dominates(Inv, CxtI))
527 return true;
528 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor()) {
529 // We don't have a DT, but this trivially dominates.
530 return true;
531 }
532
533 return false;
534}
535
536// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
537// we still have enough information about `RHS` to conclude non-zero. For
538// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
539// so the extra compile time may not be worth it, but possibly a second API
540// should be created for use outside of loops.
541static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
542 // v u> y implies v != 0.
543 if (Pred == ICmpInst::ICMP_UGT)
544 return true;
545
546 // Special-case v != 0 to also handle v != null.
547 if (Pred == ICmpInst::ICMP_NE)
548 return match(RHS, m_Zero());
549
550 // All other predicates - rely on generic ConstantRange handling.
551 const APInt *C;
553 if (match(RHS, m_APInt(C))) {
555 return !TrueValues.contains(Zero);
556 }
557
558 auto *VC = dyn_cast<ConstantDataVector>(RHS);
559 if (VC == nullptr)
560 return false;
561
562 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
563 ++ElemIdx) {
565 Pred, VC->getElementAsAPInt(ElemIdx));
566 if (TrueValues.contains(Zero))
567 return false;
568 }
569 return true;
570}
571
572static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
573 // Use of assumptions is context-sensitive. If we don't have a context, we
574 // cannot use them!
575 if (!Q.AC || !Q.CxtI)
576 return false;
577
578 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
579 if (!Elem.Assume)
580 continue;
581
582 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
583 assert(I->getFunction() == Q.CxtI->getFunction() &&
584 "Got assumption for the wrong function!");
585
586 if (Elem.Index != AssumptionCache::ExprResultIdx) {
587 if (!V->getType()->isPointerTy())
588 continue;
590 *I, I->bundle_op_info_begin()[Elem.Index])) {
591 if (RK.WasOn == V &&
592 (RK.AttrKind == Attribute::NonNull ||
593 (RK.AttrKind == Attribute::Dereferenceable &&
595 V->getType()->getPointerAddressSpace()))) &&
597 return true;
598 }
599 continue;
600 }
601
602 // Warning: This loop can end up being somewhat performance sensitive.
603 // We're running this loop for once for each value queried resulting in a
604 // runtime of ~O(#assumes * #values).
605
606 Value *RHS;
608 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
609 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
610 return false;
611
612 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
613 return true;
614 }
615
616 return false;
617}
618
620 Value *LHS, Value *RHS, KnownBits &Known,
621 const SimplifyQuery &Q) {
622 if (RHS->getType()->isPointerTy()) {
623 // Handle comparison of pointer to null explicitly, as it will not be
624 // covered by the m_APInt() logic below.
625 if (LHS == V && match(RHS, m_Zero())) {
626 switch (Pred) {
627 case ICmpInst::ICMP_EQ:
628 Known.setAllZero();
629 break;
630 case ICmpInst::ICMP_SGE:
631 case ICmpInst::ICMP_SGT:
632 Known.makeNonNegative();
633 break;
634 case ICmpInst::ICMP_SLT:
635 Known.makeNegative();
636 break;
637 default:
638 break;
639 }
640 }
641 return;
642 }
643
644 unsigned BitWidth = Known.getBitWidth();
645 auto m_V =
647
648 Value *Y;
649 const APInt *Mask, *C;
650 uint64_t ShAmt;
651 switch (Pred) {
652 case ICmpInst::ICMP_EQ:
653 // assume(V = C)
654 if (match(LHS, m_V) && match(RHS, m_APInt(C))) {
655 Known = Known.unionWith(KnownBits::makeConstant(*C));
656 // assume(V & Mask = C)
657 } else if (match(LHS, m_c_And(m_V, m_Value(Y))) &&
658 match(RHS, m_APInt(C))) {
659 // For one bits in Mask, we can propagate bits from C to V.
660 Known.One |= *C;
661 if (match(Y, m_APInt(Mask)))
662 Known.Zero |= ~*C & *Mask;
663 // assume(V | Mask = C)
664 } else if (match(LHS, m_c_Or(m_V, m_Value(Y))) && match(RHS, m_APInt(C))) {
665 // For zero bits in Mask, we can propagate bits from C to V.
666 Known.Zero |= ~*C;
667 if (match(Y, m_APInt(Mask)))
668 Known.One |= *C & ~*Mask;
669 // assume(V ^ Mask = C)
670 } else if (match(LHS, m_Xor(m_V, m_APInt(Mask))) &&
671 match(RHS, m_APInt(C))) {
672 // Equivalent to assume(V == Mask ^ C)
673 Known = Known.unionWith(KnownBits::makeConstant(*C ^ *Mask));
674 // assume(V << ShAmt = C)
675 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
676 match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
677 // For those bits in C that are known, we can propagate them to known
678 // bits in V shifted to the right by ShAmt.
680 RHSKnown.Zero.lshrInPlace(ShAmt);
681 RHSKnown.One.lshrInPlace(ShAmt);
682 Known = Known.unionWith(RHSKnown);
683 // assume(V >> ShAmt = C)
684 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
685 match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
687 // For those bits in RHS that are known, we can propagate them to known
688 // bits in V shifted to the right by C.
689 Known.Zero |= RHSKnown.Zero << ShAmt;
690 Known.One |= RHSKnown.One << ShAmt;
691 }
692 break;
693 case ICmpInst::ICMP_NE: {
694 // assume (V & B != 0) where B is a power of 2
695 const APInt *BPow2;
696 if (match(LHS, m_And(m_V, m_Power2(BPow2))) && match(RHS, m_Zero()))
697 Known.One |= *BPow2;
698 break;
699 }
700 default:
701 if (match(RHS, m_APInt(C))) {
702 const APInt *Offset = nullptr;
703 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
705 if (Offset)
706 LHSRange = LHSRange.sub(*Offset);
707 Known = Known.unionWith(LHSRange.toKnownBits());
708 }
709 // X & Y u> C -> X u> C && Y u> C
710 if ((Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) &&
711 match(LHS, m_c_And(m_V, m_Value()))) {
712 Known.One.setHighBits(
713 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
714 }
715 // X | Y u< C -> X u< C && Y u< C
716 if ((Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) &&
717 match(LHS, m_c_Or(m_V, m_Value()))) {
718 Known.Zero.setHighBits(
719 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
720 }
721 }
722 break;
723 }
724}
725
726static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
727 KnownBits &Known,
728 const SimplifyQuery &SQ, bool Invert) {
730 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
731 Value *LHS = Cmp->getOperand(0);
732 Value *RHS = Cmp->getOperand(1);
733
734 // Handle icmp pred (trunc V), C
735 if (match(LHS, m_Trunc(m_Specific(V)))) {
737 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
738 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
739 return;
740 }
741
742 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
743}
744
746 KnownBits &Known, unsigned Depth,
747 const SimplifyQuery &SQ, bool Invert) {
748 Value *A, *B;
751 KnownBits Known2(Known.getBitWidth());
752 KnownBits Known3(Known.getBitWidth());
753 computeKnownBitsFromCond(V, A, Known2, Depth + 1, SQ, Invert);
754 computeKnownBitsFromCond(V, B, Known3, Depth + 1, SQ, Invert);
755 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
757 Known2 = Known2.unionWith(Known3);
758 else
759 Known2 = Known2.intersectWith(Known3);
760 Known = Known.unionWith(Known2);
761 }
762
763 if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
764 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
765}
766
768 unsigned Depth, const SimplifyQuery &Q) {
769 if (!Q.CxtI)
770 return;
771
772 if (Q.DC && Q.DT) {
773 // Handle dominating conditions.
774 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
775 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
776 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
777 computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
778 /*Invert*/ false);
779
780 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
781 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
782 computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
783 /*Invert*/ true);
784 }
785
786 if (Known.hasConflict())
787 Known.resetAll();
788 }
789
790 if (!Q.AC)
791 return;
792
793 unsigned BitWidth = Known.getBitWidth();
794
795 // Note that the patterns below need to be kept in sync with the code
796 // in AssumptionCache::updateAffectedValues.
797
798 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
799 if (!Elem.Assume)
800 continue;
801
802 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
803 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
804 "Got assumption for the wrong function!");
805
806 if (Elem.Index != AssumptionCache::ExprResultIdx) {
807 if (!V->getType()->isPointerTy())
808 continue;
810 *I, I->bundle_op_info_begin()[Elem.Index])) {
811 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
812 isPowerOf2_64(RK.ArgValue) &&
814 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
815 }
816 continue;
817 }
818
819 // Warning: This loop can end up being somewhat performance sensitive.
820 // We're running this loop for once for each value queried resulting in a
821 // runtime of ~O(#assumes * #values).
822
823 Value *Arg = I->getArgOperand(0);
824
825 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
826 assert(BitWidth == 1 && "assume operand is not i1?");
827 (void)BitWidth;
828 Known.setAllOnes();
829 return;
830 }
831 if (match(Arg, m_Not(m_Specific(V))) &&
833 assert(BitWidth == 1 && "assume operand is not i1?");
834 (void)BitWidth;
835 Known.setAllZero();
836 return;
837 }
838
839 // The remaining tests are all recursive, so bail out if we hit the limit.
841 continue;
842
843 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
844 if (!Cmp)
845 continue;
846
847 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
848 continue;
849
850 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
851 }
852
853 // Conflicting assumption: Undefined behavior will occur on this execution
854 // path.
855 if (Known.hasConflict())
856 Known.resetAll();
857}
858
859/// Compute known bits from a shift operator, including those with a
860/// non-constant shift amount. Known is the output of this function. Known2 is a
861/// pre-allocated temporary with the same bit width as Known and on return
862/// contains the known bit of the shift value source. KF is an
863/// operator-specific function that, given the known-bits and a shift amount,
864/// compute the implied known-bits of the shift operator's result respectively
865/// for that shift amount. The results from calling KF are conservatively
866/// combined for all permitted shift amounts.
868 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
869 KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q,
870 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
871 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
872 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
873 // To limit compile-time impact, only query isKnownNonZero() if we know at
874 // least something about the shift amount.
875 bool ShAmtNonZero =
876 Known.isNonZero() ||
877 (Known.getMaxValue().ult(Known.getBitWidth()) &&
878 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
879 Known = KF(Known2, Known, ShAmtNonZero);
880}
881
882static KnownBits
883getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
884 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
885 unsigned Depth, const SimplifyQuery &Q) {
886 unsigned BitWidth = KnownLHS.getBitWidth();
887 KnownBits KnownOut(BitWidth);
888 bool IsAnd = false;
889 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
890 Value *X = nullptr, *Y = nullptr;
891
892 switch (I->getOpcode()) {
893 case Instruction::And:
894 KnownOut = KnownLHS & KnownRHS;
895 IsAnd = true;
896 // and(x, -x) is common idioms that will clear all but lowest set
897 // bit. If we have a single known bit in x, we can clear all bits
898 // above it.
899 // TODO: instcombine often reassociates independent `and` which can hide
900 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
901 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
902 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
903 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
904 KnownOut = KnownLHS.blsi();
905 else
906 KnownOut = KnownRHS.blsi();
907 }
908 break;
909 case Instruction::Or:
910 KnownOut = KnownLHS | KnownRHS;
911 break;
912 case Instruction::Xor:
913 KnownOut = KnownLHS ^ KnownRHS;
914 // xor(x, x-1) is common idioms that will clear all but lowest set
915 // bit. If we have a single known bit in x, we can clear all bits
916 // above it.
917 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
918 // -1 but for the purpose of demanded bits (xor(x, x-C) &
919 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
920 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
921 if (HasKnownOne &&
923 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
924 KnownOut = XBits.blsmsk();
925 }
926 break;
927 default:
928 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
929 }
930
931 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
932 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
933 // here we handle the more general case of adding any odd number by
934 // matching the form and/xor/or(x, add(x, y)) where y is odd.
935 // TODO: This could be generalized to clearing any bit set in y where the
936 // following bit is known to be unset in y.
937 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
941 KnownBits KnownY(BitWidth);
942 computeKnownBits(Y, DemandedElts, KnownY, Depth + 1, Q);
943 if (KnownY.countMinTrailingOnes() > 0) {
944 if (IsAnd)
945 KnownOut.Zero.setBit(0);
946 else
947 KnownOut.One.setBit(0);
948 }
949 }
950 return KnownOut;
951}
952
953// Public so this can be used in `SimplifyDemandedUseBits`.
955 const KnownBits &KnownLHS,
956 const KnownBits &KnownRHS,
957 unsigned Depth,
958 const SimplifyQuery &SQ) {
959 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
960 APInt DemandedElts =
961 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
962
963 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, Depth,
964 SQ);
965}
966
968 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
969 // Without vscale_range, we only know that vscale is non-zero.
970 if (!Attr.isValid())
972
973 unsigned AttrMin = Attr.getVScaleRangeMin();
974 // Minimum is larger than vscale width, result is always poison.
975 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
976 return ConstantRange::getEmpty(BitWidth);
977
978 APInt Min(BitWidth, AttrMin);
979 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
980 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
982
983 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
984}
985
987 const APInt &DemandedElts,
988 KnownBits &Known, unsigned Depth,
989 const SimplifyQuery &Q) {
990 unsigned BitWidth = Known.getBitWidth();
991
992 KnownBits Known2(BitWidth);
993 switch (I->getOpcode()) {
994 default: break;
995 case Instruction::Load:
996 if (MDNode *MD =
997 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
999 break;
1000 case Instruction::And:
1001 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1002 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1003
1004 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1005 break;
1006 case Instruction::Or:
1007 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1008 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1009
1010 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1011 break;
1012 case Instruction::Xor:
1013 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1014 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1015
1016 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1017 break;
1018 case Instruction::Mul: {
1019 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1020 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, DemandedElts,
1021 Known, Known2, Depth, Q);
1022 break;
1023 }
1024 case Instruction::UDiv: {
1025 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1026 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1027 Known =
1028 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1029 break;
1030 }
1031 case Instruction::SDiv: {
1032 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1033 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1034 Known =
1035 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1036 break;
1037 }
1038 case Instruction::Select: {
1039 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1040 KnownBits Res(Known.getBitWidth());
1041 computeKnownBits(Arm, Res, Depth + 1, Q);
1042 // If we have a constant arm, we are done.
1043 if (Res.isConstant())
1044 return Res;
1045
1046 // See what condition implies about the bits of the two select arms.
1047 KnownBits CondRes(Res.getBitWidth());
1048 computeKnownBitsFromCond(Arm, I->getOperand(0), CondRes, Depth + 1, Q,
1049 Invert);
1050 // If we don't get any information from the condition, no reason to
1051 // proceed.
1052 if (CondRes.isUnknown())
1053 return Res;
1054
1055 // We can have conflict if the condition is dead. I.e if we have
1056 // (x | 64) < 32 ? (x | 64) : y
1057 // we will have conflict at bit 6 from the condition/the `or`.
1058 // In that case just return. Its not particularly important
1059 // what we do, as this select is going to be simplified soon.
1060 CondRes = CondRes.unionWith(Res);
1061 if (CondRes.hasConflict())
1062 return Res;
1063
1064 // Finally make sure the information we found is valid. This is relatively
1065 // expensive so it's left for the very end.
1066 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1067 return Res;
1068
1069 // Finally, we know we get information from the condition and its valid,
1070 // so return it.
1071 return CondRes;
1072 };
1073 // Only known if known in both the LHS and RHS.
1074 Known =
1075 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1076 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1077 break;
1078 }
1079 case Instruction::FPTrunc:
1080 case Instruction::FPExt:
1081 case Instruction::FPToUI:
1082 case Instruction::FPToSI:
1083 case Instruction::SIToFP:
1084 case Instruction::UIToFP:
1085 break; // Can't work with floating point.
1086 case Instruction::PtrToInt:
1087 case Instruction::IntToPtr:
1088 // Fall through and handle them the same as zext/trunc.
1089 [[fallthrough]];
1090 case Instruction::ZExt:
1091 case Instruction::Trunc: {
1092 Type *SrcTy = I->getOperand(0)->getType();
1093
1094 unsigned SrcBitWidth;
1095 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1096 // which fall through here.
1097 Type *ScalarTy = SrcTy->getScalarType();
1098 SrcBitWidth = ScalarTy->isPointerTy() ?
1099 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1100 Q.DL.getTypeSizeInBits(ScalarTy);
1101
1102 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1103 Known = Known.anyextOrTrunc(SrcBitWidth);
1104 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1105 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1106 Inst && Inst->hasNonNeg() && !Known.isNegative())
1107 Known.makeNonNegative();
1108 Known = Known.zextOrTrunc(BitWidth);
1109 break;
1110 }
1111 case Instruction::BitCast: {
1112 Type *SrcTy = I->getOperand(0)->getType();
1113 if (SrcTy->isIntOrPtrTy() &&
1114 // TODO: For now, not handling conversions like:
1115 // (bitcast i64 %x to <2 x i32>)
1116 !I->getType()->isVectorTy()) {
1117 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1118 break;
1119 }
1120
1121 const Value *V;
1122 // Handle bitcast from floating point to integer.
1123 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1124 V->getType()->isFPOrFPVectorTy()) {
1125 Type *FPType = V->getType()->getScalarType();
1126 KnownFPClass Result = computeKnownFPClass(V, fcAllFlags, Depth + 1, Q);
1127 FPClassTest FPClasses = Result.KnownFPClasses;
1128
1129 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1130 Known.Zero.setAllBits();
1131 Known.One.setAllBits();
1132
1133 if (FPClasses & fcInf)
1136
1137 if (FPClasses & fcZero)
1140
1141 Known.Zero.clearSignBit();
1142 Known.One.clearSignBit();
1143 }
1144
1145 if (Result.SignBit) {
1146 if (*Result.SignBit)
1147 Known.makeNegative();
1148 else
1149 Known.makeNonNegative();
1150 }
1151
1152 assert(!Known.hasConflict() && "Bits known to be one AND zero?");
1153 break;
1154 }
1155
1156 // Handle cast from vector integer type to scalar or vector integer.
1157 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1158 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1159 !I->getType()->isIntOrIntVectorTy() ||
1160 isa<ScalableVectorType>(I->getType()))
1161 break;
1162
1163 // Look through a cast from narrow vector elements to wider type.
1164 // Examples: v4i32 -> v2i64, v3i8 -> v24
1165 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1166 if (BitWidth % SubBitWidth == 0) {
1167 // Known bits are automatically intersected across demanded elements of a
1168 // vector. So for example, if a bit is computed as known zero, it must be
1169 // zero across all demanded elements of the vector.
1170 //
1171 // For this bitcast, each demanded element of the output is sub-divided
1172 // across a set of smaller vector elements in the source vector. To get
1173 // the known bits for an entire element of the output, compute the known
1174 // bits for each sub-element sequentially. This is done by shifting the
1175 // one-set-bit demanded elements parameter across the sub-elements for
1176 // consecutive calls to computeKnownBits. We are using the demanded
1177 // elements parameter as a mask operator.
1178 //
1179 // The known bits of each sub-element are then inserted into place
1180 // (dependent on endian) to form the full result of known bits.
1181 unsigned NumElts = DemandedElts.getBitWidth();
1182 unsigned SubScale = BitWidth / SubBitWidth;
1183 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1184 for (unsigned i = 0; i != NumElts; ++i) {
1185 if (DemandedElts[i])
1186 SubDemandedElts.setBit(i * SubScale);
1187 }
1188
1189 KnownBits KnownSrc(SubBitWidth);
1190 for (unsigned i = 0; i != SubScale; ++i) {
1191 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc,
1192 Depth + 1, Q);
1193 unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i;
1194 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1195 }
1196 }
1197 break;
1198 }
1199 case Instruction::SExt: {
1200 // Compute the bits in the result that are not present in the input.
1201 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1202
1203 Known = Known.trunc(SrcBitWidth);
1204 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1205 // If the sign bit of the input is known set or clear, then we know the
1206 // top bits of the result.
1207 Known = Known.sext(BitWidth);
1208 break;
1209 }
1210 case Instruction::Shl: {
1211 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1212 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1213 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1214 bool ShAmtNonZero) {
1215 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1216 };
1217 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1218 KF);
1219 // Trailing zeros of a right-shifted constant never decrease.
1220 const APInt *C;
1221 if (match(I->getOperand(0), m_APInt(C)))
1222 Known.Zero.setLowBits(C->countr_zero());
1223 break;
1224 }
1225 case Instruction::LShr: {
1226 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1227 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1228 bool ShAmtNonZero) {
1229 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1230 };
1231 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1232 KF);
1233 // Leading zeros of a left-shifted constant never decrease.
1234 const APInt *C;
1235 if (match(I->getOperand(0), m_APInt(C)))
1236 Known.Zero.setHighBits(C->countl_zero());
1237 break;
1238 }
1239 case Instruction::AShr: {
1240 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1241 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1242 bool ShAmtNonZero) {
1243 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1244 };
1245 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1246 KF);
1247 break;
1248 }
1249 case Instruction::Sub: {
1250 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1251 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1252 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1253 DemandedElts, Known, Known2, Depth, Q);
1254 break;
1255 }
1256 case Instruction::Add: {
1257 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1258 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1259 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1260 DemandedElts, Known, Known2, Depth, Q);
1261 break;
1262 }
1263 case Instruction::SRem:
1264 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1265 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1266 Known = KnownBits::srem(Known, Known2);
1267 break;
1268
1269 case Instruction::URem:
1270 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1271 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1272 Known = KnownBits::urem(Known, Known2);
1273 break;
1274 case Instruction::Alloca:
1275 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
1276 break;
1277 case Instruction::GetElementPtr: {
1278 // Analyze all of the subscripts of this getelementptr instruction
1279 // to determine if we can prove known low zero bits.
1280 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1281 // Accumulate the constant indices in a separate variable
1282 // to minimize the number of calls to computeForAddSub.
1283 APInt AccConstIndices(BitWidth, 0, /*IsSigned*/ true);
1284
1286 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1287 // TrailZ can only become smaller, short-circuit if we hit zero.
1288 if (Known.isUnknown())
1289 break;
1290
1291 Value *Index = I->getOperand(i);
1292
1293 // Handle case when index is zero.
1294 Constant *CIndex = dyn_cast<Constant>(Index);
1295 if (CIndex && CIndex->isZeroValue())
1296 continue;
1297
1298 if (StructType *STy = GTI.getStructTypeOrNull()) {
1299 // Handle struct member offset arithmetic.
1300
1301 assert(CIndex &&
1302 "Access to structure field must be known at compile time");
1303
1304 if (CIndex->getType()->isVectorTy())
1305 Index = CIndex->getSplatValue();
1306
1307 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1308 const StructLayout *SL = Q.DL.getStructLayout(STy);
1310 AccConstIndices += Offset;
1311 continue;
1312 }
1313
1314 // Handle array index arithmetic.
1315 Type *IndexedTy = GTI.getIndexedType();
1316 if (!IndexedTy->isSized()) {
1317 Known.resetAll();
1318 break;
1319 }
1320
1321 unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits();
1322 KnownBits IndexBits(IndexBitWidth);
1323 computeKnownBits(Index, IndexBits, Depth + 1, Q);
1324 TypeSize IndexTypeSize = GTI.getSequentialElementStride(Q.DL);
1325 uint64_t TypeSizeInBytes = IndexTypeSize.getKnownMinValue();
1326 KnownBits ScalingFactor(IndexBitWidth);
1327 // Multiply by current sizeof type.
1328 // &A[i] == A + i * sizeof(*A[i]).
1329 if (IndexTypeSize.isScalable()) {
1330 // For scalable types the only thing we know about sizeof is
1331 // that this is a multiple of the minimum size.
1332 ScalingFactor.Zero.setLowBits(llvm::countr_zero(TypeSizeInBytes));
1333 } else if (IndexBits.isConstant()) {
1334 APInt IndexConst = IndexBits.getConstant();
1335 APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes);
1336 IndexConst *= ScalingFactor;
1337 AccConstIndices += IndexConst.sextOrTrunc(BitWidth);
1338 continue;
1339 } else {
1340 ScalingFactor =
1341 KnownBits::makeConstant(APInt(IndexBitWidth, TypeSizeInBytes));
1342 }
1343 IndexBits = KnownBits::mul(IndexBits, ScalingFactor);
1344
1345 // If the offsets have a different width from the pointer, according
1346 // to the language reference we need to sign-extend or truncate them
1347 // to the width of the pointer.
1348 IndexBits = IndexBits.sextOrTrunc(BitWidth);
1349
1350 // Note that inbounds does *not* guarantee nsw for the addition, as only
1351 // the offset is signed, while the base address is unsigned.
1353 /*Add=*/true, /*NSW=*/false, /* NUW=*/false, Known, IndexBits);
1354 }
1355 if (!Known.isUnknown() && !AccConstIndices.isZero()) {
1356 KnownBits Index = KnownBits::makeConstant(AccConstIndices);
1358 /*Add=*/true, /*NSW=*/false, /* NUW=*/false, Known, Index);
1359 }
1360 break;
1361 }
1362 case Instruction::PHI: {
1363 const PHINode *P = cast<PHINode>(I);
1364 BinaryOperator *BO = nullptr;
1365 Value *R = nullptr, *L = nullptr;
1366 if (matchSimpleRecurrence(P, BO, R, L)) {
1367 // Handle the case of a simple two-predecessor recurrence PHI.
1368 // There's a lot more that could theoretically be done here, but
1369 // this is sufficient to catch some interesting cases.
1370 unsigned Opcode = BO->getOpcode();
1371
1372 // If this is a shift recurrence, we know the bits being shifted in.
1373 // We can combine that with information about the start value of the
1374 // recurrence to conclude facts about the result.
1375 if ((Opcode == Instruction::LShr || Opcode == Instruction::AShr ||
1376 Opcode == Instruction::Shl) &&
1377 BO->getOperand(0) == I) {
1378
1379 // We have matched a recurrence of the form:
1380 // %iv = [R, %entry], [%iv.next, %backedge]
1381 // %iv.next = shift_op %iv, L
1382
1383 // Recurse with the phi context to avoid concern about whether facts
1384 // inferred hold at original context instruction. TODO: It may be
1385 // correct to use the original context. IF warranted, explore and
1386 // add sufficient tests to cover.
1387 SimplifyQuery RecQ = Q;
1388 RecQ.CxtI = P;
1389 computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1390 switch (Opcode) {
1391 case Instruction::Shl:
1392 // A shl recurrence will only increase the tailing zeros
1393 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1394 break;
1395 case Instruction::LShr:
1396 // A lshr recurrence will preserve the leading zeros of the
1397 // start value
1398 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1399 break;
1400 case Instruction::AShr:
1401 // An ashr recurrence will extend the initial sign bit
1402 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1403 Known.One.setHighBits(Known2.countMinLeadingOnes());
1404 break;
1405 };
1406 }
1407
1408 // Check for operations that have the property that if
1409 // both their operands have low zero bits, the result
1410 // will have low zero bits.
1411 if (Opcode == Instruction::Add ||
1412 Opcode == Instruction::Sub ||
1413 Opcode == Instruction::And ||
1414 Opcode == Instruction::Or ||
1415 Opcode == Instruction::Mul) {
1416 // Change the context instruction to the "edge" that flows into the
1417 // phi. This is important because that is where the value is actually
1418 // "evaluated" even though it is used later somewhere else. (see also
1419 // D69571).
1420 SimplifyQuery RecQ = Q;
1421
1422 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1423 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1424 Instruction *LInst = P->getIncomingBlock(1-OpNum)->getTerminator();
1425
1426 // Ok, we have a PHI of the form L op= R. Check for low
1427 // zero bits.
1428 RecQ.CxtI = RInst;
1429 computeKnownBits(R, Known2, Depth + 1, RecQ);
1430
1431 // We need to take the minimum number of known bits
1432 KnownBits Known3(BitWidth);
1433 RecQ.CxtI = LInst;
1434 computeKnownBits(L, Known3, Depth + 1, RecQ);
1435
1436 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1437 Known3.countMinTrailingZeros()));
1438
1439 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1440 if (OverflowOp && Q.IIQ.hasNoSignedWrap(OverflowOp)) {
1441 // If initial value of recurrence is nonnegative, and we are adding
1442 // a nonnegative number with nsw, the result can only be nonnegative
1443 // or poison value regardless of the number of times we execute the
1444 // add in phi recurrence. If initial value is negative and we are
1445 // adding a negative number with nsw, the result can only be
1446 // negative or poison value. Similar arguments apply to sub and mul.
1447 //
1448 // (add non-negative, non-negative) --> non-negative
1449 // (add negative, negative) --> negative
1450 if (Opcode == Instruction::Add) {
1451 if (Known2.isNonNegative() && Known3.isNonNegative())
1452 Known.makeNonNegative();
1453 else if (Known2.isNegative() && Known3.isNegative())
1454 Known.makeNegative();
1455 }
1456
1457 // (sub nsw non-negative, negative) --> non-negative
1458 // (sub nsw negative, non-negative) --> negative
1459 else if (Opcode == Instruction::Sub && BO->getOperand(0) == I) {
1460 if (Known2.isNonNegative() && Known3.isNegative())
1461 Known.makeNonNegative();
1462 else if (Known2.isNegative() && Known3.isNonNegative())
1463 Known.makeNegative();
1464 }
1465
1466 // (mul nsw non-negative, non-negative) --> non-negative
1467 else if (Opcode == Instruction::Mul && Known2.isNonNegative() &&
1468 Known3.isNonNegative())
1469 Known.makeNonNegative();
1470 }
1471
1472 break;
1473 }
1474 }
1475
1476 // Unreachable blocks may have zero-operand PHI nodes.
1477 if (P->getNumIncomingValues() == 0)
1478 break;
1479
1480 // Otherwise take the unions of the known bit sets of the operands,
1481 // taking conservative care to avoid excessive recursion.
1482 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1483 // Skip if every incoming value references to ourself.
1484 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1485 break;
1486
1487 Known.Zero.setAllBits();
1488 Known.One.setAllBits();
1489 for (unsigned u = 0, e = P->getNumIncomingValues(); u < e; ++u) {
1490 Value *IncValue = P->getIncomingValue(u);
1491 // Skip direct self references.
1492 if (IncValue == P) continue;
1493
1494 // Change the context instruction to the "edge" that flows into the
1495 // phi. This is important because that is where the value is actually
1496 // "evaluated" even though it is used later somewhere else. (see also
1497 // D69571).
1498 SimplifyQuery RecQ = Q;
1499 RecQ.CxtI = P->getIncomingBlock(u)->getTerminator();
1500
1501 Known2 = KnownBits(BitWidth);
1502
1503 // Recurse, but cap the recursion to one level, because we don't
1504 // want to waste time spinning around in loops.
1505 // TODO: See if we can base recursion limiter on number of incoming phi
1506 // edges so we don't overly clamp analysis.
1507 computeKnownBits(IncValue, Known2, MaxAnalysisRecursionDepth - 1, RecQ);
1508
1509 // See if we can further use a conditional branch into the phi
1510 // to help us determine the range of the value.
1511 if (!Known2.isConstant()) {
1513 const APInt *RHSC;
1514 BasicBlock *TrueSucc, *FalseSucc;
1515 // TODO: Use RHS Value and compute range from its known bits.
1516 if (match(RecQ.CxtI,
1517 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1518 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1519 // Check for cases of duplicate successors.
1520 if ((TrueSucc == P->getParent()) != (FalseSucc == P->getParent())) {
1521 // If we're using the false successor, invert the predicate.
1522 if (FalseSucc == P->getParent())
1523 Pred = CmpInst::getInversePredicate(Pred);
1524 // Get the knownbits implied by the incoming phi condition.
1525 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1526 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1527 // We can have conflicts here if we are analyzing deadcode (its
1528 // impossible for us reach this BB based the icmp).
1529 if (KnownUnion.hasConflict()) {
1530 // No reason to continue analyzing in a known dead region, so
1531 // just resetAll and break. This will cause us to also exit the
1532 // outer loop.
1533 Known.resetAll();
1534 break;
1535 }
1536 Known2 = KnownUnion;
1537 }
1538 }
1539 }
1540
1541 Known = Known.intersectWith(Known2);
1542 // If all bits have been ruled out, there's no need to check
1543 // more operands.
1544 if (Known.isUnknown())
1545 break;
1546 }
1547 }
1548 break;
1549 }
1550 case Instruction::Call:
1551 case Instruction::Invoke: {
1552 // If range metadata is attached to this call, set known bits from that,
1553 // and then intersect with known bits based on other properties of the
1554 // function.
1555 if (MDNode *MD =
1556 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1558
1559 const auto *CB = cast<CallBase>(I);
1560
1561 if (std::optional<ConstantRange> Range = CB->getRange())
1562 Known = Known.unionWith(Range->toKnownBits());
1563
1564 if (const Value *RV = CB->getReturnedArgOperand()) {
1565 if (RV->getType() == I->getType()) {
1566 computeKnownBits(RV, Known2, Depth + 1, Q);
1567 Known = Known.unionWith(Known2);
1568 // If the function doesn't return properly for all input values
1569 // (e.g. unreachable exits) then there might be conflicts between the
1570 // argument value and the range metadata. Simply discard the known bits
1571 // in case of conflicts.
1572 if (Known.hasConflict())
1573 Known.resetAll();
1574 }
1575 }
1576 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1577 switch (II->getIntrinsicID()) {
1578 default: break;
1579 case Intrinsic::abs: {
1580 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1581 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
1582 Known = Known2.abs(IntMinIsPoison);
1583 break;
1584 }
1585 case Intrinsic::bitreverse:
1586 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1587 Known.Zero |= Known2.Zero.reverseBits();
1588 Known.One |= Known2.One.reverseBits();
1589 break;
1590 case Intrinsic::bswap:
1591 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1592 Known.Zero |= Known2.Zero.byteSwap();
1593 Known.One |= Known2.One.byteSwap();
1594 break;
1595 case Intrinsic::ctlz: {
1596 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1597 // If we have a known 1, its position is our upper bound.
1598 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
1599 // If this call is poison for 0 input, the result will be less than 2^n.
1600 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1601 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1602 unsigned LowBits = llvm::bit_width(PossibleLZ);
1603 Known.Zero.setBitsFrom(LowBits);
1604 break;
1605 }
1606 case Intrinsic::cttz: {
1607 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1608 // If we have a known 1, its position is our upper bound.
1609 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
1610 // If this call is poison for 0 input, the result will be less than 2^n.
1611 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1612 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1613 unsigned LowBits = llvm::bit_width(PossibleTZ);
1614 Known.Zero.setBitsFrom(LowBits);
1615 break;
1616 }
1617 case Intrinsic::ctpop: {
1618 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1619 // We can bound the space the count needs. Also, bits known to be zero
1620 // can't contribute to the population.
1621 unsigned BitsPossiblySet = Known2.countMaxPopulation();
1622 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
1623 Known.Zero.setBitsFrom(LowBits);
1624 // TODO: we could bound KnownOne using the lower bound on the number
1625 // of bits which might be set provided by popcnt KnownOne2.
1626 break;
1627 }
1628 case Intrinsic::fshr:
1629 case Intrinsic::fshl: {
1630 const APInt *SA;
1631 if (!match(I->getOperand(2), m_APInt(SA)))
1632 break;
1633
1634 // Normalize to funnel shift left.
1635 uint64_t ShiftAmt = SA->urem(BitWidth);
1636 if (II->getIntrinsicID() == Intrinsic::fshr)
1637 ShiftAmt = BitWidth - ShiftAmt;
1638
1639 KnownBits Known3(BitWidth);
1640 computeKnownBits(I->getOperand(0), Known2, Depth + 1, Q);
1641 computeKnownBits(I->getOperand(1), Known3, Depth + 1, Q);
1642
1643 Known.Zero =
1644 Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
1645 Known.One =
1646 Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
1647 break;
1648 }
1649 case Intrinsic::uadd_sat:
1650 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1651 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1652 Known = KnownBits::uadd_sat(Known, Known2);
1653 break;
1654 case Intrinsic::usub_sat:
1655 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1656 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1657 Known = KnownBits::usub_sat(Known, Known2);
1658 break;
1659 case Intrinsic::sadd_sat:
1660 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1661 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1662 Known = KnownBits::sadd_sat(Known, Known2);
1663 break;
1664 case Intrinsic::ssub_sat:
1665 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1666 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1667 Known = KnownBits::ssub_sat(Known, Known2);
1668 break;
1669 // for min/max/and/or reduce, any bit common to each element in the
1670 // input vec is set in the output.
1671 case Intrinsic::vector_reduce_and:
1672 case Intrinsic::vector_reduce_or:
1673 case Intrinsic::vector_reduce_umax:
1674 case Intrinsic::vector_reduce_umin:
1675 case Intrinsic::vector_reduce_smax:
1676 case Intrinsic::vector_reduce_smin:
1677 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1678 break;
1679 case Intrinsic::vector_reduce_xor: {
1680 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1681 // The zeros common to all vecs are zero in the output.
1682 // If the number of elements is odd, then the common ones remain. If the
1683 // number of elements is even, then the common ones becomes zeros.
1684 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
1685 // Even, so the ones become zeros.
1686 bool EvenCnt = VecTy->getElementCount().isKnownEven();
1687 if (EvenCnt)
1688 Known.Zero |= Known.One;
1689 // Maybe even element count so need to clear ones.
1690 if (VecTy->isScalableTy() || EvenCnt)
1691 Known.One.clearAllBits();
1692 break;
1693 }
1694 case Intrinsic::umin:
1695 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1696 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1697 Known = KnownBits::umin(Known, Known2);
1698 break;
1699 case Intrinsic::umax:
1700 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1701 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1702 Known = KnownBits::umax(Known, Known2);
1703 break;
1704 case Intrinsic::smin:
1705 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1706 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1707 Known = KnownBits::smin(Known, Known2);
1708 break;
1709 case Intrinsic::smax:
1710 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1711 computeKnownBits(I->getOperand(1), Known2, Depth + 1, Q);
1712 Known = KnownBits::smax(Known, Known2);
1713 break;
1714 case Intrinsic::ptrmask: {
1715 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1716
1717 const Value *Mask = I->getOperand(1);
1718 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
1719 computeKnownBits(Mask, Known2, Depth + 1, Q);
1720 // TODO: 1-extend would be more precise.
1721 Known &= Known2.anyextOrTrunc(BitWidth);
1722 break;
1723 }
1724 case Intrinsic::x86_sse42_crc32_64_64:
1725 Known.Zero.setBitsFrom(32);
1726 break;
1727 case Intrinsic::riscv_vsetvli:
1728 case Intrinsic::riscv_vsetvlimax: {
1729 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
1730 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
1732 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
1733 RISCVII::VLMUL VLMUL = static_cast<RISCVII::VLMUL>(
1734 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
1735 // The Range is [Lower, Upper), so we need to subtract 1 here to get the
1736 // real upper value.
1737 uint64_t MaxVLEN =
1738 (Range.getUpper().getZExtValue() - 1) * RISCV::RVVBitsPerBlock;
1739 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
1740
1741 // Result of vsetvli must be not larger than AVL.
1742 if (HasAVL)
1743 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
1744 MaxVL = std::min(MaxVL, CI->getZExtValue());
1745
1746 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
1747 if (BitWidth > KnownZeroFirstBit)
1748 Known.Zero.setBitsFrom(KnownZeroFirstBit);
1749 break;
1750 }
1751 case Intrinsic::vscale: {
1752 if (!II->getParent() || !II->getFunction())
1753 break;
1754
1755 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
1756 break;
1757 }
1758 }
1759 }
1760 break;
1761 }
1762 case Instruction::ShuffleVector: {
1763 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
1764 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
1765 if (!Shuf) {
1766 Known.resetAll();
1767 return;
1768 }
1769 // For undef elements, we don't know anything about the common state of
1770 // the shuffle result.
1771 APInt DemandedLHS, DemandedRHS;
1772 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
1773 Known.resetAll();
1774 return;
1775 }
1776 Known.One.setAllBits();
1777 Known.Zero.setAllBits();
1778 if (!!DemandedLHS) {
1779 const Value *LHS = Shuf->getOperand(0);
1780 computeKnownBits(LHS, DemandedLHS, Known, Depth + 1, Q);
1781 // If we don't know any bits, early out.
1782 if (Known.isUnknown())
1783 break;
1784 }
1785 if (!!DemandedRHS) {
1786 const Value *RHS = Shuf->getOperand(1);
1787 computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q);
1788 Known = Known.intersectWith(Known2);
1789 }
1790 break;
1791 }
1792 case Instruction::InsertElement: {
1793 if (isa<ScalableVectorType>(I->getType())) {
1794 Known.resetAll();
1795 return;
1796 }
1797 const Value *Vec = I->getOperand(0);
1798 const Value *Elt = I->getOperand(1);
1799 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
1800 unsigned NumElts = DemandedElts.getBitWidth();
1801 APInt DemandedVecElts = DemandedElts;
1802 bool NeedsElt = true;
1803 // If we know the index we are inserting too, clear it from Vec check.
1804 if (CIdx && CIdx->getValue().ult(NumElts)) {
1805 DemandedVecElts.clearBit(CIdx->getZExtValue());
1806 NeedsElt = DemandedElts[CIdx->getZExtValue()];
1807 }
1808
1809 Known.One.setAllBits();
1810 Known.Zero.setAllBits();
1811 if (NeedsElt) {
1812 computeKnownBits(Elt, Known, Depth + 1, Q);
1813 // If we don't know any bits, early out.
1814 if (Known.isUnknown())
1815 break;
1816 }
1817
1818 if (!DemandedVecElts.isZero()) {
1819 computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q);
1820 Known = Known.intersectWith(Known2);
1821 }
1822 break;
1823 }
1824 case Instruction::ExtractElement: {
1825 // Look through extract element. If the index is non-constant or
1826 // out-of-range demand all elements, otherwise just the extracted element.
1827 const Value *Vec = I->getOperand(0);
1828 const Value *Idx = I->getOperand(1);
1829 auto *CIdx = dyn_cast<ConstantInt>(Idx);
1830 if (isa<ScalableVectorType>(Vec->getType())) {
1831 // FIXME: there's probably *something* we can do with scalable vectors
1832 Known.resetAll();
1833 break;
1834 }
1835 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
1836 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
1837 if (CIdx && CIdx->getValue().ult(NumElts))
1838 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
1839 computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q);
1840 break;
1841 }
1842 case Instruction::ExtractValue:
1843 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
1844 const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
1845 if (EVI->getNumIndices() != 1) break;
1846 if (EVI->getIndices()[0] == 0) {
1847 switch (II->getIntrinsicID()) {
1848 default: break;
1849 case Intrinsic::uadd_with_overflow:
1850 case Intrinsic::sadd_with_overflow:
1852 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
1853 /* NUW=*/false, DemandedElts, Known, Known2, Depth, Q);
1854 break;
1855 case Intrinsic::usub_with_overflow:
1856 case Intrinsic::ssub_with_overflow:
1858 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
1859 /* NUW=*/false, DemandedElts, Known, Known2, Depth, Q);
1860 break;
1861 case Intrinsic::umul_with_overflow:
1862 case Intrinsic::smul_with_overflow:
1863 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
1864 DemandedElts, Known, Known2, Depth, Q);
1865 break;
1866 }
1867 }
1868 }
1869 break;
1870 case Instruction::Freeze:
1871 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
1872 Depth + 1))
1873 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1874 break;
1875 }
1876}
1877
1878/// Determine which bits of V are known to be either zero or one and return
1879/// them.
1880KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
1881 unsigned Depth, const SimplifyQuery &Q) {
1882 KnownBits Known(getBitWidth(V->getType(), Q.DL));
1883 ::computeKnownBits(V, DemandedElts, Known, Depth, Q);
1884 return Known;
1885}
1886
1887/// Determine which bits of V are known to be either zero or one and return
1888/// them.
1890 const SimplifyQuery &Q) {
1891 KnownBits Known(getBitWidth(V->getType(), Q.DL));
1892 computeKnownBits(V, Known, Depth, Q);
1893 return Known;
1894}
1895
1896/// Determine which bits of V are known to be either zero or one and return
1897/// them in the Known bit set.
1898///
1899/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
1900/// we cannot optimize based on the assumption that it is zero without changing
1901/// it to be an explicit zero. If we don't change it to zero, other code could
1902/// optimized based on the contradictory assumption that it is non-zero.
1903/// Because instcombine aggressively folds operations with undef args anyway,
1904/// this won't lose us code quality.
1905///
1906/// This function is defined on values with integer type, values with pointer
1907/// type, and vectors of integers. In the case
1908/// where V is a vector, known zero, and known one values are the
1909/// same width as the vector element, and the bit is set only if it is true
1910/// for all of the demanded elements in the vector specified by DemandedElts.
1911void computeKnownBits(const Value *V, const APInt &DemandedElts,
1912 KnownBits &Known, unsigned Depth,
1913 const SimplifyQuery &Q) {
1914 if (!DemandedElts) {
1915 // No demanded elts, better to assume we don't know anything.
1916 Known.resetAll();
1917 return;
1918 }
1919
1920 assert(V && "No Value?");
1921 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
1922
1923#ifndef NDEBUG
1924 Type *Ty = V->getType();
1925 unsigned BitWidth = Known.getBitWidth();
1926
1928 "Not integer or pointer type!");
1929
1930 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
1931 assert(
1932 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
1933 "DemandedElt width should equal the fixed vector number of elements");
1934 } else {
1935 assert(DemandedElts == APInt(1, 1) &&
1936 "DemandedElt width should be 1 for scalars or scalable vectors");
1937 }
1938
1939 Type *ScalarTy = Ty->getScalarType();
1940 if (ScalarTy->isPointerTy()) {
1941 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
1942 "V and Known should have same BitWidth");
1943 } else {
1944 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
1945 "V and Known should have same BitWidth");
1946 }
1947#endif
1948
1949 const APInt *C;
1950 if (match(V, m_APInt(C))) {
1951 // We know all of the bits for a scalar constant or a splat vector constant!
1952 Known = KnownBits::makeConstant(*C);
1953 return;
1954 }
1955 // Null and aggregate-zero are all-zeros.
1956 if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
1957 Known.setAllZero();
1958 return;
1959 }
1960 // Handle a constant vector by taking the intersection of the known bits of
1961 // each element.
1962 if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) {
1963 assert(!isa<ScalableVectorType>(V->getType()));
1964 // We know that CDV must be a vector of integers. Take the intersection of
1965 // each element.
1966 Known.Zero.setAllBits(); Known.One.setAllBits();
1967 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
1968 if (!DemandedElts[i])
1969 continue;
1970 APInt Elt = CDV->getElementAsAPInt(i);
1971 Known.Zero &= ~Elt;
1972 Known.One &= Elt;
1973 }
1974 if (Known.hasConflict())
1975 Known.resetAll();
1976 return;
1977 }
1978
1979 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
1980 assert(!isa<ScalableVectorType>(V->getType()));
1981 // We know that CV must be a vector of integers. Take the intersection of
1982 // each element.
1983 Known.Zero.setAllBits(); Known.One.setAllBits();
1984 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
1985 if (!DemandedElts[i])
1986 continue;
1987 Constant *Element = CV->getAggregateElement(i);
1988 if (isa<PoisonValue>(Element))
1989 continue;
1990 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
1991 if (!ElementCI) {
1992 Known.resetAll();
1993 return;
1994 }
1995 const APInt &Elt = ElementCI->getValue();
1996 Known.Zero &= ~Elt;
1997 Known.One &= Elt;
1998 }
1999 if (Known.hasConflict())
2000 Known.resetAll();
2001 return;
2002 }
2003
2004 // Start out not knowing anything.
2005 Known.resetAll();
2006
2007 // We can't imply anything about undefs.
2008 if (isa<UndefValue>(V))
2009 return;
2010
2011 // There's no point in looking through other users of ConstantData for
2012 // assumptions. Confirm that we've handled them all.
2013 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2014
2015 if (const auto *A = dyn_cast<Argument>(V))
2016 if (std::optional<ConstantRange> Range = A->getRange())
2017 Known = Range->toKnownBits();
2018
2019 // All recursive calls that increase depth must come after this.
2021 return;
2022
2023 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2024 // the bits of its aliasee.
2025 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2026 if (!GA->isInterposable())
2027 computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q);
2028 return;
2029 }
2030
2031 if (const Operator *I = dyn_cast<Operator>(V))
2032 computeKnownBitsFromOperator(I, DemandedElts, Known, Depth, Q);
2033 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2034 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2035 Known = CR->toKnownBits();
2036 }
2037
2038 // Aligned pointers have trailing zeros - refine Known.Zero set
2039 if (isa<PointerType>(V->getType())) {
2040 Align Alignment = V->getPointerAlignment(Q.DL);
2041 Known.Zero.setLowBits(Log2(Alignment));
2042 }
2043
2044 // computeKnownBitsFromContext strictly refines Known.
2045 // Therefore, we run them after computeKnownBitsFromOperator.
2046
2047 // Check whether we can determine known bits from context such as assumes.
2048 computeKnownBitsFromContext(V, Known, Depth, Q);
2049
2050 assert((Known.Zero & Known.One) == 0 && "Bits known to be one AND zero?");
2051}
2052
2053/// Try to detect a recurrence that the value of the induction variable is
2054/// always a power of two (or zero).
2055static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2056 unsigned Depth, SimplifyQuery &Q) {
2057 BinaryOperator *BO = nullptr;
2058 Value *Start = nullptr, *Step = nullptr;
2059 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2060 return false;
2061
2062 // Initial value must be a power of two.
2063 for (const Use &U : PN->operands()) {
2064 if (U.get() == Start) {
2065 // Initial value comes from a different BB, need to adjust context
2066 // instruction for analysis.
2067 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2068 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Depth, Q))
2069 return false;
2070 }
2071 }
2072
2073 // Except for Mul, the induction variable must be on the left side of the
2074 // increment expression, otherwise its value can be arbitrary.
2075 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2076 return false;
2077
2078 Q.CxtI = BO->getParent()->getTerminator();
2079 switch (BO->getOpcode()) {
2080 case Instruction::Mul:
2081 // Power of two is closed under multiplication.
2082 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2083 Q.IIQ.hasNoSignedWrap(BO)) &&
2084 isKnownToBeAPowerOfTwo(Step, OrZero, Depth, Q);
2085 case Instruction::SDiv:
2086 // Start value must not be signmask for signed division, so simply being a
2087 // power of two is not sufficient, and it has to be a constant.
2088 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2089 return false;
2090 [[fallthrough]];
2091 case Instruction::UDiv:
2092 // Divisor must be a power of two.
2093 // If OrZero is false, cannot guarantee induction variable is non-zero after
2094 // division, same for Shr, unless it is exact division.
2095 return (OrZero || Q.IIQ.isExact(BO)) &&
2096 isKnownToBeAPowerOfTwo(Step, false, Depth, Q);
2097 case Instruction::Shl:
2098 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2099 case Instruction::AShr:
2100 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2101 return false;
2102 [[fallthrough]];
2103 case Instruction::LShr:
2104 return OrZero || Q.IIQ.isExact(BO);
2105 default:
2106 return false;
2107 }
2108}
2109
2110/// Return true if the given value is known to have exactly one
2111/// bit set when defined. For vectors return true if every element is known to
2112/// be a power of two when defined. Supports values with integer or pointer
2113/// types and vectors of integers.
2114bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
2115 const SimplifyQuery &Q) {
2116 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2117
2118 if (isa<Constant>(V))
2119 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2120
2121 // i1 is by definition a power of 2 or zero.
2122 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2123 return true;
2124
2125 auto *I = dyn_cast<Instruction>(V);
2126 if (!I)
2127 return false;
2128
2129 if (Q.CxtI && match(V, m_VScale())) {
2130 const Function *F = Q.CxtI->getFunction();
2131 // The vscale_range indicates vscale is a power-of-two.
2132 return F->hasFnAttribute(Attribute::VScaleRange);
2133 }
2134
2135 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2136 // it is shifted off the end then the result is undefined.
2137 if (match(I, m_Shl(m_One(), m_Value())))
2138 return true;
2139
2140 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2141 // the bottom. If it is shifted off the bottom then the result is undefined.
2142 if (match(I, m_LShr(m_SignMask(), m_Value())))
2143 return true;
2144
2145 // The remaining tests are all recursive, so bail out if we hit the limit.
2147 return false;
2148
2149 switch (I->getOpcode()) {
2150 case Instruction::ZExt:
2151 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2152 case Instruction::Trunc:
2153 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2154 case Instruction::Shl:
2155 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2156 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2157 return false;
2158 case Instruction::LShr:
2159 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2160 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2161 return false;
2162 case Instruction::UDiv:
2163 if (Q.IIQ.isExact(cast<BinaryOperator>(I)))
2164 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2165 return false;
2166 case Instruction::Mul:
2167 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2168 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q) &&
2169 (OrZero || isKnownNonZero(I, Q, Depth));
2170 case Instruction::And:
2171 // A power of two and'd with anything is a power of two or zero.
2172 if (OrZero &&
2173 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Depth, Q) ||
2174 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Depth, Q)))
2175 return true;
2176 // X & (-X) is always a power of two or zero.
2177 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2178 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2179 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2180 return false;
2181 case Instruction::Add: {
2182 // Adding a power-of-two or zero to the same power-of-two or zero yields
2183 // either the original power-of-two, a larger power-of-two or zero.
2184 const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
2185 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2186 Q.IIQ.hasNoSignedWrap(VOBO)) {
2187 if (match(I->getOperand(0),
2188 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2189 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q))
2190 return true;
2191 if (match(I->getOperand(1),
2192 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2193 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q))
2194 return true;
2195
2196 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2197 KnownBits LHSBits(BitWidth);
2198 computeKnownBits(I->getOperand(0), LHSBits, Depth, Q);
2199
2200 KnownBits RHSBits(BitWidth);
2201 computeKnownBits(I->getOperand(1), RHSBits, Depth, Q);
2202 // If i8 V is a power of two or zero:
2203 // ZeroBits: 1 1 1 0 1 1 1 1
2204 // ~ZeroBits: 0 0 0 1 0 0 0 0
2205 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2206 // If OrZero isn't set, we cannot give back a zero result.
2207 // Make sure either the LHS or RHS has a bit set.
2208 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2209 return true;
2210 }
2211
2212 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2213 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2214 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2215 return true;
2216 return false;
2217 }
2218 case Instruction::Select:
2219 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2220 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Depth, Q);
2221 case Instruction::PHI: {
2222 // A PHI node is power of two if all incoming values are power of two, or if
2223 // it is an induction variable where in each step its value is a power of
2224 // two.
2225 auto *PN = cast<PHINode>(I);
2226 SimplifyQuery RecQ = Q;
2227
2228 // Check if it is an induction variable and always power of two.
2229 if (isPowerOfTwoRecurrence(PN, OrZero, Depth, RecQ))
2230 return true;
2231
2232 // Recursively check all incoming values. Limit recursion to 2 levels, so
2233 // that search complexity is limited to number of operands^2.
2234 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2235 return llvm::all_of(PN->operands(), [&](const Use &U) {
2236 // Value is power of 2 if it is coming from PHI node itself by induction.
2237 if (U.get() == PN)
2238 return true;
2239
2240 // Change the context instruction to the incoming block where it is
2241 // evaluated.
2242 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2243 return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ);
2244 });
2245 }
2246 case Instruction::Invoke:
2247 case Instruction::Call: {
2248 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2249 switch (II->getIntrinsicID()) {
2250 case Intrinsic::umax:
2251 case Intrinsic::smax:
2252 case Intrinsic::umin:
2253 case Intrinsic::smin:
2254 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Depth, Q) &&
2255 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2256 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2257 // thus dont change pow2/non-pow2 status.
2258 case Intrinsic::bitreverse:
2259 case Intrinsic::bswap:
2260 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2261 case Intrinsic::fshr:
2262 case Intrinsic::fshl:
2263 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2264 if (II->getArgOperand(0) == II->getArgOperand(1))
2265 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2266 break;
2267 default:
2268 break;
2269 }
2270 }
2271 return false;
2272 }
2273 default:
2274 return false;
2275 }
2276}
2277
2278/// Test whether a GEP's result is known to be non-null.
2279///
2280/// Uses properties inherent in a GEP to try to determine whether it is known
2281/// to be non-null.
2282///
2283/// Currently this routine does not support vector GEPs.
2284static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
2285 const SimplifyQuery &Q) {
2286 const Function *F = nullptr;
2287 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2288 F = I->getFunction();
2289
2290 if (!GEP->isInBounds() ||
2291 NullPointerIsDefined(F, GEP->getPointerAddressSpace()))
2292 return false;
2293
2294 // FIXME: Support vector-GEPs.
2295 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2296
2297 // If the base pointer is non-null, we cannot walk to a null address with an
2298 // inbounds GEP in address space zero.
2299 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2300 return true;
2301
2302 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2303 // If so, then the GEP cannot produce a null pointer, as doing so would
2304 // inherently violate the inbounds contract within address space zero.
2306 GTI != GTE; ++GTI) {
2307 // Struct types are easy -- they must always be indexed by a constant.
2308 if (StructType *STy = GTI.getStructTypeOrNull()) {
2309 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2310 unsigned ElementIdx = OpC->getZExtValue();
2311 const StructLayout *SL = Q.DL.getStructLayout(STy);
2312 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2313 if (ElementOffset > 0)
2314 return true;
2315 continue;
2316 }
2317
2318 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2319 if (GTI.getSequentialElementStride(Q.DL).isZero())
2320 continue;
2321
2322 // Fast path the constant operand case both for efficiency and so we don't
2323 // increment Depth when just zipping down an all-constant GEP.
2324 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2325 if (!OpC->isZero())
2326 return true;
2327 continue;
2328 }
2329
2330 // We post-increment Depth here because while isKnownNonZero increments it
2331 // as well, when we pop back up that increment won't persist. We don't want
2332 // to recurse 10k times just because we have 10k GEP operands. We don't
2333 // bail completely out because we want to handle constant GEPs regardless
2334 // of depth.
2336 continue;
2337
2338 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2339 return true;
2340 }
2341
2342 return false;
2343}
2344
2346 const Instruction *CtxI,
2347 const DominatorTree *DT) {
2348 assert(!isa<Constant>(V) && "Called for constant?");
2349
2350 if (!CtxI || !DT)
2351 return false;
2352
2353 unsigned NumUsesExplored = 0;
2354 for (const auto *U : V->users()) {
2355 // Avoid massive lists
2356 if (NumUsesExplored >= DomConditionsMaxUses)
2357 break;
2358 NumUsesExplored++;
2359
2360 // If the value is used as an argument to a call or invoke, then argument
2361 // attributes may provide an answer about null-ness.
2362 if (const auto *CB = dyn_cast<CallBase>(U))
2363 if (auto *CalledFunc = CB->getCalledFunction())
2364 for (const Argument &Arg : CalledFunc->args())
2365 if (CB->getArgOperand(Arg.getArgNo()) == V &&
2366 Arg.hasNonNullAttr(/* AllowUndefOrPoison */ false) &&
2367 DT->dominates(CB, CtxI))
2368 return true;
2369
2370 // If the value is used as a load/store, then the pointer must be non null.
2371 if (V == getLoadStorePointerOperand(U)) {
2372 const Instruction *I = cast<Instruction>(U);
2373 if (!NullPointerIsDefined(I->getFunction(),
2374 V->getType()->getPointerAddressSpace()) &&
2375 DT->dominates(I, CtxI))
2376 return true;
2377 }
2378
2379 if ((match(U, m_IDiv(m_Value(), m_Specific(V))) ||
2380 match(U, m_IRem(m_Value(), m_Specific(V)))) &&
2381 isValidAssumeForContext(cast<Instruction>(U), CtxI, DT))
2382 return true;
2383
2384 // Consider only compare instructions uniquely controlling a branch
2385 Value *RHS;
2386 CmpInst::Predicate Pred;
2387 if (!match(U, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2388 continue;
2389
2390 bool NonNullIfTrue;
2391 if (cmpExcludesZero(Pred, RHS))
2392 NonNullIfTrue = true;
2394 NonNullIfTrue = false;
2395 else
2396 continue;
2397
2400 for (const auto *CmpU : U->users()) {
2401 assert(WorkList.empty() && "Should be!");
2402 if (Visited.insert(CmpU).second)
2403 WorkList.push_back(CmpU);
2404
2405 while (!WorkList.empty()) {
2406 auto *Curr = WorkList.pop_back_val();
2407
2408 // If a user is an AND, add all its users to the work list. We only
2409 // propagate "pred != null" condition through AND because it is only
2410 // correct to assume that all conditions of AND are met in true branch.
2411 // TODO: Support similar logic of OR and EQ predicate?
2412 if (NonNullIfTrue)
2413 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2414 for (const auto *CurrU : Curr->users())
2415 if (Visited.insert(CurrU).second)
2416 WorkList.push_back(CurrU);
2417 continue;
2418 }
2419
2420 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2421 assert(BI->isConditional() && "uses a comparison!");
2422
2423 BasicBlock *NonNullSuccessor =
2424 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2425 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2426 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2427 return true;
2428 } else if (NonNullIfTrue && isGuard(Curr) &&
2429 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2430 return true;
2431 }
2432 }
2433 }
2434 }
2435
2436 return false;
2437}
2438
2439/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2440/// ensure that the value it's attached to is never Value? 'RangeType' is
2441/// is the type of the value described by the range.
2442static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2443 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2444 assert(NumRanges >= 1);
2445 for (unsigned i = 0; i < NumRanges; ++i) {
2447 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2449 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2450 ConstantRange Range(Lower->getValue(), Upper->getValue());
2451 if (Range.contains(Value))
2452 return false;
2453 }
2454 return true;
2455}
2456
2457/// Try to detect a recurrence that monotonically increases/decreases from a
2458/// non-zero starting value. These are common as induction variables.
2459static bool isNonZeroRecurrence(const PHINode *PN) {
2460 BinaryOperator *BO = nullptr;
2461 Value *Start = nullptr, *Step = nullptr;
2462 const APInt *StartC, *StepC;
2463 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2464 !match(Start, m_APInt(StartC)) || StartC->isZero())
2465 return false;
2466
2467 switch (BO->getOpcode()) {
2468 case Instruction::Add:
2469 // Starting from non-zero and stepping away from zero can never wrap back
2470 // to zero.
2471 return BO->hasNoUnsignedWrap() ||
2472 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
2473 StartC->isNegative() == StepC->isNegative());
2474 case Instruction::Mul:
2475 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2476 match(Step, m_APInt(StepC)) && !StepC->isZero();
2477 case Instruction::Shl:
2478 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
2479 case Instruction::AShr:
2480 case Instruction::LShr:
2481 return BO->isExact();
2482 default:
2483 return false;
2484 }
2485}
2486
2487static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth,
2488 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2489 Value *Y, bool NSW, bool NUW) {
2490 if (NUW)
2491 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2492 isKnownNonZero(X, DemandedElts, Q, Depth);
2493
2494 KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2495 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2496
2497 // If X and Y are both non-negative (as signed values) then their sum is not
2498 // zero unless both X and Y are zero.
2499 if (XKnown.isNonNegative() && YKnown.isNonNegative())
2500 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2501 isKnownNonZero(X, DemandedElts, Q, Depth))
2502 return true;
2503
2504 // If X and Y are both negative (as signed values) then their sum is not
2505 // zero unless both X and Y equal INT_MIN.
2506 if (XKnown.isNegative() && YKnown.isNegative()) {
2508 // The sign bit of X is set. If some other bit is set then X is not equal
2509 // to INT_MIN.
2510 if (XKnown.One.intersects(Mask))
2511 return true;
2512 // The sign bit of Y is set. If some other bit is set then Y is not equal
2513 // to INT_MIN.
2514 if (YKnown.One.intersects(Mask))
2515 return true;
2516 }
2517
2518 // The sum of a non-negative number and a power of two is not zero.
2519 if (XKnown.isNonNegative() &&
2520 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q))
2521 return true;
2522 if (YKnown.isNonNegative() &&
2523 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
2524 return true;
2525
2526 return KnownBits::computeForAddSub(/*Add=*/true, NSW, NUW, XKnown, YKnown)
2527 .isNonZero();
2528}
2529
2530static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth,
2531 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2532 Value *Y) {
2533 // TODO: Move this case into isKnownNonEqual().
2534 if (auto *C = dyn_cast<Constant>(X))
2535 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
2536 return true;
2537
2538 return ::isKnownNonEqual(X, Y, Depth, Q);
2539}
2540
2541static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth,
2542 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2543 Value *Y, bool NSW, bool NUW) {
2544 // If X and Y are non-zero then so is X * Y as long as the multiplication
2545 // does not overflow.
2546 if (NSW || NUW)
2547 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
2548 isKnownNonZero(Y, DemandedElts, Q, Depth);
2549
2550 // If either X or Y is odd, then if the other is non-zero the result can't
2551 // be zero.
2552 KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2553 if (XKnown.One[0])
2554 return isKnownNonZero(Y, DemandedElts, Q, Depth);
2555
2556 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2557 if (YKnown.One[0])
2558 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
2559
2560 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
2561 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
2562 // the lowest known One of X and Y. If they are non-zero, the result
2563 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
2564 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
2565 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
2566 BitWidth;
2567}
2568
2569static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
2570 unsigned Depth, const SimplifyQuery &Q,
2571 const KnownBits &KnownVal) {
2572 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2573 switch (I->getOpcode()) {
2574 case Instruction::Shl:
2575 return Lhs.shl(Rhs);
2576 case Instruction::LShr:
2577 return Lhs.lshr(Rhs);
2578 case Instruction::AShr:
2579 return Lhs.ashr(Rhs);
2580 default:
2581 llvm_unreachable("Unknown Shift Opcode");
2582 }
2583 };
2584
2585 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2586 switch (I->getOpcode()) {
2587 case Instruction::Shl:
2588 return Lhs.lshr(Rhs);
2589 case Instruction::LShr:
2590 case Instruction::AShr:
2591 return Lhs.shl(Rhs);
2592 default:
2593 llvm_unreachable("Unknown Shift Opcode");
2594 }
2595 };
2596
2597 if (KnownVal.isUnknown())
2598 return false;
2599
2600 KnownBits KnownCnt =
2601 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
2602 APInt MaxShift = KnownCnt.getMaxValue();
2603 unsigned NumBits = KnownVal.getBitWidth();
2604 if (MaxShift.uge(NumBits))
2605 return false;
2606
2607 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
2608 return true;
2609
2610 // If all of the bits shifted out are known to be zero, and Val is known
2611 // non-zero then at least one non-zero bit must remain.
2612 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
2613 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
2614 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
2615 return true;
2616
2617 return false;
2618}
2619
2621 const APInt &DemandedElts,
2622 unsigned Depth, const SimplifyQuery &Q) {
2623 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
2624 switch (I->getOpcode()) {
2625 case Instruction::Alloca:
2626 // Alloca never returns null, malloc might.
2627 return I->getType()->getPointerAddressSpace() == 0;
2628 case Instruction::GetElementPtr:
2629 if (I->getType()->isPointerTy())
2630 return isGEPKnownNonNull(cast<GEPOperator>(I), Depth, Q);
2631 break;
2632 case Instruction::BitCast: {
2633 // We need to be a bit careful here. We can only peek through the bitcast
2634 // if the scalar size of elements in the operand are smaller than and a
2635 // multiple of the size they are casting too. Take three cases:
2636 //
2637 // 1) Unsafe:
2638 // bitcast <2 x i16> %NonZero to <4 x i8>
2639 //
2640 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
2641 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
2642 // guranteed (imagine just sign bit set in the 2 i16 elements).
2643 //
2644 // 2) Unsafe:
2645 // bitcast <4 x i3> %NonZero to <3 x i4>
2646 //
2647 // Even though the scalar size of the src (`i3`) is smaller than the
2648 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
2649 // its possible for the `3 x i4` elements to be zero because there are
2650 // some elements in the destination that don't contain any full src
2651 // element.
2652 //
2653 // 3) Safe:
2654 // bitcast <4 x i8> %NonZero to <2 x i16>
2655 //
2656 // This is always safe as non-zero in the 4 i8 elements implies
2657 // non-zero in the combination of any two adjacent ones. Since i8 is a
2658 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
2659 // This all implies the 2 i16 elements are non-zero.
2660 Type *FromTy = I->getOperand(0)->getType();
2661 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
2662 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
2663 return isKnownNonZero(I->getOperand(0), Q, Depth);
2664 } break;
2665 case Instruction::IntToPtr:
2666 // Note that we have to take special care to avoid looking through
2667 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
2668 // as casts that can alter the value, e.g., AddrSpaceCasts.
2669 if (!isa<ScalableVectorType>(I->getType()) &&
2670 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2671 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2672 return isKnownNonZero(I->getOperand(0), Q, Depth);
2673 break;
2674 case Instruction::PtrToInt:
2675 // Similar to int2ptr above, we can look through ptr2int here if the cast
2676 // is a no-op or an extend and not a truncate.
2677 if (!isa<ScalableVectorType>(I->getType()) &&
2678 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2679 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2680 return isKnownNonZero(I->getOperand(0), Q, Depth);
2681 break;
2682 case Instruction::Trunc:
2683 // nuw/nsw trunc preserves zero/non-zero status of input.
2684 if (auto *TI = dyn_cast<TruncInst>(I))
2685 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
2686 return isKnownNonZero(TI->getOperand(0), Q, Depth);
2687 break;
2688
2689 case Instruction::Sub:
2690 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2691 I->getOperand(1));
2692 case Instruction::Or:
2693 // X | Y != 0 if X != 0 or Y != 0.
2694 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
2695 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2696 case Instruction::SExt:
2697 case Instruction::ZExt:
2698 // ext X != 0 if X != 0.
2699 return isKnownNonZero(I->getOperand(0), Q, Depth);
2700
2701 case Instruction::Shl: {
2702 // shl nsw/nuw can't remove any non-zero bits.
2703 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
2704 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
2705 return isKnownNonZero(I->getOperand(0), Q, Depth);
2706
2707 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
2708 // if the lowest bit is shifted off the end.
2709 KnownBits Known(BitWidth);
2710 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth, Q);
2711 if (Known.One[0])
2712 return true;
2713
2714 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
2715 }
2716 case Instruction::LShr:
2717 case Instruction::AShr: {
2718 // shr exact can only shift out zero bits.
2719 const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(I);
2720 if (BO->isExact())
2721 return isKnownNonZero(I->getOperand(0), Q, Depth);
2722
2723 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
2724 // defined if the sign bit is shifted off the end.
2725 KnownBits Known =
2726 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
2727 if (Known.isNegative())
2728 return true;
2729
2730 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
2731 }
2732 case Instruction::UDiv:
2733 case Instruction::SDiv: {
2734 // X / Y
2735 // div exact can only produce a zero if the dividend is zero.
2736 if (cast<PossiblyExactOperator>(I)->isExact())
2737 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2738
2739 KnownBits XKnown =
2740 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
2741 // If X is fully unknown we won't be able to figure anything out so don't
2742 // both computing knownbits for Y.
2743 if (XKnown.isUnknown())
2744 return false;
2745
2746 KnownBits YKnown =
2747 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
2748 if (I->getOpcode() == Instruction::SDiv) {
2749 // For signed division need to compare abs value of the operands.
2750 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
2751 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
2752 }
2753 // If X u>= Y then div is non zero (0/0 is UB).
2754 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
2755 // If X is total unknown or X u< Y we won't be able to prove non-zero
2756 // with compute known bits so just return early.
2757 return XUgeY && *XUgeY;
2758 }
2759 case Instruction::Add: {
2760 // X + Y.
2761
2762 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
2763 // non-zero.
2764 auto *BO = cast<OverflowingBinaryOperator>(I);
2765 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2766 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
2767 Q.IIQ.hasNoUnsignedWrap(BO));
2768 }
2769 case Instruction::Mul: {
2770 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
2771 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2772 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
2773 Q.IIQ.hasNoUnsignedWrap(BO));
2774 }
2775 case Instruction::Select: {
2776 // (C ? X : Y) != 0 if X != 0 and Y != 0.
2777
2778 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
2779 // then see if the select condition implies the arm is non-zero. For example
2780 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
2781 // dominated by `X != 0`.
2782 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
2783 Value *Op;
2784 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
2785 // Op is trivially non-zero.
2786 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
2787 return true;
2788
2789 // The condition of the select dominates the true/false arm. Check if the
2790 // condition implies that a given arm is non-zero.
2791 Value *X;
2792 CmpInst::Predicate Pred;
2793 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
2794 return false;
2795
2796 if (!IsTrueArm)
2797 Pred = ICmpInst::getInversePredicate(Pred);
2798
2799 return cmpExcludesZero(Pred, X);
2800 };
2801
2802 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
2803 SelectArmIsNonZero(/* IsTrueArm */ false))
2804 return true;
2805 break;
2806 }
2807 case Instruction::PHI: {
2808 auto *PN = cast<PHINode>(I);
2810 return true;
2811
2812 // Check if all incoming values are non-zero using recursion.
2813 SimplifyQuery RecQ = Q;
2814 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2815 return llvm::all_of(PN->operands(), [&](const Use &U) {
2816 if (U.get() == PN)
2817 return true;
2818 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2819 // Check if the branch on the phi excludes zero.
2820 ICmpInst::Predicate Pred;
2821 Value *X;
2822 BasicBlock *TrueSucc, *FalseSucc;
2823 if (match(RecQ.CxtI,
2824 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
2825 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
2826 // Check for cases of duplicate successors.
2827 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
2828 // If we're using the false successor, invert the predicate.
2829 if (FalseSucc == PN->getParent())
2830 Pred = CmpInst::getInversePredicate(Pred);
2831 if (cmpExcludesZero(Pred, X))
2832 return true;
2833 }
2834 }
2835 // Finally recurse on the edge and check it directly.
2836 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
2837 });
2838 }
2839 case Instruction::InsertElement: {
2840 if (isa<ScalableVectorType>(I->getType()))
2841 break;
2842
2843 const Value *Vec = I->getOperand(0);
2844 const Value *Elt = I->getOperand(1);
2845 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2846
2847 unsigned NumElts = DemandedElts.getBitWidth();
2848 APInt DemandedVecElts = DemandedElts;
2849 bool SkipElt = false;
2850 // If we know the index we are inserting too, clear it from Vec check.
2851 if (CIdx && CIdx->getValue().ult(NumElts)) {
2852 DemandedVecElts.clearBit(CIdx->getZExtValue());
2853 SkipElt = !DemandedElts[CIdx->getZExtValue()];
2854 }
2855
2856 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
2857 // are non-zero.
2858 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
2859 (DemandedVecElts.isZero() ||
2860 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
2861 }
2862 case Instruction::ExtractElement:
2863 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
2864 const Value *Vec = EEI->getVectorOperand();
2865 const Value *Idx = EEI->getIndexOperand();
2866 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2867 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
2868 unsigned NumElts = VecTy->getNumElements();
2869 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2870 if (CIdx && CIdx->getValue().ult(NumElts))
2871 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2872 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
2873 }
2874 }
2875 break;
2876 case Instruction::ShuffleVector: {
2877 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2878 if (!Shuf)
2879 break;
2880 APInt DemandedLHS, DemandedRHS;
2881 // For undef elements, we don't know anything about the common state of
2882 // the shuffle result.
2883 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
2884 break;
2885 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
2886 return (DemandedRHS.isZero() ||
2887 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
2888 (DemandedLHS.isZero() ||
2889 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
2890 }
2891 case Instruction::Freeze:
2892 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
2893 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2894 Depth);
2895 case Instruction::Load: {
2896 auto *LI = cast<LoadInst>(I);
2897 // A Load tagged with nonnull or dereferenceable with null pointer undefined
2898 // is never null.
2899 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
2900 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
2901 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
2902 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
2903 return true;
2904 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
2906 }
2907
2908 // No need to fall through to computeKnownBits as range metadata is already
2909 // handled in isKnownNonZero.
2910 return false;
2911 }
2912 case Instruction::ExtractValue: {
2913 const WithOverflowInst *WO;
2914 if (match(I, m_ExtractValue<0>(m_WithOverflowInst(WO)))) {
2915 switch (WO->getBinaryOp()) {
2916 default:
2917 break;
2918 case Instruction::Add:
2919 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
2920 WO->getArgOperand(0), WO->getArgOperand(1),
2921 /*NSW=*/false,
2922 /*NUW=*/false);
2923 case Instruction::Sub:
2924 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
2925 WO->getArgOperand(0), WO->getArgOperand(1));
2926 case Instruction::Mul:
2927 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth,
2928 WO->getArgOperand(0), WO->getArgOperand(1),
2929 /*NSW=*/false, /*NUW=*/false);
2930 break;
2931 }
2932 }
2933 break;
2934 }
2935 case Instruction::Call:
2936 case Instruction::Invoke: {
2937 const auto *Call = cast<CallBase>(I);
2938 if (I->getType()->isPointerTy()) {
2939 if (Call->isReturnNonNull())
2940 return true;
2941 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
2942 return isKnownNonZero(RP, Q, Depth);
2943 } else {
2944 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
2946 if (std::optional<ConstantRange> Range = Call->getRange()) {
2947 const APInt ZeroValue(Range->getBitWidth(), 0);
2948 if (!Range->contains(ZeroValue))
2949 return true;
2950 }
2951 if (const Value *RV = Call->getReturnedArgOperand())
2952 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
2953 return true;
2954 }
2955
2956 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2957 switch (II->getIntrinsicID()) {
2958 case Intrinsic::sshl_sat:
2959 case Intrinsic::ushl_sat:
2960 case Intrinsic::abs:
2961 case Intrinsic::bitreverse:
2962 case Intrinsic::bswap:
2963 case Intrinsic::ctpop:
2964 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
2965 // NB: We don't do usub_sat here as in any case we can prove its
2966 // non-zero, we will fold it to `sub nuw` in InstCombine.
2967 case Intrinsic::ssub_sat:
2968 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
2969 II->getArgOperand(0), II->getArgOperand(1));
2970 case Intrinsic::sadd_sat:
2971 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
2972 II->getArgOperand(0), II->getArgOperand(1),
2973 /*NSW=*/true, /* NUW=*/false);
2974 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
2975 case Intrinsic::vector_reduce_or:
2976 case Intrinsic::vector_reduce_umax:
2977 case Intrinsic::vector_reduce_umin:
2978 case Intrinsic::vector_reduce_smax:
2979 case Intrinsic::vector_reduce_smin:
2980 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
2981 case Intrinsic::umax:
2982 case Intrinsic::uadd_sat:
2983 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
2984 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
2985 case Intrinsic::smax: {
2986 // If either arg is strictly positive the result is non-zero. Otherwise
2987 // the result is non-zero if both ops are non-zero.
2988 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
2989 const KnownBits &OpKnown) {
2990 if (!OpNonZero.has_value())
2991 OpNonZero = OpKnown.isNonZero() ||
2992 isKnownNonZero(Op, DemandedElts, Q, Depth);
2993 return *OpNonZero;
2994 };
2995 // Avoid re-computing isKnownNonZero.
2996 std::optional<bool> Op0NonZero, Op1NonZero;
2997 KnownBits Op1Known =
2998 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
2999 if (Op1Known.isNonNegative() &&
3000 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3001 return true;
3002 KnownBits Op0Known =
3003 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
3004 if (Op0Known.isNonNegative() &&
3005 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3006 return true;
3007 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3008 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3009 }
3010 case Intrinsic::smin: {
3011 // If either arg is negative the result is non-zero. Otherwise
3012 // the result is non-zero if both ops are non-zero.
3013 KnownBits Op1Known =
3014 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
3015 if (Op1Known.isNegative())
3016 return true;
3017 KnownBits Op0Known =
3018 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
3019 if (Op0Known.isNegative())
3020 return true;
3021
3022 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3023 return true;
3024 }
3025 [[fallthrough]];
3026 case Intrinsic::umin:
3027 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3028 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3029 case Intrinsic::cttz:
3030 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
3031 .Zero[0];
3032 case Intrinsic::ctlz:
3033 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
3034 .isNonNegative();
3035 case Intrinsic::fshr:
3036 case Intrinsic::fshl:
3037 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3038 if (II->getArgOperand(0) == II->getArgOperand(1))
3039 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3040 break;
3041 case Intrinsic::vscale:
3042 return true;
3043 case Intrinsic::experimental_get_vector_length:
3044 return isKnownNonZero(I->getOperand(0), Q, Depth);
3045 default:
3046 break;
3047 }
3048 break;
3049 }
3050
3051 return false;
3052 }
3053 }
3054
3055 KnownBits Known(BitWidth);
3056 computeKnownBits(I, DemandedElts, Known, Depth, Q);
3057 return Known.One != 0;
3058}
3059
3060/// Return true if the given value is known to be non-zero when defined. For
3061/// vectors, return true if every demanded element is known to be non-zero when
3062/// defined. For pointers, if the context instruction and dominator tree are
3063/// specified, perform context-sensitive analysis and return true if the
3064/// pointer couldn't possibly be null at the specified instruction.
3065/// Supports values with integer or pointer type and vectors of integers.
3066bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3067 const SimplifyQuery &Q, unsigned Depth) {
3068 Type *Ty = V->getType();
3069
3070#ifndef NDEBUG
3071 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3072
3073 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3074 assert(
3075 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3076 "DemandedElt width should equal the fixed vector number of elements");
3077 } else {
3078 assert(DemandedElts == APInt(1, 1) &&
3079 "DemandedElt width should be 1 for scalars");
3080 }
3081#endif
3082
3083 if (auto *C = dyn_cast<Constant>(V)) {
3084 if (C->isNullValue())
3085 return false;
3086 if (isa<ConstantInt>(C))
3087 // Must be non-zero due to null test above.
3088 return true;
3089
3090 // For constant vectors, check that all elements are poison or known
3091 // non-zero to determine that the whole vector is known non-zero.
3092 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3093 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3094 if (!DemandedElts[i])
3095 continue;
3096 Constant *Elt = C->getAggregateElement(i);
3097 if (!Elt || Elt->isNullValue())
3098 return false;
3099 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3100 return false;
3101 }
3102 return true;
3103 }
3104
3105 // A global variable in address space 0 is non null unless extern weak
3106 // or an absolute symbol reference. Other address spaces may have null as a
3107 // valid address for a global, so we can't assume anything.
3108 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3109 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3110 GV->getType()->getAddressSpace() == 0)
3111 return true;
3112 }
3113
3114 // For constant expressions, fall through to the Operator code below.
3115 if (!isa<ConstantExpr>(V))
3116 return false;
3117 }
3118
3119 if (const auto *A = dyn_cast<Argument>(V))
3120 if (std::optional<ConstantRange> Range = A->getRange()) {
3121 const APInt ZeroValue(Range->getBitWidth(), 0);
3122 if (!Range->contains(ZeroValue))
3123 return true;
3124 }
3125
3126 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3127 return true;
3128
3129 // Some of the tests below are recursive, so bail out if we hit the limit.
3131 return false;
3132
3133 // Check for pointer simplifications.
3134
3135 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3136 // A byval, inalloca may not be null in a non-default addres space. A
3137 // nonnull argument is assumed never 0.
3138 if (const Argument *A = dyn_cast<Argument>(V)) {
3139 if (((A->hasPassPointeeByValueCopyAttr() &&
3140 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3141 A->hasNonNullAttr()))
3142 return true;
3143 }
3144 }
3145
3146 if (const auto *I = dyn_cast<Operator>(V))
3147 if (isKnownNonZeroFromOperator(I, DemandedElts, Depth, Q))
3148 return true;
3149
3150 if (!isa<Constant>(V) &&
3152 return true;
3153
3154 return false;
3155}
3156
3158 unsigned Depth) {
3159 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3160 APInt DemandedElts =
3161 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3162 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3163}
3164
3165/// If the pair of operators are the same invertible function, return the
3166/// the operands of the function corresponding to each input. Otherwise,
3167/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3168/// every input value to exactly one output value. This is equivalent to
3169/// saying that Op1 and Op2 are equal exactly when the specified pair of
3170/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3171static std::optional<std::pair<Value*, Value*>>
3173 const Operator *Op2) {
3174 if (Op1->getOpcode() != Op2->getOpcode())
3175 return std::nullopt;
3176
3177 auto getOperands = [&](unsigned OpNum) -> auto {
3178 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3179 };
3180
3181 switch (Op1->getOpcode()) {
3182 default:
3183 break;
3184 case Instruction::Or:
3185 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3186 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3187 break;
3188 [[fallthrough]];
3189 case Instruction::Xor:
3190 case Instruction::Add: {
3191 Value *Other;
3192 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3193 return std::make_pair(Op1->getOperand(1), Other);
3194 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3195 return std::make_pair(Op1->getOperand(0), Other);
3196 break;
3197 }
3198 case Instruction::Sub:
3199 if (Op1->getOperand(0) == Op2->getOperand(0))
3200 return getOperands(1);
3201 if (Op1->getOperand(1) == Op2->getOperand(1))
3202 return getOperands(0);
3203 break;
3204 case Instruction::Mul: {
3205 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3206 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3207 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3208 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3209 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3210 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3211 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3212 break;
3213
3214 // Assume operand order has been canonicalized
3215 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3216 isa<ConstantInt>(Op1->getOperand(1)) &&
3217 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3218 return getOperands(0);
3219 break;
3220 }
3221 case Instruction::Shl: {
3222 // Same as multiplies, with the difference that we don't need to check
3223 // for a non-zero multiply. Shifts always multiply by non-zero.
3224 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3225 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3226 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3227 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3228 break;
3229
3230 if (Op1->getOperand(1) == Op2->getOperand(1))
3231 return getOperands(0);
3232 break;
3233 }
3234 case Instruction::AShr:
3235 case Instruction::LShr: {
3236 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3237 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3238 if (!PEO1->isExact() || !PEO2->isExact())
3239 break;
3240
3241 if (Op1->getOperand(1) == Op2->getOperand(1))
3242 return getOperands(0);
3243 break;
3244 }
3245 case Instruction::SExt:
3246 case Instruction::ZExt:
3247 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3248 return getOperands(0);
3249 break;
3250 case Instruction::PHI: {
3251 const PHINode *PN1 = cast<PHINode>(Op1);
3252 const PHINode *PN2 = cast<PHINode>(Op2);
3253
3254 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3255 // are a single invertible function of the start values? Note that repeated
3256 // application of an invertible function is also invertible
3257 BinaryOperator *BO1 = nullptr;
3258 Value *Start1 = nullptr, *Step1 = nullptr;
3259 BinaryOperator *BO2 = nullptr;
3260 Value *Start2 = nullptr, *Step2 = nullptr;
3261 if (PN1->getParent() != PN2->getParent() ||
3262 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3263 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3264 break;
3265
3266 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3267 cast<Operator>(BO2));
3268 if (!Values)
3269 break;
3270
3271 // We have to be careful of mutually defined recurrences here. Ex:
3272 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3273 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3274 // The invertibility of these is complicated, and not worth reasoning
3275 // about (yet?).
3276 if (Values->first != PN1 || Values->second != PN2)
3277 break;
3278
3279 return std::make_pair(Start1, Start2);
3280 }
3281 }
3282 return std::nullopt;
3283}
3284
3285/// Return true if V1 == (binop V2, X), where X is known non-zero.
3286/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3287/// implies V2 != V1.
3288static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3289 unsigned Depth, const SimplifyQuery &Q) {
3290 const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
3291 if (!BO)
3292 return false;
3293 switch (BO->getOpcode()) {
3294 default:
3295 break;
3296 case Instruction::Or:
3297 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3298 break;
3299 [[fallthrough]];
3300 case Instruction::Xor:
3301 case Instruction::Add:
3302 Value *Op = nullptr;
3303 if (V2 == BO->getOperand(0))
3304 Op = BO->getOperand(1);
3305 else if (V2 == BO->getOperand(1))
3306 Op = BO->getOperand(0);
3307 else
3308 return false;
3309 return isKnownNonZero(Op, Q, Depth + 1);
3310 }
3311 return false;
3312}
3313
3314/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3315/// the multiplication is nuw or nsw.
3316static bool isNonEqualMul(const Value *V1, const Value *V2, unsigned Depth,
3317 const SimplifyQuery &Q) {
3318 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3319 const APInt *C;
3320 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3321 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3322 !C->isZero() && !C->isOne() && isKnownNonZero(V1, Q, Depth + 1);
3323 }
3324 return false;
3325}
3326
3327/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3328/// the shift is nuw or nsw.
3329static bool isNonEqualShl(const Value *V1, const Value *V2, unsigned Depth,
3330 const SimplifyQuery &Q) {
3331 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3332 const APInt *C;
3333 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3334 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3335 !C->isZero() && isKnownNonZero(V1, Q, Depth + 1);
3336 }
3337 return false;
3338}
3339
3340static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3341 unsigned Depth, const SimplifyQuery &Q) {
3342 // Check two PHIs are in same block.
3343 if (PN1->getParent() != PN2->getParent())
3344 return false;
3345
3347 bool UsedFullRecursion = false;
3348 for (const BasicBlock *IncomBB : PN1->blocks()) {
3349 if (!VisitedBBs.insert(IncomBB).second)
3350 continue; // Don't reprocess blocks that we have dealt with already.
3351 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3352 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3353 const APInt *C1, *C2;
3354 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3355 continue;
3356
3357 // Only one pair of phi operands is allowed for full recursion.
3358 if (UsedFullRecursion)
3359 return false;
3360
3361 SimplifyQuery RecQ = Q;
3362 RecQ.CxtI = IncomBB->getTerminator();
3363 if (!isKnownNonEqual(IV1, IV2, Depth + 1, RecQ))
3364 return false;
3365 UsedFullRecursion = true;
3366 }
3367 return true;
3368}
3369
3370static bool isNonEqualSelect(const Value *V1, const Value *V2, unsigned Depth,
3371 const SimplifyQuery &Q) {
3372 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3373 if (!SI1)
3374 return false;
3375
3376 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3377 const Value *Cond1 = SI1->getCondition();
3378 const Value *Cond2 = SI2->getCondition();
3379 if (Cond1 == Cond2)
3380 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3381 Depth + 1, Q) &&
3382 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3383 Depth + 1, Q);
3384 }
3385 return isKnownNonEqual(SI1->getTrueValue(), V2, Depth + 1, Q) &&
3386 isKnownNonEqual(SI1->getFalseValue(), V2, Depth + 1, Q);
3387}
3388
3389// Check to see if A is both a GEP and is the incoming value for a PHI in the
3390// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3391// one of them being the recursive GEP A and the other a ptr at same base and at
3392// the same/higher offset than B we are only incrementing the pointer further in
3393// loop if offset of recursive GEP is greater than 0.
3395 const SimplifyQuery &Q) {
3396 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3397 return false;
3398
3399 auto *GEPA = dyn_cast<GEPOperator>(A);
3400 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3401 return false;
3402
3403 // Handle 2 incoming PHI values with one being a recursive GEP.
3404 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3405 if (!PN || PN->getNumIncomingValues() != 2)
3406 return false;
3407
3408 // Search for the recursive GEP as an incoming operand, and record that as
3409 // Step.
3410 Value *Start = nullptr;
3411 Value *Step = const_cast<Value *>(A);
3412 if (PN->getIncomingValue(0) == Step)
3413 Start = PN->getIncomingValue(1);
3414 else if (PN->getIncomingValue(1) == Step)
3415 Start = PN->getIncomingValue(0);
3416 else
3417 return false;
3418
3419 // Other incoming node base should match the B base.
3420 // StartOffset >= OffsetB && StepOffset > 0?
3421 // StartOffset <= OffsetB && StepOffset < 0?
3422 // Is non-equal if above are true.
3423 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3424 // optimisation to inbounds GEPs only.
3425 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3426 APInt StartOffset(IndexWidth, 0);
3427 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3428 APInt StepOffset(IndexWidth, 0);
3429 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3430
3431 // Check if Base Pointer of Step matches the PHI.
3432 if (Step != PN)
3433 return false;
3434 APInt OffsetB(IndexWidth, 0);
3435 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3436 return Start == B &&
3437 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
3438 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
3439}
3440
3441/// Return true if it is known that V1 != V2.
3442static bool isKnownNonEqual(const Value *V1, const Value *V2, unsigned Depth,
3443 const SimplifyQuery &Q) {
3444 if (V1 == V2)
3445 return false;
3446 if (V1->getType() != V2->getType())
3447 // We can't look through casts yet.
3448 return false;
3449
3451 return false;
3452
3453 // See if we can recurse through (exactly one of) our operands. This
3454 // requires our operation be 1-to-1 and map every input value to exactly
3455 // one output value. Such an operation is invertible.
3456 auto *O1 = dyn_cast<Operator>(V1);
3457 auto *O2 = dyn_cast<Operator>(V2);
3458 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3459 if (auto Values = getInvertibleOperands(O1, O2))
3460 return isKnownNonEqual(Values->first, Values->second, Depth + 1, Q);
3461
3462 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3463 const PHINode *PN2 = cast<PHINode>(V2);
3464 // FIXME: This is missing a generalization to handle the case where one is
3465 // a PHI and another one isn't.
3466 if (isNonEqualPHIs(PN1, PN2, Depth, Q))
3467 return true;
3468 };
3469 }
3470
3471 if (isModifyingBinopOfNonZero(V1, V2, Depth, Q) ||
3472 isModifyingBinopOfNonZero(V2, V1, Depth, Q))
3473 return true;
3474
3475 if (isNonEqualMul(V1, V2, Depth, Q) || isNonEqualMul(V2, V1, Depth, Q))
3476 return true;
3477
3478 if (isNonEqualShl(V1, V2, Depth, Q) || isNonEqualShl(V2, V1, Depth, Q))
3479 return true;
3480
3481 if (V1->getType()->isIntOrIntVectorTy()) {
3482 // Are any known bits in V1 contradictory to known bits in V2? If V1
3483 // has a known zero where V2 has a known one, they must not be equal.
3484 KnownBits Known1 = computeKnownBits(V1, Depth, Q);
3485 if (!Known1.isUnknown()) {
3486 KnownBits Known2 = computeKnownBits(V2, Depth, Q);
3487 if (Known1.Zero.intersects(Known2.One) ||
3488 Known2.Zero.intersects(Known1.One))
3489 return true;
3490 }
3491 }
3492
3493 if (isNonEqualSelect(V1, V2, Depth, Q) || isNonEqualSelect(V2, V1, Depth, Q))
3494 return true;
3495
3496 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3498 return true;
3499
3500 Value *A, *B;
3501 // PtrToInts are NonEqual if their Ptrs are NonEqual.
3502 // Check PtrToInt type matches the pointer size.
3503 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
3505 return isKnownNonEqual(A, B, Depth + 1, Q);
3506
3507 return false;
3508}
3509
3510// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
3511// Returns the input and lower/upper bounds.
3512static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
3513 const APInt *&CLow, const APInt *&CHigh) {
3514 assert(isa<Operator>(Select) &&
3515 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
3516 "Input should be a Select!");
3517
3518 const Value *LHS = nullptr, *RHS = nullptr;
3520 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
3521 return false;
3522
3523 if (!match(RHS, m_APInt(CLow)))
3524 return false;
3525
3526 const Value *LHS2 = nullptr, *RHS2 = nullptr;
3528 if (getInverseMinMaxFlavor(SPF) != SPF2)
3529 return false;
3530
3531 if (!match(RHS2, m_APInt(CHigh)))
3532 return false;
3533
3534 if (SPF == SPF_SMIN)
3535 std::swap(CLow, CHigh);
3536
3537 In = LHS2;
3538 return CLow->sle(*CHigh);
3539}
3540
3542 const APInt *&CLow,
3543 const APInt *&CHigh) {
3544 assert((II->getIntrinsicID() == Intrinsic::smin ||
3545 II->getIntrinsicID() == Intrinsic::smax) && "Must be smin/smax");
3546
3548 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
3549 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
3550 !match(II->getArgOperand(1), m_APInt(CLow)) ||
3551 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
3552 return false;
3553
3554 if (II->getIntrinsicID() == Intrinsic::smin)
3555 std::swap(CLow, CHigh);
3556 return CLow->sle(*CHigh);
3557}
3558
3559/// For vector constants, loop over the elements and find the constant with the
3560/// minimum number of sign bits. Return 0 if the value is not a vector constant
3561/// or if any element was not analyzed; otherwise, return the count for the
3562/// element with the minimum number of sign bits.
3564 const APInt &DemandedElts,
3565 unsigned TyBits) {
3566 const auto *CV = dyn_cast<Constant>(V);
3567 if (!CV || !isa<FixedVectorType>(CV->getType()))
3568 return 0;
3569
3570 unsigned MinSignBits = TyBits;
3571 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3572 for (unsigned i = 0; i != NumElts; ++i) {
3573 if (!DemandedElts[i])
3574 continue;
3575 // If we find a non-ConstantInt, bail out.
3576 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3577 if (!Elt)
3578 return 0;
3579
3580 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3581 }
3582
3583 return MinSignBits;
3584}
3585
3586static unsigned ComputeNumSignBitsImpl(const Value *V,
3587 const APInt &DemandedElts,
3588 unsigned Depth, const SimplifyQuery &Q);
3589
3590static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
3591 unsigned Depth, const SimplifyQuery &Q) {
3592 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q);
3593 assert(Result > 0 && "At least one sign bit needs to be present!");
3594 return Result;
3595}
3596
3597/// Return the number of times the sign bit of the register is replicated into
3598/// the other bits. We know that at least 1 bit is always equal to the sign bit
3599/// (itself), but other cases can give us information. For example, immediately
3600/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
3601/// other, so we return 3. For vectors, return the number of sign bits for the
3602/// vector element with the minimum number of known sign bits of the demanded
3603/// elements in the vector specified by DemandedElts.
3604static unsigned ComputeNumSignBitsImpl(const Value *V,
3605 const APInt &DemandedElts,
3606 unsigned Depth, const SimplifyQuery &Q) {
3607 Type *Ty = V->getType();
3608#ifndef NDEBUG
3609 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3610
3611 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3612 assert(
3613 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3614 "DemandedElt width should equal the fixed vector number of elements");
3615 } else {
3616 assert(DemandedElts == APInt(1, 1) &&
3617 "DemandedElt width should be 1 for scalars");
3618 }
3619#endif
3620
3621 // We return the minimum number of sign bits that are guaranteed to be present
3622 // in V, so for undef we have to conservatively return 1. We don't have the
3623 // same behavior for poison though -- that's a FIXME today.
3624
3625 Type *ScalarTy = Ty->getScalarType();
3626 unsigned TyBits = ScalarTy->isPointerTy() ?
3627 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
3628 Q.DL.getTypeSizeInBits(ScalarTy);
3629
3630 unsigned Tmp, Tmp2;
3631 unsigned FirstAnswer = 1;
3632
3633 // Note that ConstantInt is handled by the general computeKnownBits case
3634 // below.
3635
3637 return 1;
3638
3639 if (auto *U = dyn_cast<Operator>(V)) {
3640 switch (Operator::getOpcode(V)) {
3641 default: break;
3642 case Instruction::SExt:
3643 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
3644 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q) + Tmp;
3645
3646 case Instruction::SDiv: {
3647 const APInt *Denominator;
3648 // sdiv X, C -> adds log(C) sign bits.
3649 if (match(U->getOperand(1), m_APInt(Denominator))) {
3650
3651 // Ignore non-positive denominator.
3652 if (!Denominator->isStrictlyPositive())
3653 break;
3654
3655 // Calculate the incoming numerator bits.
3656 unsigned NumBits = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3657
3658 // Add floor(log(C)) bits to the numerator bits.
3659 return std::min(TyBits, NumBits + Denominator->logBase2());
3660 }
3661 break;
3662 }
3663
3664 case Instruction::SRem: {
3665 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3666
3667 const APInt *Denominator;
3668 // srem X, C -> we know that the result is within [-C+1,C) when C is a
3669 // positive constant. This let us put a lower bound on the number of sign
3670 // bits.
3671 if (match(U->getOperand(1), m_APInt(Denominator))) {
3672
3673 // Ignore non-positive denominator.
3674 if (Denominator->isStrictlyPositive()) {
3675 // Calculate the leading sign bit constraints by examining the
3676 // denominator. Given that the denominator is positive, there are two
3677 // cases:
3678 //
3679 // 1. The numerator is positive. The result range is [0,C) and
3680 // [0,C) u< (1 << ceilLogBase2(C)).
3681 //
3682 // 2. The numerator is negative. Then the result range is (-C,0] and
3683 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
3684 //
3685 // Thus a lower bound on the number of sign bits is `TyBits -
3686 // ceilLogBase2(C)`.
3687
3688 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
3689 Tmp = std::max(Tmp, ResBits);
3690 }
3691 }
3692 return Tmp;
3693 }
3694
3695 case Instruction::AShr: {
3696 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3697 // ashr X, C -> adds C sign bits. Vectors too.
3698 const APInt *ShAmt;
3699 if (match(U->getOperand(1), m_APInt(ShAmt))) {
3700 if (ShAmt->uge(TyBits))
3701 break; // Bad shift.
3702 unsigned ShAmtLimited = ShAmt->getZExtValue();
3703 Tmp += ShAmtLimited;
3704 if (Tmp > TyBits) Tmp = TyBits;
3705 }
3706 return Tmp;
3707 }
3708 case Instruction::Shl: {
3709 const APInt *ShAmt;
3710 if (match(U->getOperand(1), m_APInt(ShAmt))) {
3711 // shl destroys sign bits.
3712 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3713 if (ShAmt->uge(TyBits) || // Bad shift.
3714 ShAmt->uge(Tmp)) break; // Shifted all sign bits out.
3715 Tmp2 = ShAmt->getZExtValue();
3716 return Tmp - Tmp2;
3717 }
3718 break;
3719 }
3720 case Instruction::And:
3721 case Instruction::Or:
3722 case Instruction::Xor: // NOT is handled here.
3723 // Logical binary ops preserve the number of sign bits at the worst.
3724 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3725 if (Tmp != 1) {
3726 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3727 FirstAnswer = std::min(Tmp, Tmp2);
3728 // We computed what we know about the sign bits as our first
3729 // answer. Now proceed to the generic code that uses
3730 // computeKnownBits, and pick whichever answer is better.
3731 }
3732 break;
3733
3734 case Instruction::Select: {
3735 // If we have a clamp pattern, we know that the number of sign bits will
3736 // be the minimum of the clamp min/max range.
3737 const Value *X;
3738 const APInt *CLow, *CHigh;
3739 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
3740 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
3741
3742 Tmp = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3743 if (Tmp == 1) break;
3744 Tmp2 = ComputeNumSignBits(U->getOperand(2), Depth + 1, Q);
3745 return std::min(Tmp, Tmp2);
3746 }
3747
3748 case Instruction::Add:
3749 // Add can have at most one carry bit. Thus we know that the output
3750 // is, at worst, one more bit than the inputs.
3751 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3752 if (Tmp == 1) break;
3753
3754 // Special case decrementing a value (ADD X, -1):
3755 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
3756 if (CRHS->isAllOnesValue()) {
3757 KnownBits Known(TyBits);
3758 computeKnownBits(U->getOperand(0), Known, Depth + 1, Q);
3759
3760 // If the input is known to be 0 or 1, the output is 0/-1, which is
3761 // all sign bits set.
3762 if ((Known.Zero | 1).isAllOnes())
3763 return TyBits;
3764
3765 // If we are subtracting one from a positive number, there is no carry
3766 // out of the result.
3767 if (Known.isNonNegative())
3768 return Tmp;
3769 }
3770
3771 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3772 if (Tmp2 == 1) break;
3773 return std::min(Tmp, Tmp2) - 1;
3774
3775 case Instruction::Sub:
3776 Tmp2 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3777 if (Tmp2 == 1) break;
3778
3779 // Handle NEG.
3780 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
3781 if (CLHS->isNullValue()) {
3782 KnownBits Known(TyBits);
3783 computeKnownBits(U->getOperand(1), Known, Depth + 1, Q);
3784 // If the input is known to be 0 or 1, the output is 0/-1, which is
3785 // all sign bits set.
3786 if ((Known.Zero | 1).isAllOnes())
3787 return TyBits;
3788
3789 // If the input is known to be positive (the sign bit is known clear),
3790 // the output of the NEG has the same number of sign bits as the
3791 // input.
3792 if (Known.isNonNegative())
3793 return Tmp2;
3794
3795 // Otherwise, we treat this like a SUB.
3796 }
3797
3798 // Sub can have at most one carry bit. Thus we know that the output
3799 // is, at worst, one more bit than the inputs.
3800 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3801 if (Tmp == 1) break;
3802 return std::min(Tmp, Tmp2) - 1;
3803
3804 case Instruction::Mul: {
3805 // The output of the Mul can be at most twice the valid bits in the
3806 // inputs.
3807 unsigned SignBitsOp0 = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3808 if (SignBitsOp0 == 1) break;
3809 unsigned SignBitsOp1 = ComputeNumSignBits(U->getOperand(1), Depth + 1, Q);
3810 if (SignBitsOp1 == 1) break;
3811 unsigned OutValidBits =
3812 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
3813 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
3814 }
3815
3816 case Instruction::PHI: {
3817 const PHINode *PN = cast<PHINode>(U);
3818 unsigned NumIncomingValues = PN->getNumIncomingValues();
3819 // Don't analyze large in-degree PHIs.
3820 if (NumIncomingValues > 4) break;
3821 // Unreachable blocks may have zero-operand PHI nodes.
3822 if (NumIncomingValues == 0) break;
3823
3824 // Take the minimum of all incoming values. This can't infinitely loop
3825 // because of our depth threshold.
3826 SimplifyQuery RecQ = Q;
3827 Tmp = TyBits;
3828 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
3829 if (Tmp == 1) return Tmp;
3830 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
3831 Tmp = std::min(
3832 Tmp, ComputeNumSignBits(PN->getIncomingValue(i), Depth + 1, RecQ));
3833 }
3834 return Tmp;
3835 }
3836
3837 case Instruction::Trunc: {
3838 // If the input contained enough sign bits that some remain after the
3839 // truncation, then we can make use of that. Otherwise we don't know
3840 // anything.
3841 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3842 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
3843 if (Tmp > (OperandTyBits - TyBits))
3844 return Tmp - (OperandTyBits - TyBits);
3845
3846 return 1;
3847 }
3848
3849 case Instruction::ExtractElement:
3850 // Look through extract element. At the moment we keep this simple and
3851 // skip tracking the specific element. But at least we might find
3852 // information valid for all elements of the vector (for example if vector
3853 // is sign extended, shifted, etc).
3854 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3855
3856 case Instruction::ShuffleVector: {
3857 // Collect the minimum number of sign bits that are shared by every vector
3858 // element referenced by the shuffle.
3859 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
3860 if (!Shuf) {
3861 // FIXME: Add support for shufflevector constant expressions.
3862 return 1;
3863 }
3864 APInt DemandedLHS, DemandedRHS;
3865 // For undef elements, we don't know anything about the common state of
3866 // the shuffle result.
3867 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3868 return 1;
3869 Tmp = std::numeric_limits<unsigned>::max();
3870 if (!!DemandedLHS) {
3871 const Value *LHS = Shuf->getOperand(0);
3872 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q);
3873 }
3874 // If we don't know anything, early out and try computeKnownBits
3875 // fall-back.
3876 if (Tmp == 1)
3877 break;
3878 if (!!DemandedRHS) {
3879 const Value *RHS = Shuf->getOperand(1);
3880 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q);
3881 Tmp = std::min(Tmp, Tmp2);
3882 }
3883 // If we don't know anything, early out and try computeKnownBits
3884 // fall-back.
3885 if (Tmp == 1)
3886 break;
3887 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
3888 return Tmp;
3889 }
3890 case Instruction::Call: {
3891 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
3892 switch (II->getIntrinsicID()) {
3893 default: break;
3894 case Intrinsic::abs:
3895 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
3896 if (Tmp == 1) break;
3897
3898 // Absolute value reduces number of sign bits by at most 1.
3899 return Tmp - 1;
3900 case Intrinsic::smin:
3901 case Intrinsic::smax: {
3902 const APInt *CLow, *CHigh;
3903 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
3904 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
3905 }
3906 }
3907 }
3908 }
3909 }
3910 }
3911
3912 // Finally, if we can prove that the top bits of the result are 0's or 1's,
3913 // use this information.
3914
3915 // If we can examine all elements of a vector constant successfully, we're
3916 // done (we can't do any better than that). If not, keep trying.
3917 if (unsigned VecSignBits =
3918 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
3919 return VecSignBits;
3920
3921 KnownBits Known(TyBits);
3922 computeKnownBits(V, DemandedElts, Known, Depth, Q);
3923
3924 // If we know that the sign bit is either zero or one, determine the number of
3925 // identical bits in the top of the input value.
3926 return std::max(FirstAnswer, Known.countMinSignBits());
3927}
3928
3930 const TargetLibraryInfo *TLI) {
3931 const Function *F = CB.getCalledFunction();
3932 if (!F)
3934
3935 if (F->isIntrinsic())
3936 return F->getIntrinsicID();
3937
3938 // We are going to infer semantics of a library function based on mapping it
3939 // to an LLVM intrinsic. Check that the library function is available from
3940 // this callbase and in this environment.
3941 LibFunc Func;
3942 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
3943 !CB.onlyReadsMemory())
3945
3946 switch (Func) {
3947 default:
3948 break;
3949 case LibFunc_sin:
3950 case LibFunc_sinf:
3951 case LibFunc_sinl:
3952 return Intrinsic::sin;
3953 case LibFunc_cos:
3954 case LibFunc_cosf:
3955 case LibFunc_cosl:
3956 return Intrinsic::cos;
3957 case LibFunc_exp:
3958 case LibFunc_expf:
3959 case LibFunc_expl:
3960 return Intrinsic::exp;
3961 case LibFunc_exp2:
3962 case LibFunc_exp2f:
3963 case LibFunc_exp2l:
3964 return Intrinsic::exp2;
3965 case LibFunc_log:
3966 case LibFunc_logf:
3967 case LibFunc_logl:
3968 return Intrinsic::log;
3969 case LibFunc_log10:
3970 case LibFunc_log10f:
3971 case LibFunc_log10l:
3972 return Intrinsic::log10;
3973 case LibFunc_log2:
3974 case LibFunc_log2f:
3975 case LibFunc_log2l:
3976 return Intrinsic::log2;
3977 case LibFunc_fabs:
3978 case LibFunc_fabsf:
3979 case LibFunc_fabsl:
3980 return Intrinsic::fabs;
3981 case LibFunc_fmin:
3982 case LibFunc_fminf:
3983 case LibFunc_fminl:
3984 return Intrinsic::minnum;
3985 case LibFunc_fmax:
3986 case LibFunc_fmaxf:
3987 case LibFunc_fmaxl:
3988 return Intrinsic::maxnum;
3989 case LibFunc_copysign:
3990 case LibFunc_copysignf:
3991 case LibFunc_copysignl:
3992 return Intrinsic::copysign;
3993 case LibFunc_floor:
3994 case LibFunc_floorf:
3995 case LibFunc_floorl:
3996 return Intrinsic::floor;
3997 case LibFunc_ceil:
3998 case LibFunc_ceilf:
3999 case LibFunc_ceill:
4000 return Intrinsic::ceil;
4001 case LibFunc_trunc:
4002 case LibFunc_truncf:
4003 case LibFunc_truncl:
4004 return Intrinsic::trunc;
4005 case LibFunc_rint:
4006 case LibFunc_rintf:
4007 case LibFunc_rintl:
4008 return Intrinsic::rint;
4009 case LibFunc_nearbyint:
4010 case LibFunc_nearbyintf:
4011 case LibFunc_nearbyintl:
4012 return Intrinsic::nearbyint;
4013 case LibFunc_round:
4014 case LibFunc_roundf:
4015 case LibFunc_roundl:
4016 return Intrinsic::round;
4017 case LibFunc_roundeven:
4018 case LibFunc_roundevenf:
4019 case LibFunc_roundevenl:
4020 return Intrinsic::roundeven;
4021 case LibFunc_pow:
4022 case LibFunc_powf:
4023 case LibFunc_powl:
4024 return Intrinsic::pow;
4025 case LibFunc_sqrt:
4026 case LibFunc_sqrtf:
4027 case LibFunc_sqrtl:
4028 return Intrinsic::sqrt;
4029 }
4030
4032}
4033
4034/// Return true if it's possible to assume IEEE treatment of input denormals in
4035/// \p F for \p Val.
4036static bool inputDenormalIsIEEE(const Function &F, const Type *Ty) {
4037 Ty = Ty->getScalarType();
4038 return F.getDenormalMode(Ty->getFltSemantics()).Input == DenormalMode::IEEE;
4039}
4040
4041static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4042 Ty = Ty->getScalarType();
4043 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4044 return Mode.Input == DenormalMode::IEEE ||
4045 Mode.Input == DenormalMode::PositiveZero;
4046}
4047
4048static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4049 Ty = Ty->getScalarType();
4050 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4051 return Mode.Output == DenormalMode::IEEE ||
4052 Mode.Output == DenormalMode::PositiveZero;
4053}
4054
4056 return isKnownNeverZero() &&
4058}
4059
4061 Type *Ty) const {
4062 return isKnownNeverNegZero() &&
4064}
4065
4067 Type *Ty) const {
4068 if (!isKnownNeverPosZero())
4069 return false;
4070
4071 // If we know there are no denormals, nothing can be flushed to zero.
4073 return true;
4074
4075 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4076 switch (Mode.Input) {
4077 case DenormalMode::IEEE:
4078 return true;
4080 // Negative subnormal won't flush to +0
4081 return isKnownNeverPosSubnormal();
4083 default:
4084 // Both positive and negative subnormal could flush to +0
4085 return false;
4086 }
4087
4088 llvm_unreachable("covered switch over denormal mode");
4089}
4090
4092 Type *Ty) {
4093 KnownFPClasses = Src.KnownFPClasses;
4094 // If we aren't assuming the source can't be a zero, we don't have to check if
4095 // a denormal input could be flushed.
4096 if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero())
4097 return;
4098
4099 // If we know the input can't be a denormal, it can't be flushed to 0.
4100 if (Src.isKnownNeverSubnormal())
4101 return;
4102
4103 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4104
4105 if (!Src.isKnownNeverPosSubnormal() && Mode != DenormalMode::getIEEE())
4107
4108 if (!Src.isKnownNeverNegSubnormal() && Mode != DenormalMode::getIEEE()) {
4109 if (Mode != DenormalMode::getPositiveZero())
4111
4112 if (Mode.Input == DenormalMode::PositiveZero ||
4113 Mode.Output == DenormalMode::PositiveZero ||
4114 Mode.Input == DenormalMode::Dynamic ||
4115 Mode.Output == DenormalMode::Dynamic)
4117 }
4118}
4119
4121 const Function &F, Type *Ty) {
4122 propagateDenormal(Src, F, Ty);
4123 propagateNaN(Src, /*PreserveSign=*/true);
4124}
4125
4126/// Given an exploded icmp instruction, return true if the comparison only
4127/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4128/// the result of the comparison is true when the input value is signed.
4130 bool &TrueIfSigned) {
4131 switch (Pred) {
4132 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4133 TrueIfSigned = true;
4134 return RHS.isZero();
4135 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4136 TrueIfSigned = true;
4137 return RHS.isAllOnes();
4138 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4139 TrueIfSigned = false;
4140 return RHS.isAllOnes();
4141 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4142 TrueIfSigned = false;
4143 return RHS.isZero();
4144 case ICmpInst::ICMP_UGT:
4145 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4146 TrueIfSigned = true;
4147 return RHS.isMaxSignedValue();
4148 case ICmpInst::ICMP_UGE:
4149 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4150 TrueIfSigned = true;
4151 return RHS.isMinSignedValue();
4152 case ICmpInst::ICMP_ULT:
4153 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4154 TrueIfSigned = false;
4155 return RHS.isMinSignedValue();
4156 case ICmpInst::ICMP_ULE:
4157 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4158 TrueIfSigned = false;
4159 return RHS.isMaxSignedValue();
4160 default:
4161 return false;
4162 }
4163}
4164
4165/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
4166/// same result as an fcmp with the given operands.
4167std::pair<Value *, FPClassTest> llvm::fcmpToClassTest(FCmpInst::Predicate Pred,
4168 const Function &F,
4169 Value *LHS, Value *RHS,
4170 bool LookThroughSrc) {
4171 const APFloat *ConstRHS;
4172 if (!match(RHS, m_APFloatAllowPoison(ConstRHS)))
4173 return {nullptr, fcAllFlags};
4174
4175 return fcmpToClassTest(Pred, F, LHS, ConstRHS, LookThroughSrc);
4176}
4177
4178std::pair<Value *, FPClassTest>
4180 const APFloat *ConstRHS, bool LookThroughSrc) {
4181
4182 auto [Src, ClassIfTrue, ClassIfFalse] =
4183 fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4184 if (Src && ClassIfTrue == ~ClassIfFalse)
4185 return {Src, ClassIfTrue};
4186 return {nullptr, fcAllFlags};
4187}
4188
4189/// Return the return value for fcmpImpliesClass for a compare that produces an
4190/// exact class test.
4191static std::tuple<Value *, FPClassTest, FPClassTest> exactClass(Value *V,
4192 FPClassTest M) {
4193 return {V, M, ~M};
4194}
4195
4196std::tuple<Value *, FPClassTest, FPClassTest>
4198 FPClassTest RHSClass, bool LookThroughSrc) {
4199 assert(RHSClass != fcNone);
4200 Value *Src = LHS;
4201
4202 if (Pred == FCmpInst::FCMP_TRUE)
4203 return exactClass(Src, fcAllFlags);
4204
4205 if (Pred == FCmpInst::FCMP_FALSE)
4206 return exactClass(Src, fcNone);
4207
4208 const FPClassTest OrigClass = RHSClass;
4209
4210 const bool IsNegativeRHS = (RHSClass & fcNegative) == RHSClass;
4211 const bool IsPositiveRHS = (RHSClass & fcPositive) == RHSClass;
4212 const bool IsNaN = (RHSClass & ~fcNan) == fcNone;
4213
4214 if (IsNaN) {
4215 // fcmp o__ x, nan -> false
4216 // fcmp u__ x, nan -> true
4217 return exactClass(Src, CmpInst::isOrdered(Pred) ? fcNone : fcAllFlags);
4218 }
4219
4220 // fcmp ord x, zero|normal|subnormal|inf -> ~fcNan
4221 if (Pred == FCmpInst::FCMP_ORD)
4222 return exactClass(Src, ~fcNan);
4223
4224 // fcmp uno x, zero|normal|subnormal|inf -> fcNan
4225 if (Pred == FCmpInst::FCMP_UNO)
4226 return exactClass(Src, fcNan);
4227
4228 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4229 if (IsFabs)
4230 RHSClass = llvm::inverse_fabs(RHSClass);
4231
4232 const bool IsZero = (OrigClass & fcZero) == OrigClass;
4233 if (IsZero) {
4234 assert(Pred != FCmpInst::FCMP_ORD && Pred != FCmpInst::FCMP_UNO);
4235 // Compares with fcNone are only exactly equal to fcZero if input denormals
4236 // are not flushed.
4237 // TODO: Handle DAZ by expanding masks to cover subnormal cases.
4238 if (!inputDenormalIsIEEE(F, LHS->getType()))
4239 return {nullptr, fcAllFlags, fcAllFlags};
4240
4241 switch (Pred) {
4242 case FCmpInst::FCMP_OEQ: // Match x == 0.0
4243 return exactClass(Src, fcZero);
4244 case FCmpInst::FCMP_UEQ: // Match isnan(x) || (x == 0.0)
4245 return exactClass(Src, fcZero | fcNan);
4246 case FCmpInst::FCMP_UNE: // Match (x != 0.0)
4247 return exactClass(Src, ~fcZero);
4248 case FCmpInst::FCMP_ONE: // Match !isnan(x) && x != 0.0
4249 return exactClass(Src, ~fcNan & ~fcZero);
4250 case FCmpInst::FCMP_ORD:
4251 // Canonical form of ord/uno is with a zero. We could also handle
4252 // non-canonical other non-NaN constants or LHS == RHS.
4253 return exactClass(Src, ~fcNan);
4254 case FCmpInst::FCMP_UNO:
4255 return exactClass(Src, fcNan);
4256 case FCmpInst::FCMP_OGT: // x > 0
4258 case FCmpInst::FCMP_UGT: // isnan(x) || x > 0
4260 case FCmpInst::FCMP_OGE: // x >= 0
4261 return exactClass(Src, fcPositive | fcNegZero);
4262 case FCmpInst::FCMP_UGE: // isnan(x) || x >= 0
4263 return exactClass(Src, fcPositive | fcNegZero | fcNan);
4264 case FCmpInst::FCMP_OLT: // x < 0
4266 case FCmpInst::FCMP_ULT: // isnan(x) || x < 0
4268 case FCmpInst::FCMP_OLE: // x <= 0
4269 return exactClass(Src, fcNegative | fcPosZero);
4270 case FCmpInst::FCMP_ULE: // isnan(x) || x <= 0
4271 return exactClass(Src, fcNegative | fcPosZero | fcNan);
4272 default:
4273 llvm_unreachable("all compare types are handled");
4274 }
4275
4276 return {nullptr, fcAllFlags, fcAllFlags};
4277 }
4278
4279 const bool IsDenormalRHS = (OrigClass & fcSubnormal) == OrigClass;
4280
4281 const bool IsInf = (OrigClass & fcInf) == OrigClass;
4282 if (IsInf) {
4283 FPClassTest Mask = fcAllFlags;
4284
4285 switch (Pred) {
4286 case FCmpInst::FCMP_OEQ:
4287 case FCmpInst::FCMP_UNE: {
4288 // Match __builtin_isinf patterns
4289 //
4290 // fcmp oeq x, +inf -> is_fpclass x, fcPosInf
4291 // fcmp oeq fabs(x), +inf -> is_fpclass x, fcInf
4292 // fcmp oeq x, -inf -> is_fpclass x, fcNegInf
4293 // fcmp oeq fabs(x), -inf -> is_fpclass x, 0 -> false
4294 //
4295 // fcmp une x, +inf -> is_fpclass x, ~fcPosInf
4296 // fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf
4297 // fcmp une x, -inf -> is_fpclass x, ~fcNegInf
4298 // fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true
4299 if (IsNegativeRHS) {
4300 Mask = fcNegInf;
4301 if (IsFabs)
4302 Mask = fcNone;
4303 } else {
4304 Mask = fcPosInf;
4305 if (IsFabs)
4306 Mask |= fcNegInf;
4307 }
4308 break;
4309 }
4310 case FCmpInst::FCMP_ONE:
4311 case FCmpInst::FCMP_UEQ: {
4312 // Match __builtin_isinf patterns
4313 // fcmp one x, -inf -> is_fpclass x, fcNegInf
4314 // fcmp one fabs(x), -inf -> is_fpclass x, ~fcNegInf & ~fcNan
4315 // fcmp one x, +inf -> is_fpclass x, ~fcNegInf & ~fcNan
4316 // fcmp one fabs(x), +inf -> is_fpclass x, ~fcInf & fcNan
4317 //
4318 // fcmp ueq x, +inf -> is_fpclass x, fcPosInf|fcNan
4319 // fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan
4320 // fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan
4321 // fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan
4322 if (IsNegativeRHS) {
4323 Mask = ~fcNegInf & ~fcNan;
4324 if (IsFabs)
4325 Mask = ~fcNan;
4326 } else {
4327 Mask = ~fcPosInf & ~fcNan;
4328 if (IsFabs)
4329 Mask &= ~fcNegInf;
4330 }
4331
4332 break;
4333 }
4334 case FCmpInst::FCMP_OLT:
4335 case FCmpInst::FCMP_UGE: {
4336 if (IsNegativeRHS) {
4337 // No value is ordered and less than negative infinity.
4338 // All values are unordered with or at least negative infinity.
4339 // fcmp olt x, -inf -> false
4340 // fcmp uge x, -inf -> true
4341 Mask = fcNone;
4342 break;
4343 }
4344
4345 // fcmp olt fabs(x), +inf -> fcFinite
4346 // fcmp uge fabs(x), +inf -> ~fcFinite
4347 // fcmp olt x, +inf -> fcFinite|fcNegInf
4348 // fcmp uge x, +inf -> ~(fcFinite|fcNegInf)
4349 Mask = fcFinite;
4350 if (!IsFabs)
4351 Mask |= fcNegInf;
4352 break;
4353 }
4354 case FCmpInst::FCMP_OGE:
4355 case FCmpInst::FCMP_ULT: {
4356 if (IsNegativeRHS) {
4357 // fcmp oge x, -inf -> ~fcNan
4358 // fcmp oge fabs(x), -inf -> ~fcNan
4359 // fcmp ult x, -inf -> fcNan
4360 // fcmp ult fabs(x), -inf -> fcNan
4361 Mask = ~fcNan;
4362 break;
4363 }
4364
4365 // fcmp oge fabs(x), +inf -> fcInf
4366 // fcmp oge x, +inf -> fcPosInf
4367 // fcmp ult fabs(x), +inf -> ~fcInf
4368 // fcmp ult x, +inf -> ~fcPosInf
4369 Mask = fcPosInf;
4370 if (IsFabs)
4371 Mask |= fcNegInf;
4372 break;
4373 }
4374 case FCmpInst::FCMP_OGT:
4375 case FCmpInst::FCMP_ULE: {
4376 if (IsNegativeRHS) {
4377 // fcmp ogt x, -inf -> fcmp one x, -inf
4378 // fcmp ogt fabs(x), -inf -> fcmp ord x, x
4379 // fcmp ule x, -inf -> fcmp ueq x, -inf
4380 // fcmp ule fabs(x), -inf -> fcmp uno x, x
4381 Mask = IsFabs ? ~fcNan : ~(fcNegInf | fcNan);
4382 break;
4383 }
4384
4385 // No value is ordered and greater than infinity.
4386 Mask = fcNone;
4387 break;
4388 }
4389 case FCmpInst::FCMP_OLE:
4390 case FCmpInst::FCMP_UGT: {
4391 if (IsNegativeRHS) {
4392 Mask = IsFabs ? fcNone : fcNegInf;
4393 break;
4394 }
4395
4396 // fcmp ole x, +inf -> fcmp ord x, x
4397 // fcmp ole fabs(x), +inf -> fcmp ord x, x
4398 // fcmp ole x, -inf -> fcmp oeq x, -inf
4399 // fcmp ole fabs(x), -inf -> false
4400 Mask = ~fcNan;
4401 break;
4402 }
4403 default:
4404 llvm_unreachable("all compare types are handled");
4405 }
4406
4407 // Invert the comparison for the unordered cases.
4408 if (FCmpInst::isUnordered(Pred))
4409 Mask = ~Mask;
4410
4411 return exactClass(Src, Mask);
4412 }
4413
4414 if (Pred == FCmpInst::FCMP_OEQ)
4415 return {Src, RHSClass, fcAllFlags};
4416
4417 if (Pred == FCmpInst::FCMP_UEQ) {
4418 FPClassTest Class = RHSClass | fcNan;
4419 return {Src, Class, ~fcNan};
4420 }
4421
4422 if (Pred == FCmpInst::FCMP_ONE)
4423 return {Src, ~fcNan, RHSClass | fcNan};
4424
4425 if (Pred == FCmpInst::FCMP_UNE)
4426 return {Src, fcAllFlags, RHSClass};
4427
4428 assert((RHSClass == fcNone || RHSClass == fcPosNormal ||
4429 RHSClass == fcNegNormal || RHSClass == fcNormal ||
4430 RHSClass == fcPosSubnormal || RHSClass == fcNegSubnormal ||
4431 RHSClass == fcSubnormal) &&
4432 "should have been recognized as an exact class test");
4433
4434 if (IsNegativeRHS) {
4435 // TODO: Handle fneg(fabs)
4436 if (IsFabs) {
4437 // fabs(x) o> -k -> fcmp ord x, x
4438 // fabs(x) u> -k -> true
4439 // fabs(x) o< -k -> false
4440 // fabs(x) u< -k -> fcmp uno x, x
4441 switch (Pred) {
4442 case FCmpInst::FCMP_OGT:
4443 case FCmpInst::FCMP_OGE:
4444 return {Src, ~fcNan, fcNan};
4445 case FCmpInst::FCMP_UGT:
4446 case FCmpInst::FCMP_UGE:
4447 return {Src, fcAllFlags, fcNone};
4448 case FCmpInst::FCMP_OLT:
4449 case FCmpInst::FCMP_OLE:
4450 return {Src, fcNone, fcAllFlags};
4451 case FCmpInst::FCMP_ULT:
4452 case FCmpInst::FCMP_ULE:
4453 return {Src, fcNan, ~fcNan};
4454 default:
4455 break;
4456 }
4457
4458 return {nullptr, fcAllFlags, fcAllFlags};
4459 }
4460
4461 FPClassTest ClassesLE = fcNegInf | fcNegNormal;
4463
4464 if (IsDenormalRHS)
4465 ClassesLE |= fcNegSubnormal;
4466 else
4467 ClassesGE |= fcNegNormal;
4468
4469 switch (Pred) {
4470 case FCmpInst::FCMP_OGT:
4471 case FCmpInst::FCMP_OGE:
4472 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4473 case FCmpInst::FCMP_UGT:
4474 case FCmpInst::FCMP_UGE:
4475 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4476 case FCmpInst::FCMP_OLT:
4477 case FCmpInst::FCMP_OLE:
4478 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4479 case FCmpInst::FCMP_ULT:
4480 case FCmpInst::FCMP_ULE:
4481 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4482 default:
4483 break;
4484 }
4485 } else if (IsPositiveRHS) {
4486 FPClassTest ClassesGE = fcPosNormal | fcPosInf;
4488 if (IsDenormalRHS)
4489 ClassesGE |= fcPosSubnormal;
4490 else
4491 ClassesLE |= fcPosNormal;
4492
4493 if (IsFabs) {
4494 ClassesGE = llvm::inverse_fabs(ClassesGE);
4495 ClassesLE = llvm::inverse_fabs(ClassesLE);
4496 }
4497
4498 switch (Pred) {
4499 case FCmpInst::FCMP_OGT:
4500 case FCmpInst::FCMP_OGE:
4501 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4502 case FCmpInst::FCMP_UGT:
4503 case FCmpInst::FCMP_UGE:
4504 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4505 case FCmpInst::FCMP_OLT:
4506 case FCmpInst::FCMP_OLE:
4507 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4508 case FCmpInst::FCMP_ULT:
4509 case FCmpInst::FCMP_ULE:
4510 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4511 default:
4512 break;
4513 }
4514 }
4515
4516 return {nullptr, fcAllFlags, fcAllFlags};
4517}
4518
4519std::tuple<Value *, FPClassTest, FPClassTest>
4521 const APFloat &ConstRHS, bool LookThroughSrc) {
4522 // We can refine checks against smallest normal / largest denormal to an
4523 // exact class test.
4524 if (!ConstRHS.isNegative() && ConstRHS.isSmallestNormalized()) {
4525 Value *Src = LHS;
4526 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4527
4528 FPClassTest Mask;
4529 // Match pattern that's used in __builtin_isnormal.
4530 switch (Pred) {
4531 case FCmpInst::FCMP_OLT:
4532 case FCmpInst::FCMP_UGE: {
4533 // fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero
4534 // fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero
4535 // fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf
4536 // fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero)
4537 Mask = fcZero | fcSubnormal;
4538 if (!IsFabs)
4539 Mask |= fcNegNormal | fcNegInf;
4540
4541 break;
4542 }
4543 case FCmpInst::FCMP_OGE:
4544 case FCmpInst::FCMP_ULT: {
4545 // fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf
4546 // fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal
4547 // fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf)
4548 // fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal)
4549 Mask = fcPosInf | fcPosNormal;
4550 if (IsFabs)
4551 Mask |= fcNegInf | fcNegNormal;
4552 break;
4553 }
4554 default:
4555 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(),
4556 LookThroughSrc);
4557 }
4558
4559 // Invert the comparison for the unordered cases.
4560 if (FCmpInst::isUnordered(Pred))
4561 Mask = ~Mask;
4562
4563 return exactClass(Src, Mask);
4564 }
4565
4566 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(), LookThroughSrc);
4567}
4568
4569std::tuple<Value *, FPClassTest, FPClassTest>
4571 Value *RHS, bool LookThroughSrc) {
4572 const APFloat *ConstRHS;
4573 if (!match(RHS, m_APFloatAllowPoison(ConstRHS)))
4574 return {nullptr, fcAllFlags, fcAllFlags};
4575
4576 // TODO: Just call computeKnownFPClass for RHS to handle non-constants.
4577 return fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4578}
4579
4581 bool CondIsTrue,
4582 const Instruction *CxtI,
4583 KnownFPClass &KnownFromContext) {
4584 CmpInst::Predicate Pred;
4585 Value *LHS;
4586 uint64_t ClassVal = 0;
4587 const APFloat *CRHS;
4588 const APInt *RHS;
4589 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4590 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4591 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4592 if (CmpVal == V)
4593 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4594 } else if (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(
4595 m_Value(LHS), m_ConstantInt(ClassVal)))) {
4596 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4597 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4598 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Value(LHS)),
4599 m_APInt(RHS)))) {
4600 bool TrueIfSigned;
4601 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4602 return;
4603 if (TrueIfSigned == CondIsTrue)
4604 KnownFromContext.signBitMustBeOne();
4605 else
4606 KnownFromContext.signBitMustBeZero();
4607 }
4608}
4609
4611 const SimplifyQuery &Q) {
4612 KnownFPClass KnownFromContext;
4613
4614 if (!Q.CxtI)
4615 return KnownFromContext;
4616
4617 if (Q.DC && Q.DT) {
4618 // Handle dominating conditions.
4619 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4620 Value *Cond = BI->getCondition();
4621
4622 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4623 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4624 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4625 KnownFromContext);
4626
4627 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4628 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4629 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4630 KnownFromContext);
4631 }
4632 }
4633
4634 if (!Q.AC)
4635 return KnownFromContext;
4636
4637 // Try to restrict the floating-point classes based on information from
4638 // assumptions.
4639 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4640 if (!AssumeVH)
4641 continue;
4642 CallInst *I = cast<CallInst>(AssumeVH);
4643
4644 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4645 "Got assumption for the wrong function!");
4646 assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
4647 "must be an assume intrinsic");
4648
4649 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4650 continue;
4651
4652 computeKnownFPClassFromCond(V, I->getArgOperand(0), /*CondIsTrue=*/true,
4653 Q.CxtI, KnownFromContext);
4654 }
4655
4656 return KnownFromContext;
4657}
4658
4659void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4660 FPClassTest InterestedClasses, KnownFPClass &Known,
4661 unsigned Depth, const SimplifyQuery &Q);
4662
4663static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4664 FPClassTest InterestedClasses, unsigned Depth,
4665 const SimplifyQuery &Q) {
4666 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4667 APInt DemandedElts =
4668 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4669 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Depth, Q);
4670}
4671
4673 const APInt &DemandedElts,
4674 FPClassTest InterestedClasses,
4675 KnownFPClass &Known, unsigned Depth,
4676 const SimplifyQuery &Q) {
4677 if ((InterestedClasses &
4679 return;
4680
4681 KnownFPClass KnownSrc;
4682 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4683 KnownSrc, Depth + 1, Q);
4684
4685 // Sign should be preserved
4686 // TODO: Handle cannot be ordered greater than zero
4687 if (KnownSrc.cannotBeOrderedLessThanZero())
4689
4690 Known.propagateNaN(KnownSrc, true);
4691
4692 // Infinity needs a range check.
4693}
4694
4695void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4696 FPClassTest InterestedClasses, KnownFPClass &Known,
4697 unsigned Depth, const SimplifyQuery &Q) {
4698 assert(Known.isUnknown() && "should not be called with known information");
4699
4700 if (!DemandedElts) {
4701 // No demanded elts, better to assume we don't know anything.
4702 Known.resetAll();
4703 return;
4704 }
4705
4706 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4707
4708 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4709 Known.KnownFPClasses = CFP->getValueAPF().classify();
4710 Known.SignBit = CFP->isNegative();
4711 return;
4712 }
4713
4714 if (isa<ConstantAggregateZero>(V)) {
4715 Known.KnownFPClasses = fcPosZero;
4716 Known.SignBit = false;
4717 return;
4718 }
4719
4720 if (isa<PoisonValue>(V)) {
4721 Known.KnownFPClasses = fcNone;
4722 Known.SignBit = false;
4723 return;
4724 }
4725
4726 // Try to handle fixed width vector constants
4727 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
4728 const Constant *CV = dyn_cast<Constant>(V);
4729 if (VFVTy && CV) {
4730 Known.KnownFPClasses = fcNone;
4731 bool SignBitAllZero = true;
4732 bool SignBitAllOne = true;
4733
4734 // For vectors, verify that each element is not NaN.
4735 unsigned NumElts = VFVTy->getNumElements();
4736 for (unsigned i = 0; i != NumElts; ++i) {
4737 if (!DemandedElts[i])
4738 continue;
4739
4740 Constant *Elt = CV->getAggregateElement(i);
4741 if (!Elt) {
4742 Known = KnownFPClass();
4743 return;
4744 }
4745 if (isa<UndefValue>(Elt))
4746 continue;
4747 auto *CElt = dyn_cast<ConstantFP>(Elt);
4748 if (!CElt) {
4749 Known = KnownFPClass();
4750 return;
4751 }
4752
4753 const APFloat &C = CElt->getValueAPF();
4754 Known.KnownFPClasses |= C.classify();
4755 if (C.isNegative())
4756 SignBitAllZero = false;
4757 else
4758 SignBitAllOne = false;
4759 }
4760 if (SignBitAllOne != SignBitAllZero)
4761 Known.SignBit = SignBitAllOne;
4762 return;
4763 }
4764
4765 FPClassTest KnownNotFromFlags = fcNone;
4766 if (const auto *CB = dyn_cast<CallBase>(V))
4767 KnownNotFromFlags |= CB->getRetNoFPClass();
4768 else if (const auto *Arg = dyn_cast<Argument>(V))
4769 KnownNotFromFlags |= Arg->getNoFPClass();
4770
4771 const Operator *Op = dyn_cast<Operator>(V);
4772 if (const FPMathOperator *FPOp = dyn_cast_or_null<FPMathOperator>(Op)) {
4773 if (FPOp->hasNoNaNs())
4774 KnownNotFromFlags |= fcNan;
4775 if (FPOp->hasNoInfs())
4776 KnownNotFromFlags |= fcInf;
4777 }
4778
4779 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
4780 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
4781
4782 // We no longer need to find out about these bits from inputs if we can
4783 // assume this from flags/attributes.
4784 InterestedClasses &= ~KnownNotFromFlags;
4785
4786 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
4787 Known.knownNot(KnownNotFromFlags);
4788 if (!Known.SignBit && AssumedClasses.SignBit) {
4789 if (*AssumedClasses.SignBit)
4790 Known.signBitMustBeOne();
4791 else
4792 Known.signBitMustBeZero();
4793 }
4794 });
4795
4796 if (!Op)
4797 return;
4798
4799 // All recursive calls that increase depth must come after this.
4801 return;
4802
4803 const unsigned Opc = Op->getOpcode();
4804 switch (Opc) {
4805 case Instruction::FNeg: {
4806 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4807 Known, Depth + 1, Q);
4808 Known.fneg();
4809 break;
4810 }
4811 case Instruction::Select: {
4812 Value *Cond = Op->getOperand(0);
4813 Value *LHS = Op->getOperand(1);
4814 Value *RHS = Op->getOperand(2);
4815
4816 FPClassTest FilterLHS = fcAllFlags;
4817 FPClassTest FilterRHS = fcAllFlags;
4818
4819 Value *TestedValue = nullptr;
4820 FPClassTest MaskIfTrue = fcAllFlags;
4821 FPClassTest MaskIfFalse = fcAllFlags;
4822 uint64_t ClassVal = 0;
4823 const Function *F = cast<Instruction>(Op)->getFunction();
4824 CmpInst::Predicate Pred;
4825 Value *CmpLHS, *CmpRHS;
4826 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
4827 // If the select filters out a value based on the class, it no longer
4828 // participates in the class of the result
4829
4830 // TODO: In some degenerate cases we can infer something if we try again
4831 // without looking through sign operations.
4832 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
4833 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
4834 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
4835 } else if (match(Cond,
4836 m_Intrinsic<Intrinsic::is_fpclass>(
4837 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
4838 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
4839 MaskIfTrue = TestedMask;
4840 MaskIfFalse = ~TestedMask;
4841 }
4842
4843 if (TestedValue == LHS) {
4844 // match !isnan(x) ? x : y
4845 FilterLHS = MaskIfTrue;
4846 } else if (TestedValue == RHS) { // && IsExactClass
4847 // match !isnan(x) ? y : x
4848 FilterRHS = MaskIfFalse;
4849 }
4850
4851 KnownFPClass Known2;
4852 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
4853 Depth + 1, Q);
4854 Known.KnownFPClasses &= FilterLHS;
4855
4856 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
4857 Known2, Depth + 1, Q);
4858 Known2.KnownFPClasses &= FilterRHS;
4859
4860 Known |= Known2;
4861 break;
4862 }
4863 case Instruction::Call: {
4864 const CallInst *II = cast<CallInst>(Op);
4865 const Intrinsic::ID IID = II->getIntrinsicID();
4866 switch (IID) {
4867 case Intrinsic::fabs: {
4868 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
4869 // If we only care about the sign bit we don't need to inspect the
4870 // operand.
4871 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
4872 InterestedClasses, Known, Depth + 1, Q);
4873 }
4874
4875 Known.fabs();
4876 break;
4877 }
4878 case Intrinsic::copysign: {
4879 KnownFPClass KnownSign;
4880
4881 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4882 Known, Depth + 1, Q);
4883 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4884 KnownSign, Depth + 1, Q);
4885 Known.copysign(KnownSign);
4886 break;
4887 }
4888 case Intrinsic::fma:
4889 case Intrinsic::fmuladd: {
4890 if ((InterestedClasses & fcNegative) == fcNone)
4891 break;
4892
4893 if (II->getArgOperand(0) != II->getArgOperand(1))
4894 break;
4895
4896 // The multiply cannot be -0 and therefore the add can't be -0
4897 Known.knownNot(fcNegZero);
4898
4899 // x * x + y is non-negative if y is non-negative.
4900 KnownFPClass KnownAddend;
4901 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
4902 KnownAddend, Depth + 1, Q);
4903
4904 if (KnownAddend.cannotBeOrderedLessThanZero())
4905 Known.knownNot(fcNegative);
4906 break;
4907 }
4908 case Intrinsic::sqrt:
4909 case Intrinsic::experimental_constrained_sqrt: {
4910 KnownFPClass KnownSrc;
4911 FPClassTest InterestedSrcs = InterestedClasses;
4912 if (InterestedClasses & fcNan)
4913 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
4914
4915 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
4916 KnownSrc, Depth + 1, Q);
4917
4918 if (KnownSrc.isKnownNeverPosInfinity())
4919 Known.knownNot(fcPosInf);
4920 if (KnownSrc.isKnownNever(fcSNan))
4921 Known.knownNot(fcSNan);
4922
4923 // Any negative value besides -0 returns a nan.
4924 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
4925 Known.knownNot(fcNan);
4926
4927 // The only negative value that can be returned is -0 for -0 inputs.
4929
4930 // If the input denormal mode could be PreserveSign, a negative
4931 // subnormal input could produce a negative zero output.
4932 const Function *F = II->getFunction();
4933 if (Q.IIQ.hasNoSignedZeros(II) ||
4934 (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))) {
4935 Known.knownNot(fcNegZero);
4936 if (KnownSrc.isKnownNeverNaN())
4937 Known.signBitMustBeZero();
4938 }
4939
4940 break;
4941 }
4942 case Intrinsic::sin:
4943 case Intrinsic::cos: {
4944 // Return NaN on infinite inputs.
4945 KnownFPClass KnownSrc;
4946 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4947 KnownSrc, Depth + 1, Q);
4948 Known.knownNot(fcInf);
4949 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
4950 Known.knownNot(fcNan);
4951 break;
4952 }
4953 case Intrinsic::maxnum:
4954 case Intrinsic::minnum:
4955 case Intrinsic::minimum:
4956 case Intrinsic::maximum: {
4957 KnownFPClass KnownLHS, KnownRHS;
4958 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4959 KnownLHS, Depth + 1, Q);
4960 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4961 KnownRHS, Depth + 1, Q);
4962
4963 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
4964 Known = KnownLHS | KnownRHS;
4965
4966 // If either operand is not NaN, the result is not NaN.
4967 if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum))
4968 Known.knownNot(fcNan);
4969
4970 if (IID == Intrinsic::maxnum) {
4971 // If at least one operand is known to be positive, the result must be
4972 // positive.
4973 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
4974 KnownLHS.isKnownNeverNaN()) ||
4975 (KnownRHS.cannotBeOrderedLessThanZero() &&
4976 KnownRHS.isKnownNeverNaN()))
4978 } else if (IID == Intrinsic::maximum) {
4979 // If at least one operand is known to be positive, the result must be
4980 // positive.
4981 if (KnownLHS.cannotBeOrderedLessThanZero() ||
4982 KnownRHS.cannotBeOrderedLessThanZero())
4984 } else if (IID == Intrinsic::minnum) {
4985 // If at least one operand is known to be negative, the result must be
4986 // negative.
4987 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
4988 KnownLHS.isKnownNeverNaN()) ||
4989 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
4990 KnownRHS.isKnownNeverNaN()))
4992 } else {
4993 // If at least one operand is known to be negative, the result must be
4994 // negative.
4995 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
4998 }
4999
5000 // Fixup zero handling if denormals could be returned as a zero.
5001 //
5002 // As there's no spec for denormal flushing, be conservative with the
5003 // treatment of denormals that could be flushed to zero. For older
5004 // subtargets on AMDGPU the min/max instructions would not flush the
5005 // output and return the original value.
5006 //
5007 if ((Known.KnownFPClasses & fcZero) != fcNone &&
5008 !Known.isKnownNeverSubnormal()) {
5009 const Function *Parent = II->getFunction();
5010 if (!Parent)
5011 break;
5012
5013 DenormalMode Mode = Parent->getDenormalMode(
5015 if (Mode != DenormalMode::getIEEE())
5016 Known.KnownFPClasses |= fcZero;
5017 }
5018
5019 if (Known.isKnownNeverNaN()) {
5020 if (KnownLHS.SignBit && KnownRHS.SignBit &&
5021 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5022 if (*KnownLHS.SignBit)
5023 Known.signBitMustBeOne();
5024 else
5025 Known.signBitMustBeZero();
5026 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum) ||
5027 ((KnownLHS.isKnownNeverNegZero() ||
5028 KnownRHS.isKnownNeverPosZero()) &&
5029 (KnownLHS.isKnownNeverPosZero() ||
5030 KnownRHS.isKnownNeverNegZero()))) {
5031 if ((IID == Intrinsic::maximum || IID == Intrinsic::maxnum) &&
5032 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5033 Known.signBitMustBeZero();
5034 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minnum) &&
5035 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5036 Known.signBitMustBeOne();
5037 }
5038 }
5039 break;
5040 }
5041 case Intrinsic::canonicalize: {
5042 KnownFPClass KnownSrc;
5043 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5044 KnownSrc, Depth + 1, Q);
5045
5046 // This is essentially a stronger form of
5047 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5048 // actually have an IR canonicalization guarantee.
5049
5050 // Canonicalize may flush denormals to zero, so we have to consider the
5051 // denormal mode to preserve known-not-0 knowledge.
5052 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5053
5054 // Stronger version of propagateNaN
5055 // Canonicalize is guaranteed to quiet signaling nans.
5056 if (KnownSrc.isKnownNeverNaN())
5057 Known.knownNot(fcNan);
5058 else
5059 Known.knownNot(fcSNan);
5060
5061 const Function *F = II->getFunction();
5062 if (!F)
5063 break;
5064
5065 // If the parent function flushes denormals, the canonical output cannot
5066 // be a denormal.
5067 const fltSemantics &FPType =
5069 DenormalMode DenormMode = F->getDenormalMode(FPType);
5070 if (DenormMode == DenormalMode::getIEEE()) {
5071 if (KnownSrc.isKnownNever(fcPosZero))
5072 Known.knownNot(fcPosZero);
5073 if (KnownSrc.isKnownNever(fcNegZero))
5074 Known.knownNot(fcNegZero);
5075 break;
5076 }
5077
5078 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5079 Known.knownNot(fcSubnormal);
5080
5081 if (DenormMode.Input == DenormalMode::PositiveZero ||
5082 (DenormMode.Output == DenormalMode::PositiveZero &&
5083 DenormMode.Input == DenormalMode::IEEE))
5084 Known.knownNot(fcNegZero);
5085
5086 break;
5087 }
5088 case Intrinsic::vector_reduce_fmax:
5089 case Intrinsic::vector_reduce_fmin:
5090 case Intrinsic::vector_reduce_fmaximum:
5091 case Intrinsic::vector_reduce_fminimum: {
5092 // reduce min/max will choose an element from one of the vector elements,
5093 // so we can infer and class information that is common to all elements.
5095 InterestedClasses, Depth + 1, Q);
5096 // Can only propagate sign if output is never NaN.
5097 if (!Known.isKnownNeverNaN())
5098 Known.SignBit.reset();
5099 break;
5100 }
5101 case Intrinsic::trunc:
5102 case Intrinsic::floor:
5103 case Intrinsic::ceil:
5104 case Intrinsic::rint:
5105 case Intrinsic::nearbyint:
5106 case Intrinsic::round:
5107 case Intrinsic::roundeven: {
5108 KnownFPClass KnownSrc;
5109 FPClassTest InterestedSrcs = InterestedClasses;
5110 if (InterestedSrcs & fcPosFinite)
5111 InterestedSrcs |= fcPosFinite;
5112 if (InterestedSrcs & fcNegFinite)
5113 InterestedSrcs |= fcNegFinite;
5114 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5115 KnownSrc, Depth + 1, Q);
5116
5117 // Integer results cannot be subnormal.
5118 Known.knownNot(fcSubnormal);
5119
5120 Known.propagateNaN(KnownSrc, true);
5121
5122 // Pass through infinities, except PPC_FP128 is a special case for
5123 // intrinsics other than trunc.
5124 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5125 if (KnownSrc.isKnownNeverPosInfinity())
5126 Known.knownNot(fcPosInf);
5127 if (KnownSrc.isKnownNeverNegInfinity())
5128 Known.knownNot(fcNegInf);
5129 }
5130
5131 // Negative round ups to 0 produce -0
5132 if (KnownSrc.isKnownNever(fcPosFinite))
5133 Known.knownNot(fcPosFinite);
5134 if (KnownSrc.isKnownNever(fcNegFinite))
5135 Known.knownNot(fcNegFinite);
5136
5137 break;
5138 }
5139 case Intrinsic::exp:
5140 case Intrinsic::exp2:
5141 case Intrinsic::exp10: {
5142 Known.knownNot(fcNegative);
5143 if ((InterestedClasses & fcNan) == fcNone)
5144 break;
5145
5146 KnownFPClass KnownSrc;
5147 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5148 KnownSrc, Depth + 1, Q);
5149 if (KnownSrc.isKnownNeverNaN()) {
5150 Known.knownNot(fcNan);
5151 Known.signBitMustBeZero();
5152 }
5153
5154 break;
5155 }
5156 case Intrinsic::fptrunc_round: {
5157 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5158 Depth, Q);
5159 break;
5160 }
5161 case Intrinsic::log:
5162 case Intrinsic::log10:
5163 case Intrinsic::log2:
5164 case Intrinsic::experimental_constrained_log:
5165 case Intrinsic::experimental_constrained_log10:
5166 case Intrinsic::experimental_constrained_log2: {
5167 // log(+inf) -> +inf
5168 // log([+-]0.0) -> -inf
5169 // log(-inf) -> nan
5170 // log(-x) -> nan
5171 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5172 break;
5173
5174 FPClassTest InterestedSrcs = InterestedClasses;
5175 if ((InterestedClasses & fcNegInf) != fcNone)
5176 InterestedSrcs |= fcZero | fcSubnormal;
5177 if ((InterestedClasses & fcNan) != fcNone)
5178 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5179
5180 KnownFPClass KnownSrc;
5181 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5182 KnownSrc, Depth + 1, Q);
5183
5184 if (KnownSrc.isKnownNeverPosInfinity())
5185 Known.knownNot(fcPosInf);
5186
5187 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5188 Known.knownNot(fcNan);
5189
5190 const Function *F = II->getFunction();
5191 if (F && KnownSrc.isKnownNeverLogicalZero(*F, II->getType()))
5192 Known.knownNot(fcNegInf);
5193
5194 break;
5195 }
5196 case Intrinsic::powi: {
5197 if ((InterestedClasses & fcNegative) == fcNone)
5198 break;
5199
5200 const Value *Exp = II->getArgOperand(1);
5201 Type *ExpTy = Exp->getType();
5202 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5203 KnownBits ExponentKnownBits(BitWidth);
5204 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5205 ExponentKnownBits, Depth + 1, Q);
5206
5207 if (ExponentKnownBits.Zero[0]) { // Is even
5208 Known.knownNot(fcNegative);
5209 break;
5210 }
5211
5212 // Given that exp is an integer, here are the
5213 // ways that pow can return a negative value:
5214 //
5215 // pow(-x, exp) --> negative if exp is odd and x is negative.
5216 // pow(-0, exp) --> -inf if exp is negative odd.
5217 // pow(-0, exp) --> -0 if exp is positive odd.
5218 // pow(-inf, exp) --> -0 if exp is negative odd.
5219 // pow(-inf, exp) --> -inf if exp is positive odd.
5220 KnownFPClass KnownSrc;
5221 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5222 KnownSrc, Depth + 1, Q);
5223 if (KnownSrc.isKnownNever(fcNegative))
5224 Known.knownNot(fcNegative);
5225 break;
5226 }
5227 case Intrinsic::ldexp: {
5228 KnownFPClass KnownSrc;
5229 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5230 KnownSrc, Depth + 1, Q);
5231 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5232
5233 // Sign is preserved, but underflows may produce zeroes.
5234 if (KnownSrc.isKnownNever(fcNegative))
5235 Known.knownNot(fcNegative);
5236 else if (KnownSrc.cannotBeOrderedLessThanZero())
5238
5239 if (KnownSrc.isKnownNever(fcPositive))
5240 Known.knownNot(fcPositive);
5241 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5243
5244 // Can refine inf/zero handling based on the exponent operand.
5245 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5246 if ((InterestedClasses & ExpInfoMask) == fcNone)
5247 break;
5248 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5249 break;
5250
5251 const fltSemantics &Flt =
5253 unsigned Precision = APFloat::semanticsPrecision(Flt);
5254 const Value *ExpArg = II->getArgOperand(1);
5256 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5257
5258 const int MantissaBits = Precision - 1;
5259 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5260 Known.knownNot(fcSubnormal);
5261
5262 const Function *F = II->getFunction();
5263 const APInt *ConstVal = ExpRange.getSingleElement();
5264 if (ConstVal && ConstVal->isZero()) {
5265 // ldexp(x, 0) -> x, so propagate everything.
5266 Known.propagateCanonicalizingSrc(KnownSrc, *F, II->getType());
5267 } else if (ExpRange.isAllNegative()) {
5268 // If we know the power is <= 0, can't introduce inf
5269 if (KnownSrc.isKnownNeverPosInfinity())
5270 Known.knownNot(fcPosInf);
5271 if (KnownSrc.isKnownNeverNegInfinity())
5272 Known.knownNot(fcNegInf);
5273 } else if (ExpRange.isAllNonNegative()) {
5274 // If we know the power is >= 0, can't introduce subnormal or zero
5275 if (KnownSrc.isKnownNeverPosSubnormal())
5276 Known.knownNot(fcPosSubnormal);
5277 if (KnownSrc.isKnownNeverNegSubnormal())
5278 Known.knownNot(fcNegSubnormal);
5279 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, II->getType()))
5280 Known.knownNot(fcPosZero);
5281 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))
5282 Known.knownNot(fcNegZero);
5283 }
5284
5285 break;
5286 }
5287 case Intrinsic::arithmetic_fence: {
5288 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5289 Known, Depth + 1, Q);
5290 break;
5291 }
5292 case Intrinsic::experimental_constrained_sitofp:
5293 case Intrinsic::experimental_constrained_uitofp:
5294 // Cannot produce nan
5295 Known.knownNot(fcNan);
5296
5297 // sitofp and uitofp turn into +0.0 for zero.
5298 Known.knownNot(fcNegZero);
5299
5300 // Integers cannot be subnormal
5301 Known.knownNot(fcSubnormal);
5302
5303 if (IID == Intrinsic::experimental_constrained_uitofp)
5304 Known.signBitMustBeZero();
5305
5306 // TODO: Copy inf handling from instructions
5307 break;
5308 default:
5309 break;
5310 }
5311
5312 break;
5313 }
5314 case Instruction::FAdd:
5315 case Instruction::FSub: {
5316 KnownFPClass KnownLHS, KnownRHS;
5317 bool WantNegative =
5318 Op->getOpcode() == Instruction::FAdd &&
5319 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5320 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5321 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5322
5323 if (!WantNaN && !WantNegative && !WantNegZero)
5324 break;
5325
5326 FPClassTest InterestedSrcs = InterestedClasses;
5327 if (WantNegative)
5328 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5329 if (InterestedClasses & fcNan)
5330 InterestedSrcs |= fcInf;
5331 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5332 KnownRHS, Depth + 1, Q);
5333
5334 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5335 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5336 WantNegZero || Opc == Instruction::FSub) {
5337
5338 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5339 // there's no point.
5340 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5341 KnownLHS, Depth + 1, Q);
5342 // Adding positive and negative infinity produces NaN.
5343 // TODO: Check sign of infinities.
5344 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5345 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5346 Known.knownNot(fcNan);
5347
5348 // FIXME: Context function should always be passed in separately
5349 const Function *F = cast<Instruction>(Op)->getFunction();
5350
5351 if (Op->getOpcode() == Instruction::FAdd) {
5352 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5353 KnownRHS.cannotBeOrderedLessThanZero())
5355 if (!F)
5356 break;
5357
5358 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5359 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5360 KnownRHS.isKnownNeverLogicalNegZero(*F, Op->getType())) &&
5361 // Make sure output negative denormal can't flush to -0
5362 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5363 Known.knownNot(fcNegZero);
5364 } else {
5365 if (!F)
5366 break;
5367
5368 // Only fsub -0, +0 can return -0
5369 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5370 KnownRHS.isKnownNeverLogicalPosZero(*F, Op->getType())) &&
5371 // Make sure output negative denormal can't flush to -0
5372 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5373 Known.knownNot(fcNegZero);
5374 }
5375 }
5376
5377 break;
5378 }
5379 case Instruction::FMul: {
5380 // X * X is always non-negative or a NaN.
5381 if (Op->getOperand(0) == Op->getOperand(1))
5382 Known.knownNot(fcNegative);
5383
5384 if ((InterestedClasses & fcNan) != fcNan)
5385 break;
5386
5387 // fcSubnormal is only needed in case of DAZ.
5388 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5389
5390 KnownFPClass KnownLHS, KnownRHS;
5391 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5392 Depth + 1, Q);
5393 if (!KnownRHS.isKnownNeverNaN())
5394 break;
5395
5396 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5397 Depth + 1, Q);
5398 if (!KnownLHS.isKnownNeverNaN())
5399 break;
5400
5401 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5402 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5403 Known.signBitMustBeZero();
5404 else
5405 Known.signBitMustBeOne();
5406 }
5407
5408 // If 0 * +/-inf produces NaN.
5409 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5410 Known.knownNot(fcNan);
5411 break;
5412 }
5413
5414 const Function *F = cast<Instruction>(Op)->getFunction();
5415 if (!F)
5416 break;
5417
5418 if ((KnownRHS.isKnownNeverInfinity() ||
5419 KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) &&
5420 (KnownLHS.isKnownNeverInfinity() ||
5421 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))
5422 Known.knownNot(fcNan);
5423
5424 break;
5425 }
5426 case Instruction::FDiv:
5427 case Instruction::FRem: {
5428 if (Op->getOperand(0) == Op->getOperand(1)) {
5429 // TODO: Could filter out snan if we inspect the operand
5430 if (Op->getOpcode() == Instruction::FDiv) {
5431 // X / X is always exactly 1.0 or a NaN.
5433 } else {
5434 // X % X is always exactly [+-]0.0 or a NaN.
5435 Known.KnownFPClasses = fcNan | fcZero;
5436 }
5437
5438 break;
5439 }
5440
5441 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5442 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5443 const bool WantPositive =
5444 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5445 if (!WantNan && !WantNegative && !WantPositive)
5446 break;
5447
5448 KnownFPClass KnownLHS, KnownRHS;
5449
5450 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5451 fcNan | fcInf | fcZero | fcNegative, KnownRHS,
5452 Depth + 1, Q);
5453
5454 bool KnowSomethingUseful =
5455 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5456
5457 if (KnowSomethingUseful || WantPositive) {
5458 const FPClassTest InterestedLHS =
5459 WantPositive ? fcAllFlags
5461
5462 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5463 InterestedClasses & InterestedLHS, KnownLHS,
5464 Depth + 1, Q);
5465 }
5466
5467 const Function *F = cast<Instruction>(Op)->getFunction();
5468
5469 if (Op->getOpcode() == Instruction::FDiv) {
5470 // Only 0/0, Inf/Inf produce NaN.
5471 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5472 (KnownLHS.isKnownNeverInfinity() ||
5473 KnownRHS.isKnownNeverInfinity()) &&
5474 ((F && KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) ||
5475 (F && KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))) {
5476 Known.knownNot(fcNan);
5477 }
5478
5479 // X / -0.0 is -Inf (or NaN).
5480 // +X / +X is +X
5481 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5482 Known.knownNot(fcNegative);
5483 } else {
5484 // Inf REM x and x REM 0 produce NaN.
5485 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5486 KnownLHS.isKnownNeverInfinity() && F &&
5487 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())) {
5488 Known.knownNot(fcNan);
5489 }
5490
5491 // The sign for frem is the same as the first operand.
5492 if (KnownLHS.cannotBeOrderedLessThanZero())
5494 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5496
5497 // See if we can be more aggressive about the sign of 0.
5498 if (KnownLHS.isKnownNever(fcNegative))
5499 Known.knownNot(fcNegative);
5500 if (KnownLHS.isKnownNever(fcPositive))
5501 Known.knownNot(fcPositive);
5502 }
5503
5504 break;
5505 }
5506 case Instruction::FPExt: {
5507 // Infinity, nan and zero propagate from source.
5508 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5509 Known, Depth + 1, Q);
5510
5511 const fltSemantics &DstTy =
5512 Op->getType()->getScalarType()->getFltSemantics();
5513 const fltSemantics &SrcTy =
5514 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5515
5516 // All subnormal inputs should be in the normal range in the result type.
5517 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5518 if (Known.KnownFPClasses & fcPosSubnormal)
5519 Known.KnownFPClasses |= fcPosNormal;
5520 if (Known.KnownFPClasses & fcNegSubnormal)
5521 Known.KnownFPClasses |= fcNegNormal;
5522 Known.knownNot(fcSubnormal);
5523 }
5524
5525 // Sign bit of a nan isn't guaranteed.
5526 if (!Known.isKnownNeverNaN())
5527 Known.SignBit = std::nullopt;
5528 break;
5529 }
5530 case Instruction::FPTrunc: {
5531 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5532 Depth, Q);
5533 break;
5534 }
5535 case Instruction::SIToFP:
5536 case Instruction::UIToFP: {
5537 // Cannot produce nan
5538 Known.knownNot(fcNan);
5539
5540 // Integers cannot be subnormal
5541 Known.knownNot(fcSubnormal);
5542
5543 // sitofp and uitofp turn into +0.0 for zero.
5544 Known.knownNot(fcNegZero);
5545 if (Op->getOpcode() == Instruction::UIToFP)
5546 Known.signBitMustBeZero();
5547
5548 if (InterestedClasses & fcInf) {
5549 // Get width of largest magnitude integer (remove a bit if signed).
5550 // This still works for a signed minimum value because the largest FP
5551 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5552 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5553 if (Op->getOpcode() == Instruction::SIToFP)
5554 --IntSize;
5555
5556 // If the exponent of the largest finite FP value can hold the largest
5557 // integer, the result of the cast must be finite.
5558 Type *FPTy = Op->getType()->getScalarType();
5559 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5560 Known.knownNot(fcInf);
5561 }
5562
5563 break;
5564 }
5565 case Instruction::ExtractElement: {
5566 // Look through extract element. If the index is non-constant or
5567 // out-of-range demand all elements, otherwise just the extracted element.
5568 const Value *Vec = Op->getOperand(0);
5569 const Value *Idx = Op->getOperand(1);
5570 auto *CIdx = dyn_cast<ConstantInt>(Idx);
5571
5572 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5573 unsigned NumElts = VecTy->getNumElements();
5574 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
5575 if (CIdx && CIdx->getValue().ult(NumElts))
5576 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5577 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5578 Depth + 1, Q);
5579 }
5580
5581 break;
5582 }
5583 case Instruction::InsertElement: {
5584 if (isa<ScalableVectorType>(Op->getType()))
5585 return;
5586
5587 const Value *Vec = Op->getOperand(0);
5588 const Value *Elt = Op->getOperand(1);
5589 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5590 unsigned NumElts = DemandedElts.getBitWidth();
5591 APInt DemandedVecElts = DemandedElts;
5592 bool NeedsElt = true;
5593 // If we know the index we are inserting to, clear it from Vec check.
5594 if (CIdx && CIdx->getValue().ult(NumElts)) {
5595 DemandedVecElts.clearBit(CIdx->getZExtValue());
5596 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5597 }
5598
5599 // Do we demand the inserted element?
5600 if (NeedsElt) {
5601 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1, Q);
5602 // If we don't know any bits, early out.
5603 if (Known.isUnknown())
5604 break;
5605 } else {
5606 Known.KnownFPClasses = fcNone;
5607 }
5608
5609 // Do we need anymore elements from Vec?
5610 if (!DemandedVecElts.isZero()) {
5611 KnownFPClass Known2;
5612 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
5613 Depth + 1, Q);
5614 Known |= Known2;
5615 }
5616
5617 break;
5618 }
5619 case Instruction::ShuffleVector: {
5620 // For undef elements, we don't know anything about the common state of
5621 // the shuffle result.
5622 APInt DemandedLHS, DemandedRHS;
5623 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5624 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5625 return;
5626
5627 if (!!DemandedLHS) {
5628 const Value *LHS = Shuf->getOperand(0);
5629 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
5630 Depth + 1, Q);
5631
5632 // If we don't know any bits, early out.
5633 if (Known.isUnknown())
5634 break;
5635 } else {
5636 Known.KnownFPClasses = fcNone;
5637 }
5638
5639 if (!!DemandedRHS) {
5640 KnownFPClass Known2;
5641 const Value *RHS = Shuf->getOperand(1);
5642 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
5643 Depth + 1, Q);
5644 Known |= Known2;
5645 }
5646
5647 break;
5648 }
5649 case Instruction::ExtractValue: {
5650 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5651 ArrayRef<unsigned> Indices = Extract->getIndices();
5652 const Value *Src = Extract->getAggregateOperand();
5653 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5654 Indices[0] == 0) {
5655 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5656 switch (II->getIntrinsicID()) {
5657 case Intrinsic::frexp: {
5658 Known.knownNot(fcSubnormal);
5659
5660 KnownFPClass KnownSrc;
5661 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5662 InterestedClasses, KnownSrc, Depth + 1, Q);
5663
5664 const Function *F = cast<Instruction>(Op)->getFunction();
5665
5666 if (KnownSrc.isKnownNever(fcNegative))
5667 Known.knownNot(fcNegative);
5668 else {
5669 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, Op->getType()))
5670 Known.knownNot(fcNegZero);
5671 if (KnownSrc.isKnownNever(fcNegInf))
5672 Known.knownNot(fcNegInf);
5673 }
5674
5675 if (KnownSrc.isKnownNever(fcPositive))
5676 Known.knownNot(fcPositive);
5677 else {
5678 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, Op->getType()))
5679 Known.knownNot(fcPosZero);
5680 if (KnownSrc.isKnownNever(fcPosInf))
5681 Known.knownNot(fcPosInf);
5682 }
5683
5684 Known.propagateNaN(KnownSrc);
5685 return;
5686 }
5687 default:
5688 break;
5689 }
5690 }
5691 }
5692
5693 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1,
5694 Q);
5695 break;
5696 }
5697 case Instruction::PHI: {
5698 const PHINode *P = cast<PHINode>(Op);
5699 // Unreachable blocks may have zero-operand PHI nodes.
5700 if (P->getNumIncomingValues() == 0)
5701 break;
5702
5703 // Otherwise take the unions of the known bit sets of the operands,
5704 // taking conservative care to avoid excessive recursion.
5705 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5706
5707 if (Depth < PhiRecursionLimit) {
5708 // Skip if every incoming value references to ourself.
5709 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5710 break;
5711
5712 bool First = true;
5713
5714 for (const Use &U : P->operands()) {
5715 Value *IncValue = U.get();
5716 // Skip direct self references.
5717 if (IncValue == P)
5718 continue;
5719
5720 KnownFPClass KnownSrc;
5721 // Recurse, but cap the recursion to two levels, because we don't want
5722 // to waste time spinning around in loops. We need at least depth 2 to
5723 // detect known sign bits.
5725 IncValue, DemandedElts, InterestedClasses, KnownSrc,
5726 PhiRecursionLimit,
5727 Q.getWithInstruction(P->getIncomingBlock(U)->getTerminator()));
5728
5729 if (First) {
5730 Known = KnownSrc;
5731 First = false;
5732 } else {
5733 Known |= KnownSrc;
5734 }
5735
5736 if (Known.KnownFPClasses == fcAllFlags)
5737 break;
5738 }
5739 }
5740
5741 break;
5742 }
5743 default:
5744 break;
5745 }
5746}
5747
5749 const APInt &DemandedElts,
5750 FPClassTest InterestedClasses,
5751 unsigned Depth,
5752 const SimplifyQuery &SQ) {
5753 KnownFPClass KnownClasses;
5754 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, Depth,
5755 SQ);
5756 return KnownClasses;
5757}
5758
5760 FPClassTest InterestedClasses,
5761 unsigned Depth,
5762 const SimplifyQuery &SQ) {
5763 KnownFPClass Known;
5764 ::computeKnownFPClass(V, Known, InterestedClasses, Depth, SQ);
5765 return Known;
5766}
5767
5769
5770 // All byte-wide stores are splatable, even of arbitrary variables.
5771 if (V->getType()->isIntegerTy(8))
5772 return V;
5773
5774 LLVMContext &Ctx = V->getContext();
5775
5776 // Undef don't care.
5777 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
5778 if (isa<UndefValue>(V))
5779 return UndefInt8;
5780
5781 // Return Undef for zero-sized type.
5782 if (DL.getTypeStoreSize(V->getType()).isZero())
5783 return UndefInt8;
5784
5785 Constant *C = dyn_cast<Constant>(V);
5786 if (!C) {
5787 // Conceptually, we could handle things like:
5788 // %a = zext i8 %X to i16
5789 // %b = shl i16 %a, 8
5790 // %c = or i16 %a, %b
5791 // but until there is an example that actually needs this, it doesn't seem
5792 // worth worrying about.
5793 return nullptr;
5794 }
5795
5796 // Handle 'null' ConstantArrayZero etc.
5797 if (C->isNullValue())
5799
5800 // Constant floating-point values can be handled as integer values if the
5801 // corresponding integer value is "byteable". An important case is 0.0.
5802 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
5803 Type *Ty = nullptr;
5804 if (CFP->getType()->isHalfTy())
5805 Ty = Type::getInt16Ty(Ctx);
5806 else if (CFP->getType()->isFloatTy())
5807 Ty = Type::getInt32Ty(Ctx);
5808 else if (CFP->getType()->isDoubleTy())
5809 Ty = Type::getInt64Ty(Ctx);
5810 // Don't handle long double formats, which have strange constraints.
5811 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
5812 : nullptr;
5813 }
5814
5815 // We can handle constant integers that are multiple of 8 bits.
5816 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
5817 if (CI->getBitWidth() % 8 == 0) {
5818 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
5819 if (!CI->getValue().isSplat(8))
5820 return nullptr;
5821 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
5822 }
5823 }
5824
5825 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
5826 if (CE->getOpcode() == Instruction::IntToPtr) {
5827 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
5828 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
5830 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
5831 return isBytewiseValue(Op, DL);
5832 }
5833 }
5834 }
5835
5836 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
5837 if (LHS == RHS)
5838 return LHS;
5839 if (!LHS || !RHS)
5840 return nullptr;
5841 if (LHS == UndefInt8)
5842 return RHS;
5843 if (RHS == UndefInt8)
5844 return LHS;
5845 return nullptr;
5846 };
5847
5848 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
5849 Value *Val = UndefInt8;
5850 for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
5851 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
5852 return nullptr;
5853 return Val;
5854 }
5855
5856 if (isa<ConstantAggregate>(C)) {
5857 Value *Val = UndefInt8;
5858 for (unsigned I = 0, E = C->getNumOperands(); I != E; ++I)
5859 if (!(Val = Merge(Val, isBytewiseValue(C->getOperand(I), DL))))
5860 return nullptr;
5861 return Val;
5862 }
5863
5864 // Don't try to handle the handful of other constants.
5865 return nullptr;
5866}
5867
5868// This is the recursive version of BuildSubAggregate. It takes a few different
5869// arguments. Idxs is the index within the nested struct From that we are
5870// looking at now (which is of type IndexedType). IdxSkip is the number of
5871// indices from Idxs that should be left out when inserting into the resulting
5872// struct. To is the result struct built so far, new insertvalue instructions
5873// build on that.
5874static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
5876 unsigned IdxSkip,
5877 BasicBlock::iterator InsertBefore) {
5878 StructType *STy = dyn_cast<StructType>(IndexedType);
5879 if (STy) {
5880 // Save the original To argument so we can modify it
5881 Value *OrigTo = To;
5882 // General case, the type indexed by Idxs is a struct
5883 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
5884 // Process each struct element recursively
5885 Idxs.push_back(i);
5886 Value *PrevTo = To;
5887 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
5888 InsertBefore);
5889 Idxs.pop_back();
5890 if (!To) {
5891 // Couldn't find any inserted value for this index? Cleanup
5892 while (PrevTo != OrigTo) {
5893 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
5894 PrevTo = Del->getAggregateOperand();
5895 Del->eraseFromParent();
5896 }
5897 // Stop processing elements
5898 break;
5899 }
5900 }
5901 // If we successfully found a value for each of our subaggregates
5902 if (To)
5903 return To;
5904 }
5905 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
5906 // the struct's elements had a value that was inserted directly. In the latter
5907 // case, perhaps we can't determine each of the subelements individually, but
5908 // we might be able to find the complete struct somewhere.
5909
5910 // Find the value that is at that particular spot
5911 Value *V = FindInsertedValue(From, Idxs);
5912
5913 if (!V)
5914 return nullptr;
5915
5916 // Insert the value in the new (sub) aggregate
5917 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
5918 InsertBefore);
5919}
5920
5921// This helper takes a nested struct and extracts a part of it (which is again a
5922// struct) into a new value. For example, given the struct:
5923// { a, { b, { c, d }, e } }
5924// and the indices "1, 1" this returns
5925// { c, d }.
5926//
5927// It does this by inserting an insertvalue for each element in the resulting
5928// struct, as opposed to just inserting a single struct. This will only work if
5929// each of the elements of the substruct are known (ie, inserted into From by an
5930// insertvalue instruction somewhere).
5931//
5932// All inserted insertvalue instructions are inserted before InsertBefore
5934 BasicBlock::iterator InsertBefore) {
5935 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
5936 idx_range);
5937 Value *To = PoisonValue::get(IndexedType);
5938 SmallVector<unsigned, 10> Idxs(idx_range.begin(), idx_range.end());
5939 unsigned IdxSkip = Idxs.size();
5940
5941 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
5942}
5943
5944/// Given an aggregate and a sequence of indices, see if the scalar value
5945/// indexed is already around as a register, for example if it was inserted
5946/// directly into the aggregate.
5947///
5948/// If InsertBefore is not null, this function will duplicate (modified)
5949/// insertvalues when a part of a nested struct is extracted.
5950Value *
5952 std::optional<BasicBlock::iterator> InsertBefore) {
5953 // Nothing to index? Just return V then (this is useful at the end of our
5954 // recursion).
5955 if (idx_range.empty())
5956 return V;
5957 // We have indices, so V should have an indexable type.
5958 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
5959 "Not looking at a struct or array?");
5960 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
5961 "Invalid indices for type?");
5962
5963 if (Constant *C = dyn_cast<Constant>(V)) {
5964 C = C->getAggregateElement(idx_range[0]);
5965 if (!C) return nullptr;
5966 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
5967 }
5968
5969 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
5970 // Loop the indices for the insertvalue instruction in parallel with the
5971 // requested indices
5972 const unsigned *req_idx = idx_range.begin();
5973 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
5974 i != e; ++i, ++req_idx) {
5975 if (req_idx == idx_range.end()) {
5976 // We can't handle this without inserting insertvalues
5977 if (!InsertBefore)
5978 return nullptr;
5979
5980 // The requested index identifies a part of a nested aggregate. Handle
5981 // this specially. For example,
5982 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
5983 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
5984 // %C = extractvalue {i32, { i32, i32 } } %B, 1
5985 // This can be changed into
5986 // %A = insertvalue {i32, i32 } undef, i32 10, 0
5987 // %C = insertvalue {i32, i32 } %A, i32 11, 1
5988 // which allows the unused 0,0 element from the nested struct to be
5989 // removed.
5990 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
5991 *InsertBefore);
5992 }
5993
5994 // This insert value inserts something else than what we are looking for.
5995 // See if the (aggregate) value inserted into has the value we are
5996 // looking for, then.
5997 if (*req_idx != *i)
5998 return FindInsertedValue(I->getAggregateOperand(), idx_range,
5999 InsertBefore);
6000 }
6001 // If we end up here, the indices of the insertvalue match with those
6002 // requested (though possibly only partially). Now we recursively look at
6003 // the inserted value, passing any remaining indices.
6004 return FindInsertedValue(I->getInsertedValueOperand(),
6005 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6006 }
6007
6008 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
6009 // If we're extracting a value from an aggregate that was extracted from
6010 // something else, we can extract from that something else directly instead.
6011 // However, we will need to chain I's indices with the requested indices.
6012
6013 // Calculate the number of indices required
6014 unsigned size = I->getNumIndices() + idx_range.size();
6015 // Allocate some space to put the new indices in
6017 Idxs.reserve(size);
6018 // Add indices from the extract value instruction
6019 Idxs.append(I->idx_begin(), I->idx_end());
6020
6021 // Add requested indices
6022 Idxs.append(idx_range.begin(), idx_range.end());
6023
6024 assert(Idxs.size() == size
6025 && "Number of indices added not correct?");
6026
6027 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6028 }
6029 // Otherwise, we don't know (such as, extracting from a function return value
6030 // or load instruction)
6031 return nullptr;
6032}
6033
6035 unsigned CharSize) {
6036 // Make sure the GEP has exactly three arguments.
6037 if (GEP->getNumOperands() != 3)
6038 return false;
6039
6040 // Make sure the index-ee is a pointer to array of \p CharSize integers.
6041 // CharSize.
6042 ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
6043 if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
6044 return false;
6045
6046 // Check to make sure that the first operand of the GEP is an integer and
6047 // has value 0 so that we are sure we're indexing into the initializer.
6048 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
6049 if (!FirstIdx || !FirstIdx->isZero())
6050 return false;
6051
6052 return true;
6053}
6054
6055// If V refers to an initialized global constant, set Slice either to
6056// its initializer if the size of its elements equals ElementSize, or,
6057// for ElementSize == 8, to its representation as an array of unsiged
6058// char. Return true on success.
6059// Offset is in the unit "nr of ElementSize sized elements".
6062 unsigned ElementSize, uint64_t Offset) {
6063 assert(V && "V should not be null.");
6064 assert((ElementSize % 8) == 0 &&
6065 "ElementSize expected to be a multiple of the size of a byte.");
6066 unsigned ElementSizeInBytes = ElementSize / 8;
6067
6068 // Drill down into the pointer expression V, ignoring any intervening
6069 // casts, and determine the identity of the object it references along
6070 // with the cumulative byte offset into it.
6071 const GlobalVariable *GV =
6072 dyn_cast<GlobalVariable>(getUnderlyingObject(V));
6073 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6074 // Fail if V is not based on constant global object.
6075 return false;
6076
6077 const DataLayout &DL = GV->getParent()->getDataLayout();
6078 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6079
6080 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6081 /*AllowNonInbounds*/ true))
6082 // Fail if a constant offset could not be determined.
6083 return false;
6084
6085 uint64_t StartIdx = Off.getLimitedValue();
6086 if (StartIdx == UINT64_MAX)
6087 // Fail if the constant offset is excessive.
6088 return false;
6089
6090 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6091 // elements. Simply bail out if that isn't possible.
6092 if ((StartIdx % ElementSizeInBytes) != 0)
6093 return false;
6094
6095 Offset += StartIdx / ElementSizeInBytes;
6096 ConstantDataArray *Array = nullptr;
6097 ArrayType *ArrayTy = nullptr;
6098
6099 if (GV->getInitializer()->isNullValue()) {
6100 Type *GVTy = GV->getValueType();
6101 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6102 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6103
6104 Slice.Array = nullptr;
6105 Slice.Offset = 0;
6106 // Return an empty Slice for undersized constants to let callers
6107 // transform even undefined library calls into simpler, well-defined
6108 // expressions. This is preferable to making the calls although it
6109 // prevents sanitizers from detecting such calls.
6110 Slice.Length = Length < Offset ? 0 : Length - Offset;
6111 return true;
6112 }
6113
6114 auto *Init = const_cast<Constant *>(GV->getInitializer());
6115 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6116 Type *InitElTy = ArrayInit->getElementType();
6117 if (InitElTy->isIntegerTy(ElementSize)) {
6118 // If Init is an initializer for an array of the expected type
6119 // and size, use it as is.
6120 Array = ArrayInit;
6121 ArrayTy = ArrayInit->getType();
6122 }
6123 }
6124
6125 if (!Array) {
6126 if (ElementSize != 8)
6127 // TODO: Handle conversions to larger integral types.
6128 return false;
6129
6130 // Otherwise extract the portion of the initializer starting
6131 // at Offset as an array of bytes, and reset Offset.
6133 if (!Init)
6134 return false;
6135
6136 Offset = 0;
6137 Array = dyn_cast<ConstantDataArray>(Init);
6138 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6139 }
6140
6141 uint64_t NumElts = ArrayTy->getArrayNumElements();
6142 if (Offset > NumElts)
6143 return false;
6144
6145 Slice.Array = Array;
6146 Slice.Offset = Offset;
6147 Slice.Length = NumElts - Offset;
6148 return true;
6149}
6150
6151/// Extract bytes from the initializer of the constant array V, which need
6152/// not be a nul-terminated string. On success, store the bytes in Str and
6153/// return true. When TrimAtNul is set, Str will contain only the bytes up
6154/// to but not including the first nul. Return false on failure.
6156 bool TrimAtNul) {
6158 if (!getConstantDataArrayInfo(V, Slice, 8))
6159 return false;
6160
6161 if (Slice.Array == nullptr) {
6162 if (TrimAtNul) {
6163 // Return a nul-terminated string even for an empty Slice. This is
6164 // safe because all existing SimplifyLibcalls callers require string
6165 // arguments and the behavior of the functions they fold is undefined
6166 // otherwise. Folding the calls this way is preferable to making
6167 // the undefined library calls, even though it prevents sanitizers
6168 // from reporting such calls.
6169 Str = StringRef();
6170 return true;
6171 }
6172 if (Slice.Length == 1) {
6173 Str = StringRef("", 1);
6174 return true;
6175 }
6176 // We cannot instantiate a StringRef as we do not have an appropriate string
6177 // of 0s at hand.
6178 return false;
6179 }
6180
6181 // Start out with the entire array in the StringRef.
6182 Str = Slice.Array->getAsString();
6183 // Skip over 'offset' bytes.
6184 Str = Str.substr(Slice.Offset);
6185
6186 if (TrimAtNul) {
6187 // Trim off the \0 and anything after it. If the array is not nul
6188 // terminated, we just return the whole end of string. The client may know
6189 // some other way that the string is length-bound.
6190 Str = Str.substr(0, Str.find('\0'));
6191 }
6192 return true;
6193}
6194
6195// These next two are very similar to the above, but also look through PHI
6196// nodes.
6197// TODO: See if we can integrate these two together.
6198
6199/// If we can compute the length of the string pointed to by
6200/// the specified pointer, return 'len+1'. If we can't, return 0.
6203 unsigned CharSize) {
6204 // Look through noop bitcast instructions.
6205 V = V->stripPointerCasts();
6206
6207 // If this is a PHI node, there are two cases: either we have already seen it
6208 // or we haven't.
6209 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6210 if (!PHIs.insert(PN).second)
6211 return ~0ULL; // already in the set.
6212
6213 // If it was new, see if all the input strings are the same length.
6214 uint64_t LenSoFar = ~0ULL;
6215 for (Value *IncValue : PN->incoming_values()) {
6216 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6217 if (Len == 0) return 0; // Unknown length -> unknown.
6218
6219 if (Len == ~0ULL) continue;
6220
6221 if (Len != LenSoFar && LenSoFar != ~0ULL)
6222 return 0; // Disagree -> unknown.
6223 LenSoFar = Len;
6224 }
6225
6226 // Success, all agree.
6227 return LenSoFar;
6228 }
6229
6230 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6231 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6232 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6233 if (Len1 == 0) return 0;
6234 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6235 if (Len2 == 0) return 0;
6236 if (Len1 == ~0ULL) return Len2;
6237 if (Len2 == ~0ULL) return Len1;
6238 if (Len1 != Len2) return 0;
6239 return Len1;
6240 }
6241
6242 // Otherwise, see if we can read the string.
6244 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6245 return 0;
6246
6247 if (Slice.Array == nullptr)
6248 // Zeroinitializer (including an empty one).
6249 return 1;
6250
6251 // Search for the first nul character. Return a conservative result even
6252 // when there is no nul. This is safe since otherwise the string function
6253 // being folded such as strlen is undefined, and can be preferable to
6254 // making the undefined library call.
6255 unsigned NullIndex = 0;
6256 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6257 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6258 break;
6259 }
6260
6261 return NullIndex + 1;
6262}
6263
6264/// If we can compute the length of the string pointed to by
6265/// the specified pointer, return 'len+1'. If we can't, return 0.
6266uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6267 if (!V->getType()->isPointerTy())
6268 return 0;
6269
6271 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6272 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6273 // an empty string as a length.
6274 return Len == ~0ULL ? 1 : Len;
6275}
6276
6277const Value *
6279 bool MustPreserveNullness) {
6280 assert(Call &&
6281 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6282 if (const Value *RV = Call->getReturnedArgOperand())
6283 return RV;
6284 // This can be used only as a aliasing property.
6286 Call, MustPreserveNullness))
6287 return Call->getArgOperand(0);
6288 return nullptr;
6289}
6290
6292 const CallBase *Call, bool MustPreserveNullness) {
6293 switch (Call->getIntrinsicID()) {
6294 case Intrinsic::launder_invariant_group:
6295 case Intrinsic::strip_invariant_group:
6296 case Intrinsic::aarch64_irg:
6297 case Intrinsic::aarch64_tagp:
6298 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6299 // input pointer (and thus preserve null-ness for the purposes of escape
6300 // analysis, which is where the MustPreserveNullness flag comes in to play).
6301 // However, it will not necessarily map ptr addrspace(N) null to ptr
6302 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6303 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6304 // list, no one should be relying on such a strict interpretation of
6305 // MustPreserveNullness (and, at time of writing, they are not), but we
6306 // document this fact out of an abundance of caution.
6307 case Intrinsic::amdgcn_make_buffer_rsrc:
6308 return true;
6309 case Intrinsic::ptrmask:
6310 return !MustPreserveNullness;
6311 case Intrinsic::threadlocal_address:
6312 // The underlying variable changes with thread ID. The Thread ID may change
6313 // at coroutine suspend points.
6314 return !Call->getParent()->getParent()->isPresplitCoroutine();
6315 default:
6316 return false;
6317 }
6318}
6319
6320/// \p PN defines a loop-variant pointer to an object. Check if the
6321/// previous iteration of the loop was referring to the same object as \p PN.
6323 const LoopInfo *LI) {
6324 // Find the loop-defined value.
6325 Loop *L = LI->getLoopFor(PN->getParent());
6326 if (PN->getNumIncomingValues() != 2)
6327 return true;
6328
6329 // Find the value from previous iteration.
6330 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6331 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6332 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6333 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6334 return true;
6335
6336 // If a new pointer is loaded in the loop, the pointer references a different
6337 // object in every iteration. E.g.:
6338 // for (i)
6339 // int *p = a[i];
6340 // ...
6341 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6342 if (!L->isLoopInvariant(Load->getPointerOperand()))
6343 return false;
6344 return true;
6345}
6346
6347const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6348 if (!V->getType()->isPointerTy())
6349 return V;
6350 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6351 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6352 V = GEP->getPointerOperand();
6353 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6354 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6355 V = cast<Operator>(V)->getOperand(0);
6356 if (!V->getType()->isPointerTy())
6357 return V;
6358 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6359 if (GA->isInterposable())
6360 return V;
6361 V = GA->getAliasee();
6362 } else {
6363 if (auto *PHI = dyn_cast<PHINode>(V)) {
6364 // Look through single-arg phi nodes created by LCSSA.
6365 if (PHI->getNumIncomingValues() == 1) {
6366 V = PHI->getIncomingValue(0);
6367 continue;
6368 }
6369 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6370 // CaptureTracking can know about special capturing properties of some
6371 // intrinsics like launder.invariant.group, that can't be expressed with
6372 // the attributes, but have properties like returning aliasing pointer.
6373 // Because some analysis may assume that nocaptured pointer is not
6374 // returned from some special intrinsic (because function would have to
6375 // be marked with returns attribute), it is crucial to use this function
6376 // because it should be in sync with CaptureTracking. Not using it may
6377 // cause weird miscompilations where 2 aliasing pointers are assumed to
6378 // noalias.
6379 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6380 V = RP;
6381 continue;
6382 }
6383 }
6384
6385 return V;
6386 }
6387 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6388 }
6389 return V;
6390}
6391
6394 LoopInfo *LI, unsigned MaxLookup) {
6397 Worklist.push_back(V);
6398 do {
6399 const Value *P = Worklist.pop_back_val();
6400 P = getUnderlyingObject(P, MaxLookup);
6401
6402 if (!Visited.insert(P).second)
6403 continue;
6404
6405 if (auto *SI = dyn_cast<SelectInst>(P)) {
6406 Worklist.push_back(SI->getTrueValue());
6407 Worklist.push_back(SI->getFalseValue());
6408 continue;
6409 }
6410
6411 if (auto *PN = dyn_cast<PHINode>(P)) {
6412 // If this PHI changes the underlying object in every iteration of the
6413 // loop, don't look through it. Consider:
6414 // int **A;
6415 // for (i) {
6416 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6417 // Curr = A[i];
6418 // *Prev, *Curr;
6419 //
6420 // Prev is tracking Curr one iteration behind so they refer to different
6421 // underlying objects.
6422 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6424 append_range(Worklist, PN->incoming_values());
6425 else
6426 Objects.push_back(P);
6427 continue;
6428 }
6429
6430 Objects.push_back(P);
6431 } while (!Worklist.empty());
6432}
6433
6434/// This is the function that does the work of looking through basic
6435/// ptrtoint+arithmetic+inttoptr sequences.
6436static const Value *getUnderlyingObjectFromInt(const Value *V) {
6437 do {
6438 if (const Operator *U = dyn_cast<Operator>(V)) {
6439 // If we find a ptrtoint, we can transfer control back to the
6440 // regular getUnderlyingObjectFromInt.
6441 if (U->getOpcode() == Instruction::PtrToInt)
6442 return U->getOperand(0);
6443 // If we find an add of a constant, a multiplied value, or a phi, it's
6444 // likely that the other operand will lead us to the base
6445 // object. We don't have to worry about the case where the
6446 // object address is somehow being computed by the multiply,
6447 // because our callers only care when the result is an
6448 // identifiable object.
6449 if (U->getOpcode() != Instruction::Add ||
6450 (!isa<ConstantInt>(U->getOperand(1)) &&
6451 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6452 !isa<PHINode>(U->getOperand(1))))
6453 return V;
6454 V = U->getOperand(0);
6455 } else {
6456 return V;
6457 }
6458 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6459 } while (true);
6460}
6461
6462/// This is a wrapper around getUnderlyingObjects and adds support for basic
6463/// ptrtoint+arithmetic+inttoptr sequences.
6464/// It returns false if unidentified object is found in getUnderlyingObjects.
6466 SmallVectorImpl<Value *> &Objects) {
6468 SmallVector<const Value *, 4> Working(1, V);
6469 do {
6470 V = Working.pop_back_val();
6471
6473 getUnderlyingObjects(V, Objs);
6474
6475 for (const Value *V : Objs) {
6476 if (!Visited.insert(V).second)
6477 continue;
6478 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6479 const Value *O =
6480 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6481 if (O->getType()->isPointerTy()) {
6482 Working.push_back(O);
6483 continue;
6484 }
6485 }
6486 // If getUnderlyingObjects fails to find an identifiable object,
6487 // getUnderlyingObjectsForCodeGen also fails for safety.
6488 if (!isIdentifiedObject(V)) {
6489 Objects.clear();
6490 return false;
6491 }
6492 Objects.push_back(const_cast<Value *>(V));
6493 }
6494 } while (!Working.empty());
6495 return true;
6496}
6497
6499 AllocaInst *Result = nullptr;
6501 SmallVector<Value *, 4> Worklist;
6502
6503 auto AddWork = [&](Value *V) {
6504 if (Visited.insert(V).second)
6505 Worklist.push_back(V);
6506 };
6507
6508 AddWork(V);
6509 do {
6510 V = Worklist.pop_back_val();
6511 assert(Visited.count(V));
6512
6513 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6514 if (Result && Result != AI)
6515 return nullptr;
6516 Result = AI;
6517 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
6518 AddWork(CI->getOperand(0));
6519 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
6520 for (Value *IncValue : PN->incoming_values())
6521 AddWork(IncValue);
6522 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
6523 AddWork(SI->getTrueValue());
6524 AddWork(SI->getFalseValue());
6525 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
6526 if (OffsetZero && !GEP->hasAllZeroIndices())
6527 return nullptr;
6528 AddWork(GEP->getPointerOperand());
6529 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
6530 Value *Returned = CB->getReturnedArgOperand();
6531 if (Returned)
6532 AddWork(Returned);
6533 else
6534 return nullptr;
6535 } else {
6536 return nullptr;
6537 }
6538 } while (!Worklist.empty());
6539
6540 return Result;
6541}
6542
6544 const Value *V, bool AllowLifetime, bool AllowDroppable) {
6545 for (const User *U : V->users()) {
6546 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
6547 if (!II)
6548 return false;
6549
6550 if (AllowLifetime && II->isLifetimeStartOrEnd())
6551 continue;
6552
6553 if (AllowDroppable && II->isDroppable())
6554 continue;
6555
6556 return false;
6557 }
6558 return true;
6559}
6560
6563 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
6564}
6567 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
6568}
6569
6571 if (!LI.isUnordered())
6572 return true;
6573 const Function &F = *LI.getFunction();
6574 // Speculative load may create a race that did not exist in the source.
6575 return F.hasFnAttribute(Attribute::SanitizeThread) ||
6576 // Speculative load may load data from dirty regions.
6577 F.hasFnAttribute(Attribute::SanitizeAddress) ||
6578 F.hasFnAttribute(Attribute::SanitizeHWAddress);
6579}
6580
6582 const Instruction *CtxI,
6583 AssumptionCache *AC,
6584 const DominatorTree *DT,
6585 const TargetLibraryInfo *TLI) {
6586 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
6587 AC, DT, TLI);
6588}
6589
6591 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
6592 AssumptionCache *AC, const DominatorTree *DT,
6593 const TargetLibraryInfo *TLI) {
6594#ifndef NDEBUG
6595 if (Inst->getOpcode() != Opcode) {
6596 // Check that the operands are actually compatible with the Opcode override.
6597 auto hasEqualReturnAndLeadingOperandTypes =
6598 [](const Instruction *Inst, unsigned NumLeadingOperands) {
6599 if (Inst->getNumOperands() < NumLeadingOperands)
6600 return false;
6601 const Type *ExpectedType = Inst->getType();
6602 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
6603 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
6604 return false;
6605 return true;
6606 };
6608 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
6609 assert(!Instruction::isUnaryOp(Opcode) ||
6610 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
6611 }
6612#endif
6613
6614 switch (Opcode) {
6615 default:
6616 return true;
6617 case Instruction::UDiv:
6618 case Instruction::URem: {
6619 // x / y is undefined if y == 0.
6620 const APInt *V;
6621 if (match(Inst->getOperand(1), m_APInt(V)))
6622 return *V != 0;
6623 return false;
6624 }
6625 case Instruction::SDiv:
6626 case Instruction::SRem: {
6627 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
6628 const APInt *Numerator, *Denominator;
6629 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
6630 return false;
6631 // We cannot hoist this division if the denominator is 0.
6632 if (*Denominator == 0)
6633 return false;
6634 // It's safe to hoist if the denominator is not 0 or -1.
6635 if (!Denominator->isAllOnes())
6636 return true;
6637 // At this point we know that the denominator is -1. It is safe to hoist as
6638 // long we know that the numerator is not INT_MIN.
6639 if (match(Inst->getOperand(0), m_APInt(Numerator)))
6640 return !Numerator->isMinSignedValue();
6641 // The numerator *might* be MinSignedValue.
6642 return false;
6643 }
6644 case Instruction::Load: {
6645 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
6646 if (!LI)
6647 return false;
6648 if (mustSuppressSpeculation(*LI))
6649 return false;
6650 const DataLayout &DL = LI->getModule()->getDataLayout();
6652 LI->getType(), LI->getAlign(), DL,
6653 CtxI, AC, DT, TLI);
6654 }
6655 case Instruction::Call: {
6656 auto *CI = dyn_cast<const CallInst>(Inst);
6657 if (!CI)
6658 return false;
6659 const Function *Callee = CI->getCalledFunction();
6660
6661 // The called function could have undefined behavior or side-effects, even
6662 // if marked readnone nounwind.
6663 return Callee && Callee->isSpeculatable();
6664 }
6665 case Instruction::VAArg:
6666 case Instruction::Alloca:
6667 case Instruction::Invoke:
6668 case Instruction::CallBr:
6669 case Instruction::PHI:
6670 case Instruction::Store:
6671 case Instruction::Ret:
6672 case Instruction::Br:
6673 case Instruction::IndirectBr:
6674 case Instruction::Switch:
6675 case Instruction::Unreachable:
6676 case Instruction::Fence:
6677 case Instruction::AtomicRMW:
6678 case Instruction::AtomicCmpXchg:
6679 case Instruction::LandingPad:
6680 case Instruction::Resume:
6681 case Instruction::CatchSwitch:
6682 case Instruction::CatchPad:
6683 case Instruction::CatchRet:
6684 case Instruction::CleanupPad:
6685 case Instruction::CleanupRet:
6686 return false; // Misc instructions which have effects
6687 }
6688}
6689
6691 if (I.mayReadOrWriteMemory())
6692 // Memory dependency possible
6693 return true;
6695 // Can't move above a maythrow call or infinite loop. Or if an
6696 // inalloca alloca, above a stacksave call.
6697 return true;
6699 // 1) Can't reorder two inf-loop calls, even if readonly
6700 // 2) Also can't reorder an inf-loop call below a instruction which isn't
6701 // safe to speculative execute. (Inverse of above)
6702 return true;
6703 return false;
6704}
6705
6706/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
6708 switch (OR) {
6717 }
6718 llvm_unreachable("Unknown OverflowResult");
6719}
6720
6721/// Combine constant ranges from computeConstantRange() and computeKnownBits().
6724 bool ForSigned,
6725 const SimplifyQuery &SQ) {
6726 ConstantRange CR1 =
6727 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
6728 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
6731 return CR1.intersectWith(CR2, RangeType);
6732}
6733
6735 const Value *RHS,
6736 const SimplifyQuery &SQ,
6737 bool IsNSW) {
6738 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
6739 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
6740
6741 // mul nsw of two non-negative numbers is also nuw.
6742 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
6744
6745 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
6746 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
6747 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
6748}
6749
6751 const Value *RHS,
6752 const SimplifyQuery &SQ) {
6753 // Multiplying n * m significant bits yields a result of n + m significant
6754 // bits. If the total number of significant bits does not exceed the
6755 // result bit width (minus 1), there is no overflow.
6756 // This means if we have enough leading sign bits in the operands
6757 // we can guarantee that the result does not overflow.
6758 // Ref: "Hacker's Delight" by Henry Warren
6759 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
6760
6761 // Note that underestimating the number of sign bits gives a more
6762 // conservative answer.
6763 unsigned SignBits =
6765
6766 // First handle the easy case: if we have enough sign bits there's
6767 // definitely no overflow.
6768 if (SignBits > BitWidth + 1)
6770
6771 // There are two ambiguous cases where there can be no overflow:
6772 // SignBits == BitWidth + 1 and
6773 // SignBits == BitWidth
6774 // The second case is difficult to check, therefore we only handle the
6775 // first case.
6776 if (SignBits == BitWidth + 1) {
6777 // It overflows only when both arguments are negative and the true
6778 // product is exactly the minimum negative number.
6779 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
6780 // For simplicity we just check if at least one side is not negative.
6781 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
6782 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
6783 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
6785 }
6787}
6788
6791 const WithCache<const Value *> &RHS,
6792 const SimplifyQuery &SQ) {
6793 ConstantRange LHSRange =
6794 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
6795 ConstantRange RHSRange =
6796 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
6797 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
6798}
6799
6800static OverflowResult
6802 const WithCache<const Value *> &RHS,
6803 const AddOperator *Add, const SimplifyQuery &SQ) {
6804 if (Add && Add->hasNoSignedWrap()) {
6806 }
6807
6808 // If LHS and RHS each have at least two sign bits, the addition will look
6809 // like
6810 //
6811 // XX..... +
6812 // YY.....
6813 //
6814 // If the carry into the most significant position is 0, X and Y can't both
6815 // be 1 and therefore the carry out of the addition is also 0.
6816 //
6817 // If the carry into the most significant position is 1, X and Y can't both
6818 // be 0 and therefore the carry out of the addition is also 1.
6819 //
6820 // Since the carry into the most significant position is always equal to
6821 // the carry out of the addition, there is no signed overflow.
6822 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
6823 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
6825
6826 ConstantRange LHSRange =
6827 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
6828 ConstantRange RHSRange =
6829 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
6830 OverflowResult OR =
6831 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
6833 return OR;
6834
6835 // The remaining code needs Add to be available. Early returns if not so.
6836 if (!Add)
6838
6839 // If the sign of Add is the same as at least one of the operands, this add
6840 // CANNOT overflow. If this can be determined from the known bits of the
6841 // operands the above signedAddMayOverflow() check will have already done so.
6842 // The only other way to improve on the known bits is from an assumption, so
6843 // call computeKnownBitsFromContext() directly.
6844 bool LHSOrRHSKnownNonNegative =
6845 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
6846 bool LHSOrRHSKnownNegative =
6847 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
6848 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
6849 KnownBits AddKnown(LHSRange.getBitWidth());
6850 computeKnownBitsFromContext(Add, AddKnown, /*Depth=*/0, SQ);
6851 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
6852 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
6854 }
6855
6857}
6858
6860 const Value *RHS,
6861 const SimplifyQuery &SQ) {
6862 // X - (X % ?)
6863 // The remainder of a value can't have greater magnitude than itself,
6864 // so the subtraction can't overflow.
6865
6866 // X - (X -nuw ?)
6867 // In the minimal case, this would simplify to "?", so there's no subtract
6868 // at all. But if this analysis is used to peek through casts, for example,
6869 // then determining no-overflow may allow other transforms.
6870
6871 // TODO: There are other patterns like this.
6872 // See simplifyICmpWithBinOpOnLHS() for candidates.
6873 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
6875 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
6877
6878 // Checking for conditions implied by dominating conditions may be expensive.
6879 // Limit it to usub_with_overflow calls for now.
6880 if (match(SQ.CxtI,
6881 m_Intrinsic<Intrinsic::usub_with_overflow>(m_Value(), m_Value())))
6883 SQ.DL)) {
6884 if (*C)
6887 }
6888 ConstantRange LHSRange =
6889 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
6890 ConstantRange RHSRange =
6891 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
6892 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
6893}
6894
6896 const Value *RHS,
6897 const SimplifyQuery &SQ) {
6898 // X - (X % ?)
6899 // The remainder of a value can't have greater magnitude than itself,
6900 // so the subtraction can't overflow.
6901
6902 // X - (X -nsw ?)
6903 // In the minimal case, this would simplify to "?", so there's no subtract
6904 // at all. But if this analysis is used to peek through casts, for example,
6905 // then determining no-overflow may allow other transforms.
6906 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
6908 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
6910
6911 // If LHS and RHS each have at least two sign bits, the subtraction
6912 // cannot overflow.
6913 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
6914 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
6916
6917 ConstantRange LHSRange =
6918 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
6919 ConstantRange RHSRange =
6920 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
6921 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
6922}
6923
6925 const DominatorTree &DT) {
6926 SmallVector<const BranchInst *, 2> GuardingBranches;
6928
6929 for (const User *U : WO->users()) {
6930 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
6931 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
6932
6933 if (EVI->getIndices()[0] == 0)
6934 Results.push_back(EVI);
6935 else {
6936 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
6937
6938 for (const auto *U : EVI->users())
6939 if (const auto *B = dyn_cast<BranchInst>(U)) {
6940 assert(B->isConditional() && "How else is it using an i1?");
6941 GuardingBranches.push_back(B);
6942 }
6943 }
6944 } else {
6945 // We are using the aggregate directly in a way we don't want to analyze
6946 // here (storing it to a global, say).
6947 return false;
6948 }
6949 }
6950
6951 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
6952 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
6953 if (!NoWrapEdge.isSingleEdge())
6954 return false;
6955
6956 // Check if all users of the add are provably no-wrap.
6957 for (const auto *Result : Results) {
6958 // If the extractvalue itself is not executed on overflow, the we don't
6959 // need to check each use separately, since domination is transitive.
6960 if (DT.dominates(NoWrapEdge, Result->getParent()))
6961 continue;
6962
6963 for (const auto &RU : Result->uses())
6964 if (!DT.dominates(NoWrapEdge, RU))
6965 return false;
6966 }
6967
6968 return true;
6969 };
6970
6971 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
6972}
6973
6974/// Shifts return poison if shiftwidth is larger than the bitwidth.
6975static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
6976 auto *C = dyn_cast<Constant>(ShiftAmount);
6977 if (!C)
6978 return false;
6979
6980 // Shifts return poison if shiftwidth is larger than the bitwidth.
6982 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
6983 unsigned NumElts = FVTy->getNumElements();
6984 for (unsigned i = 0; i < NumElts; ++i)
6985 ShiftAmounts.push_back(C->getAggregateElement(i));
6986 } else if (isa<ScalableVectorType>(C->getType()))
6987 return false; // Can't tell, just return false to be safe
6988 else
6989 ShiftAmounts.push_back(C);
6990
6991 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
6992 auto *CI = dyn_cast_or_null<ConstantInt>(C);
6993 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
6994 });
6995
6996 return Safe;
6997}
6998
7000 PoisonOnly = (1 << 0),
7001 UndefOnly = (1 << 1),
7003};
7004
7006 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7007}
7008
7010 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7011}
7012
7014 bool ConsiderFlagsAndMetadata) {
7015
7016 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7017 Op->hasPoisonGeneratingAnnotations())
7018 return true;
7019
7020 unsigned Opcode = Op->getOpcode();
7021
7022 // Check whether opcode is a poison/undef-generating operation
7023 switch (Opcode) {
7024 case Instruction::Shl:
7025 case Instruction::AShr:
7026 case Instruction::LShr:
7027 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7028 case Instruction::FPToSI:
7029 case Instruction::FPToUI:
7030 // fptosi/ui yields poison if the resulting value does not fit in the
7031 // destination type.
7032 return true;
7033 case Instruction::Call:
7034 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7035 switch (II->getIntrinsicID()) {
7036 // TODO: Add more intrinsics.
7037 case Intrinsic::ctlz:
7038 case Intrinsic::cttz:
7039 case Intrinsic::abs:
7040 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7041 return false;
7042 break;
7043 case Intrinsic::ctpop:
7044 case Intrinsic::bswap:
7045 case Intrinsic::bitreverse:
7046 case Intrinsic::fshl:
7047 case Intrinsic::fshr:
7048 case Intrinsic::smax:
7049 case Intrinsic::smin:
7050 case Intrinsic::umax:
7051 case Intrinsic::umin:
7052 case Intrinsic::ptrmask:
7053 case Intrinsic::fptoui_sat:
7054 case Intrinsic::fptosi_sat:
7055 case Intrinsic::sadd_with_overflow:
7056 case Intrinsic::ssub_with_overflow:
7057 case Intrinsic::smul_with_overflow:
7058 case Intrinsic::uadd_with_overflow:
7059 case Intrinsic::usub_with_overflow:
7060 case Intrinsic::umul_with_overflow:
7061 case Intrinsic::sadd_sat:
7062 case Intrinsic::uadd_sat:
7063 case Intrinsic::ssub_sat:
7064 case Intrinsic::usub_sat:
7065 return false;
7066 case Intrinsic::sshl_sat:
7067 case Intrinsic::ushl_sat:
7068 return includesPoison(Kind) &&
7069 !shiftAmountKnownInRange(II->getArgOperand(1));
7070 case Intrinsic::fma:
7071 case Intrinsic::fmuladd:
7072 case Intrinsic::sqrt:
7073 case Intrinsic::powi:
7074 case Intrinsic::sin:
7075 case Intrinsic::cos:
7076 case Intrinsic::pow:
7077 case Intrinsic::log:
7078 case Intrinsic::log10:
7079 case Intrinsic::log2:
7080 case Intrinsic::exp:
7081 case Intrinsic::exp2:
7082 case Intrinsic::exp10:
7083 case Intrinsic::fabs:
7084 case Intrinsic::copysign:
7085 case Intrinsic::floor:
7086 case Intrinsic::ceil:
7087 case Intrinsic::trunc:
7088 case Intrinsic::rint:
7089 case Intrinsic::nearbyint:
7090 case Intrinsic::round:
7091 case Intrinsic::roundeven:
7092 case Intrinsic::fptrunc_round:
7093 case Intrinsic::canonicalize:
7094 case Intrinsic::arithmetic_fence:
7095 case Intrinsic::minnum:
7096 case Intrinsic::maxnum:
7097 case Intrinsic::minimum:
7098 case Intrinsic::maximum:
7099 case Intrinsic::is_fpclass:
7100 case Intrinsic::ldexp:
7101 case Intrinsic::frexp:
7102 return false;
7103 case Intrinsic::lround:
7104 case Intrinsic::llround:
7105 case Intrinsic::lrint:
7106 case Intrinsic::llrint:
7107 // If the value doesn't fit an unspecified value is returned (but this
7108 // is not poison).
7109 return false;
7110 }
7111 }
7112 [[fallthrough]];
7113 case Instruction::CallBr:
7114 case Instruction::Invoke: {
7115 const auto *CB = cast<CallBase>(Op);
7116 return !CB->hasRetAttr(Attribute::NoUndef);
7117 }
7118 case Instruction::InsertElement:
7119 case Instruction::ExtractElement: {
7120 // If index exceeds the length of the vector, it returns poison
7121 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7122 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7123 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7124 if (includesPoison(Kind))
7125 return !Idx ||
7126 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7127 return false;
7128 }
7129 case Instruction::ShuffleVector: {
7130 ArrayRef<int> Mask = isa<ConstantExpr>(Op)
7131 ? cast<ConstantExpr>(Op)->getShuffleMask()
7132 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7133 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7134 }
7135 case Instruction::FNeg:
7136 case Instruction::PHI:
7137 case Instruction::Select:
7138 case Instruction::URem:
7139 case Instruction::SRem:
7140 case Instruction::ExtractValue:
7141 case Instruction::InsertValue:
7142 case Instruction::Freeze:
7143 case Instruction::ICmp:
7144 case Instruction::FCmp:
7145 case Instruction::FAdd:
7146 case Instruction::FSub:
7147 case Instruction::FMul:
7148 case Instruction::FDiv:
7149 case Instruction::FRem:
7150 return false;
7151 case Instruction::GetElementPtr:
7152 // inbounds is handled above
7153 // TODO: what about inrange on constexpr?
7154 return false;
7155 default: {
7156 const auto *CE = dyn_cast<ConstantExpr>(Op);
7157 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7158 return false;
7159 else if (Instruction::isBinaryOp(Opcode))
7160 return false;
7161 // Be conservative and return true.
7162 return true;
7163 }
7164 }
7165}
7166
7168 bool ConsiderFlagsAndMetadata) {
7169 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7170 ConsiderFlagsAndMetadata);
7171}
7172
7173bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7174 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7175 ConsiderFlagsAndMetadata);
7176}
7177
7178static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7179 unsigned Depth) {
7180 if (ValAssumedPoison == V)
7181 return true;
7182
7183 const unsigned MaxDepth = 2;
7184 if (Depth >= MaxDepth)
7185 return false;
7186
7187 if (const auto *I = dyn_cast<Instruction>(V)) {
7188 if (any_of(I->operands(), [=](const Use &Op) {
7189 return propagatesPoison(Op) &&
7190 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7191 }))
7192 return true;
7193
7194 // V = extractvalue V0, idx
7195 // V2 = extractvalue V0, idx2
7196 // V0's elements are all poison or not. (e.g., add_with_overflow)
7197 const WithOverflowInst *II;
7199 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7200 llvm::is_contained(II->args(), ValAssumedPoison)))
7201 return true;
7202 }
7203 return false;
7204}
7205
7206static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7207 unsigned Depth) {
7208 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7209 return true;
7210
7211 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7212 return true;
7213
7214 const unsigned MaxDepth = 2;
7215 if (Depth >= MaxDepth)
7216 return false;
7217
7218 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7219 if (I && !canCreatePoison(cast<Operator>(I))) {
7220 return all_of(I->operands(), [=](const Value *Op) {
7221 return impliesPoison(Op, V, Depth + 1);
7222 });
7223 }
7224 return false;
7225}
7226
7227bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7228 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7229}
7230
7231static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7232
7234 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7235 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7237 return false;
7238
7239 if (isa<MetadataAsValue>(V))
7240 return false;
7241
7242 if (const auto *A = dyn_cast<Argument>(V)) {
7243 if (A->hasAttribute(Attribute::NoUndef) ||
7244 A->hasAttribute(Attribute::Dereferenceable) ||
7245 A->hasAttribute(Attribute::DereferenceableOrNull))
7246 return true;
7247 }
7248
7249 if (auto *C = dyn_cast<Constant>(V)) {
7250 if (isa<PoisonValue>(C))
7251 return !includesPoison(Kind);
7252
7253 if (isa<UndefValue>(C))
7254 return !includesUndef(Kind);
7255
7256 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(V) ||
7257 isa<ConstantPointerNull>(C) || isa<Function>(C))
7258 return true;
7259
7260 if (C->getType()->isVectorTy() && !isa<ConstantExpr>(C))
7261 return (!includesUndef(Kind) ? !C->containsPoisonElement()
7262 : !C->containsUndefOrPoisonElement()) &&
7263 !C->containsConstantExpression();
7264 }
7265
7266 // Strip cast operations from a pointer value.
7267 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7268 // inbounds with zero offset. To guarantee that the result isn't poison, the
7269 // stripped pointer is checked as it has to be pointing into an allocated
7270 // object or be null `null` to ensure `inbounds` getelement pointers with a
7271 // zero offset could not produce poison.
7272 // It can strip off addrspacecast that do not change bit representation as
7273 // well. We believe that such addrspacecast is equivalent to no-op.
7274 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7275 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7276 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7277 return true;
7278
7279 auto OpCheck = [&](const Value *V) {
7280 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7281 };
7282
7283 if (auto *Opr = dyn_cast<Operator>(V)) {
7284 // If the value is a freeze instruction, then it can never
7285 // be undef or poison.
7286 if (isa<FreezeInst>(V))
7287 return true;
7288
7289 if (const auto *CB = dyn_cast<CallBase>(V)) {
7290 if (CB->hasRetAttr(Attribute::NoUndef) ||
7291 CB->hasRetAttr(Attribute::Dereferenceable) ||
7292 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7293 return true;
7294 }
7295
7296 if (const auto *PN = dyn_cast<PHINode>(V)) {
7297 unsigned Num = PN->getNumIncomingValues();
7298 bool IsWellDefined = true;
7299 for (unsigned i = 0; i < Num; ++i) {
7300 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7301 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7302 DT, Depth + 1, Kind)) {
7303 IsWellDefined = false;
7304 break;
7305 }
7306 }
7307 if (IsWellDefined)
7308 return true;
7309 } else if (!::canCreateUndefOrPoison(Opr, Kind,
7310 /*ConsiderFlagsAndMetadata*/ true) &&
7311 all_of(Opr->operands(), OpCheck))
7312 return true;
7313 }
7314
7315 if (auto *I = dyn_cast<LoadInst>(V))
7316 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7317 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7318 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7319 return true;
7320
7322 return true;
7323
7324 // CxtI may be null or a cloned instruction.
7325 if (!CtxI || !CtxI->getParent() || !DT)
7326 return false;
7327
7328 auto *DNode = DT->getNode(CtxI->getParent());
7329 if (!DNode)
7330 // Unreachable block
7331 return false;
7332
7333 // If V is used as a branch condition before reaching CtxI, V cannot be
7334 // undef or poison.
7335 // br V, BB1, BB2
7336 // BB1:
7337 // CtxI ; V cannot be undef or poison here
7338 auto *Dominator = DNode->getIDom();
7339 // This check is purely for compile time reasons: we can skip the IDom walk
7340 // if what we are checking for includes undef and the value is not an integer.
7341 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7342 while (Dominator) {
7343 auto *TI = Dominator->getBlock()->getTerminator();
7344
7345 Value *Cond = nullptr;
7346 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7347 if (BI->isConditional())
7348 Cond = BI->getCondition();
7349 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7350 Cond = SI->getCondition();
7351 }
7352
7353 if (Cond) {
7354 if (Cond == V)
7355 return true;
7356 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7357 // For poison, we can analyze further
7358 auto *Opr = cast<Operator>(Cond);
7359 if (any_of(Opr->operands(), [V](const Use &U) {
7360 return V == U && propagatesPoison(U);
7361 }))
7362 return true;
7363 }
7364 }
7365
7366 Dominator = Dominator->getIDom();
7367 }
7368
7369 if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
7370 return true;
7371
7372 return false;
7373}
7374
7376 const Instruction *CtxI,
7377 const DominatorTree *DT,
7378 unsigned Depth) {
7379 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7380 UndefPoisonKind::UndefOrPoison);
7381}
7382
7384 const Instruction *CtxI,
7385 const DominatorTree *DT, unsigned Depth) {
7386 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7387 UndefPoisonKind::PoisonOnly);
7388}
7389
7391 const Instruction *CtxI,
7392 const DominatorTree *DT, unsigned Depth) {
7393 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7394 UndefPoisonKind::UndefOnly);
7395}
7396
7397/// Return true if undefined behavior would provably be executed on the path to
7398/// OnPathTo if Root produced a posion result. Note that this doesn't say
7399/// anything about whether OnPathTo is actually executed or whether Root is
7400/// actually poison. This can be used to assess whether a new use of Root can
7401/// be added at a location which is control equivalent with OnPathTo (such as
7402/// immediately before it) without introducing UB which didn't previously
7403/// exist. Note that a false result conveys no information.
7405 Instruction *OnPathTo,
7406 DominatorTree *DT) {
7407 // Basic approach is to assume Root is poison, propagate poison forward
7408 // through all users we can easily track, and then check whether any of those
7409 // users are provable UB and must execute before out exiting block might
7410 // exit.
7411
7412 // The set of all recursive users we've visited (which are assumed to all be
7413 // poison because of said visit)
7414 SmallSet<const Value *, 16> KnownPoison;
7416 Worklist.push_back(Root);
7417 while (!Worklist.empty()) {
7418 const Instruction *I = Worklist.pop_back_val();
7419
7420 // If we know this must trigger UB on a path leading our target.
7421 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7422 return true;
7423
7424 // If we can't analyze propagation through this instruction, just skip it
7425 // and transitive users. Safe as false is a conservative result.
7426 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7427 return KnownPoison.contains(U) && propagatesPoison(U);
7428 }))
7429 continue;
7430
7431 if (KnownPoison.insert(I).second)
7432 for (const User *User : I->users())
7433 Worklist.push_back(cast<Instruction>(User));
7434 }
7435
7436 // Might be non-UB, or might have a path we couldn't prove must execute on
7437 // way to exiting bb.
7438 return false;
7439}
7440
7442 const SimplifyQuery &SQ) {
7443 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7444 Add, SQ);
7445}
7446
7449 const WithCache<const Value *> &RHS,
7450 const SimplifyQuery &SQ) {
7451 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7452}
7453
7455 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7456 // of time because it's possible for another thread to interfere with it for an
7457 // arbitrary length of time, but programs aren't allowed to rely on that.
7458
7459 // If there is no successor, then execution can't transfer to it.
7460 if (isa<ReturnInst>(I))
7461 return false;
7462 if (isa<UnreachableInst>(I))
7463 return false;
7464
7465 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7466 // Instruction::willReturn.
7467 //
7468 // FIXME: Move this check into Instruction::willReturn.
7469 if (isa<CatchPadInst>(I)) {
7470 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7471 default:
7472 // A catchpad may invoke exception object constructors and such, which
7473 // in some languages can be arbitrary code, so be conservative by default.
7474 return false;
7476 // For CoreCLR, it just involves a type test.
7477 return true;
7478 }
7479 }
7480
7481 // An instruction that returns without throwing must transfer control flow
7482 // to a successor.
7483 return !I->mayThrow() && I->willReturn();
7484}
7485
7487 // TODO: This is slightly conservative for invoke instruction since exiting
7488 // via an exception *is* normal control for them.
7489 for (const Instruction &I : *BB)
7491 return false;
7492 return true;
7493}
7494
7497 unsigned ScanLimit) {
7499 ScanLimit);
7500}
7501
7503 iterator_range<BasicBlock::const_iterator> Range, unsigned ScanLimit) {
7504 assert(ScanLimit && "scan limit must be non-zero");
7505 for (const Instruction &I : Range) {
7506 if (isa<DbgInfoIntrinsic>(I))
7507 continue;
7508 if (--ScanLimit == 0)
7509 return false;
7511 return false;
7512 }
7513 return true;
7514}
7515
7517 const Loop *L) {
7518 // The loop header is guaranteed to be executed for every iteration.
7519 //
7520 // FIXME: Relax this constraint to cover all basic blocks that are
7521 // guaranteed to be executed at every iteration.
7522 if (I->getParent() != L->getHeader()) return false;
7523
7524 for (const Instruction &LI : *L->getHeader()) {
7525 if (&LI == I) return true;
7526 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7527 }
7528 llvm_unreachable("Instruction not contained in its own parent basic block.");
7529}
7530
7531bool llvm::propagatesPoison(const Use &PoisonOp) {
7532 const Operator *I = cast<Operator>(PoisonOp.getUser());
7533 switch (I->getOpcode()) {
7534 case Instruction::Freeze:
7535 case Instruction::PHI:
7536 case Instruction::Invoke:
7537 return false;
7538 case Instruction::Select:
7539 return PoisonOp.getOperandNo() == 0;
7540 case Instruction::Call:
7541 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
7542 switch (II->getIntrinsicID()) {
7543 // TODO: Add more intrinsics.
7544 case Intrinsic::sadd_with_overflow:
7545 case Intrinsic::ssub_with_overflow:
7546 case Intrinsic::smul_with_overflow:
7547 case Intrinsic::uadd_with_overflow:
7548 case Intrinsic::usub_with_overflow:
7549 case Intrinsic::umul_with_overflow:
7550 // If an input is a vector containing a poison element, the
7551 // two output vectors (calculated results, overflow bits)'
7552 // corresponding lanes are poison.
7553 return true;
7554 case Intrinsic::ctpop:
7555 case Intrinsic::ctlz:
7556 case Intrinsic::cttz:
7557 case Intrinsic::abs:
7558 case Intrinsic::smax:
7559 case Intrinsic::smin:
7560 case Intrinsic::umax:
7561 case Intrinsic::umin:
7562 case Intrinsic::bitreverse:
7563 case Intrinsic::bswap:
7564 case Intrinsic::sadd_sat:
7565 case Intrinsic::ssub_sat:
7566 case Intrinsic::sshl_sat:
7567 case Intrinsic::uadd_sat:
7568 case Intrinsic::usub_sat:
7569 case Intrinsic::ushl_sat:
7570 return true;
7571 }
7572 }
7573 return false;
7574 case Instruction::ICmp:
7575 case Instruction::FCmp:
7576 case Instruction::GetElementPtr:
7577 return true;
7578 default:
7579 if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I))
7580 return true;
7581
7582 // Be conservative and return false.
7583 return false;
7584 }
7585}
7586
7587/// Enumerates all operands of \p I that are guaranteed to not be undef or
7588/// poison. If the callback \p Handle returns true, stop processing and return
7589/// true. Otherwise, return false.
7590template <typename CallableT>
7592 const CallableT &Handle) {
7593 switch (I->getOpcode()) {
7594 case Instruction::Store:
7595 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
7596 return true;
7597 break;
7598
7599 case Instruction::Load:
7600 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
7601 return true;
7602 break;
7603
7604 // Since dereferenceable attribute imply noundef, atomic operations
7605 // also implicitly have noundef pointers too
7606 case Instruction::AtomicCmpXchg:
7607 if (Handle(cast<AtomicCmpXchgInst>(I)->getPointerOperand()))
7608 return true;
7609 break;
7610
7611 case Instruction::AtomicRMW:
7612 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
7613 return true;
7614 break;
7615
7616 case Instruction::Call:
7617 case Instruction::Invoke: {
7618 const CallBase *CB = cast<CallBase>(I);
7619 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
7620 return true;
7621 for (unsigned i = 0; i < CB->arg_size(); ++i)
7622 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
7623 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
7624 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
7625 Handle(CB->getArgOperand(i)))
7626 return true;
7627 break;
7628 }
7629 case Instruction::Ret:
7630 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
7631 Handle(I->getOperand(0)))
7632 return true;
7633 break;
7634 case Instruction::Switch:
7635 if (Handle(cast<SwitchInst>(I)->getCondition()))
7636 return true;
7637 break;
7638 case Instruction::Br: {
7639 auto *BR = cast<BranchInst>(I);
7640 if (BR->isConditional() && Handle(BR->getCondition()))
7641 return true;
7642 break;
7643 }
7644 default:
7645 break;
7646 }
7647
7648 return false;
7649}
7650
7653 handleGuaranteedWellDefinedOps(I, [&](const Value *V) {
7654 Operands.push_back(V);
7655 return false;
7656 });
7657}
7658
7659/// Enumerates all operands of \p I that are guaranteed to not be poison.
7660template <typename CallableT>
7662 const CallableT &Handle) {
7663 if (handleGuaranteedWellDefinedOps(I, Handle))
7664 return true;
7665 switch (I->getOpcode()) {
7666 // Divisors of these operations are allowed to be partially undef.
7667 case Instruction::UDiv:
7668 case Instruction::SDiv:
7669 case Instruction::URem:
7670 case Instruction::SRem:
7671 return Handle(I->getOperand(1));
7672 default:
7673 return false;
7674 }
7675}
7676
7679 handleGuaranteedNonPoisonOps(I, [&](const Value *V) {
7680 Operands.push_back(V);
7681 return false;
7682 });
7683}
7684
7686 const SmallPtrSetImpl<const Value *> &KnownPoison) {
7688 I, [&](const Value *V) { return KnownPoison.count(V); });
7689}
7690
7692 bool PoisonOnly) {
7693 // We currently only look for uses of values within the same basic
7694 // block, as that makes it easier to guarantee that the uses will be
7695 // executed given that Inst is executed.
7696 //
7697 // FIXME: Expand this to consider uses beyond the same basic block. To do
7698 // this, look out for the distinction between post-dominance and strong
7699 // post-dominance.
7700 const BasicBlock *BB = nullptr;
7702 if (const auto *Inst = dyn_cast<Instruction>(V)) {
7703 BB = Inst->getParent();
7704 Begin = Inst->getIterator();
7705 Begin++;
7706 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
7707 if (Arg->getParent()->isDeclaration())
7708 return false;
7709 BB = &Arg->getParent()->getEntryBlock();
7710 Begin = BB->begin();
7711 } else {
7712 return false;
7713 }
7714
7715 // Limit number of instructions we look at, to avoid scanning through large
7716 // blocks. The current limit is chosen arbitrarily.
7717 unsigned ScanLimit = 32;
7719
7720 if (!PoisonOnly) {
7721 // Since undef does not propagate eagerly, be conservative & just check
7722 // whether a value is directly passed to an instruction that must take
7723 // well-defined operands.
7724
7725 for (const auto &I : make_range(Begin, End)) {
7726 if (isa<DbgInfoIntrinsic>(I))
7727 continue;
7728 if (--ScanLimit == 0)
7729 break;
7730
7731 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
7732 return WellDefinedOp == V;
7733 }))
7734 return true;
7735
7737 break;
7738 }
7739 return false;
7740 }
7741
7742 // Set of instructions that we have proved will yield poison if Inst
7743 // does.
7744 SmallSet<const Value *, 16> YieldsPoison;
7746
7747 YieldsPoison.insert(V);
7748 Visited.insert(BB);
7749
7750 while (true) {
7751 for (const auto &I : make_range(Begin, End)) {
7752 if (isa<DbgInfoIntrinsic>(I))
7753 continue;
7754 if (--ScanLimit == 0)
7755 return false;
7756 if (mustTriggerUB(&I, YieldsPoison))
7757 return true;
7759 return false;
7760
7761 // If an operand is poison and propagates it, mark I as yielding poison.
7762 for (const Use &Op : I.operands()) {
7763 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
7764 YieldsPoison.insert(&I);
7765 break;
7766 }
7767 }
7768
7769 // Special handling for select, which returns poison if its operand 0 is
7770 // poison (handled in the loop above) *or* if both its true/false operands
7771 // are poison (handled here).
7772 if (I.getOpcode() == Instruction::Select &&
7773 YieldsPoison.count(I.getOperand(1)) &&
7774 YieldsPoison.count(I.getOperand(2))) {
7775 YieldsPoison.insert(&I);
7776 }
7777 }
7778
7779 BB = BB->getSingleSuccessor();
7780 if (!BB || !Visited.insert(BB).second)
7781 break;
7782
7783 Begin = BB->getFirstNonPHI()->getIterator();
7784 End = BB->end();
7785 }
7786 return false;
7787}
7788
7790 return ::programUndefinedIfUndefOrPoison(Inst, false);
7791}
7792
7794 return ::programUndefinedIfUndefOrPoison(Inst, true);
7795}
7796
7797static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
7798 if (FMF.noNaNs())
7799 return true;
7800
7801 if (auto *C = dyn_cast<ConstantFP>(V))
7802 return !C->isNaN();
7803
7804 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
7805 if (!C->getElementType()->isFloatingPointTy())
7806 return false;
7807 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
7808 if (C->getElementAsAPFloat(I).isNaN())
7809 return false;
7810 }
7811 return true;
7812 }
7813
7814 if (isa<ConstantAggregateZero>(V))
7815 return true;
7816
7817 return false;
7818}
7819
7820static bool isKnownNonZero(const Value *V) {
7821 if (auto *C = dyn_cast<ConstantFP>(V))
7822 return !C->isZero();
7823
7824 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
7825 if (!C->getElementType()->isFloatingPointTy())
7826 return false;
7827 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
7828 if (C->getElementAsAPFloat(I).isZero())
7829 return false;
7830 }
7831 return true;
7832 }
7833
7834 return false;
7835}
7836
7837/// Match clamp pattern for float types without care about NaNs or signed zeros.
7838/// Given non-min/max outer cmp/select from the clamp pattern this
7839/// function recognizes if it can be substitued by a "canonical" min/max
7840/// pattern.
7842 Value *CmpLHS, Value *CmpRHS,
7843 Value *TrueVal, Value *FalseVal,
7844 Value *&LHS, Value *&RHS) {
7845 // Try to match
7846 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
7847 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
7848 // and return description of the outer Max/Min.
7849
7850 // First, check if select has inverse order:
7851 if (CmpRHS == FalseVal) {
7852 std::swap(TrueVal, FalseVal);
7853 Pred = CmpInst::getInversePredicate(Pred);
7854 }
7855
7856 // Assume success now. If there's no match, callers should not use these anyway.
7857 LHS = TrueVal;
7858 RHS = FalseVal;
7859
7860 const APFloat *FC1;
7861 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
7862 return {SPF_UNKNOWN, SPNB_NA, false};
7863
7864 const APFloat *FC2;
7865 switch (Pred) {
7866 case CmpInst::FCMP_OLT:
7867 case CmpInst::FCMP_OLE:
7868 case CmpInst::FCMP_ULT:
7869 case CmpInst::FCMP_ULE:
7870 if (match(FalseVal,
7872 m_UnordFMin(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
7873 *FC1 < *FC2)
7874 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
7875 break;
7876 case CmpInst::FCMP_OGT:
7877 case CmpInst::FCMP_OGE:
7878 case CmpInst::FCMP_UGT:
7879 case CmpInst::FCMP_UGE:
7880 if (match(FalseVal,
7882 m_UnordFMax(m_Specific(CmpLHS), m_APFloat(FC2)))) &&
7883 *FC1 > *FC2)
7884 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
7885 break;
7886 default:
7887 break;
7888 }
7889
7890 return {SPF_UNKNOWN, SPNB_NA, false};
7891}
7892
7893/// Recognize variations of:
7894/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
7896 Value *CmpLHS, Value *CmpRHS,
7897 Value *TrueVal, Value *FalseVal) {
7898 // Swap the select operands and predicate to match the patterns below.
7899 if (CmpRHS != TrueVal) {
7900 Pred = ICmpInst::getSwappedPredicate(Pred);
7901 std::swap(TrueVal, FalseVal);
7902 }
7903 const APInt *C1;
7904 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
7905 const APInt *C2;
7906 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
7907 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
7908 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
7909 return {SPF_SMAX, SPNB_NA, false};
7910
7911 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
7912 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
7913 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
7914 return {SPF_SMIN, SPNB_NA, false};
7915
7916 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
7917 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
7918 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
7919 return {SPF_UMAX, SPNB_NA, false};
7920
7921 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
7922 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
7923 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
7924 return {SPF_UMIN, SPNB_NA, false};
7925 }
7926 return {SPF_UNKNOWN, SPNB_NA, false};
7927}
7928
7929/// Recognize variations of:
7930/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
7932 Value *CmpLHS, Value *CmpRHS,
7933 Value *TVal, Value *FVal,
7934 unsigned Depth) {
7935 // TODO: Allow FP min/max with nnan/nsz.
7936 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
7937
7938 Value *A = nullptr, *B = nullptr;
7939 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
7940 if (!SelectPatternResult::isMinOrMax(L.Flavor))
7941 return {SPF_UNKNOWN, SPNB_NA, false};
7942
7943 Value *C = nullptr, *D = nullptr;
7944 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
7945 if (L.Flavor != R.Flavor)
7946 return {SPF_UNKNOWN, SPNB_NA, false};
7947
7948 // We have something like: x Pred y ? min(a, b) : min(c, d).
7949 // Try to match the compare to the min/max operations of the select operands.
7950 // First, make sure we have the right compare predicate.
7951 switch (L.Flavor) {
7952 case SPF_SMIN:
7953 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
7954 Pred = ICmpInst::getSwappedPredicate(Pred);
7955 std::swap(CmpLHS, CmpRHS);
7956 }
7957 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
7958 break;
7959 return {SPF_UNKNOWN, SPNB_NA, false};
7960 case SPF_SMAX:
7961 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
7962 Pred = ICmpInst::getSwappedPredicate(Pred);
7963 std::swap(CmpLHS, CmpRHS);
7964 }
7965 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
7966 break;
7967 return {SPF_UNKNOWN, SPNB_NA, false};
7968 case SPF_UMIN:
7969 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
7970 Pred = ICmpInst::getSwappedPredicate(Pred);
7971 std::swap(CmpLHS, CmpRHS);
7972 }
7973 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
7974 break;
7975 return {SPF_UNKNOWN, SPNB_NA, false};
7976 case SPF_UMAX:
7977 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
7978 Pred = ICmpInst::getSwappedPredicate(Pred);
7979 std::swap(CmpLHS, CmpRHS);
7980 }
7981 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
7982 break;
7983 return {SPF_UNKNOWN, SPNB_NA, false};
7984 default:
7985 return {SPF_UNKNOWN, SPNB_NA, false};
7986 }
7987
7988 // If there is a common operand in the already matched min/max and the other
7989 // min/max operands match the compare operands (either directly or inverted),
7990 // then this is min/max of the same flavor.
7991
7992 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
7993 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
7994 if (D == B) {
7995 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
7996 match(A, m_Not(m_Specific(CmpRHS)))))
7997 return {L.Flavor, SPNB_NA, false};
7998 }
7999 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8000 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8001 if (C == B) {
8002 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8003 match(A, m_Not(m_Specific(CmpRHS)))))
8004 return {L.Flavor, SPNB_NA, false};
8005 }
8006 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8007 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8008 if (D == A) {
8009 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8010 match(B, m_Not(m_Specific(CmpRHS)))))
8011 return {L.Flavor, SPNB_NA, false};
8012 }
8013 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8014 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8015 if (C == A) {
8016 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8017 match(B, m_Not(m_Specific(CmpRHS)))))
8018 return {L.Flavor, SPNB_NA, false};
8019 }
8020
8021 return {SPF_UNKNOWN, SPNB_NA, false};
8022}
8023
8024/// If the input value is the result of a 'not' op, constant integer, or vector
8025/// splat of a constant integer, return the bitwise-not source value.
8026/// TODO: This could be extended to handle non-splat vector integer constants.
8028 Value *NotV;
8029 if (match(V, m_Not(m_Value(NotV))))
8030 return NotV;
8031
8032 const APInt *C;
8033 if (match(V, m_APInt(C)))
8034 return ConstantInt::get(V->getType(), ~(*C));
8035
8036 return nullptr;
8037}
8038
8039/// Match non-obvious integer minimum and maximum sequences.
8041 Value *CmpLHS, Value *CmpRHS,
8042 Value *TrueVal, Value *FalseVal,
8043 Value *&LHS, Value *&RHS,
8044 unsigned Depth) {
8045 // Assume success. If there's no match, callers should not use these anyway.
8046 LHS = TrueVal;
8047 RHS = FalseVal;
8048
8049 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8051 return SPR;
8052
8053 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8055 return SPR;
8056
8057 // Look through 'not' ops to find disguised min/max.
8058 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8059 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8060 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8061 switch (Pred) {
8062 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8063 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8064 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8065 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8066 default: break;
8067 }
8068 }
8069
8070 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8071 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8072 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8073 switch (Pred) {
8074 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8075 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8076 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8077 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8078 default: break;
8079 }
8080 }
8081
8082 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8083 return {SPF_UNKNOWN, SPNB_NA, false};
8084
8085 const APInt *C1;
8086 if (!match(CmpRHS, m_APInt(C1)))
8087 return {SPF_UNKNOWN, SPNB_NA, false};
8088
8089 // An unsigned min/max can be written with a signed compare.
8090 const APInt *C2;
8091 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8092 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8093 // Is the sign bit set?
8094 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8095 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8096 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8097 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8098
8099 // Is the sign bit clear?
8100 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8101 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8102 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8103 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8104 }
8105
8106 return {SPF_UNKNOWN, SPNB_NA, false};
8107}
8108
8109bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8110 bool AllowPoison) {
8111 assert(X && Y && "Invalid operand");
8112
8113 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8114 if (!match(X, m_Neg(m_Specific(Y))))
8115 return false;
8116
8117 auto *BO = cast<BinaryOperator>(X);
8118 if (NeedNSW && !BO->hasNoSignedWrap())
8119 return false;
8120
8121 auto *Zero = cast<Constant>(BO->getOperand(0));
8122 if (!AllowPoison && !Zero->isNullValue())
8123 return false;
8124
8125 return true;
8126 };
8127
8128 // X = -Y or Y = -X
8129 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8130 return true;
8131
8132 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8133 Value *A, *B;
8134 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8135 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8136 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8138}
8139
8141 FastMathFlags FMF,
8142 Value *CmpLHS, Value *CmpRHS,
8143 Value *TrueVal, Value *FalseVal,
8144 Value *&LHS, Value *&RHS,
8145 unsigned Depth) {
8146 bool HasMismatchedZeros = false;
8147 if (CmpInst::isFPPredicate(Pred)) {
8148 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8149 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8150 // purpose of identifying min/max. Disregard vector constants with undefined
8151 // elements because those can not be back-propagated for analysis.
8152 Value *OutputZeroVal = nullptr;
8153 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8154 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8155 OutputZeroVal = TrueVal;
8156 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8157 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8158 OutputZeroVal = FalseVal;
8159
8160 if (OutputZeroVal) {
8161 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8162 HasMismatchedZeros = true;
8163 CmpLHS = OutputZeroVal;
8164 }
8165 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8166 HasMismatchedZeros = true;
8167 CmpRHS = OutputZeroVal;
8168 }
8169 }
8170 }
8171
8172 LHS = CmpLHS;
8173 RHS = CmpRHS;
8174
8175 // Signed zero may return inconsistent results between implementations.
8176 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8177 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8178 // Therefore, we behave conservatively and only proceed if at least one of the
8179 // operands is known to not be zero or if we don't care about signed zero.
8180 switch (Pred) {
8181 default: break;
8184 if (!HasMismatchedZeros)
8185 break;
8186 [[fallthrough]];
8189 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8190 !isKnownNonZero(CmpRHS))
8191 return {SPF_UNKNOWN, SPNB_NA, false};
8192 }
8193
8194 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8195 bool Ordered = false;
8196
8197 // When given one NaN and one non-NaN input:
8198 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8199 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8200 // ordered comparison fails), which could be NaN or non-NaN.
8201 // so here we discover exactly what NaN behavior is required/accepted.
8202 if (CmpInst::isFPPredicate(Pred)) {
8203 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8204 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8205
8206 if (LHSSafe && RHSSafe) {
8207 // Both operands are known non-NaN.
8208 NaNBehavior = SPNB_RETURNS_ANY;
8209 } else if (CmpInst::isOrdered(Pred)) {
8210 // An ordered comparison will return false when given a NaN, so it
8211 // returns the RHS.
8212 Ordered = true;
8213 if (LHSSafe)
8214 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8215 NaNBehavior = SPNB_RETURNS_NAN;
8216 else if (RHSSafe)
8217 NaNBehavior = SPNB_RETURNS_OTHER;
8218 else
8219 // Completely unsafe.
8220 return {SPF_UNKNOWN, SPNB_NA, false};
8221 } else {
8222 Ordered = false;
8223 // An unordered comparison will return true when given a NaN, so it
8224 // returns the LHS.
8225 if (LHSSafe)
8226 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8227 NaNBehavior = SPNB_RETURNS_OTHER;
8228 else if (RHSSafe)
8229 NaNBehavior = SPNB_RETURNS_NAN;
8230 else
8231 // Completely unsafe.
8232 return {SPF_UNKNOWN, SPNB_NA, false};
8233 }
8234 }
8235
8236 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8237 std::swap(CmpLHS, CmpRHS);
8238 Pred = CmpInst::getSwappedPredicate(Pred);
8239 if (NaNBehavior == SPNB_RETURNS_NAN)
8240 NaNBehavior = SPNB_RETURNS_OTHER;
8241 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8242 NaNBehavior = SPNB_RETURNS_NAN;
8243 Ordered = !Ordered;
8244 }
8245
8246 // ([if]cmp X, Y) ? X : Y
8247 if (TrueVal == CmpLHS && FalseVal == CmpRHS) {
8248 switch (Pred) {
8249 default: return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8250 case ICmpInst::ICMP_UGT:
8251 case ICmpInst::ICMP_UGE: return {SPF_UMAX, SPNB_NA, false};
8252 case ICmpInst::ICMP_SGT:
8253 case ICmpInst::ICMP_SGE: return {SPF_SMAX, SPNB_NA, false};
8254 case ICmpInst::ICMP_ULT:
8255 case ICmpInst::ICMP_ULE: return {SPF_UMIN, SPNB_NA, false};
8256 case ICmpInst::ICMP_SLT:
8257 case ICmpInst::ICMP_SLE: return {SPF_SMIN, SPNB_NA, false};
8258 case FCmpInst::FCMP_UGT:
8259 case FCmpInst::FCMP_UGE:
8260 case FCmpInst::FCMP_OGT:
8261 case FCmpInst::FCMP_OGE: return {SPF_FMAXNUM, NaNBehavior, Ordered};
8262 case FCmpInst::FCMP_ULT:
8263 case FCmpInst::FCMP_ULE:
8264 case FCmpInst::FCMP_OLT:
8265 case FCmpInst::FCMP_OLE: return {SPF_FMINNUM, NaNBehavior, Ordered};
8266 }
8267 }
8268
8269 if (isKnownNegation(TrueVal, FalseVal)) {
8270 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8271 // match against either LHS or sext(LHS).
8272 auto MaybeSExtCmpLHS =
8273 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8274 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8275 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8276 if (match(TrueVal, MaybeSExtCmpLHS)) {
8277 // Set the return values. If the compare uses the negated value (-X >s 0),
8278 // swap the return values because the negated value is always 'RHS'.
8279 LHS = TrueVal;
8280 RHS = FalseVal;
8281 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8282 std::swap(LHS, RHS);
8283
8284 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8285 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8286 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8287 return {SPF_ABS, SPNB_NA, false};
8288
8289 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8290 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8291 return {SPF_ABS, SPNB_NA, false};
8292
8293 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8294 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8295 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8296 return {SPF_NABS, SPNB_NA, false};
8297 }
8298 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8299 // Set the return values. If the compare uses the negated value (-X >s 0),
8300 // swap the return values because the negated value is always 'RHS'.
8301 LHS = FalseVal;
8302 RHS = TrueVal;
8303 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8304 std::swap(LHS, RHS);
8305
8306 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8307 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8308 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8309 return {SPF_NABS, SPNB_NA, false};
8310
8311 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8312 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8313 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8314 return {SPF_ABS, SPNB_NA, false};
8315 }
8316 }
8317
8318 if (CmpInst::isIntPredicate(Pred))
8319 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8320
8321 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8322 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8323 // semantics than minNum. Be conservative in such case.
8324 if (NaNBehavior != SPNB_RETURNS_ANY ||
8325 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8326 !isKnownNonZero(CmpRHS)))
8327 return {SPF_UNKNOWN, SPNB_NA, false};
8328
8329 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8330}
8331
8332/// Helps to match a select pattern in case of a type mismatch.
8333///
8334/// The function processes the case when type of true and false values of a
8335/// select instruction differs from type of the cmp instruction operands because
8336/// of a cast instruction. The function checks if it is legal to move the cast
8337/// operation after "select". If yes, it returns the new second value of
8338/// "select" (with the assumption that cast is moved):
8339/// 1. As operand of cast instruction when both values of "select" are same cast
8340/// instructions.
8341/// 2. As restored constant (by applying reverse cast operation) when the first
8342/// value of the "select" is a cast operation and the second value is a
8343/// constant.
8344/// NOTE: We return only the new second value because the first value could be
8345/// accessed as operand of cast instruction.
8346static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8347 Instruction::CastOps *CastOp) {
8348 auto *Cast1 = dyn_cast<CastInst>(V1);
8349 if (!Cast1)
8350 return nullptr;
8351
8352 *CastOp = Cast1->getOpcode();
8353 Type *SrcTy = Cast1->getSrcTy();
8354 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8355 // If V1 and V2 are both the same cast from the same type, look through V1.
8356 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
8357 return Cast2->getOperand(0);
8358 return nullptr;
8359 }
8360
8361 auto *C = dyn_cast<Constant>(V2);
8362 if (!C)
8363 return nullptr;
8364
8365 const DataLayout &DL = CmpI->getModule()->getDataLayout();
8366 Constant *CastedTo = nullptr;
8367 switch (*CastOp) {
8368 case Instruction::ZExt:
8369 if (CmpI->isUnsigned())
8370 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8371 break;
8372 case Instruction::SExt:
8373 if (CmpI->isSigned())
8374 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8375 break;
8376 case Instruction::Trunc:
8377 Constant *CmpConst;
8378 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8379 CmpConst->getType() == SrcTy) {
8380 // Here we have the following case:
8381 //
8382 // %cond = cmp iN %x, CmpConst
8383 // %tr = trunc iN %x to iK
8384 // %narrowsel = select i1 %cond, iK %t, iK C
8385 //
8386 // We can always move trunc after select operation:
8387 //
8388 // %cond = cmp iN %x, CmpConst
8389 // %widesel = select i1 %cond, iN %x, iN CmpConst
8390 // %tr = trunc iN %widesel to iK
8391 //
8392 // Note that C could be extended in any way because we don't care about
8393 // upper bits after truncation. It can't be abs pattern, because it would
8394 // look like:
8395 //
8396 // select i1 %cond, x, -x.
8397 //
8398 // So only min/max pattern could be matched. Such match requires widened C
8399 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8400 // CmpConst == C is checked below.
8401 CastedTo = CmpConst;
8402 } else {
8403 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8404 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8405 }
8406 break;
8407 case Instruction::FPTrunc:
8408 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8409 break;
8410 case Instruction::FPExt:
8411 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8412 break;
8413 case Instruction::FPToUI:
8414 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8415 break;
8416 case Instruction::FPToSI:
8417 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8418 break;
8419 case Instruction::UIToFP:
8420 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8421 break;
8422 case Instruction::SIToFP:
8423 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8424 break;
8425 default:
8426 break;
8427 }
8428
8429 if (!CastedTo)
8430 return nullptr;
8431
8432 // Make sure the cast doesn't lose any information.
8433 Constant *CastedBack =
8434 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8435 if (CastedBack && CastedBack != C)
8436 return nullptr;
8437
8438 return CastedTo;
8439}
8440
8442 Instruction::CastOps *CastOp,
8443 unsigned Depth) {
8445 return {SPF_UNKNOWN, SPNB_NA, false};
8446
8447 SelectInst *SI = dyn_cast<SelectInst>(V);
8448 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
8449
8450 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
8451 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
8452
8453 Value *TrueVal = SI->getTrueValue();
8454 Value *FalseVal = SI->getFalseValue();
8455
8456 return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
8457 CastOp, Depth);
8458}
8459
8461 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
8462 Instruction::CastOps *CastOp, unsigned Depth) {
8463 CmpInst::Predicate Pred = CmpI->getPredicate();
8464 Value *CmpLHS = CmpI->getOperand(0);
8465 Value *CmpRHS = CmpI->getOperand(1);
8466 FastMathFlags FMF;
8467 if (isa<FPMathOperator>(CmpI))
8468 FMF = CmpI->getFastMathFlags();
8469
8470 // Bail out early.
8471 if (CmpI->isEquality())
8472 return {SPF_UNKNOWN, SPNB_NA, false};
8473
8474 // Deal with type mismatches.
8475 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
8476 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
8477 // If this is a potential fmin/fmax with a cast to integer, then ignore
8478 // -0.0 because there is no corresponding integer value.
8479 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
8480 FMF.setNoSignedZeros();
8481 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
8482 cast<CastInst>(TrueVal)->getOperand(0), C,
8483 LHS, RHS, Depth);
8484 }
8485 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
8486 // If this is a potential fmin/fmax with a cast to integer, then ignore
8487 // -0.0 because there is no corresponding integer value.
8488 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
8489 FMF.setNoSignedZeros();
8490 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
8491 C, cast<CastInst>(FalseVal)->getOperand(0),
8492 LHS, RHS, Depth);
8493 }
8494 }
8495 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
8496 LHS, RHS, Depth);
8497}
8498
8500 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
8501 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
8502 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
8503 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
8504 if (SPF == SPF_FMINNUM)
8505 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
8506 if (SPF == SPF_FMAXNUM)
8507 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
8508 llvm_unreachable("unhandled!");
8509}
8510
8512 if (SPF == SPF_SMIN) return SPF_SMAX;
8513 if (SPF == SPF_UMIN) return SPF_UMAX;
8514 if (SPF == SPF_SMAX) return SPF_SMIN;
8515 if (SPF == SPF_UMAX) return SPF_UMIN;
8516 llvm_unreachable("unhandled!");
8517}
8518
8520 switch (MinMaxID) {
8521 case Intrinsic::smax: return Intrinsic::smin;
8522 case Intrinsic::smin: return Intrinsic::smax;
8523 case Intrinsic::umax: return Intrinsic::umin;
8524 case Intrinsic::umin: return Intrinsic::umax;
8525 // Please note that next four intrinsics may produce the same result for
8526 // original and inverted case even if X != Y due to NaN is handled specially.
8527 case Intrinsic::maximum: return Intrinsic::minimum;
8528 case Intrinsic::minimum: return Intrinsic::maximum;
8529 case Intrinsic::maxnum: return Intrinsic::minnum;
8530 case Intrinsic::minnum: return Intrinsic::maxnum;
8531 default: llvm_unreachable("Unexpected intrinsic");
8532 }
8533}
8534
8536 switch (SPF) {
8539 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
8540 case SPF_UMIN: return APInt::getMinValue(BitWidth);
8541 default: llvm_unreachable("Unexpected flavor");
8542 }
8543}
8544
8545std::pair<Intrinsic::ID, bool>
8547 // Check if VL contains select instructions that can be folded into a min/max
8548 // vector intrinsic and return the intrinsic if it is possible.
8549 // TODO: Support floating point min/max.
8550 bool AllCmpSingleUse = true;
8551 SelectPatternResult SelectPattern;
8552 SelectPattern.Flavor = SPF_UNKNOWN;
8553 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
8554 Value *LHS, *RHS;
8555 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
8556 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor) ||
8557 CurrentPattern.Flavor == SPF_FMINNUM ||
8558 CurrentPattern.Flavor == SPF_FMAXNUM ||
8559 !I->getType()->isIntOrIntVectorTy())
8560 return false;
8561 if (SelectPattern.Flavor != SPF_UNKNOWN &&
8562 SelectPattern.Flavor != CurrentPattern.Flavor)
8563 return false;
8564 SelectPattern = CurrentPattern;
8565 AllCmpSingleUse &=
8567 return true;
8568 })) {
8569 switch (SelectPattern.Flavor) {
8570 case SPF_SMIN:
8571 return {Intrinsic::smin, AllCmpSingleUse};
8572 case SPF_UMIN:
8573 return {Intrinsic::umin, AllCmpSingleUse};
8574 case SPF_SMAX:
8575 return {Intrinsic::smax, AllCmpSingleUse};
8576 case SPF_UMAX:
8577 return {Intrinsic::umax, AllCmpSingleUse};
8578 default:
8579 llvm_unreachable("unexpected select pattern flavor");
8580 }
8581 }
8582 return {Intrinsic::not_intrinsic, false};
8583}
8584
8586 Value *&Start, Value *&Step) {
8587 // Handle the case of a simple two-predecessor recurrence PHI.
8588 // There's a lot more that could theoretically be done here, but
8589 // this is sufficient to catch some interesting cases.
8590 if (P->getNumIncomingValues() != 2)
8591 return false;
8592
8593 for (unsigned i = 0; i != 2; ++i) {
8594 Value *L = P->getIncomingValue(i);
8595 Value *R = P->getIncomingValue(!i);
8596 auto *LU = dyn_cast<BinaryOperator>(L);
8597 if (!LU)
8598 continue;
8599 unsigned Opcode = LU->getOpcode();
8600
8601 switch (Opcode) {
8602 default:
8603 continue;
8604 // TODO: Expand list -- xor, div, gep, uaddo, etc..
8605 case Instruction::LShr:
8606 case Instruction::AShr:
8607 case Instruction::Shl:
8608 case Instruction::Add:
8609 case Instruction::Sub:
8610 case Instruction::And:
8611 case Instruction::Or:
8612 case Instruction::Mul:
8613 case Instruction::FMul: {
8614 Value *LL = LU->getOperand(0);
8615 Value *LR = LU->getOperand(1);
8616 // Find a recurrence.
8617 if (LL == P)
8618 L = LR;
8619 else if (LR == P)
8620 L = LL;
8621 else
8622 continue; // Check for recurrence with L and R flipped.
8623
8624 break; // Match!
8625 }
8626 };
8627
8628 // We have matched a recurrence of the form:
8629 // %iv = [R, %entry], [%iv.next, %backedge]
8630 // %iv.next = binop %iv, L
8631 // OR
8632 // %iv = [R, %entry], [%iv.next, %backedge]
8633 // %iv.next = binop L, %iv
8634 BO = LU;
8635 Start = R;
8636 Step = L;
8637 return true;
8638 }
8639 return false;
8640}
8641
8643 Value *&Start, Value *&Step) {
8644 BinaryOperator *BO = nullptr;
8645 P = dyn_cast<PHINode>(I->getOperand(0));
8646 if (!P)
8647 P = dyn_cast<PHINode>(I->getOperand(1));
8648 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
8649}
8650
8651/// Return true if "icmp Pred LHS RHS" is always true.
8652static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
8653 const Value *RHS) {
8654 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
8655 return true;
8656
8657 switch (Pred) {
8658 default:
8659 return false;
8660
8661 case CmpInst::ICMP_SLE: {
8662 const APInt *C;
8663
8664 // LHS s<= LHS +_{nsw} C if C >= 0
8665 // LHS s<= LHS | C if C >= 0
8666 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
8668 return !C->isNegative();
8669
8670 // LHS s<= smax(LHS, V) for any V
8672 return true;
8673
8674 // smin(RHS, V) s<= RHS for any V
8676 return true;
8677
8678 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
8679 const Value *X;
8680 const APInt *CLHS, *CRHS;
8681 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
8683 return CLHS->sle(*CRHS);
8684
8685 return false;
8686 }
8687
8688 case CmpInst::ICMP_ULE: {
8689 // LHS u<= LHS +_{nuw} V for any V
8690 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
8691 cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
8692 return true;
8693
8694 // LHS u<= LHS | V for any V
8695 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
8696 return true;
8697
8698 // LHS u<= umax(LHS, V) for any V
8700 return true;
8701
8702 // RHS >> V u<= RHS for any V
8703 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
8704 return true;
8705
8706 // RHS u/ C_ugt_1 u<= RHS
8707 const APInt *C;
8708 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
8709 return true;
8710
8711 // RHS & V u<= RHS for any V
8713 return true;
8714
8715 // umin(RHS, V) u<= RHS for any V
8717 return true;
8718
8719 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
8720 const Value *X;
8721 const APInt *CLHS, *CRHS;
8722 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
8724 return CLHS->ule(*CRHS);
8725
8726 return false;
8727 }
8728 }
8729}
8730
8731/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
8732/// ALHS ARHS" is true. Otherwise, return std::nullopt.
8733static std::optional<bool>
8735 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
8736 switch (Pred) {
8737 default:
8738 return std::nullopt;
8739
8740 case CmpInst::ICMP_SLT:
8741 case CmpInst::ICMP_SLE:
8742 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
8744 return true;
8745 return std::nullopt;
8746
8747 case CmpInst::ICMP_SGT:
8748 case CmpInst::ICMP_SGE:
8749 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
8751 return true;
8752 return std::nullopt;
8753
8754 case CmpInst::ICMP_ULT:
8755 case CmpInst::ICMP_ULE:
8756 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
8758 return true;
8759 return std::nullopt;
8760
8761 case CmpInst::ICMP_UGT:
8762 case CmpInst::ICMP_UGE:
8763 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
8765 return true;
8766 return std::nullopt;
8767 }
8768}
8769
8770/// Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true.
8771/// Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false.
8772/// Otherwise, return std::nullopt if we can't infer anything.
8773static std::optional<bool>
8775 CmpInst::Predicate RPred) {
8776 if (CmpInst::isImpliedTrueByMatchingCmp(LPred, RPred))
8777 return true;
8778 if (CmpInst::isImpliedFalseByMatchingCmp(LPred, RPred))
8779 return false;
8780
8781 return std::nullopt;
8782}
8783
8784/// Return true if "icmp LPred X, LC" implies "icmp RPred X, RC" is true.
8785/// Return false if "icmp LPred X, LC" implies "icmp RPred X, RC" is false.
8786/// Otherwise, return std::nullopt if we can't infer anything.
8788 CmpInst::Predicate LPred, const APInt &LC, CmpInst::Predicate RPred,
8789 const APInt &RC) {
8792 ConstantRange Intersection = DomCR.intersectWith(CR);
8793 ConstantRange Difference = DomCR.difference(CR);
8794 if (Intersection.isEmptySet())
8795 return false;
8796 if (Difference.isEmptySet())
8797 return true;
8798 return std::nullopt;
8799}
8800
8801/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
8802/// is true. Return false if LHS implies RHS is false. Otherwise, return
8803/// std::nullopt if we can't infer anything.
8804static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
8805 CmpInst::Predicate RPred,
8806 const Value *R0, const Value *R1,
8807 const DataLayout &DL,
8808 bool LHSIsTrue) {
8809 Value *L0 = LHS->getOperand(0);
8810 Value *L1 = LHS->getOperand(1);
8811
8812 // The rest of the logic assumes the LHS condition is true. If that's not the
8813 // case, invert the predicate to make it so.
8814 CmpInst::Predicate LPred =
8815 LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
8816
8817 // We can have non-canonical operands, so try to normalize any common operand
8818 // to L0/R0.
8819 if (L0 == R1) {
8820 std::swap(R0, R1);
8821 RPred = ICmpInst::getSwappedPredicate(RPred);
8822 }
8823 if (R0 == L1) {
8824 std::swap(L0, L1);
8825 LPred = ICmpInst::getSwappedPredicate(LPred);
8826 }
8827 if (L1 == R1) {
8828 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
8829 if (L0 != R0 || match(L0, m_ImmConstant())) {
8830 std::swap(L0, L1);
8831 LPred = ICmpInst::getSwappedPredicate(LPred);
8832 std::swap(R0, R1);
8833 RPred = ICmpInst::getSwappedPredicate(RPred);
8834 }
8835 }
8836
8837 // Can we infer anything when the 0-operands match and the 1-operands are
8838 // constants (not necessarily matching)?
8839 const APInt *LC, *RC;
8840 if (L0 == R0 && match(L1, m_APInt(LC)) && match(R1, m_APInt(RC)))
8841 return isImpliedCondCommonOperandWithConstants(LPred, *LC, RPred, *RC);
8842
8843 // Can we infer anything when the two compares have matching operands?
8844 if (L0 == R0 && L1 == R1)
8845 return isImpliedCondMatchingOperands(LPred, RPred);
8846
8847 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
8848 if (L0 == R0 &&
8849 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
8850 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
8851 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
8852 return LPred == RPred;
8853
8854 if (LPred == RPred)
8855 return isImpliedCondOperands(LPred, L0, L1, R0, R1);
8856
8857 return std::nullopt;
8858}
8859
8860/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
8861/// false. Otherwise, return std::nullopt if we can't infer anything. We
8862/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
8863/// instruction.
8864static std::optional<bool>
8866 const Value *RHSOp0, const Value *RHSOp1,
8867 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
8868 // The LHS must be an 'or', 'and', or a 'select' instruction.
8869 assert((LHS->getOpcode() == Instruction::And ||
8870 LHS->getOpcode() == Instruction::Or ||
8871 LHS->getOpcode() == Instruction::Select) &&
8872 "Expected LHS to be 'and', 'or', or 'select'.");
8873
8874 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
8875
8876 // If the result of an 'or' is false, then we know both legs of the 'or' are
8877 // false. Similarly, if the result of an 'and' is true, then we know both
8878 // legs of the 'and' are true.
8879 const Value *ALHS, *ARHS;
8880 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
8881 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
8882 // FIXME: Make this non-recursion.
8883 if (std::optional<bool> Implication = isImpliedCondition(
8884 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
8885 return Implication;
8886 if (std::optional<bool> Implication = isImpliedCondition(
8887 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
8888 return Implication;
8889 return std::nullopt;
8890 }
8891 return std::nullopt;
8892}
8893
8894std::optional<bool>
8896 const Value *RHSOp0, const Value *RHSOp1,
8897 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
8898 // Bail out when we hit the limit.
8900 return std::nullopt;
8901
8902 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
8903 // example.
8904 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
8905 return std::nullopt;
8906
8908 "Expected integer type only!");
8909
8910 // Match not
8911 if (match(LHS, m_Not(m_Value(LHS))))
8912 LHSIsTrue = !LHSIsTrue;
8913
8914 // Both LHS and RHS are icmps.
8915 const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
8916 if (LHSCmp)
8917 return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
8918
8919 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
8920 /// the RHS to be an icmp.
8921 /// FIXME: Add support for and/or/select on the RHS.
8922 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
8923 if ((LHSI->getOpcode() == Instruction::And ||
8924 LHSI->getOpcode() == Instruction::Or ||
8925 LHSI->getOpcode() == Instruction::Select))
8926 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
8927 Depth);
8928 }
8929 return std::nullopt;
8930}
8931
8932std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
8933 const DataLayout &DL,
8934 bool LHSIsTrue, unsigned Depth) {
8935 // LHS ==> RHS by definition
8936 if (LHS == RHS)
8937 return LHSIsTrue;
8938
8939 // Match not
8940 bool InvertRHS = false;
8941 if (match(RHS, m_Not(m_Value(RHS)))) {
8942 if (LHS == RHS)
8943 return !LHSIsTrue;
8944 InvertRHS = true;
8945 }
8946
8947 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
8948 if (auto Implied = isImpliedCondition(
8949 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
8950 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
8951 return InvertRHS ? !*Implied : *Implied;
8952 return std::nullopt;
8953 }
8954
8956 return std::nullopt;
8957
8958 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
8959 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
8960 const Value *RHS1, *RHS2;
8961 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
8962 if (std::optional<bool> Imp =
8963 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
8964 if (*Imp == true)
8965 return !InvertRHS;
8966 if (std::optional<bool> Imp =
8967 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
8968 if (*Imp == true)
8969 return !InvertRHS;
8970 }
8971 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
8972 if (std::optional<bool> Imp =
8973 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
8974 if (*Imp == false)
8975 return InvertRHS;
8976 if (std::optional<bool> Imp =
8977 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
8978 if (*Imp == false)
8979 return InvertRHS;
8980 }
8981
8982 return std::nullopt;
8983}
8984
8985// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
8986// condition dominating ContextI or nullptr, if no condition is found.
8987static std::pair<Value *, bool>
8989 if (!ContextI || !ContextI->getParent())
8990 return {nullptr, false};
8991
8992 // TODO: This is a poor/cheap way to determine dominance. Should we use a
8993 // dominator tree (eg, from a SimplifyQuery) instead?
8994 const BasicBlock *ContextBB = ContextI->getParent();
8995 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
8996 if (!PredBB)
8997 return {nullptr, false};
8998
8999 // We need a conditional branch in the predecessor.
9000 Value *PredCond;
9001 BasicBlock *TrueBB, *FalseBB;
9002 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9003 return {nullptr, false};
9004
9005 // The branch should get simplified. Don't bother simplifying this condition.
9006 if (TrueBB == FalseBB)
9007 return {nullptr, false};
9008
9009 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9010 "Predecessor block does not point to successor?");
9011
9012 // Is this condition implied by the predecessor condition?
9013 return {PredCond, TrueBB == ContextBB};
9014}
9015
9016std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9017 const Instruction *ContextI,
9018 const DataLayout &DL) {
9019 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9020 auto PredCond = getDomPredecessorCondition(ContextI);
9021 if (PredCond.first)
9022 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9023 return std::nullopt;
9024}
9025
9027 const Value *LHS,
9028 const Value *RHS,
9029 const Instruction *ContextI,
9030 const DataLayout &DL) {
9031 auto PredCond = getDomPredecessorCondition(ContextI);
9032 if (PredCond.first)
9033 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9034 PredCond.second);
9035 return std::nullopt;
9036}
9037
9039 APInt &Upper, const InstrInfoQuery &IIQ,
9040 bool PreferSignedRange) {
9041 unsigned Width = Lower.getBitWidth();
9042 const APInt *C;
9043 switch (BO.getOpcode()) {
9044 case Instruction::Add:
9045 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9046 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9047 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9048
9049 // If the caller expects a signed compare, then try to use a signed range.
9050 // Otherwise if both no-wraps are set, use the unsigned range because it
9051 // is never larger than the signed range. Example:
9052 // "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9053 if (PreferSignedRange && HasNSW && HasNUW)
9054 HasNUW = false;
9055
9056 if (HasNUW) {
9057 // 'add nuw x, C' produces [C, UINT_MAX].
9058 Lower = *C;
9059 } else if (HasNSW) {
9060 if (C->isNegative()) {
9061 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9063 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9064 } else {
9065 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9066 Lower = APInt::getSignedMinValue(Width) + *C;
9067 Upper = APInt::getSignedMaxValue(Width) + 1;
9068 }
9069 }
9070 }
9071 break;
9072
9073 case Instruction::And:
9074 if (match(BO.getOperand(1), m_APInt(C)))
9075 // 'and x, C' produces [0, C].
9076 Upper = *C + 1;
9077 // X & -X is a power of two or zero. So we can cap the value at max power of
9078 // two.
9079 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9080 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9081 Upper = APInt::getSignedMinValue(Width) + 1;
9082 break;
9083
9084 case Instruction::Or:
9085 if (match(BO.getOperand(1), m_APInt(C)))
9086 // 'or x, C' produces [C, UINT_MAX].
9087 Lower = *C;
9088 break;
9089
9090 case Instruction::AShr:
9091 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9092 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9094 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9095 } else if (match(BO.getOperand(0), m_APInt(C))) {
9096 unsigned ShiftAmount = Width - 1;
9097 if (!C->isZero() && IIQ.isExact(&BO))
9098 ShiftAmount = C->countr_zero();
9099 if (C->isNegative()) {
9100 // 'ashr C, x' produces [C, C >> (Width-1)]
9101 Lower = *C;
9102 Upper = C->ashr(ShiftAmount) + 1;
9103 } else {
9104 // 'ashr C, x' produces [C >> (Width-1), C]
9105 Lower = C->ashr(ShiftAmount);
9106 Upper = *C + 1;
9107 }
9108 }
9109 break;
9110
9111 case Instruction::LShr:
9112 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9113 // 'lshr x, C' produces [0, UINT_MAX >> C].
9114 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9115 } else if (match(BO.getOperand(0), m_APInt(C))) {
9116 // 'lshr C, x' produces [C >> (Width-1), C].
9117 unsigned ShiftAmount = Width - 1;
9118 if (!C->isZero() && IIQ.isExact(&BO))
9119 ShiftAmount = C->countr_zero();
9120 Lower = C->lshr(ShiftAmount);
9121 Upper = *C + 1;
9122 }
9123 break;
9124
9125 case Instruction::Shl:
9126 if (match(BO.getOperand(0), m_APInt(C))) {
9127 if (IIQ.hasNoUnsignedWrap(&BO)) {
9128 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9129 Lower = *C;
9130 Upper = Lower.shl(Lower.countl_zero()) + 1;
9131 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9132 if (C->isNegative()) {
9133 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9134 unsigned ShiftAmount = C->countl_one() - 1;
9135 Lower = C->shl(ShiftAmount);
9136 Upper = *C + 1;
9137 } else {
9138 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9139 unsigned ShiftAmount = C->countl_zero() - 1;
9140 Lower = *C;
9141 Upper = C->shl(ShiftAmount) + 1;
9142 }
9143 } else {
9144 // If lowbit is set, value can never be zero.
9145 if ((*C)[0])
9146 Lower = APInt::getOneBitSet(Width, 0);
9147 // If we are shifting a constant the largest it can be is if the longest
9148 // sequence of consecutive ones is shifted to the highbits (breaking
9149 // ties for which sequence is higher). At the moment we take a liberal
9150 // upper bound on this by just popcounting the constant.
9151 // TODO: There may be a bitwise trick for it longest/highest
9152 // consecutative sequence of ones (naive method is O(Width) loop).
9153 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9154 }
9155 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9156 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9157 }
9158 break;
9159
9160 case Instruction::SDiv:
9161 if (match(BO.getOperand(1), m_APInt(C))) {
9162 APInt IntMin = APInt::getSignedMinValue(Width);
9163 APInt IntMax = APInt::getSignedMaxValue(Width);
9164 if (C->isAllOnes()) {
9165 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9166 // where C != -1 and C != 0 and C != 1
9167 Lower = IntMin + 1;
9168 Upper = IntMax + 1;
9169 } else if (C->countl_zero() < Width - 1) {
9170 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9171 // where C != -1 and C != 0 and C != 1
9172 Lower = IntMin.sdiv(*C);
9173 Upper = IntMax.sdiv(*C);
9174 if (Lower.sgt(Upper))
9176 Upper = Upper + 1;
9177 assert(Upper != Lower && "Upper part of range has wrapped!");
9178 }
9179 } else if (match(BO.getOperand(0), m_APInt(C))) {
9180 if (C->isMinSignedValue()) {
9181 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9182 Lower = *C;
9183 Upper = Lower.lshr(1) + 1;
9184 } else {
9185 // 'sdiv C, x' produces [-|C|, |C|].
9186 Upper = C->abs() + 1;
9187 Lower = (-Upper) + 1;
9188 }
9189 }
9190 break;
9191
9192 case Instruction::UDiv:
9193 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9194 // 'udiv x, C' produces [0, UINT_MAX / C].
9195 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9196 } else if (match(BO.getOperand(0), m_APInt(C))) {
9197 // 'udiv C, x' produces [0, C].
9198 Upper = *C + 1;
9199 }
9200 break;
9201
9202 case Instruction::SRem:
9203 if (match(BO.getOperand(1), m_APInt(C))) {
9204 // 'srem x, C' produces (-|C|, |C|).
9205 Upper = C->abs();
9206 Lower = (-Upper) + 1;
9207 } else if (match(BO.getOperand(0), m_APInt(C))) {
9208 if (C->isNegative()) {
9209 // 'srem -|C|, x' produces [-|C|, 0].
9210 Upper = 1;
9211 Lower = *C;
9212 } else {
9213 // 'srem |C|, x' produces [0, |C|].
9214 Upper = *C + 1;
9215 }
9216 }
9217 break;
9218
9219 case Instruction::URem:
9220 if (match(BO.getOperand(1), m_APInt(C)))
9221 // 'urem x, C' produces [0, C).
9222 Upper = *C;
9223 else if (match(BO.getOperand(0), m_APInt(C)))
9224 // 'urem C, x' produces [0, C].
9225 Upper = *C + 1;
9226 break;
9227
9228 default:
9229 break;
9230 }
9231}
9232
9234 unsigned Width = II.getType()->getScalarSizeInBits();
9235 const APInt *C;
9236 switch (II.getIntrinsicID()) {
9237 case Intrinsic::ctpop:
9238 case Intrinsic::ctlz:
9239 case Intrinsic::cttz:
9240 // Maximum of set/clear bits is the bit width.
9242 APInt(Width, Width + 1));
9243 case Intrinsic::uadd_sat:
9244 // uadd.sat(x, C) produces [C, UINT_MAX].
9245 if (match(II.getOperand(0), m_APInt(C)) ||
9246 match(II.getOperand(1), m_APInt(C)))
9248 break;
9249 case Intrinsic::sadd_sat:
9250 if (match(II.getOperand(0), m_APInt(C)) ||
9251 match(II.getOperand(1), m_APInt(C))) {
9252 if (C->isNegative())
9253 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9255 APInt::getSignedMaxValue(Width) + *C +
9256 1);
9257
9258 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9260 APInt::getSignedMaxValue(Width) + 1);
9261 }
9262 break;
9263 case Intrinsic::usub_sat:
9264 // usub.sat(C, x) produces [0, C].
9265 if (match(II.getOperand(0), m_APInt(C)))
9266 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9267
9268 // usub.sat(x, C) produces [0, UINT_MAX - C].
9269 if (match(II.getOperand(1), m_APInt(C)))
9271 APInt::getMaxValue(Width) - *C + 1);
9272 break;
9273 case Intrinsic::ssub_sat:
9274 if (match(II.getOperand(0), m_APInt(C))) {
9275 if (C->isNegative())
9276 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9278 *C - APInt::getSignedMinValue(Width) +
9279 1);
9280
9281 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9283 APInt::getSignedMaxValue(Width) + 1);
9284 } else if (match(II.getOperand(1), m_APInt(C))) {
9285 if (C->isNegative())
9286 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9288 APInt::getSignedMaxValue(Width) + 1);
9289
9290 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9292 APInt::getSignedMaxValue(Width) - *C +
9293 1);
9294 }
9295 break;
9296 case Intrinsic::umin:
9297 case Intrinsic::umax:
9298 case Intrinsic::smin:
9299 case Intrinsic::smax:
9300 if (!match(II.getOperand(0), m_APInt(C)) &&
9301 !match(II.getOperand(1), m_APInt(C)))
9302 break;
9303
9304 switch (II.getIntrinsicID()) {
9305 case Intrinsic::umin:
9306 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9307 case Intrinsic::umax:
9309 case Intrinsic::smin:
9311 *C + 1);
9312 case Intrinsic::smax:
9314 APInt::getSignedMaxValue(Width) + 1);
9315 default:
9316 llvm_unreachable("Must be min/max intrinsic");
9317 }
9318 break;
9319 case Intrinsic::abs:
9320 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
9321 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9322 if (match(II.getOperand(1), m_One()))
9324 APInt::getSignedMaxValue(Width) + 1);
9325
9327 APInt::getSignedMinValue(Width) + 1);
9328 case Intrinsic::vscale:
9329 if (!II.getParent() || !II.getFunction())
9330 break;
9331 return getVScaleRange(II.getFunction(), Width);
9332 default:
9333 break;
9334 }
9335
9336 return ConstantRange::getFull(Width);
9337}
9338
9340 const InstrInfoQuery &IIQ) {
9341 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
9342 const Value *LHS = nullptr, *RHS = nullptr;
9344 if (R.Flavor == SPF_UNKNOWN)
9345 return ConstantRange::getFull(BitWidth);
9346
9347 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
9348 // If the negation part of the abs (in RHS) has the NSW flag,
9349 // then the result of abs(X) is [0..SIGNED_MAX],
9350 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9351 if (match(RHS, m_Neg(m_Specific(LHS))) &&
9352 IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
9355
9358 }
9359
9360 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
9361 // The result of -abs(X) is <= 0.
9363 APInt(BitWidth, 1));
9364 }
9365
9366 const APInt *C;
9367 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
9368 return ConstantRange::getFull(BitWidth);
9369
9370 switch (R.Flavor) {
9371 case SPF_UMIN:
9373 case SPF_UMAX:
9375 case SPF_SMIN:
9377 *C + 1);
9378 case SPF_SMAX:
9381 default:
9382 return ConstantRange::getFull(BitWidth);
9383 }
9384}
9385
9387 // The maximum representable value of a half is 65504. For floats the maximum
9388 // value is 3.4e38 which requires roughly 129 bits.
9389 unsigned BitWidth = I->getType()->getScalarSizeInBits();
9390 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
9391 return;
9392 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
9393 Lower = APInt(BitWidth, -65504);
9394 Upper = APInt(BitWidth, 65505);
9395 }
9396
9397 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
9398 // For a fptoui the lower limit is left as 0.
9399 Upper = APInt(BitWidth, 65505);
9400 }
9401}
9402
9404 bool UseInstrInfo, AssumptionCache *AC,
9405 const Instruction *CtxI,
9406 const DominatorTree *DT,
9407 unsigned Depth) {
9408 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
9409
9411 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
9412
9413 const APInt *C;
9414 if (match(V, m_APInt(C)))
9415 return ConstantRange(*C);
9416 unsigned BitWidth = V->getType()->getScalarSizeInBits();
9417
9418 if (auto *VC = dyn_cast<ConstantDataVector>(V)) {
9419 ConstantRange CR = ConstantRange::getEmpty(BitWidth);
9420 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
9421 ++ElemIdx)
9422 CR = CR.unionWith(VC->getElementAsAPInt(ElemIdx));
9423 return CR;
9424 }
9425
9426 InstrInfoQuery IIQ(UseInstrInfo);
9427 ConstantRange CR = ConstantRange::getFull(BitWidth);
9428 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
9429 APInt Lower = APInt(BitWidth, 0);
9430 APInt Upper = APInt(BitWidth, 0);
9431 // TODO: Return ConstantRange.
9432 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
9434 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
9435 CR = getRangeForIntrinsic(*II);
9436 else if (auto *SI = dyn_cast<SelectInst>(V)) {
9438 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
9440 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
9441 CR = CRTrue.unionWith(CRFalse);
9442 CR = CR.intersectWith(getRangeForSelectPattern(*SI, IIQ));
9443 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
9444 APInt Lower = APInt(BitWidth, 0);
9445 APInt Upper = APInt(BitWidth, 0);
9446 // TODO: Return ConstantRange.
9447 setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
9449 } else if (const auto *A = dyn_cast<Argument>(V))
9450 if (std::optional<ConstantRange> Range = A->getRange())
9451 CR = *Range;
9452
9453 if (auto *I = dyn_cast<Instruction>(V)) {
9454 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
9456
9457 if (const auto *CB = dyn_cast<CallBase>(V))
9458 if (std::optional<ConstantRange> Range = CB->getRange())
9459 CR = CR.intersectWith(*Range);
9460 }
9461
9462 if (CtxI && AC) {
9463 // Try to restrict the range based on information from assumptions.
9464 for (auto &AssumeVH : AC->assumptionsFor(V)) {
9465 if (!AssumeVH)
9466 continue;
9467 CallInst *I = cast<CallInst>(AssumeVH);
9468 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
9469 "Got assumption for the wrong function!");
9470 assert(I->getCalledFunction()->getIntrinsicID() == Intrinsic::assume &&
9471 "must be an assume intrinsic");
9472
9473 if (!isValidAssumeForContext(I, CtxI, DT))
9474 continue;
9475 Value *Arg = I->getArgOperand(0);
9476 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
9477 // Currently we just use information from comparisons.
9478 if (!Cmp || Cmp->getOperand(0) != V)
9479 continue;
9480 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
9482 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
9483 UseInstrInfo, AC, I, DT, Depth + 1);
9484 CR = CR.intersectWith(
9485 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
9486 }
9487 }
9488
9489 return CR;
9490}
9491
9492static void
9494 function_ref<void(Value *)> InsertAffected) {
9495 assert(V != nullptr);
9496 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
9497 InsertAffected(V);
9498 } else if (auto *I = dyn_cast<Instruction>(V)) {
9499 InsertAffected(V);
9500
9501 // Peek through unary operators to find the source of the condition.
9502 Value *Op;
9504 if (isa<Instruction>(Op) || isa<Argument>(Op))
9505 InsertAffected(Op);
9506 }
9507 }
9508}
9509
9511 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
9512 auto AddAffected = [&InsertAffected](Value *V) {
9513 addValueAffectedByCondition(V, InsertAffected);
9514 };
9515
9516 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
9517 if (IsAssume) {
9518 AddAffected(LHS);
9519 AddAffected(RHS);
9520 } else if (match(RHS, m_Constant()))
9521 AddAffected(LHS);
9522 };
9523
9524 SmallVector<Value *, 8> Worklist;
9526 Worklist.push_back(Cond);
9527 while (!Worklist.empty()) {
9528 Value *V = Worklist.pop_back_val();
9529 if (!Visited.insert(V).second)
9530 continue;
9531
9532 CmpInst::Predicate Pred;
9533 Value *A, *B, *X;
9534
9535 if (IsAssume) {
9536 AddAffected(V);
9537 if (match(V, m_Not(m_Value(X))))
9538 AddAffected(X);
9539 }
9540
9541 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
9542 // assume(A && B) is split to -> assume(A); assume(B);
9543 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
9544 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
9545 // enough information to be worth handling (intersection of information as
9546 // opposed to union).
9547 if (!IsAssume) {
9548 Worklist.push_back(A);
9549 Worklist.push_back(B);
9550 }
9551 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
9552 AddCmpOperands(A, B);
9553
9554 if (ICmpInst::isEquality(Pred)) {
9555 if (match(B, m_ConstantInt())) {
9556 Value *Y;
9557 // (X & C) or (X | C) or (X ^ C).
9558 // (X << C) or (X >>_s C) or (X >>_u C).
9561 AddAffected(X);
9562 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
9563 match(A, m_Or(m_Value(X), m_Value(Y)))) {
9564 AddAffected(X);
9565 AddAffected(Y);
9566 }
9567 }
9568 } else {
9569 if (match(B, m_ConstantInt())) {
9570 // Handle (A + C1) u< C2, which is the canonical form of
9571 // A > C3 && A < C4.
9573 AddAffected(X);
9574
9575 Value *Y;
9576 // X & Y u> C -> X >u C && Y >u C
9577 // X | Y u< C -> X u< C && Y u< C
9578 if (ICmpInst::isUnsigned(Pred) &&
9579 (match(A, m_And(m_Value(X), m_Value(Y))) ||
9580 match(A, m_Or(m_Value(X), m_Value(Y))))) {
9581 AddAffected(X);
9582 AddAffected(Y);
9583 }
9584 }
9585
9586 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
9587 // by computeKnownFPClass().
9589 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
9590 InsertAffected(X);
9591 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
9592 InsertAffected(X);
9593 }
9594 }
9595 } else if (match(Cond, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
9596 AddCmpOperands(A, B);
9597
9598 // fcmp fneg(x), y
9599 // fcmp fabs(x), y
9600 // fcmp fneg(fabs(x)), y
9601 if (match(A, m_FNeg(m_Value(A))))
9602 AddAffected(A);
9603 if (match(A, m_FAbs(m_Value(A))))
9604 AddAffected(A);
9605
9606 } else if (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
9607 m_Value()))) {
9608 // Handle patterns that computeKnownFPClass() support.
9609 AddAffected(A);
9610 }
9611 }
9612}
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)
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth, const SimplifyQuery &Q)
Value * RHS
Value * LHS
bool isNegative() const
Definition: APFloat.h:1295
bool isFinite() const
Definition: APFloat.h:1300
APInt bitcastToAPInt() const
Definition: APFloat.h:1210
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1006
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:966
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5178
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:1498
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:1713
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:1446
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:1585
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:1710
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
void clearSignBit()
Set the sign bit to 0.
Definition: APInt.h:1409
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:2093
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:655
Type * getValueType() const
Definition: GlobalValue.h:295
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:199
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:524
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)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition: PatternMatch.h:664
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:619
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:652
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:764
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:875
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:592
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:822
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:893
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:599
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:854
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:612
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...
bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
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.
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).
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
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:2058
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