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