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