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