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