LLVM 20.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"
37#include "llvm/IR/Argument.h"
38#include "llvm/IR/Attributes.h"
39#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/Constant.h"
42#include "llvm/IR/Constants.h"
45#include "llvm/IR/Dominators.h"
47#include "llvm/IR/Function.h"
49#include "llvm/IR/GlobalAlias.h"
50#include "llvm/IR/GlobalValue.h"
52#include "llvm/IR/InstrTypes.h"
53#include "llvm/IR/Instruction.h"
56#include "llvm/IR/Intrinsics.h"
57#include "llvm/IR/IntrinsicsAArch64.h"
58#include "llvm/IR/IntrinsicsAMDGPU.h"
59#include "llvm/IR/IntrinsicsRISCV.h"
60#include "llvm/IR/IntrinsicsX86.h"
61#include "llvm/IR/LLVMContext.h"
62#include "llvm/IR/Metadata.h"
63#include "llvm/IR/Module.h"
64#include "llvm/IR/Operator.h"
66#include "llvm/IR/Type.h"
67#include "llvm/IR/User.h"
68#include "llvm/IR/Value.h"
76#include <algorithm>
77#include <cassert>
78#include <cstdint>
79#include <optional>
80#include <utility>
81
82using namespace llvm;
83using namespace llvm::PatternMatch;
84
85// Controls the number of uses of the value searched for possible
86// dominating comparisons.
87static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
88 cl::Hidden, cl::init(20));
89
90
91/// Returns the bitwidth of the given scalar or pointer type. For vector types,
92/// returns the element type's bitwidth.
93static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
94 if (unsigned BitWidth = Ty->getScalarSizeInBits())
95 return BitWidth;
96
97 return DL.getPointerTypeSizeInBits(Ty);
98}
99
100// Given the provided Value and, potentially, a context instruction, return
101// the preferred context instruction (if any).
102static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
103 // If we've been provided with a context instruction, then use that (provided
104 // it has been inserted).
105 if (CxtI && CxtI->getParent())
106 return CxtI;
107
108 // If the value is really an already-inserted instruction, then use that.
109 CxtI = dyn_cast<Instruction>(V);
110 if (CxtI && CxtI->getParent())
111 return CxtI;
112
113 return nullptr;
114}
115
116static const Instruction *safeCxtI(const Value *V1, const Value *V2, const Instruction *CxtI) {
117 // If we've been provided with a context instruction, then use that (provided
118 // it has been inserted).
119 if (CxtI && CxtI->getParent())
120 return CxtI;
121
122 // If the value is really an already-inserted instruction, then use that.
123 CxtI = dyn_cast<Instruction>(V1);
124 if (CxtI && CxtI->getParent())
125 return CxtI;
126
127 CxtI = dyn_cast<Instruction>(V2);
128 if (CxtI && CxtI->getParent())
129 return CxtI;
130
131 return nullptr;
132}
133
135 const APInt &DemandedElts,
136 APInt &DemandedLHS, APInt &DemandedRHS) {
137 if (isa<ScalableVectorType>(Shuf->getType())) {
138 assert(DemandedElts == APInt(1,1));
139 DemandedLHS = DemandedRHS = DemandedElts;
140 return true;
141 }
142
143 int NumElts =
144 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
145 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
146 DemandedElts, DemandedLHS, DemandedRHS);
147}
148
149static void computeKnownBits(const Value *V, const APInt &DemandedElts,
150 KnownBits &Known, unsigned Depth,
151 const SimplifyQuery &Q);
152
153void llvm::computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
154 const SimplifyQuery &Q) {
155 // Since the number of lanes in a scalable vector is unknown at compile time,
156 // we track one bit which is implicitly broadcast to all lanes. This means
157 // that all lanes in a scalable vector are considered demanded.
158 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
159 APInt DemandedElts =
160 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
161 ::computeKnownBits(V, DemandedElts, Known, Depth, Q);
162}
163
165 const DataLayout &DL, unsigned Depth,
166 AssumptionCache *AC, const Instruction *CxtI,
167 const DominatorTree *DT, bool UseInstrInfo) {
169 V, Known, Depth,
170 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
171}
172
174 unsigned Depth, AssumptionCache *AC,
175 const Instruction *CxtI,
176 const DominatorTree *DT, bool UseInstrInfo) {
177 return computeKnownBits(
178 V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
179}
180
181KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
182 const DataLayout &DL, unsigned Depth,
183 AssumptionCache *AC, const Instruction *CxtI,
184 const DominatorTree *DT, bool UseInstrInfo) {
185 return computeKnownBits(
186 V, DemandedElts, Depth,
187 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
188}
189
190static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS,
191 const SimplifyQuery &SQ) {
192 // Look for an inverted mask: (X & ~M) op (Y & M).
193 {
194 Value *M;
195 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
197 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
198 return true;
199 }
200
201 // X op (Y & ~X)
204 return true;
205
206 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
207 // for constant Y.
208 Value *Y;
209 if (match(RHS,
211 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
212 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
213 return true;
214
215 // Peek through extends to find a 'not' of the other side:
216 // (ext Y) op ext(~Y)
217 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
219 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
220 return true;
221
222 // Look for: (A & B) op ~(A | B)
223 {
224 Value *A, *B;
225 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
227 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
228 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
229 return true;
230 }
231
232 return false;
233}
234
236 const WithCache<const Value *> &RHSCache,
237 const SimplifyQuery &SQ) {
238 const Value *LHS = LHSCache.getValue();
239 const Value *RHS = RHSCache.getValue();
240
241 assert(LHS->getType() == RHS->getType() &&
242 "LHS and RHS should have the same type");
244 "LHS and RHS should be integers");
245
248 return true;
249
251 RHSCache.getKnownBits(SQ));
252}
253
255 return !I->user_empty() && all_of(I->users(), [](const User *U) {
256 return match(U, m_ICmp(m_Value(), m_Zero()));
257 });
258}
259
261 return !I->user_empty() && all_of(I->users(), [](const User *U) {
262 CmpPredicate P;
263 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
264 });
265}
266
268 bool OrZero, unsigned Depth,
269 AssumptionCache *AC, const Instruction *CxtI,
270 const DominatorTree *DT, bool UseInstrInfo) {
271 return ::isKnownToBeAPowerOfTwo(
272 V, OrZero, Depth,
273 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
274}
275
276static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
277 const SimplifyQuery &Q, unsigned Depth);
278
280 unsigned Depth) {
281 return computeKnownBits(V, Depth, SQ).isNonNegative();
282}
283
285 unsigned Depth) {
286 if (auto *CI = dyn_cast<ConstantInt>(V))
287 return CI->getValue().isStrictlyPositive();
288
289 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
290 // this updated.
291 KnownBits Known = computeKnownBits(V, Depth, SQ);
292 return Known.isNonNegative() &&
293 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
294}
295
297 unsigned Depth) {
298 return computeKnownBits(V, Depth, SQ).isNegative();
299}
300
301static bool isKnownNonEqual(const Value *V1, const Value *V2,
302 const APInt &DemandedElts, unsigned Depth,
303 const SimplifyQuery &Q);
304
305bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
306 const DataLayout &DL, AssumptionCache *AC,
307 const Instruction *CxtI, const DominatorTree *DT,
308 bool UseInstrInfo) {
309 // We don't support looking through casts.
310 if (V1 == V2 || V1->getType() != V2->getType())
311 return false;
312 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
313 APInt DemandedElts =
314 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
315 return ::isKnownNonEqual(
316 V1, V2, DemandedElts, 0,
317 SimplifyQuery(DL, DT, AC, safeCxtI(V2, V1, CxtI), UseInstrInfo));
318}
319
320bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
321 const SimplifyQuery &SQ, unsigned Depth) {
322 KnownBits Known(Mask.getBitWidth());
323 computeKnownBits(V, Known, Depth, SQ);
324 return Mask.isSubsetOf(Known.Zero);
325}
326
327static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
328 unsigned Depth, const SimplifyQuery &Q);
329
330static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
331 const SimplifyQuery &Q) {
332 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
333 APInt DemandedElts =
334 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
335 return ComputeNumSignBits(V, DemandedElts, Depth, Q);
336}
337
338unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
339 unsigned Depth, AssumptionCache *AC,
340 const Instruction *CxtI,
341 const DominatorTree *DT, bool UseInstrInfo) {
342 return ::ComputeNumSignBits(
343 V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
344}
345
347 unsigned Depth, AssumptionCache *AC,
348 const Instruction *CxtI,
349 const DominatorTree *DT) {
350 unsigned SignBits = ComputeNumSignBits(V, DL, Depth, AC, CxtI, DT);
351 return V->getType()->getScalarSizeInBits() - SignBits + 1;
352}
353
354static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
355 bool NSW, bool NUW,
356 const APInt &DemandedElts,
357 KnownBits &KnownOut, KnownBits &Known2,
358 unsigned Depth, const SimplifyQuery &Q) {
359 computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q);
360
361 // If one operand is unknown and we have no nowrap information,
362 // the result will be unknown independently of the second operand.
363 if (KnownOut.isUnknown() && !NSW && !NUW)
364 return;
365
366 computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
367 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
368}
369
370static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
371 bool NUW, const APInt &DemandedElts,
372 KnownBits &Known, KnownBits &Known2,
373 unsigned Depth, const SimplifyQuery &Q) {
374 computeKnownBits(Op1, DemandedElts, Known, Depth + 1, Q);
375 computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
376
377 bool isKnownNegative = false;
378 bool isKnownNonNegative = false;
379 // If the multiplication is known not to overflow, compute the sign bit.
380 if (NSW) {
381 if (Op0 == Op1) {
382 // The product of a number with itself is non-negative.
383 isKnownNonNegative = true;
384 } else {
385 bool isKnownNonNegativeOp1 = Known.isNonNegative();
386 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
387 bool isKnownNegativeOp1 = Known.isNegative();
388 bool isKnownNegativeOp0 = Known2.isNegative();
389 // The product of two numbers with the same sign is non-negative.
390 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
391 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
392 if (!isKnownNonNegative && NUW) {
393 // mul nuw nsw with a factor > 1 is non-negative.
395 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
396 KnownBits::sgt(Known2, One).value_or(false);
397 }
398
399 // The product of a negative number and a non-negative number is either
400 // negative or zero.
403 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
404 Known2.isNonZero()) ||
405 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
406 }
407 }
408
409 bool SelfMultiply = Op0 == Op1;
410 if (SelfMultiply)
411 SelfMultiply &=
412 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
413 Known = KnownBits::mul(Known, Known2, SelfMultiply);
414
415 // Only make use of no-wrap flags if we failed to compute the sign bit
416 // directly. This matters if the multiplication always overflows, in
417 // which case we prefer to follow the result of the direct computation,
418 // though as the program is invoking undefined behaviour we can choose
419 // whatever we like here.
420 if (isKnownNonNegative && !Known.isNegative())
421 Known.makeNonNegative();
422 else if (isKnownNegative && !Known.isNonNegative())
423 Known.makeNegative();
424}
425
427 KnownBits &Known) {
428 unsigned BitWidth = Known.getBitWidth();
429 unsigned NumRanges = Ranges.getNumOperands() / 2;
430 assert(NumRanges >= 1);
431
432 Known.Zero.setAllBits();
433 Known.One.setAllBits();
434
435 for (unsigned i = 0; i < NumRanges; ++i) {
437 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
439 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
440 ConstantRange Range(Lower->getValue(), Upper->getValue());
441
442 // The first CommonPrefixBits of all values in Range are equal.
443 unsigned CommonPrefixBits =
445 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
447 Known.One &= UnsignedMax & Mask;
448 Known.Zero &= ~UnsignedMax & Mask;
449 }
450}
451
452static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
456
457 // The instruction defining an assumption's condition itself is always
458 // considered ephemeral to that assumption (even if it has other
459 // non-ephemeral users). See r246696's test case for an example.
460 if (is_contained(I->operands(), E))
461 return true;
462
463 while (!WorkSet.empty()) {
464 const Value *V = WorkSet.pop_back_val();
465 if (!Visited.insert(V).second)
466 continue;
467
468 // If all uses of this value are ephemeral, then so is this value.
469 if (llvm::all_of(V->users(), [&](const User *U) {
470 return EphValues.count(U);
471 })) {
472 if (V == E)
473 return true;
474
475 if (V == I || (isa<Instruction>(V) &&
476 !cast<Instruction>(V)->mayHaveSideEffects() &&
477 !cast<Instruction>(V)->isTerminator())) {
478 EphValues.insert(V);
479 if (const User *U = dyn_cast<User>(V))
480 append_range(WorkSet, U->operands());
481 }
482 }
483 }
484
485 return false;
486}
487
488// Is this an intrinsic that cannot be speculated but also cannot trap?
490 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
491 return CI->isAssumeLikeIntrinsic();
492
493 return false;
494}
495
497 const Instruction *CxtI,
498 const DominatorTree *DT,
499 bool AllowEphemerals) {
500 // There are two restrictions on the use of an assume:
501 // 1. The assume must dominate the context (or the control flow must
502 // reach the assume whenever it reaches the context).
503 // 2. The context must not be in the assume's set of ephemeral values
504 // (otherwise we will use the assume to prove that the condition
505 // feeding the assume is trivially true, thus causing the removal of
506 // the assume).
507
508 if (Inv->getParent() == CxtI->getParent()) {
509 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
510 // in the BB.
511 if (Inv->comesBefore(CxtI))
512 return true;
513
514 // Don't let an assume affect itself - this would cause the problems
515 // `isEphemeralValueOf` is trying to prevent, and it would also make
516 // the loop below go out of bounds.
517 if (!AllowEphemerals && Inv == CxtI)
518 return false;
519
520 // The context comes first, but they're both in the same block.
521 // Make sure there is nothing in between that might interrupt
522 // the control flow, not even CxtI itself.
523 // We limit the scan distance between the assume and its context instruction
524 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
525 // it can be adjusted if needed (could be turned into a cl::opt).
526 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
528 return false;
529
530 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
531 }
532
533 // Inv and CxtI are in different blocks.
534 if (DT) {
535 if (DT->dominates(Inv, CxtI))
536 return true;
537 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
538 Inv->getParent()->isEntryBlock()) {
539 // We don't have a DT, but this trivially dominates.
540 return true;
541 }
542
543 return false;
544}
545
546// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
547// we still have enough information about `RHS` to conclude non-zero. For
548// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
549// so the extra compile time may not be worth it, but possibly a second API
550// should be created for use outside of loops.
551static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
552 // v u> y implies v != 0.
553 if (Pred == ICmpInst::ICMP_UGT)
554 return true;
555
556 // Special-case v != 0 to also handle v != null.
557 if (Pred == ICmpInst::ICMP_NE)
558 return match(RHS, m_Zero());
559
560 // All other predicates - rely on generic ConstantRange handling.
561 const APInt *C;
563 if (match(RHS, m_APInt(C))) {
565 return !TrueValues.contains(Zero);
566 }
567
568 auto *VC = dyn_cast<ConstantDataVector>(RHS);
569 if (VC == nullptr)
570 return false;
571
572 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
573 ++ElemIdx) {
575 Pred, VC->getElementAsAPInt(ElemIdx));
576 if (TrueValues.contains(Zero))
577 return false;
578 }
579 return true;
580}
581
582static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
583 // Use of assumptions is context-sensitive. If we don't have a context, we
584 // cannot use them!
585 if (!Q.AC || !Q.CxtI)
586 return false;
587
588 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
589 if (!Elem.Assume)
590 continue;
591
592 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
593 assert(I->getFunction() == Q.CxtI->getFunction() &&
594 "Got assumption for the wrong function!");
595
596 if (Elem.Index != AssumptionCache::ExprResultIdx) {
597 if (!V->getType()->isPointerTy())
598 continue;
600 *I, I->bundle_op_info_begin()[Elem.Index])) {
601 if (RK.WasOn == V &&
602 (RK.AttrKind == Attribute::NonNull ||
603 (RK.AttrKind == Attribute::Dereferenceable &&
605 V->getType()->getPointerAddressSpace()))) &&
607 return true;
608 }
609 continue;
610 }
611
612 // Warning: This loop can end up being somewhat performance sensitive.
613 // We're running this loop for once for each value queried resulting in a
614 // runtime of ~O(#assumes * #values).
615
616 Value *RHS;
617 CmpPredicate Pred;
618 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
619 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
620 continue;
621
622 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
623 return true;
624 }
625
626 return false;
627}
628
630 Value *LHS, Value *RHS, KnownBits &Known,
631 const SimplifyQuery &Q) {
632 if (RHS->getType()->isPointerTy()) {
633 // Handle comparison of pointer to null explicitly, as it will not be
634 // covered by the m_APInt() logic below.
635 if (LHS == V && match(RHS, m_Zero())) {
636 switch (Pred) {
637 case ICmpInst::ICMP_EQ:
638 Known.setAllZero();
639 break;
640 case ICmpInst::ICMP_SGE:
641 case ICmpInst::ICMP_SGT:
642 Known.makeNonNegative();
643 break;
644 case ICmpInst::ICMP_SLT:
645 Known.makeNegative();
646 break;
647 default:
648 break;
649 }
650 }
651 return;
652 }
653
654 unsigned BitWidth = Known.getBitWidth();
655 auto m_V =
657
658 Value *Y;
659 const APInt *Mask, *C;
660 uint64_t ShAmt;
661 switch (Pred) {
662 case ICmpInst::ICMP_EQ:
663 // assume(V = C)
664 if (match(LHS, m_V) && match(RHS, m_APInt(C))) {
665 Known = Known.unionWith(KnownBits::makeConstant(*C));
666 // assume(V & Mask = C)
667 } else if (match(LHS, m_c_And(m_V, m_Value(Y))) &&
668 match(RHS, m_APInt(C))) {
669 // For one bits in Mask, we can propagate bits from C to V.
670 Known.One |= *C;
671 if (match(Y, m_APInt(Mask)))
672 Known.Zero |= ~*C & *Mask;
673 // assume(V | Mask = C)
674 } else if (match(LHS, m_c_Or(m_V, m_Value(Y))) && match(RHS, m_APInt(C))) {
675 // For zero bits in Mask, we can propagate bits from C to V.
676 Known.Zero |= ~*C;
677 if (match(Y, m_APInt(Mask)))
678 Known.One |= *C & ~*Mask;
679 // assume(V ^ Mask = C)
680 } else if (match(LHS, m_Xor(m_V, m_APInt(Mask))) &&
681 match(RHS, m_APInt(C))) {
682 // Equivalent to assume(V == Mask ^ C)
683 Known = Known.unionWith(KnownBits::makeConstant(*C ^ *Mask));
684 // assume(V << ShAmt = C)
685 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
686 match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
687 // For those bits in C that are known, we can propagate them to known
688 // bits in V shifted to the right by ShAmt.
690 RHSKnown.Zero.lshrInPlace(ShAmt);
691 RHSKnown.One.lshrInPlace(ShAmt);
692 Known = Known.unionWith(RHSKnown);
693 // assume(V >> ShAmt = C)
694 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
695 match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
697 // For those bits in RHS that are known, we can propagate them to known
698 // bits in V shifted to the right by C.
699 Known.Zero |= RHSKnown.Zero << ShAmt;
700 Known.One |= RHSKnown.One << ShAmt;
701 }
702 break;
703 case ICmpInst::ICMP_NE: {
704 // assume (V & B != 0) where B is a power of 2
705 const APInt *BPow2;
706 if (match(LHS, m_And(m_V, m_Power2(BPow2))) && match(RHS, m_Zero()))
707 Known.One |= *BPow2;
708 break;
709 }
710 default:
711 if (match(RHS, m_APInt(C))) {
712 const APInt *Offset = nullptr;
713 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
715 if (Offset)
716 LHSRange = LHSRange.sub(*Offset);
717 Known = Known.unionWith(LHSRange.toKnownBits());
718 }
719 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
720 // X & Y u> C -> X u> C && Y u> C
721 // X nuw- Y u> C -> X u> C
722 if (match(LHS, m_c_And(m_V, m_Value())) ||
723 match(LHS, m_NUWSub(m_V, m_Value())))
724 Known.One.setHighBits(
725 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
726 }
727 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
728 // X | Y u< C -> X u< C && Y u< C
729 // X nuw+ Y u< C -> X u< C && Y u< C
730 if (match(LHS, m_c_Or(m_V, m_Value())) ||
731 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
732 Known.Zero.setHighBits(
733 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
734 }
735 }
736 }
737 break;
738 }
739}
740
741static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
742 KnownBits &Known,
743 const SimplifyQuery &SQ, bool Invert) {
745 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
746 Value *LHS = Cmp->getOperand(0);
747 Value *RHS = Cmp->getOperand(1);
748
749 // Handle icmp pred (trunc V), C
750 if (match(LHS, m_Trunc(m_Specific(V)))) {
752 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
753 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
754 return;
755 }
756
757 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
758}
759
761 KnownBits &Known, unsigned Depth,
762 const SimplifyQuery &SQ, bool Invert) {
763 Value *A, *B;
766 KnownBits Known2(Known.getBitWidth());
767 KnownBits Known3(Known.getBitWidth());
768 computeKnownBitsFromCond(V, A, Known2, Depth + 1, SQ, Invert);
769 computeKnownBitsFromCond(V, B, Known3, Depth + 1, SQ, Invert);
770 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
772 Known2 = Known2.unionWith(Known3);
773 else
774 Known2 = Known2.intersectWith(Known3);
775 Known = Known.unionWith(Known2);
776 }
777
778 if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
779 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
780}
781
783 unsigned Depth, const SimplifyQuery &Q) {
784 // Handle injected condition.
785 if (Q.CC && Q.CC->AffectedValues.contains(V))
786 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Depth, Q, Q.CC->Invert);
787
788 if (!Q.CxtI)
789 return;
790
791 if (Q.DC && Q.DT) {
792 // Handle dominating conditions.
793 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
794 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
795 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
796 computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
797 /*Invert*/ false);
798
799 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
800 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
801 computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
802 /*Invert*/ true);
803 }
804
805 if (Known.hasConflict())
806 Known.resetAll();
807 }
808
809 if (!Q.AC)
810 return;
811
812 unsigned BitWidth = Known.getBitWidth();
813
814 // Note that the patterns below need to be kept in sync with the code
815 // in AssumptionCache::updateAffectedValues.
816
817 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
818 if (!Elem.Assume)
819 continue;
820
821 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
822 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
823 "Got assumption for the wrong function!");
824
825 if (Elem.Index != AssumptionCache::ExprResultIdx) {
826 if (!V->getType()->isPointerTy())
827 continue;
829 *I, I->bundle_op_info_begin()[Elem.Index])) {
830 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
831 // be the producer of the pointer in the bundle. At the moment, align
832 // assumptions aren't optimized away.
833 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
834 isPowerOf2_64(RK.ArgValue) &&
835 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
836 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
837 }
838 continue;
839 }
840
841 // Warning: This loop can end up being somewhat performance sensitive.
842 // We're running this loop for once for each value queried resulting in a
843 // runtime of ~O(#assumes * #values).
844
845 Value *Arg = I->getArgOperand(0);
846
847 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
848 assert(BitWidth == 1 && "assume operand is not i1?");
849 (void)BitWidth;
850 Known.setAllOnes();
851 return;
852 }
853 if (match(Arg, m_Not(m_Specific(V))) &&
855 assert(BitWidth == 1 && "assume operand is not i1?");
856 (void)BitWidth;
857 Known.setAllZero();
858 return;
859 }
860
861 // The remaining tests are all recursive, so bail out if we hit the limit.
863 continue;
864
865 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
866 if (!Cmp)
867 continue;
868
869 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
870 continue;
871
872 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
873 }
874
875 // Conflicting assumption: Undefined behavior will occur on this execution
876 // path.
877 if (Known.hasConflict())
878 Known.resetAll();
879}
880
881/// Compute known bits from a shift operator, including those with a
882/// non-constant shift amount. Known is the output of this function. Known2 is a
883/// pre-allocated temporary with the same bit width as Known and on return
884/// contains the known bit of the shift value source. KF is an
885/// operator-specific function that, given the known-bits and a shift amount,
886/// compute the implied known-bits of the shift operator's result respectively
887/// for that shift amount. The results from calling KF are conservatively
888/// combined for all permitted shift amounts.
890 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
891 KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q,
892 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
893 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
894 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
895 // To limit compile-time impact, only query isKnownNonZero() if we know at
896 // least something about the shift amount.
897 bool ShAmtNonZero =
898 Known.isNonZero() ||
899 (Known.getMaxValue().ult(Known.getBitWidth()) &&
900 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
901 Known = KF(Known2, Known, ShAmtNonZero);
902}
903
904static KnownBits
905getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
906 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
907 unsigned Depth, const SimplifyQuery &Q) {
908 unsigned BitWidth = KnownLHS.getBitWidth();
909 KnownBits KnownOut(BitWidth);
910 bool IsAnd = false;
911 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
912 Value *X = nullptr, *Y = nullptr;
913
914 switch (I->getOpcode()) {
915 case Instruction::And:
916 KnownOut = KnownLHS & KnownRHS;
917 IsAnd = true;
918 // and(x, -x) is common idioms that will clear all but lowest set
919 // bit. If we have a single known bit in x, we can clear all bits
920 // above it.
921 // TODO: instcombine often reassociates independent `and` which can hide
922 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
923 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
924 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
925 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
926 KnownOut = KnownLHS.blsi();
927 else
928 KnownOut = KnownRHS.blsi();
929 }
930 break;
931 case Instruction::Or:
932 KnownOut = KnownLHS | KnownRHS;
933 break;
934 case Instruction::Xor:
935 KnownOut = KnownLHS ^ KnownRHS;
936 // xor(x, x-1) is common idioms that will clear all but lowest set
937 // bit. If we have a single known bit in x, we can clear all bits
938 // above it.
939 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
940 // -1 but for the purpose of demanded bits (xor(x, x-C) &
941 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
942 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
943 if (HasKnownOne &&
945 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
946 KnownOut = XBits.blsmsk();
947 }
948 break;
949 default:
950 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
951 }
952
953 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
954 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
955 // here we handle the more general case of adding any odd number by
956 // matching the form and/xor/or(x, add(x, y)) where y is odd.
957 // TODO: This could be generalized to clearing any bit set in y where the
958 // following bit is known to be unset in y.
959 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
963 KnownBits KnownY(BitWidth);
964 computeKnownBits(Y, DemandedElts, KnownY, Depth + 1, Q);
965 if (KnownY.countMinTrailingOnes() > 0) {
966 if (IsAnd)
967 KnownOut.Zero.setBit(0);
968 else
969 KnownOut.One.setBit(0);
970 }
971 }
972 return KnownOut;
973}
974
976 const Operator *I, const APInt &DemandedElts, unsigned Depth,
977 const SimplifyQuery &Q,
978 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
979 KnownBitsFunc) {
980 APInt DemandedEltsLHS, DemandedEltsRHS;
982 DemandedElts, DemandedEltsLHS,
983 DemandedEltsRHS);
984
985 const auto ComputeForSingleOpFunc =
986 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
987 return KnownBitsFunc(
988 computeKnownBits(Op, DemandedEltsOp, Depth + 1, Q),
989 computeKnownBits(Op, DemandedEltsOp << 1, Depth + 1, Q));
990 };
991
992 if (DemandedEltsRHS.isZero())
993 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
994 if (DemandedEltsLHS.isZero())
995 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
996
997 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
998 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
999}
1000
1001// Public so this can be used in `SimplifyDemandedUseBits`.
1003 const KnownBits &KnownLHS,
1004 const KnownBits &KnownRHS,
1005 unsigned Depth,
1006 const SimplifyQuery &SQ) {
1007 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1008 APInt DemandedElts =
1009 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1010
1011 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, Depth,
1012 SQ);
1013}
1014
1016 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1017 // Without vscale_range, we only know that vscale is non-zero.
1018 if (!Attr.isValid())
1020
1021 unsigned AttrMin = Attr.getVScaleRangeMin();
1022 // Minimum is larger than vscale width, result is always poison.
1023 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1024 return ConstantRange::getEmpty(BitWidth);
1025
1026 APInt Min(BitWidth, AttrMin);
1027 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1028 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1030
1031 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1032}
1033
1035 Value *Arm, bool Invert, unsigned Depth,
1036 const SimplifyQuery &Q) {
1037 // If we have a constant arm, we are done.
1038 if (Known.isConstant())
1039 return;
1040
1041 // See what condition implies about the bits of the select arm.
1042 KnownBits CondRes(Known.getBitWidth());
1043 computeKnownBitsFromCond(Arm, Cond, CondRes, Depth + 1, Q, Invert);
1044 // If we don't get any information from the condition, no reason to
1045 // proceed.
1046 if (CondRes.isUnknown())
1047 return;
1048
1049 // We can have conflict if the condition is dead. I.e if we have
1050 // (x | 64) < 32 ? (x | 64) : y
1051 // we will have conflict at bit 6 from the condition/the `or`.
1052 // In that case just return. Its not particularly important
1053 // what we do, as this select is going to be simplified soon.
1054 CondRes = CondRes.unionWith(Known);
1055 if (CondRes.hasConflict())
1056 return;
1057
1058 // Finally make sure the information we found is valid. This is relatively
1059 // expensive so it's left for the very end.
1060 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1061 return;
1062
1063 // Finally, we know we get information from the condition and its valid,
1064 // so return it.
1065 Known = CondRes;
1066}
1067
1069 const APInt &DemandedElts,
1070 KnownBits &Known, unsigned Depth,
1071 const SimplifyQuery &Q) {
1072 unsigned BitWidth = Known.getBitWidth();
1073
1074 KnownBits Known2(BitWidth);
1075 switch (I->getOpcode()) {
1076 default: break;
1077 case Instruction::Load:
1078 if (MDNode *MD =
1079 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1081 break;
1082 case Instruction::And:
1083 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1084 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1085
1086 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1087 break;
1088 case Instruction::Or:
1089 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1090 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1091
1092 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1093 break;
1094 case Instruction::Xor:
1095 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1096 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1097
1098 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1099 break;
1100 case Instruction::Mul: {
1101 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1102 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1103 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1104 DemandedElts, Known, Known2, Depth, Q);
1105 break;
1106 }
1107 case Instruction::UDiv: {
1108 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1109 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1110 Known =
1111 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1112 break;
1113 }
1114 case Instruction::SDiv: {
1115 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1116 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1117 Known =
1118 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1119 break;
1120 }
1121 case Instruction::Select: {
1122 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1123 KnownBits Res(Known.getBitWidth());
1124 computeKnownBits(Arm, DemandedElts, Res, Depth + 1, Q);
1125 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Depth, Q);
1126 return Res;
1127 };
1128 // Only known if known in both the LHS and RHS.
1129 Known =
1130 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1131 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1132 break;
1133 }
1134 case Instruction::FPTrunc:
1135 case Instruction::FPExt:
1136 case Instruction::FPToUI:
1137 case Instruction::FPToSI:
1138 case Instruction::SIToFP:
1139 case Instruction::UIToFP:
1140 break; // Can't work with floating point.
1141 case Instruction::PtrToInt:
1142 case Instruction::IntToPtr:
1143 // Fall through and handle them the same as zext/trunc.
1144 [[fallthrough]];
1145 case Instruction::ZExt:
1146 case Instruction::Trunc: {
1147 Type *SrcTy = I->getOperand(0)->getType();
1148
1149 unsigned SrcBitWidth;
1150 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1151 // which fall through here.
1152 Type *ScalarTy = SrcTy->getScalarType();
1153 SrcBitWidth = ScalarTy->isPointerTy() ?
1154 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1155 Q.DL.getTypeSizeInBits(ScalarTy);
1156
1157 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1158 Known = Known.anyextOrTrunc(SrcBitWidth);
1159 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1160 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1161 Inst && Inst->hasNonNeg() && !Known.isNegative())
1162 Known.makeNonNegative();
1163 Known = Known.zextOrTrunc(BitWidth);
1164 break;
1165 }
1166 case Instruction::BitCast: {
1167 Type *SrcTy = I->getOperand(0)->getType();
1168 if (SrcTy->isIntOrPtrTy() &&
1169 // TODO: For now, not handling conversions like:
1170 // (bitcast i64 %x to <2 x i32>)
1171 !I->getType()->isVectorTy()) {
1172 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1173 break;
1174 }
1175
1176 const Value *V;
1177 // Handle bitcast from floating point to integer.
1178 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1179 V->getType()->isFPOrFPVectorTy()) {
1180 Type *FPType = V->getType()->getScalarType();
1181 KnownFPClass Result =
1182 computeKnownFPClass(V, DemandedElts, fcAllFlags, Depth + 1, Q);
1183 FPClassTest FPClasses = Result.KnownFPClasses;
1184
1185 // TODO: Treat it as zero/poison if the use of I is unreachable.
1186 if (FPClasses == fcNone)
1187 break;
1188
1189 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1190 Known.Zero.setAllBits();
1191 Known.One.setAllBits();
1192
1193 if (FPClasses & fcInf)
1195 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1196
1197 if (FPClasses & fcZero)
1199 APInt::getZero(FPType->getScalarSizeInBits())));
1200
1201 Known.Zero.clearSignBit();
1202 Known.One.clearSignBit();
1203 }
1204
1205 if (Result.SignBit) {
1206 if (*Result.SignBit)
1207 Known.makeNegative();
1208 else
1209 Known.makeNonNegative();
1210 }
1211
1212 break;
1213 }
1214
1215 // Handle cast from vector integer type to scalar or vector integer.
1216 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1217 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1218 !I->getType()->isIntOrIntVectorTy() ||
1219 isa<ScalableVectorType>(I->getType()))
1220 break;
1221
1222 // Look through a cast from narrow vector elements to wider type.
1223 // Examples: v4i32 -> v2i64, v3i8 -> v24
1224 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1225 if (BitWidth % SubBitWidth == 0) {
1226 // Known bits are automatically intersected across demanded elements of a
1227 // vector. So for example, if a bit is computed as known zero, it must be
1228 // zero across all demanded elements of the vector.
1229 //
1230 // For this bitcast, each demanded element of the output is sub-divided
1231 // across a set of smaller vector elements in the source vector. To get
1232 // the known bits for an entire element of the output, compute the known
1233 // bits for each sub-element sequentially. This is done by shifting the
1234 // one-set-bit demanded elements parameter across the sub-elements for
1235 // consecutive calls to computeKnownBits. We are using the demanded
1236 // elements parameter as a mask operator.
1237 //
1238 // The known bits of each sub-element are then inserted into place
1239 // (dependent on endian) to form the full result of known bits.
1240 unsigned NumElts = DemandedElts.getBitWidth();
1241 unsigned SubScale = BitWidth / SubBitWidth;
1242 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1243 for (unsigned i = 0; i != NumElts; ++i) {
1244 if (DemandedElts[i])
1245 SubDemandedElts.setBit(i * SubScale);
1246 }
1247
1248 KnownBits KnownSrc(SubBitWidth);
1249 for (unsigned i = 0; i != SubScale; ++i) {
1250 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc,
1251 Depth + 1, Q);
1252 unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i;
1253 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1254 }
1255 }
1256 break;
1257 }
1258 case Instruction::SExt: {
1259 // Compute the bits in the result that are not present in the input.
1260 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1261
1262 Known = Known.trunc(SrcBitWidth);
1263 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1264 // If the sign bit of the input is known set or clear, then we know the
1265 // top bits of the result.
1266 Known = Known.sext(BitWidth);
1267 break;
1268 }
1269 case Instruction::Shl: {
1270 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1271 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1272 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1273 bool ShAmtNonZero) {
1274 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1275 };
1276 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1277 KF);
1278 // Trailing zeros of a right-shifted constant never decrease.
1279 const APInt *C;
1280 if (match(I->getOperand(0), m_APInt(C)))
1281 Known.Zero.setLowBits(C->countr_zero());
1282 break;
1283 }
1284 case Instruction::LShr: {
1285 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1286 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1287 bool ShAmtNonZero) {
1288 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1289 };
1290 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1291 KF);
1292 // Leading zeros of a left-shifted constant never decrease.
1293 const APInt *C;
1294 if (match(I->getOperand(0), m_APInt(C)))
1295 Known.Zero.setHighBits(C->countl_zero());
1296 break;
1297 }
1298 case Instruction::AShr: {
1299 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1300 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1301 bool ShAmtNonZero) {
1302 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1303 };
1304 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1305 KF);
1306 break;
1307 }
1308 case Instruction::Sub: {
1309 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1310 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1311 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1312 DemandedElts, Known, Known2, Depth, Q);
1313 break;
1314 }
1315 case Instruction::Add: {
1316 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1317 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1318 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1319 DemandedElts, Known, Known2, Depth, Q);
1320 break;
1321 }
1322 case Instruction::SRem:
1323 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1324 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1325 Known = KnownBits::srem(Known, Known2);
1326 break;
1327
1328 case Instruction::URem:
1329 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1330 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1331 Known = KnownBits::urem(Known, Known2);
1332 break;
1333 case Instruction::Alloca:
1334 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
1335 break;
1336 case Instruction::GetElementPtr: {
1337 // Analyze all of the subscripts of this getelementptr instruction
1338 // to determine if we can prove known low zero bits.
1339 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1340 // Accumulate the constant indices in a separate variable
1341 // to minimize the number of calls to computeForAddSub.
1342 APInt AccConstIndices(BitWidth, 0, /*IsSigned*/ true);
1343
1345 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1346 // TrailZ can only become smaller, short-circuit if we hit zero.
1347 if (Known.isUnknown())
1348 break;
1349
1350 Value *Index = I->getOperand(i);
1351
1352 // Handle case when index is zero.
1353 Constant *CIndex = dyn_cast<Constant>(Index);
1354 if (CIndex && CIndex->isZeroValue())
1355 continue;
1356
1357 if (StructType *STy = GTI.getStructTypeOrNull()) {
1358 // Handle struct member offset arithmetic.
1359
1360 assert(CIndex &&
1361 "Access to structure field must be known at compile time");
1362
1363 if (CIndex->getType()->isVectorTy())
1364 Index = CIndex->getSplatValue();
1365
1366 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1367 const StructLayout *SL = Q.DL.getStructLayout(STy);
1369 AccConstIndices += Offset;
1370 continue;
1371 }
1372
1373 // Handle array index arithmetic.
1374 Type *IndexedTy = GTI.getIndexedType();
1375 if (!IndexedTy->isSized()) {
1376 Known.resetAll();
1377 break;
1378 }
1379
1380 unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits();
1381 KnownBits IndexBits(IndexBitWidth);
1382 computeKnownBits(Index, IndexBits, Depth + 1, Q);
1383 TypeSize IndexTypeSize = GTI.getSequentialElementStride(Q.DL);
1384 uint64_t TypeSizeInBytes = IndexTypeSize.getKnownMinValue();
1385 KnownBits ScalingFactor(IndexBitWidth);
1386 // Multiply by current sizeof type.
1387 // &A[i] == A + i * sizeof(*A[i]).
1388 if (IndexTypeSize.isScalable()) {
1389 // For scalable types the only thing we know about sizeof is
1390 // that this is a multiple of the minimum size.
1391 ScalingFactor.Zero.setLowBits(llvm::countr_zero(TypeSizeInBytes));
1392 } else if (IndexBits.isConstant()) {
1393 APInt IndexConst = IndexBits.getConstant();
1394 APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes);
1395 IndexConst *= ScalingFactor;
1396 AccConstIndices += IndexConst.sextOrTrunc(BitWidth);
1397 continue;
1398 } else {
1399 ScalingFactor =
1400 KnownBits::makeConstant(APInt(IndexBitWidth, TypeSizeInBytes));
1401 }
1402 IndexBits = KnownBits::mul(IndexBits, ScalingFactor);
1403
1404 // If the offsets have a different width from the pointer, according
1405 // to the language reference we need to sign-extend or truncate them
1406 // to the width of the pointer.
1407 IndexBits = IndexBits.sextOrTrunc(BitWidth);
1408
1409 // Note that inbounds does *not* guarantee nsw for the addition, as only
1410 // the offset is signed, while the base address is unsigned.
1411 Known = KnownBits::add(Known, IndexBits);
1412 }
1413 if (!Known.isUnknown() && !AccConstIndices.isZero()) {
1414 KnownBits Index = KnownBits::makeConstant(AccConstIndices);
1415 Known = KnownBits::add(Known, Index);
1416 }
1417 break;
1418 }
1419 case Instruction::PHI: {
1420 const PHINode *P = cast<PHINode>(I);
1421 BinaryOperator *BO = nullptr;
1422 Value *R = nullptr, *L = nullptr;
1423 if (matchSimpleRecurrence(P, BO, R, L)) {
1424 // Handle the case of a simple two-predecessor recurrence PHI.
1425 // There's a lot more that could theoretically be done here, but
1426 // this is sufficient to catch some interesting cases.
1427 unsigned Opcode = BO->getOpcode();
1428
1429 switch (Opcode) {
1430 // If this is a shift recurrence, we know the bits being shifted in. We
1431 // can combine that with information about the start value of the
1432 // recurrence to conclude facts about the result. If this is a udiv
1433 // recurrence, we know that the result can never exceed either the
1434 // numerator or the start value, whichever is greater.
1435 case Instruction::LShr:
1436 case Instruction::AShr:
1437 case Instruction::Shl:
1438 case Instruction::UDiv:
1439 if (BO->getOperand(0) != I)
1440 break;
1441 [[fallthrough]];
1442
1443 // For a urem recurrence, the result can never exceed the start value. The
1444 // phi could either be the numerator or the denominator.
1445 case Instruction::URem: {
1446 // We have matched a recurrence of the form:
1447 // %iv = [R, %entry], [%iv.next, %backedge]
1448 // %iv.next = shift_op %iv, L
1449
1450 // Recurse with the phi context to avoid concern about whether facts
1451 // inferred hold at original context instruction. TODO: It may be
1452 // correct to use the original context. IF warranted, explore and
1453 // add sufficient tests to cover.
1455 RecQ.CxtI = P;
1456 computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1457 switch (Opcode) {
1458 case Instruction::Shl:
1459 // A shl recurrence will only increase the tailing zeros
1460 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1461 break;
1462 case Instruction::LShr:
1463 case Instruction::UDiv:
1464 case Instruction::URem:
1465 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1466 // the start value.
1467 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1468 break;
1469 case Instruction::AShr:
1470 // An ashr recurrence will extend the initial sign bit
1471 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1472 Known.One.setHighBits(Known2.countMinLeadingOnes());
1473 break;
1474 }
1475 break;
1476 }
1477
1478 // Check for operations that have the property that if
1479 // both their operands have low zero bits, the result
1480 // will have low zero bits.
1481 case Instruction::Add:
1482 case Instruction::Sub:
1483 case Instruction::And:
1484 case Instruction::Or:
1485 case Instruction::Mul: {
1486 // Change the context instruction to the "edge" that flows into the
1487 // phi. This is important because that is where the value is actually
1488 // "evaluated" even though it is used later somewhere else. (see also
1489 // D69571).
1491
1492 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1493 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1494 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1495
1496 // Ok, we have a PHI of the form L op= R. Check for low
1497 // zero bits.
1498 RecQ.CxtI = RInst;
1499 computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1500
1501 // We need to take the minimum number of known bits
1502 KnownBits Known3(BitWidth);
1503 RecQ.CxtI = LInst;
1504 computeKnownBits(L, DemandedElts, Known3, Depth + 1, RecQ);
1505
1506 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1507 Known3.countMinTrailingZeros()));
1508
1509 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1510 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1511 break;
1512
1513 switch (Opcode) {
1514 // If initial value of recurrence is nonnegative, and we are adding
1515 // a nonnegative number with nsw, the result can only be nonnegative
1516 // or poison value regardless of the number of times we execute the
1517 // add in phi recurrence. If initial value is negative and we are
1518 // adding a negative number with nsw, the result can only be
1519 // negative or poison value. Similar arguments apply to sub and mul.
1520 //
1521 // (add non-negative, non-negative) --> non-negative
1522 // (add negative, negative) --> negative
1523 case Instruction::Add: {
1524 if (Known2.isNonNegative() && Known3.isNonNegative())
1525 Known.makeNonNegative();
1526 else if (Known2.isNegative() && Known3.isNegative())
1527 Known.makeNegative();
1528 break;
1529 }
1530
1531 // (sub nsw non-negative, negative) --> non-negative
1532 // (sub nsw negative, non-negative) --> negative
1533 case Instruction::Sub: {
1534 if (BO->getOperand(0) != I)
1535 break;
1536 if (Known2.isNonNegative() && Known3.isNegative())
1537 Known.makeNonNegative();
1538 else if (Known2.isNegative() && Known3.isNonNegative())
1539 Known.makeNegative();
1540 break;
1541 }
1542
1543 // (mul nsw non-negative, non-negative) --> non-negative
1544 case Instruction::Mul:
1545 if (Known2.isNonNegative() && Known3.isNonNegative())
1546 Known.makeNonNegative();
1547 break;
1548
1549 default:
1550 break;
1551 }
1552 break;
1553 }
1554
1555 default:
1556 break;
1557 }
1558 }
1559
1560 // Unreachable blocks may have zero-operand PHI nodes.
1561 if (P->getNumIncomingValues() == 0)
1562 break;
1563
1564 // Otherwise take the unions of the known bit sets of the operands,
1565 // taking conservative care to avoid excessive recursion.
1566 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1567 // Skip if every incoming value references to ourself.
1568 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1569 break;
1570
1571 Known.Zero.setAllBits();
1572 Known.One.setAllBits();
1573 for (unsigned u = 0, e = P->getNumIncomingValues(); u < e; ++u) {
1574 Value *IncValue = P->getIncomingValue(u);
1575 // Skip direct self references.
1576 if (IncValue == P) continue;
1577
1578 // If the Use is a select of this phi, use the knownbit of the other
1579 // operand to break the recursion.
1580 if (auto *SI = dyn_cast<SelectInst>(IncValue)) {
1581 if (SI->getTrueValue() == P || SI->getFalseValue() == P)
1582 IncValue = SI->getTrueValue() == P ? SI->getFalseValue()
1583 : SI->getTrueValue();
1584 }
1585
1586 // Change the context instruction to the "edge" that flows into the
1587 // phi. This is important because that is where the value is actually
1588 // "evaluated" even though it is used later somewhere else. (see also
1589 // D69571).
1591 RecQ.CxtI = P->getIncomingBlock(u)->getTerminator();
1592
1593 Known2 = KnownBits(BitWidth);
1594
1595 // Recurse, but cap the recursion to one level, because we don't
1596 // want to waste time spinning around in loops.
1597 // TODO: See if we can base recursion limiter on number of incoming phi
1598 // edges so we don't overly clamp analysis.
1599 computeKnownBits(IncValue, DemandedElts, Known2,
1600 MaxAnalysisRecursionDepth - 1, RecQ);
1601
1602 // See if we can further use a conditional branch into the phi
1603 // to help us determine the range of the value.
1604 if (!Known2.isConstant()) {
1605 CmpPredicate Pred;
1606 const APInt *RHSC;
1607 BasicBlock *TrueSucc, *FalseSucc;
1608 // TODO: Use RHS Value and compute range from its known bits.
1609 if (match(RecQ.CxtI,
1610 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1611 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1612 // Check for cases of duplicate successors.
1613 if ((TrueSucc == P->getParent()) != (FalseSucc == P->getParent())) {
1614 // If we're using the false successor, invert the predicate.
1615 if (FalseSucc == P->getParent())
1616 Pred = CmpInst::getInversePredicate(Pred);
1617 // Get the knownbits implied by the incoming phi condition.
1618 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1619 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1620 // We can have conflicts here if we are analyzing deadcode (its
1621 // impossible for us reach this BB based the icmp).
1622 if (KnownUnion.hasConflict()) {
1623 // No reason to continue analyzing in a known dead region, so
1624 // just resetAll and break. This will cause us to also exit the
1625 // outer loop.
1626 Known.resetAll();
1627 break;
1628 }
1629 Known2 = KnownUnion;
1630 }
1631 }
1632 }
1633
1634 Known = Known.intersectWith(Known2);
1635 // If all bits have been ruled out, there's no need to check
1636 // more operands.
1637 if (Known.isUnknown())
1638 break;
1639 }
1640 }
1641 break;
1642 }
1643 case Instruction::Call:
1644 case Instruction::Invoke: {
1645 // If range metadata is attached to this call, set known bits from that,
1646 // and then intersect with known bits based on other properties of the
1647 // function.
1648 if (MDNode *MD =
1649 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1651
1652 const auto *CB = cast<CallBase>(I);
1653
1654 if (std::optional<ConstantRange> Range = CB->getRange())
1655 Known = Known.unionWith(Range->toKnownBits());
1656
1657 if (const Value *RV = CB->getReturnedArgOperand()) {
1658 if (RV->getType() == I->getType()) {
1659 computeKnownBits(RV, Known2, Depth + 1, Q);
1660 Known = Known.unionWith(Known2);
1661 // If the function doesn't return properly for all input values
1662 // (e.g. unreachable exits) then there might be conflicts between the
1663 // argument value and the range metadata. Simply discard the known bits
1664 // in case of conflicts.
1665 if (Known.hasConflict())
1666 Known.resetAll();
1667 }
1668 }
1669 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1670 switch (II->getIntrinsicID()) {
1671 default:
1672 break;
1673 case Intrinsic::abs: {
1674 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1675 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
1676 Known = Known2.abs(IntMinIsPoison);
1677 break;
1678 }
1679 case Intrinsic::bitreverse:
1680 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1681 Known.Zero |= Known2.Zero.reverseBits();
1682 Known.One |= Known2.One.reverseBits();
1683 break;
1684 case Intrinsic::bswap:
1685 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1686 Known.Zero |= Known2.Zero.byteSwap();
1687 Known.One |= Known2.One.byteSwap();
1688 break;
1689 case Intrinsic::ctlz: {
1690 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1691 // If we have a known 1, its position is our upper bound.
1692 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
1693 // If this call is poison for 0 input, the result will be less than 2^n.
1694 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1695 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1696 unsigned LowBits = llvm::bit_width(PossibleLZ);
1697 Known.Zero.setBitsFrom(LowBits);
1698 break;
1699 }
1700 case Intrinsic::cttz: {
1701 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1702 // If we have a known 1, its position is our upper bound.
1703 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
1704 // If this call is poison for 0 input, the result will be less than 2^n.
1705 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1706 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1707 unsigned LowBits = llvm::bit_width(PossibleTZ);
1708 Known.Zero.setBitsFrom(LowBits);
1709 break;
1710 }
1711 case Intrinsic::ctpop: {
1712 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1713 // We can bound the space the count needs. Also, bits known to be zero
1714 // can't contribute to the population.
1715 unsigned BitsPossiblySet = Known2.countMaxPopulation();
1716 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
1717 Known.Zero.setBitsFrom(LowBits);
1718 // TODO: we could bound KnownOne using the lower bound on the number
1719 // of bits which might be set provided by popcnt KnownOne2.
1720 break;
1721 }
1722 case Intrinsic::fshr:
1723 case Intrinsic::fshl: {
1724 const APInt *SA;
1725 if (!match(I->getOperand(2), m_APInt(SA)))
1726 break;
1727
1728 // Normalize to funnel shift left.
1729 uint64_t ShiftAmt = SA->urem(BitWidth);
1730 if (II->getIntrinsicID() == Intrinsic::fshr)
1731 ShiftAmt = BitWidth - ShiftAmt;
1732
1733 KnownBits Known3(BitWidth);
1734 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1735 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Depth + 1, Q);
1736
1737 Known.Zero =
1738 Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
1739 Known.One =
1740 Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
1741 break;
1742 }
1743 case Intrinsic::uadd_sat:
1744 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1745 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1746 Known = KnownBits::uadd_sat(Known, Known2);
1747 break;
1748 case Intrinsic::usub_sat:
1749 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1750 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1751 Known = KnownBits::usub_sat(Known, Known2);
1752 break;
1753 case Intrinsic::sadd_sat:
1754 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1755 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1756 Known = KnownBits::sadd_sat(Known, Known2);
1757 break;
1758 case Intrinsic::ssub_sat:
1759 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1760 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1761 Known = KnownBits::ssub_sat(Known, Known2);
1762 break;
1763 // Vec reverse preserves bits from input vec.
1764 case Intrinsic::vector_reverse:
1765 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known,
1766 Depth + 1, Q);
1767 break;
1768 // for min/max/and/or reduce, any bit common to each element in the
1769 // input vec is set in the output.
1770 case Intrinsic::vector_reduce_and:
1771 case Intrinsic::vector_reduce_or:
1772 case Intrinsic::vector_reduce_umax:
1773 case Intrinsic::vector_reduce_umin:
1774 case Intrinsic::vector_reduce_smax:
1775 case Intrinsic::vector_reduce_smin:
1776 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1777 break;
1778 case Intrinsic::vector_reduce_xor: {
1779 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1780 // The zeros common to all vecs are zero in the output.
1781 // If the number of elements is odd, then the common ones remain. If the
1782 // number of elements is even, then the common ones becomes zeros.
1783 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
1784 // Even, so the ones become zeros.
1785 bool EvenCnt = VecTy->getElementCount().isKnownEven();
1786 if (EvenCnt)
1787 Known.Zero |= Known.One;
1788 // Maybe even element count so need to clear ones.
1789 if (VecTy->isScalableTy() || EvenCnt)
1790 Known.One.clearAllBits();
1791 break;
1792 }
1793 case Intrinsic::umin:
1794 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1795 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1796 Known = KnownBits::umin(Known, Known2);
1797 break;
1798 case Intrinsic::umax:
1799 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1800 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1801 Known = KnownBits::umax(Known, Known2);
1802 break;
1803 case Intrinsic::smin:
1804 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1805 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1806 Known = KnownBits::smin(Known, Known2);
1807 break;
1808 case Intrinsic::smax:
1809 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1810 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1811 Known = KnownBits::smax(Known, Known2);
1812 break;
1813 case Intrinsic::ptrmask: {
1814 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1815
1816 const Value *Mask = I->getOperand(1);
1817 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
1818 computeKnownBits(Mask, DemandedElts, Known2, Depth + 1, Q);
1819 // TODO: 1-extend would be more precise.
1820 Known &= Known2.anyextOrTrunc(BitWidth);
1821 break;
1822 }
1823 case Intrinsic::x86_sse2_pmulh_w:
1824 case Intrinsic::x86_avx2_pmulh_w:
1825 case Intrinsic::x86_avx512_pmulh_w_512:
1826 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1827 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1828 Known = KnownBits::mulhs(Known, Known2);
1829 break;
1830 case Intrinsic::x86_sse2_pmulhu_w:
1831 case Intrinsic::x86_avx2_pmulhu_w:
1832 case Intrinsic::x86_avx512_pmulhu_w_512:
1833 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1834 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1835 Known = KnownBits::mulhu(Known, Known2);
1836 break;
1837 case Intrinsic::x86_sse42_crc32_64_64:
1838 Known.Zero.setBitsFrom(32);
1839 break;
1840 case Intrinsic::x86_ssse3_phadd_d_128:
1841 case Intrinsic::x86_ssse3_phadd_w_128:
1842 case Intrinsic::x86_avx2_phadd_d:
1843 case Intrinsic::x86_avx2_phadd_w: {
1845 I, DemandedElts, Depth, Q,
1846 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
1847 return KnownBits::add(KnownLHS, KnownRHS);
1848 });
1849 break;
1850 }
1851 case Intrinsic::x86_ssse3_phadd_sw_128:
1852 case Intrinsic::x86_avx2_phadd_sw: {
1853 Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
1855 break;
1856 }
1857 case Intrinsic::x86_ssse3_phsub_d_128:
1858 case Intrinsic::x86_ssse3_phsub_w_128:
1859 case Intrinsic::x86_avx2_phsub_d:
1860 case Intrinsic::x86_avx2_phsub_w: {
1862 I, DemandedElts, Depth, Q,
1863 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
1864 return KnownBits::sub(KnownLHS, KnownRHS);
1865 });
1866 break;
1867 }
1868 case Intrinsic::x86_ssse3_phsub_sw_128:
1869 case Intrinsic::x86_avx2_phsub_sw: {
1870 Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
1872 break;
1873 }
1874 case Intrinsic::riscv_vsetvli:
1875 case Intrinsic::riscv_vsetvlimax: {
1876 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
1877 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
1879 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
1880 RISCVII::VLMUL VLMUL = static_cast<RISCVII::VLMUL>(
1881 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
1882 uint64_t MaxVLEN =
1884 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
1885
1886 // Result of vsetvli must be not larger than AVL.
1887 if (HasAVL)
1888 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
1889 MaxVL = std::min(MaxVL, CI->getZExtValue());
1890
1891 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
1892 if (BitWidth > KnownZeroFirstBit)
1893 Known.Zero.setBitsFrom(KnownZeroFirstBit);
1894 break;
1895 }
1896 case Intrinsic::vscale: {
1897 if (!II->getParent() || !II->getFunction())
1898 break;
1899
1900 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
1901 break;
1902 }
1903 }
1904 }
1905 break;
1906 }
1907 case Instruction::ShuffleVector: {
1908 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
1909 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
1910 if (!Shuf) {
1911 Known.resetAll();
1912 return;
1913 }
1914 // For undef elements, we don't know anything about the common state of
1915 // the shuffle result.
1916 APInt DemandedLHS, DemandedRHS;
1917 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
1918 Known.resetAll();
1919 return;
1920 }
1921 Known.One.setAllBits();
1922 Known.Zero.setAllBits();
1923 if (!!DemandedLHS) {
1924 const Value *LHS = Shuf->getOperand(0);
1925 computeKnownBits(LHS, DemandedLHS, Known, Depth + 1, Q);
1926 // If we don't know any bits, early out.
1927 if (Known.isUnknown())
1928 break;
1929 }
1930 if (!!DemandedRHS) {
1931 const Value *RHS = Shuf->getOperand(1);
1932 computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q);
1933 Known = Known.intersectWith(Known2);
1934 }
1935 break;
1936 }
1937 case Instruction::InsertElement: {
1938 if (isa<ScalableVectorType>(I->getType())) {
1939 Known.resetAll();
1940 return;
1941 }
1942 const Value *Vec = I->getOperand(0);
1943 const Value *Elt = I->getOperand(1);
1944 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
1945 unsigned NumElts = DemandedElts.getBitWidth();
1946 APInt DemandedVecElts = DemandedElts;
1947 bool NeedsElt = true;
1948 // If we know the index we are inserting too, clear it from Vec check.
1949 if (CIdx && CIdx->getValue().ult(NumElts)) {
1950 DemandedVecElts.clearBit(CIdx->getZExtValue());
1951 NeedsElt = DemandedElts[CIdx->getZExtValue()];
1952 }
1953
1954 Known.One.setAllBits();
1955 Known.Zero.setAllBits();
1956 if (NeedsElt) {
1957 computeKnownBits(Elt, Known, Depth + 1, Q);
1958 // If we don't know any bits, early out.
1959 if (Known.isUnknown())
1960 break;
1961 }
1962
1963 if (!DemandedVecElts.isZero()) {
1964 computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q);
1965 Known = Known.intersectWith(Known2);
1966 }
1967 break;
1968 }
1969 case Instruction::ExtractElement: {
1970 // Look through extract element. If the index is non-constant or
1971 // out-of-range demand all elements, otherwise just the extracted element.
1972 const Value *Vec = I->getOperand(0);
1973 const Value *Idx = I->getOperand(1);
1974 auto *CIdx = dyn_cast<ConstantInt>(Idx);
1975 if (isa<ScalableVectorType>(Vec->getType())) {
1976 // FIXME: there's probably *something* we can do with scalable vectors
1977 Known.resetAll();
1978 break;
1979 }
1980 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
1981 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
1982 if (CIdx && CIdx->getValue().ult(NumElts))
1983 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
1984 computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q);
1985 break;
1986 }
1987 case Instruction::ExtractValue:
1988 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
1989 const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
1990 if (EVI->getNumIndices() != 1) break;
1991 if (EVI->getIndices()[0] == 0) {
1992 switch (II->getIntrinsicID()) {
1993 default: break;
1994 case Intrinsic::uadd_with_overflow:
1995 case Intrinsic::sadd_with_overflow:
1997 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
1998 /* NUW=*/false, DemandedElts, Known, Known2, Depth, Q);
1999 break;
2000 case Intrinsic::usub_with_overflow:
2001 case Intrinsic::ssub_with_overflow:
2003 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2004 /* NUW=*/false, DemandedElts, Known, Known2, Depth, Q);
2005 break;
2006 case Intrinsic::umul_with_overflow:
2007 case Intrinsic::smul_with_overflow:
2008 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2009 false, DemandedElts, Known, Known2, Depth, Q);
2010 break;
2011 }
2012 }
2013 }
2014 break;
2015 case Instruction::Freeze:
2016 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2017 Depth + 1))
2018 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
2019 break;
2020 }
2021}
2022
2023/// Determine which bits of V are known to be either zero or one and return
2024/// them.
2025KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2026 unsigned Depth, const SimplifyQuery &Q) {
2027 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2028 ::computeKnownBits(V, DemandedElts, Known, Depth, Q);
2029 return Known;
2030}
2031
2032/// Determine which bits of V are known to be either zero or one and return
2033/// them.
2035 const SimplifyQuery &Q) {
2036 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2037 computeKnownBits(V, Known, Depth, Q);
2038 return Known;
2039}
2040
2041/// Determine which bits of V are known to be either zero or one and return
2042/// them in the Known bit set.
2043///
2044/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2045/// we cannot optimize based on the assumption that it is zero without changing
2046/// it to be an explicit zero. If we don't change it to zero, other code could
2047/// optimized based on the contradictory assumption that it is non-zero.
2048/// Because instcombine aggressively folds operations with undef args anyway,
2049/// this won't lose us code quality.
2050///
2051/// This function is defined on values with integer type, values with pointer
2052/// type, and vectors of integers. In the case
2053/// where V is a vector, known zero, and known one values are the
2054/// same width as the vector element, and the bit is set only if it is true
2055/// for all of the demanded elements in the vector specified by DemandedElts.
2056void computeKnownBits(const Value *V, const APInt &DemandedElts,
2057 KnownBits &Known, unsigned Depth,
2058 const SimplifyQuery &Q) {
2059 if (!DemandedElts) {
2060 // No demanded elts, better to assume we don't know anything.
2061 Known.resetAll();
2062 return;
2063 }
2064
2065 assert(V && "No Value?");
2066 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2067
2068#ifndef NDEBUG
2069 Type *Ty = V->getType();
2070 unsigned BitWidth = Known.getBitWidth();
2071
2073 "Not integer or pointer type!");
2074
2075 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2076 assert(
2077 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2078 "DemandedElt width should equal the fixed vector number of elements");
2079 } else {
2080 assert(DemandedElts == APInt(1, 1) &&
2081 "DemandedElt width should be 1 for scalars or scalable vectors");
2082 }
2083
2084 Type *ScalarTy = Ty->getScalarType();
2085 if (ScalarTy->isPointerTy()) {
2086 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2087 "V and Known should have same BitWidth");
2088 } else {
2089 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2090 "V and Known should have same BitWidth");
2091 }
2092#endif
2093
2094 const APInt *C;
2095 if (match(V, m_APInt(C))) {
2096 // We know all of the bits for a scalar constant or a splat vector constant!
2097 Known = KnownBits::makeConstant(*C);
2098 return;
2099 }
2100 // Null and aggregate-zero are all-zeros.
2101 if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
2102 Known.setAllZero();
2103 return;
2104 }
2105 // Handle a constant vector by taking the intersection of the known bits of
2106 // each element.
2107 if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) {
2108 assert(!isa<ScalableVectorType>(V->getType()));
2109 // We know that CDV must be a vector of integers. Take the intersection of
2110 // each element.
2111 Known.Zero.setAllBits(); Known.One.setAllBits();
2112 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2113 if (!DemandedElts[i])
2114 continue;
2115 APInt Elt = CDV->getElementAsAPInt(i);
2116 Known.Zero &= ~Elt;
2117 Known.One &= Elt;
2118 }
2119 if (Known.hasConflict())
2120 Known.resetAll();
2121 return;
2122 }
2123
2124 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2125 assert(!isa<ScalableVectorType>(V->getType()));
2126 // We know that CV must be a vector of integers. Take the intersection of
2127 // each element.
2128 Known.Zero.setAllBits(); Known.One.setAllBits();
2129 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2130 if (!DemandedElts[i])
2131 continue;
2132 Constant *Element = CV->getAggregateElement(i);
2133 if (isa<PoisonValue>(Element))
2134 continue;
2135 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2136 if (!ElementCI) {
2137 Known.resetAll();
2138 return;
2139 }
2140 const APInt &Elt = ElementCI->getValue();
2141 Known.Zero &= ~Elt;
2142 Known.One &= Elt;
2143 }
2144 if (Known.hasConflict())
2145 Known.resetAll();
2146 return;
2147 }
2148
2149 // Start out not knowing anything.
2150 Known.resetAll();
2151
2152 // We can't imply anything about undefs.
2153 if (isa<UndefValue>(V))
2154 return;
2155
2156 // There's no point in looking through other users of ConstantData for
2157 // assumptions. Confirm that we've handled them all.
2158 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2159
2160 if (const auto *A = dyn_cast<Argument>(V))
2161 if (std::optional<ConstantRange> Range = A->getRange())
2162 Known = Range->toKnownBits();
2163
2164 // All recursive calls that increase depth must come after this.
2166 return;
2167
2168 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2169 // the bits of its aliasee.
2170 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2171 if (!GA->isInterposable())
2172 computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q);
2173 return;
2174 }
2175
2176 if (const Operator *I = dyn_cast<Operator>(V))
2177 computeKnownBitsFromOperator(I, DemandedElts, Known, Depth, Q);
2178 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2179 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2180 Known = CR->toKnownBits();
2181 }
2182
2183 // Aligned pointers have trailing zeros - refine Known.Zero set
2184 if (isa<PointerType>(V->getType())) {
2185 Align Alignment = V->getPointerAlignment(Q.DL);
2186 Known.Zero.setLowBits(Log2(Alignment));
2187 }
2188
2189 // computeKnownBitsFromContext strictly refines Known.
2190 // Therefore, we run them after computeKnownBitsFromOperator.
2191
2192 // Check whether we can determine known bits from context such as assumes.
2193 computeKnownBitsFromContext(V, Known, Depth, Q);
2194}
2195
2196/// Try to detect a recurrence that the value of the induction variable is
2197/// always a power of two (or zero).
2198static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2199 unsigned Depth, SimplifyQuery &Q) {
2200 BinaryOperator *BO = nullptr;
2201 Value *Start = nullptr, *Step = nullptr;
2202 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2203 return false;
2204
2205 // Initial value must be a power of two.
2206 for (const Use &U : PN->operands()) {
2207 if (U.get() == Start) {
2208 // Initial value comes from a different BB, need to adjust context
2209 // instruction for analysis.
2210 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2211 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Depth, Q))
2212 return false;
2213 }
2214 }
2215
2216 // Except for Mul, the induction variable must be on the left side of the
2217 // increment expression, otherwise its value can be arbitrary.
2218 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2219 return false;
2220
2221 Q.CxtI = BO->getParent()->getTerminator();
2222 switch (BO->getOpcode()) {
2223 case Instruction::Mul:
2224 // Power of two is closed under multiplication.
2225 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2226 Q.IIQ.hasNoSignedWrap(BO)) &&
2227 isKnownToBeAPowerOfTwo(Step, OrZero, Depth, Q);
2228 case Instruction::SDiv:
2229 // Start value must not be signmask for signed division, so simply being a
2230 // power of two is not sufficient, and it has to be a constant.
2231 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2232 return false;
2233 [[fallthrough]];
2234 case Instruction::UDiv:
2235 // Divisor must be a power of two.
2236 // If OrZero is false, cannot guarantee induction variable is non-zero after
2237 // division, same for Shr, unless it is exact division.
2238 return (OrZero || Q.IIQ.isExact(BO)) &&
2239 isKnownToBeAPowerOfTwo(Step, false, Depth, Q);
2240 case Instruction::Shl:
2241 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2242 case Instruction::AShr:
2243 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2244 return false;
2245 [[fallthrough]];
2246 case Instruction::LShr:
2247 return OrZero || Q.IIQ.isExact(BO);
2248 default:
2249 return false;
2250 }
2251}
2252
2253/// Return true if we can infer that \p V is known to be a power of 2 from
2254/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2255static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2256 const Value *Cond,
2257 bool CondIsTrue) {
2258 CmpPredicate Pred;
2259 const APInt *RHSC;
2260 if (!match(Cond, m_ICmp(Pred, m_Intrinsic<Intrinsic::ctpop>(m_Specific(V)),
2261 m_APInt(RHSC))))
2262 return false;
2263 if (!CondIsTrue)
2264 Pred = ICmpInst::getInversePredicate(Pred);
2265 // ctpop(V) u< 2
2266 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2267 return true;
2268 // ctpop(V) == 1
2269 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2270}
2271
2272/// Return true if the given value is known to have exactly one
2273/// bit set when defined. For vectors return true if every element is known to
2274/// be a power of two when defined. Supports values with integer or pointer
2275/// types and vectors of integers.
2276bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
2277 const SimplifyQuery &Q) {
2278 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2279
2280 if (isa<Constant>(V))
2281 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2282
2283 // i1 is by definition a power of 2 or zero.
2284 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2285 return true;
2286
2287 // Try to infer from assumptions.
2288 if (Q.AC && Q.CxtI) {
2289 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2290 if (!AssumeVH)
2291 continue;
2292 CallInst *I = cast<CallInst>(AssumeVH);
2293 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2294 /*CondIsTrue=*/true) &&
2296 return true;
2297 }
2298 }
2299
2300 // Handle dominating conditions.
2301 if (Q.DC && Q.CxtI && Q.DT) {
2302 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2303 Value *Cond = BI->getCondition();
2304
2305 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2307 /*CondIsTrue=*/true) &&
2308 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2309 return true;
2310
2311 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2313 /*CondIsTrue=*/false) &&
2314 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2315 return true;
2316 }
2317 }
2318
2319 auto *I = dyn_cast<Instruction>(V);
2320 if (!I)
2321 return false;
2322
2323 if (Q.CxtI && match(V, m_VScale())) {
2324 const Function *F = Q.CxtI->getFunction();
2325 // The vscale_range indicates vscale is a power-of-two.
2326 return F->hasFnAttribute(Attribute::VScaleRange);
2327 }
2328
2329 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2330 // it is shifted off the end then the result is undefined.
2331 if (match(I, m_Shl(m_One(), m_Value())))
2332 return true;
2333
2334 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2335 // the bottom. If it is shifted off the bottom then the result is undefined.
2336 if (match(I, m_LShr(m_SignMask(), m_Value())))
2337 return true;
2338
2339 // The remaining tests are all recursive, so bail out if we hit the limit.
2341 return false;
2342
2343 switch (I->getOpcode()) {
2344 case Instruction::ZExt:
2345 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2346 case Instruction::Trunc:
2347 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2348 case Instruction::Shl:
2349 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2350 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2351 return false;
2352 case Instruction::LShr:
2353 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2354 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2355 return false;
2356 case Instruction::UDiv:
2357 if (Q.IIQ.isExact(cast<BinaryOperator>(I)))
2358 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2359 return false;
2360 case Instruction::Mul:
2361 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2362 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q) &&
2363 (OrZero || isKnownNonZero(I, Q, Depth));
2364 case Instruction::And:
2365 // A power of two and'd with anything is a power of two or zero.
2366 if (OrZero &&
2367 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Depth, Q) ||
2368 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Depth, Q)))
2369 return true;
2370 // X & (-X) is always a power of two or zero.
2371 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2372 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2373 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2374 return false;
2375 case Instruction::Add: {
2376 // Adding a power-of-two or zero to the same power-of-two or zero yields
2377 // either the original power-of-two, a larger power-of-two or zero.
2378 const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
2379 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2380 Q.IIQ.hasNoSignedWrap(VOBO)) {
2381 if (match(I->getOperand(0),
2382 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2383 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q))
2384 return true;
2385 if (match(I->getOperand(1),
2386 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2387 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q))
2388 return true;
2389
2390 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2391 KnownBits LHSBits(BitWidth);
2392 computeKnownBits(I->getOperand(0), LHSBits, Depth, Q);
2393
2394 KnownBits RHSBits(BitWidth);
2395 computeKnownBits(I->getOperand(1), RHSBits, Depth, Q);
2396 // If i8 V is a power of two or zero:
2397 // ZeroBits: 1 1 1 0 1 1 1 1
2398 // ~ZeroBits: 0 0 0 1 0 0 0 0
2399 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2400 // If OrZero isn't set, we cannot give back a zero result.
2401 // Make sure either the LHS or RHS has a bit set.
2402 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2403 return true;
2404 }
2405
2406 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2407 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2408 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2409 return true;
2410 return false;
2411 }
2412 case Instruction::Select:
2413 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2414 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Depth, Q);
2415 case Instruction::PHI: {
2416 // A PHI node is power of two if all incoming values are power of two, or if
2417 // it is an induction variable where in each step its value is a power of
2418 // two.
2419 auto *PN = cast<PHINode>(I);
2421
2422 // Check if it is an induction variable and always power of two.
2423 if (isPowerOfTwoRecurrence(PN, OrZero, Depth, RecQ))
2424 return true;
2425
2426 // Recursively check all incoming values. Limit recursion to 2 levels, so
2427 // that search complexity is limited to number of operands^2.
2428 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2429 return llvm::all_of(PN->operands(), [&](const Use &U) {
2430 // Value is power of 2 if it is coming from PHI node itself by induction.
2431 if (U.get() == PN)
2432 return true;
2433
2434 // Change the context instruction to the incoming block where it is
2435 // evaluated.
2436 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2437 return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ);
2438 });
2439 }
2440 case Instruction::Invoke:
2441 case Instruction::Call: {
2442 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2443 switch (II->getIntrinsicID()) {
2444 case Intrinsic::umax:
2445 case Intrinsic::smax:
2446 case Intrinsic::umin:
2447 case Intrinsic::smin:
2448 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Depth, Q) &&
2449 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2450 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2451 // thus dont change pow2/non-pow2 status.
2452 case Intrinsic::bitreverse:
2453 case Intrinsic::bswap:
2454 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2455 case Intrinsic::fshr:
2456 case Intrinsic::fshl:
2457 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2458 if (II->getArgOperand(0) == II->getArgOperand(1))
2459 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2460 break;
2461 default:
2462 break;
2463 }
2464 }
2465 return false;
2466 }
2467 default:
2468 return false;
2469 }
2470}
2471
2472/// Test whether a GEP's result is known to be non-null.
2473///
2474/// Uses properties inherent in a GEP to try to determine whether it is known
2475/// to be non-null.
2476///
2477/// Currently this routine does not support vector GEPs.
2478static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
2479 const SimplifyQuery &Q) {
2480 const Function *F = nullptr;
2481 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2482 F = I->getFunction();
2483
2484 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2485 // may be null iff the base pointer is null and the offset is zero.
2486 if (!GEP->hasNoUnsignedWrap() &&
2487 !(GEP->isInBounds() &&
2488 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2489 return false;
2490
2491 // FIXME: Support vector-GEPs.
2492 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2493
2494 // If the base pointer is non-null, we cannot walk to a null address with an
2495 // inbounds GEP in address space zero.
2496 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2497 return true;
2498
2499 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2500 // If so, then the GEP cannot produce a null pointer, as doing so would
2501 // inherently violate the inbounds contract within address space zero.
2503 GTI != GTE; ++GTI) {
2504 // Struct types are easy -- they must always be indexed by a constant.
2505 if (StructType *STy = GTI.getStructTypeOrNull()) {
2506 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2507 unsigned ElementIdx = OpC->getZExtValue();
2508 const StructLayout *SL = Q.DL.getStructLayout(STy);
2509 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2510 if (ElementOffset > 0)
2511 return true;
2512 continue;
2513 }
2514
2515 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2516 if (GTI.getSequentialElementStride(Q.DL).isZero())
2517 continue;
2518
2519 // Fast path the constant operand case both for efficiency and so we don't
2520 // increment Depth when just zipping down an all-constant GEP.
2521 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2522 if (!OpC->isZero())
2523 return true;
2524 continue;
2525 }
2526
2527 // We post-increment Depth here because while isKnownNonZero increments it
2528 // as well, when we pop back up that increment won't persist. We don't want
2529 // to recurse 10k times just because we have 10k GEP operands. We don't
2530 // bail completely out because we want to handle constant GEPs regardless
2531 // of depth.
2533 continue;
2534
2535 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2536 return true;
2537 }
2538
2539 return false;
2540}
2541
2543 const Instruction *CtxI,
2544 const DominatorTree *DT) {
2545 assert(!isa<Constant>(V) && "Called for constant?");
2546
2547 if (!CtxI || !DT)
2548 return false;
2549
2550 unsigned NumUsesExplored = 0;
2551 for (const auto *U : V->users()) {
2552 // Avoid massive lists
2553 if (NumUsesExplored >= DomConditionsMaxUses)
2554 break;
2555 NumUsesExplored++;
2556
2557 // If the value is used as an argument to a call or invoke, then argument
2558 // attributes may provide an answer about null-ness.
2559 if (const auto *CB = dyn_cast<CallBase>(U))
2560 if (auto *CalledFunc = CB->getCalledFunction())
2561 for (const Argument &Arg : CalledFunc->args())
2562 if (CB->getArgOperand(Arg.getArgNo()) == V &&
2563 Arg.hasNonNullAttr(/* AllowUndefOrPoison */ false) &&
2564 DT->dominates(CB, CtxI))
2565 return true;
2566
2567 // If the value is used as a load/store, then the pointer must be non null.
2568 if (V == getLoadStorePointerOperand(U)) {
2569 const Instruction *I = cast<Instruction>(U);
2570 if (!NullPointerIsDefined(I->getFunction(),
2571 V->getType()->getPointerAddressSpace()) &&
2572 DT->dominates(I, CtxI))
2573 return true;
2574 }
2575
2576 if ((match(U, m_IDiv(m_Value(), m_Specific(V))) ||
2577 match(U, m_IRem(m_Value(), m_Specific(V)))) &&
2578 isValidAssumeForContext(cast<Instruction>(U), CtxI, DT))
2579 return true;
2580
2581 // Consider only compare instructions uniquely controlling a branch
2582 Value *RHS;
2583 CmpPredicate Pred;
2584 if (!match(U, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2585 continue;
2586
2587 bool NonNullIfTrue;
2588 if (cmpExcludesZero(Pred, RHS))
2589 NonNullIfTrue = true;
2591 NonNullIfTrue = false;
2592 else
2593 continue;
2594
2597 for (const auto *CmpU : U->users()) {
2598 assert(WorkList.empty() && "Should be!");
2599 if (Visited.insert(CmpU).second)
2600 WorkList.push_back(CmpU);
2601
2602 while (!WorkList.empty()) {
2603 auto *Curr = WorkList.pop_back_val();
2604
2605 // If a user is an AND, add all its users to the work list. We only
2606 // propagate "pred != null" condition through AND because it is only
2607 // correct to assume that all conditions of AND are met in true branch.
2608 // TODO: Support similar logic of OR and EQ predicate?
2609 if (NonNullIfTrue)
2610 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2611 for (const auto *CurrU : Curr->users())
2612 if (Visited.insert(CurrU).second)
2613 WorkList.push_back(CurrU);
2614 continue;
2615 }
2616
2617 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2618 assert(BI->isConditional() && "uses a comparison!");
2619
2620 BasicBlock *NonNullSuccessor =
2621 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2622 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2623 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2624 return true;
2625 } else if (NonNullIfTrue && isGuard(Curr) &&
2626 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2627 return true;
2628 }
2629 }
2630 }
2631 }
2632
2633 return false;
2634}
2635
2636/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2637/// ensure that the value it's attached to is never Value? 'RangeType' is
2638/// is the type of the value described by the range.
2639static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2640 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2641 assert(NumRanges >= 1);
2642 for (unsigned i = 0; i < NumRanges; ++i) {
2644 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2646 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2647 ConstantRange Range(Lower->getValue(), Upper->getValue());
2648 if (Range.contains(Value))
2649 return false;
2650 }
2651 return true;
2652}
2653
2654/// Try to detect a recurrence that monotonically increases/decreases from a
2655/// non-zero starting value. These are common as induction variables.
2656static bool isNonZeroRecurrence(const PHINode *PN) {
2657 BinaryOperator *BO = nullptr;
2658 Value *Start = nullptr, *Step = nullptr;
2659 const APInt *StartC, *StepC;
2660 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2661 !match(Start, m_APInt(StartC)) || StartC->isZero())
2662 return false;
2663
2664 switch (BO->getOpcode()) {
2665 case Instruction::Add:
2666 // Starting from non-zero and stepping away from zero can never wrap back
2667 // to zero.
2668 return BO->hasNoUnsignedWrap() ||
2669 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
2670 StartC->isNegative() == StepC->isNegative());
2671 case Instruction::Mul:
2672 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2673 match(Step, m_APInt(StepC)) && !StepC->isZero();
2674 case Instruction::Shl:
2675 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
2676 case Instruction::AShr:
2677 case Instruction::LShr:
2678 return BO->isExact();
2679 default:
2680 return false;
2681 }
2682}
2683
2684static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
2685 return match(Op0, m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,
2686 m_Specific(Op1), m_Zero()))) ||
2687 match(Op1, m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,
2688 m_Specific(Op0), m_Zero())));
2689}
2690
2691static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth,
2692 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2693 Value *Y, bool NSW, bool NUW) {
2694 // (X + (X != 0)) is non zero
2695 if (matchOpWithOpEqZero(X, Y))
2696 return true;
2697
2698 if (NUW)
2699 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2700 isKnownNonZero(X, DemandedElts, Q, Depth);
2701
2702 KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2703 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2704
2705 // If X and Y are both non-negative (as signed values) then their sum is not
2706 // zero unless both X and Y are zero.
2707 if (XKnown.isNonNegative() && YKnown.isNonNegative())
2708 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2709 isKnownNonZero(X, DemandedElts, Q, Depth))
2710 return true;
2711
2712 // If X and Y are both negative (as signed values) then their sum is not
2713 // zero unless both X and Y equal INT_MIN.
2714 if (XKnown.isNegative() && YKnown.isNegative()) {
2716 // The sign bit of X is set. If some other bit is set then X is not equal
2717 // to INT_MIN.
2718 if (XKnown.One.intersects(Mask))
2719 return true;
2720 // The sign bit of Y is set. If some other bit is set then Y is not equal
2721 // to INT_MIN.
2722 if (YKnown.One.intersects(Mask))
2723 return true;
2724 }
2725
2726 // The sum of a non-negative number and a power of two is not zero.
2727 if (XKnown.isNonNegative() &&
2728 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q))
2729 return true;
2730 if (YKnown.isNonNegative() &&
2731 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
2732 return true;
2733
2734 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
2735}
2736
2737static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth,
2738 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2739 Value *Y) {
2740 // (X - (X != 0)) is non zero
2741 // ((X != 0) - X) is non zero
2742 if (matchOpWithOpEqZero(X, Y))
2743 return true;
2744
2745 // TODO: Move this case into isKnownNonEqual().
2746 if (auto *C = dyn_cast<Constant>(X))
2747 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
2748 return true;
2749
2750 return ::isKnownNonEqual(X, Y, DemandedElts, Depth, Q);
2751}
2752
2753static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth,
2754 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2755 Value *Y, bool NSW, bool NUW) {
2756 // If X and Y are non-zero then so is X * Y as long as the multiplication
2757 // does not overflow.
2758 if (NSW || NUW)
2759 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
2760 isKnownNonZero(Y, DemandedElts, Q, Depth);
2761
2762 // If either X or Y is odd, then if the other is non-zero the result can't
2763 // be zero.
2764 KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2765 if (XKnown.One[0])
2766 return isKnownNonZero(Y, DemandedElts, Q, Depth);
2767
2768 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2769 if (YKnown.One[0])
2770 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
2771
2772 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
2773 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
2774 // the lowest known One of X and Y. If they are non-zero, the result
2775 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
2776 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
2777 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
2778 BitWidth;
2779}
2780
2781static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
2782 unsigned Depth, const SimplifyQuery &Q,
2783 const KnownBits &KnownVal) {
2784 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2785 switch (I->getOpcode()) {
2786 case Instruction::Shl:
2787 return Lhs.shl(Rhs);
2788 case Instruction::LShr:
2789 return Lhs.lshr(Rhs);
2790 case Instruction::AShr:
2791 return Lhs.ashr(Rhs);
2792 default:
2793 llvm_unreachable("Unknown Shift Opcode");
2794 }
2795 };
2796
2797 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2798 switch (I->getOpcode()) {
2799 case Instruction::Shl:
2800 return Lhs.lshr(Rhs);
2801 case Instruction::LShr:
2802 case Instruction::AShr:
2803 return Lhs.shl(Rhs);
2804 default:
2805 llvm_unreachable("Unknown Shift Opcode");
2806 }
2807 };
2808
2809 if (KnownVal.isUnknown())
2810 return false;
2811
2812 KnownBits KnownCnt =
2813 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
2814 APInt MaxShift = KnownCnt.getMaxValue();
2815 unsigned NumBits = KnownVal.getBitWidth();
2816 if (MaxShift.uge(NumBits))
2817 return false;
2818
2819 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
2820 return true;
2821
2822 // If all of the bits shifted out are known to be zero, and Val is known
2823 // non-zero then at least one non-zero bit must remain.
2824 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
2825 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
2826 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
2827 return true;
2828
2829 return false;
2830}
2831
2833 const APInt &DemandedElts,
2834 unsigned Depth, const SimplifyQuery &Q) {
2835 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
2836 switch (I->getOpcode()) {
2837 case Instruction::Alloca:
2838 // Alloca never returns null, malloc might.
2839 return I->getType()->getPointerAddressSpace() == 0;
2840 case Instruction::GetElementPtr:
2841 if (I->getType()->isPointerTy())
2842 return isGEPKnownNonNull(cast<GEPOperator>(I), Depth, Q);
2843 break;
2844 case Instruction::BitCast: {
2845 // We need to be a bit careful here. We can only peek through the bitcast
2846 // if the scalar size of elements in the operand are smaller than and a
2847 // multiple of the size they are casting too. Take three cases:
2848 //
2849 // 1) Unsafe:
2850 // bitcast <2 x i16> %NonZero to <4 x i8>
2851 //
2852 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
2853 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
2854 // guranteed (imagine just sign bit set in the 2 i16 elements).
2855 //
2856 // 2) Unsafe:
2857 // bitcast <4 x i3> %NonZero to <3 x i4>
2858 //
2859 // Even though the scalar size of the src (`i3`) is smaller than the
2860 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
2861 // its possible for the `3 x i4` elements to be zero because there are
2862 // some elements in the destination that don't contain any full src
2863 // element.
2864 //
2865 // 3) Safe:
2866 // bitcast <4 x i8> %NonZero to <2 x i16>
2867 //
2868 // This is always safe as non-zero in the 4 i8 elements implies
2869 // non-zero in the combination of any two adjacent ones. Since i8 is a
2870 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
2871 // This all implies the 2 i16 elements are non-zero.
2872 Type *FromTy = I->getOperand(0)->getType();
2873 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
2874 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
2875 return isKnownNonZero(I->getOperand(0), Q, Depth);
2876 } break;
2877 case Instruction::IntToPtr:
2878 // Note that we have to take special care to avoid looking through
2879 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
2880 // as casts that can alter the value, e.g., AddrSpaceCasts.
2881 if (!isa<ScalableVectorType>(I->getType()) &&
2882 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2883 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2884 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2885 break;
2886 case Instruction::PtrToInt:
2887 // Similar to int2ptr above, we can look through ptr2int here if the cast
2888 // is a no-op or an extend and not a truncate.
2889 if (!isa<ScalableVectorType>(I->getType()) &&
2890 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2891 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2892 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2893 break;
2894 case Instruction::Trunc:
2895 // nuw/nsw trunc preserves zero/non-zero status of input.
2896 if (auto *TI = dyn_cast<TruncInst>(I))
2897 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
2898 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
2899 break;
2900
2901 case Instruction::Sub:
2902 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2903 I->getOperand(1));
2904 case Instruction::Xor:
2905 // (X ^ (X != 0)) is non zero
2906 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
2907 return true;
2908 break;
2909 case Instruction::Or:
2910 // (X | (X != 0)) is non zero
2911 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
2912 return true;
2913 // X | Y != 0 if X != 0 or Y != 0.
2914 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
2915 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2916 case Instruction::SExt:
2917 case Instruction::ZExt:
2918 // ext X != 0 if X != 0.
2919 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2920
2921 case Instruction::Shl: {
2922 // shl nsw/nuw can't remove any non-zero bits.
2923 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
2924 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
2925 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2926
2927 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
2928 // if the lowest bit is shifted off the end.
2929 KnownBits Known(BitWidth);
2930 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth, Q);
2931 if (Known.One[0])
2932 return true;
2933
2934 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
2935 }
2936 case Instruction::LShr:
2937 case Instruction::AShr: {
2938 // shr exact can only shift out zero bits.
2939 const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(I);
2940 if (BO->isExact())
2941 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2942
2943 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
2944 // defined if the sign bit is shifted off the end.
2945 KnownBits Known =
2946 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
2947 if (Known.isNegative())
2948 return true;
2949
2950 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
2951 }
2952 case Instruction::UDiv:
2953 case Instruction::SDiv: {
2954 // X / Y
2955 // div exact can only produce a zero if the dividend is zero.
2956 if (cast<PossiblyExactOperator>(I)->isExact())
2957 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2958
2959 KnownBits XKnown =
2960 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
2961 // If X is fully unknown we won't be able to figure anything out so don't
2962 // both computing knownbits for Y.
2963 if (XKnown.isUnknown())
2964 return false;
2965
2966 KnownBits YKnown =
2967 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
2968 if (I->getOpcode() == Instruction::SDiv) {
2969 // For signed division need to compare abs value of the operands.
2970 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
2971 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
2972 }
2973 // If X u>= Y then div is non zero (0/0 is UB).
2974 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
2975 // If X is total unknown or X u< Y we won't be able to prove non-zero
2976 // with compute known bits so just return early.
2977 return XUgeY && *XUgeY;
2978 }
2979 case Instruction::Add: {
2980 // X + Y.
2981
2982 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
2983 // non-zero.
2984 auto *BO = cast<OverflowingBinaryOperator>(I);
2985 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2986 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
2987 Q.IIQ.hasNoUnsignedWrap(BO));
2988 }
2989 case Instruction::Mul: {
2990 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
2991 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
2992 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
2993 Q.IIQ.hasNoUnsignedWrap(BO));
2994 }
2995 case Instruction::Select: {
2996 // (C ? X : Y) != 0 if X != 0 and Y != 0.
2997
2998 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
2999 // then see if the select condition implies the arm is non-zero. For example
3000 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3001 // dominated by `X != 0`.
3002 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3003 Value *Op;
3004 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3005 // Op is trivially non-zero.
3006 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3007 return true;
3008
3009 // The condition of the select dominates the true/false arm. Check if the
3010 // condition implies that a given arm is non-zero.
3011 Value *X;
3012 CmpPredicate Pred;
3013 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3014 return false;
3015
3016 if (!IsTrueArm)
3017 Pred = ICmpInst::getInversePredicate(Pred);
3018
3019 return cmpExcludesZero(Pred, X);
3020 };
3021
3022 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3023 SelectArmIsNonZero(/* IsTrueArm */ false))
3024 return true;
3025 break;
3026 }
3027 case Instruction::PHI: {
3028 auto *PN = cast<PHINode>(I);
3030 return true;
3031
3032 // Check if all incoming values are non-zero using recursion.
3034 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3035 return llvm::all_of(PN->operands(), [&](const Use &U) {
3036 if (U.get() == PN)
3037 return true;
3038 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3039 // Check if the branch on the phi excludes zero.
3040 CmpPredicate Pred;
3041 Value *X;
3042 BasicBlock *TrueSucc, *FalseSucc;
3043 if (match(RecQ.CxtI,
3044 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3045 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3046 // Check for cases of duplicate successors.
3047 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3048 // If we're using the false successor, invert the predicate.
3049 if (FalseSucc == PN->getParent())
3050 Pred = CmpInst::getInversePredicate(Pred);
3051 if (cmpExcludesZero(Pred, X))
3052 return true;
3053 }
3054 }
3055 // Finally recurse on the edge and check it directly.
3056 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3057 });
3058 }
3059 case Instruction::InsertElement: {
3060 if (isa<ScalableVectorType>(I->getType()))
3061 break;
3062
3063 const Value *Vec = I->getOperand(0);
3064 const Value *Elt = I->getOperand(1);
3065 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3066
3067 unsigned NumElts = DemandedElts.getBitWidth();
3068 APInt DemandedVecElts = DemandedElts;
3069 bool SkipElt = false;
3070 // If we know the index we are inserting too, clear it from Vec check.
3071 if (CIdx && CIdx->getValue().ult(NumElts)) {
3072 DemandedVecElts.clearBit(CIdx->getZExtValue());
3073 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3074 }
3075
3076 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3077 // are non-zero.
3078 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3079 (DemandedVecElts.isZero() ||
3080 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3081 }
3082 case Instruction::ExtractElement:
3083 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3084 const Value *Vec = EEI->getVectorOperand();
3085 const Value *Idx = EEI->getIndexOperand();
3086 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3087 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3088 unsigned NumElts = VecTy->getNumElements();
3089 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3090 if (CIdx && CIdx->getValue().ult(NumElts))
3091 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3092 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3093 }
3094 }
3095 break;
3096 case Instruction::ShuffleVector: {
3097 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3098 if (!Shuf)
3099 break;
3100 APInt DemandedLHS, DemandedRHS;
3101 // For undef elements, we don't know anything about the common state of
3102 // the shuffle result.
3103 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3104 break;
3105 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3106 return (DemandedRHS.isZero() ||
3107 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3108 (DemandedLHS.isZero() ||
3109 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3110 }
3111 case Instruction::Freeze:
3112 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3113 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3114 Depth);
3115 case Instruction::Load: {
3116 auto *LI = cast<LoadInst>(I);
3117 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3118 // is never null.
3119 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3120 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3121 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3122 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3123 return true;
3124 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3126 }
3127
3128 // No need to fall through to computeKnownBits as range metadata is already
3129 // handled in isKnownNonZero.
3130 return false;
3131 }
3132 case Instruction::ExtractValue: {
3133 const WithOverflowInst *WO;
3134 if (match(I, m_ExtractValue<0>(m_WithOverflowInst(WO)))) {
3135 switch (WO->getBinaryOp()) {
3136 default:
3137 break;
3138 case Instruction::Add:
3139 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
3140 WO->getArgOperand(0), WO->getArgOperand(1),
3141 /*NSW=*/false,
3142 /*NUW=*/false);
3143 case Instruction::Sub:
3144 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
3145 WO->getArgOperand(0), WO->getArgOperand(1));
3146 case Instruction::Mul:
3147 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth,
3148 WO->getArgOperand(0), WO->getArgOperand(1),
3149 /*NSW=*/false, /*NUW=*/false);
3150 break;
3151 }
3152 }
3153 break;
3154 }
3155 case Instruction::Call:
3156 case Instruction::Invoke: {
3157 const auto *Call = cast<CallBase>(I);
3158 if (I->getType()->isPointerTy()) {
3159 if (Call->isReturnNonNull())
3160 return true;
3161 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3162 return isKnownNonZero(RP, Q, Depth);
3163 } else {
3164 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3166 if (std::optional<ConstantRange> Range = Call->getRange()) {
3167 const APInt ZeroValue(Range->getBitWidth(), 0);
3168 if (!Range->contains(ZeroValue))
3169 return true;
3170 }
3171 if (const Value *RV = Call->getReturnedArgOperand())
3172 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3173 return true;
3174 }
3175
3176 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3177 switch (II->getIntrinsicID()) {
3178 case Intrinsic::sshl_sat:
3179 case Intrinsic::ushl_sat:
3180 case Intrinsic::abs:
3181 case Intrinsic::bitreverse:
3182 case Intrinsic::bswap:
3183 case Intrinsic::ctpop:
3184 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3185 // NB: We don't do usub_sat here as in any case we can prove its
3186 // non-zero, we will fold it to `sub nuw` in InstCombine.
3187 case Intrinsic::ssub_sat:
3188 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
3189 II->getArgOperand(0), II->getArgOperand(1));
3190 case Intrinsic::sadd_sat:
3191 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
3192 II->getArgOperand(0), II->getArgOperand(1),
3193 /*NSW=*/true, /* NUW=*/false);
3194 // Vec reverse preserves zero/non-zero status from input vec.
3195 case Intrinsic::vector_reverse:
3196 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3197 Q, Depth);
3198 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3199 case Intrinsic::vector_reduce_or:
3200 case Intrinsic::vector_reduce_umax:
3201 case Intrinsic::vector_reduce_umin:
3202 case Intrinsic::vector_reduce_smax:
3203 case Intrinsic::vector_reduce_smin:
3204 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3205 case Intrinsic::umax:
3206 case Intrinsic::uadd_sat:
3207 // umax(X, (X != 0)) is non zero
3208 // X +usat (X != 0) is non zero
3209 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3210 return true;
3211
3212 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3213 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3214 case Intrinsic::smax: {
3215 // If either arg is strictly positive the result is non-zero. Otherwise
3216 // the result is non-zero if both ops are non-zero.
3217 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3218 const KnownBits &OpKnown) {
3219 if (!OpNonZero.has_value())
3220 OpNonZero = OpKnown.isNonZero() ||
3221 isKnownNonZero(Op, DemandedElts, Q, Depth);
3222 return *OpNonZero;
3223 };
3224 // Avoid re-computing isKnownNonZero.
3225 std::optional<bool> Op0NonZero, Op1NonZero;
3226 KnownBits Op1Known =
3227 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
3228 if (Op1Known.isNonNegative() &&
3229 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3230 return true;
3231 KnownBits Op0Known =
3232 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
3233 if (Op0Known.isNonNegative() &&
3234 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3235 return true;
3236 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3237 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3238 }
3239 case Intrinsic::smin: {
3240 // If either arg is negative the result is non-zero. Otherwise
3241 // the result is non-zero if both ops are non-zero.
3242 KnownBits Op1Known =
3243 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
3244 if (Op1Known.isNegative())
3245 return true;
3246 KnownBits Op0Known =
3247 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
3248 if (Op0Known.isNegative())
3249 return true;
3250
3251 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3252 return true;
3253 }
3254 [[fallthrough]];
3255 case Intrinsic::umin:
3256 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3257 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3258 case Intrinsic::cttz:
3259 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
3260 .Zero[0];
3261 case Intrinsic::ctlz:
3262 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
3263 .isNonNegative();
3264 case Intrinsic::fshr:
3265 case Intrinsic::fshl:
3266 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3267 if (II->getArgOperand(0) == II->getArgOperand(1))
3268 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3269 break;
3270 case Intrinsic::vscale:
3271 return true;
3272 case Intrinsic::experimental_get_vector_length:
3273 return isKnownNonZero(I->getOperand(0), Q, Depth);
3274 default:
3275 break;
3276 }
3277 break;
3278 }
3279
3280 return false;
3281 }
3282 }
3283
3284 KnownBits Known(BitWidth);
3285 computeKnownBits(I, DemandedElts, Known, Depth, Q);
3286 return Known.One != 0;
3287}
3288
3289/// Return true if the given value is known to be non-zero when defined. For
3290/// vectors, return true if every demanded element is known to be non-zero when
3291/// defined. For pointers, if the context instruction and dominator tree are
3292/// specified, perform context-sensitive analysis and return true if the
3293/// pointer couldn't possibly be null at the specified instruction.
3294/// Supports values with integer or pointer type and vectors of integers.
3295bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3296 const SimplifyQuery &Q, unsigned Depth) {
3297 Type *Ty = V->getType();
3298
3299#ifndef NDEBUG
3300 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3301
3302 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3303 assert(
3304 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3305 "DemandedElt width should equal the fixed vector number of elements");
3306 } else {
3307 assert(DemandedElts == APInt(1, 1) &&
3308 "DemandedElt width should be 1 for scalars");
3309 }
3310#endif
3311
3312 if (auto *C = dyn_cast<Constant>(V)) {
3313 if (C->isNullValue())
3314 return false;
3315 if (isa<ConstantInt>(C))
3316 // Must be non-zero due to null test above.
3317 return true;
3318
3319 // For constant vectors, check that all elements are poison or known
3320 // non-zero to determine that the whole vector is known non-zero.
3321 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3322 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3323 if (!DemandedElts[i])
3324 continue;
3325 Constant *Elt = C->getAggregateElement(i);
3326 if (!Elt || Elt->isNullValue())
3327 return false;
3328 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3329 return false;
3330 }
3331 return true;
3332 }
3333
3334 // Constant ptrauth can be null, iff the base pointer can be.
3335 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3336 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3337
3338 // A global variable in address space 0 is non null unless extern weak
3339 // or an absolute symbol reference. Other address spaces may have null as a
3340 // valid address for a global, so we can't assume anything.
3341 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3342 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3343 GV->getType()->getAddressSpace() == 0)
3344 return true;
3345 }
3346
3347 // For constant expressions, fall through to the Operator code below.
3348 if (!isa<ConstantExpr>(V))
3349 return false;
3350 }
3351
3352 if (const auto *A = dyn_cast<Argument>(V))
3353 if (std::optional<ConstantRange> Range = A->getRange()) {
3354 const APInt ZeroValue(Range->getBitWidth(), 0);
3355 if (!Range->contains(ZeroValue))
3356 return true;
3357 }
3358
3359 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3360 return true;
3361
3362 // Some of the tests below are recursive, so bail out if we hit the limit.
3364 return false;
3365
3366 // Check for pointer simplifications.
3367
3368 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3369 // A byval, inalloca may not be null in a non-default addres space. A
3370 // nonnull argument is assumed never 0.
3371 if (const Argument *A = dyn_cast<Argument>(V)) {
3372 if (((A->hasPassPointeeByValueCopyAttr() &&
3373 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3374 A->hasNonNullAttr()))
3375 return true;
3376 }
3377 }
3378
3379 if (const auto *I = dyn_cast<Operator>(V))
3380 if (isKnownNonZeroFromOperator(I, DemandedElts, Depth, Q))
3381 return true;
3382
3383 if (!isa<Constant>(V) &&
3385 return true;
3386
3387 return false;
3388}
3389
3391 unsigned Depth) {
3392 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3393 APInt DemandedElts =
3394 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3395 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3396}
3397
3398/// If the pair of operators are the same invertible function, return the
3399/// the operands of the function corresponding to each input. Otherwise,
3400/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3401/// every input value to exactly one output value. This is equivalent to
3402/// saying that Op1 and Op2 are equal exactly when the specified pair of
3403/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3404static std::optional<std::pair<Value*, Value*>>
3406 const Operator *Op2) {
3407 if (Op1->getOpcode() != Op2->getOpcode())
3408 return std::nullopt;
3409
3410 auto getOperands = [&](unsigned OpNum) -> auto {
3411 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3412 };
3413
3414 switch (Op1->getOpcode()) {
3415 default:
3416 break;
3417 case Instruction::Or:
3418 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3419 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3420 break;
3421 [[fallthrough]];
3422 case Instruction::Xor:
3423 case Instruction::Add: {
3424 Value *Other;
3425 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3426 return std::make_pair(Op1->getOperand(1), Other);
3427 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3428 return std::make_pair(Op1->getOperand(0), Other);
3429 break;
3430 }
3431 case Instruction::Sub:
3432 if (Op1->getOperand(0) == Op2->getOperand(0))
3433 return getOperands(1);
3434 if (Op1->getOperand(1) == Op2->getOperand(1))
3435 return getOperands(0);
3436 break;
3437 case Instruction::Mul: {
3438 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3439 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3440 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3441 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3442 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3443 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3444 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3445 break;
3446
3447 // Assume operand order has been canonicalized
3448 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3449 isa<ConstantInt>(Op1->getOperand(1)) &&
3450 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3451 return getOperands(0);
3452 break;
3453 }
3454 case Instruction::Shl: {
3455 // Same as multiplies, with the difference that we don't need to check
3456 // for a non-zero multiply. Shifts always multiply by non-zero.
3457 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3458 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3459 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3460 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3461 break;
3462
3463 if (Op1->getOperand(1) == Op2->getOperand(1))
3464 return getOperands(0);
3465 break;
3466 }
3467 case Instruction::AShr:
3468 case Instruction::LShr: {
3469 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3470 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3471 if (!PEO1->isExact() || !PEO2->isExact())
3472 break;
3473
3474 if (Op1->getOperand(1) == Op2->getOperand(1))
3475 return getOperands(0);
3476 break;
3477 }
3478 case Instruction::SExt:
3479 case Instruction::ZExt:
3480 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3481 return getOperands(0);
3482 break;
3483 case Instruction::PHI: {
3484 const PHINode *PN1 = cast<PHINode>(Op1);
3485 const PHINode *PN2 = cast<PHINode>(Op2);
3486
3487 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3488 // are a single invertible function of the start values? Note that repeated
3489 // application of an invertible function is also invertible
3490 BinaryOperator *BO1 = nullptr;
3491 Value *Start1 = nullptr, *Step1 = nullptr;
3492 BinaryOperator *BO2 = nullptr;
3493 Value *Start2 = nullptr, *Step2 = nullptr;
3494 if (PN1->getParent() != PN2->getParent() ||
3495 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3496 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3497 break;
3498
3499 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3500 cast<Operator>(BO2));
3501 if (!Values)
3502 break;
3503
3504 // We have to be careful of mutually defined recurrences here. Ex:
3505 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3506 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3507 // The invertibility of these is complicated, and not worth reasoning
3508 // about (yet?).
3509 if (Values->first != PN1 || Values->second != PN2)
3510 break;
3511
3512 return std::make_pair(Start1, Start2);
3513 }
3514 }
3515 return std::nullopt;
3516}
3517
3518/// Return true if V1 == (binop V2, X), where X is known non-zero.
3519/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3520/// implies V2 != V1.
3521static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3522 const APInt &DemandedElts, unsigned Depth,
3523 const SimplifyQuery &Q) {
3524 const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
3525 if (!BO)
3526 return false;
3527 switch (BO->getOpcode()) {
3528 default:
3529 break;
3530 case Instruction::Or:
3531 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3532 break;
3533 [[fallthrough]];
3534 case Instruction::Xor:
3535 case Instruction::Add:
3536 Value *Op = nullptr;
3537 if (V2 == BO->getOperand(0))
3538 Op = BO->getOperand(1);
3539 else if (V2 == BO->getOperand(1))
3540 Op = BO->getOperand(0);
3541 else
3542 return false;
3543 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3544 }
3545 return false;
3546}
3547
3548/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3549/// the multiplication is nuw or nsw.
3550static bool isNonEqualMul(const Value *V1, const Value *V2,
3551 const APInt &DemandedElts, unsigned Depth,
3552 const SimplifyQuery &Q) {
3553 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3554 const APInt *C;
3555 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3556 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3557 !C->isZero() && !C->isOne() &&
3558 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3559 }
3560 return false;
3561}
3562
3563/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3564/// the shift is nuw or nsw.
3565static bool isNonEqualShl(const Value *V1, const Value *V2,
3566 const APInt &DemandedElts, unsigned Depth,
3567 const SimplifyQuery &Q) {
3568 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3569 const APInt *C;
3570 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3571 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3572 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3573 }
3574 return false;
3575}
3576
3577static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3578 const APInt &DemandedElts, unsigned Depth,
3579 const SimplifyQuery &Q) {
3580 // Check two PHIs are in same block.
3581 if (PN1->getParent() != PN2->getParent())
3582 return false;
3583
3585 bool UsedFullRecursion = false;
3586 for (const BasicBlock *IncomBB : PN1->blocks()) {
3587 if (!VisitedBBs.insert(IncomBB).second)
3588 continue; // Don't reprocess blocks that we have dealt with already.
3589 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3590 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3591 const APInt *C1, *C2;
3592 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3593 continue;
3594
3595 // Only one pair of phi operands is allowed for full recursion.
3596 if (UsedFullRecursion)
3597 return false;
3598
3600 RecQ.CxtI = IncomBB->getTerminator();
3601 if (!isKnownNonEqual(IV1, IV2, DemandedElts, Depth + 1, RecQ))
3602 return false;
3603 UsedFullRecursion = true;
3604 }
3605 return true;
3606}
3607
3608static bool isNonEqualSelect(const Value *V1, const Value *V2,
3609 const APInt &DemandedElts, unsigned Depth,
3610 const SimplifyQuery &Q) {
3611 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3612 if (!SI1)
3613 return false;
3614
3615 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3616 const Value *Cond1 = SI1->getCondition();
3617 const Value *Cond2 = SI2->getCondition();
3618 if (Cond1 == Cond2)
3619 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3620 DemandedElts, Depth + 1, Q) &&
3621 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3622 DemandedElts, Depth + 1, Q);
3623 }
3624 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Depth + 1, Q) &&
3625 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Depth + 1, Q);
3626}
3627
3628// Check to see if A is both a GEP and is the incoming value for a PHI in the
3629// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3630// one of them being the recursive GEP A and the other a ptr at same base and at
3631// the same/higher offset than B we are only incrementing the pointer further in
3632// loop if offset of recursive GEP is greater than 0.
3634 const SimplifyQuery &Q) {
3635 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3636 return false;
3637
3638 auto *GEPA = dyn_cast<GEPOperator>(A);
3639 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3640 return false;
3641
3642 // Handle 2 incoming PHI values with one being a recursive GEP.
3643 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3644 if (!PN || PN->getNumIncomingValues() != 2)
3645 return false;
3646
3647 // Search for the recursive GEP as an incoming operand, and record that as
3648 // Step.
3649 Value *Start = nullptr;
3650 Value *Step = const_cast<Value *>(A);
3651 if (PN->getIncomingValue(0) == Step)
3652 Start = PN->getIncomingValue(1);
3653 else if (PN->getIncomingValue(1) == Step)
3654 Start = PN->getIncomingValue(0);
3655 else
3656 return false;
3657
3658 // Other incoming node base should match the B base.
3659 // StartOffset >= OffsetB && StepOffset > 0?
3660 // StartOffset <= OffsetB && StepOffset < 0?
3661 // Is non-equal if above are true.
3662 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3663 // optimisation to inbounds GEPs only.
3664 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3665 APInt StartOffset(IndexWidth, 0);
3666 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3667 APInt StepOffset(IndexWidth, 0);
3668 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3669
3670 // Check if Base Pointer of Step matches the PHI.
3671 if (Step != PN)
3672 return false;
3673 APInt OffsetB(IndexWidth, 0);
3674 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3675 return Start == B &&
3676 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
3677 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
3678}
3679
3680/// Return true if it is known that V1 != V2.
3681static bool isKnownNonEqual(const Value *V1, const Value *V2,
3682 const APInt &DemandedElts, unsigned Depth,
3683 const SimplifyQuery &Q) {
3684 if (V1 == V2)
3685 return false;
3686 if (V1->getType() != V2->getType())
3687 // We can't look through casts yet.
3688 return false;
3689
3691 return false;
3692
3693 // See if we can recurse through (exactly one of) our operands. This
3694 // requires our operation be 1-to-1 and map every input value to exactly
3695 // one output value. Such an operation is invertible.
3696 auto *O1 = dyn_cast<Operator>(V1);
3697 auto *O2 = dyn_cast<Operator>(V2);
3698 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3699 if (auto Values = getInvertibleOperands(O1, O2))
3700 return isKnownNonEqual(Values->first, Values->second, DemandedElts,
3701 Depth + 1, Q);
3702
3703 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3704 const PHINode *PN2 = cast<PHINode>(V2);
3705 // FIXME: This is missing a generalization to handle the case where one is
3706 // a PHI and another one isn't.
3707 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Depth, Q))
3708 return true;
3709 };
3710 }
3711
3712 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Depth, Q) ||
3713 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Depth, Q))
3714 return true;
3715
3716 if (isNonEqualMul(V1, V2, DemandedElts, Depth, Q) ||
3717 isNonEqualMul(V2, V1, DemandedElts, Depth, Q))
3718 return true;
3719
3720 if (isNonEqualShl(V1, V2, DemandedElts, Depth, Q) ||
3721 isNonEqualShl(V2, V1, DemandedElts, Depth, Q))
3722 return true;
3723
3724 if (V1->getType()->isIntOrIntVectorTy()) {
3725 // Are any known bits in V1 contradictory to known bits in V2? If V1
3726 // has a known zero where V2 has a known one, they must not be equal.
3727 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Depth, Q);
3728 if (!Known1.isUnknown()) {
3729 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Depth, Q);
3730 if (Known1.Zero.intersects(Known2.One) ||
3731 Known2.Zero.intersects(Known1.One))
3732 return true;
3733 }
3734 }
3735
3736 if (isNonEqualSelect(V1, V2, DemandedElts, Depth, Q) ||
3737 isNonEqualSelect(V2, V1, DemandedElts, Depth, Q))
3738 return true;
3739
3740 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3742 return true;
3743
3744 Value *A, *B;
3745 // PtrToInts are NonEqual if their Ptrs are NonEqual.
3746 // Check PtrToInt type matches the pointer size.
3747 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
3749 return isKnownNonEqual(A, B, DemandedElts, Depth + 1, Q);
3750
3751 return false;
3752}
3753
3754// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
3755// Returns the input and lower/upper bounds.
3756static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
3757 const APInt *&CLow, const APInt *&CHigh) {
3758 assert(isa<Operator>(Select) &&
3759 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
3760 "Input should be a Select!");
3761
3762 const Value *LHS = nullptr, *RHS = nullptr;
3764 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
3765 return false;
3766
3767 if (!match(RHS, m_APInt(CLow)))
3768 return false;
3769
3770 const Value *LHS2 = nullptr, *RHS2 = nullptr;
3772 if (getInverseMinMaxFlavor(SPF) != SPF2)
3773 return false;
3774
3775 if (!match(RHS2, m_APInt(CHigh)))
3776 return false;
3777
3778 if (SPF == SPF_SMIN)
3779 std::swap(CLow, CHigh);
3780
3781 In = LHS2;
3782 return CLow->sle(*CHigh);
3783}
3784
3786 const APInt *&CLow,
3787 const APInt *&CHigh) {
3788 assert((II->getIntrinsicID() == Intrinsic::smin ||
3789 II->getIntrinsicID() == Intrinsic::smax) && "Must be smin/smax");
3790
3791 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
3792 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
3793 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
3794 !match(II->getArgOperand(1), m_APInt(CLow)) ||
3795 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
3796 return false;
3797
3798 if (II->getIntrinsicID() == Intrinsic::smin)
3799 std::swap(CLow, CHigh);
3800 return CLow->sle(*CHigh);
3801}
3802
3803/// For vector constants, loop over the elements and find the constant with the
3804/// minimum number of sign bits. Return 0 if the value is not a vector constant
3805/// or if any element was not analyzed; otherwise, return the count for the
3806/// element with the minimum number of sign bits.
3808 const APInt &DemandedElts,
3809 unsigned TyBits) {
3810 const auto *CV = dyn_cast<Constant>(V);
3811 if (!CV || !isa<FixedVectorType>(CV->getType()))
3812 return 0;
3813
3814 unsigned MinSignBits = TyBits;
3815 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3816 for (unsigned i = 0; i != NumElts; ++i) {
3817 if (!DemandedElts[i])
3818 continue;
3819 // If we find a non-ConstantInt, bail out.
3820 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3821 if (!Elt)
3822 return 0;
3823
3824 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3825 }
3826
3827 return MinSignBits;
3828}
3829
3830static unsigned ComputeNumSignBitsImpl(const Value *V,
3831 const APInt &DemandedElts,
3832 unsigned Depth, const SimplifyQuery &Q);
3833
3834static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
3835 unsigned Depth, const SimplifyQuery &Q) {
3836 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q);
3837 assert(Result > 0 && "At least one sign bit needs to be present!");
3838 return Result;
3839}
3840
3841/// Return the number of times the sign bit of the register is replicated into
3842/// the other bits. We know that at least 1 bit is always equal to the sign bit
3843/// (itself), but other cases can give us information. For example, immediately
3844/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
3845/// other, so we return 3. For vectors, return the number of sign bits for the
3846/// vector element with the minimum number of known sign bits of the demanded
3847/// elements in the vector specified by DemandedElts.
3848static unsigned ComputeNumSignBitsImpl(const Value *V,
3849 const APInt &DemandedElts,
3850 unsigned Depth, const SimplifyQuery &Q) {
3851 Type *Ty = V->getType();
3852#ifndef NDEBUG
3853 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3854
3855 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3856 assert(
3857 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3858 "DemandedElt width should equal the fixed vector number of elements");
3859 } else {
3860 assert(DemandedElts == APInt(1, 1) &&
3861 "DemandedElt width should be 1 for scalars");
3862 }
3863#endif
3864
3865 // We return the minimum number of sign bits that are guaranteed to be present
3866 // in V, so for undef we have to conservatively return 1. We don't have the
3867 // same behavior for poison though -- that's a FIXME today.
3868
3869 Type *ScalarTy = Ty->getScalarType();
3870 unsigned TyBits = ScalarTy->isPointerTy() ?
3871 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
3872 Q.DL.getTypeSizeInBits(ScalarTy);
3873
3874 unsigned Tmp, Tmp2;
3875 unsigned FirstAnswer = 1;
3876
3877 // Note that ConstantInt is handled by the general computeKnownBits case
3878 // below.
3879
3881 return 1;
3882
3883 if (auto *U = dyn_cast<Operator>(V)) {
3884 switch (Operator::getOpcode(V)) {
3885 default: break;
3886 case Instruction::SExt:
3887 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
3888 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q) +
3889 Tmp;
3890
3891 case Instruction::SDiv: {
3892 const APInt *Denominator;
3893 // sdiv X, C -> adds log(C) sign bits.
3894 if (match(U->getOperand(1), m_APInt(Denominator))) {
3895
3896 // Ignore non-positive denominator.
3897 if (!Denominator->isStrictlyPositive())
3898 break;
3899
3900 // Calculate the incoming numerator bits.
3901 unsigned NumBits =
3902 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
3903
3904 // Add floor(log(C)) bits to the numerator bits.
3905 return std::min(TyBits, NumBits + Denominator->logBase2());
3906 }
3907 break;
3908 }
3909
3910 case Instruction::SRem: {
3911 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
3912
3913 const APInt *Denominator;
3914 // srem X, C -> we know that the result is within [-C+1,C) when C is a
3915 // positive constant. This let us put a lower bound on the number of sign
3916 // bits.
3917 if (match(U->getOperand(1), m_APInt(Denominator))) {
3918
3919 // Ignore non-positive denominator.
3920 if (Denominator->isStrictlyPositive()) {
3921 // Calculate the leading sign bit constraints by examining the
3922 // denominator. Given that the denominator is positive, there are two
3923 // cases:
3924 //
3925 // 1. The numerator is positive. The result range is [0,C) and
3926 // [0,C) u< (1 << ceilLogBase2(C)).
3927 //
3928 // 2. The numerator is negative. Then the result range is (-C,0] and
3929 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
3930 //
3931 // Thus a lower bound on the number of sign bits is `TyBits -
3932 // ceilLogBase2(C)`.
3933
3934 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
3935 Tmp = std::max(Tmp, ResBits);
3936 }
3937 }
3938 return Tmp;
3939 }
3940
3941 case Instruction::AShr: {
3942 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
3943 // ashr X, C -> adds C sign bits. Vectors too.
3944 const APInt *ShAmt;
3945 if (match(U->getOperand(1), m_APInt(ShAmt))) {
3946 if (ShAmt->uge(TyBits))
3947 break; // Bad shift.
3948 unsigned ShAmtLimited = ShAmt->getZExtValue();
3949 Tmp += ShAmtLimited;
3950 if (Tmp > TyBits) Tmp = TyBits;
3951 }
3952 return Tmp;
3953 }
3954 case Instruction::Shl: {
3955 const APInt *ShAmt;
3956 Value *X = nullptr;
3957 if (match(U->getOperand(1), m_APInt(ShAmt))) {
3958 // shl destroys sign bits.
3959 if (ShAmt->uge(TyBits))
3960 break; // Bad shift.
3961 // We can look through a zext (more or less treating it as a sext) if
3962 // all extended bits are shifted out.
3963 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
3964 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
3965 Tmp = ComputeNumSignBits(X, DemandedElts, Depth + 1, Q);
3966 Tmp += TyBits - X->getType()->getScalarSizeInBits();
3967 } else
3968 Tmp =
3969 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
3970 if (ShAmt->uge(Tmp))
3971 break; // Shifted all sign bits out.
3972 Tmp2 = ShAmt->getZExtValue();
3973 return Tmp - Tmp2;
3974 }
3975 break;
3976 }
3977 case Instruction::And:
3978 case Instruction::Or:
3979 case Instruction::Xor: // NOT is handled here.
3980 // Logical binary ops preserve the number of sign bits at the worst.
3981 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
3982 if (Tmp != 1) {
3983 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
3984 FirstAnswer = std::min(Tmp, Tmp2);
3985 // We computed what we know about the sign bits as our first
3986 // answer. Now proceed to the generic code that uses
3987 // computeKnownBits, and pick whichever answer is better.
3988 }
3989 break;
3990
3991 case Instruction::Select: {
3992 // If we have a clamp pattern, we know that the number of sign bits will
3993 // be the minimum of the clamp min/max range.
3994 const Value *X;
3995 const APInt *CLow, *CHigh;
3996 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
3997 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
3998
3999 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4000 if (Tmp == 1)
4001 break;
4002 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Depth + 1, Q);
4003 return std::min(Tmp, Tmp2);
4004 }
4005
4006 case Instruction::Add:
4007 // Add can have at most one carry bit. Thus we know that the output
4008 // is, at worst, one more bit than the inputs.
4009 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
4010 if (Tmp == 1) break;
4011
4012 // Special case decrementing a value (ADD X, -1):
4013 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4014 if (CRHS->isAllOnesValue()) {
4015 KnownBits Known(TyBits);
4016 computeKnownBits(U->getOperand(0), DemandedElts, Known, Depth + 1, Q);
4017
4018 // If the input is known to be 0 or 1, the output is 0/-1, which is
4019 // all sign bits set.
4020 if ((Known.Zero | 1).isAllOnes())
4021 return TyBits;
4022
4023 // If we are subtracting one from a positive number, there is no carry
4024 // out of the result.
4025 if (Known.isNonNegative())
4026 return Tmp;
4027 }
4028
4029 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4030 if (Tmp2 == 1)
4031 break;
4032 return std::min(Tmp, Tmp2) - 1;
4033
4034 case Instruction::Sub:
4035 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4036 if (Tmp2 == 1)
4037 break;
4038
4039 // Handle NEG.
4040 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4041 if (CLHS->isNullValue()) {
4042 KnownBits Known(TyBits);
4043 computeKnownBits(U->getOperand(1), DemandedElts, Known, Depth + 1, Q);
4044 // If the input is known to be 0 or 1, the output is 0/-1, which is
4045 // all sign bits set.
4046 if ((Known.Zero | 1).isAllOnes())
4047 return TyBits;
4048
4049 // If the input is known to be positive (the sign bit is known clear),
4050 // the output of the NEG has the same number of sign bits as the
4051 // input.
4052 if (Known.isNonNegative())
4053 return Tmp2;
4054
4055 // Otherwise, we treat this like a SUB.
4056 }
4057
4058 // Sub can have at most one carry bit. Thus we know that the output
4059 // is, at worst, one more bit than the inputs.
4060 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4061 if (Tmp == 1)
4062 break;
4063 return std::min(Tmp, Tmp2) - 1;
4064
4065 case Instruction::Mul: {
4066 // The output of the Mul can be at most twice the valid bits in the
4067 // inputs.
4068 unsigned SignBitsOp0 =
4069 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4070 if (SignBitsOp0 == 1)
4071 break;
4072 unsigned SignBitsOp1 =
4073 ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4074 if (SignBitsOp1 == 1)
4075 break;
4076 unsigned OutValidBits =
4077 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4078 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4079 }
4080
4081 case Instruction::PHI: {
4082 const PHINode *PN = cast<PHINode>(U);
4083 unsigned NumIncomingValues = PN->getNumIncomingValues();
4084 // Don't analyze large in-degree PHIs.
4085 if (NumIncomingValues > 4) break;
4086 // Unreachable blocks may have zero-operand PHI nodes.
4087 if (NumIncomingValues == 0) break;
4088
4089 // Take the minimum of all incoming values. This can't infinitely loop
4090 // because of our depth threshold.
4092 Tmp = TyBits;
4093 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4094 if (Tmp == 1) return Tmp;
4095 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4096 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4097 DemandedElts, Depth + 1, RecQ));
4098 }
4099 return Tmp;
4100 }
4101
4102 case Instruction::Trunc: {
4103 // If the input contained enough sign bits that some remain after the
4104 // truncation, then we can make use of that. Otherwise we don't know
4105 // anything.
4106 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
4107 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4108 if (Tmp > (OperandTyBits - TyBits))
4109 return Tmp - (OperandTyBits - TyBits);
4110
4111 return 1;
4112 }
4113
4114 case Instruction::ExtractElement:
4115 // Look through extract element. At the moment we keep this simple and
4116 // skip tracking the specific element. But at least we might find
4117 // information valid for all elements of the vector (for example if vector
4118 // is sign extended, shifted, etc).
4119 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
4120
4121 case Instruction::ShuffleVector: {
4122 // Collect the minimum number of sign bits that are shared by every vector
4123 // element referenced by the shuffle.
4124 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4125 if (!Shuf) {
4126 // FIXME: Add support for shufflevector constant expressions.
4127 return 1;
4128 }
4129 APInt DemandedLHS, DemandedRHS;
4130 // For undef elements, we don't know anything about the common state of
4131 // the shuffle result.
4132 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4133 return 1;
4134 Tmp = std::numeric_limits<unsigned>::max();
4135 if (!!DemandedLHS) {
4136 const Value *LHS = Shuf->getOperand(0);
4137 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q);
4138 }
4139 // If we don't know anything, early out and try computeKnownBits
4140 // fall-back.
4141 if (Tmp == 1)
4142 break;
4143 if (!!DemandedRHS) {
4144 const Value *RHS = Shuf->getOperand(1);
4145 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q);
4146 Tmp = std::min(Tmp, Tmp2);
4147 }
4148 // If we don't know anything, early out and try computeKnownBits
4149 // fall-back.
4150 if (Tmp == 1)
4151 break;
4152 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4153 return Tmp;
4154 }
4155 case Instruction::Call: {
4156 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4157 switch (II->getIntrinsicID()) {
4158 default:
4159 break;
4160 case Intrinsic::abs:
4161 Tmp =
4162 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4163 if (Tmp == 1)
4164 break;
4165
4166 // Absolute value reduces number of sign bits by at most 1.
4167 return Tmp - 1;
4168 case Intrinsic::smin:
4169 case Intrinsic::smax: {
4170 const APInt *CLow, *CHigh;
4171 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4172 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4173 }
4174 }
4175 }
4176 }
4177 }
4178 }
4179
4180 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4181 // use this information.
4182
4183 // If we can examine all elements of a vector constant successfully, we're
4184 // done (we can't do any better than that). If not, keep trying.
4185 if (unsigned VecSignBits =
4186 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4187 return VecSignBits;
4188
4189 KnownBits Known(TyBits);
4190 computeKnownBits(V, DemandedElts, Known, Depth, Q);
4191
4192 // If we know that the sign bit is either zero or one, determine the number of
4193 // identical bits in the top of the input value.
4194 return std::max(FirstAnswer, Known.countMinSignBits());
4195}
4196
4198 const TargetLibraryInfo *TLI) {
4199 const Function *F = CB.getCalledFunction();
4200 if (!F)
4202
4203 if (F->isIntrinsic())
4204 return F->getIntrinsicID();
4205
4206 // We are going to infer semantics of a library function based on mapping it
4207 // to an LLVM intrinsic. Check that the library function is available from
4208 // this callbase and in this environment.
4209 LibFunc Func;
4210 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4211 !CB.onlyReadsMemory())
4213
4214 switch (Func) {
4215 default:
4216 break;
4217 case LibFunc_sin:
4218 case LibFunc_sinf:
4219 case LibFunc_sinl:
4220 return Intrinsic::sin;
4221 case LibFunc_cos:
4222 case LibFunc_cosf:
4223 case LibFunc_cosl:
4224 return Intrinsic::cos;
4225 case LibFunc_tan:
4226 case LibFunc_tanf:
4227 case LibFunc_tanl:
4228 return Intrinsic::tan;
4229 case LibFunc_asin:
4230 case LibFunc_asinf:
4231 case LibFunc_asinl:
4232 return Intrinsic::asin;
4233 case LibFunc_acos:
4234 case LibFunc_acosf:
4235 case LibFunc_acosl:
4236 return Intrinsic::acos;
4237 case LibFunc_atan:
4238 case LibFunc_atanf:
4239 case LibFunc_atanl:
4240 return Intrinsic::atan;
4241 case LibFunc_atan2:
4242 case LibFunc_atan2f:
4243 case LibFunc_atan2l:
4244 return Intrinsic::atan2;
4245 case LibFunc_sinh:
4246 case LibFunc_sinhf:
4247 case LibFunc_sinhl:
4248 return Intrinsic::sinh;
4249 case LibFunc_cosh:
4250 case LibFunc_coshf:
4251 case LibFunc_coshl:
4252 return Intrinsic::cosh;
4253 case LibFunc_tanh:
4254 case LibFunc_tanhf:
4255 case LibFunc_tanhl:
4256 return Intrinsic::tanh;
4257 case LibFunc_exp:
4258 case LibFunc_expf:
4259 case LibFunc_expl:
4260 return Intrinsic::exp;
4261 case LibFunc_exp2:
4262 case LibFunc_exp2f:
4263 case LibFunc_exp2l:
4264 return Intrinsic::exp2;
4265 case LibFunc_exp10:
4266 case LibFunc_exp10f:
4267 case LibFunc_exp10l:
4268 return Intrinsic::exp10;
4269 case LibFunc_log:
4270 case LibFunc_logf:
4271 case LibFunc_logl:
4272 return Intrinsic::log;
4273 case LibFunc_log10:
4274 case LibFunc_log10f:
4275 case LibFunc_log10l:
4276 return Intrinsic::log10;
4277 case LibFunc_log2:
4278 case LibFunc_log2f:
4279 case LibFunc_log2l:
4280 return Intrinsic::log2;
4281 case LibFunc_fabs:
4282 case LibFunc_fabsf:
4283 case LibFunc_fabsl:
4284 return Intrinsic::fabs;
4285 case LibFunc_fmin:
4286 case LibFunc_fminf:
4287 case LibFunc_fminl:
4288 return Intrinsic::minnum;
4289 case LibFunc_fmax:
4290 case LibFunc_fmaxf:
4291 case LibFunc_fmaxl:
4292 return Intrinsic::maxnum;
4293 case LibFunc_copysign:
4294 case LibFunc_copysignf:
4295 case LibFunc_copysignl:
4296 return Intrinsic::copysign;
4297 case LibFunc_floor:
4298 case LibFunc_floorf:
4299 case LibFunc_floorl:
4300 return Intrinsic::floor;
4301 case LibFunc_ceil:
4302 case LibFunc_ceilf:
4303 case LibFunc_ceill:
4304 return Intrinsic::ceil;
4305 case LibFunc_trunc:
4306 case LibFunc_truncf:
4307 case LibFunc_truncl:
4308 return Intrinsic::trunc;
4309 case LibFunc_rint:
4310 case LibFunc_rintf:
4311 case LibFunc_rintl:
4312 return Intrinsic::rint;
4313 case LibFunc_nearbyint:
4314 case LibFunc_nearbyintf:
4315 case LibFunc_nearbyintl:
4316 return Intrinsic::nearbyint;
4317 case LibFunc_round:
4318 case LibFunc_roundf:
4319 case LibFunc_roundl:
4320 return Intrinsic::round;
4321 case LibFunc_roundeven:
4322 case LibFunc_roundevenf:
4323 case LibFunc_roundevenl:
4324 return Intrinsic::roundeven;
4325 case LibFunc_pow:
4326 case LibFunc_powf:
4327 case LibFunc_powl:
4328 return Intrinsic::pow;
4329 case LibFunc_sqrt:
4330 case LibFunc_sqrtf:
4331 case LibFunc_sqrtl:
4332 return Intrinsic::sqrt;
4333 }
4334
4336}
4337
4338/// Return true if it's possible to assume IEEE treatment of input denormals in
4339/// \p F for \p Val.
4340static bool inputDenormalIsIEEE(const Function &F, const Type *Ty) {
4341 Ty = Ty->getScalarType();
4342 return F.getDenormalMode(Ty->getFltSemantics()).Input == DenormalMode::IEEE;
4343}
4344
4345static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4346 Ty = Ty->getScalarType();
4347 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4348 return Mode.Input == DenormalMode::IEEE ||
4349 Mode.Input == DenormalMode::PositiveZero;
4350}
4351
4352static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4353 Ty = Ty->getScalarType();
4354 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4355 return Mode.Output == DenormalMode::IEEE ||
4356 Mode.Output == DenormalMode::PositiveZero;
4357}
4358
4360 return isKnownNeverZero() &&
4362}
4363
4365 Type *Ty) const {
4366 return isKnownNeverNegZero() &&
4368}
4369
4371 Type *Ty) const {
4372 if (!isKnownNeverPosZero())
4373 return false;
4374
4375 // If we know there are no denormals, nothing can be flushed to zero.
4377 return true;
4378
4379 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4380 switch (Mode.Input) {
4381 case DenormalMode::IEEE:
4382 return true;
4384 // Negative subnormal won't flush to +0
4385 return isKnownNeverPosSubnormal();
4387 default:
4388 // Both positive and negative subnormal could flush to +0
4389 return false;
4390 }
4391
4392 llvm_unreachable("covered switch over denormal mode");
4393}
4394
4396 Type *Ty) {
4397 KnownFPClasses = Src.KnownFPClasses;
4398 // If we aren't assuming the source can't be a zero, we don't have to check if
4399 // a denormal input could be flushed.
4400 if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero())
4401 return;
4402
4403 // If we know the input can't be a denormal, it can't be flushed to 0.
4404 if (Src.isKnownNeverSubnormal())
4405 return;
4406
4407 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4408
4409 if (!Src.isKnownNeverPosSubnormal() && Mode != DenormalMode::getIEEE())
4411
4412 if (!Src.isKnownNeverNegSubnormal() && Mode != DenormalMode::getIEEE()) {
4413 if (Mode != DenormalMode::getPositiveZero())
4415
4416 if (Mode.Input == DenormalMode::PositiveZero ||
4417 Mode.Output == DenormalMode::PositiveZero ||
4418 Mode.Input == DenormalMode::Dynamic ||
4419 Mode.Output == DenormalMode::Dynamic)
4421 }
4422}
4423
4425 const Function &F, Type *Ty) {
4426 propagateDenormal(Src, F, Ty);
4427 propagateNaN(Src, /*PreserveSign=*/true);
4428}
4429
4430/// Given an exploded icmp instruction, return true if the comparison only
4431/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4432/// the result of the comparison is true when the input value is signed.
4434 bool &TrueIfSigned) {
4435 switch (Pred) {
4436 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4437 TrueIfSigned = true;
4438 return RHS.isZero();
4439 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4440 TrueIfSigned = true;
4441 return RHS.isAllOnes();
4442 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4443 TrueIfSigned = false;
4444 return RHS.isAllOnes();
4445 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4446 TrueIfSigned = false;
4447 return RHS.isZero();
4448 case ICmpInst::ICMP_UGT:
4449 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4450 TrueIfSigned = true;
4451 return RHS.isMaxSignedValue();
4452 case ICmpInst::ICMP_UGE:
4453 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4454 TrueIfSigned = true;
4455 return RHS.isMinSignedValue();
4456 case ICmpInst::ICMP_ULT:
4457 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4458 TrueIfSigned = false;
4459 return RHS.isMinSignedValue();
4460 case ICmpInst::ICMP_ULE:
4461 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4462 TrueIfSigned = false;
4463 return RHS.isMaxSignedValue();
4464 default:
4465 return false;
4466 }
4467}
4468
4469/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
4470/// same result as an fcmp with the given operands.
4471std::pair<Value *, FPClassTest> llvm::fcmpToClassTest(FCmpInst::Predicate Pred,
4472 const Function &F,
4473 Value *LHS, Value *RHS,
4474 bool LookThroughSrc) {
4475 const APFloat *ConstRHS;
4476 if (!match(RHS, m_APFloatAllowPoison(ConstRHS)))
4477 return {nullptr, fcAllFlags};
4478
4479 return fcmpToClassTest(Pred, F, LHS, ConstRHS, LookThroughSrc);
4480}
4481
4482std::pair<Value *, FPClassTest>
4484 const APFloat *ConstRHS, bool LookThroughSrc) {
4485
4486 auto [Src, ClassIfTrue, ClassIfFalse] =
4487 fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4488 if (Src && ClassIfTrue == ~ClassIfFalse)
4489 return {Src, ClassIfTrue};
4490 return {nullptr, fcAllFlags};
4491}
4492
4493/// Return the return value for fcmpImpliesClass for a compare that produces an
4494/// exact class test.
4495static std::tuple<Value *, FPClassTest, FPClassTest> exactClass(Value *V,
4496 FPClassTest M) {
4497 return {V, M, ~M};
4498}
4499
4500std::tuple<Value *, FPClassTest, FPClassTest>
4502 FPClassTest RHSClass, bool LookThroughSrc) {
4503 assert(RHSClass != fcNone);
4504 Value *Src = LHS;
4505
4506 if (Pred == FCmpInst::FCMP_TRUE)
4507 return exactClass(Src, fcAllFlags);
4508
4509 if (Pred == FCmpInst::FCMP_FALSE)
4510 return exactClass(Src, fcNone);
4511
4512 const FPClassTest OrigClass = RHSClass;
4513
4514 const bool IsNegativeRHS = (RHSClass & fcNegative) == RHSClass;
4515 const bool IsPositiveRHS = (RHSClass & fcPositive) == RHSClass;
4516 const bool IsNaN = (RHSClass & ~fcNan) == fcNone;
4517
4518 if (IsNaN) {
4519 // fcmp o__ x, nan -> false
4520 // fcmp u__ x, nan -> true
4521 return exactClass(Src, CmpInst::isOrdered(Pred) ? fcNone : fcAllFlags);
4522 }
4523
4524 // fcmp ord x, zero|normal|subnormal|inf -> ~fcNan
4525 if (Pred == FCmpInst::FCMP_ORD)
4526 return exactClass(Src, ~fcNan);
4527
4528 // fcmp uno x, zero|normal|subnormal|inf -> fcNan
4529 if (Pred == FCmpInst::FCMP_UNO)
4530 return exactClass(Src, fcNan);
4531
4532 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4533 if (IsFabs)
4534 RHSClass = llvm::inverse_fabs(RHSClass);
4535
4536 const bool IsZero = (OrigClass & fcZero) == OrigClass;
4537 if (IsZero) {
4538 assert(Pred != FCmpInst::FCMP_ORD && Pred != FCmpInst::FCMP_UNO);
4539 // Compares with fcNone are only exactly equal to fcZero if input denormals
4540 // are not flushed.
4541 // TODO: Handle DAZ by expanding masks to cover subnormal cases.
4542 if (!inputDenormalIsIEEE(F, LHS->getType()))
4543 return {nullptr, fcAllFlags, fcAllFlags};
4544
4545 switch (Pred) {
4546 case FCmpInst::FCMP_OEQ: // Match x == 0.0
4547 return exactClass(Src, fcZero);
4548 case FCmpInst::FCMP_UEQ: // Match isnan(x) || (x == 0.0)
4549 return exactClass(Src, fcZero | fcNan);
4550 case FCmpInst::FCMP_UNE: // Match (x != 0.0)
4551 return exactClass(Src, ~fcZero);
4552 case FCmpInst::FCMP_ONE: // Match !isnan(x) && x != 0.0
4553 return exactClass(Src, ~fcNan & ~fcZero);
4554 case FCmpInst::FCMP_ORD:
4555 // Canonical form of ord/uno is with a zero. We could also handle
4556 // non-canonical other non-NaN constants or LHS == RHS.
4557 return exactClass(Src, ~fcNan);
4558 case FCmpInst::FCMP_UNO:
4559 return exactClass(Src, fcNan);
4560 case FCmpInst::FCMP_OGT: // x > 0
4562 case FCmpInst::FCMP_UGT: // isnan(x) || x > 0
4564 case FCmpInst::FCMP_OGE: // x >= 0
4565 return exactClass(Src, fcPositive | fcNegZero);
4566 case FCmpInst::FCMP_UGE: // isnan(x) || x >= 0
4567 return exactClass(Src, fcPositive | fcNegZero | fcNan);
4568 case FCmpInst::FCMP_OLT: // x < 0
4570 case FCmpInst::FCMP_ULT: // isnan(x) || x < 0
4572 case FCmpInst::FCMP_OLE: // x <= 0
4573 return exactClass(Src, fcNegative | fcPosZero);
4574 case FCmpInst::FCMP_ULE: // isnan(x) || x <= 0
4575 return exactClass(Src, fcNegative | fcPosZero | fcNan);
4576 default:
4577 llvm_unreachable("all compare types are handled");
4578 }
4579
4580 return {nullptr, fcAllFlags, fcAllFlags};
4581 }
4582
4583 const bool IsDenormalRHS = (OrigClass & fcSubnormal) == OrigClass;
4584
4585 const bool IsInf = (OrigClass & fcInf) == OrigClass;
4586 if (IsInf) {
4587 FPClassTest Mask = fcAllFlags;
4588
4589 switch (Pred) {
4590 case FCmpInst::FCMP_OEQ:
4591 case FCmpInst::FCMP_UNE: {
4592 // Match __builtin_isinf patterns
4593 //
4594 // fcmp oeq x, +inf -> is_fpclass x, fcPosInf
4595 // fcmp oeq fabs(x), +inf -> is_fpclass x, fcInf
4596 // fcmp oeq x, -inf -> is_fpclass x, fcNegInf
4597 // fcmp oeq fabs(x), -inf -> is_fpclass x, 0 -> false
4598 //
4599 // fcmp une x, +inf -> is_fpclass x, ~fcPosInf
4600 // fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf
4601 // fcmp une x, -inf -> is_fpclass x, ~fcNegInf
4602 // fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true
4603 if (IsNegativeRHS) {
4604 Mask = fcNegInf;
4605 if (IsFabs)
4606 Mask = fcNone;
4607 } else {
4608 Mask = fcPosInf;
4609 if (IsFabs)
4610 Mask |= fcNegInf;
4611 }
4612 break;
4613 }
4614 case FCmpInst::FCMP_ONE:
4615 case FCmpInst::FCMP_UEQ: {
4616 // Match __builtin_isinf patterns
4617 // fcmp one x, -inf -> is_fpclass x, fcNegInf
4618 // fcmp one fabs(x), -inf -> is_fpclass x, ~fcNegInf & ~fcNan
4619 // fcmp one x, +inf -> is_fpclass x, ~fcNegInf & ~fcNan
4620 // fcmp one fabs(x), +inf -> is_fpclass x, ~fcInf & fcNan
4621 //
4622 // fcmp ueq x, +inf -> is_fpclass x, fcPosInf|fcNan
4623 // fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan
4624 // fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan
4625 // fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan
4626 if (IsNegativeRHS) {
4627 Mask = ~fcNegInf & ~fcNan;
4628 if (IsFabs)
4629 Mask = ~fcNan;
4630 } else {
4631 Mask = ~fcPosInf & ~fcNan;
4632 if (IsFabs)
4633 Mask &= ~fcNegInf;
4634 }
4635
4636 break;
4637 }
4638 case FCmpInst::FCMP_OLT:
4639 case FCmpInst::FCMP_UGE: {
4640 if (IsNegativeRHS) {
4641 // No value is ordered and less than negative infinity.
4642 // All values are unordered with or at least negative infinity.
4643 // fcmp olt x, -inf -> false
4644 // fcmp uge x, -inf -> true
4645 Mask = fcNone;
4646 break;
4647 }
4648
4649 // fcmp olt fabs(x), +inf -> fcFinite
4650 // fcmp uge fabs(x), +inf -> ~fcFinite
4651 // fcmp olt x, +inf -> fcFinite|fcNegInf
4652 // fcmp uge x, +inf -> ~(fcFinite|fcNegInf)
4653 Mask = fcFinite;
4654 if (!IsFabs)
4655 Mask |= fcNegInf;
4656 break;
4657 }
4658 case FCmpInst::FCMP_OGE:
4659 case FCmpInst::FCMP_ULT: {
4660 if (IsNegativeRHS) {
4661 // fcmp oge x, -inf -> ~fcNan
4662 // fcmp oge fabs(x), -inf -> ~fcNan
4663 // fcmp ult x, -inf -> fcNan
4664 // fcmp ult fabs(x), -inf -> fcNan
4665 Mask = ~fcNan;
4666 break;
4667 }
4668
4669 // fcmp oge fabs(x), +inf -> fcInf
4670 // fcmp oge x, +inf -> fcPosInf
4671 // fcmp ult fabs(x), +inf -> ~fcInf
4672 // fcmp ult x, +inf -> ~fcPosInf
4673 Mask = fcPosInf;
4674 if (IsFabs)
4675 Mask |= fcNegInf;
4676 break;
4677 }
4678 case FCmpInst::FCMP_OGT:
4679 case FCmpInst::FCMP_ULE: {
4680 if (IsNegativeRHS) {
4681 // fcmp ogt x, -inf -> fcmp one x, -inf
4682 // fcmp ogt fabs(x), -inf -> fcmp ord x, x
4683 // fcmp ule x, -inf -> fcmp ueq x, -inf
4684 // fcmp ule fabs(x), -inf -> fcmp uno x, x
4685 Mask = IsFabs ? ~fcNan : ~(fcNegInf | fcNan);
4686 break;
4687 }
4688
4689 // No value is ordered and greater than infinity.
4690 Mask = fcNone;
4691 break;
4692 }
4693 case FCmpInst::FCMP_OLE:
4694 case FCmpInst::FCMP_UGT: {
4695 if (IsNegativeRHS) {
4696 Mask = IsFabs ? fcNone : fcNegInf;
4697 break;
4698 }
4699
4700 // fcmp ole x, +inf -> fcmp ord x, x
4701 // fcmp ole fabs(x), +inf -> fcmp ord x, x
4702 // fcmp ole x, -inf -> fcmp oeq x, -inf
4703 // fcmp ole fabs(x), -inf -> false
4704 Mask = ~fcNan;
4705 break;
4706 }
4707 default:
4708 llvm_unreachable("all compare types are handled");
4709 }
4710
4711 // Invert the comparison for the unordered cases.
4712 if (FCmpInst::isUnordered(Pred))
4713 Mask = ~Mask;
4714
4715 return exactClass(Src, Mask);
4716 }
4717
4718 if (Pred == FCmpInst::FCMP_OEQ)
4719 return {Src, RHSClass, fcAllFlags};
4720
4721 if (Pred == FCmpInst::FCMP_UEQ) {
4722 FPClassTest Class = RHSClass | fcNan;
4723 return {Src, Class, ~fcNan};
4724 }
4725
4726 if (Pred == FCmpInst::FCMP_ONE)
4727 return {Src, ~fcNan, RHSClass | fcNan};
4728
4729 if (Pred == FCmpInst::FCMP_UNE)
4730 return {Src, fcAllFlags, RHSClass};
4731
4732 assert((RHSClass == fcNone || RHSClass == fcPosNormal ||
4733 RHSClass == fcNegNormal || RHSClass == fcNormal ||
4734 RHSClass == fcPosSubnormal || RHSClass == fcNegSubnormal ||
4735 RHSClass == fcSubnormal) &&
4736 "should have been recognized as an exact class test");
4737
4738 if (IsNegativeRHS) {
4739 // TODO: Handle fneg(fabs)
4740 if (IsFabs) {
4741 // fabs(x) o> -k -> fcmp ord x, x
4742 // fabs(x) u> -k -> true
4743 // fabs(x) o< -k -> false
4744 // fabs(x) u< -k -> fcmp uno x, x
4745 switch (Pred) {
4746 case FCmpInst::FCMP_OGT:
4747 case FCmpInst::FCMP_OGE:
4748 return {Src, ~fcNan, fcNan};
4749 case FCmpInst::FCMP_UGT:
4750 case FCmpInst::FCMP_UGE:
4751 return {Src, fcAllFlags, fcNone};
4752 case FCmpInst::FCMP_OLT:
4753 case FCmpInst::FCMP_OLE:
4754 return {Src, fcNone, fcAllFlags};
4755 case FCmpInst::FCMP_ULT:
4756 case FCmpInst::FCMP_ULE:
4757 return {Src, fcNan, ~fcNan};
4758 default:
4759 break;
4760 }
4761
4762 return {nullptr, fcAllFlags, fcAllFlags};
4763 }
4764
4765 FPClassTest ClassesLE = fcNegInf | fcNegNormal;
4767
4768 if (IsDenormalRHS)
4769 ClassesLE |= fcNegSubnormal;
4770 else
4771 ClassesGE |= fcNegNormal;
4772
4773 switch (Pred) {
4774 case FCmpInst::FCMP_OGT:
4775 case FCmpInst::FCMP_OGE:
4776 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4777 case FCmpInst::FCMP_UGT:
4778 case FCmpInst::FCMP_UGE:
4779 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4780 case FCmpInst::FCMP_OLT:
4781 case FCmpInst::FCMP_OLE:
4782 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4783 case FCmpInst::FCMP_ULT:
4784 case FCmpInst::FCMP_ULE:
4785 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4786 default:
4787 break;
4788 }
4789 } else if (IsPositiveRHS) {
4790 FPClassTest ClassesGE = fcPosNormal | fcPosInf;
4792 if (IsDenormalRHS)
4793 ClassesGE |= fcPosSubnormal;
4794 else
4795 ClassesLE |= fcPosNormal;
4796
4797 if (IsFabs) {
4798 ClassesGE = llvm::inverse_fabs(ClassesGE);
4799 ClassesLE = llvm::inverse_fabs(ClassesLE);
4800 }
4801
4802 switch (Pred) {
4803 case FCmpInst::FCMP_OGT:
4804 case FCmpInst::FCMP_OGE:
4805 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4806 case FCmpInst::FCMP_UGT:
4807 case FCmpInst::FCMP_UGE:
4808 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4809 case FCmpInst::FCMP_OLT:
4810 case FCmpInst::FCMP_OLE:
4811 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4812 case FCmpInst::FCMP_ULT:
4813 case FCmpInst::FCMP_ULE:
4814 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4815 default:
4816 break;
4817 }
4818 }
4819
4820 return {nullptr, fcAllFlags, fcAllFlags};
4821}
4822
4823std::tuple<Value *, FPClassTest, FPClassTest>
4825 const APFloat &ConstRHS, bool LookThroughSrc) {
4826 // We can refine checks against smallest normal / largest denormal to an
4827 // exact class test.
4828 if (!ConstRHS.isNegative() && ConstRHS.isSmallestNormalized()) {
4829 Value *Src = LHS;
4830 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4831
4832 FPClassTest Mask;
4833 // Match pattern that's used in __builtin_isnormal.
4834 switch (Pred) {
4835 case FCmpInst::FCMP_OLT:
4836 case FCmpInst::FCMP_UGE: {
4837 // fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero
4838 // fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero
4839 // fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf
4840 // fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero)
4841 Mask = fcZero | fcSubnormal;
4842 if (!IsFabs)
4843 Mask |= fcNegNormal | fcNegInf;
4844
4845 break;
4846 }
4847 case FCmpInst::FCMP_OGE:
4848 case FCmpInst::FCMP_ULT: {
4849 // fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf
4850 // fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal
4851 // fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf)
4852 // fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal)
4853 Mask = fcPosInf | fcPosNormal;
4854 if (IsFabs)
4855 Mask |= fcNegInf | fcNegNormal;
4856 break;
4857 }
4858 default:
4859 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(),
4860 LookThroughSrc);
4861 }
4862
4863 // Invert the comparison for the unordered cases.
4864 if (FCmpInst::isUnordered(Pred))
4865 Mask = ~Mask;
4866
4867 return exactClass(Src, Mask);
4868 }
4869
4870 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(), LookThroughSrc);
4871}
4872
4873std::tuple<Value *, FPClassTest, FPClassTest>
4875 Value *RHS, bool LookThroughSrc) {
4876 const APFloat *ConstRHS;
4877 if (!match(RHS, m_APFloatAllowPoison(ConstRHS)))
4878 return {nullptr, fcAllFlags, fcAllFlags};
4879
4880 // TODO: Just call computeKnownFPClass for RHS to handle non-constants.
4881 return fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4882}
4883
4885 unsigned Depth, bool CondIsTrue,
4886 const Instruction *CxtI,
4887 KnownFPClass &KnownFromContext) {
4888 Value *A, *B;
4890 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4891 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4892 computeKnownFPClassFromCond(V, A, Depth + 1, CondIsTrue, CxtI,
4893 KnownFromContext);
4894 computeKnownFPClassFromCond(V, B, Depth + 1, CondIsTrue, CxtI,
4895 KnownFromContext);
4896 return;
4897 }
4898 CmpPredicate Pred;
4899 Value *LHS;
4900 uint64_t ClassVal = 0;
4901 const APFloat *CRHS;
4902 const APInt *RHS;
4903 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4904 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4905 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4906 if (CmpVal == V)
4907 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4908 } else if (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(
4909 m_Specific(V), m_ConstantInt(ClassVal)))) {
4910 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4911 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4912 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4913 m_APInt(RHS)))) {
4914 bool TrueIfSigned;
4915 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4916 return;
4917 if (TrueIfSigned == CondIsTrue)
4918 KnownFromContext.signBitMustBeOne();
4919 else
4920 KnownFromContext.signBitMustBeZero();
4921 }
4922}
4923
4925 const SimplifyQuery &Q) {
4926 KnownFPClass KnownFromContext;
4927
4928 if (!Q.CxtI)
4929 return KnownFromContext;
4930
4931 if (Q.DC && Q.DT) {
4932 // Handle dominating conditions.
4933 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4934 Value *Cond = BI->getCondition();
4935
4936 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4937 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4938 computeKnownFPClassFromCond(V, Cond, /*Depth=*/0, /*CondIsTrue=*/true,
4939 Q.CxtI, KnownFromContext);
4940
4941 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4942 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4943 computeKnownFPClassFromCond(V, Cond, /*Depth=*/0, /*CondIsTrue=*/false,
4944 Q.CxtI, KnownFromContext);
4945 }
4946 }
4947
4948 if (!Q.AC)
4949 return KnownFromContext;
4950
4951 // Try to restrict the floating-point classes based on information from
4952 // assumptions.
4953 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4954 if (!AssumeVH)
4955 continue;
4956 CallInst *I = cast<CallInst>(AssumeVH);
4957
4958 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4959 "Got assumption for the wrong function!");
4960 assert(I->getIntrinsicID() == Intrinsic::assume &&
4961 "must be an assume intrinsic");
4962
4963 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4964 continue;
4965
4966 computeKnownFPClassFromCond(V, I->getArgOperand(0), /*Depth=*/0,
4967 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4968 }
4969
4970 return KnownFromContext;
4971}
4972
4973void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4974 FPClassTest InterestedClasses, KnownFPClass &Known,
4975 unsigned Depth, const SimplifyQuery &Q);
4976
4977static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4978 FPClassTest InterestedClasses, unsigned Depth,
4979 const SimplifyQuery &Q) {
4980 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4981 APInt DemandedElts =
4982 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4983 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Depth, Q);
4984}
4985
4987 const APInt &DemandedElts,
4988 FPClassTest InterestedClasses,
4989 KnownFPClass &Known, unsigned Depth,
4990 const SimplifyQuery &Q) {
4991 if ((InterestedClasses &
4993 return;
4994
4995 KnownFPClass KnownSrc;
4996 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4997 KnownSrc, Depth + 1, Q);
4998
4999 // Sign should be preserved
5000 // TODO: Handle cannot be ordered greater than zero
5001 if (KnownSrc.cannotBeOrderedLessThanZero())
5003
5004 Known.propagateNaN(KnownSrc, true);
5005
5006 // Infinity needs a range check.
5007}
5008
5009void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5010 FPClassTest InterestedClasses, KnownFPClass &Known,
5011 unsigned Depth, const SimplifyQuery &Q) {
5012 assert(Known.isUnknown() && "should not be called with known information");
5013
5014 if (!DemandedElts) {
5015 // No demanded elts, better to assume we don't know anything.
5016 Known.resetAll();
5017 return;
5018 }
5019
5020 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
5021
5022 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5023 Known.KnownFPClasses = CFP->getValueAPF().classify();
5024 Known.SignBit = CFP->isNegative();
5025 return;
5026 }
5027
5028 if (isa<ConstantAggregateZero>(V)) {
5029 Known.KnownFPClasses = fcPosZero;
5030 Known.SignBit = false;
5031 return;
5032 }
5033
5034 if (isa<PoisonValue>(V)) {
5035 Known.KnownFPClasses = fcNone;
5036 Known.SignBit = false;
5037 return;
5038 }
5039
5040 // Try to handle fixed width vector constants
5041 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5042 const Constant *CV = dyn_cast<Constant>(V);
5043 if (VFVTy && CV) {
5044 Known.KnownFPClasses = fcNone;
5045 bool SignBitAllZero = true;
5046 bool SignBitAllOne = true;
5047
5048 // For vectors, verify that each element is not NaN.
5049 unsigned NumElts = VFVTy->getNumElements();
5050 for (unsigned i = 0; i != NumElts; ++i) {
5051 if (!DemandedElts[i])
5052 continue;
5053
5054 Constant *Elt = CV->getAggregateElement(i);
5055 if (!Elt) {
5056 Known = KnownFPClass();
5057 return;
5058 }
5059 if (isa<PoisonValue>(Elt))
5060 continue;
5061 auto *CElt = dyn_cast<ConstantFP>(Elt);
5062 if (!CElt) {
5063 Known = KnownFPClass();
5064 return;
5065 }
5066
5067 const APFloat &C = CElt->getValueAPF();
5068 Known.KnownFPClasses |= C.classify();
5069 if (C.isNegative())
5070 SignBitAllZero = false;
5071 else
5072 SignBitAllOne = false;
5073 }
5074 if (SignBitAllOne != SignBitAllZero)
5075 Known.SignBit = SignBitAllOne;
5076 return;
5077 }
5078
5079 FPClassTest KnownNotFromFlags = fcNone;
5080 if (const auto *CB = dyn_cast<CallBase>(V))
5081 KnownNotFromFlags |= CB->getRetNoFPClass();
5082 else if (const auto *Arg = dyn_cast<Argument>(V))
5083 KnownNotFromFlags |= Arg->getNoFPClass();
5084
5085 const Operator *Op = dyn_cast<Operator>(V);
5086 if (const FPMathOperator *FPOp = dyn_cast_or_null<FPMathOperator>(Op)) {
5087 if (FPOp->hasNoNaNs())
5088 KnownNotFromFlags |= fcNan;
5089 if (FPOp->hasNoInfs())
5090 KnownNotFromFlags |= fcInf;
5091 }
5092
5093 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5094 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5095
5096 // We no longer need to find out about these bits from inputs if we can
5097 // assume this from flags/attributes.
5098 InterestedClasses &= ~KnownNotFromFlags;
5099
5100 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
5101 Known.knownNot(KnownNotFromFlags);
5102 if (!Known.SignBit && AssumedClasses.SignBit) {
5103 if (*AssumedClasses.SignBit)
5104 Known.signBitMustBeOne();
5105 else
5106 Known.signBitMustBeZero();
5107 }
5108 });
5109
5110 if (!Op)
5111 return;
5112
5113 // All recursive calls that increase depth must come after this.
5115 return;
5116
5117 const unsigned Opc = Op->getOpcode();
5118 switch (Opc) {
5119 case Instruction::FNeg: {
5120 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5121 Known, Depth + 1, Q);
5122 Known.fneg();
5123 break;
5124 }
5125 case Instruction::Select: {
5126 Value *Cond = Op->getOperand(0);
5127 Value *LHS = Op->getOperand(1);
5128 Value *RHS = Op->getOperand(2);
5129
5130 FPClassTest FilterLHS = fcAllFlags;
5131 FPClassTest FilterRHS = fcAllFlags;
5132
5133 Value *TestedValue = nullptr;
5134 FPClassTest MaskIfTrue = fcAllFlags;
5135 FPClassTest MaskIfFalse = fcAllFlags;
5136 uint64_t ClassVal = 0;
5137 const Function *F = cast<Instruction>(Op)->getFunction();
5138 CmpPredicate Pred;
5139 Value *CmpLHS, *CmpRHS;
5140 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
5141 // If the select filters out a value based on the class, it no longer
5142 // participates in the class of the result
5143
5144 // TODO: In some degenerate cases we can infer something if we try again
5145 // without looking through sign operations.
5146 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
5147 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
5148 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
5149 } else if (match(Cond,
5150 m_Intrinsic<Intrinsic::is_fpclass>(
5151 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
5152 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
5153 MaskIfTrue = TestedMask;
5154 MaskIfFalse = ~TestedMask;
5155 }
5156
5157 if (TestedValue == LHS) {
5158 // match !isnan(x) ? x : y
5159 FilterLHS = MaskIfTrue;
5160 } else if (TestedValue == RHS) { // && IsExactClass
5161 // match !isnan(x) ? y : x
5162 FilterRHS = MaskIfFalse;
5163 }
5164
5165 KnownFPClass Known2;
5166 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
5167 Depth + 1, Q);
5168 Known.KnownFPClasses &= FilterLHS;
5169
5170 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
5171 Known2, Depth + 1, Q);
5172 Known2.KnownFPClasses &= FilterRHS;
5173
5174 Known |= Known2;
5175 break;
5176 }
5177 case Instruction::Call: {
5178 const CallInst *II = cast<CallInst>(Op);
5179 const Intrinsic::ID IID = II->getIntrinsicID();
5180 switch (IID) {
5181 case Intrinsic::fabs: {
5182 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5183 // If we only care about the sign bit we don't need to inspect the
5184 // operand.
5185 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5186 InterestedClasses, Known, Depth + 1, Q);
5187 }
5188
5189 Known.fabs();
5190 break;
5191 }
5192 case Intrinsic::copysign: {
5193 KnownFPClass KnownSign;
5194
5195 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5196 Known, Depth + 1, Q);
5197 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5198 KnownSign, Depth + 1, Q);
5199 Known.copysign(KnownSign);
5200 break;
5201 }
5202 case Intrinsic::fma:
5203 case Intrinsic::fmuladd: {
5204 if ((InterestedClasses & fcNegative) == fcNone)
5205 break;
5206
5207 if (II->getArgOperand(0) != II->getArgOperand(1))
5208 break;
5209
5210 // The multiply cannot be -0 and therefore the add can't be -0
5211 Known.knownNot(fcNegZero);
5212
5213 // x * x + y is non-negative if y is non-negative.
5214 KnownFPClass KnownAddend;
5215 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
5216 KnownAddend, Depth + 1, Q);
5217
5218 if (KnownAddend.cannotBeOrderedLessThanZero())
5219 Known.knownNot(fcNegative);
5220 break;
5221 }
5222 case Intrinsic::sqrt:
5223 case Intrinsic::experimental_constrained_sqrt: {
5224 KnownFPClass KnownSrc;
5225 FPClassTest InterestedSrcs = InterestedClasses;
5226 if (InterestedClasses & fcNan)
5227 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5228
5229 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5230 KnownSrc, Depth + 1, Q);
5231
5232 if (KnownSrc.isKnownNeverPosInfinity())
5233 Known.knownNot(fcPosInf);
5234 if (KnownSrc.isKnownNever(fcSNan))
5235 Known.knownNot(fcSNan);
5236
5237 // Any negative value besides -0 returns a nan.
5238 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5239 Known.knownNot(fcNan);
5240
5241 // The only negative value that can be returned is -0 for -0 inputs.
5243
5244 // If the input denormal mode could be PreserveSign, a negative
5245 // subnormal input could produce a negative zero output.
5246 const Function *F = II->getFunction();
5247 if (Q.IIQ.hasNoSignedZeros(II) ||
5248 (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType())))
5249 Known.knownNot(fcNegZero);
5250
5251 break;
5252 }
5253 case Intrinsic::sin:
5254 case Intrinsic::cos: {
5255 // Return NaN on infinite inputs.
5256 KnownFPClass KnownSrc;
5257 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5258 KnownSrc, Depth + 1, Q);
5259 Known.knownNot(fcInf);
5260 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
5261 Known.knownNot(fcNan);
5262 break;
5263 }
5264 case Intrinsic::maxnum:
5265 case Intrinsic::minnum:
5266 case Intrinsic::minimum:
5267 case Intrinsic::maximum: {
5268 KnownFPClass KnownLHS, KnownRHS;
5269 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5270 KnownLHS, Depth + 1, Q);
5271 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5272 KnownRHS, Depth + 1, Q);
5273
5274 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
5275 Known = KnownLHS | KnownRHS;
5276
5277 // If either operand is not NaN, the result is not NaN.
5278 if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum))
5279 Known.knownNot(fcNan);
5280
5281 if (IID == Intrinsic::maxnum) {
5282 // If at least one operand is known to be positive, the result must be
5283 // positive.
5284 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
5285 KnownLHS.isKnownNeverNaN()) ||
5286 (KnownRHS.cannotBeOrderedLessThanZero() &&
5287 KnownRHS.isKnownNeverNaN()))
5289 } else if (IID == Intrinsic::maximum) {
5290 // If at least one operand is known to be positive, the result must be
5291 // positive.
5292 if (KnownLHS.cannotBeOrderedLessThanZero() ||
5293 KnownRHS.cannotBeOrderedLessThanZero())
5295 } else if (IID == Intrinsic::minnum) {
5296 // If at least one operand is known to be negative, the result must be
5297 // negative.
5298 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
5299 KnownLHS.isKnownNeverNaN()) ||
5300 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
5301 KnownRHS.isKnownNeverNaN()))
5303 } else {
5304 // If at least one operand is known to be negative, the result must be
5305 // negative.
5306 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
5309 }
5310
5311 // Fixup zero handling if denormals could be returned as a zero.
5312 //
5313 // As there's no spec for denormal flushing, be conservative with the
5314 // treatment of denormals that could be flushed to zero. For older
5315 // subtargets on AMDGPU the min/max instructions would not flush the
5316 // output and return the original value.
5317 //
5318 if ((Known.KnownFPClasses & fcZero) != fcNone &&
5319 !Known.isKnownNeverSubnormal()) {
5320 const Function *Parent = II->getFunction();
5321 if (!Parent)
5322 break;
5323
5324 DenormalMode Mode = Parent->getDenormalMode(
5325 II->getType()->getScalarType()->getFltSemantics());
5326 if (Mode != DenormalMode::getIEEE())
5327 Known.KnownFPClasses |= fcZero;
5328 }
5329
5330 if (Known.isKnownNeverNaN()) {
5331 if (KnownLHS.SignBit && KnownRHS.SignBit &&
5332 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5333 if (*KnownLHS.SignBit)
5334 Known.signBitMustBeOne();
5335 else
5336 Known.signBitMustBeZero();
5337 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum) ||
5338 ((KnownLHS.isKnownNeverNegZero() ||
5339 KnownRHS.isKnownNeverPosZero()) &&
5340 (KnownLHS.isKnownNeverPosZero() ||
5341 KnownRHS.isKnownNeverNegZero()))) {
5342 if ((IID == Intrinsic::maximum || IID == Intrinsic::maxnum) &&
5343 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5344 Known.signBitMustBeZero();
5345 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minnum) &&
5346 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5347 Known.signBitMustBeOne();
5348 }
5349 }
5350 break;
5351 }
5352 case Intrinsic::canonicalize: {
5353 KnownFPClass KnownSrc;
5354 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5355 KnownSrc, Depth + 1, Q);
5356
5357 // This is essentially a stronger form of
5358 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5359 // actually have an IR canonicalization guarantee.
5360
5361 // Canonicalize may flush denormals to zero, so we have to consider the
5362 // denormal mode to preserve known-not-0 knowledge.
5363 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5364
5365 // Stronger version of propagateNaN
5366 // Canonicalize is guaranteed to quiet signaling nans.
5367 if (KnownSrc.isKnownNeverNaN())
5368 Known.knownNot(fcNan);
5369 else
5370 Known.knownNot(fcSNan);
5371
5372 const Function *F = II->getFunction();
5373 if (!F)
5374 break;
5375
5376 // If the parent function flushes denormals, the canonical output cannot
5377 // be a denormal.
5378 const fltSemantics &FPType =
5379 II->getType()->getScalarType()->getFltSemantics();
5380 DenormalMode DenormMode = F->getDenormalMode(FPType);
5381 if (DenormMode == DenormalMode::getIEEE()) {
5382 if (KnownSrc.isKnownNever(fcPosZero))
5383 Known.knownNot(fcPosZero);
5384 if (KnownSrc.isKnownNever(fcNegZero))
5385 Known.knownNot(fcNegZero);
5386 break;
5387 }
5388
5389 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5390 Known.knownNot(fcSubnormal);
5391
5392 if (DenormMode.Input == DenormalMode::PositiveZero ||
5393 (DenormMode.Output == DenormalMode::PositiveZero &&
5394 DenormMode.Input == DenormalMode::IEEE))
5395 Known.knownNot(fcNegZero);
5396
5397 break;
5398 }
5399 case Intrinsic::vector_reduce_fmax:
5400 case Intrinsic::vector_reduce_fmin:
5401 case Intrinsic::vector_reduce_fmaximum:
5402 case Intrinsic::vector_reduce_fminimum: {
5403 // reduce min/max will choose an element from one of the vector elements,
5404 // so we can infer and class information that is common to all elements.
5405 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5406 InterestedClasses, Depth + 1, Q);
5407 // Can only propagate sign if output is never NaN.
5408 if (!Known.isKnownNeverNaN())
5409 Known.SignBit.reset();
5410 break;
5411 }
5412 // reverse preserves all characteristics of the input vec's element.
5413 case Intrinsic::vector_reverse:
5414 Known = computeKnownFPClass(
5415 II->getArgOperand(0), DemandedElts.reverseBits(),
5416 II->getFastMathFlags(), InterestedClasses, Depth + 1, Q);
5417 break;
5418 case Intrinsic::trunc:
5419 case Intrinsic::floor:
5420 case Intrinsic::ceil:
5421 case Intrinsic::rint:
5422 case Intrinsic::nearbyint:
5423 case Intrinsic::round:
5424 case Intrinsic::roundeven: {
5425 KnownFPClass KnownSrc;
5426 FPClassTest InterestedSrcs = InterestedClasses;
5427 if (InterestedSrcs & fcPosFinite)
5428 InterestedSrcs |= fcPosFinite;
5429 if (InterestedSrcs & fcNegFinite)
5430 InterestedSrcs |= fcNegFinite;
5431 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5432 KnownSrc, Depth + 1, Q);
5433
5434 // Integer results cannot be subnormal.
5435 Known.knownNot(fcSubnormal);
5436
5437 Known.propagateNaN(KnownSrc, true);
5438
5439 // Pass through infinities, except PPC_FP128 is a special case for
5440 // intrinsics other than trunc.
5441 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5442 if (KnownSrc.isKnownNeverPosInfinity())
5443 Known.knownNot(fcPosInf);
5444 if (KnownSrc.isKnownNeverNegInfinity())
5445 Known.knownNot(fcNegInf);
5446 }
5447
5448 // Negative round ups to 0 produce -0
5449 if (KnownSrc.isKnownNever(fcPosFinite))
5450 Known.knownNot(fcPosFinite);
5451 if (KnownSrc.isKnownNever(fcNegFinite))
5452 Known.knownNot(fcNegFinite);
5453
5454 break;
5455 }
5456 case Intrinsic::exp:
5457 case Intrinsic::exp2:
5458 case Intrinsic::exp10: {
5459 Known.knownNot(fcNegative);
5460 if ((InterestedClasses & fcNan) == fcNone)
5461 break;
5462
5463 KnownFPClass KnownSrc;
5464 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5465 KnownSrc, Depth + 1, Q);
5466 if (KnownSrc.isKnownNeverNaN()) {
5467 Known.knownNot(fcNan);
5468 Known.signBitMustBeZero();
5469 }
5470
5471 break;
5472 }
5473 case Intrinsic::fptrunc_round: {
5474 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5475 Depth, Q);
5476 break;
5477 }
5478 case Intrinsic::log:
5479 case Intrinsic::log10:
5480 case Intrinsic::log2:
5481 case Intrinsic::experimental_constrained_log:
5482 case Intrinsic::experimental_constrained_log10:
5483 case Intrinsic::experimental_constrained_log2: {
5484 // log(+inf) -> +inf
5485 // log([+-]0.0) -> -inf
5486 // log(-inf) -> nan
5487 // log(-x) -> nan
5488 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5489 break;
5490
5491 FPClassTest InterestedSrcs = InterestedClasses;
5492 if ((InterestedClasses & fcNegInf) != fcNone)
5493 InterestedSrcs |= fcZero | fcSubnormal;
5494 if ((InterestedClasses & fcNan) != fcNone)
5495 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5496
5497 KnownFPClass KnownSrc;
5498 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5499 KnownSrc, Depth + 1, Q);
5500
5501 if (KnownSrc.isKnownNeverPosInfinity())
5502 Known.knownNot(fcPosInf);
5503
5504 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5505 Known.knownNot(fcNan);
5506
5507 const Function *F = II->getFunction();
5508 if (F && KnownSrc.isKnownNeverLogicalZero(*F, II->getType()))
5509 Known.knownNot(fcNegInf);
5510
5511 break;
5512 }
5513 case Intrinsic::powi: {
5514 if ((InterestedClasses & fcNegative) == fcNone)
5515 break;
5516
5517 const Value *Exp = II->getArgOperand(1);
5518 Type *ExpTy = Exp->getType();
5519 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5520 KnownBits ExponentKnownBits(BitWidth);
5521 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5522 ExponentKnownBits, Depth + 1, Q);
5523
5524 if (ExponentKnownBits.Zero[0]) { // Is even
5525 Known.knownNot(fcNegative);
5526 break;
5527 }
5528
5529 // Given that exp is an integer, here are the
5530 // ways that pow can return a negative value:
5531 //
5532 // pow(-x, exp) --> negative if exp is odd and x is negative.
5533 // pow(-0, exp) --> -inf if exp is negative odd.
5534 // pow(-0, exp) --> -0 if exp is positive odd.
5535 // pow(-inf, exp) --> -0 if exp is negative odd.
5536 // pow(-inf, exp) --> -inf if exp is positive odd.
5537 KnownFPClass KnownSrc;
5538 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5539 KnownSrc, Depth + 1, Q);
5540 if (KnownSrc.isKnownNever(fcNegative))
5541 Known.knownNot(fcNegative);
5542 break;
5543 }
5544 case Intrinsic::ldexp: {
5545 KnownFPClass KnownSrc;
5546 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5547 KnownSrc, Depth + 1, Q);
5548 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5549
5550 // Sign is preserved, but underflows may produce zeroes.
5551 if (KnownSrc.isKnownNever(fcNegative))
5552 Known.knownNot(fcNegative);
5553 else if (KnownSrc.cannotBeOrderedLessThanZero())
5555
5556 if (KnownSrc.isKnownNever(fcPositive))
5557 Known.knownNot(fcPositive);
5558 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5560
5561 // Can refine inf/zero handling based on the exponent operand.
5562 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5563 if ((InterestedClasses & ExpInfoMask) == fcNone)
5564 break;
5565 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5566 break;
5567
5568 const fltSemantics &Flt =
5569 II->getType()->getScalarType()->getFltSemantics();
5570 unsigned Precision = APFloat::semanticsPrecision(Flt);
5571 const Value *ExpArg = II->getArgOperand(1);
5573 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5574
5575 const int MantissaBits = Precision - 1;
5576 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5577 Known.knownNot(fcSubnormal);
5578
5579 const Function *F = II->getFunction();
5580 const APInt *ConstVal = ExpRange.getSingleElement();
5581 if (ConstVal && ConstVal->isZero()) {
5582 // ldexp(x, 0) -> x, so propagate everything.
5583 Known.propagateCanonicalizingSrc(KnownSrc, *F, II->getType());
5584 } else if (ExpRange.isAllNegative()) {
5585 // If we know the power is <= 0, can't introduce inf
5586 if (KnownSrc.isKnownNeverPosInfinity())
5587 Known.knownNot(fcPosInf);
5588 if (KnownSrc.isKnownNeverNegInfinity())
5589 Known.knownNot(fcNegInf);
5590 } else if (ExpRange.isAllNonNegative()) {
5591 // If we know the power is >= 0, can't introduce subnormal or zero
5592 if (KnownSrc.isKnownNeverPosSubnormal())
5593 Known.knownNot(fcPosSubnormal);
5594 if (KnownSrc.isKnownNeverNegSubnormal())
5595 Known.knownNot(fcNegSubnormal);
5596 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, II->getType()))
5597 Known.knownNot(fcPosZero);
5598 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))
5599 Known.knownNot(fcNegZero);
5600 }
5601
5602 break;
5603 }
5604 case Intrinsic::arithmetic_fence: {
5605 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5606 Known, Depth + 1, Q);
5607 break;
5608 }
5609 case Intrinsic::experimental_constrained_sitofp:
5610 case Intrinsic::experimental_constrained_uitofp:
5611 // Cannot produce nan
5612 Known.knownNot(fcNan);
5613
5614 // sitofp and uitofp turn into +0.0 for zero.
5615 Known.knownNot(fcNegZero);
5616
5617 // Integers cannot be subnormal
5618 Known.knownNot(fcSubnormal);
5619
5620 if (IID == Intrinsic::experimental_constrained_uitofp)
5621 Known.signBitMustBeZero();
5622
5623 // TODO: Copy inf handling from instructions
5624 break;
5625 default:
5626 break;
5627 }
5628
5629 break;
5630 }
5631 case Instruction::FAdd:
5632 case Instruction::FSub: {
5633 KnownFPClass KnownLHS, KnownRHS;
5634 bool WantNegative =
5635 Op->getOpcode() == Instruction::FAdd &&
5636 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5637 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5638 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5639
5640 if (!WantNaN && !WantNegative && !WantNegZero)
5641 break;
5642
5643 FPClassTest InterestedSrcs = InterestedClasses;
5644 if (WantNegative)
5645 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5646 if (InterestedClasses & fcNan)
5647 InterestedSrcs |= fcInf;
5648 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5649 KnownRHS, Depth + 1, Q);
5650
5651 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5652 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5653 WantNegZero || Opc == Instruction::FSub) {
5654
5655 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5656 // there's no point.
5657 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5658 KnownLHS, Depth + 1, Q);
5659 // Adding positive and negative infinity produces NaN.
5660 // TODO: Check sign of infinities.
5661 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5662 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5663 Known.knownNot(fcNan);
5664
5665 // FIXME: Context function should always be passed in separately
5666 const Function *F = cast<Instruction>(Op)->getFunction();
5667
5668 if (Op->getOpcode() == Instruction::FAdd) {
5669 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5670 KnownRHS.cannotBeOrderedLessThanZero())
5672 if (!F)
5673 break;
5674
5675 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5676 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5677 KnownRHS.isKnownNeverLogicalNegZero(*F, Op->getType())) &&
5678 // Make sure output negative denormal can't flush to -0
5679 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5680 Known.knownNot(fcNegZero);
5681 } else {
5682 if (!F)
5683 break;
5684
5685 // Only fsub -0, +0 can return -0
5686 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5687 KnownRHS.isKnownNeverLogicalPosZero(*F, Op->getType())) &&
5688 // Make sure output negative denormal can't flush to -0
5689 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5690 Known.knownNot(fcNegZero);
5691 }
5692 }
5693
5694 break;
5695 }
5696 case Instruction::FMul: {
5697 // X * X is always non-negative or a NaN.
5698 if (Op->getOperand(0) == Op->getOperand(1))
5699 Known.knownNot(fcNegative);
5700
5701 if ((InterestedClasses & fcNan) != fcNan)
5702 break;
5703
5704 // fcSubnormal is only needed in case of DAZ.
5705 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5706
5707 KnownFPClass KnownLHS, KnownRHS;
5708 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5709 Depth + 1, Q);
5710 if (!KnownRHS.isKnownNeverNaN())
5711 break;
5712
5713 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5714 Depth + 1, Q);
5715 if (!KnownLHS.isKnownNeverNaN())
5716 break;
5717
5718 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5719 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5720 Known.signBitMustBeZero();
5721 else
5722 Known.signBitMustBeOne();
5723 }
5724
5725 // If 0 * +/-inf produces NaN.
5726 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5727 Known.knownNot(fcNan);
5728 break;
5729 }
5730
5731 const Function *F = cast<Instruction>(Op)->getFunction();
5732 if (!F)
5733 break;
5734
5735 if ((KnownRHS.isKnownNeverInfinity() ||
5736 KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) &&
5737 (KnownLHS.isKnownNeverInfinity() ||
5738 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))
5739 Known.knownNot(fcNan);
5740
5741 break;
5742 }
5743 case Instruction::FDiv:
5744 case Instruction::FRem: {
5745 if (Op->getOperand(0) == Op->getOperand(1)) {
5746 // TODO: Could filter out snan if we inspect the operand
5747 if (Op->getOpcode() == Instruction::FDiv) {
5748 // X / X is always exactly 1.0 or a NaN.
5750 } else {
5751 // X % X is always exactly [+-]0.0 or a NaN.
5752 Known.KnownFPClasses = fcNan | fcZero;
5753 }
5754
5755 break;
5756 }
5757
5758 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5759 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5760 const bool WantPositive =
5761 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5762 if (!WantNan && !WantNegative && !WantPositive)
5763 break;
5764
5765 KnownFPClass KnownLHS, KnownRHS;
5766
5767 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5768 fcNan | fcInf | fcZero | fcNegative, KnownRHS,
5769 Depth + 1, Q);
5770
5771 bool KnowSomethingUseful =
5772 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5773
5774 if (KnowSomethingUseful || WantPositive) {
5775 const FPClassTest InterestedLHS =
5776 WantPositive ? fcAllFlags
5778
5779 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5780 InterestedClasses & InterestedLHS, KnownLHS,
5781 Depth + 1, Q);
5782 }
5783
5784 const Function *F = cast<Instruction>(Op)->getFunction();
5785
5786 if (Op->getOpcode() == Instruction::FDiv) {
5787 // Only 0/0, Inf/Inf produce NaN.
5788 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5789 (KnownLHS.isKnownNeverInfinity() ||
5790 KnownRHS.isKnownNeverInfinity()) &&
5791 ((F && KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) ||
5792 (F && KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))) {
5793 Known.knownNot(fcNan);
5794 }
5795
5796 // X / -0.0 is -Inf (or NaN).
5797 // +X / +X is +X
5798 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5799 Known.knownNot(fcNegative);
5800 } else {
5801 // Inf REM x and x REM 0 produce NaN.
5802 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5803 KnownLHS.isKnownNeverInfinity() && F &&
5804 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())) {
5805 Known.knownNot(fcNan);
5806 }
5807
5808 // The sign for frem is the same as the first operand.
5809 if (KnownLHS.cannotBeOrderedLessThanZero())
5811 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5813
5814 // See if we can be more aggressive about the sign of 0.
5815 if (KnownLHS.isKnownNever(fcNegative))
5816 Known.knownNot(fcNegative);
5817 if (KnownLHS.isKnownNever(fcPositive))
5818 Known.knownNot(fcPositive);
5819 }
5820
5821 break;
5822 }
5823 case Instruction::FPExt: {
5824 // Infinity, nan and zero propagate from source.
5825 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5826 Known, Depth + 1, Q);
5827
5828 const fltSemantics &DstTy =
5829 Op->getType()->getScalarType()->getFltSemantics();
5830 const fltSemantics &SrcTy =
5831 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5832
5833 // All subnormal inputs should be in the normal range in the result type.
5834 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5835 if (Known.KnownFPClasses & fcPosSubnormal)
5836 Known.KnownFPClasses |= fcPosNormal;
5837 if (Known.KnownFPClasses & fcNegSubnormal)
5838 Known.KnownFPClasses |= fcNegNormal;
5839 Known.knownNot(fcSubnormal);
5840 }
5841
5842 // Sign bit of a nan isn't guaranteed.
5843 if (!Known.isKnownNeverNaN())
5844 Known.SignBit = std::nullopt;
5845 break;
5846 }
5847 case Instruction::FPTrunc: {
5848 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5849 Depth, Q);
5850 break;
5851 }
5852 case Instruction::SIToFP:
5853 case Instruction::UIToFP: {
5854 // Cannot produce nan
5855 Known.knownNot(fcNan);
5856
5857 // Integers cannot be subnormal
5858 Known.knownNot(fcSubnormal);
5859
5860 // sitofp and uitofp turn into +0.0 for zero.
5861 Known.knownNot(fcNegZero);
5862 if (Op->getOpcode() == Instruction::UIToFP)
5863 Known.signBitMustBeZero();
5864
5865 if (InterestedClasses & fcInf) {
5866 // Get width of largest magnitude integer (remove a bit if signed).
5867 // This still works for a signed minimum value because the largest FP
5868 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5869 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5870 if (Op->getOpcode() == Instruction::SIToFP)
5871 --IntSize;
5872
5873 // If the exponent of the largest finite FP value can hold the largest
5874 // integer, the result of the cast must be finite.
5875 Type *FPTy = Op->getType()->getScalarType();
5876 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5877 Known.knownNot(fcInf);
5878 }
5879
5880 break;
5881 }
5882 case Instruction::ExtractElement: {
5883 // Look through extract element. If the index is non-constant or
5884 // out-of-range demand all elements, otherwise just the extracted element.
5885 const Value *Vec = Op->getOperand(0);
5886 const Value *Idx = Op->getOperand(1);
5887 auto *CIdx = dyn_cast<ConstantInt>(Idx);
5888
5889 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5890 unsigned NumElts = VecTy->getNumElements();
5891 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
5892 if (CIdx && CIdx->getValue().ult(NumElts))
5893 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5894 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5895 Depth + 1, Q);
5896 }
5897
5898 break;
5899 }
5900 case Instruction::InsertElement: {
5901 if (isa<ScalableVectorType>(Op->getType()))
5902 return;
5903
5904 const Value *Vec = Op->getOperand(0);
5905 const Value *Elt = Op->getOperand(1);
5906 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5907 unsigned NumElts = DemandedElts.getBitWidth();
5908 APInt DemandedVecElts = DemandedElts;
5909 bool NeedsElt = true;
5910 // If we know the index we are inserting to, clear it from Vec check.
5911 if (CIdx && CIdx->getValue().ult(NumElts)) {
5912 DemandedVecElts.clearBit(CIdx->getZExtValue());
5913 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5914 }
5915
5916 // Do we demand the inserted element?
5917 if (NeedsElt) {
5918 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1, Q);
5919 // If we don't know any bits, early out.
5920 if (Known.isUnknown())
5921 break;
5922 } else {
5923 Known.KnownFPClasses = fcNone;
5924 }
5925
5926 // Do we need anymore elements from Vec?
5927 if (!DemandedVecElts.isZero()) {
5928 KnownFPClass Known2;
5929 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
5930 Depth + 1, Q);
5931 Known |= Known2;
5932 }
5933
5934 break;
5935 }
5936 case Instruction::ShuffleVector: {
5937 // For undef elements, we don't know anything about the common state of
5938 // the shuffle result.
5939 APInt DemandedLHS, DemandedRHS;
5940 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5941 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5942 return;
5943
5944 if (!!DemandedLHS) {
5945 const Value *LHS = Shuf->getOperand(0);
5946 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
5947 Depth + 1, Q);
5948
5949 // If we don't know any bits, early out.
5950 if (Known.isUnknown())
5951 break;
5952 } else {
5953 Known.KnownFPClasses = fcNone;
5954 }
5955
5956 if (!!DemandedRHS) {
5957 KnownFPClass Known2;
5958 const Value *RHS = Shuf->getOperand(1);
5959 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
5960 Depth + 1, Q);
5961 Known |= Known2;
5962 }
5963
5964 break;
5965 }
5966 case Instruction::ExtractValue: {
5967 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5968 ArrayRef<unsigned> Indices = Extract->getIndices();
5969 const Value *Src = Extract->getAggregateOperand();
5970 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5971 Indices[0] == 0) {
5972 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5973 switch (II->getIntrinsicID()) {
5974 case Intrinsic::frexp: {
5975 Known.knownNot(fcSubnormal);
5976
5977 KnownFPClass KnownSrc;
5978 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5979 InterestedClasses, KnownSrc, Depth + 1, Q);
5980
5981 const Function *F = cast<Instruction>(Op)->getFunction();
5982
5983 if (KnownSrc.isKnownNever(fcNegative))
5984 Known.knownNot(fcNegative);
5985 else {
5986 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, Op->getType()))
5987 Known.knownNot(fcNegZero);
5988 if (KnownSrc.isKnownNever(fcNegInf))
5989 Known.knownNot(fcNegInf);
5990 }
5991
5992 if (KnownSrc.isKnownNever(fcPositive))
5993 Known.knownNot(fcPositive);
5994 else {
5995 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, Op->getType()))
5996 Known.knownNot(fcPosZero);
5997 if (KnownSrc.isKnownNever(fcPosInf))
5998 Known.knownNot(fcPosInf);
5999 }
6000
6001 Known.propagateNaN(KnownSrc);
6002 return;
6003 }
6004 default:
6005 break;
6006 }
6007 }
6008 }
6009
6010 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1,
6011 Q);
6012 break;
6013 }
6014 case Instruction::PHI: {
6015 const PHINode *P = cast<PHINode>(Op);
6016 // Unreachable blocks may have zero-operand PHI nodes.
6017 if (P->getNumIncomingValues() == 0)
6018 break;
6019
6020 // Otherwise take the unions of the known bit sets of the operands,
6021 // taking conservative care to avoid excessive recursion.
6022 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6023
6024 if (Depth < PhiRecursionLimit) {
6025 // Skip if every incoming value references to ourself.
6026 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6027 break;
6028
6029 bool First = true;
6030
6031 for (const Use &U : P->operands()) {
6032 Value *IncValue = U.get();
6033 // Skip direct self references.
6034 if (IncValue == P)
6035 continue;
6036
6037 Instruction *CxtI = P->getIncomingBlock(U)->getTerminator();
6038
6039 // If the Use is a select of this phi, use the fp class of the other
6040 // operand to break the recursion. Same around 2-operand phi nodes
6041 Value *V;
6042 if (match(IncValue, m_Select(m_Value(), m_Specific(P), m_Value(V))) ||
6043 match(IncValue, m_Select(m_Value(), m_Value(V), m_Specific(P)))) {
6044 IncValue = V;
6045 } else if (auto *IncPhi = dyn_cast<PHINode>(IncValue);
6046 IncPhi && IncPhi->getNumIncomingValues() == 2) {
6047 for (int Idx = 0; Idx < 2; ++Idx) {
6048 if (IncPhi->getIncomingValue(Idx) == P) {
6049 IncValue = IncPhi->getIncomingValue(1 - Idx);
6050 CxtI = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
6051 break;
6052 }
6053 }
6054 }
6055
6056 KnownFPClass KnownSrc;
6057 // Recurse, but cap the recursion to two levels, because we don't want
6058 // to waste time spinning around in loops. We need at least depth 2 to
6059 // detect known sign bits.
6060 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6061 PhiRecursionLimit,
6063
6064 if (First) {
6065 Known = KnownSrc;
6066 First = false;
6067 } else {
6068 Known |= KnownSrc;
6069 }
6070
6071 if (Known.KnownFPClasses == fcAllFlags)
6072 break;
6073 }
6074 }
6075
6076 break;
6077 }
6078 case Instruction::BitCast: {
6079 const Value *Src;
6080 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6081 !Src->getType()->isIntOrIntVectorTy())
6082 break;
6083
6084 const Type *Ty = Op->getType()->getScalarType();
6085 KnownBits Bits(Ty->getScalarSizeInBits());
6086 computeKnownBits(Src, DemandedElts, Bits, Depth + 1, Q);
6087
6088 // Transfer information from the sign bit.
6089 if (Bits.isNonNegative())
6090 Known.signBitMustBeZero();
6091 else if (Bits.isNegative())
6092 Known.signBitMustBeOne();
6093
6094 if (Ty->isIEEE()) {
6095 // IEEE floats are NaN when all bits of the exponent plus at least one of
6096 // the fraction bits are 1. This means:
6097 // - If we assume unknown bits are 0 and the value is NaN, it will
6098 // always be NaN
6099 // - If we assume unknown bits are 1 and the value is not NaN, it can
6100 // never be NaN
6101 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
6102 Known.KnownFPClasses = fcNan;
6103 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
6104 Known.knownNot(fcNan);
6105
6106 // Build KnownBits representing Inf and check if it must be equal or
6107 // unequal to this value.
6108 auto InfKB = KnownBits::makeConstant(
6110 InfKB.Zero.clearSignBit();
6111 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
6112 assert(!InfResult.value());
6113 Known.knownNot(fcInf);
6114 } else if (Bits == InfKB) {
6115 Known.KnownFPClasses = fcInf;
6116 }
6117
6118 // Build KnownBits representing Zero and check if it must be equal or
6119 // unequal to this value.
6120 auto ZeroKB = KnownBits::makeConstant(
6122 ZeroKB.Zero.clearSignBit();
6123 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
6124 assert(!ZeroResult.value());
6125 Known.knownNot(fcZero);
6126 } else if (Bits == ZeroKB) {
6127 Known.KnownFPClasses = fcZero;
6128 }
6129 }
6130
6131 break;
6132 }
6133 default:
6134 break;
6135 }
6136}
6137
6139 const APInt &DemandedElts,
6140 FPClassTest InterestedClasses,
6141 unsigned Depth,
6142 const SimplifyQuery &SQ) {
6143 KnownFPClass KnownClasses;
6144 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, Depth,
6145 SQ);
6146 return KnownClasses;
6147}
6148
6150 FPClassTest InterestedClasses,
6151 unsigned Depth,
6152 const SimplifyQuery &SQ) {
6153 KnownFPClass Known;
6154 ::computeKnownFPClass(V, Known, InterestedClasses, Depth, SQ);
6155 return Known;
6156}
6157
6159
6160 // All byte-wide stores are splatable, even of arbitrary variables.
6161 if (V->getType()->isIntegerTy(8))
6162 return V;
6163
6164 LLVMContext &Ctx = V->getContext();
6165
6166 // Undef don't care.
6167 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6168 if (isa<UndefValue>(V))
6169 return UndefInt8;
6170
6171 // Return Undef for zero-sized type.
6172 if (DL.getTypeStoreSize(V->getType()).isZero())
6173 return UndefInt8;
6174
6175 Constant *C = dyn_cast<Constant>(V);
6176 if (!C) {
6177 // Conceptually, we could handle things like:
6178 // %a = zext i8 %X to i16
6179 // %b = shl i16 %a, 8
6180 // %c = or i16 %a, %b
6181 // but until there is an example that actually needs this, it doesn't seem
6182 // worth worrying about.
6183 return nullptr;
6184 }
6185
6186 // Handle 'null' ConstantArrayZero etc.
6187 if (C->isNullValue())
6189
6190 // Constant floating-point values can be handled as integer values if the
6191 // corresponding integer value is "byteable". An important case is 0.0.
6192 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6193 Type *Ty = nullptr;
6194 if (CFP->getType()->isHalfTy())
6195 Ty = Type::getInt16Ty(Ctx);
6196 else if (CFP->getType()->isFloatTy())
6197 Ty = Type::getInt32Ty(Ctx);
6198 else if (CFP->getType()->isDoubleTy())
6199 Ty = Type::getInt64Ty(Ctx);
6200 // Don't handle long double formats, which have strange constraints.
6201 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6202 : nullptr;
6203 }
6204
6205 // We can handle constant integers that are multiple of 8 bits.
6206 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6207 if (CI->getBitWidth() % 8 == 0) {
6208 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6209 if (!CI->getValue().isSplat(8))
6210 return nullptr;
6211 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6212 }
6213 }
6214
6215 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6216 if (CE->getOpcode() == Instruction::IntToPtr) {
6217 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6218 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6220 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6221 return isBytewiseValue(Op, DL);
6222 }
6223 }
6224 }
6225
6226 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6227 if (LHS == RHS)
6228 return LHS;
6229 if (!LHS || !RHS)
6230 return nullptr;
6231 if (LHS == UndefInt8)
6232 return RHS;
6233 if (RHS == UndefInt8)
6234 return LHS;
6235 return nullptr;
6236 };
6237
6238 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
6239 Value *Val = UndefInt8;
6240 for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
6241 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6242 return nullptr;
6243 return Val;
6244 }
6245
6246 if (isa<ConstantAggregate>(C)) {
6247 Value *Val = UndefInt8;
6248 for (Value *Op : C->operands())
6249 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6250 return nullptr;
6251 return Val;
6252 }
6253
6254 // Don't try to handle the handful of other constants.
6255 return nullptr;
6256}
6257
6258// This is the recursive version of BuildSubAggregate. It takes a few different
6259// arguments. Idxs is the index within the nested struct From that we are
6260// looking at now (which is of type IndexedType). IdxSkip is the number of
6261// indices from Idxs that should be left out when inserting into the resulting
6262// struct. To is the result struct built so far, new insertvalue instructions
6263// build on that.
6264static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6266 unsigned IdxSkip,
6267 BasicBlock::iterator InsertBefore) {
6268 StructType *STy = dyn_cast<StructType>(IndexedType);
6269 if (STy) {
6270 // Save the original To argument so we can modify it
6271 Value *OrigTo = To;
6272 // General case, the type indexed by Idxs is a struct
6273 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6274 // Process each struct element recursively
6275 Idxs.push_back(i);
6276 Value *PrevTo = To;
6277 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6278 InsertBefore);
6279 Idxs.pop_back();
6280 if (!To) {
6281 // Couldn't find any inserted value for this index? Cleanup
6282 while (PrevTo != OrigTo) {
6283 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
6284 PrevTo = Del->getAggregateOperand();
6285 Del->eraseFromParent();
6286 }
6287 // Stop processing elements
6288 break;
6289 }
6290 }
6291 // If we successfully found a value for each of our subaggregates
6292 if (To)
6293 return To;
6294 }
6295 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6296 // the struct's elements had a value that was inserted directly. In the latter
6297 // case, perhaps we can't determine each of the subelements individually, but
6298 // we might be able to find the complete struct somewhere.
6299
6300 // Find the value that is at that particular spot
6301 Value *V = FindInsertedValue(From, Idxs);
6302
6303 if (!V)
6304 return nullptr;
6305
6306 // Insert the value in the new (sub) aggregate
6307 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6308 InsertBefore);
6309}
6310
6311// This helper takes a nested struct and extracts a part of it (which is again a
6312// struct) into a new value. For example, given the struct:
6313// { a, { b, { c, d }, e } }
6314// and the indices "1, 1" this returns
6315// { c, d }.
6316//
6317// It does this by inserting an insertvalue for each element in the resulting
6318// struct, as opposed to just inserting a single struct. This will only work if
6319// each of the elements of the substruct are known (ie, inserted into From by an
6320// insertvalue instruction somewhere).
6321//
6322// All inserted insertvalue instructions are inserted before InsertBefore
6324 BasicBlock::iterator InsertBefore) {
6325 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6326 idx_range);
6327 Value *To = PoisonValue::get(IndexedType);
6328 SmallVector<unsigned, 10> Idxs(idx_range);
6329 unsigned IdxSkip = Idxs.size();
6330
6331 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6332}
6333
6334/// Given an aggregate and a sequence of indices, see if the scalar value
6335/// indexed is already around as a register, for example if it was inserted
6336/// directly into the aggregate.
6337///
6338/// If InsertBefore is not null, this function will duplicate (modified)
6339/// insertvalues when a part of a nested struct is extracted.
6340Value *
6342 std::optional<BasicBlock::iterator> InsertBefore) {
6343 // Nothing to index? Just return V then (this is useful at the end of our
6344 // recursion).
6345 if (idx_range.empty())
6346 return V;
6347 // We have indices, so V should have an indexable type.
6348 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6349 "Not looking at a struct or array?");
6350 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6351 "Invalid indices for type?");
6352
6353 if (Constant *C = dyn_cast<Constant>(V)) {
6354 C = C->getAggregateElement(idx_range[0]);
6355 if (!C) return nullptr;
6356 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6357 }
6358
6359 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
6360 // Loop the indices for the insertvalue instruction in parallel with the
6361 // requested indices
6362 const unsigned *req_idx = idx_range.begin();
6363 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6364 i != e; ++i, ++req_idx) {
6365 if (req_idx == idx_range.end()) {
6366 // We can't handle this without inserting insertvalues
6367 if (!InsertBefore)
6368 return nullptr;
6369
6370 // The requested index identifies a part of a nested aggregate. Handle
6371 // this specially. For example,
6372 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6373 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6374 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6375 // This can be changed into
6376 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6377 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6378 // which allows the unused 0,0 element from the nested struct to be
6379 // removed.
6380 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6381 *InsertBefore);
6382 }
6383
6384 // This insert value inserts something else than what we are looking for.
6385 // See if the (aggregate) value inserted into has the value we are
6386 // looking for, then.
6387 if (*req_idx != *i)
6388 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6389 InsertBefore);
6390 }
6391 // If we end up here, the indices of the insertvalue match with those
6392 // requested (though possibly only partially). Now we recursively look at
6393 // the inserted value, passing any remaining indices.
6394 return FindInsertedValue(I->getInsertedValueOperand(),
6395 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6396 }
6397
6398 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
6399 // If we're extracting a value from an aggregate that was extracted from
6400 // something else, we can extract from that something else directly instead.
6401 // However, we will need to chain I's indices with the requested indices.
6402
6403 // Calculate the number of indices required
6404 unsigned size = I->getNumIndices() + idx_range.size();
6405 // Allocate some space to put the new indices in
6407 Idxs.reserve(size);
6408 // Add indices from the extract value instruction
6409 Idxs.append(I->idx_begin(), I->idx_end());
6410
6411 // Add requested indices
6412 Idxs.append(idx_range.begin(), idx_range.end());
6413
6414 assert(Idxs.size() == size
6415 && "Number of indices added not correct?");
6416
6417 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6418 }
6419 // Otherwise, we don't know (such as, extracting from a function return value
6420 // or load instruction)
6421 return nullptr;
6422}
6423
6425 unsigned CharSize) {
6426 // Make sure the GEP has exactly three arguments.
6427 if (GEP->getNumOperands() != 3)
6428 return false;
6429
6430 // Make sure the index-ee is a pointer to array of \p CharSize integers.
6431 // CharSize.
6432 ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
6433 if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
6434 return false;
6435
6436 // Check to make sure that the first operand of the GEP is an integer and
6437 // has value 0 so that we are sure we're indexing into the initializer.
6438 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
6439 if (!FirstIdx || !FirstIdx->isZero())
6440 return false;
6441
6442 return true;
6443}
6444
6445// If V refers to an initialized global constant, set Slice either to
6446// its initializer if the size of its elements equals ElementSize, or,
6447// for ElementSize == 8, to its representation as an array of unsiged
6448// char. Return true on success.
6449// Offset is in the unit "nr of ElementSize sized elements".
6452 unsigned ElementSize, uint64_t Offset) {
6453 assert(V && "V should not be null.");
6454 assert((ElementSize % 8) == 0 &&
6455 "ElementSize expected to be a multiple of the size of a byte.");
6456 unsigned ElementSizeInBytes = ElementSize / 8;
6457
6458 // Drill down into the pointer expression V, ignoring any intervening
6459 // casts, and determine the identity of the object it references along
6460 // with the cumulative byte offset into it.
6461 const GlobalVariable *GV =
6462 dyn_cast<GlobalVariable>(getUnderlyingObject(V));
6463 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6464 // Fail if V is not based on constant global object.
6465 return false;
6466
6467 const DataLayout &DL = GV->getDataLayout();
6468 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6469
6470 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6471 /*AllowNonInbounds*/ true))
6472 // Fail if a constant offset could not be determined.
6473 return false;
6474
6475 uint64_t StartIdx = Off.getLimitedValue();
6476 if (StartIdx == UINT64_MAX)
6477 // Fail if the constant offset is excessive.
6478 return false;
6479
6480 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6481 // elements. Simply bail out if that isn't possible.
6482 if ((StartIdx % ElementSizeInBytes) != 0)
6483 return false;
6484
6485 Offset += StartIdx / ElementSizeInBytes;
6486 ConstantDataArray *Array = nullptr;
6487 ArrayType *ArrayTy = nullptr;
6488
6489 if (GV->getInitializer()->isNullValue()) {
6490 Type *GVTy = GV->getValueType();
6491 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6492 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6493
6494 Slice.Array = nullptr;
6495 Slice.Offset = 0;
6496 // Return an empty Slice for undersized constants to let callers
6497 // transform even undefined library calls into simpler, well-defined
6498 // expressions. This is preferable to making the calls although it
6499 // prevents sanitizers from detecting such calls.
6500 Slice.Length = Length < Offset ? 0 : Length - Offset;
6501 return true;
6502 }
6503
6504 auto *Init = const_cast<Constant *>(GV->getInitializer());
6505 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6506 Type *InitElTy = ArrayInit->getElementType();
6507 if (InitElTy->isIntegerTy(ElementSize)) {
6508 // If Init is an initializer for an array of the expected type
6509 // and size, use it as is.
6510 Array = ArrayInit;
6511 ArrayTy = ArrayInit->getType();
6512 }
6513 }
6514
6515 if (!Array) {
6516 if (ElementSize != 8)
6517 // TODO: Handle conversions to larger integral types.
6518 return false;
6519
6520 // Otherwise extract the portion of the initializer starting
6521 // at Offset as an array of bytes, and reset Offset.
6523 if (!Init)
6524 return false;
6525
6526 Offset = 0;
6527 Array = dyn_cast<ConstantDataArray>(Init);
6528 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6529 }
6530
6531 uint64_t NumElts = ArrayTy->getArrayNumElements();
6532 if (Offset > NumElts)
6533 return false;
6534
6535 Slice.Array = Array;
6536 Slice.Offset = Offset;
6537 Slice.Length = NumElts - Offset;
6538 return true;
6539}
6540
6541/// Extract bytes from the initializer of the constant array V, which need
6542/// not be a nul-terminated string. On success, store the bytes in Str and
6543/// return true. When TrimAtNul is set, Str will contain only the bytes up
6544/// to but not including the first nul. Return false on failure.
6546 bool TrimAtNul) {
6548 if (!getConstantDataArrayInfo(V, Slice, 8))
6549 return false;
6550
6551 if (Slice.Array == nullptr) {
6552 if (TrimAtNul) {
6553 // Return a nul-terminated string even for an empty Slice. This is
6554 // safe because all existing SimplifyLibcalls callers require string
6555 // arguments and the behavior of the functions they fold is undefined
6556 // otherwise. Folding the calls this way is preferable to making
6557 // the undefined library calls, even though it prevents sanitizers
6558 // from reporting such calls.
6559 Str = StringRef();
6560 return true;
6561 }
6562 if (Slice.Length == 1) {
6563 Str = StringRef("", 1);
6564 return true;
6565 }
6566 // We cannot instantiate a StringRef as we do not have an appropriate string
6567 // of 0s at hand.
6568 return false;
6569 }
6570
6571 // Start out with the entire array in the StringRef.
6572 Str = Slice.Array->getAsString();
6573 // Skip over 'offset' bytes.
6574 Str = Str.substr(Slice.Offset);
6575
6576 if (TrimAtNul) {
6577 // Trim off the \0 and anything after it. If the array is not nul
6578 // terminated, we just return the whole end of string. The client may know
6579 // some other way that the string is length-bound.
6580 Str = Str.substr(0, Str.find('\0'));
6581 }
6582 return true;
6583}
6584
6585// These next two are very similar to the above, but also look through PHI
6586// nodes.
6587// TODO: See if we can integrate these two together.
6588
6589/// If we can compute the length of the string pointed to by
6590/// the specified pointer, return 'len+1'. If we can't, return 0.
6593 unsigned CharSize) {
6594 // Look through noop bitcast instructions.
6595 V = V->stripPointerCasts();
6596
6597 // If this is a PHI node, there are two cases: either we have already seen it
6598 // or we haven't.
6599 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6600 if (!PHIs.insert(PN).second)
6601 return ~0ULL; // already in the set.
6602
6603 // If it was new, see if all the input strings are the same length.
6604 uint64_t LenSoFar = ~0ULL;
6605 for (Value *IncValue : PN->incoming_values()) {
6606 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6607 if (Len == 0) return 0; // Unknown length -> unknown.
6608
6609 if (Len == ~0ULL) continue;
6610
6611 if (Len != LenSoFar && LenSoFar != ~0ULL)
6612 return 0; // Disagree -> unknown.
6613 LenSoFar = Len;
6614 }
6615
6616 // Success, all agree.
6617 return LenSoFar;
6618 }
6619
6620 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6621 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6622 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6623 if (Len1 == 0) return 0;
6624 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6625 if (Len2 == 0) return 0;
6626 if (Len1 == ~0ULL) return Len2;
6627 if (Len2 == ~0ULL) return Len1;
6628 if (Len1 != Len2) return 0;
6629 return Len1;
6630 }
6631
6632 // Otherwise, see if we can read the string.
6634 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6635 return 0;
6636
6637 if (Slice.Array == nullptr)
6638 // Zeroinitializer (including an empty one).
6639 return 1;
6640
6641 // Search for the first nul character. Return a conservative result even
6642 // when there is no nul. This is safe since otherwise the string function
6643 // being folded such as strlen is undefined, and can be preferable to
6644 // making the undefined library call.
6645 unsigned NullIndex = 0;
6646 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6647 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6648 break;
6649 }
6650
6651 return NullIndex + 1;
6652}
6653
6654/// If we can compute the length of the string pointed to by
6655/// the specified pointer, return 'len+1'. If we can't, return 0.
6656uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6657 if (!V->getType()->isPointerTy())
6658 return 0;
6659
6661 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6662 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6663 // an empty string as a length.
6664 return Len == ~0ULL ? 1 : Len;
6665}
6666
6667const Value *
6669 bool MustPreserveNullness) {
6670 assert(Call &&
6671 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6672 if (const Value *RV = Call->getReturnedArgOperand())
6673 return RV;
6674 // This can be used only as a aliasing property.
6676 Call, MustPreserveNullness))
6677 return Call->getArgOperand(0);
6678 return nullptr;
6679}
6680
6682 const CallBase *Call, bool MustPreserveNullness) {
6683 switch (Call->getIntrinsicID()) {
6684 case Intrinsic::launder_invariant_group:
6685 case Intrinsic::strip_invariant_group:
6686 case Intrinsic::aarch64_irg:
6687 case Intrinsic::aarch64_tagp:
6688 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6689 // input pointer (and thus preserve null-ness for the purposes of escape
6690 // analysis, which is where the MustPreserveNullness flag comes in to play).
6691 // However, it will not necessarily map ptr addrspace(N) null to ptr
6692 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6693 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6694 // list, no one should be relying on such a strict interpretation of
6695 // MustPreserveNullness (and, at time of writing, they are not), but we
6696 // document this fact out of an abundance of caution.
6697 case Intrinsic::amdgcn_make_buffer_rsrc:
6698 return true;
6699 case Intrinsic::ptrmask:
6700 return !MustPreserveNullness;
6701 case Intrinsic::threadlocal_address:
6702 // The underlying variable changes with thread ID. The Thread ID may change
6703 // at coroutine suspend points.
6704 return !Call->getParent()->getParent()->isPresplitCoroutine();
6705 default:
6706 return false;
6707 }
6708}
6709
6710/// \p PN defines a loop-variant pointer to an object. Check if the
6711/// previous iteration of the loop was referring to the same object as \p PN.
6713 const LoopInfo *LI) {
6714 // Find the loop-defined value.
6715 Loop *L = LI->getLoopFor(PN->getParent());
6716 if (PN->getNumIncomingValues() != 2)
6717 return true;
6718
6719 // Find the value from previous iteration.
6720 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6721 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6722 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6723 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6724 return true;
6725
6726 // If a new pointer is loaded in the loop, the pointer references a different
6727 // object in every iteration. E.g.:
6728 // for (i)
6729 // int *p = a[i];
6730 // ...
6731 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6732 if (!L->isLoopInvariant(Load->getPointerOperand()))
6733 return false;
6734 return true;
6735}
6736
6737const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6738 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6739 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6740 const Value *PtrOp = GEP->getPointerOperand();
6741 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6742 return V;
6743 V = PtrOp;
6744 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6745 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6746 Value *NewV = cast<Operator>(V)->getOperand(0);
6747 if (!NewV->getType()->isPointerTy())
6748 return V;
6749 V = NewV;
6750 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6751 if (GA->isInterposable())
6752 return V;
6753 V = GA->getAliasee();
6754 } else {
6755 if (auto *PHI = dyn_cast<PHINode>(V)) {
6756 // Look through single-arg phi nodes created by LCSSA.
6757 if (PHI->getNumIncomingValues() == 1) {
6758 V = PHI->getIncomingValue(0);
6759 continue;
6760 }
6761 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6762 // CaptureTracking can know about special capturing properties of some
6763 // intrinsics like launder.invariant.group, that can't be expressed with
6764 // the attributes, but have properties like returning aliasing pointer.
6765 // Because some analysis may assume that nocaptured pointer is not
6766 // returned from some special intrinsic (because function would have to
6767 // be marked with returns attribute), it is crucial to use this function
6768 // because it should be in sync with CaptureTracking. Not using it may
6769 // cause weird miscompilations where 2 aliasing pointers are assumed to
6770 // noalias.
6771 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6772 V = RP;
6773 continue;
6774 }
6775 }
6776
6777 return V;
6778 }
6779 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6780 }
6781 return V;
6782}
6783
6786 const LoopInfo *LI, unsigned MaxLookup) {
6789 Worklist.push_back(V);
6790 do {
6791 const Value *P = Worklist.pop_back_val();
6792 P = getUnderlyingObject(P, MaxLookup);
6793
6794 if (!Visited.insert(P).second)
6795 continue;
6796
6797 if (auto *SI = dyn_cast<SelectInst>(P)) {
6798 Worklist.push_back(SI->getTrueValue());
6799 Worklist.push_back(SI->getFalseValue());
6800 continue;
6801 }
6802
6803 if (auto *PN = dyn_cast<PHINode>(P)) {
6804 // If this PHI changes the underlying object in every iteration of the
6805 // loop, don't look through it. Consider:
6806 // int **A;
6807 // for (i) {
6808 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6809 // Curr = A[i];
6810 // *Prev, *Curr;
6811 //
6812 // Prev is tracking Curr one iteration behind so they refer to different
6813 // underlying objects.
6814 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6816 append_range(Worklist, PN->incoming_values());
6817 else
6818 Objects.push_back(P);
6819 continue;
6820 }
6821
6822 Objects.push_back(P);
6823 } while (!Worklist.empty());
6824}
6825
6827 const unsigned MaxVisited = 8;
6828
6831 Worklist.push_back(V);
6832 const Value *Object = nullptr;
6833 // Used as fallback if we can't find a common underlying object through
6834 // recursion.
6835 bool First = true;
6836 const Value *FirstObject = getUnderlyingObject(V);
6837 do {
6838 const Value *P = Worklist.pop_back_val();
6839 P = First ? FirstObject : getUnderlyingObject(P);
6840 First = false;
6841
6842 if (!Visited.insert(P).second)
6843 continue;
6844
6845 if (Visited.size() == MaxVisited)
6846 return FirstObject;
6847
6848 if (auto *SI = dyn_cast<SelectInst>(P)) {
6849 Worklist.push_back(SI->getTrueValue());
6850 Worklist.push_back(SI->getFalseValue());
6851 continue;
6852 }
6853
6854 if (auto *PN = dyn_cast<PHINode>(P)) {
6855 append_range(Worklist, PN->incoming_values());
6856 continue;
6857 }
6858
6859 if (!Object)
6860 Object = P;
6861 else if (Object != P)
6862 return FirstObject;
6863 } while (!Worklist.empty());
6864
6865 return Object;
6866}
6867
6868/// This is the function that does the work of looking through basic
6869/// ptrtoint+arithmetic+inttoptr sequences.
6870static const Value *getUnderlyingObjectFromInt(const Value *V) {
6871 do {
6872 if (const Operator *U = dyn_cast<Operator>(V)) {
6873 // If we find a ptrtoint, we can transfer control back to the
6874 // regular getUnderlyingObjectFromInt.
6875 if (U->getOpcode() == Instruction::PtrToInt)
6876 return U->getOperand(0);
6877 // If we find an add of a constant, a multiplied value, or a phi, it's
6878 // likely that the other operand will lead us to the base
6879 // object. We don't have to worry about the case where the
6880 // object address is somehow being computed by the multiply,
6881 // because our callers only care when the result is an
6882 // identifiable object.
6883 if (U->getOpcode() != Instruction::Add ||
6884 (!isa<ConstantInt>(U->getOperand(1)) &&
6885 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6886 !isa<PHINode>(U->getOperand(1))))
6887 return V;
6888 V = U->getOperand(0);
6889 } else {
6890 return V;
6891 }
6892 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6893 } while (true);
6894}
6895
6896/// This is a wrapper around getUnderlyingObjects and adds support for basic
6897/// ptrtoint+arithmetic+inttoptr sequences.
6898/// It returns false if unidentified object is found in getUnderlyingObjects.
6900 SmallVectorImpl<Value *> &Objects) {
6902 SmallVector<const Value *, 4> Working(1, V);
6903 do {
6904 V = Working.pop_back_val();
6905
6907 getUnderlyingObjects(V, Objs);
6908
6909 for (const Value *V : Objs) {
6910 if (!Visited.insert(V).second)
6911 continue;
6912 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6913 const Value *O =
6914 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6915 if (O->getType()->isPointerTy()) {
6916 Working.push_back(O);
6917 continue;
6918 }
6919 }
6920 // If getUnderlyingObjects fails to find an identifiable object,
6921 // getUnderlyingObjectsForCodeGen also fails for safety.
6922 if (!isIdentifiedObject(V)) {
6923 Objects.clear();
6924 return false;
6925 }
6926 Objects.push_back(const_cast<Value *>(V));
6927 }
6928 } while (!Working.empty());
6929 return true;
6930}
6931
6933 AllocaInst *Result = nullptr;
6935 SmallVector<Value *, 4> Worklist;
6936
6937 auto AddWork = [&](Value *V) {
6938 if (Visited.insert(V).second)
6939 Worklist.push_back(V);
6940 };
6941
6942 AddWork(V);
6943 do {
6944 V = Worklist.pop_back_val();
6945 assert(Visited.count(V));
6946
6947 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6948 if (Result && Result != AI)
6949 return nullptr;
6950 Result = AI;
6951 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
6952 AddWork(CI->getOperand(0));
6953 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
6954 for (Value *IncValue : PN->incoming_values())
6955 AddWork(IncValue);
6956 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
6957 AddWork(SI->getTrueValue());
6958 AddWork(SI->getFalseValue());
6959 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
6960 if (OffsetZero && !GEP->hasAllZeroIndices())
6961 return nullptr;
6962 AddWork(GEP->getPointerOperand());
6963 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
6964 Value *Returned = CB->getReturnedArgOperand();
6965 if (Returned)
6966 AddWork(Returned);
6967 else
6968 return nullptr;
6969 } else {
6970 return nullptr;
6971 }
6972 } while (!Worklist.empty());
6973
6974 return Result;
6975}
6976
6978 const Value *V, bool AllowLifetime, bool AllowDroppable) {
6979 for (const User *U : V->users()) {
6980 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
6981 if (!II)
6982 return false;
6983
6984 if (AllowLifetime && II->isLifetimeStartOrEnd())
6985 continue;
6986
6987 if (AllowDroppable && II->isDroppable())
6988 continue;
6989
6990 return false;
6991 }
6992 return true;
6993}
6994
6997 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
6998}
7001 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7002}
7003
7005 if (auto *II = dyn_cast<IntrinsicInst>(I))
7006 return isTriviallyVectorizable(II->getIntrinsicID());
7007 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7008 return (!Shuffle || Shuffle->isSelect()) &&
7009 !isa<CallBase, BitCastInst, ExtractElementInst>(I);
7010}
7011
7013 const Instruction *CtxI,
7014 AssumptionCache *AC,
7015 const DominatorTree *DT,
7016 const TargetLibraryInfo *TLI,
7017 bool UseVariableInfo) {
7018 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7019 AC, DT, TLI, UseVariableInfo);
7020}
7021
7023 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7024 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7025 bool UseVariableInfo) {
7026#ifndef NDEBUG
7027 if (Inst->getOpcode() != Opcode) {
7028 // Check that the operands are actually compatible with the Opcode override.
7029 auto hasEqualReturnAndLeadingOperandTypes =
7030 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7031 if (Inst->getNumOperands() < NumLeadingOperands)
7032 return false;
7033 const Type *ExpectedType = Inst->getType();
7034 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7035 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7036 return false;
7037 return true;
7038 };
7040 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7041 assert(!Instruction::isUnaryOp(Opcode) ||
7042 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7043 }
7044#endif
7045
7046 switch (Opcode) {
7047 default:
7048 return true;
7049 case Instruction::UDiv:
7050 case Instruction::URem: {
7051 // x / y is undefined if y == 0.
7052 const APInt *V;
7053 if (match(Inst->getOperand(1), m_APInt(V)))
7054 return *V != 0;
7055 return false;
7056 }
7057 case Instruction::SDiv:
7058 case Instruction::SRem: {
7059 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7060 const APInt *Numerator, *Denominator;
7061 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7062 return false;
7063 // We cannot hoist this division if the denominator is 0.
7064 if (*Denominator == 0)
7065 return false;
7066 // It's safe to hoist if the denominator is not 0 or -1.
7067 if (!Denominator->isAllOnes())
7068 return true;
7069 // At this point we know that the denominator is -1. It is safe to hoist as
7070 // long we know that the numerator is not INT_MIN.
7071 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7072 return !Numerator->isMinSignedValue();
7073 // The numerator *might* be MinSignedValue.
7074 return false;
7075 }
7076 case Instruction::Load: {
7077 if (!UseVariableInfo)
7078 return false;
7079
7080 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7081 if (!LI)
7082 return false;
7083 if (mustSuppressSpeculation(*LI))
7084 return false;
7085 const DataLayout &DL = LI->getDataLayout();
7087 LI->getType(), LI->getAlign(), DL,
7088 CtxI, AC, DT, TLI);
7089 }
7090 case Instruction::Call: {
7091 auto *CI = dyn_cast<const CallInst>(Inst);
7092 if (!CI)
7093 return false;
7094 const Function *Callee = CI->getCalledFunction();
7095
7096 // The called function could have undefined behavior or side-effects, even
7097 // if marked readnone nounwind.
7098 return Callee && Callee->isSpeculatable();
7099 }
7100 case Instruction::VAArg:
7101 case Instruction::Alloca:
7102 case Instruction::Invoke:
7103 case Instruction::CallBr:
7104 case Instruction::PHI:
7105 case Instruction::Store:
7106 case Instruction::Ret:
7107 case Instruction::Br:
7108 case Instruction::IndirectBr:
7109 case Instruction::Switch:
7110 case Instruction::Unreachable:
7111 case Instruction::Fence:
7112 case Instruction::AtomicRMW:
7113 case Instruction::AtomicCmpXchg:
7114 case Instruction::LandingPad:
7115 case Instruction::Resume:
7116 case Instruction::CatchSwitch:
7117 case Instruction::CatchPad:
7118 case Instruction::CatchRet:
7119 case Instruction::CleanupPad:
7120 case Instruction::CleanupRet:
7121 return false; // Misc instructions which have effects
7122 }
7123}
7124
7126 if (I.mayReadOrWriteMemory())
7127 // Memory dependency possible
7128 return true;
7130 // Can't move above a maythrow call or infinite loop. Or if an
7131 // inalloca alloca, above a stacksave call.
7132 return true;
7134 // 1) Can't reorder two inf-loop calls, even if readonly
7135 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7136 // safe to speculative execute. (Inverse of above)
7137 return true;
7138 return false;
7139}
7140
7141/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7143 switch (OR) {
7152 }
7153 llvm_unreachable("Unknown OverflowResult");
7154}
7155
7156/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7159 bool ForSigned,
7160 const SimplifyQuery &SQ) {
7161 ConstantRange CR1 =
7162 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7163 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7166 return CR1.intersectWith(CR2, RangeType);
7167}
7168
7170 const Value *RHS,
7171 const SimplifyQuery &SQ,
7172 bool IsNSW) {
7173 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
7174 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
7175
7176 // mul nsw of two non-negative numbers is also nuw.
7177 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
7179
7180 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
7181 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
7182 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7183}
7184
7186 const Value *RHS,
7187 const SimplifyQuery &SQ) {
7188 // Multiplying n * m significant bits yields a result of n + m significant
7189 // bits. If the total number of significant bits does not exceed the
7190 // result bit width (minus 1), there is no overflow.
7191 // This means if we have enough leading sign bits in the operands
7192 // we can guarantee that the result does not overflow.
7193 // Ref: "Hacker's Delight" by Henry Warren
7194 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7195
7196 // Note that underestimating the number of sign bits gives a more
7197 // conservative answer.
7198 unsigned SignBits =
7200
7201 // First handle the easy case: if we have enough sign bits there's
7202 // definitely no overflow.
7203 if (SignBits > BitWidth + 1)
7205
7206 // There are two ambiguous cases where there can be no overflow:
7207 // SignBits == BitWidth + 1 and
7208 // SignBits == BitWidth
7209 // The second case is difficult to check, therefore we only handle the
7210 // first case.
7211 if (SignBits == BitWidth + 1) {
7212 // It overflows only when both arguments are negative and the true
7213 // product is exactly the minimum negative number.
7214 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7215 // For simplicity we just check if at least one side is not negative.
7216 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
7217 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
7218 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7220 }
7222}
7223
7226 const WithCache<const Value *> &RHS,
7227 const SimplifyQuery &SQ) {
7228 ConstantRange LHSRange =
7229 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7230 ConstantRange RHSRange =
7231 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7232 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7233}
7234
7235static OverflowResult
7237 const WithCache<const Value *> &RHS,
7238 const AddOperator *Add, const SimplifyQuery &SQ) {
7239 if (Add && Add->hasNoSignedWrap()) {
7241 }
7242
7243 // If LHS and RHS each have at least two sign bits, the addition will look
7244 // like
7245 //
7246 // XX..... +
7247 // YY.....
7248 //
7249 // If the carry into the most significant position is 0, X and Y can't both
7250 // be 1 and therefore the carry out of the addition is also 0.
7251 //
7252 // If the carry into the most significant position is 1, X and Y can't both
7253 // be 0 and therefore the carry out of the addition is also 1.
7254 //
7255 // Since the carry into the most significant position is always equal to
7256 // the carry out of the addition, there is no signed overflow.
7257 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
7258 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
7260
7261 ConstantRange LHSRange =
7262 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7263 ConstantRange RHSRange =
7264 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7265 OverflowResult OR =
7266 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7268 return OR;
7269
7270 // The remaining code needs Add to be available. Early returns if not so.
7271 if (!Add)
7273
7274 // If the sign of Add is the same as at least one of the operands, this add
7275 // CANNOT overflow. If this can be determined from the known bits of the
7276 // operands the above signedAddMayOverflow() check will have already done so.
7277 // The only other way to improve on the known bits is from an assumption, so
7278 // call computeKnownBitsFromContext() directly.
7279 bool LHSOrRHSKnownNonNegative =
7280 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7281 bool LHSOrRHSKnownNegative =
7282 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7283 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7284 KnownBits AddKnown(LHSRange.getBitWidth());
7285 computeKnownBitsFromContext(Add, AddKnown, /*Depth=*/0, SQ);
7286 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7287 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7289 }
7290
7292}
7293
7295 const Value *RHS,
7296 const SimplifyQuery &SQ) {
7297 // X - (X % ?)
7298 // The remainder of a value can't have greater magnitude than itself,
7299 // so the subtraction can't overflow.
7300
7301 // X - (X -nuw ?)
7302 // In the minimal case, this would simplify to "?", so there's no subtract
7303 // at all. But if this analysis is used to peek through casts, for example,
7304 // then determining no-overflow may allow other transforms.
7305
7306 // TODO: There are other patterns like this.
7307 // See simplifyICmpWithBinOpOnLHS() for candidates.
7308 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7310 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7312
7314 SQ.DL)) {
7315 if (*C)
7318 }
7319
7320 ConstantRange LHSRange =
7321 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7322 ConstantRange RHSRange =
7323 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7324 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7325}
7326
7328 const Value *RHS,
7329 const SimplifyQuery &SQ) {
7330 // X - (X % ?)
7331 // The remainder of a value can't have greater magnitude than itself,
7332 // so the subtraction can't overflow.
7333
7334 // X - (X -nsw ?)
7335 // In the minimal case, this would simplify to "?", so there's no subtract
7336 // at all. But if this analysis is used to peek through casts, for example,
7337 // then determining no-overflow may allow other transforms.
7338 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7340 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7342
7343 // If LHS and RHS each have at least two sign bits, the subtraction
7344 // cannot overflow.
7345 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
7346 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
7348
7349 ConstantRange LHSRange =
7350 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7351 ConstantRange RHSRange =
7352 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7353 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7354}
7355
7357 const DominatorTree &DT) {
7358 SmallVector<const BranchInst *, 2> GuardingBranches;
7360
7361 for (const User *U : WO->users()) {
7362 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7363 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7364
7365 if (EVI->getIndices()[0] == 0)
7366 Results.push_back(EVI);
7367 else {
7368 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7369
7370 for (const auto *U : EVI->users())
7371 if (const auto *B = dyn_cast<BranchInst>(U)) {
7372 assert(B->isConditional() && "How else is it using an i1?");
7373 GuardingBranches.push_back(B);
7374 }
7375 }
7376 } else {
7377 // We are using the aggregate directly in a way we don't want to analyze
7378 // here (storing it to a global, say).
7379 return false;
7380 }
7381 }
7382
7383 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7384 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7385 if (!NoWrapEdge.isSingleEdge())
7386 return false;
7387
7388 // Check if all users of the add are provably no-wrap.
7389 for (const auto *Result : Results) {
7390 // If the extractvalue itself is not executed on overflow, the we don't
7391 // need to check each use separately, since domination is transitive.
7392 if (DT.dominates(NoWrapEdge, Result->getParent()))
7393 continue;
7394
7395 for (const auto &RU : Result->uses())
7396 if (!DT.dominates(NoWrapEdge, RU))
7397 return false;
7398 }
7399
7400 return true;
7401 };
7402
7403 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7404}
7405
7406/// Shifts return poison if shiftwidth is larger than the bitwidth.
7407static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7408 auto *C = dyn_cast<Constant>(ShiftAmount);
7409 if (!C)
7410 return false;
7411
7412 // Shifts return poison if shiftwidth is larger than the bitwidth.
7414 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7415 unsigned NumElts = FVTy->getNumElements();
7416 for (unsigned i = 0; i < NumElts; ++i)
7417 ShiftAmounts.push_back(C->getAggregateElement(i));
7418 } else if (isa<ScalableVectorType>(C->getType()))
7419 return false; // Can't tell, just return false to be safe
7420 else
7421 ShiftAmounts.push_back(C);
7422
7423 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7424 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7425 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7426 });
7427
7428 return Safe;
7429}
7430
7432 PoisonOnly = (1 << 0),
7433 UndefOnly = (1 << 1),
7435};
7436
7438 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7439}
7440
7442 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7443}
7444
7446 bool ConsiderFlagsAndMetadata) {
7447
7448 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7449 Op->hasPoisonGeneratingAnnotations())
7450 return true;
7451
7452 unsigned Opcode = Op->getOpcode();
7453
7454 // Check whether opcode is a poison/undef-generating operation
7455 switch (Opcode) {
7456 case Instruction::Shl:
7457 case Instruction::AShr:
7458 case Instruction::LShr:
7459 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7460 case Instruction::FPToSI:
7461 case Instruction::FPToUI:
7462 // fptosi/ui yields poison if the resulting value does not fit in the
7463 // destination type.
7464 return true;
7465 case Instruction::Call:
7466 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7467 switch (II->getIntrinsicID()) {
7468 // TODO: Add more intrinsics.
7469 case Intrinsic::ctlz:
7470 case Intrinsic::cttz:
7471 case Intrinsic::abs:
7472 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7473 return false;
7474 break;
7475 case Intrinsic::ctpop:
7476 case Intrinsic::bswap:
7477 case Intrinsic::bitreverse:
7478 case Intrinsic::fshl:
7479 case Intrinsic::fshr:
7480 case Intrinsic::smax:
7481 case Intrinsic::smin:
7482 case Intrinsic::umax:
7483 case Intrinsic::umin:
7484 case Intrinsic::ptrmask:
7485 case Intrinsic::fptoui_sat:
7486 case Intrinsic::fptosi_sat:
7487 case Intrinsic::sadd_with_overflow:
7488 case Intrinsic::ssub_with_overflow:
7489 case Intrinsic::smul_with_overflow:
7490 case Intrinsic::uadd_with_overflow:
7491 case Intrinsic::usub_with_overflow:
7492 case Intrinsic::umul_with_overflow:
7493 case Intrinsic::sadd_sat:
7494 case Intrinsic::uadd_sat:
7495 case Intrinsic::ssub_sat:
7496 case Intrinsic::usub_sat:
7497 return false;
7498 case Intrinsic::sshl_sat:
7499 case Intrinsic::ushl_sat:
7500 return includesPoison(Kind) &&
7501 !shiftAmountKnownInRange(II->getArgOperand(1));
7502 case Intrinsic::fma:
7503 case Intrinsic::fmuladd:
7504 case Intrinsic::sqrt:
7505 case Intrinsic::powi:
7506 case Intrinsic::sin:
7507 case Intrinsic::cos:
7508 case Intrinsic::pow:
7509 case Intrinsic::log:
7510 case Intrinsic::log10:
7511 case Intrinsic::log2:
7512 case Intrinsic::exp:
7513 case Intrinsic::exp2:
7514 case Intrinsic::exp10:
7515 case Intrinsic::fabs:
7516 case Intrinsic::copysign:
7517 case Intrinsic::floor:
7518 case Intrinsic::ceil:
7519 case Intrinsic::trunc:
7520 case Intrinsic::rint:
7521 case Intrinsic::nearbyint:
7522 case Intrinsic::round:
7523 case Intrinsic::roundeven:
7524 case Intrinsic::fptrunc_round:
7525 case Intrinsic::canonicalize:
7526 case Intrinsic::arithmetic_fence:
7527 case Intrinsic::minnum:
7528 case Intrinsic::maxnum:
7529 case Intrinsic::minimum:
7530 case Intrinsic::maximum:
7531 case Intrinsic::is_fpclass:
7532 case Intrinsic::ldexp:
7533 case Intrinsic::frexp:
7534 return false;
7535 case Intrinsic::lround:
7536 case Intrinsic::llround:
7537 case Intrinsic::lrint:
7538 case Intrinsic::llrint:
7539 // If the value doesn't fit an unspecified value is returned (but this
7540 // is not poison).
7541 return false;
7542 }
7543 }
7544 [[fallthrough]];
7545 case Instruction::CallBr:
7546 case Instruction::Invoke: {
7547 const auto *CB = cast<CallBase>(Op);
7548 return !CB->hasRetAttr(Attribute::NoUndef);
7549 }
7550 case Instruction::InsertElement:
7551 case Instruction::ExtractElement: {
7552 // If index exceeds the length of the vector, it returns poison
7553 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7554 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7555 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7556 if (includesPoison(Kind))
7557 return !Idx ||
7558 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7559 return false;
7560 }
7561 case Instruction::ShuffleVector: {
7562 ArrayRef<int> Mask = isa<ConstantExpr>(Op)
7563 ? cast<ConstantExpr>(Op)->getShuffleMask()
7564 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7565 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7566 }
7567 case Instruction::FNeg:
7568 case Instruction::PHI:
7569 case Instruction::Select:
7570 case Instruction::URem:
7571 case Instruction::SRem:
7572 case Instruction::ExtractValue:
7573 case Instruction::InsertValue:
7574 case Instruction::Freeze:
7575 case Instruction::ICmp:
7576 case Instruction::FCmp:
7577 case Instruction::FAdd:
7578 case Instruction::FSub:
7579 case Instruction::FMul:
7580 case Instruction::FDiv:
7581 case Instruction::FRem:
7582 return false;
7583 case Instruction::GetElementPtr:
7584 // inbounds is handled above
7585 // TODO: what about inrange on constexpr?
7586 return false;
7587 default: {
7588 const auto *CE = dyn_cast<ConstantExpr>(Op);
7589 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7590 return false;
7591 else if (Instruction::isBinaryOp(Opcode))
7592 return false;
7593 // Be conservative and return true.
7594 return true;
7595 }
7596 }
7597}
7598
7600 bool ConsiderFlagsAndMetadata) {
7601 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7602 ConsiderFlagsAndMetadata);
7603}
7604
7605bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7606 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7607 ConsiderFlagsAndMetadata);
7608}
7609
7610static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7611 unsigned Depth) {
7612 if (ValAssumedPoison == V)
7613 return true;
7614
7615 const unsigned MaxDepth = 2;
7616 if (Depth >= MaxDepth)
7617 return false;
7618
7619 if (const auto *I = dyn_cast<Instruction>(V)) {
7620 if (any_of(I->operands(), [=](const Use &Op) {
7621 return propagatesPoison(Op) &&
7622 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7623 }))
7624 return true;
7625
7626 // V = extractvalue V0, idx
7627 // V2 = extractvalue V0, idx2
7628 // V0's elements are all poison or not. (e.g., add_with_overflow)
7629 const WithOverflowInst *II;
7631 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7632 llvm::is_contained(II->args(), ValAssumedPoison)))
7633 return true;
7634 }
7635 return false;
7636}
7637
7638static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7639 unsigned Depth) {
7640 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7641 return true;
7642
7643 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7644 return true;
7645
7646 const unsigned MaxDepth = 2;
7647 if (Depth >= MaxDepth)
7648 return false;
7649
7650 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7651 if (I && !canCreatePoison(cast<Operator>(I))) {
7652 return all_of(I->operands(), [=](const Value *Op) {
7653 return impliesPoison(Op, V, Depth + 1);
7654 });
7655 }
7656 return false;
7657}
7658
7659bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7660 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7661}
7662
7663static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7664
7666 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7667 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7669 return false;
7670
7671 if (isa<MetadataAsValue>(V))
7672 return false;
7673
7674 if (const auto *A = dyn_cast<Argument>(V)) {
7675 if (A->hasAttribute(Attribute::NoUndef) ||
7676 A->hasAttribute(Attribute::Dereferenceable) ||
7677 A->hasAttribute(Attribute::DereferenceableOrNull))
7678 return true;
7679 }
7680
7681 if (auto *C = dyn_cast<Constant>(V)) {
7682 if (isa<PoisonValue>(C))
7683 return !includesPoison(Kind);
7684
7685 if (isa<UndefValue>(C))
7686 return !includesUndef(Kind);
7687
7688 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(V) ||
7689 isa<ConstantPointerNull>(C) || isa<Function>(C))
7690 return true;
7691
7692 if (C->getType()->isVectorTy() && !isa<ConstantExpr>(C)) {
7693 if (includesUndef(Kind) && C->containsUndefElement())
7694 return false;
7695 if (includesPoison(Kind) && C->containsPoisonElement())
7696 return false;
7697 return !C->containsConstantExpression();
7698 }
7699 }
7700
7701 // Strip cast operations from a pointer value.
7702 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7703 // inbounds with zero offset. To guarantee that the result isn't poison, the
7704 // stripped pointer is checked as it has to be pointing into an allocated
7705 // object or be null `null` to ensure `inbounds` getelement pointers with a
7706 // zero offset could not produce poison.
7707 // It can strip off addrspacecast that do not change bit representation as
7708 // well. We believe that such addrspacecast is equivalent to no-op.
7709 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7710 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7711 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7712 return true;
7713
7714 auto OpCheck = [&](const Value *V) {
7715 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7716 };
7717
7718 if (auto *Opr = dyn_cast<Operator>(V)) {
7719 // If the value is a freeze instruction, then it can never
7720 // be undef or poison.
7721 if (isa<FreezeInst>(V))
7722 return true;
7723
7724 if (const auto *CB = dyn_cast<CallBase>(V)) {
7725 if (CB->hasRetAttr(Attribute::NoUndef) ||
7726 CB->hasRetAttr(Attribute::Dereferenceable) ||
7727 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7728 return true;
7729 }
7730
7731 if (const auto *PN = dyn_cast<PHINode>(V)) {
7732 unsigned Num = PN->getNumIncomingValues();
7733 bool IsWellDefined = true;
7734 for (unsigned i = 0; i < Num; ++i) {
7735 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7736 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7737 DT, Depth + 1, Kind)) {
7738 IsWellDefined = false;
7739 break;
7740 }
7741 }
7742 if (IsWellDefined)
7743 return true;
7744 } else if (!::canCreateUndefOrPoison(Opr, Kind,
7745 /*ConsiderFlagsAndMetadata*/ true) &&
7746 all_of(Opr->operands(), OpCheck))
7747 return true;
7748 }
7749
7750 if (auto *I = dyn_cast<LoadInst>(V))
7751 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7752 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7753 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7754 return true;
7755
7757 return true;
7758
7759 // CxtI may be null or a cloned instruction.
7760 if (!CtxI || !CtxI->getParent() || !DT)
7761 return false;
7762
7763 auto *DNode = DT->getNode(CtxI->getParent());
7764 if (!DNode)
7765 // Unreachable block
7766 return false;
7767
7768 // If V is used as a branch condition before reaching CtxI, V cannot be
7769 // undef or poison.
7770 // br V, BB1, BB2
7771 // BB1:
7772 // CtxI ; V cannot be undef or poison here
7773 auto *Dominator = DNode->getIDom();
7774 // This check is purely for compile time reasons: we can skip the IDom walk
7775 // if what we are checking for includes undef and the value is not an integer.
7776 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7777 while (Dominator) {
7778 auto *TI = Dominator->getBlock()->getTerminator();
7779
7780 Value *Cond = nullptr;
7781 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7782 if (BI->isConditional())
7783 Cond = BI->getCondition();
7784 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7785 Cond = SI->getCondition();
7786 }
7787
7788 if (Cond) {
7789 if (Cond == V)
7790 return true;
7791 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7792 // For poison, we can analyze further
7793 auto *Opr = cast<Operator>(Cond);
7794 if (any_of(Opr->operands(), [V](const Use &U) {
7795 return V == U && propagatesPoison(U);
7796 }))
7797 return true;
7798 }
7799 }
7800
7801 Dominator = Dominator->getIDom();
7802 }
7803
7804 if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
7805 return true;
7806
7807 return false;
7808}
7809
7811 const Instruction *CtxI,
7812 const DominatorTree *DT,
7813 unsigned Depth) {
7814 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7815 UndefPoisonKind::UndefOrPoison);
7816}
7817
7819 const Instruction *CtxI,
7820 const DominatorTree *DT, unsigned Depth) {
7821 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7822 UndefPoisonKind::PoisonOnly);
7823}
7824
7826 const Instruction *CtxI,
7827 const DominatorTree *DT, unsigned Depth) {
7828 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7829 UndefPoisonKind::UndefOnly);
7830}
7831
7832/// Return true if undefined behavior would provably be executed on the path to
7833/// OnPathTo if Root produced a posion result. Note that this doesn't say
7834/// anything about whether OnPathTo is actually executed or whether Root is
7835/// actually poison. This can be used to assess whether a new use of Root can
7836/// be added at a location which is control equivalent with OnPathTo (such as
7837/// immediately before it) without introducing UB which didn't previously
7838/// exist. Note that a false result conveys no information.
7840 Instruction *OnPathTo,
7841 DominatorTree *DT) {
7842 // Basic approach is to assume Root is poison, propagate poison forward
7843 // through all users we can easily track, and then check whether any of those
7844 // users are provable UB and must execute before out exiting block might
7845 // exit.
7846
7847 // The set of all recursive users we've visited (which are assumed to all be
7848 // poison because of said visit)
7849 SmallSet<const Value *, 16> KnownPoison;
7851 Worklist.push_back(Root);
7852 while (!Worklist.empty()) {
7853 const Instruction *I = Worklist.pop_back_val();
7854
7855 // If we know this must trigger UB on a path leading our target.
7856 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7857 return true;
7858
7859 // If we can't analyze propagation through this instruction, just skip it
7860 // and transitive users. Safe as false is a conservative result.
7861 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7862 return KnownPoison.contains(U) && propagatesPoison(U);
7863 }))
7864 continue;
7865
7866 if (KnownPoison.insert(I).second)
7867 for (const User *User : I->users())
7868 Worklist.push_back(cast<Instruction>(User));
7869 }
7870
7871 // Might be non-UB, or might have a path we couldn't prove must execute on
7872 // way to exiting bb.
7873 return false;
7874}
7875
7877 const SimplifyQuery &SQ) {
7878 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7879 Add, SQ);
7880}
7881
7884 const WithCache<const Value *> &RHS,
7885 const SimplifyQuery &SQ) {
7886 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7887}
7888
7890 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7891 // of time because it's possible for another thread to interfere with it for an
7892 // arbitrary length of time, but programs aren't allowed to rely on that.
7893
7894 // If there is no successor, then execution can't transfer to it.
7895 if (isa<ReturnInst>(I))
7896 return false;
7897 if (isa<UnreachableInst>(I))
7898 return false;
7899
7900 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7901 // Instruction::willReturn.
7902 //
7903 // FIXME: Move this check into Instruction::willReturn.
7904 if (isa<CatchPadInst>(I)) {
7905 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7906 default:
7907 // A catchpad may invoke exception object constructors and such, which
7908 // in some languages can be arbitrary code, so be conservative by default.
7909 return false;
7911 // For CoreCLR, it just involves a type test.
7912 return true;
7913 }
7914 }
7915
7916 // An instruction that returns without throwing must transfer control flow
7917 // to a successor.
7918 return !I->mayThrow() && I->willReturn();
7919}
7920
7922 // TODO: This is slightly conservative for invoke instruction since exiting
7923 // via an exception *is* normal control for them.
7924 for (const Instruction &I : *BB)
7926 return false;
7927 return true;
7928}
7929
7932 unsigned ScanLimit) {
7934 ScanLimit);
7935}
7936
7939 assert(ScanLimit && "scan limit must be non-zero");
7940 for (const Instruction &I : Range) {
7941 if (isa<DbgInfoIntrinsic>(I))
7942 continue;
7943 if (--ScanLimit == 0)
7944 return false;
7946 return false;
7947 }
7948 return true;
7949}
7950
7952 const Loop *L) {
7953 // The loop header is guaranteed to be executed for every iteration.
7954 //
7955 // FIXME: Relax this constraint to cover all basic blocks that are
7956 // guaranteed to be executed at every iteration.
7957 if (I->getParent() != L->getHeader()) return false;
7958
7959 for (const Instruction &LI : *L->getHeader()) {
7960 if (&LI == I) return true;
7961 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7962 }
7963 llvm_unreachable("Instruction not contained in its own parent basic block.");
7964}
7965
7966bool llvm::propagatesPoison(const Use &PoisonOp) {
7967 const Operator *I = cast<Operator>(PoisonOp.getUser());
7968 switch (I->getOpcode()) {
7969 case Instruction::Freeze:
7970 case Instruction::PHI:
7971 case Instruction::Invoke:
7972 return false;
7973 case Instruction::Select:
7974 return PoisonOp.getOperandNo() == 0;
7975 case Instruction::Call:
7976 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
7977 switch (II->getIntrinsicID()) {
7978 // TODO: Add more intrinsics.
7979 case Intrinsic::sadd_with_overflow:
7980 case Intrinsic::ssub_with_overflow:
7981 case Intrinsic::smul_with_overflow:
7982 case Intrinsic::uadd_with_overflow:
7983 case Intrinsic::usub_with_overflow:
7984 case Intrinsic::umul_with_overflow:
7985 // If an input is a vector containing a poison element, the
7986 // two output vectors (calculated results, overflow bits)'
7987 // corresponding lanes are poison.
7988 return true;
7989 case Intrinsic::ctpop:
7990 case Intrinsic::ctlz:
7991 case Intrinsic::cttz:
7992 case Intrinsic::abs:
7993 case Intrinsic::smax:
7994 case Intrinsic::smin:
7995 case Intrinsic::umax:
7996 case Intrinsic::umin:
7997 case Intrinsic::bitreverse:
7998 case Intrinsic::bswap:
7999 case Intrinsic::sadd_sat:
8000 case Intrinsic::ssub_sat:
8001 case Intrinsic::sshl_sat:
8002 case Intrinsic::uadd_sat:
8003 case Intrinsic::usub_sat:
8004 case Intrinsic::ushl_sat:
8005 return true;
8006 }
8007 }
8008 return false;
8009 case Instruction::ICmp:
8010 case Instruction::FCmp:
8011 case Instruction::GetElementPtr:
8012 return true;
8013 default:
8014 if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I))
8015 return true;
8016
8017 // Be conservative and return false.
8018 return false;
8019 }
8020}
8021
8022/// Enumerates all operands of \p I that are guaranteed to not be undef or
8023/// poison. If the callback \p Handle returns true, stop processing and return
8024/// true. Otherwise, return false.
8025template <typename CallableT>
8027 const CallableT &Handle) {
8028 switch (I->getOpcode()) {
8029 case Instruction::Store:
8030 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8031 return true;
8032 break;
8033
8034 case Instruction::Load:
8035 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8036 return true;
8037 break;
8038
8039 // Since dereferenceable attribute imply noundef, atomic operations
8040 // also implicitly have noundef pointers too
8041 case Instruction::AtomicCmpXchg:
8042 if (Handle(cast<AtomicCmpXchgInst>(I)->getPointerOperand()))
8043 return true;
8044 break;
8045
8046 case Instruction::AtomicRMW:
8047 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8048 return true;
8049 break;
8050
8051 case Instruction::Call:
8052 case Instruction::Invoke: {
8053 const CallBase *CB = cast<CallBase>(I);
8054 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8055 return true;
8056 for (unsigned i = 0; i < CB->arg_size(); ++i)
8057 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8058 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8059 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8060 Handle(CB->getArgOperand(i)))
8061 return true;
8062 break;
8063 }
8064 case Instruction::Ret:
8065 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8066 Handle(I->getOperand(0)))
8067 return true;
8068 break;
8069 case Instruction::Switch:
8070 if (Handle(cast<SwitchInst>(I)->getCondition()))
8071 return true;
8072 break;
8073 case Instruction::Br: {
8074 auto *BR = cast<BranchInst>(I);
8075 if (BR->isConditional() && Handle(BR->getCondition()))
8076 return true;
8077 break;
8078 }
8079 default:
8080 break;
8081 }
8082
8083 return false;
8084}
8085
8088 handleGuaranteedWellDefinedOps(I, [&](const Value *V) {
8089 Operands.push_back(V);
8090 return false;
8091 });
8092}
8093
8094/// Enumerates all operands of \p I that are guaranteed to not be poison.
8095template <typename CallableT>
8097 const CallableT &Handle) {
8098 if (handleGuaranteedWellDefinedOps(I, Handle))
8099 return true;
8100 switch (I->getOpcode()) {
8101 // Divisors of these operations are allowed to be partially undef.
8102 case Instruction::UDiv:
8103 case Instruction::SDiv:
8104 case Instruction::URem:
8105 case Instruction::SRem:
8106 return Handle(I->getOperand(1));
8107 default:
8108 return false;
8109 }
8110}
8111
8114 handleGuaranteedNonPoisonOps(I, [&](const Value *V) {
8115 Operands.push_back(V);
8116 return false;
8117 });
8118}
8119
8121 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8123 I, [&](const Value *V) { return KnownPoison.count(V); });
8124}
8125
8127 bool PoisonOnly) {
8128 // We currently only look for uses of values within the same basic
8129 // block, as that makes it easier to guarantee that the uses will be
8130 // executed given that Inst is executed.
8131 //
8132 // FIXME: Expand this to consider uses beyond the same basic block. To do
8133 // this, look out for the distinction between post-dominance and strong
8134 // post-dominance.
8135 const BasicBlock *BB = nullptr;
8137 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8138 BB = Inst->getParent();
8139 Begin = Inst->getIterator();
8140 Begin++;
8141 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8142 if (Arg->getParent()->isDeclaration())
8143 return false;
8144 BB = &Arg->getParent()->getEntryBlock();
8145 Begin = BB->begin();
8146 } else {
8147 return false;
8148 }
8149
8150 // Limit number of instructions we look at, to avoid scanning through large
8151 // blocks. The current limit is chosen arbitrarily.
8152 unsigned ScanLimit = 32;
8154
8155 if (!PoisonOnly) {
8156 // Since undef does not propagate eagerly, be conservative & just check
8157 // whether a value is directly passed to an instruction that must take
8158 // well-defined operands.
8159
8160 for (const auto &I : make_range(Begin, End)) {
8161 if (isa<DbgInfoIntrinsic>(I))
8162 continue;
8163 if (--ScanLimit == 0)
8164 break;
8165
8166 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8167 return WellDefinedOp == V;
8168 }))
8169 return true;
8170
8172 break;
8173 }
8174 return false;
8175 }
8176
8177 // Set of instructions that we have proved will yield poison if Inst
8178 // does.
8179 SmallSet<const Value *, 16> YieldsPoison;
8181
8182 YieldsPoison.insert(V);
8183 Visited.insert(BB);
8184
8185 while (true) {
8186 for (const auto &I : make_range(Begin, End)) {
8187 if (isa<DbgInfoIntrinsic>(I))
8188 continue;
8189 if (--ScanLimit == 0)
8190 return false;
8191 if (mustTriggerUB(&I, YieldsPoison))
8192 return true;
8194 return false;
8195
8196 // If an operand is poison and propagates it, mark I as yielding poison.
8197 for (const Use &Op : I.operands()) {
8198 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8199 YieldsPoison.insert(&I);
8200 break;
8201 }
8202 }
8203
8204 // Special handling for select, which returns poison if its operand 0 is
8205 // poison (handled in the loop above) *or* if both its true/false operands
8206 // are poison (handled here).
8207 if (I.getOpcode() == Instruction::Select &&
8208 YieldsPoison.count(I.getOperand(1)) &&
8209 YieldsPoison.count(I.getOperand(2))) {
8210 YieldsPoison.insert(&I);
8211 }
8212 }
8213
8214 BB = BB->getSingleSuccessor();
8215 if (!BB || !Visited.insert(BB).second)
8216 break;
8217
8218 Begin = BB->getFirstNonPHI()->getIterator();
8219 End = BB->end();
8220 }
8221 return false;
8222}
8223
8225 return ::programUndefinedIfUndefOrPoison(Inst, false);
8226}
8227
8229 return ::programUndefinedIfUndefOrPoison(Inst, true);
8230}
8231
8232static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8233 if (FMF.noNaNs())
8234 return true;
8235
8236 if (auto *C = dyn_cast<ConstantFP>(V))
8237 return !C->isNaN();
8238
8239 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8240 if (!C->getElementType()->isFloatingPointTy())
8241 return false;
8242 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8243 if (C->getElementAsAPFloat(I).isNaN())
8244 return false;
8245 }
8246 return true;
8247 }
8248
8249 if (isa<ConstantAggregateZero>(V))
8250 return true;
8251
8252 return false;
8253}
8254
8255static bool isKnownNonZero(const Value *V) {
8256 if (auto *C = dyn_cast<ConstantFP>(V))
8257 return !C->isZero();
8258
8259 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8260 if (!C->getElementType()->isFloatingPointTy())
8261 return false;
8262 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8263 if (C->getElementAsAPFloat(I).isZero())
8264 return false;
8265 }
8266 return true;
8267 }
8268
8269 return false;
8270}
8271
8272/// Match clamp pattern for float types without care about NaNs or signed zeros.
8273/// Given non-min/max outer cmp/select from the clamp pattern this
8274/// function recognizes if it can be substitued by a "canonical" min/max
8275/// pattern.
8277 Value *CmpLHS, Value *CmpRHS,
8278 Value *TrueVal, Value *FalseVal,
8279 Value *&LHS, Value *&RHS) {
8280 // Try to match
8281 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8282 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8283 // and return description of the outer Max/Min.
8284
8285 // First, check if select has inverse order:
8286 if (CmpRHS == FalseVal) {
8287 std::swap(TrueVal, FalseVal);
8288 Pred = CmpInst::getInversePredicate(Pred);
8289 }
8290
8291 // Assume success now. If there's no match, callers should not use these anyway.
8292 LHS = TrueVal;
8293 RHS = FalseVal;
8294
8295 const APFloat *FC1;
8296 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8297 return {SPF_UNKNOWN, SPNB_NA, false};
8298
8299 const APFloat *FC2;
8300 switch (Pred) {
8301 case CmpInst::FCMP_OLT:
8302 case CmpInst::FCMP_OLE:
8303 case CmpInst::FCMP_ULT:
8304 case CmpInst::FCMP_ULE:
8305 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8306 *FC1 < *FC2)
8307 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8308 break;
8309 case CmpInst::FCMP_OGT:
8310 case CmpInst::FCMP_OGE:
8311 case CmpInst::FCMP_UGT:
8312 case CmpInst::FCMP_UGE:
8313 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8314 *FC1 > *FC2)
8315 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8316 break;
8317 default:
8318 break;
8319 }
8320
8321 return {SPF_UNKNOWN, SPNB_NA, false};
8322}
8323
8324/// Recognize variations of:
8325/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8327 Value *CmpLHS, Value *CmpRHS,
8328 Value *TrueVal, Value *FalseVal) {
8329 // Swap the select operands and predicate to match the patterns below.
8330 if (CmpRHS != TrueVal) {
8331 Pred = ICmpInst::getSwappedPredicate(Pred);
8332 std::swap(TrueVal, FalseVal);
8333 }
8334 const APInt *C1;
8335 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8336 const APInt *C2;
8337 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8338 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8339 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8340 return {SPF_SMAX, SPNB_NA, false};
8341
8342 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8343 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8344 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8345 return {SPF_SMIN, SPNB_NA, false};
8346
8347 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8348 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8349 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8350 return {SPF_UMAX, SPNB_NA, false};
8351
8352 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8353 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8354 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8355 return {SPF_UMIN, SPNB_NA, false};
8356 }
8357 return {SPF_UNKNOWN, SPNB_NA, false};
8358}
8359
8360/// Recognize variations of:
8361/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8363 Value *CmpLHS, Value *CmpRHS,
8364 Value *TVal, Value *FVal,
8365 unsigned Depth) {
8366 // TODO: Allow FP min/max with nnan/nsz.
8367 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8368
8369 Value *A = nullptr, *B = nullptr;
8370 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8371 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8372 return {SPF_UNKNOWN, SPNB_NA, false};
8373
8374 Value *C = nullptr, *D = nullptr;
8375 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8376 if (L.Flavor != R.Flavor)
8377 return {SPF_UNKNOWN, SPNB_NA, false};
8378
8379 // We have something like: x Pred y ? min(a, b) : min(c, d).
8380 // Try to match the compare to the min/max operations of the select operands.
8381 // First, make sure we have the right compare predicate.
8382 switch (L.Flavor) {
8383 case SPF_SMIN:
8384 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8385 Pred = ICmpInst::getSwappedPredicate(Pred);
8386 std::swap(CmpLHS, CmpRHS);
8387 }
8388 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8389 break;
8390 return {SPF_UNKNOWN, SPNB_NA, false};
8391 case SPF_SMAX:
8392 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8393 Pred = ICmpInst::getSwappedPredicate(Pred);
8394 std::swap(CmpLHS, CmpRHS);
8395 }
8396 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8397 break;
8398 return {SPF_UNKNOWN, SPNB_NA, false};
8399 case SPF_UMIN:
8400 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8401 Pred = ICmpInst::getSwappedPredicate(Pred);
8402 std::swap(CmpLHS, CmpRHS);
8403 }
8404 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8405 break;
8406 return {SPF_UNKNOWN, SPNB_NA, false};
8407 case SPF_UMAX:
8408 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8409 Pred = ICmpInst::getSwappedPredicate(Pred);
8410 std::swap(CmpLHS, CmpRHS);
8411 }
8412 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8413 break;
8414 return {SPF_UNKNOWN, SPNB_NA, false};
8415 default:
8416 return {SPF_UNKNOWN, SPNB_NA, false};
8417 }
8418
8419 // If there is a common operand in the already matched min/max and the other
8420 // min/max operands match the compare operands (either directly or inverted),
8421 // then this is min/max of the same flavor.
8422
8423 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8424 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8425 if (D == B) {
8426 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8427 match(A, m_Not(m_Specific(CmpRHS)))))
8428 return {L.Flavor, SPNB_NA, false};
8429 }
8430 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8431 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8432 if (C == B) {
8433 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8434 match(A, m_Not(m_Specific(CmpRHS)))))
8435 return {L.Flavor, SPNB_NA, false};
8436 }
8437 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8438 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8439 if (D == A) {
8440 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8441 match(B, m_Not(m_Specific(CmpRHS)))))
8442 return {L.Flavor, SPNB_NA, false};
8443 }
8444 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8445 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8446 if (C == A) {
8447 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8448 match(B, m_Not(m_Specific(CmpRHS)))))
8449 return {L.Flavor, SPNB_NA, false};
8450 }
8451
8452 return {SPF_UNKNOWN, SPNB_NA, false};
8453}
8454
8455/// If the input value is the result of a 'not' op, constant integer, or vector
8456/// splat of a constant integer, return the bitwise-not source value.
8457/// TODO: This could be extended to handle non-splat vector integer constants.
8459 Value *NotV;
8460 if (match(V, m_Not(m_Value(NotV))))
8461 return NotV;
8462
8463 const APInt *C;
8464 if (match(V, m_APInt(C)))
8465 return ConstantInt::get(V->getType(), ~(*C));
8466
8467 return nullptr;
8468}
8469
8470/// Match non-obvious integer minimum and maximum sequences.
8472 Value *CmpLHS, Value *CmpRHS,
8473 Value *TrueVal, Value *FalseVal,
8474 Value *&LHS, Value *&RHS,
8475 unsigned Depth) {
8476 // Assume success. If there's no match, callers should not use these anyway.
8477 LHS = TrueVal;
8478 RHS = FalseVal;
8479
8480 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8482 return SPR;
8483
8484 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8486 return SPR;
8487
8488 // Look through 'not' ops to find disguised min/max.
8489 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8490 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8491 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8492 switch (Pred) {
8493 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8494 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8495 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8496 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8497 default: break;
8498 }
8499 }
8500
8501 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8502 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8503 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8504 switch (Pred) {
8505 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8506 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8507 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8508 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8509 default: break;
8510 }
8511 }
8512
8513 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8514 return {SPF_UNKNOWN, SPNB_NA, false};
8515
8516 const APInt *C1;
8517 if (!match(CmpRHS, m_APInt(C1)))
8518 return {SPF_UNKNOWN, SPNB_NA, false};
8519
8520 // An unsigned min/max can be written with a signed compare.
8521 const APInt *C2;
8522 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8523 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8524 // Is the sign bit set?
8525 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8526 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8527 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8528 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8529
8530 // Is the sign bit clear?
8531 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8532 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8533 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8534 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8535 }
8536
8537 return {SPF_UNKNOWN, SPNB_NA, false};
8538}
8539
8540bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8541 bool AllowPoison) {
8542 assert(X && Y && "Invalid operand");
8543
8544 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8545 if (!match(X, m_Neg(m_Specific(Y))))
8546 return false;
8547
8548 auto *BO = cast<BinaryOperator>(X);
8549 if (NeedNSW && !BO->hasNoSignedWrap())
8550 return false;
8551
8552 auto *Zero = cast<Constant>(BO->getOperand(0));
8553 if (!AllowPoison && !Zero->isNullValue())
8554 return false;
8555
8556 return true;
8557 };
8558
8559 // X = -Y or Y = -X
8560 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8561 return true;
8562
8563 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8564 Value *A, *B;
8565 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8566 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8567 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8569}
8570
8571bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8572 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8573 Value *A, *B, *C;
8574 CmpPredicate Pred1, Pred2;
8575 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8576 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8577 return false;
8578
8579 // They must both have samesign flag or not.
8580 if (cast<ICmpInst>(X)->hasSameSign() != cast<ICmpInst>(Y)->hasSameSign())
8581 return false;
8582
8583 if (B == C)
8584 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8585
8586 // Try to infer the relationship from constant ranges.
8587 const APInt *RHSC1, *RHSC2;
8588 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8589 return false;
8590
8591 // Sign bits of two RHSCs should match.
8592 if (cast<ICmpInst>(X)->hasSameSign() &&
8593 RHSC1->isNonNegative() != RHSC2->isNonNegative())
8594 return false;
8595
8596 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8597 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8598
8599 return CR1.inverse() == CR2;
8600}
8601
8603 SelectPatternNaNBehavior NaNBehavior,
8604 bool Ordered) {
8605 switch (Pred) {
8606 default:
8607 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8608 case ICmpInst::ICMP_UGT:
8609 case ICmpInst::ICMP_UGE:
8610 return {SPF_UMAX, SPNB_NA, false};
8611 case ICmpInst::ICMP_SGT:
8612 case ICmpInst::ICMP_SGE:
8613 return {SPF_SMAX, SPNB_NA, false};
8614 case ICmpInst::ICMP_ULT:
8615 case ICmpInst::ICMP_ULE:
8616 return {SPF_UMIN, SPNB_NA, false};
8617 case ICmpInst::ICMP_SLT:
8618 case ICmpInst::ICMP_SLE:
8619 return {SPF_SMIN, SPNB_NA, false};
8620 case FCmpInst::FCMP_UGT:
8621 case FCmpInst::FCMP_UGE:
8622 case FCmpInst::FCMP_OGT:
8623 case FCmpInst::FCMP_OGE:
8624 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8625 case FCmpInst::FCMP_ULT:
8626 case FCmpInst::FCMP_ULE:
8627 case FCmpInst::FCMP_OLT:
8628 case FCmpInst::FCMP_OLE:
8629 return {SPF_FMINNUM, NaNBehavior, Ordered};
8630 }
8631}
8632
8634 FastMathFlags FMF,
8635 Value *CmpLHS, Value *CmpRHS,
8636 Value *TrueVal, Value *FalseVal,
8637 Value *&LHS, Value *&RHS,
8638 unsigned Depth) {
8639 bool HasMismatchedZeros = false;
8640 if (CmpInst::isFPPredicate(Pred)) {
8641 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8642 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8643 // purpose of identifying min/max. Disregard vector constants with undefined
8644 // elements because those can not be back-propagated for analysis.
8645 Value *OutputZeroVal = nullptr;
8646 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8647 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8648 OutputZeroVal = TrueVal;
8649 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8650 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8651 OutputZeroVal = FalseVal;
8652
8653 if (OutputZeroVal) {
8654 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8655 HasMismatchedZeros = true;
8656 CmpLHS = OutputZeroVal;
8657 }
8658 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8659 HasMismatchedZeros = true;
8660 CmpRHS = OutputZeroVal;
8661 }
8662 }
8663 }
8664
8665 LHS = CmpLHS;
8666 RHS = CmpRHS;
8667
8668 // Signed zero may return inconsistent results between implementations.
8669 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8670 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8671 // Therefore, we behave conservatively and only proceed if at least one of the
8672 // operands is known to not be zero or if we don't care about signed zero.
8673 switch (Pred) {
8674 default: break;
8677 if (!HasMismatchedZeros)
8678 break;
8679 [[fallthrough]];
8682 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8683 !isKnownNonZero(CmpRHS))
8684 return {SPF_UNKNOWN, SPNB_NA, false};
8685 }
8686
8687 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8688 bool Ordered = false;
8689
8690 // When given one NaN and one non-NaN input:
8691 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8692 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8693 // ordered comparison fails), which could be NaN or non-NaN.
8694 // so here we discover exactly what NaN behavior is required/accepted.
8695 if (CmpInst::isFPPredicate(Pred)) {
8696 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8697 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8698
8699 if (LHSSafe && RHSSafe) {
8700 // Both operands are known non-NaN.
8701 NaNBehavior = SPNB_RETURNS_ANY;
8702 } else if (CmpInst::isOrdered(Pred)) {
8703 // An ordered comparison will return false when given a NaN, so it
8704 // returns the RHS.
8705 Ordered = true;
8706 if (LHSSafe)
8707 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8708 NaNBehavior = SPNB_RETURNS_NAN;
8709 else if (RHSSafe)
8710 NaNBehavior = SPNB_RETURNS_OTHER;
8711 else
8712 // Completely unsafe.
8713 return {SPF_UNKNOWN, SPNB_NA, false};
8714 } else {
8715 Ordered = false;
8716 // An unordered comparison will return true when given a NaN, so it
8717 // returns the LHS.
8718 if (LHSSafe)
8719 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8720 NaNBehavior = SPNB_RETURNS_OTHER;
8721 else if (RHSSafe)
8722 NaNBehavior = SPNB_RETURNS_NAN;
8723 else
8724 // Completely unsafe.
8725 return {SPF_UNKNOWN, SPNB_NA, false};
8726 }
8727 }
8728
8729 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8730 std::swap(CmpLHS, CmpRHS);
8731 Pred = CmpInst::getSwappedPredicate(Pred);
8732 if (NaNBehavior == SPNB_RETURNS_NAN)
8733 NaNBehavior = SPNB_RETURNS_OTHER;
8734 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8735 NaNBehavior = SPNB_RETURNS_NAN;
8736 Ordered = !Ordered;
8737 }
8738
8739 // ([if]cmp X, Y) ? X : Y
8740 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8741 return getSelectPattern(Pred, NaNBehavior, Ordered);
8742
8743 if (isKnownNegation(TrueVal, FalseVal)) {
8744 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8745 // match against either LHS or sext(LHS).
8746 auto MaybeSExtCmpLHS =
8747 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8748 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8749 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8750 if (match(TrueVal, MaybeSExtCmpLHS)) {
8751 // Set the return values. If the compare uses the negated value (-X >s 0),
8752 // swap the return values because the negated value is always 'RHS'.
8753 LHS = TrueVal;
8754 RHS = FalseVal;
8755 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8756 std::swap(LHS, RHS);
8757
8758 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8759 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8760 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8761 return {SPF_ABS, SPNB_NA, false};
8762
8763 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8764 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8765 return {SPF_ABS, SPNB_NA, false};
8766
8767 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8768 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8769 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8770 return {SPF_NABS, SPNB_NA, false};
8771 }
8772 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8773 // Set the return values. If the compare uses the negated value (-X >s 0),
8774 // swap the return values because the negated value is always 'RHS'.
8775 LHS = FalseVal;
8776 RHS = TrueVal;
8777 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8778 std::swap(LHS, RHS);
8779
8780 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8781 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8782 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8783 return {SPF_NABS, SPNB_NA, false};
8784
8785 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8786 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8787 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8788 return {SPF_ABS, SPNB_NA, false};
8789 }
8790 }
8791
8792 if (CmpInst::isIntPredicate(Pred))
8793 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8794
8795 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8796 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8797 // semantics than minNum. Be conservative in such case.
8798 if (NaNBehavior != SPNB_RETURNS_ANY ||
8799 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8800 !isKnownNonZero(CmpRHS)))
8801 return {SPF_UNKNOWN, SPNB_NA, false};
8802
8803 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8804}
8805
8807 Instruction::CastOps *CastOp) {
8808 const DataLayout &DL = CmpI->getDataLayout();
8809
8810 Constant *CastedTo = nullptr;
8811 switch (*CastOp) {
8812 case Instruction::ZExt:
8813 if (CmpI->isUnsigned())
8814 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8815 break;
8816 case Instruction::SExt:
8817 if (CmpI->isSigned())
8818 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8819 break;
8820 case Instruction::Trunc:
8821 Constant *CmpConst;
8822 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8823 CmpConst->getType() == SrcTy) {
8824 // Here we have the following case:
8825 //
8826 // %cond = cmp iN %x, CmpConst
8827 // %tr = trunc iN %x to iK
8828 // %narrowsel = select i1 %cond, iK %t, iK C
8829 //
8830 // We can always move trunc after select operation:
8831 //
8832 // %cond = cmp iN %x, CmpConst
8833 // %widesel = select i1 %cond, iN %x, iN CmpConst
8834 // %tr = trunc iN %widesel to iK
8835 //
8836 // Note that C could be extended in any way because we don't care about
8837 // upper bits after truncation. It can't be abs pattern, because it would
8838 // look like:
8839 //
8840 // select i1 %cond, x, -x.
8841 //
8842 // So only min/max pattern could be matched. Such match requires widened C
8843 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8844 // CmpConst == C is checked below.
8845 CastedTo = CmpConst;
8846 } else {
8847 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8848 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8849 }
8850 break;
8851 case Instruction::FPTrunc:
8852 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8853 break;
8854 case Instruction::FPExt:
8855 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8856 break;
8857 case Instruction::FPToUI:
8858 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8859 break;
8860 case Instruction::FPToSI:
8861 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8862 break;
8863 case Instruction::UIToFP:
8864 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8865 break;
8866 case Instruction::SIToFP:
8867 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8868 break;
8869 default:
8870 break;
8871 }
8872
8873 if (!CastedTo)
8874 return nullptr;
8875
8876 // Make sure the cast doesn't lose any information.
8877 Constant *CastedBack =
8878 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8879 if (CastedBack && CastedBack != C)
8880 return nullptr;
8881
8882 return CastedTo;
8883}
8884
8885/// Helps to match a select pattern in case of a type mismatch.
8886///
8887/// The function processes the case when type of true and false values of a
8888/// select instruction differs from type of the cmp instruction operands because
8889/// of a cast instruction. The function checks if it is legal to move the cast
8890/// operation after "select". If yes, it returns the new second value of
8891/// "select" (with the assumption that cast is moved):
8892/// 1. As operand of cast instruction when both values of "select" are same cast
8893/// instructions.
8894/// 2. As restored constant (by applying reverse cast operation) when the first
8895/// value of the "select" is a cast operation and the second value is a
8896/// constant. It is implemented in lookThroughCastConst().
8897/// 3. As one operand is cast instruction and the other is not. The operands in
8898/// sel(cmp) are in different type integer.
8899/// NOTE: We return only the new second value because the first value could be
8900/// accessed as operand of cast instruction.
8901static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8902 Instruction::CastOps *CastOp) {
8903 auto *Cast1 = dyn_cast<CastInst>(V1);
8904 if (!Cast1)
8905 return nullptr;
8906
8907 *CastOp = Cast1->getOpcode();
8908 Type *SrcTy = Cast1->getSrcTy();
8909 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8910 // If V1 and V2 are both the same cast from the same type, look through V1.
8911 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
8912 return Cast2->getOperand(0);
8913 return nullptr;
8914 }
8915
8916 auto *C = dyn_cast<Constant>(V2);
8917 if (C)
8918 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
8919
8920 Value *CastedTo = nullptr;
8921 if (*CastOp == Instruction::Trunc) {
8922 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
8923 // Here we have the following case:
8924 // %y_ext = sext iK %y to iN
8925 // %cond = cmp iN %x, %y_ext
8926 // %tr = trunc iN %x to iK
8927 // %narrowsel = select i1 %cond, iK %tr, iK %y
8928 //
8929 // We can always move trunc after select operation:
8930 // %y_ext = sext iK %y to iN
8931 // %cond = cmp iN %x, %y_ext
8932 // %widesel = select i1 %cond, iN %x, iN %y_ext
8933 // %tr = trunc iN %widesel to iK
8934 assert(V2->getType() == Cast1->getType() &&
8935 "V2 and Cast1 should be the same type.");
8936 CastedTo = CmpI->getOperand(1);
8937 }
8938 }
8939
8940 return CastedTo;
8941}
8943 Instruction::CastOps *CastOp,
8944 unsigned Depth) {
8946 return {SPF_UNKNOWN, SPNB_NA, false};
8947
8948 SelectInst *SI = dyn_cast<SelectInst>(V);
8949 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
8950
8951 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
8952 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
8953
8954 Value *TrueVal = SI->getTrueValue();
8955 Value *FalseVal = SI->getFalseValue();
8956
8957 return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
8958 CastOp, Depth);
8959}
8960
8962 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
8963 Instruction::CastOps *CastOp, unsigned Depth) {
8964 CmpInst::Predicate Pred = CmpI->getPredicate();
8965 Value *CmpLHS = CmpI->getOperand(0);
8966 Value *CmpRHS = CmpI->getOperand(1);
8967 FastMathFlags FMF;
8968 if (isa<FPMathOperator>(CmpI))
8969 FMF = CmpI->getFastMathFlags();
8970
8971 // Bail out early.
8972 if (CmpI->isEquality())
8973 return {SPF_UNKNOWN, SPNB_NA, false};
8974
8975 // Deal with type mismatches.
8976 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
8977 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
8978 // If this is a potential fmin/fmax with a cast to integer, then ignore
8979 // -0.0 because there is no corresponding integer value.
8980 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
8981 FMF.setNoSignedZeros();
8982 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
8983 cast<CastInst>(TrueVal)->getOperand(0), C,
8984 LHS, RHS, Depth);
8985 }
8986 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
8987 // If this is a potential fmin/fmax with a cast to integer, then ignore
8988 // -0.0 because there is no corresponding integer value.
8989 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
8990 FMF.setNoSignedZeros();
8991 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
8992 C, cast<CastInst>(FalseVal)->getOperand(0),
8993 LHS, RHS, Depth);
8994 }
8995 }
8996 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
8997 LHS, RHS, Depth);
8998}
8999
9001 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9002 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9003 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9004 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9005 if (SPF == SPF_FMINNUM)
9006 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9007 if (SPF == SPF_FMAXNUM)
9008 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9009 llvm_unreachable("unhandled!");
9010}
9011
9013 switch (SPF) {
9015 return Intrinsic::umin;
9017 return Intrinsic::umax;
9019 return Intrinsic::smin;
9021 return Intrinsic::smax;
9022 default:
9023 llvm_unreachable("Unexpected SPF");
9024 }
9025}
9026
9028 if (SPF == SPF_SMIN) return SPF_SMAX;
9029 if (SPF == SPF_UMIN) return SPF_UMAX;
9030 if (SPF == SPF_SMAX) return SPF_SMIN;
9031 if (SPF == SPF_UMAX) return SPF_UMIN;
9032 llvm_unreachable("unhandled!");
9033}
9034
9036 switch (MinMaxID) {
9037 case Intrinsic::smax: return Intrinsic::smin;
9038 case Intrinsic::smin: return Intrinsic::smax;
9039 case Intrinsic::umax: return Intrinsic::umin;
9040 case Intrinsic::umin: return Intrinsic::umax;
9041 // Please note that next four intrinsics may produce the same result for
9042 // original and inverted case even if X != Y due to NaN is handled specially.
9043 case Intrinsic::maximum: return Intrinsic::minimum;
9044 case Intrinsic::minimum: return Intrinsic::maximum;
9045 case Intrinsic::maxnum: return Intrinsic::minnum;
9046 case Intrinsic::minnum: return Intrinsic::maxnum;
9047 default: llvm_unreachable("Unexpected intrinsic");
9048 }
9049}
9050
9052 switch (SPF) {
9055 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9056 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9057 default: llvm_unreachable("Unexpected flavor");
9058 }
9059}
9060
9061std::pair<Intrinsic::ID, bool>
9063 // Check if VL contains select instructions that can be folded into a min/max
9064 // vector intrinsic and return the intrinsic if it is possible.
9065 // TODO: Support floating point min/max.
9066 bool AllCmpSingleUse = true;
9067 SelectPatternResult SelectPattern;
9068 SelectPattern.Flavor = SPF_UNKNOWN;
9069 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9070 Value *LHS, *RHS;
9071 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9072 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9073 return false;
9074 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9075 SelectPattern.Flavor != CurrentPattern.Flavor)
9076 return false;
9077 SelectPattern = CurrentPattern;
9078 AllCmpSingleUse &=
9080 return true;
9081 })) {
9082 switch (SelectPattern.Flavor) {
9083 case SPF_SMIN:
9084 return {Intrinsic::smin, AllCmpSingleUse};
9085 case SPF_UMIN:
9086 return {Intrinsic::umin, AllCmpSingleUse};
9087 case SPF_SMAX:
9088 return {Intrinsic::smax, AllCmpSingleUse};
9089 case SPF_UMAX:
9090 return {Intrinsic::umax, AllCmpSingleUse};
9091 case SPF_FMAXNUM:
9092 return {Intrinsic::maxnum, AllCmpSingleUse};
9093 case SPF_FMINNUM:
9094 return {Intrinsic::minnum, AllCmpSingleUse};
9095 default:
9096 llvm_unreachable("unexpected select pattern flavor");
9097 }
9098 }
9099 return {Intrinsic::not_intrinsic, false};
9100}
9101
9103 Value *&Start, Value *&Step) {
9104 // Handle the case of a simple two-predecessor recurrence PHI.
9105 // There's a lot more that could theoretically be done here, but
9106 // this is sufficient to catch some interesting cases.
9107 if (P->getNumIncomingValues() != 2)
9108 return false;
9109
9110 for (unsigned i = 0; i != 2; ++i) {
9111 Value *L = P->getIncomingValue(i);
9112 Value *R = P->getIncomingValue(!i);
9113 auto *LU = dyn_cast<BinaryOperator>(L);
9114 if (!LU)
9115 continue;
9116 unsigned Opcode = LU->getOpcode();
9117
9118 switch (Opcode) {
9119 default:
9120 continue;
9121 // TODO: Expand list -- xor, gep, uadd.sat etc.
9122 case Instruction::LShr:
9123 case Instruction::AShr:
9124 case Instruction::Shl:
9125 case Instruction::Add:
9126 case Instruction::Sub:
9127 case Instruction::UDiv:
9128 case Instruction::URem:
9129 case Instruction::And:
9130 case Instruction::Or:
9131 case Instruction::Mul:
9132 case Instruction::FMul: {
9133 Value *LL = LU->getOperand(0);
9134 Value *LR = LU->getOperand(1);
9135 // Find a recurrence.
9136 if (LL == P)
9137 L = LR;
9138 else if (LR == P)
9139 L = LL;
9140 else
9141 continue; // Check for recurrence with L and R flipped.
9142
9143 break; // Match!
9144 }
9145 };
9146
9147 // We have matched a recurrence of the form:
9148 // %iv = [R, %entry], [%iv.next, %backedge]
9149 // %iv.next = binop %iv, L
9150 // OR
9151 // %iv = [R, %entry], [%iv.next, %backedge]
9152 // %iv.next = binop L, %iv
9153 BO = LU;
9154 Start = R;
9155 Step = L;
9156 return true;
9157 }
9158 return false;
9159}
9160
9162 Value *&Start, Value *&Step) {
9163 BinaryOperator *BO = nullptr;
9164 P = dyn_cast<PHINode>(I->getOperand(0));
9165 if (!P)
9166 P = dyn_cast<PHINode>(I->getOperand(1));
9167 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9168}
9169
9170/// Return true if "icmp Pred LHS RHS" is always true.
9171static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
9172 const Value *RHS) {
9173 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9174 return true;
9175
9176 switch (Pred) {
9177 default:
9178 return false;
9179
9180 case CmpInst::ICMP_SLE: {
9181 const APInt *C;
9182
9183 // LHS s<= LHS +_{nsw} C if C >= 0
9184 // LHS s<= LHS | C if C >= 0
9185 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9187 return !C->isNegative();
9188
9189 // LHS s<= smax(LHS, V) for any V
9191 return true;
9192
9193 // smin(RHS, V) s<= RHS for any V
9195 return true;
9196
9197 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9198 const Value *X;
9199 const APInt *CLHS, *CRHS;
9200 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9202 return CLHS->sle(*CRHS);
9203
9204 return false;
9205 }
9206
9207 case CmpInst::ICMP_ULE: {
9208 // LHS u<= LHS +_{nuw} V for any V
9209 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9210 cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
9211 return true;
9212
9213 // LHS u<= LHS | V for any V
9214 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9215 return true;
9216
9217 // LHS u<= umax(LHS, V) for any V
9219 return true;
9220
9221 // RHS >> V u<= RHS for any V
9222 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9223 return true;
9224
9225 // RHS u/ C_ugt_1 u<= RHS
9226 const APInt *C;
9227 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9228 return true;
9229
9230 // RHS & V u<= RHS for any V
9232 return true;
9233
9234 // umin(RHS, V) u<= RHS for any V
9236 return true;
9237
9238 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9239 const Value *X;
9240 const APInt *CLHS, *CRHS;
9241 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9243 return CLHS->ule(*CRHS);
9244
9245 return false;
9246 }
9247 }
9248}
9249
9250/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9251/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9252static std::optional<bool>
9254 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9255 switch (Pred) {
9256 default:
9257 return std::nullopt;
9258
9259 case CmpInst::ICMP_SLT:
9260 case CmpInst::ICMP_SLE:
9261 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9263 return true;
9264 return std::nullopt;
9265
9266 case CmpInst::ICMP_SGT:
9267 case CmpInst::ICMP_SGE:
9268 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9270 return true;
9271 return std::nullopt;
9272
9273 case CmpInst::ICMP_ULT:
9274 case CmpInst::ICMP_ULE:
9275 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9277 return true;
9278 return std::nullopt;
9279
9280 case CmpInst::ICMP_UGT:
9281 case CmpInst::ICMP_UGE:
9282 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9284 return true;
9285 return std::nullopt;
9286 }
9287}
9288
9289/// Return true if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is true.
9290/// Return false if "icmp1 LPred X, Y" implies "icmp2 RPred X, Y" is false.
9291/// Otherwise, return std::nullopt if we can't infer anything.
9292static std::optional<bool>
9294 CmpInst::Predicate RPred) {
9295 if (CmpInst::isImpliedTrueByMatchingCmp(LPred, RPred))
9296 return true;
9297 if (CmpInst::isImpliedFalseByMatchingCmp(LPred, RPred))
9298 return false;
9299
9300 return std::nullopt;
9301}
9302
9303/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9304/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9305/// Otherwise, return std::nullopt if we can't infer anything.
9306static std::optional<bool> isImpliedCondCommonOperandWithCR(
9307 CmpInst::Predicate LPred, const ConstantRange &LCR,
9308 CmpInst::Predicate RPred, const ConstantRange &RCR) {
9310 // If all true values for lhs and true for rhs, lhs implies rhs
9311 if (DomCR.icmp(RPred, RCR))
9312 return true;
9313
9314 // If there is no overlap, lhs implies not rhs
9315 if (DomCR.icmp(CmpInst::getInversePredicate(RPred), RCR))
9316 return false;
9317 return std::nullopt;
9318}
9319
9320/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9321/// is true. Return false if LHS implies RHS is false. Otherwise, return
9322/// std::nullopt if we can't infer anything.
9323static std::optional<bool> isImpliedCondICmps(const ICmpInst *LHS,
9324 CmpInst::Predicate RPred,
9325 const Value *R0, const Value *R1,
9326 const DataLayout &DL,
9327 bool LHSIsTrue) {
9328 Value *L0 = LHS->getOperand(0);
9329 Value *L1 = LHS->getOperand(1);
9330
9331 // The rest of the logic assumes the LHS condition is true. If that's not the
9332 // case, invert the predicate to make it so.
9333 CmpInst::Predicate LPred =
9334 LHSIsTrue ? LHS->getPredicate() : LHS->getInversePredicate();
9335
9336 // We can have non-canonical operands, so try to normalize any common operand
9337 // to L0/R0.
9338 if (L0 == R1) {
9339 std::swap(R0, R1);
9340 RPred = ICmpInst::getSwappedPredicate(RPred);
9341 }
9342 if (R0 == L1) {
9343 std::swap(L0, L1);
9344 LPred = ICmpInst::getSwappedPredicate(LPred);
9345 }
9346 if (L1 == R1) {
9347 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9348 if (L0 != R0 || match(L0, m_ImmConstant())) {
9349 std::swap(L0, L1);
9350 LPred = ICmpInst::getSwappedPredicate(LPred);
9351 std::swap(R0, R1);
9352 RPred = ICmpInst::getSwappedPredicate(RPred);
9353 }
9354 }
9355
9356 // See if we can infer anything if operand-0 matches and we have at least one
9357 // constant.
9358 const APInt *Unused;
9359 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9360 // Potential TODO: We could also further use the constant range of L0/R0 to
9361 // further constraint the constant ranges. At the moment this leads to
9362 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9363 // C1` (see discussion: D58633).
9365 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9366 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9368 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9369 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9370 // Even if L1/R1 are not both constant, we can still sometimes deduce
9371 // relationship from a single constant. For example X u> Y implies X != 0.
9372 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9373 return R;
9374 // If both L1/R1 were exact constant ranges and we didn't get anything
9375 // here, we won't be able to deduce this.
9376 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9377 return std::nullopt;
9378 }
9379
9380 // Can we infer anything when the two compares have matching operands?
9381 if (L0 == R0 && L1 == R1)
9382 return isImpliedCondMatchingOperands(LPred, RPred);
9383
9384 // It only really makes sense in the context of signed comparison for "X - Y
9385 // must be positive if X >= Y and no overflow".
9386 // Take SGT as an example: L0:x > L1:y and C >= 0
9387 // ==> R0:(x -nsw y) < R1:(-C) is false
9388 if ((LPred == ICmpInst::ICMP_SGT || LPred == ICmpInst::ICMP_SGE) &&
9389 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9390 if (match(R1, m_NonPositive()) &&
9391 isImpliedCondMatchingOperands(LPred, RPred) == false)
9392 return false;
9393 }
9394
9395 // Take SLT as an example: L0:x < L1:y and C <= 0
9396 // ==> R0:(x -nsw y) < R1:(-C) is true
9397 if ((LPred == ICmpInst::ICMP_SLT || LPred == ICmpInst::ICMP_SLE) &&
9398 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9399 if (match(R1, m_NonNegative()) &&
9400 isImpliedCondMatchingOperands(LPred, RPred) == true)
9401 return true;
9402 }
9403
9404 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9405 if (L0 == R0 &&
9406 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9407 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9408 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9409 return CmpPredicate::getMatching(LPred, RPred).has_value();
9410
9411 if (LPred == RPred)
9412 return isImpliedCondOperands(LPred, L0, L1, R0, R1);
9413
9414 return std::nullopt;
9415}
9416
9417/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9418/// false. Otherwise, return std::nullopt if we can't infer anything. We
9419/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9420/// instruction.
9421static std::optional<bool>
9423 const Value *RHSOp0, const Value *RHSOp1,
9424 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9425 // The LHS must be an 'or', 'and', or a 'select' instruction.
9426 assert((LHS->getOpcode() == Instruction::And ||
9427 LHS->getOpcode() == Instruction::Or ||
9428 LHS->getOpcode() == Instruction::Select) &&
9429 "Expected LHS to be 'and', 'or', or 'select'.");
9430
9431 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9432
9433 // If the result of an 'or' is false, then we know both legs of the 'or' are
9434 // false. Similarly, if the result of an 'and' is true, then we know both
9435 // legs of the 'and' are true.
9436 const Value *ALHS, *ARHS;
9437 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9438 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9439 // FIXME: Make this non-recursion.
9440 if (std::optional<bool> Implication = isImpliedCondition(
9441 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9442 return Implication;
9443 if (std::optional<bool> Implication = isImpliedCondition(
9444 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9445 return Implication;
9446 return std::nullopt;
9447 }
9448 return std::nullopt;
9449}
9450
9451std::optional<bool>
9453 const Value *RHSOp0, const Value *RHSOp1,
9454 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9455 // Bail out when we hit the limit.
9457 return std::nullopt;
9458
9459 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9460 // example.
9461 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9462 return std::nullopt;
9463
9465 "Expected integer type only!");
9466
9467 // Match not
9468 if (match(LHS, m_Not(m_Value(LHS))))
9469 LHSIsTrue = !LHSIsTrue;
9470
9471 // Both LHS and RHS are icmps.
9472 const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
9473 if (LHSCmp)
9474 return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
9475
9476 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9477 /// the RHS to be an icmp.
9478 /// FIXME: Add support for and/or/select on the RHS.
9479 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9480 if ((LHSI->getOpcode() == Instruction::And ||
9481 LHSI->getOpcode() == Instruction::Or ||
9482 LHSI->getOpcode() == Instruction::Select))
9483 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9484 Depth);
9485 }
9486 return std::nullopt;
9487}
9488
9489std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9490 const DataLayout &DL,
9491 bool LHSIsTrue, unsigned Depth) {
9492 // LHS ==> RHS by definition
9493 if (LHS == RHS)
9494 return LHSIsTrue;
9495
9496 // Match not
9497 bool InvertRHS = false;
9498 if (match(RHS, m_Not(m_Value(RHS)))) {
9499 if (LHS == RHS)
9500 return !LHSIsTrue;
9501 InvertRHS = true;
9502 }
9503
9504 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9505 if (auto Implied = isImpliedCondition(
9506 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9507 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9508 return InvertRHS ? !*Implied : *Implied;
9509 return std::nullopt;
9510 }
9511
9513 return std::nullopt;
9514
9515 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9516 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9517 const Value *RHS1, *RHS2;
9518 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9519 if (std::optional<bool> Imp =
9520 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9521 if (*Imp == true)
9522 return !InvertRHS;
9523 if (std::optional<bool> Imp =
9524 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9525 if (*Imp == true)
9526 return !InvertRHS;
9527 }
9528 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9529 if (std::optional<bool> Imp =
9530 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9531 if (*Imp == false)
9532 return InvertRHS;
9533 if (std::optional<bool> Imp =
9534 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9535 if (*Imp == false)
9536 return InvertRHS;
9537 }
9538
9539 return std::nullopt;
9540}
9541
9542// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9543// condition dominating ContextI or nullptr, if no condition is found.
9544static std::pair<Value *, bool>
9546 if (!ContextI || !ContextI->getParent())
9547 return {nullptr, false};
9548
9549 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9550 // dominator tree (eg, from a SimplifyQuery) instead?
9551 const BasicBlock *ContextBB = ContextI->getParent();
9552 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9553 if (!PredBB)
9554 return {nullptr, false};
9555
9556 // We need a conditional branch in the predecessor.
9557 Value *PredCond;
9558 BasicBlock *TrueBB, *FalseBB;
9559 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9560 return {nullptr, false};
9561
9562 // The branch should get simplified. Don't bother simplifying this condition.
9563 if (TrueBB == FalseBB)
9564 return {nullptr, false};
9565
9566 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9567 "Predecessor block does not point to successor?");
9568
9569 // Is this condition implied by the predecessor condition?
9570 return {PredCond, TrueBB == ContextBB};
9571}
9572
9573std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9574 const Instruction *ContextI,
9575 const DataLayout &DL) {
9576 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9577 auto PredCond = getDomPredecessorCondition(ContextI);
9578 if (PredCond.first)
9579 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9580 return std::nullopt;
9581}
9582
9584 const Value *LHS,
9585 const Value *RHS,
9586 const Instruction *ContextI,
9587 const DataLayout &DL) {
9588 auto PredCond = getDomPredecessorCondition(ContextI);
9589 if (PredCond.first)
9590 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9591 PredCond.second);
9592 return std::nullopt;
9593}
9594
9596 APInt &Upper, const InstrInfoQuery &IIQ,
9597 bool PreferSignedRange) {
9598 unsigned Width = Lower.getBitWidth();
9599 const APInt *C;
9600 switch (BO.getOpcode()) {
9601 case Instruction::Add:
9602 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9603 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9604 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9605
9606 // If the caller expects a signed compare, then try to use a signed range.
9607 // Otherwise if both no-wraps are set, use the unsigned range because it
9608 // is never larger than the signed range. Example:
9609 // "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9610 if (PreferSignedRange && HasNSW && HasNUW)
9611 HasNUW = false;
9612
9613 if (HasNUW) {
9614 // 'add nuw x, C' produces [C, UINT_MAX].
9615 Lower = *C;
9616 } else if (HasNSW) {
9617 if (C->isNegative()) {
9618 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9620 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9621 } else {
9622 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9623 Lower = APInt::getSignedMinValue(Width) + *C;
9624 Upper = APInt::getSignedMaxValue(Width) + 1;
9625 }
9626 }
9627 }
9628 break;
9629
9630 case Instruction::And:
9631 if (match(BO.getOperand(1), m_APInt(C)))
9632 // 'and x, C' produces [0, C].
9633 Upper = *C + 1;
9634 // X & -X is a power of two or zero. So we can cap the value at max power of
9635 // two.
9636 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9637 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9638 Upper = APInt::getSignedMinValue(Width) + 1;
9639 break;
9640
9641 case Instruction::Or:
9642 if (match(BO.getOperand(1), m_APInt(C)))
9643 // 'or x, C' produces [C, UINT_MAX].
9644 Lower = *C;
9645 break;
9646
9647 case Instruction::AShr:
9648 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9649 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9651 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9652 } else if (match(BO.getOperand(0), m_APInt(C))) {
9653 unsigned ShiftAmount = Width - 1;
9654 if (!C->isZero() && IIQ.isExact(&BO))
9655 ShiftAmount = C->countr_zero();
9656 if (C->isNegative()) {
9657 // 'ashr C, x' produces [C, C >> (Width-1)]
9658 Lower = *C;
9659 Upper = C->ashr(ShiftAmount) + 1;
9660 } else {
9661 // 'ashr C, x' produces [C >> (Width-1), C]
9662 Lower = C->ashr(ShiftAmount);
9663 Upper = *C + 1;
9664 }
9665 }
9666 break;
9667
9668 case Instruction::LShr:
9669 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9670 // 'lshr x, C' produces [0, UINT_MAX >> C].
9671 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9672 } else if (match(BO.getOperand(0), m_APInt(C))) {
9673 // 'lshr C, x' produces [C >> (Width-1), C].
9674 unsigned ShiftAmount = Width - 1;
9675 if (!C->isZero() && IIQ.isExact(&BO))
9676 ShiftAmount = C->countr_zero();
9677 Lower = C->lshr(ShiftAmount);
9678 Upper = *C + 1;
9679 }
9680 break;
9681
9682 case Instruction::Shl:
9683 if (match(BO.getOperand(0), m_APInt(C))) {
9684 if (IIQ.hasNoUnsignedWrap(&BO)) {
9685 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9686 Lower = *C;
9687 Upper = Lower.shl(Lower.countl_zero()) + 1;
9688 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9689 if (C->isNegative()) {
9690 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9691 unsigned ShiftAmount = C->countl_one() - 1;
9692 Lower = C->shl(ShiftAmount);
9693 Upper = *C + 1;
9694 } else {
9695 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9696 unsigned ShiftAmount = C->countl_zero() - 1;
9697 Lower = *C;
9698 Upper = C->shl(ShiftAmount) + 1;
9699 }
9700 } else {
9701 // If lowbit is set, value can never be zero.
9702 if ((*C)[0])
9703 Lower = APInt::getOneBitSet(Width, 0);
9704 // If we are shifting a constant the largest it can be is if the longest
9705 // sequence of consecutive ones is shifted to the highbits (breaking
9706 // ties for which sequence is higher). At the moment we take a liberal
9707 // upper bound on this by just popcounting the constant.
9708 // TODO: There may be a bitwise trick for it longest/highest
9709 // consecutative sequence of ones (naive method is O(Width) loop).
9710 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9711 }
9712 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9713 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9714 }
9715 break;
9716
9717 case Instruction::SDiv:
9718 if (match(BO.getOperand(1), m_APInt(C))) {
9719 APInt IntMin = APInt::getSignedMinValue(Width);
9720 APInt IntMax = APInt::getSignedMaxValue(Width);
9721 if (C->isAllOnes()) {
9722 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9723 // where C != -1 and C != 0 and C != 1
9724 Lower = IntMin + 1;
9725 Upper = IntMax + 1;
9726 } else if (C->countl_zero() < Width - 1) {
9727 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9728 // where C != -1 and C != 0 and C != 1
9729 Lower = IntMin.sdiv(*C);
9730 Upper = IntMax.sdiv(*C);
9731 if (Lower.sgt(Upper))
9733 Upper = Upper + 1;
9734 assert(Upper != Lower && "Upper part of range has wrapped!");
9735 }
9736 } else if (match(BO.getOperand(0), m_APInt(C))) {
9737 if (C->isMinSignedValue()) {
9738 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9739 Lower = *C;
9740 Upper = Lower.lshr(1) + 1;
9741 } else {
9742 // 'sdiv C, x' produces [-|C|, |C|].
9743 Upper = C->abs() + 1;
9744 Lower = (-Upper) + 1;
9745 }
9746 }
9747 break;
9748
9749 case Instruction::UDiv:
9750 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9751 // 'udiv x, C' produces [0, UINT_MAX / C].
9752 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9753 } else if (match(BO.getOperand(0), m_APInt(C))) {
9754 // 'udiv C, x' produces [0, C].
9755 Upper = *C + 1;
9756 }
9757 break;
9758
9759 case Instruction::SRem:
9760 if (match(BO.getOperand(1), m_APInt(C))) {
9761 // 'srem x, C' produces (-|C|, |C|).
9762 Upper = C->abs();
9763 Lower = (-Upper) + 1;
9764 } else if (match(BO.getOperand(0), m_APInt(C))) {
9765 if (C->isNegative()) {
9766 // 'srem -|C|, x' produces [-|C|, 0].
9767 Upper = 1;
9768 Lower = *C;
9769 } else {
9770 // 'srem |C|, x' produces [0, |C|].
9771 Upper = *C + 1;
9772 }
9773 }
9774 break;
9775
9776 case Instruction::URem:
9777 if (match(BO.getOperand(1), m_APInt(C)))
9778 // 'urem x, C' produces [0, C).
9779 Upper = *C;
9780 else if (match(BO.getOperand(0), m_APInt(C)))
9781 // 'urem C, x' produces [0, C].
9782 Upper = *C + 1;
9783 break;
9784
9785 default:
9786 break;
9787 }
9788}
9789
9791 unsigned Width = II.getType()->getScalarSizeInBits();
9792 const APInt *C;
9793 switch (II.getIntrinsicID()) {
9794 case Intrinsic::ctpop:
9795 case Intrinsic::ctlz:
9796 case Intrinsic::cttz:
9797 // Maximum of set/clear bits is the bit width.
9799 APInt(Width, Width) + 1);
9800 case Intrinsic::uadd_sat:
9801 // uadd.sat(x, C) produces [C, UINT_MAX].
9802 if (match(II.getOperand(0), m_APInt(C)) ||
9803 match(II.getOperand(1), m_APInt(C)))
9805 break;
9806 case Intrinsic::sadd_sat:
9807 if (match(II.getOperand(0), m_APInt(C)) ||
9808 match(II.getOperand(1), m_APInt(C))) {
9809 if (C->isNegative())
9810 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9812 APInt::getSignedMaxValue(Width) + *C +
9813 1);
9814
9815 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9817 APInt::getSignedMaxValue(Width) + 1);
9818 }
9819 break;
9820 case Intrinsic::usub_sat:
9821 // usub.sat(C, x) produces [0, C].
9822 if (match(II.getOperand(0), m_APInt(C)))
9823 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9824
9825 // usub.sat(x, C) produces [0, UINT_MAX - C].
9826 if (match(II.getOperand(1), m_APInt(C)))
9828 APInt::getMaxValue(Width) - *C + 1);
9829 break;
9830 case Intrinsic::ssub_sat:
9831 if (match(II.getOperand(0), m_APInt(C))) {
9832 if (C->isNegative())
9833 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9835 *C - APInt::getSignedMinValue(Width) +
9836 1);
9837
9838 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9840 APInt::getSignedMaxValue(Width) + 1);
9841 } else if (match(II.getOperand(1), m_APInt(C))) {
9842 if (C->isNegative())
9843 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9845 APInt::getSignedMaxValue(Width) + 1);
9846
9847 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9849 APInt::getSignedMaxValue(Width) - *C +
9850 1);
9851 }
9852 break;
9853 case Intrinsic::umin:
9854 case Intrinsic::umax:
9855 case Intrinsic::smin:
9856 case Intrinsic::smax:
9857 if (!match(II.getOperand(0), m_APInt(C)) &&
9858 !match(II.getOperand(1), m_APInt(C)))
9859 break;
9860
9861 switch (II.getIntrinsicID()) {
9862 case Intrinsic::umin:
9863 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9864 case Intrinsic::umax:
9866 case Intrinsic::smin:
9868 *C + 1);
9869 case Intrinsic::smax:
9871 APInt::getSignedMaxValue(Width) + 1);
9872 default:
9873 llvm_unreachable("Must be min/max intrinsic");
9874 }
9875 break;
9876 case Intrinsic::abs:
9877 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
9878 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9879 if (match(II.getOperand(1), m_One()))
9881 APInt::getSignedMaxValue(Width) + 1);
9882
9884 APInt::getSignedMinValue(Width) + 1);
9885 case Intrinsic::vscale:
9886 if (!II.getParent() || !II.getFunction())
9887 break;
9888 return getVScaleRange(II.getFunction(), Width);
9889 case Intrinsic::scmp:
9890 case Intrinsic::ucmp:
9892 APInt(Width, 2));
9893 default:
9894 break;
9895 }
9896
9897 return ConstantRange::getFull(Width);
9898}
9899
9901 const InstrInfoQuery &IIQ) {
9902 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
9903 const Value *LHS = nullptr, *RHS = nullptr;
9905 if (R.Flavor == SPF_UNKNOWN)
9906 return ConstantRange::getFull(BitWidth);
9907
9908 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
9909 // If the negation part of the abs (in RHS) has the NSW flag,
9910 // then the result of abs(X) is [0..SIGNED_MAX],
9911 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9912 if (match(RHS, m_Neg(m_Specific(LHS))) &&
9913 IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
9916
9919 }
9920
9921 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
9922 // The result of -abs(X) is <= 0.
9924 APInt(BitWidth, 1));
9925 }
9926
9927 const APInt *C;
9928 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
9929 return ConstantRange::getFull(BitWidth);
9930
9931 switch (R.Flavor) {
9932 case SPF_UMIN:
9934 case SPF_UMAX:
9936 case SPF_SMIN:
9938 *C + 1);
9939 case SPF_SMAX:
9942 default:
9943 return ConstantRange::getFull(BitWidth);
9944 }
9945}
9946
9948 // The maximum representable value of a half is 65504. For floats the maximum
9949 // value is 3.4e38 which requires roughly 129 bits.
9950 unsigned BitWidth = I->getType()->getScalarSizeInBits();
9951 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
9952 return;
9953 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
9954 Lower = APInt(BitWidth, -65504, true);
9955 Upper = APInt(BitWidth, 65505);
9956 }
9957
9958 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
9959 // For a fptoui the lower limit is left as 0.
9960 Upper = APInt(BitWidth, 65505);
9961 }
9962}
9963
9965 bool UseInstrInfo, AssumptionCache *AC,
9966 const Instruction *CtxI,
9967 const DominatorTree *DT,
9968 unsigned Depth) {
9969 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
9970
9972 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
9973
9974 if (auto *C = dyn_cast<Constant>(V))
9975 return C->toConstantRange();
9976
9977 unsigned BitWidth = V->getType()->getScalarSizeInBits();
9978 InstrInfoQuery IIQ(UseInstrInfo);
9979 ConstantRange CR = ConstantRange::getFull(BitWidth);
9980 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
9981 APInt Lower = APInt(BitWidth, 0);
9982 APInt Upper = APInt(BitWidth, 0);
9983 // TODO: Return ConstantRange.
9984 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
9986 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
9987 CR = getRangeForIntrinsic(*II);
9988 else if (auto *SI = dyn_cast<SelectInst>(V)) {
9990 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
9992 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
9993 CR = CRTrue.unionWith(CRFalse);
9994 CR = CR.intersectWith(getRangeForSelectPattern(*SI, IIQ));
9995 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
9996 APInt Lower = APInt(BitWidth, 0);
9997 APInt Upper = APInt(BitWidth, 0);
9998 // TODO: Return ConstantRange.
9999 setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
10001 } else if (const auto *A = dyn_cast<Argument>(V))
10002 if (std::optional<ConstantRange> Range = A->getRange())
10003 CR = *Range;
10004
10005 if (auto *I = dyn_cast<Instruction>(V)) {
10006 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10008
10009 if (const auto *CB = dyn_cast<CallBase>(V))
10010 if (std::optional<ConstantRange> Range = CB->getRange())
10011 CR = CR.intersectWith(*Range);
10012 }
10013
10014 if (CtxI && AC) {
10015 // Try to restrict the range based on information from assumptions.
10016 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10017 if (!AssumeVH)
10018 continue;
10019 CallInst *I = cast<CallInst>(AssumeVH);
10020 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10021 "Got assumption for the wrong function!");
10022 assert(I->getIntrinsicID() == Intrinsic::assume &&
10023 "must be an assume intrinsic");
10024
10025 if (!isValidAssumeForContext(I, CtxI, DT))
10026 continue;
10027 Value *Arg = I->getArgOperand(0);
10028 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10029 // Currently we just use information from comparisons.
10030 if (!Cmp || Cmp->getOperand(0) != V)
10031 continue;
10032 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10034 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10035 UseInstrInfo, AC, I, DT, Depth + 1);
10036 CR = CR.intersectWith(
10037 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10038 }
10039 }
10040
10041 return CR;
10042}
10043
10044static void
10046 function_ref<void(Value *)> InsertAffected) {
10047 assert(V != nullptr);
10048 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10049 InsertAffected(V);
10050 } else if (auto *I = dyn_cast<Instruction>(V)) {
10051 InsertAffected(V);
10052
10053 // Peek through unary operators to find the source of the condition.
10054 Value *Op;
10056 if (isa<Instruction>(Op) || isa<Argument>(Op))
10057 InsertAffected(Op);
10058 }
10059 }
10060}
10061
10063 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10064 auto AddAffected = [&InsertAffected](Value *V) {
10065 addValueAffectedByCondition(V, InsertAffected);
10066 };
10067
10068 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10069 if (IsAssume) {
10070 AddAffected(LHS);
10071 AddAffected(RHS);
10072 } else if (match(RHS, m_Constant()))
10073 AddAffected(LHS);
10074 };
10075
10076 SmallVector<Value *, 8> Worklist;
10078 Worklist.push_back(Cond);
10079 while (!Worklist.empty()) {
10080 Value *V = Worklist.pop_back_val();
10081 if (!Visited.insert(V).second)
10082 continue;
10083
10084 CmpPredicate Pred;
10085 Value *A, *B, *X;
10086
10087 if (IsAssume) {
10088 AddAffected(V);
10089 if (match(V, m_Not(m_Value(X))))
10090 AddAffected(X);
10091 }
10092
10093 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10094 // assume(A && B) is split to -> assume(A); assume(B);
10095 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10096 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10097 // enough information to be worth handling (intersection of information as
10098 // opposed to union).
10099 if (!IsAssume) {
10100 Worklist.push_back(A);
10101 Worklist.push_back(B);
10102 }
10103 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10104 AddCmpOperands(A, B);
10105
10106 bool HasRHSC = match(B, m_ConstantInt());
10107 if (ICmpInst::isEquality(Pred)) {
10108 if (HasRHSC) {
10109 Value *Y;
10110 // (X & C) or (X | C) or (X ^ C).
10111 // (X << C) or (X >>_s C) or (X >>_u C).
10114 AddAffected(X);
10115 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10116 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10117 AddAffected(X);
10118 AddAffected(Y);
10119 }
10120 }
10121 } else {
10122 if (HasRHSC) {
10123 // Handle (A + C1) u< C2, which is the canonical form of
10124 // A > C3 && A < C4.
10126 AddAffected(X);
10127
10128 if (ICmpInst::isUnsigned(Pred)) {
10129 Value *Y;
10130 // X & Y u> C -> X >u C && Y >u C
10131 // X | Y u< C -> X u< C && Y u< C
10132 // X nuw+ Y u< C -> X u< C && Y u< C
10133 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10134 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10135 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10136 AddAffected(X);
10137 AddAffected(Y);
10138 }
10139 // X nuw- Y u> C -> X u> C
10140 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10141 AddAffected(X);
10142 }
10143 }
10144
10145 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10146 // by computeKnownFPClass().
10148 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10149 InsertAffected(X);
10150 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10151 InsertAffected(X);
10152 }
10153 }
10154
10155 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10156 AddAffected(X);
10157 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10158 AddCmpOperands(A, B);
10159
10160 // fcmp fneg(x), y
10161 // fcmp fabs(x), y
10162 // fcmp fneg(fabs(x)), y
10163 if (match(A, m_FNeg(m_Value(A))))
10164 AddAffected(A);
10165 if (match(A, m_FAbs(m_Value(A))))
10166 AddAffected(A);
10167
10168 } else if (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
10169 m_Value()))) {
10170 // Handle patterns that computeKnownFPClass() support.
10171 AddAffected(A);
10172 }
10173 }
10174}
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:1313
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:500
Module.h This file contains the declarations for the Module class.
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.
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 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 std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, unsigned Depth, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext)
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 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 Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
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 void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q)
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 isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
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:1440
bool isFinite() const
Definition: APFloat.h:1445
bool isNaN() const
Definition: APFloat.h:1438
APInt bitcastToAPInt() const
Definition: APFloat.h:1346
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1135
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1095
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5452
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:1076
bool isSmallestNormalized() const
Definition: APFloat.h:1460
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1547
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1407
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:423
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1520
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1392
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1386
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1007
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:206
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1330
unsigned ceilLogBase2() const
Definition: APInt.h:1742
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1201
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:371
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1640
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1468
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:209
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:216
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:329
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:1249
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1618
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1397
APInt reverseBits() const
Definition: APInt.cpp:741
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1166
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1607
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1015
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:356
unsigned logBase2() const
Definition: APInt.h:1739
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:827
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1319
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:471
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:405
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1150
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:873
APInt byteSwap() const
Definition: APInt.cpp:719
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1130
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:296
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1389
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1237
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:239
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:858
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
void clearSignBit()
Set the sign bit to 0.
Definition: APInt.h:1431
an instruction to allocate memory on the stack
Definition: Instructions.h:63
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:157
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
iterator begin() const
Definition: ArrayRef.h:156
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
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:198
Class to represent array types.
Definition: DerivedTypes.h:395
Type * getElementType() const
Definition: DerivedTypes.h:408
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:466
unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
Definition: Attributes.cpp:460
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:208
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:461
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:448
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:178
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:367
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:459
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:489
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:219
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
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:239
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition: InstrTypes.h:370
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1120
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1349
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:1724
Value * getCalledOperand() const
Definition: InstrTypes.h:1342
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1294
unsigned arg_size() const
Definition: InstrTypes.h:1292
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:444
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:661
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:673
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:676
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:690
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:702
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:703
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:679
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:688
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:677
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:678
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:697
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:696
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:700
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:687
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:681
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:684
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:698
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:685
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:680
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:682
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:701
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:689
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:699
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:686
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:675
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:683
bool isSigned() const
Definition: InstrTypes.h:928
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:825
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:940
bool isFPPredicate() const
Definition: InstrTypes.h:780
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:787
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:763
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:781
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:934
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition: CmpPredicate.h:22
static std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:696
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition: Constants.h:587
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition: Constants.h:662
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:3114
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:770
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2321
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2279
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:271
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:866
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:208
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:157
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:1708
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
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:63
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:197
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:709
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
Definition: DataLayout.cpp:754
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:743
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:617
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:205
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:809
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition: Function.cpp:807
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:933
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition: Globals.cpp:130
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:176
Value * getPointerOperand()
Definition: Instructions.h:255
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:211
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:39
Metadata node.
Definition: Metadata.h:1069
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:1878
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed.
Definition: Operator.h:155
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition: Operator.h:174
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.
size_type size() const
Definition: SmallPtrSet.h:94
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
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:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:175
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:181
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void reserve(size_type N)
Definition: SmallVector.h:663
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:683
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:567
TypeSize getElementOffset(unsigned Idx) const
Definition: DataLayout.h:596
Class to represent struct types.
Definition: DerivedTypes.h:218
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:365
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:366
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:270
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:243
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:264
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:310
static IntegerType * getInt16Ty(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
bool isIEEE() const
Return whether the type is IEEE compatible, as defined by the eponymous method in APFloat.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:267
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition: Type.h:252
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:237
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1859
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:288
Value * getOperand(unsigned i) const
Definition: User.h:228
unsigned getNumOperands() const
Definition: User.h:250
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:740
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)
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)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, 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:885
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
Definition: PatternMatch.h:560
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.
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
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)
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
Definition: PatternMatch.h:832
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:903
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
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:864
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.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
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)
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".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
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.
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
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)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, 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.
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)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
Definition: PatternMatch.h:582
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:1739
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:1697
bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
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:215
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:2115
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...
Definition: Loads.cpp:370
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.
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
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...
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 isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
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:1746
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:44
bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
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.
SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
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:1187
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...
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
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 isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
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.
const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
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 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.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
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...
void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=6)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1903
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 isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
Definition: VectorUtils.cpp:46
bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
static unsigned int semanticsPrecision(const fltSemantics &)
Definition: APFloat.cpp:317
static bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition: APFloat.cpp:358
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:293
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
Definition: KnownBits.cpp:765
static std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
Definition: KnownBits.cpp:488
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition: KnownBits.h:178
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:909
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:247
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:211
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:100
KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
Definition: KnownBits.cpp:1120
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:116
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:774
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition: KnownBits.h:243
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:234
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:768
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1049
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:65
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:266
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:1131
void makeNegative()
Make this value negative.
Definition: KnownBits.h:111
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:153
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:50
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:281
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:85
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:43
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:187
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:53
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:73
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:313
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:103
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:303
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:172
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition: KnownBits.h:237
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition: KnownBits.h:336
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:188
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:240
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:137
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:901
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1066
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1009
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:60
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:953
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:318
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:97
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition: KnownBits.h:342
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:272
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition: KnownBits.h:91
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:211
static KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
Definition: KnownBits.cpp:771
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:804
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:159
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:550
static std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
Definition: KnownBits.cpp:526
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:205
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition: KnownBits.h:198
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition: KnownBits.h:59
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
SimplifyQuery getWithoutCondContext() const
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