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