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