LLVM 22.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"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Constant.h"
43#include "llvm/IR/Constants.h"
46#include "llvm/IR/Dominators.h"
48#include "llvm/IR/Function.h"
50#include "llvm/IR/GlobalAlias.h"
51#include "llvm/IR/GlobalValue.h"
53#include "llvm/IR/InstrTypes.h"
54#include "llvm/IR/Instruction.h"
57#include "llvm/IR/Intrinsics.h"
58#include "llvm/IR/IntrinsicsAArch64.h"
59#include "llvm/IR/IntrinsicsAMDGPU.h"
60#include "llvm/IR/IntrinsicsRISCV.h"
61#include "llvm/IR/IntrinsicsX86.h"
62#include "llvm/IR/LLVMContext.h"
63#include "llvm/IR/Metadata.h"
64#include "llvm/IR/Module.h"
65#include "llvm/IR/Operator.h"
67#include "llvm/IR/Type.h"
68#include "llvm/IR/User.h"
69#include "llvm/IR/Value.h"
78#include <algorithm>
79#include <cassert>
80#include <cstdint>
81#include <optional>
82#include <utility>
83
84using namespace llvm;
85using namespace llvm::PatternMatch;
86
87// Controls the number of uses of the value searched for possible
88// dominating comparisons.
89static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
90 cl::Hidden, cl::init(20));
91
92
93/// Returns the bitwidth of the given scalar or pointer type. For vector types,
94/// returns the element type's bitwidth.
95static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
96 if (unsigned BitWidth = Ty->getScalarSizeInBits())
97 return BitWidth;
98
99 return DL.getPointerTypeSizeInBits(Ty);
100}
101
102// Given the provided Value and, potentially, a context instruction, return
103// the preferred context instruction (if any).
104static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
105 // If we've been provided with a context instruction, then use that (provided
106 // it has been inserted).
107 if (CxtI && CxtI->getParent())
108 return CxtI;
109
110 // If the value is really an already-inserted instruction, then use that.
111 CxtI = dyn_cast<Instruction>(V);
112 if (CxtI && CxtI->getParent())
113 return CxtI;
114
115 return nullptr;
116}
117
119 const APInt &DemandedElts,
120 APInt &DemandedLHS, APInt &DemandedRHS) {
121 if (isa<ScalableVectorType>(Shuf->getType())) {
122 assert(DemandedElts == APInt(1,1));
123 DemandedLHS = DemandedRHS = DemandedElts;
124 return true;
125 }
126
127 int NumElts =
128 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
129 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
130 DemandedElts, DemandedLHS, DemandedRHS);
131}
132
133static void computeKnownBits(const Value *V, const APInt &DemandedElts,
134 KnownBits &Known, const SimplifyQuery &Q,
135 unsigned Depth);
136
138 const SimplifyQuery &Q, unsigned Depth) {
139 // Since the number of lanes in a scalable vector is unknown at compile time,
140 // we track one bit which is implicitly broadcast to all lanes. This means
141 // that all lanes in a scalable vector are considered demanded.
142 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
143 APInt DemandedElts =
144 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
145 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
146}
147
149 const DataLayout &DL, AssumptionCache *AC,
150 const Instruction *CxtI, const DominatorTree *DT,
151 bool UseInstrInfo, unsigned Depth) {
152 computeKnownBits(V, Known,
153 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
154 Depth);
155}
156
158 AssumptionCache *AC, const Instruction *CxtI,
159 const DominatorTree *DT, bool UseInstrInfo,
160 unsigned Depth) {
161 return computeKnownBits(
162 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
163}
164
165KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
166 const DataLayout &DL, AssumptionCache *AC,
167 const Instruction *CxtI,
168 const DominatorTree *DT, bool UseInstrInfo,
169 unsigned Depth) {
170 return computeKnownBits(
171 V, DemandedElts,
172 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
173}
174
176 const SimplifyQuery &SQ) {
177 // Look for an inverted mask: (X & ~M) op (Y & M).
178 {
179 Value *M;
180 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
182 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
183 return true;
184 }
185
186 // X op (Y & ~X)
189 return true;
190
191 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
192 // for constant Y.
193 Value *Y;
194 if (match(RHS,
196 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
197 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
198 return true;
199
200 // Peek through extends to find a 'not' of the other side:
201 // (ext Y) op ext(~Y)
202 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
204 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
205 return true;
206
207 // Look for: (A & B) op ~(A | B)
208 {
209 Value *A, *B;
210 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
212 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
213 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
214 return true;
215 }
216
217 // Look for: (X << V) op (Y >> (BitWidth - V))
218 // or (X >> V) op (Y << (BitWidth - V))
219 {
220 const Value *V;
221 const APInt *R;
222 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
223 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
224 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
225 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
226 R->uge(LHS->getType()->getScalarSizeInBits()))
227 return true;
228 }
229
230 return false;
231}
232
234 const WithCache<const Value *> &RHSCache,
235 const SimplifyQuery &SQ) {
236 const Value *LHS = LHSCache.getValue();
237 const Value *RHS = RHSCache.getValue();
238
239 assert(LHS->getType() == RHS->getType() &&
240 "LHS and RHS should have the same type");
241 assert(LHS->getType()->isIntOrIntVectorTy() &&
242 "LHS and RHS should be integers");
243
244 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
246 return true;
247
249 RHSCache.getKnownBits(SQ));
250}
251
253 return !I->user_empty() &&
254 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
255}
256
258 return !I->user_empty() && all_of(I->users(), [](const User *U) {
259 CmpPredicate P;
260 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
261 });
262}
263
265 bool OrZero, AssumptionCache *AC,
266 const Instruction *CxtI,
267 const DominatorTree *DT, bool UseInstrInfo,
268 unsigned Depth) {
269 return ::isKnownToBeAPowerOfTwo(
270 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
271 Depth);
272}
273
274static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
275 const SimplifyQuery &Q, unsigned Depth);
276
278 unsigned Depth) {
279 return computeKnownBits(V, SQ, Depth).isNonNegative();
280}
281
283 unsigned Depth) {
284 if (auto *CI = dyn_cast<ConstantInt>(V))
285 return CI->getValue().isStrictlyPositive();
286
287 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
288 // this updated.
289 KnownBits Known = computeKnownBits(V, SQ, Depth);
290 return Known.isNonNegative() &&
291 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
292}
293
295 unsigned Depth) {
296 return computeKnownBits(V, SQ, Depth).isNegative();
297}
298
299static bool isKnownNonEqual(const Value *V1, const Value *V2,
300 const APInt &DemandedElts, const SimplifyQuery &Q,
301 unsigned Depth);
302
303bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
304 const SimplifyQuery &Q, unsigned Depth) {
305 // We don't support looking through casts.
306 if (V1 == V2 || V1->getType() != V2->getType())
307 return false;
308 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
309 APInt DemandedElts =
310 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
311 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
312}
313
314bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
315 const SimplifyQuery &SQ, unsigned Depth) {
316 KnownBits Known(Mask.getBitWidth());
317 computeKnownBits(V, Known, SQ, Depth);
318 return Mask.isSubsetOf(Known.Zero);
319}
320
321static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
322 const SimplifyQuery &Q, unsigned Depth);
323
324static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
325 unsigned Depth = 0) {
326 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
327 APInt DemandedElts =
328 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
329 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
330}
331
332unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
333 AssumptionCache *AC, const Instruction *CxtI,
334 const DominatorTree *DT, bool UseInstrInfo,
335 unsigned Depth) {
336 return ::ComputeNumSignBits(
337 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
338}
339
341 AssumptionCache *AC,
342 const Instruction *CxtI,
343 const DominatorTree *DT,
344 unsigned Depth) {
345 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
346 return V->getType()->getScalarSizeInBits() - SignBits + 1;
347}
348
349static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
350 bool NSW, bool NUW,
351 const APInt &DemandedElts,
352 KnownBits &KnownOut, KnownBits &Known2,
353 const SimplifyQuery &Q, unsigned Depth) {
354 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
355
356 // If one operand is unknown and we have no nowrap information,
357 // the result will be unknown independently of the second operand.
358 if (KnownOut.isUnknown() && !NSW && !NUW)
359 return;
360
361 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
362 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
363
364 if (!Add && NSW && !KnownOut.isNonNegative() &&
366 .value_or(false))
367 KnownOut.makeNonNegative();
368}
369
370static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
371 bool NUW, const APInt &DemandedElts,
372 KnownBits &Known, KnownBits &Known2,
373 const SimplifyQuery &Q, unsigned Depth) {
374 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
375 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
376
377 bool isKnownNegative = false;
378 bool isKnownNonNegative = false;
379 // If the multiplication is known not to overflow, compute the sign bit.
380 if (NSW) {
381 if (Op0 == Op1) {
382 // The product of a number with itself is non-negative.
383 isKnownNonNegative = true;
384 } else {
385 bool isKnownNonNegativeOp1 = Known.isNonNegative();
386 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
387 bool isKnownNegativeOp1 = Known.isNegative();
388 bool isKnownNegativeOp0 = Known2.isNegative();
389 // The product of two numbers with the same sign is non-negative.
390 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
391 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
392 if (!isKnownNonNegative && NUW) {
393 // mul nuw nsw with a factor > 1 is non-negative.
395 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
396 KnownBits::sgt(Known2, One).value_or(false);
397 }
398
399 // The product of a negative number and a non-negative number is either
400 // negative or zero.
403 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
404 Known2.isNonZero()) ||
405 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
406 }
407 }
408
409 bool SelfMultiply = Op0 == Op1;
410 if (SelfMultiply)
411 SelfMultiply &=
412 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
413 Known = KnownBits::mul(Known, Known2, SelfMultiply);
414
415 if (SelfMultiply) {
416 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
417 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
418 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
419
420 if (OutValidBits < TyBits) {
421 APInt KnownZeroMask =
422 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
423 Known.Zero |= KnownZeroMask;
424 }
425 }
426
427 // Only make use of no-wrap flags if we failed to compute the sign bit
428 // directly. This matters if the multiplication always overflows, in
429 // which case we prefer to follow the result of the direct computation,
430 // though as the program is invoking undefined behaviour we can choose
431 // whatever we like here.
432 if (isKnownNonNegative && !Known.isNegative())
433 Known.makeNonNegative();
434 else if (isKnownNegative && !Known.isNonNegative())
435 Known.makeNegative();
436}
437
439 KnownBits &Known) {
440 unsigned BitWidth = Known.getBitWidth();
441 unsigned NumRanges = Ranges.getNumOperands() / 2;
442 assert(NumRanges >= 1);
443
444 Known.Zero.setAllBits();
445 Known.One.setAllBits();
446
447 for (unsigned i = 0; i < NumRanges; ++i) {
449 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
451 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
452 ConstantRange Range(Lower->getValue(), Upper->getValue());
453 // BitWidth must equal the Ranges BitWidth for the correct number of high
454 // bits to be set.
455 assert(BitWidth == Range.getBitWidth() &&
456 "Known bit width must match range bit width!");
457
458 // The first CommonPrefixBits of all values in Range are equal.
459 unsigned CommonPrefixBits =
460 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
461 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
462 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
463 Known.One &= UnsignedMax & Mask;
464 Known.Zero &= ~UnsignedMax & Mask;
465 }
466}
467
468static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
472
473 // The instruction defining an assumption's condition itself is always
474 // considered ephemeral to that assumption (even if it has other
475 // non-ephemeral users). See r246696's test case for an example.
476 if (is_contained(I->operands(), E))
477 return true;
478
479 while (!WorkSet.empty()) {
480 const Instruction *V = WorkSet.pop_back_val();
481 if (!Visited.insert(V).second)
482 continue;
483
484 // If all uses of this value are ephemeral, then so is this value.
485 if (all_of(V->users(), [&](const User *U) {
486 return EphValues.count(cast<Instruction>(U));
487 })) {
488 if (V == E)
489 return true;
490
491 if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {
492 EphValues.insert(V);
493
494 if (const User *U = dyn_cast<User>(V)) {
495 for (const Use &U : U->operands()) {
496 if (const auto *I = dyn_cast<Instruction>(U.get()))
497 WorkSet.push_back(I);
498 }
499 }
500 }
501 }
502 }
503
504 return false;
505}
506
507// Is this an intrinsic that cannot be speculated but also cannot trap?
509 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
510 return CI->isAssumeLikeIntrinsic();
511
512 return false;
513}
514
516 const Instruction *CxtI,
517 const DominatorTree *DT,
518 bool AllowEphemerals) {
519 // There are two restrictions on the use of an assume:
520 // 1. The assume must dominate the context (or the control flow must
521 // reach the assume whenever it reaches the context).
522 // 2. The context must not be in the assume's set of ephemeral values
523 // (otherwise we will use the assume to prove that the condition
524 // feeding the assume is trivially true, thus causing the removal of
525 // the assume).
526
527 if (Inv->getParent() == CxtI->getParent()) {
528 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
529 // in the BB.
530 if (Inv->comesBefore(CxtI))
531 return true;
532
533 // Don't let an assume affect itself - this would cause the problems
534 // `isEphemeralValueOf` is trying to prevent, and it would also make
535 // the loop below go out of bounds.
536 if (!AllowEphemerals && Inv == CxtI)
537 return false;
538
539 // The context comes first, but they're both in the same block.
540 // Make sure there is nothing in between that might interrupt
541 // the control flow, not even CxtI itself.
542 // We limit the scan distance between the assume and its context instruction
543 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
544 // it can be adjusted if needed (could be turned into a cl::opt).
545 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
547 return false;
548
549 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
550 }
551
552 // Inv and CxtI are in different blocks.
553 if (DT) {
554 if (DT->dominates(Inv, CxtI))
555 return true;
556 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
557 Inv->getParent()->isEntryBlock()) {
558 // We don't have a DT, but this trivially dominates.
559 return true;
560 }
561
562 return false;
563}
564
565// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
566// we still have enough information about `RHS` to conclude non-zero. For
567// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
568// so the extra compile time may not be worth it, but possibly a second API
569// should be created for use outside of loops.
570static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
571 // v u> y implies v != 0.
572 if (Pred == ICmpInst::ICMP_UGT)
573 return true;
574
575 // Special-case v != 0 to also handle v != null.
576 if (Pred == ICmpInst::ICMP_NE)
577 return match(RHS, m_Zero());
578
579 // All other predicates - rely on generic ConstantRange handling.
580 const APInt *C;
581 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
582 if (match(RHS, m_APInt(C))) {
584 return !TrueValues.contains(Zero);
585 }
586
588 if (VC == nullptr)
589 return false;
590
591 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
592 ++ElemIdx) {
594 Pred, VC->getElementAsAPInt(ElemIdx));
595 if (TrueValues.contains(Zero))
596 return false;
597 }
598 return true;
599}
600
601static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
602 Value *&ValOut, Instruction *&CtxIOut,
603 const PHINode **PhiOut = nullptr) {
604 ValOut = U->get();
605 if (ValOut == PHI)
606 return;
607 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
608 if (PhiOut)
609 *PhiOut = PHI;
610 Value *V;
611 // If the Use is a select of this phi, compute analysis on other arm to break
612 // recursion.
613 // TODO: Min/Max
614 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
615 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
616 ValOut = V;
617
618 // Same for select, if this phi is 2-operand phi, compute analysis on other
619 // incoming value to break recursion.
620 // TODO: We could handle any number of incoming edges as long as we only have
621 // two unique values.
622 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
623 IncPhi && IncPhi->getNumIncomingValues() == 2) {
624 for (int Idx = 0; Idx < 2; ++Idx) {
625 if (IncPhi->getIncomingValue(Idx) == PHI) {
626 ValOut = IncPhi->getIncomingValue(1 - Idx);
627 if (PhiOut)
628 *PhiOut = IncPhi;
629 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
630 break;
631 }
632 }
633 }
634}
635
636static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
637 // Use of assumptions is context-sensitive. If we don't have a context, we
638 // cannot use them!
639 if (!Q.AC || !Q.CxtI)
640 return false;
641
642 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
643 if (!Elem.Assume)
644 continue;
645
646 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
647 assert(I->getFunction() == Q.CxtI->getFunction() &&
648 "Got assumption for the wrong function!");
649
650 if (Elem.Index != AssumptionCache::ExprResultIdx) {
651 if (!V->getType()->isPointerTy())
652 continue;
654 *I, I->bundle_op_info_begin()[Elem.Index])) {
655 if (RK.WasOn == V &&
656 (RK.AttrKind == Attribute::NonNull ||
657 (RK.AttrKind == Attribute::Dereferenceable &&
659 V->getType()->getPointerAddressSpace()))) &&
661 return true;
662 }
663 continue;
664 }
665
666 // Warning: This loop can end up being somewhat performance sensitive.
667 // We're running this loop for once for each value queried resulting in a
668 // runtime of ~O(#assumes * #values).
669
670 Value *RHS;
671 CmpPredicate Pred;
672 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
673 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
674 continue;
675
676 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
677 return true;
678 }
679
680 return false;
681}
682
684 Value *LHS, Value *RHS, KnownBits &Known,
685 const SimplifyQuery &Q) {
686 if (RHS->getType()->isPointerTy()) {
687 // Handle comparison of pointer to null explicitly, as it will not be
688 // covered by the m_APInt() logic below.
689 if (LHS == V && match(RHS, m_Zero())) {
690 switch (Pred) {
692 Known.setAllZero();
693 break;
696 Known.makeNonNegative();
697 break;
699 Known.makeNegative();
700 break;
701 default:
702 break;
703 }
704 }
705 return;
706 }
707
708 unsigned BitWidth = Known.getBitWidth();
709 auto m_V =
711
712 Value *Y;
713 const APInt *Mask, *C;
714 if (!match(RHS, m_APInt(C)))
715 return;
716
717 uint64_t ShAmt;
718 switch (Pred) {
720 // assume(V = C)
721 if (match(LHS, m_V)) {
722 Known = Known.unionWith(KnownBits::makeConstant(*C));
723 // assume(V & Mask = C)
724 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
725 // For one bits in Mask, we can propagate bits from C to V.
726 Known.One |= *C;
727 if (match(Y, m_APInt(Mask)))
728 Known.Zero |= ~*C & *Mask;
729 // assume(V | Mask = C)
730 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
731 // For zero bits in Mask, we can propagate bits from C to V.
732 Known.Zero |= ~*C;
733 if (match(Y, m_APInt(Mask)))
734 Known.One |= *C & ~*Mask;
735 // assume(V << ShAmt = C)
736 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
737 ShAmt < BitWidth) {
738 // For those bits in C that are known, we can propagate them to known
739 // bits in V shifted to the right by ShAmt.
741 RHSKnown >>= ShAmt;
742 Known = Known.unionWith(RHSKnown);
743 // assume(V >> ShAmt = C)
744 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
745 ShAmt < BitWidth) {
746 // For those bits in RHS that are known, we can propagate them to known
747 // bits in V shifted to the right by C.
749 RHSKnown <<= ShAmt;
750 Known = Known.unionWith(RHSKnown);
751 }
752 break;
753 case ICmpInst::ICMP_NE: {
754 // assume (V & B != 0) where B is a power of 2
755 const APInt *BPow2;
756 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
757 Known.One |= *BPow2;
758 break;
759 }
760 default: {
761 const APInt *Offset = nullptr;
762 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
764 if (Offset)
765 LHSRange = LHSRange.sub(*Offset);
766 Known = Known.unionWith(LHSRange.toKnownBits());
767 }
768 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
769 // X & Y u> C -> X u> C && Y u> C
770 // X nuw- Y u> C -> X u> C
771 if (match(LHS, m_c_And(m_V, m_Value())) ||
772 match(LHS, m_NUWSub(m_V, m_Value())))
773 Known.One.setHighBits(
774 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
775 }
776 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
777 // X | Y u< C -> X u< C && Y u< C
778 // X nuw+ Y u< C -> X u< C && Y u< C
779 if (match(LHS, m_c_Or(m_V, m_Value())) ||
780 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
781 Known.Zero.setHighBits(
782 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
783 }
784 }
785 } break;
786 }
787}
788
789static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
790 KnownBits &Known,
791 const SimplifyQuery &SQ, bool Invert) {
793 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
794 Value *LHS = Cmp->getOperand(0);
795 Value *RHS = Cmp->getOperand(1);
796
797 // Handle icmp pred (trunc V), C
798 if (match(LHS, m_Trunc(m_Specific(V)))) {
799 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
800 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
802 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
803 else
804 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
805 return;
806 }
807
808 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
809}
810
812 KnownBits &Known, const SimplifyQuery &SQ,
813 bool Invert, unsigned Depth) {
814 Value *A, *B;
817 KnownBits Known2(Known.getBitWidth());
818 KnownBits Known3(Known.getBitWidth());
819 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
820 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
821 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
823 Known2 = Known2.unionWith(Known3);
824 else
825 Known2 = Known2.intersectWith(Known3);
826 Known = Known.unionWith(Known2);
827 return;
828 }
829
830 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
831 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
832 return;
833 }
834
835 if (match(Cond, m_Trunc(m_Specific(V)))) {
836 KnownBits DstKnown(1);
837 if (Invert) {
838 DstKnown.setAllZero();
839 } else {
840 DstKnown.setAllOnes();
841 }
843 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
844 return;
845 }
846 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
847 return;
848 }
849
851 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
852}
853
855 const SimplifyQuery &Q, unsigned Depth) {
856 // Handle injected condition.
857 if (Q.CC && Q.CC->AffectedValues.contains(V))
858 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
859
860 if (!Q.CxtI)
861 return;
862
863 if (Q.DC && Q.DT) {
864 // Handle dominating conditions.
865 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
866 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
867 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
868 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
869 /*Invert*/ false, Depth);
870
871 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
872 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
873 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
874 /*Invert*/ true, Depth);
875 }
876
877 if (Known.hasConflict())
878 Known.resetAll();
879 }
880
881 if (!Q.AC)
882 return;
883
884 unsigned BitWidth = Known.getBitWidth();
885
886 // Note that the patterns below need to be kept in sync with the code
887 // in AssumptionCache::updateAffectedValues.
888
889 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
890 if (!Elem.Assume)
891 continue;
892
893 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
894 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
895 "Got assumption for the wrong function!");
896
897 if (Elem.Index != AssumptionCache::ExprResultIdx) {
898 if (!V->getType()->isPointerTy())
899 continue;
901 *I, I->bundle_op_info_begin()[Elem.Index])) {
902 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
903 // be the producer of the pointer in the bundle. At the moment, align
904 // assumptions aren't optimized away.
905 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
906 isPowerOf2_64(RK.ArgValue) &&
907 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
908 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
909 }
910 continue;
911 }
912
913 // Warning: This loop can end up being somewhat performance sensitive.
914 // We're running this loop for once for each value queried resulting in a
915 // runtime of ~O(#assumes * #values).
916
917 Value *Arg = I->getArgOperand(0);
918
919 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
920 assert(BitWidth == 1 && "assume operand is not i1?");
921 (void)BitWidth;
922 Known.setAllOnes();
923 return;
924 }
925 if (match(Arg, m_Not(m_Specific(V))) &&
927 assert(BitWidth == 1 && "assume operand is not i1?");
928 (void)BitWidth;
929 Known.setAllZero();
930 return;
931 }
932 auto *Trunc = dyn_cast<TruncInst>(Arg);
933 if (Trunc && Trunc->getOperand(0) == V &&
935 if (Trunc->hasNoUnsignedWrap()) {
937 return;
938 }
939 Known.One.setBit(0);
940 return;
941 }
942
943 // The remaining tests are all recursive, so bail out if we hit the limit.
945 continue;
946
947 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
948 if (!Cmp)
949 continue;
950
951 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
952 continue;
953
954 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
955 }
956
957 // Conflicting assumption: Undefined behavior will occur on this execution
958 // path.
959 if (Known.hasConflict())
960 Known.resetAll();
961}
962
963/// Compute known bits from a shift operator, including those with a
964/// non-constant shift amount. Known is the output of this function. Known2 is a
965/// pre-allocated temporary with the same bit width as Known and on return
966/// contains the known bit of the shift value source. KF is an
967/// operator-specific function that, given the known-bits and a shift amount,
968/// compute the implied known-bits of the shift operator's result respectively
969/// for that shift amount. The results from calling KF are conservatively
970/// combined for all permitted shift amounts.
972 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
973 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
974 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
975 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
976 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
977 // To limit compile-time impact, only query isKnownNonZero() if we know at
978 // least something about the shift amount.
979 bool ShAmtNonZero =
980 Known.isNonZero() ||
981 (Known.getMaxValue().ult(Known.getBitWidth()) &&
982 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
983 Known = KF(Known2, Known, ShAmtNonZero);
984}
985
986static KnownBits
987getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
988 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
989 const SimplifyQuery &Q, unsigned Depth) {
990 unsigned BitWidth = KnownLHS.getBitWidth();
991 KnownBits KnownOut(BitWidth);
992 bool IsAnd = false;
993 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
994 Value *X = nullptr, *Y = nullptr;
995
996 switch (I->getOpcode()) {
997 case Instruction::And:
998 KnownOut = KnownLHS & KnownRHS;
999 IsAnd = true;
1000 // and(x, -x) is common idioms that will clear all but lowest set
1001 // bit. If we have a single known bit in x, we can clear all bits
1002 // above it.
1003 // TODO: instcombine often reassociates independent `and` which can hide
1004 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1005 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1006 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1007 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1008 KnownOut = KnownLHS.blsi();
1009 else
1010 KnownOut = KnownRHS.blsi();
1011 }
1012 break;
1013 case Instruction::Or:
1014 KnownOut = KnownLHS | KnownRHS;
1015 break;
1016 case Instruction::Xor:
1017 KnownOut = KnownLHS ^ KnownRHS;
1018 // xor(x, x-1) is common idioms that will clear all but lowest set
1019 // bit. If we have a single known bit in x, we can clear all bits
1020 // above it.
1021 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1022 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1023 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1024 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1025 if (HasKnownOne &&
1027 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1028 KnownOut = XBits.blsmsk();
1029 }
1030 break;
1031 default:
1032 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1033 }
1034
1035 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1036 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1037 // here we handle the more general case of adding any odd number by
1038 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1039 // TODO: This could be generalized to clearing any bit set in y where the
1040 // following bit is known to be unset in y.
1041 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1045 KnownBits KnownY(BitWidth);
1046 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1047 if (KnownY.countMinTrailingOnes() > 0) {
1048 if (IsAnd)
1049 KnownOut.Zero.setBit(0);
1050 else
1051 KnownOut.One.setBit(0);
1052 }
1053 }
1054 return KnownOut;
1055}
1056
1058 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1059 unsigned Depth,
1060 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1061 KnownBitsFunc) {
1062 APInt DemandedEltsLHS, DemandedEltsRHS;
1064 DemandedElts, DemandedEltsLHS,
1065 DemandedEltsRHS);
1066
1067 const auto ComputeForSingleOpFunc =
1068 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1069 return KnownBitsFunc(
1070 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1071 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1072 };
1073
1074 if (DemandedEltsRHS.isZero())
1075 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1076 if (DemandedEltsLHS.isZero())
1077 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1078
1079 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1080 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1081}
1082
1083// Public so this can be used in `SimplifyDemandedUseBits`.
1085 const KnownBits &KnownLHS,
1086 const KnownBits &KnownRHS,
1087 const SimplifyQuery &SQ,
1088 unsigned Depth) {
1089 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1090 APInt DemandedElts =
1091 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1092
1093 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1094 Depth);
1095}
1096
1098 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1099 // Without vscale_range, we only know that vscale is non-zero.
1100 if (!Attr.isValid())
1102
1103 unsigned AttrMin = Attr.getVScaleRangeMin();
1104 // Minimum is larger than vscale width, result is always poison.
1105 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1106 return ConstantRange::getEmpty(BitWidth);
1107
1108 APInt Min(BitWidth, AttrMin);
1109 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1110 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1112
1113 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1114}
1115
1117 Value *Arm, bool Invert,
1118 const SimplifyQuery &Q, unsigned Depth) {
1119 // If we have a constant arm, we are done.
1120 if (Known.isConstant())
1121 return;
1122
1123 // See what condition implies about the bits of the select arm.
1124 KnownBits CondRes(Known.getBitWidth());
1125 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1126 // If we don't get any information from the condition, no reason to
1127 // proceed.
1128 if (CondRes.isUnknown())
1129 return;
1130
1131 // We can have conflict if the condition is dead. I.e if we have
1132 // (x | 64) < 32 ? (x | 64) : y
1133 // we will have conflict at bit 6 from the condition/the `or`.
1134 // In that case just return. Its not particularly important
1135 // what we do, as this select is going to be simplified soon.
1136 CondRes = CondRes.unionWith(Known);
1137 if (CondRes.hasConflict())
1138 return;
1139
1140 // Finally make sure the information we found is valid. This is relatively
1141 // expensive so it's left for the very end.
1142 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1143 return;
1144
1145 // Finally, we know we get information from the condition and its valid,
1146 // so return it.
1147 Known = CondRes;
1148}
1149
1150// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1151// Returns the input and lower/upper bounds.
1152static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1153 const APInt *&CLow, const APInt *&CHigh) {
1155 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1156 "Input should be a Select!");
1157
1158 const Value *LHS = nullptr, *RHS = nullptr;
1160 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1161 return false;
1162
1163 if (!match(RHS, m_APInt(CLow)))
1164 return false;
1165
1166 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1168 if (getInverseMinMaxFlavor(SPF) != SPF2)
1169 return false;
1170
1171 if (!match(RHS2, m_APInt(CHigh)))
1172 return false;
1173
1174 if (SPF == SPF_SMIN)
1175 std::swap(CLow, CHigh);
1176
1177 In = LHS2;
1178 return CLow->sle(*CHigh);
1179}
1180
1182 const APInt *&CLow,
1183 const APInt *&CHigh) {
1184 assert((II->getIntrinsicID() == Intrinsic::smin ||
1185 II->getIntrinsicID() == Intrinsic::smax) &&
1186 "Must be smin/smax");
1187
1188 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1189 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1190 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1191 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1192 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1193 return false;
1194
1195 if (II->getIntrinsicID() == Intrinsic::smin)
1196 std::swap(CLow, CHigh);
1197 return CLow->sle(*CHigh);
1198}
1199
1201 KnownBits &Known) {
1202 const APInt *CLow, *CHigh;
1203 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1204 Known = Known.unionWith(
1205 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1206}
1207
1209 const APInt &DemandedElts,
1210 KnownBits &Known,
1211 const SimplifyQuery &Q,
1212 unsigned Depth) {
1213 unsigned BitWidth = Known.getBitWidth();
1214
1215 KnownBits Known2(BitWidth);
1216 switch (I->getOpcode()) {
1217 default: break;
1218 case Instruction::Load:
1219 if (MDNode *MD =
1220 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1222 break;
1223 case Instruction::And:
1224 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1225 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1226
1227 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1228 break;
1229 case Instruction::Or:
1230 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1231 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1232
1233 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1234 break;
1235 case Instruction::Xor:
1236 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1237 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1238
1239 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1240 break;
1241 case Instruction::Mul: {
1244 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1245 DemandedElts, Known, Known2, Q, Depth);
1246 break;
1247 }
1248 case Instruction::UDiv: {
1249 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1250 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1251 Known =
1252 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1253 break;
1254 }
1255 case Instruction::SDiv: {
1256 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1257 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1258 Known =
1259 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1260 break;
1261 }
1262 case Instruction::Select: {
1263 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1264 KnownBits Res(Known.getBitWidth());
1265 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1266 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1267 return Res;
1268 };
1269 // Only known if known in both the LHS and RHS.
1270 Known =
1271 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1272 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1273 break;
1274 }
1275 case Instruction::FPTrunc:
1276 case Instruction::FPExt:
1277 case Instruction::FPToUI:
1278 case Instruction::FPToSI:
1279 case Instruction::SIToFP:
1280 case Instruction::UIToFP:
1281 break; // Can't work with floating point.
1282 case Instruction::PtrToInt:
1283 case Instruction::IntToPtr:
1284 // Fall through and handle them the same as zext/trunc.
1285 [[fallthrough]];
1286 case Instruction::ZExt:
1287 case Instruction::Trunc: {
1288 Type *SrcTy = I->getOperand(0)->getType();
1289
1290 unsigned SrcBitWidth;
1291 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1292 // which fall through here.
1293 Type *ScalarTy = SrcTy->getScalarType();
1294 SrcBitWidth = ScalarTy->isPointerTy() ?
1295 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1296 Q.DL.getTypeSizeInBits(ScalarTy);
1297
1298 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1299 Known = Known.anyextOrTrunc(SrcBitWidth);
1300 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1301 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1302 Inst && Inst->hasNonNeg() && !Known.isNegative())
1303 Known.makeNonNegative();
1304 Known = Known.zextOrTrunc(BitWidth);
1305 break;
1306 }
1307 case Instruction::BitCast: {
1308 Type *SrcTy = I->getOperand(0)->getType();
1309 if (SrcTy->isIntOrPtrTy() &&
1310 // TODO: For now, not handling conversions like:
1311 // (bitcast i64 %x to <2 x i32>)
1312 !I->getType()->isVectorTy()) {
1313 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1314 break;
1315 }
1316
1317 const Value *V;
1318 // Handle bitcast from floating point to integer.
1319 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1320 V->getType()->isFPOrFPVectorTy()) {
1321 Type *FPType = V->getType()->getScalarType();
1322 KnownFPClass Result =
1323 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1324 FPClassTest FPClasses = Result.KnownFPClasses;
1325
1326 // TODO: Treat it as zero/poison if the use of I is unreachable.
1327 if (FPClasses == fcNone)
1328 break;
1329
1330 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1331 Known.Zero.setAllBits();
1332 Known.One.setAllBits();
1333
1334 if (FPClasses & fcInf)
1336 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1337
1338 if (FPClasses & fcZero)
1340 APInt::getZero(FPType->getScalarSizeInBits())));
1341
1342 Known.Zero.clearSignBit();
1343 Known.One.clearSignBit();
1344 }
1345
1346 if (Result.SignBit) {
1347 if (*Result.SignBit)
1348 Known.makeNegative();
1349 else
1350 Known.makeNonNegative();
1351 }
1352
1353 break;
1354 }
1355
1356 // Handle cast from vector integer type to scalar or vector integer.
1357 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1358 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1359 !I->getType()->isIntOrIntVectorTy() ||
1360 isa<ScalableVectorType>(I->getType()))
1361 break;
1362
1363 unsigned NumElts = DemandedElts.getBitWidth();
1364 bool IsLE = Q.DL.isLittleEndian();
1365 // Look through a cast from narrow vector elements to wider type.
1366 // Examples: v4i32 -> v2i64, v3i8 -> v24
1367 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1368 if (BitWidth % SubBitWidth == 0) {
1369 // Known bits are automatically intersected across demanded elements of a
1370 // vector. So for example, if a bit is computed as known zero, it must be
1371 // zero across all demanded elements of the vector.
1372 //
1373 // For this bitcast, each demanded element of the output is sub-divided
1374 // across a set of smaller vector elements in the source vector. To get
1375 // the known bits for an entire element of the output, compute the known
1376 // bits for each sub-element sequentially. This is done by shifting the
1377 // one-set-bit demanded elements parameter across the sub-elements for
1378 // consecutive calls to computeKnownBits. We are using the demanded
1379 // elements parameter as a mask operator.
1380 //
1381 // The known bits of each sub-element are then inserted into place
1382 // (dependent on endian) to form the full result of known bits.
1383 unsigned SubScale = BitWidth / SubBitWidth;
1384 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1385 for (unsigned i = 0; i != NumElts; ++i) {
1386 if (DemandedElts[i])
1387 SubDemandedElts.setBit(i * SubScale);
1388 }
1389
1390 KnownBits KnownSrc(SubBitWidth);
1391 for (unsigned i = 0; i != SubScale; ++i) {
1392 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1393 Depth + 1);
1394 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1395 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1396 }
1397 }
1398 // Look through a cast from wider vector elements to narrow type.
1399 // Examples: v2i64 -> v4i32
1400 if (SubBitWidth % BitWidth == 0) {
1401 unsigned SubScale = SubBitWidth / BitWidth;
1402 KnownBits KnownSrc(SubBitWidth);
1403 APInt SubDemandedElts =
1404 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1405 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1406 Depth + 1);
1407
1408 Known.Zero.setAllBits();
1409 Known.One.setAllBits();
1410 for (unsigned i = 0; i != NumElts; ++i) {
1411 if (DemandedElts[i]) {
1412 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1413 unsigned Offset = (Shifts % SubScale) * BitWidth;
1414 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1415 if (Known.isUnknown())
1416 break;
1417 }
1418 }
1419 }
1420 break;
1421 }
1422 case Instruction::SExt: {
1423 // Compute the bits in the result that are not present in the input.
1424 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1425
1426 Known = Known.trunc(SrcBitWidth);
1427 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1428 // If the sign bit of the input is known set or clear, then we know the
1429 // top bits of the result.
1430 Known = Known.sext(BitWidth);
1431 break;
1432 }
1433 case Instruction::Shl: {
1436 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1437 bool ShAmtNonZero) {
1438 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1439 };
1440 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1441 KF);
1442 // Trailing zeros of a right-shifted constant never decrease.
1443 const APInt *C;
1444 if (match(I->getOperand(0), m_APInt(C)))
1445 Known.Zero.setLowBits(C->countr_zero());
1446 break;
1447 }
1448 case Instruction::LShr: {
1449 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1450 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1451 bool ShAmtNonZero) {
1452 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1453 };
1454 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1455 KF);
1456 // Leading zeros of a left-shifted constant never decrease.
1457 const APInt *C;
1458 if (match(I->getOperand(0), m_APInt(C)))
1459 Known.Zero.setHighBits(C->countl_zero());
1460 break;
1461 }
1462 case Instruction::AShr: {
1463 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1464 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1465 bool ShAmtNonZero) {
1466 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1467 };
1468 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1469 KF);
1470 break;
1471 }
1472 case Instruction::Sub: {
1475 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1476 DemandedElts, Known, Known2, Q, Depth);
1477 break;
1478 }
1479 case Instruction::Add: {
1482 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1483 DemandedElts, Known, Known2, Q, Depth);
1484 break;
1485 }
1486 case Instruction::SRem:
1487 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1488 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1489 Known = KnownBits::srem(Known, Known2);
1490 break;
1491
1492 case Instruction::URem:
1493 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1494 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1495 Known = KnownBits::urem(Known, Known2);
1496 break;
1497 case Instruction::Alloca:
1499 break;
1500 case Instruction::GetElementPtr: {
1501 // Analyze all of the subscripts of this getelementptr instruction
1502 // to determine if we can prove known low zero bits.
1503 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1504 // Accumulate the constant indices in a separate variable
1505 // to minimize the number of calls to computeForAddSub.
1506 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1507 APInt AccConstIndices(IndexWidth, 0);
1508
1509 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1510 if (IndexWidth == BitWidth) {
1511 // Note that inbounds does *not* guarantee nsw for the addition, as only
1512 // the offset is signed, while the base address is unsigned.
1513 Known = KnownBits::add(Known, IndexBits);
1514 } else {
1515 // If the index width is smaller than the pointer width, only add the
1516 // value to the low bits.
1517 assert(IndexWidth < BitWidth &&
1518 "Index width can't be larger than pointer width");
1519 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1520 }
1521 };
1522
1524 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1525 // TrailZ can only become smaller, short-circuit if we hit zero.
1526 if (Known.isUnknown())
1527 break;
1528
1529 Value *Index = I->getOperand(i);
1530
1531 // Handle case when index is zero.
1532 Constant *CIndex = dyn_cast<Constant>(Index);
1533 if (CIndex && CIndex->isZeroValue())
1534 continue;
1535
1536 if (StructType *STy = GTI.getStructTypeOrNull()) {
1537 // Handle struct member offset arithmetic.
1538
1539 assert(CIndex &&
1540 "Access to structure field must be known at compile time");
1541
1542 if (CIndex->getType()->isVectorTy())
1543 Index = CIndex->getSplatValue();
1544
1545 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1546 const StructLayout *SL = Q.DL.getStructLayout(STy);
1547 uint64_t Offset = SL->getElementOffset(Idx);
1548 AccConstIndices += Offset;
1549 continue;
1550 }
1551
1552 // Handle array index arithmetic.
1553 Type *IndexedTy = GTI.getIndexedType();
1554 if (!IndexedTy->isSized()) {
1555 Known.resetAll();
1556 break;
1557 }
1558
1559 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1560 uint64_t StrideInBytes = Stride.getKnownMinValue();
1561 if (!Stride.isScalable()) {
1562 // Fast path for constant offset.
1563 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1564 AccConstIndices +=
1565 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1566 continue;
1567 }
1568 }
1569
1570 KnownBits IndexBits =
1571 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1572 KnownBits ScalingFactor(IndexWidth);
1573 // Multiply by current sizeof type.
1574 // &A[i] == A + i * sizeof(*A[i]).
1575 if (Stride.isScalable()) {
1576 // For scalable types the only thing we know about sizeof is
1577 // that this is a multiple of the minimum size.
1578 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1579 } else {
1580 ScalingFactor =
1581 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1582 }
1583 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1584 }
1585 if (!Known.isUnknown() && !AccConstIndices.isZero())
1586 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1587 break;
1588 }
1589 case Instruction::PHI: {
1590 const PHINode *P = cast<PHINode>(I);
1591 BinaryOperator *BO = nullptr;
1592 Value *R = nullptr, *L = nullptr;
1593 if (matchSimpleRecurrence(P, BO, R, L)) {
1594 // Handle the case of a simple two-predecessor recurrence PHI.
1595 // There's a lot more that could theoretically be done here, but
1596 // this is sufficient to catch some interesting cases.
1597 unsigned Opcode = BO->getOpcode();
1598
1599 switch (Opcode) {
1600 // If this is a shift recurrence, we know the bits being shifted in. We
1601 // can combine that with information about the start value of the
1602 // recurrence to conclude facts about the result. If this is a udiv
1603 // recurrence, we know that the result can never exceed either the
1604 // numerator or the start value, whichever is greater.
1605 case Instruction::LShr:
1606 case Instruction::AShr:
1607 case Instruction::Shl:
1608 case Instruction::UDiv:
1609 if (BO->getOperand(0) != I)
1610 break;
1611 [[fallthrough]];
1612
1613 // For a urem recurrence, the result can never exceed the start value. The
1614 // phi could either be the numerator or the denominator.
1615 case Instruction::URem: {
1616 // We have matched a recurrence of the form:
1617 // %iv = [R, %entry], [%iv.next, %backedge]
1618 // %iv.next = shift_op %iv, L
1619
1620 // Recurse with the phi context to avoid concern about whether facts
1621 // inferred hold at original context instruction. TODO: It may be
1622 // correct to use the original context. IF warranted, explore and
1623 // add sufficient tests to cover.
1625 RecQ.CxtI = P;
1626 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1627 switch (Opcode) {
1628 case Instruction::Shl:
1629 // A shl recurrence will only increase the tailing zeros
1630 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1631 break;
1632 case Instruction::LShr:
1633 case Instruction::UDiv:
1634 case Instruction::URem:
1635 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1636 // the start value.
1637 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1638 break;
1639 case Instruction::AShr:
1640 // An ashr recurrence will extend the initial sign bit
1641 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1642 Known.One.setHighBits(Known2.countMinLeadingOnes());
1643 break;
1644 }
1645 break;
1646 }
1647
1648 // Check for operations that have the property that if
1649 // both their operands have low zero bits, the result
1650 // will have low zero bits.
1651 case Instruction::Add:
1652 case Instruction::Sub:
1653 case Instruction::And:
1654 case Instruction::Or:
1655 case Instruction::Mul: {
1656 // Change the context instruction to the "edge" that flows into the
1657 // phi. This is important because that is where the value is actually
1658 // "evaluated" even though it is used later somewhere else. (see also
1659 // D69571).
1661
1662 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1663 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1664 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1665
1666 // Ok, we have a PHI of the form L op= R. Check for low
1667 // zero bits.
1668 RecQ.CxtI = RInst;
1669 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1670
1671 // We need to take the minimum number of known bits
1672 KnownBits Known3(BitWidth);
1673 RecQ.CxtI = LInst;
1674 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1675
1676 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1677 Known3.countMinTrailingZeros()));
1678
1679 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1680 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1681 break;
1682
1683 switch (Opcode) {
1684 // If initial value of recurrence is nonnegative, and we are adding
1685 // a nonnegative number with nsw, the result can only be nonnegative
1686 // or poison value regardless of the number of times we execute the
1687 // add in phi recurrence. If initial value is negative and we are
1688 // adding a negative number with nsw, the result can only be
1689 // negative or poison value. Similar arguments apply to sub and mul.
1690 //
1691 // (add non-negative, non-negative) --> non-negative
1692 // (add negative, negative) --> negative
1693 case Instruction::Add: {
1694 if (Known2.isNonNegative() && Known3.isNonNegative())
1695 Known.makeNonNegative();
1696 else if (Known2.isNegative() && Known3.isNegative())
1697 Known.makeNegative();
1698 break;
1699 }
1700
1701 // (sub nsw non-negative, negative) --> non-negative
1702 // (sub nsw negative, non-negative) --> negative
1703 case Instruction::Sub: {
1704 if (BO->getOperand(0) != I)
1705 break;
1706 if (Known2.isNonNegative() && Known3.isNegative())
1707 Known.makeNonNegative();
1708 else if (Known2.isNegative() && Known3.isNonNegative())
1709 Known.makeNegative();
1710 break;
1711 }
1712
1713 // (mul nsw non-negative, non-negative) --> non-negative
1714 case Instruction::Mul:
1715 if (Known2.isNonNegative() && Known3.isNonNegative())
1716 Known.makeNonNegative();
1717 break;
1718
1719 default:
1720 break;
1721 }
1722 break;
1723 }
1724
1725 default:
1726 break;
1727 }
1728 }
1729
1730 // Unreachable blocks may have zero-operand PHI nodes.
1731 if (P->getNumIncomingValues() == 0)
1732 break;
1733
1734 // Otherwise take the unions of the known bit sets of the operands,
1735 // taking conservative care to avoid excessive recursion.
1736 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1737 // Skip if every incoming value references to ourself.
1738 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1739 break;
1740
1741 Known.Zero.setAllBits();
1742 Known.One.setAllBits();
1743 for (const Use &U : P->operands()) {
1744 Value *IncValue;
1745 const PHINode *CxtPhi;
1746 Instruction *CxtI;
1747 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1748 // Skip direct self references.
1749 if (IncValue == P)
1750 continue;
1751
1752 // Change the context instruction to the "edge" that flows into the
1753 // phi. This is important because that is where the value is actually
1754 // "evaluated" even though it is used later somewhere else. (see also
1755 // D69571).
1757
1758 Known2 = KnownBits(BitWidth);
1759
1760 // Recurse, but cap the recursion to one level, because we don't
1761 // want to waste time spinning around in loops.
1762 // TODO: See if we can base recursion limiter on number of incoming phi
1763 // edges so we don't overly clamp analysis.
1764 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1766
1767 // See if we can further use a conditional branch into the phi
1768 // to help us determine the range of the value.
1769 if (!Known2.isConstant()) {
1770 CmpPredicate Pred;
1771 const APInt *RHSC;
1772 BasicBlock *TrueSucc, *FalseSucc;
1773 // TODO: Use RHS Value and compute range from its known bits.
1774 if (match(RecQ.CxtI,
1775 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1776 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1777 // Check for cases of duplicate successors.
1778 if ((TrueSucc == CxtPhi->getParent()) !=
1779 (FalseSucc == CxtPhi->getParent())) {
1780 // If we're using the false successor, invert the predicate.
1781 if (FalseSucc == CxtPhi->getParent())
1782 Pred = CmpInst::getInversePredicate(Pred);
1783 // Get the knownbits implied by the incoming phi condition.
1784 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1785 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1786 // We can have conflicts here if we are analyzing deadcode (its
1787 // impossible for us reach this BB based the icmp).
1788 if (KnownUnion.hasConflict()) {
1789 // No reason to continue analyzing in a known dead region, so
1790 // just resetAll and break. This will cause us to also exit the
1791 // outer loop.
1792 Known.resetAll();
1793 break;
1794 }
1795 Known2 = KnownUnion;
1796 }
1797 }
1798 }
1799
1800 Known = Known.intersectWith(Known2);
1801 // If all bits have been ruled out, there's no need to check
1802 // more operands.
1803 if (Known.isUnknown())
1804 break;
1805 }
1806 }
1807 break;
1808 }
1809 case Instruction::Call:
1810 case Instruction::Invoke: {
1811 // If range metadata is attached to this call, set known bits from that,
1812 // and then intersect with known bits based on other properties of the
1813 // function.
1814 if (MDNode *MD =
1815 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1817
1818 const auto *CB = cast<CallBase>(I);
1819
1820 if (std::optional<ConstantRange> Range = CB->getRange())
1821 Known = Known.unionWith(Range->toKnownBits());
1822
1823 if (const Value *RV = CB->getReturnedArgOperand()) {
1824 if (RV->getType() == I->getType()) {
1825 computeKnownBits(RV, Known2, Q, Depth + 1);
1826 Known = Known.unionWith(Known2);
1827 // If the function doesn't return properly for all input values
1828 // (e.g. unreachable exits) then there might be conflicts between the
1829 // argument value and the range metadata. Simply discard the known bits
1830 // in case of conflicts.
1831 if (Known.hasConflict())
1832 Known.resetAll();
1833 }
1834 }
1835 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1836 switch (II->getIntrinsicID()) {
1837 default:
1838 break;
1839 case Intrinsic::abs: {
1840 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1841 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
1842 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
1843 break;
1844 }
1845 case Intrinsic::bitreverse:
1846 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1847 Known = Known.unionWith(Known2.reverseBits());
1848 break;
1849 case Intrinsic::bswap:
1850 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1851 Known = Known.unionWith(Known2.byteSwap());
1852 break;
1853 case Intrinsic::ctlz: {
1854 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1855 // If we have a known 1, its position is our upper bound.
1856 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
1857 // If this call is poison for 0 input, the result will be less than 2^n.
1858 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1859 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1860 unsigned LowBits = llvm::bit_width(PossibleLZ);
1861 Known.Zero.setBitsFrom(LowBits);
1862 break;
1863 }
1864 case Intrinsic::cttz: {
1865 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1866 // If we have a known 1, its position is our upper bound.
1867 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
1868 // If this call is poison for 0 input, the result will be less than 2^n.
1869 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1870 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1871 unsigned LowBits = llvm::bit_width(PossibleTZ);
1872 Known.Zero.setBitsFrom(LowBits);
1873 break;
1874 }
1875 case Intrinsic::ctpop: {
1876 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1877 // We can bound the space the count needs. Also, bits known to be zero
1878 // can't contribute to the population.
1879 unsigned BitsPossiblySet = Known2.countMaxPopulation();
1880 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
1881 Known.Zero.setBitsFrom(LowBits);
1882 // TODO: we could bound KnownOne using the lower bound on the number
1883 // of bits which might be set provided by popcnt KnownOne2.
1884 break;
1885 }
1886 case Intrinsic::fshr:
1887 case Intrinsic::fshl: {
1888 const APInt *SA;
1889 if (!match(I->getOperand(2), m_APInt(SA)))
1890 break;
1891
1892 // Normalize to funnel shift left.
1893 uint64_t ShiftAmt = SA->urem(BitWidth);
1894 if (II->getIntrinsicID() == Intrinsic::fshr)
1895 ShiftAmt = BitWidth - ShiftAmt;
1896
1897 KnownBits Known3(BitWidth);
1898 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1899 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
1900
1901 Known2 <<= ShiftAmt;
1902 Known3 >>= BitWidth - ShiftAmt;
1903 Known = Known2.unionWith(Known3);
1904 break;
1905 }
1906 case Intrinsic::uadd_sat:
1907 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1908 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1909 Known = KnownBits::uadd_sat(Known, Known2);
1910 break;
1911 case Intrinsic::usub_sat:
1912 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1913 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1914 Known = KnownBits::usub_sat(Known, Known2);
1915 break;
1916 case Intrinsic::sadd_sat:
1917 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1918 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1919 Known = KnownBits::sadd_sat(Known, Known2);
1920 break;
1921 case Intrinsic::ssub_sat:
1922 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1923 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1924 Known = KnownBits::ssub_sat(Known, Known2);
1925 break;
1926 // Vec reverse preserves bits from input vec.
1927 case Intrinsic::vector_reverse:
1928 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
1929 Depth + 1);
1930 break;
1931 // for min/max/and/or reduce, any bit common to each element in the
1932 // input vec is set in the output.
1933 case Intrinsic::vector_reduce_and:
1934 case Intrinsic::vector_reduce_or:
1935 case Intrinsic::vector_reduce_umax:
1936 case Intrinsic::vector_reduce_umin:
1937 case Intrinsic::vector_reduce_smax:
1938 case Intrinsic::vector_reduce_smin:
1939 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1940 break;
1941 case Intrinsic::vector_reduce_xor: {
1942 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1943 // The zeros common to all vecs are zero in the output.
1944 // If the number of elements is odd, then the common ones remain. If the
1945 // number of elements is even, then the common ones becomes zeros.
1946 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
1947 // Even, so the ones become zeros.
1948 bool EvenCnt = VecTy->getElementCount().isKnownEven();
1949 if (EvenCnt)
1950 Known.Zero |= Known.One;
1951 // Maybe even element count so need to clear ones.
1952 if (VecTy->isScalableTy() || EvenCnt)
1953 Known.One.clearAllBits();
1954 break;
1955 }
1956 case Intrinsic::umin:
1957 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1958 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1959 Known = KnownBits::umin(Known, Known2);
1960 break;
1961 case Intrinsic::umax:
1962 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1963 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1964 Known = KnownBits::umax(Known, Known2);
1965 break;
1966 case Intrinsic::smin:
1967 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1968 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1969 Known = KnownBits::smin(Known, Known2);
1971 break;
1972 case Intrinsic::smax:
1973 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1974 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1975 Known = KnownBits::smax(Known, Known2);
1977 break;
1978 case Intrinsic::ptrmask: {
1979 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1980
1981 const Value *Mask = I->getOperand(1);
1982 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
1983 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
1984 // TODO: 1-extend would be more precise.
1985 Known &= Known2.anyextOrTrunc(BitWidth);
1986 break;
1987 }
1988 case Intrinsic::x86_sse2_pmulh_w:
1989 case Intrinsic::x86_avx2_pmulh_w:
1990 case Intrinsic::x86_avx512_pmulh_w_512:
1991 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1992 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1993 Known = KnownBits::mulhs(Known, Known2);
1994 break;
1995 case Intrinsic::x86_sse2_pmulhu_w:
1996 case Intrinsic::x86_avx2_pmulhu_w:
1997 case Intrinsic::x86_avx512_pmulhu_w_512:
1998 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1999 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2000 Known = KnownBits::mulhu(Known, Known2);
2001 break;
2002 case Intrinsic::x86_sse42_crc32_64_64:
2003 Known.Zero.setBitsFrom(32);
2004 break;
2005 case Intrinsic::x86_ssse3_phadd_d_128:
2006 case Intrinsic::x86_ssse3_phadd_w_128:
2007 case Intrinsic::x86_avx2_phadd_d:
2008 case Intrinsic::x86_avx2_phadd_w: {
2010 I, DemandedElts, Q, Depth,
2011 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2012 return KnownBits::add(KnownLHS, KnownRHS);
2013 });
2014 break;
2015 }
2016 case Intrinsic::x86_ssse3_phadd_sw_128:
2017 case Intrinsic::x86_avx2_phadd_sw: {
2019 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2020 break;
2021 }
2022 case Intrinsic::x86_ssse3_phsub_d_128:
2023 case Intrinsic::x86_ssse3_phsub_w_128:
2024 case Intrinsic::x86_avx2_phsub_d:
2025 case Intrinsic::x86_avx2_phsub_w: {
2027 I, DemandedElts, Q, Depth,
2028 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2029 return KnownBits::sub(KnownLHS, KnownRHS);
2030 });
2031 break;
2032 }
2033 case Intrinsic::x86_ssse3_phsub_sw_128:
2034 case Intrinsic::x86_avx2_phsub_sw: {
2036 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2037 break;
2038 }
2039 case Intrinsic::riscv_vsetvli:
2040 case Intrinsic::riscv_vsetvlimax: {
2041 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2042 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2044 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2045 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2046 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2047 uint64_t MaxVLEN =
2048 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2049 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2050
2051 // Result of vsetvli must be not larger than AVL.
2052 if (HasAVL)
2053 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2054 MaxVL = std::min(MaxVL, CI->getZExtValue());
2055
2056 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2057 if (BitWidth > KnownZeroFirstBit)
2058 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2059 break;
2060 }
2061 case Intrinsic::vscale: {
2062 if (!II->getParent() || !II->getFunction())
2063 break;
2064
2065 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2066 break;
2067 }
2068 }
2069 }
2070 break;
2071 }
2072 case Instruction::ShuffleVector: {
2073 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2074 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2075 if (!Shuf) {
2076 Known.resetAll();
2077 return;
2078 }
2079 // For undef elements, we don't know anything about the common state of
2080 // the shuffle result.
2081 APInt DemandedLHS, DemandedRHS;
2082 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2083 Known.resetAll();
2084 return;
2085 }
2086 Known.One.setAllBits();
2087 Known.Zero.setAllBits();
2088 if (!!DemandedLHS) {
2089 const Value *LHS = Shuf->getOperand(0);
2090 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2091 // If we don't know any bits, early out.
2092 if (Known.isUnknown())
2093 break;
2094 }
2095 if (!!DemandedRHS) {
2096 const Value *RHS = Shuf->getOperand(1);
2097 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2098 Known = Known.intersectWith(Known2);
2099 }
2100 break;
2101 }
2102 case Instruction::InsertElement: {
2103 if (isa<ScalableVectorType>(I->getType())) {
2104 Known.resetAll();
2105 return;
2106 }
2107 const Value *Vec = I->getOperand(0);
2108 const Value *Elt = I->getOperand(1);
2109 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2110 unsigned NumElts = DemandedElts.getBitWidth();
2111 APInt DemandedVecElts = DemandedElts;
2112 bool NeedsElt = true;
2113 // If we know the index we are inserting too, clear it from Vec check.
2114 if (CIdx && CIdx->getValue().ult(NumElts)) {
2115 DemandedVecElts.clearBit(CIdx->getZExtValue());
2116 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2117 }
2118
2119 Known.One.setAllBits();
2120 Known.Zero.setAllBits();
2121 if (NeedsElt) {
2122 computeKnownBits(Elt, Known, Q, Depth + 1);
2123 // If we don't know any bits, early out.
2124 if (Known.isUnknown())
2125 break;
2126 }
2127
2128 if (!DemandedVecElts.isZero()) {
2129 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2130 Known = Known.intersectWith(Known2);
2131 }
2132 break;
2133 }
2134 case Instruction::ExtractElement: {
2135 // Look through extract element. If the index is non-constant or
2136 // out-of-range demand all elements, otherwise just the extracted element.
2137 const Value *Vec = I->getOperand(0);
2138 const Value *Idx = I->getOperand(1);
2139 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2140 if (isa<ScalableVectorType>(Vec->getType())) {
2141 // FIXME: there's probably *something* we can do with scalable vectors
2142 Known.resetAll();
2143 break;
2144 }
2145 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2146 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2147 if (CIdx && CIdx->getValue().ult(NumElts))
2148 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2149 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2150 break;
2151 }
2152 case Instruction::ExtractValue:
2153 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2155 if (EVI->getNumIndices() != 1) break;
2156 if (EVI->getIndices()[0] == 0) {
2157 switch (II->getIntrinsicID()) {
2158 default: break;
2159 case Intrinsic::uadd_with_overflow:
2160 case Intrinsic::sadd_with_overflow:
2162 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2163 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2164 break;
2165 case Intrinsic::usub_with_overflow:
2166 case Intrinsic::ssub_with_overflow:
2168 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2169 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2170 break;
2171 case Intrinsic::umul_with_overflow:
2172 case Intrinsic::smul_with_overflow:
2173 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2174 false, DemandedElts, Known, Known2, Q, Depth);
2175 break;
2176 }
2177 }
2178 }
2179 break;
2180 case Instruction::Freeze:
2181 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2182 Depth + 1))
2183 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2184 break;
2185 }
2186}
2187
2188/// Determine which bits of V are known to be either zero or one and return
2189/// them.
2190KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2191 const SimplifyQuery &Q, unsigned Depth) {
2192 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2193 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2194 return Known;
2195}
2196
2197/// Determine which bits of V are known to be either zero or one and return
2198/// them.
2200 unsigned Depth) {
2201 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2202 computeKnownBits(V, Known, Q, Depth);
2203 return Known;
2204}
2205
2206/// Determine which bits of V are known to be either zero or one and return
2207/// them in the Known bit set.
2208///
2209/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2210/// we cannot optimize based on the assumption that it is zero without changing
2211/// it to be an explicit zero. If we don't change it to zero, other code could
2212/// optimized based on the contradictory assumption that it is non-zero.
2213/// Because instcombine aggressively folds operations with undef args anyway,
2214/// this won't lose us code quality.
2215///
2216/// This function is defined on values with integer type, values with pointer
2217/// type, and vectors of integers. In the case
2218/// where V is a vector, known zero, and known one values are the
2219/// same width as the vector element, and the bit is set only if it is true
2220/// for all of the demanded elements in the vector specified by DemandedElts.
2221void computeKnownBits(const Value *V, const APInt &DemandedElts,
2222 KnownBits &Known, const SimplifyQuery &Q,
2223 unsigned Depth) {
2224 if (!DemandedElts) {
2225 // No demanded elts, better to assume we don't know anything.
2226 Known.resetAll();
2227 return;
2228 }
2229
2230 assert(V && "No Value?");
2231 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2232
2233#ifndef NDEBUG
2234 Type *Ty = V->getType();
2235 unsigned BitWidth = Known.getBitWidth();
2236
2237 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2238 "Not integer or pointer type!");
2239
2240 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2241 assert(
2242 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2243 "DemandedElt width should equal the fixed vector number of elements");
2244 } else {
2245 assert(DemandedElts == APInt(1, 1) &&
2246 "DemandedElt width should be 1 for scalars or scalable vectors");
2247 }
2248
2249 Type *ScalarTy = Ty->getScalarType();
2250 if (ScalarTy->isPointerTy()) {
2251 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2252 "V and Known should have same BitWidth");
2253 } else {
2254 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2255 "V and Known should have same BitWidth");
2256 }
2257#endif
2258
2259 const APInt *C;
2260 if (match(V, m_APInt(C))) {
2261 // We know all of the bits for a scalar constant or a splat vector constant!
2262 Known = KnownBits::makeConstant(*C);
2263 return;
2264 }
2265 // Null and aggregate-zero are all-zeros.
2267 Known.setAllZero();
2268 return;
2269 }
2270 // Handle a constant vector by taking the intersection of the known bits of
2271 // each element.
2273 assert(!isa<ScalableVectorType>(V->getType()));
2274 // We know that CDV must be a vector of integers. Take the intersection of
2275 // each element.
2276 Known.Zero.setAllBits(); Known.One.setAllBits();
2277 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2278 if (!DemandedElts[i])
2279 continue;
2280 APInt Elt = CDV->getElementAsAPInt(i);
2281 Known.Zero &= ~Elt;
2282 Known.One &= Elt;
2283 }
2284 if (Known.hasConflict())
2285 Known.resetAll();
2286 return;
2287 }
2288
2289 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2290 assert(!isa<ScalableVectorType>(V->getType()));
2291 // We know that CV must be a vector of integers. Take the intersection of
2292 // each element.
2293 Known.Zero.setAllBits(); Known.One.setAllBits();
2294 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2295 if (!DemandedElts[i])
2296 continue;
2297 Constant *Element = CV->getAggregateElement(i);
2298 if (isa<PoisonValue>(Element))
2299 continue;
2300 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2301 if (!ElementCI) {
2302 Known.resetAll();
2303 return;
2304 }
2305 const APInt &Elt = ElementCI->getValue();
2306 Known.Zero &= ~Elt;
2307 Known.One &= Elt;
2308 }
2309 if (Known.hasConflict())
2310 Known.resetAll();
2311 return;
2312 }
2313
2314 // Start out not knowing anything.
2315 Known.resetAll();
2316
2317 // We can't imply anything about undefs.
2318 if (isa<UndefValue>(V))
2319 return;
2320
2321 // There's no point in looking through other users of ConstantData for
2322 // assumptions. Confirm that we've handled them all.
2323 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2324
2325 if (const auto *A = dyn_cast<Argument>(V))
2326 if (std::optional<ConstantRange> Range = A->getRange())
2327 Known = Range->toKnownBits();
2328
2329 // All recursive calls that increase depth must come after this.
2331 return;
2332
2333 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2334 // the bits of its aliasee.
2335 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2336 if (!GA->isInterposable())
2337 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2338 return;
2339 }
2340
2341 if (const Operator *I = dyn_cast<Operator>(V))
2342 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2343 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2344 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2345 Known = CR->toKnownBits();
2346 }
2347
2348 // Aligned pointers have trailing zeros - refine Known.Zero set
2349 if (isa<PointerType>(V->getType())) {
2350 Align Alignment = V->getPointerAlignment(Q.DL);
2351 Known.Zero.setLowBits(Log2(Alignment));
2352 }
2353
2354 // computeKnownBitsFromContext strictly refines Known.
2355 // Therefore, we run them after computeKnownBitsFromOperator.
2356
2357 // Check whether we can determine known bits from context such as assumes.
2358 computeKnownBitsFromContext(V, Known, Q, Depth);
2359}
2360
2361/// Try to detect a recurrence that the value of the induction variable is
2362/// always a power of two (or zero).
2363static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2364 SimplifyQuery &Q, unsigned Depth) {
2365 BinaryOperator *BO = nullptr;
2366 Value *Start = nullptr, *Step = nullptr;
2367 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2368 return false;
2369
2370 // Initial value must be a power of two.
2371 for (const Use &U : PN->operands()) {
2372 if (U.get() == Start) {
2373 // Initial value comes from a different BB, need to adjust context
2374 // instruction for analysis.
2375 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2376 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2377 return false;
2378 }
2379 }
2380
2381 // Except for Mul, the induction variable must be on the left side of the
2382 // increment expression, otherwise its value can be arbitrary.
2383 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2384 return false;
2385
2386 Q.CxtI = BO->getParent()->getTerminator();
2387 switch (BO->getOpcode()) {
2388 case Instruction::Mul:
2389 // Power of two is closed under multiplication.
2390 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2391 Q.IIQ.hasNoSignedWrap(BO)) &&
2392 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2393 case Instruction::SDiv:
2394 // Start value must not be signmask for signed division, so simply being a
2395 // power of two is not sufficient, and it has to be a constant.
2396 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2397 return false;
2398 [[fallthrough]];
2399 case Instruction::UDiv:
2400 // Divisor must be a power of two.
2401 // If OrZero is false, cannot guarantee induction variable is non-zero after
2402 // division, same for Shr, unless it is exact division.
2403 return (OrZero || Q.IIQ.isExact(BO)) &&
2404 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2405 case Instruction::Shl:
2406 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2407 case Instruction::AShr:
2408 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2409 return false;
2410 [[fallthrough]];
2411 case Instruction::LShr:
2412 return OrZero || Q.IIQ.isExact(BO);
2413 default:
2414 return false;
2415 }
2416}
2417
2418/// Return true if we can infer that \p V is known to be a power of 2 from
2419/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2420static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2421 const Value *Cond,
2422 bool CondIsTrue) {
2423 CmpPredicate Pred;
2424 const APInt *RHSC;
2426 m_APInt(RHSC))))
2427 return false;
2428 if (!CondIsTrue)
2429 Pred = ICmpInst::getInversePredicate(Pred);
2430 // ctpop(V) u< 2
2431 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2432 return true;
2433 // ctpop(V) == 1
2434 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2435}
2436
2437/// Return true if the given value is known to have exactly one
2438/// bit set when defined. For vectors return true if every element is known to
2439/// be a power of two when defined. Supports values with integer or pointer
2440/// types and vectors of integers.
2441bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2442 const SimplifyQuery &Q, unsigned Depth) {
2443 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2444
2445 if (isa<Constant>(V))
2446 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2447
2448 // i1 is by definition a power of 2 or zero.
2449 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2450 return true;
2451
2452 // Try to infer from assumptions.
2453 if (Q.AC && Q.CxtI) {
2454 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2455 if (!AssumeVH)
2456 continue;
2457 CallInst *I = cast<CallInst>(AssumeVH);
2458 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2459 /*CondIsTrue=*/true) &&
2461 return true;
2462 }
2463 }
2464
2465 // Handle dominating conditions.
2466 if (Q.DC && Q.CxtI && Q.DT) {
2467 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2468 Value *Cond = BI->getCondition();
2469
2470 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2472 /*CondIsTrue=*/true) &&
2473 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2474 return true;
2475
2476 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2478 /*CondIsTrue=*/false) &&
2479 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2480 return true;
2481 }
2482 }
2483
2484 auto *I = dyn_cast<Instruction>(V);
2485 if (!I)
2486 return false;
2487
2488 if (Q.CxtI && match(V, m_VScale())) {
2489 const Function *F = Q.CxtI->getFunction();
2490 // The vscale_range indicates vscale is a power-of-two.
2491 return F->hasFnAttribute(Attribute::VScaleRange);
2492 }
2493
2494 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2495 // it is shifted off the end then the result is undefined.
2496 if (match(I, m_Shl(m_One(), m_Value())))
2497 return true;
2498
2499 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2500 // the bottom. If it is shifted off the bottom then the result is undefined.
2501 if (match(I, m_LShr(m_SignMask(), m_Value())))
2502 return true;
2503
2504 // The remaining tests are all recursive, so bail out if we hit the limit.
2506 return false;
2507
2508 switch (I->getOpcode()) {
2509 case Instruction::ZExt:
2510 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2511 case Instruction::Trunc:
2512 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2513 case Instruction::Shl:
2514 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2515 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2516 return false;
2517 case Instruction::LShr:
2518 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2519 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2520 return false;
2521 case Instruction::UDiv:
2523 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2524 return false;
2525 case Instruction::Mul:
2526 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2527 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2528 (OrZero || isKnownNonZero(I, Q, Depth));
2529 case Instruction::And:
2530 // A power of two and'd with anything is a power of two or zero.
2531 if (OrZero &&
2532 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2533 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2534 return true;
2535 // X & (-X) is always a power of two or zero.
2536 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2537 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2538 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2539 return false;
2540 case Instruction::Add: {
2541 // Adding a power-of-two or zero to the same power-of-two or zero yields
2542 // either the original power-of-two, a larger power-of-two or zero.
2544 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2545 Q.IIQ.hasNoSignedWrap(VOBO)) {
2546 if (match(I->getOperand(0),
2547 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2548 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2549 return true;
2550 if (match(I->getOperand(1),
2551 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2552 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2553 return true;
2554
2555 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2556 KnownBits LHSBits(BitWidth);
2557 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2558
2559 KnownBits RHSBits(BitWidth);
2560 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2561 // If i8 V is a power of two or zero:
2562 // ZeroBits: 1 1 1 0 1 1 1 1
2563 // ~ZeroBits: 0 0 0 1 0 0 0 0
2564 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2565 // If OrZero isn't set, we cannot give back a zero result.
2566 // Make sure either the LHS or RHS has a bit set.
2567 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2568 return true;
2569 }
2570
2571 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2572 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2573 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2574 return true;
2575 return false;
2576 }
2577 case Instruction::Select:
2578 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2579 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2580 case Instruction::PHI: {
2581 // A PHI node is power of two if all incoming values are power of two, or if
2582 // it is an induction variable where in each step its value is a power of
2583 // two.
2584 auto *PN = cast<PHINode>(I);
2586
2587 // Check if it is an induction variable and always power of two.
2588 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2589 return true;
2590
2591 // Recursively check all incoming values. Limit recursion to 2 levels, so
2592 // that search complexity is limited to number of operands^2.
2593 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2594 return llvm::all_of(PN->operands(), [&](const Use &U) {
2595 // Value is power of 2 if it is coming from PHI node itself by induction.
2596 if (U.get() == PN)
2597 return true;
2598
2599 // Change the context instruction to the incoming block where it is
2600 // evaluated.
2601 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2602 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2603 });
2604 }
2605 case Instruction::Invoke:
2606 case Instruction::Call: {
2607 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2608 switch (II->getIntrinsicID()) {
2609 case Intrinsic::umax:
2610 case Intrinsic::smax:
2611 case Intrinsic::umin:
2612 case Intrinsic::smin:
2613 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2614 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2615 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2616 // thus dont change pow2/non-pow2 status.
2617 case Intrinsic::bitreverse:
2618 case Intrinsic::bswap:
2619 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2620 case Intrinsic::fshr:
2621 case Intrinsic::fshl:
2622 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2623 if (II->getArgOperand(0) == II->getArgOperand(1))
2624 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2625 break;
2626 default:
2627 break;
2628 }
2629 }
2630 return false;
2631 }
2632 default:
2633 return false;
2634 }
2635}
2636
2637/// Test whether a GEP's result is known to be non-null.
2638///
2639/// Uses properties inherent in a GEP to try to determine whether it is known
2640/// to be non-null.
2641///
2642/// Currently this routine does not support vector GEPs.
2643static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2644 unsigned Depth) {
2645 const Function *F = nullptr;
2646 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2647 F = I->getFunction();
2648
2649 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2650 // may be null iff the base pointer is null and the offset is zero.
2651 if (!GEP->hasNoUnsignedWrap() &&
2652 !(GEP->isInBounds() &&
2653 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2654 return false;
2655
2656 // FIXME: Support vector-GEPs.
2657 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2658
2659 // If the base pointer is non-null, we cannot walk to a null address with an
2660 // inbounds GEP in address space zero.
2661 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2662 return true;
2663
2664 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2665 // If so, then the GEP cannot produce a null pointer, as doing so would
2666 // inherently violate the inbounds contract within address space zero.
2668 GTI != GTE; ++GTI) {
2669 // Struct types are easy -- they must always be indexed by a constant.
2670 if (StructType *STy = GTI.getStructTypeOrNull()) {
2671 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2672 unsigned ElementIdx = OpC->getZExtValue();
2673 const StructLayout *SL = Q.DL.getStructLayout(STy);
2674 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2675 if (ElementOffset > 0)
2676 return true;
2677 continue;
2678 }
2679
2680 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2681 if (GTI.getSequentialElementStride(Q.DL).isZero())
2682 continue;
2683
2684 // Fast path the constant operand case both for efficiency and so we don't
2685 // increment Depth when just zipping down an all-constant GEP.
2686 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2687 if (!OpC->isZero())
2688 return true;
2689 continue;
2690 }
2691
2692 // We post-increment Depth here because while isKnownNonZero increments it
2693 // as well, when we pop back up that increment won't persist. We don't want
2694 // to recurse 10k times just because we have 10k GEP operands. We don't
2695 // bail completely out because we want to handle constant GEPs regardless
2696 // of depth.
2698 continue;
2699
2700 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2701 return true;
2702 }
2703
2704 return false;
2705}
2706
2708 const Instruction *CtxI,
2709 const DominatorTree *DT) {
2710 assert(!isa<Constant>(V) && "Called for constant?");
2711
2712 if (!CtxI || !DT)
2713 return false;
2714
2715 unsigned NumUsesExplored = 0;
2716 for (auto &U : V->uses()) {
2717 // Avoid massive lists
2718 if (NumUsesExplored >= DomConditionsMaxUses)
2719 break;
2720 NumUsesExplored++;
2721
2722 const Instruction *UI = cast<Instruction>(U.getUser());
2723 // If the value is used as an argument to a call or invoke, then argument
2724 // attributes may provide an answer about null-ness.
2725 if (V->getType()->isPointerTy()) {
2726 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2727 if (CB->isArgOperand(&U) &&
2728 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2729 /*AllowUndefOrPoison=*/false) &&
2730 DT->dominates(CB, CtxI))
2731 return true;
2732 }
2733 }
2734
2735 // If the value is used as a load/store, then the pointer must be non null.
2736 if (V == getLoadStorePointerOperand(UI)) {
2739 DT->dominates(UI, CtxI))
2740 return true;
2741 }
2742
2743 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2744 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2745 isValidAssumeForContext(UI, CtxI, DT))
2746 return true;
2747
2748 // Consider only compare instructions uniquely controlling a branch
2749 Value *RHS;
2750 CmpPredicate Pred;
2751 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2752 continue;
2753
2754 bool NonNullIfTrue;
2755 if (cmpExcludesZero(Pred, RHS))
2756 NonNullIfTrue = true;
2758 NonNullIfTrue = false;
2759 else
2760 continue;
2761
2764 for (const auto *CmpU : UI->users()) {
2765 assert(WorkList.empty() && "Should be!");
2766 if (Visited.insert(CmpU).second)
2767 WorkList.push_back(CmpU);
2768
2769 while (!WorkList.empty()) {
2770 auto *Curr = WorkList.pop_back_val();
2771
2772 // If a user is an AND, add all its users to the work list. We only
2773 // propagate "pred != null" condition through AND because it is only
2774 // correct to assume that all conditions of AND are met in true branch.
2775 // TODO: Support similar logic of OR and EQ predicate?
2776 if (NonNullIfTrue)
2777 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2778 for (const auto *CurrU : Curr->users())
2779 if (Visited.insert(CurrU).second)
2780 WorkList.push_back(CurrU);
2781 continue;
2782 }
2783
2784 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2785 assert(BI->isConditional() && "uses a comparison!");
2786
2787 BasicBlock *NonNullSuccessor =
2788 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2789 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2790 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2791 return true;
2792 } else if (NonNullIfTrue && isGuard(Curr) &&
2793 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2794 return true;
2795 }
2796 }
2797 }
2798 }
2799
2800 return false;
2801}
2802
2803/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2804/// ensure that the value it's attached to is never Value? 'RangeType' is
2805/// is the type of the value described by the range.
2806static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2807 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2808 assert(NumRanges >= 1);
2809 for (unsigned i = 0; i < NumRanges; ++i) {
2811 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2813 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2814 ConstantRange Range(Lower->getValue(), Upper->getValue());
2815 if (Range.contains(Value))
2816 return false;
2817 }
2818 return true;
2819}
2820
2821/// Try to detect a recurrence that monotonically increases/decreases from a
2822/// non-zero starting value. These are common as induction variables.
2823static bool isNonZeroRecurrence(const PHINode *PN) {
2824 BinaryOperator *BO = nullptr;
2825 Value *Start = nullptr, *Step = nullptr;
2826 const APInt *StartC, *StepC;
2827 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2828 !match(Start, m_APInt(StartC)) || StartC->isZero())
2829 return false;
2830
2831 switch (BO->getOpcode()) {
2832 case Instruction::Add:
2833 // Starting from non-zero and stepping away from zero can never wrap back
2834 // to zero.
2835 return BO->hasNoUnsignedWrap() ||
2836 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
2837 StartC->isNegative() == StepC->isNegative());
2838 case Instruction::Mul:
2839 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2840 match(Step, m_APInt(StepC)) && !StepC->isZero();
2841 case Instruction::Shl:
2842 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
2843 case Instruction::AShr:
2844 case Instruction::LShr:
2845 return BO->isExact();
2846 default:
2847 return false;
2848 }
2849}
2850
2851static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
2853 m_Specific(Op1), m_Zero()))) ||
2855 m_Specific(Op0), m_Zero())));
2856}
2857
2858static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
2859 unsigned BitWidth, Value *X, Value *Y, bool NSW,
2860 bool NUW, unsigned Depth) {
2861 // (X + (X != 0)) is non zero
2862 if (matchOpWithOpEqZero(X, Y))
2863 return true;
2864
2865 if (NUW)
2866 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2867 isKnownNonZero(X, DemandedElts, Q, Depth);
2868
2869 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
2870 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
2871
2872 // If X and Y are both non-negative (as signed values) then their sum is not
2873 // zero unless both X and Y are zero.
2874 if (XKnown.isNonNegative() && YKnown.isNonNegative())
2875 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2876 isKnownNonZero(X, DemandedElts, Q, Depth))
2877 return true;
2878
2879 // If X and Y are both negative (as signed values) then their sum is not
2880 // zero unless both X and Y equal INT_MIN.
2881 if (XKnown.isNegative() && YKnown.isNegative()) {
2883 // The sign bit of X is set. If some other bit is set then X is not equal
2884 // to INT_MIN.
2885 if (XKnown.One.intersects(Mask))
2886 return true;
2887 // The sign bit of Y is set. If some other bit is set then Y is not equal
2888 // to INT_MIN.
2889 if (YKnown.One.intersects(Mask))
2890 return true;
2891 }
2892
2893 // The sum of a non-negative number and a power of two is not zero.
2894 if (XKnown.isNonNegative() &&
2895 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
2896 return true;
2897 if (YKnown.isNonNegative() &&
2898 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
2899 return true;
2900
2901 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
2902}
2903
2904static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
2905 unsigned BitWidth, Value *X, Value *Y,
2906 unsigned Depth) {
2907 // (X - (X != 0)) is non zero
2908 // ((X != 0) - X) is non zero
2909 if (matchOpWithOpEqZero(X, Y))
2910 return true;
2911
2912 // TODO: Move this case into isKnownNonEqual().
2913 if (auto *C = dyn_cast<Constant>(X))
2914 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
2915 return true;
2916
2917 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
2918}
2919
2920static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
2921 unsigned BitWidth, Value *X, Value *Y, bool NSW,
2922 bool NUW, unsigned Depth) {
2923 // If X and Y are non-zero then so is X * Y as long as the multiplication
2924 // does not overflow.
2925 if (NSW || NUW)
2926 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
2927 isKnownNonZero(Y, DemandedElts, Q, Depth);
2928
2929 // If either X or Y is odd, then if the other is non-zero the result can't
2930 // be zero.
2931 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
2932 if (XKnown.One[0])
2933 return isKnownNonZero(Y, DemandedElts, Q, Depth);
2934
2935 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
2936 if (YKnown.One[0])
2937 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
2938
2939 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
2940 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
2941 // the lowest known One of X and Y. If they are non-zero, the result
2942 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
2943 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
2944 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
2945 BitWidth;
2946}
2947
2948static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
2949 const SimplifyQuery &Q, const KnownBits &KnownVal,
2950 unsigned Depth) {
2951 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2952 switch (I->getOpcode()) {
2953 case Instruction::Shl:
2954 return Lhs.shl(Rhs);
2955 case Instruction::LShr:
2956 return Lhs.lshr(Rhs);
2957 case Instruction::AShr:
2958 return Lhs.ashr(Rhs);
2959 default:
2960 llvm_unreachable("Unknown Shift Opcode");
2961 }
2962 };
2963
2964 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2965 switch (I->getOpcode()) {
2966 case Instruction::Shl:
2967 return Lhs.lshr(Rhs);
2968 case Instruction::LShr:
2969 case Instruction::AShr:
2970 return Lhs.shl(Rhs);
2971 default:
2972 llvm_unreachable("Unknown Shift Opcode");
2973 }
2974 };
2975
2976 if (KnownVal.isUnknown())
2977 return false;
2978
2979 KnownBits KnownCnt =
2980 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
2981 APInt MaxShift = KnownCnt.getMaxValue();
2982 unsigned NumBits = KnownVal.getBitWidth();
2983 if (MaxShift.uge(NumBits))
2984 return false;
2985
2986 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
2987 return true;
2988
2989 // If all of the bits shifted out are known to be zero, and Val is known
2990 // non-zero then at least one non-zero bit must remain.
2991 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
2992 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
2993 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
2994 return true;
2995
2996 return false;
2997}
2998
3000 const APInt &DemandedElts,
3001 const SimplifyQuery &Q, unsigned Depth) {
3002 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3003 switch (I->getOpcode()) {
3004 case Instruction::Alloca:
3005 // Alloca never returns null, malloc might.
3006 return I->getType()->getPointerAddressSpace() == 0;
3007 case Instruction::GetElementPtr:
3008 if (I->getType()->isPointerTy())
3010 break;
3011 case Instruction::BitCast: {
3012 // We need to be a bit careful here. We can only peek through the bitcast
3013 // if the scalar size of elements in the operand are smaller than and a
3014 // multiple of the size they are casting too. Take three cases:
3015 //
3016 // 1) Unsafe:
3017 // bitcast <2 x i16> %NonZero to <4 x i8>
3018 //
3019 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3020 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3021 // guranteed (imagine just sign bit set in the 2 i16 elements).
3022 //
3023 // 2) Unsafe:
3024 // bitcast <4 x i3> %NonZero to <3 x i4>
3025 //
3026 // Even though the scalar size of the src (`i3`) is smaller than the
3027 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3028 // its possible for the `3 x i4` elements to be zero because there are
3029 // some elements in the destination that don't contain any full src
3030 // element.
3031 //
3032 // 3) Safe:
3033 // bitcast <4 x i8> %NonZero to <2 x i16>
3034 //
3035 // This is always safe as non-zero in the 4 i8 elements implies
3036 // non-zero in the combination of any two adjacent ones. Since i8 is a
3037 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3038 // This all implies the 2 i16 elements are non-zero.
3039 Type *FromTy = I->getOperand(0)->getType();
3040 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3041 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3042 return isKnownNonZero(I->getOperand(0), Q, Depth);
3043 } break;
3044 case Instruction::IntToPtr:
3045 // Note that we have to take special care to avoid looking through
3046 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3047 // as casts that can alter the value, e.g., AddrSpaceCasts.
3048 if (!isa<ScalableVectorType>(I->getType()) &&
3049 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3050 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3051 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3052 break;
3053 case Instruction::PtrToInt:
3054 // Similar to int2ptr above, we can look through ptr2int here if the cast
3055 // is a no-op or an extend and not a truncate.
3056 if (!isa<ScalableVectorType>(I->getType()) &&
3057 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3058 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3059 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3060 break;
3061 case Instruction::Trunc:
3062 // nuw/nsw trunc preserves zero/non-zero status of input.
3063 if (auto *TI = dyn_cast<TruncInst>(I))
3064 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3065 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3066 break;
3067
3068 // Iff x - y != 0, then x ^ y != 0
3069 // Therefore we can do the same exact checks
3070 case Instruction::Xor:
3071 case Instruction::Sub:
3072 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3073 I->getOperand(1), Depth);
3074 case Instruction::Or:
3075 // (X | (X != 0)) is non zero
3076 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3077 return true;
3078 // X | Y != 0 if X != Y.
3079 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3080 Depth))
3081 return true;
3082 // X | Y != 0 if X != 0 or Y != 0.
3083 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3084 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3085 case Instruction::SExt:
3086 case Instruction::ZExt:
3087 // ext X != 0 if X != 0.
3088 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3089
3090 case Instruction::Shl: {
3091 // shl nsw/nuw can't remove any non-zero bits.
3093 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3094 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3095
3096 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3097 // if the lowest bit is shifted off the end.
3098 KnownBits Known(BitWidth);
3099 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3100 if (Known.One[0])
3101 return true;
3102
3103 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3104 }
3105 case Instruction::LShr:
3106 case Instruction::AShr: {
3107 // shr exact can only shift out zero bits.
3109 if (BO->isExact())
3110 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3111
3112 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3113 // defined if the sign bit is shifted off the end.
3114 KnownBits Known =
3115 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3116 if (Known.isNegative())
3117 return true;
3118
3119 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3120 }
3121 case Instruction::UDiv:
3122 case Instruction::SDiv: {
3123 // X / Y
3124 // div exact can only produce a zero if the dividend is zero.
3125 if (cast<PossiblyExactOperator>(I)->isExact())
3126 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3127
3128 KnownBits XKnown =
3129 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3130 // If X is fully unknown we won't be able to figure anything out so don't
3131 // both computing knownbits for Y.
3132 if (XKnown.isUnknown())
3133 return false;
3134
3135 KnownBits YKnown =
3136 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3137 if (I->getOpcode() == Instruction::SDiv) {
3138 // For signed division need to compare abs value of the operands.
3139 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3140 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3141 }
3142 // If X u>= Y then div is non zero (0/0 is UB).
3143 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3144 // If X is total unknown or X u< Y we won't be able to prove non-zero
3145 // with compute known bits so just return early.
3146 return XUgeY && *XUgeY;
3147 }
3148 case Instruction::Add: {
3149 // X + Y.
3150
3151 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3152 // non-zero.
3154 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3155 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3156 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3157 }
3158 case Instruction::Mul: {
3160 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3161 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3162 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3163 }
3164 case Instruction::Select: {
3165 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3166
3167 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3168 // then see if the select condition implies the arm is non-zero. For example
3169 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3170 // dominated by `X != 0`.
3171 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3172 Value *Op;
3173 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3174 // Op is trivially non-zero.
3175 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3176 return true;
3177
3178 // The condition of the select dominates the true/false arm. Check if the
3179 // condition implies that a given arm is non-zero.
3180 Value *X;
3181 CmpPredicate Pred;
3182 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3183 return false;
3184
3185 if (!IsTrueArm)
3186 Pred = ICmpInst::getInversePredicate(Pred);
3187
3188 return cmpExcludesZero(Pred, X);
3189 };
3190
3191 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3192 SelectArmIsNonZero(/* IsTrueArm */ false))
3193 return true;
3194 break;
3195 }
3196 case Instruction::PHI: {
3197 auto *PN = cast<PHINode>(I);
3199 return true;
3200
3201 // Check if all incoming values are non-zero using recursion.
3203 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3204 return llvm::all_of(PN->operands(), [&](const Use &U) {
3205 if (U.get() == PN)
3206 return true;
3207 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3208 // Check if the branch on the phi excludes zero.
3209 CmpPredicate Pred;
3210 Value *X;
3211 BasicBlock *TrueSucc, *FalseSucc;
3212 if (match(RecQ.CxtI,
3213 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3214 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3215 // Check for cases of duplicate successors.
3216 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3217 // If we're using the false successor, invert the predicate.
3218 if (FalseSucc == PN->getParent())
3219 Pred = CmpInst::getInversePredicate(Pred);
3220 if (cmpExcludesZero(Pred, X))
3221 return true;
3222 }
3223 }
3224 // Finally recurse on the edge and check it directly.
3225 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3226 });
3227 }
3228 case Instruction::InsertElement: {
3229 if (isa<ScalableVectorType>(I->getType()))
3230 break;
3231
3232 const Value *Vec = I->getOperand(0);
3233 const Value *Elt = I->getOperand(1);
3234 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3235
3236 unsigned NumElts = DemandedElts.getBitWidth();
3237 APInt DemandedVecElts = DemandedElts;
3238 bool SkipElt = false;
3239 // If we know the index we are inserting too, clear it from Vec check.
3240 if (CIdx && CIdx->getValue().ult(NumElts)) {
3241 DemandedVecElts.clearBit(CIdx->getZExtValue());
3242 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3243 }
3244
3245 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3246 // are non-zero.
3247 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3248 (DemandedVecElts.isZero() ||
3249 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3250 }
3251 case Instruction::ExtractElement:
3252 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3253 const Value *Vec = EEI->getVectorOperand();
3254 const Value *Idx = EEI->getIndexOperand();
3255 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3256 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3257 unsigned NumElts = VecTy->getNumElements();
3258 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3259 if (CIdx && CIdx->getValue().ult(NumElts))
3260 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3261 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3262 }
3263 }
3264 break;
3265 case Instruction::ShuffleVector: {
3266 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3267 if (!Shuf)
3268 break;
3269 APInt DemandedLHS, DemandedRHS;
3270 // For undef elements, we don't know anything about the common state of
3271 // the shuffle result.
3272 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3273 break;
3274 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3275 return (DemandedRHS.isZero() ||
3276 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3277 (DemandedLHS.isZero() ||
3278 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3279 }
3280 case Instruction::Freeze:
3281 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3282 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3283 Depth);
3284 case Instruction::Load: {
3285 auto *LI = cast<LoadInst>(I);
3286 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3287 // is never null.
3288 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3289 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3290 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3291 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3292 return true;
3293 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3295 }
3296
3297 // No need to fall through to computeKnownBits as range metadata is already
3298 // handled in isKnownNonZero.
3299 return false;
3300 }
3301 case Instruction::ExtractValue: {
3302 const WithOverflowInst *WO;
3304 switch (WO->getBinaryOp()) {
3305 default:
3306 break;
3307 case Instruction::Add:
3308 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3309 WO->getArgOperand(1),
3310 /*NSW=*/false,
3311 /*NUW=*/false, Depth);
3312 case Instruction::Sub:
3313 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3314 WO->getArgOperand(1), Depth);
3315 case Instruction::Mul:
3316 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3317 WO->getArgOperand(1),
3318 /*NSW=*/false, /*NUW=*/false, Depth);
3319 break;
3320 }
3321 }
3322 break;
3323 }
3324 case Instruction::Call:
3325 case Instruction::Invoke: {
3326 const auto *Call = cast<CallBase>(I);
3327 if (I->getType()->isPointerTy()) {
3328 if (Call->isReturnNonNull())
3329 return true;
3330 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3331 return isKnownNonZero(RP, Q, Depth);
3332 } else {
3333 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3335 if (std::optional<ConstantRange> Range = Call->getRange()) {
3336 const APInt ZeroValue(Range->getBitWidth(), 0);
3337 if (!Range->contains(ZeroValue))
3338 return true;
3339 }
3340 if (const Value *RV = Call->getReturnedArgOperand())
3341 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3342 return true;
3343 }
3344
3345 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3346 switch (II->getIntrinsicID()) {
3347 case Intrinsic::sshl_sat:
3348 case Intrinsic::ushl_sat:
3349 case Intrinsic::abs:
3350 case Intrinsic::bitreverse:
3351 case Intrinsic::bswap:
3352 case Intrinsic::ctpop:
3353 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3354 // NB: We don't do usub_sat here as in any case we can prove its
3355 // non-zero, we will fold it to `sub nuw` in InstCombine.
3356 case Intrinsic::ssub_sat:
3357 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3358 II->getArgOperand(1), Depth);
3359 case Intrinsic::sadd_sat:
3360 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3361 II->getArgOperand(1),
3362 /*NSW=*/true, /* NUW=*/false, Depth);
3363 // Vec reverse preserves zero/non-zero status from input vec.
3364 case Intrinsic::vector_reverse:
3365 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3366 Q, Depth);
3367 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3368 case Intrinsic::vector_reduce_or:
3369 case Intrinsic::vector_reduce_umax:
3370 case Intrinsic::vector_reduce_umin:
3371 case Intrinsic::vector_reduce_smax:
3372 case Intrinsic::vector_reduce_smin:
3373 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3374 case Intrinsic::umax:
3375 case Intrinsic::uadd_sat:
3376 // umax(X, (X != 0)) is non zero
3377 // X +usat (X != 0) is non zero
3378 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3379 return true;
3380
3381 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3382 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3383 case Intrinsic::smax: {
3384 // If either arg is strictly positive the result is non-zero. Otherwise
3385 // the result is non-zero if both ops are non-zero.
3386 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3387 const KnownBits &OpKnown) {
3388 if (!OpNonZero.has_value())
3389 OpNonZero = OpKnown.isNonZero() ||
3390 isKnownNonZero(Op, DemandedElts, Q, Depth);
3391 return *OpNonZero;
3392 };
3393 // Avoid re-computing isKnownNonZero.
3394 std::optional<bool> Op0NonZero, Op1NonZero;
3395 KnownBits Op1Known =
3396 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3397 if (Op1Known.isNonNegative() &&
3398 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3399 return true;
3400 KnownBits Op0Known =
3401 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3402 if (Op0Known.isNonNegative() &&
3403 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3404 return true;
3405 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3406 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3407 }
3408 case Intrinsic::smin: {
3409 // If either arg is negative the result is non-zero. Otherwise
3410 // the result is non-zero if both ops are non-zero.
3411 KnownBits Op1Known =
3412 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3413 if (Op1Known.isNegative())
3414 return true;
3415 KnownBits Op0Known =
3416 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3417 if (Op0Known.isNegative())
3418 return true;
3419
3420 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3421 return true;
3422 }
3423 [[fallthrough]];
3424 case Intrinsic::umin:
3425 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3426 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3427 case Intrinsic::cttz:
3428 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3429 .Zero[0];
3430 case Intrinsic::ctlz:
3431 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3432 .isNonNegative();
3433 case Intrinsic::fshr:
3434 case Intrinsic::fshl:
3435 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3436 if (II->getArgOperand(0) == II->getArgOperand(1))
3437 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3438 break;
3439 case Intrinsic::vscale:
3440 return true;
3441 case Intrinsic::experimental_get_vector_length:
3442 return isKnownNonZero(I->getOperand(0), Q, Depth);
3443 default:
3444 break;
3445 }
3446 break;
3447 }
3448
3449 return false;
3450 }
3451 }
3452
3453 KnownBits Known(BitWidth);
3454 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3455 return Known.One != 0;
3456}
3457
3458/// Return true if the given value is known to be non-zero when defined. For
3459/// vectors, return true if every demanded element is known to be non-zero when
3460/// defined. For pointers, if the context instruction and dominator tree are
3461/// specified, perform context-sensitive analysis and return true if the
3462/// pointer couldn't possibly be null at the specified instruction.
3463/// Supports values with integer or pointer type and vectors of integers.
3464bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3465 const SimplifyQuery &Q, unsigned Depth) {
3466 Type *Ty = V->getType();
3467
3468#ifndef NDEBUG
3469 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3470
3471 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3472 assert(
3473 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3474 "DemandedElt width should equal the fixed vector number of elements");
3475 } else {
3476 assert(DemandedElts == APInt(1, 1) &&
3477 "DemandedElt width should be 1 for scalars");
3478 }
3479#endif
3480
3481 if (auto *C = dyn_cast<Constant>(V)) {
3482 if (C->isNullValue())
3483 return false;
3484 if (isa<ConstantInt>(C))
3485 // Must be non-zero due to null test above.
3486 return true;
3487
3488 // For constant vectors, check that all elements are poison or known
3489 // non-zero to determine that the whole vector is known non-zero.
3490 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3491 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3492 if (!DemandedElts[i])
3493 continue;
3494 Constant *Elt = C->getAggregateElement(i);
3495 if (!Elt || Elt->isNullValue())
3496 return false;
3497 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3498 return false;
3499 }
3500 return true;
3501 }
3502
3503 // Constant ptrauth can be null, iff the base pointer can be.
3504 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3505 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3506
3507 // A global variable in address space 0 is non null unless extern weak
3508 // or an absolute symbol reference. Other address spaces may have null as a
3509 // valid address for a global, so we can't assume anything.
3510 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3511 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3512 GV->getType()->getAddressSpace() == 0)
3513 return true;
3514 }
3515
3516 // For constant expressions, fall through to the Operator code below.
3517 if (!isa<ConstantExpr>(V))
3518 return false;
3519 }
3520
3521 if (const auto *A = dyn_cast<Argument>(V))
3522 if (std::optional<ConstantRange> Range = A->getRange()) {
3523 const APInt ZeroValue(Range->getBitWidth(), 0);
3524 if (!Range->contains(ZeroValue))
3525 return true;
3526 }
3527
3528 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3529 return true;
3530
3531 // Some of the tests below are recursive, so bail out if we hit the limit.
3533 return false;
3534
3535 // Check for pointer simplifications.
3536
3537 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3538 // A byval, inalloca may not be null in a non-default addres space. A
3539 // nonnull argument is assumed never 0.
3540 if (const Argument *A = dyn_cast<Argument>(V)) {
3541 if (((A->hasPassPointeeByValueCopyAttr() &&
3542 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3543 A->hasNonNullAttr()))
3544 return true;
3545 }
3546 }
3547
3548 if (const auto *I = dyn_cast<Operator>(V))
3549 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3550 return true;
3551
3552 if (!isa<Constant>(V) &&
3554 return true;
3555
3556 if (const Value *Stripped = stripNullTest(V))
3557 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3558
3559 return false;
3560}
3561
3563 unsigned Depth) {
3564 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3565 APInt DemandedElts =
3566 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3567 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3568}
3569
3570/// If the pair of operators are the same invertible function, return the
3571/// the operands of the function corresponding to each input. Otherwise,
3572/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3573/// every input value to exactly one output value. This is equivalent to
3574/// saying that Op1 and Op2 are equal exactly when the specified pair of
3575/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3576static std::optional<std::pair<Value*, Value*>>
3578 const Operator *Op2) {
3579 if (Op1->getOpcode() != Op2->getOpcode())
3580 return std::nullopt;
3581
3582 auto getOperands = [&](unsigned OpNum) -> auto {
3583 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3584 };
3585
3586 switch (Op1->getOpcode()) {
3587 default:
3588 break;
3589 case Instruction::Or:
3590 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3591 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3592 break;
3593 [[fallthrough]];
3594 case Instruction::Xor:
3595 case Instruction::Add: {
3596 Value *Other;
3597 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3598 return std::make_pair(Op1->getOperand(1), Other);
3599 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3600 return std::make_pair(Op1->getOperand(0), Other);
3601 break;
3602 }
3603 case Instruction::Sub:
3604 if (Op1->getOperand(0) == Op2->getOperand(0))
3605 return getOperands(1);
3606 if (Op1->getOperand(1) == Op2->getOperand(1))
3607 return getOperands(0);
3608 break;
3609 case Instruction::Mul: {
3610 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3611 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3612 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3613 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3614 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3615 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3616 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3617 break;
3618
3619 // Assume operand order has been canonicalized
3620 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3621 isa<ConstantInt>(Op1->getOperand(1)) &&
3622 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3623 return getOperands(0);
3624 break;
3625 }
3626 case Instruction::Shl: {
3627 // Same as multiplies, with the difference that we don't need to check
3628 // for a non-zero multiply. Shifts always multiply by non-zero.
3629 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3630 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3631 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3632 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3633 break;
3634
3635 if (Op1->getOperand(1) == Op2->getOperand(1))
3636 return getOperands(0);
3637 break;
3638 }
3639 case Instruction::AShr:
3640 case Instruction::LShr: {
3641 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3642 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3643 if (!PEO1->isExact() || !PEO2->isExact())
3644 break;
3645
3646 if (Op1->getOperand(1) == Op2->getOperand(1))
3647 return getOperands(0);
3648 break;
3649 }
3650 case Instruction::SExt:
3651 case Instruction::ZExt:
3652 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3653 return getOperands(0);
3654 break;
3655 case Instruction::PHI: {
3656 const PHINode *PN1 = cast<PHINode>(Op1);
3657 const PHINode *PN2 = cast<PHINode>(Op2);
3658
3659 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3660 // are a single invertible function of the start values? Note that repeated
3661 // application of an invertible function is also invertible
3662 BinaryOperator *BO1 = nullptr;
3663 Value *Start1 = nullptr, *Step1 = nullptr;
3664 BinaryOperator *BO2 = nullptr;
3665 Value *Start2 = nullptr, *Step2 = nullptr;
3666 if (PN1->getParent() != PN2->getParent() ||
3667 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3668 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3669 break;
3670
3671 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3672 cast<Operator>(BO2));
3673 if (!Values)
3674 break;
3675
3676 // We have to be careful of mutually defined recurrences here. Ex:
3677 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3678 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3679 // The invertibility of these is complicated, and not worth reasoning
3680 // about (yet?).
3681 if (Values->first != PN1 || Values->second != PN2)
3682 break;
3683
3684 return std::make_pair(Start1, Start2);
3685 }
3686 }
3687 return std::nullopt;
3688}
3689
3690/// Return true if V1 == (binop V2, X), where X is known non-zero.
3691/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3692/// implies V2 != V1.
3693static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3694 const APInt &DemandedElts,
3695 const SimplifyQuery &Q, unsigned Depth) {
3697 if (!BO)
3698 return false;
3699 switch (BO->getOpcode()) {
3700 default:
3701 break;
3702 case Instruction::Or:
3703 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3704 break;
3705 [[fallthrough]];
3706 case Instruction::Xor:
3707 case Instruction::Add:
3708 Value *Op = nullptr;
3709 if (V2 == BO->getOperand(0))
3710 Op = BO->getOperand(1);
3711 else if (V2 == BO->getOperand(1))
3712 Op = BO->getOperand(0);
3713 else
3714 return false;
3715 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3716 }
3717 return false;
3718}
3719
3720/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3721/// the multiplication is nuw or nsw.
3722static bool isNonEqualMul(const Value *V1, const Value *V2,
3723 const APInt &DemandedElts, const SimplifyQuery &Q,
3724 unsigned Depth) {
3725 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3726 const APInt *C;
3727 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3728 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3729 !C->isZero() && !C->isOne() &&
3730 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3731 }
3732 return false;
3733}
3734
3735/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3736/// the shift is nuw or nsw.
3737static bool isNonEqualShl(const Value *V1, const Value *V2,
3738 const APInt &DemandedElts, const SimplifyQuery &Q,
3739 unsigned Depth) {
3740 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3741 const APInt *C;
3742 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3743 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3744 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3745 }
3746 return false;
3747}
3748
3749static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3750 const APInt &DemandedElts, const SimplifyQuery &Q,
3751 unsigned Depth) {
3752 // Check two PHIs are in same block.
3753 if (PN1->getParent() != PN2->getParent())
3754 return false;
3755
3757 bool UsedFullRecursion = false;
3758 for (const BasicBlock *IncomBB : PN1->blocks()) {
3759 if (!VisitedBBs.insert(IncomBB).second)
3760 continue; // Don't reprocess blocks that we have dealt with already.
3761 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3762 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3763 const APInt *C1, *C2;
3764 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3765 continue;
3766
3767 // Only one pair of phi operands is allowed for full recursion.
3768 if (UsedFullRecursion)
3769 return false;
3770
3772 RecQ.CxtI = IncomBB->getTerminator();
3773 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3774 return false;
3775 UsedFullRecursion = true;
3776 }
3777 return true;
3778}
3779
3780static bool isNonEqualSelect(const Value *V1, const Value *V2,
3781 const APInt &DemandedElts, const SimplifyQuery &Q,
3782 unsigned Depth) {
3783 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3784 if (!SI1)
3785 return false;
3786
3787 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3788 const Value *Cond1 = SI1->getCondition();
3789 const Value *Cond2 = SI2->getCondition();
3790 if (Cond1 == Cond2)
3791 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3792 DemandedElts, Q, Depth + 1) &&
3793 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3794 DemandedElts, Q, Depth + 1);
3795 }
3796 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
3797 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
3798}
3799
3800// Check to see if A is both a GEP and is the incoming value for a PHI in the
3801// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3802// one of them being the recursive GEP A and the other a ptr at same base and at
3803// the same/higher offset than B we are only incrementing the pointer further in
3804// loop if offset of recursive GEP is greater than 0.
3806 const SimplifyQuery &Q) {
3807 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3808 return false;
3809
3810 auto *GEPA = dyn_cast<GEPOperator>(A);
3811 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3812 return false;
3813
3814 // Handle 2 incoming PHI values with one being a recursive GEP.
3815 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3816 if (!PN || PN->getNumIncomingValues() != 2)
3817 return false;
3818
3819 // Search for the recursive GEP as an incoming operand, and record that as
3820 // Step.
3821 Value *Start = nullptr;
3822 Value *Step = const_cast<Value *>(A);
3823 if (PN->getIncomingValue(0) == Step)
3824 Start = PN->getIncomingValue(1);
3825 else if (PN->getIncomingValue(1) == Step)
3826 Start = PN->getIncomingValue(0);
3827 else
3828 return false;
3829
3830 // Other incoming node base should match the B base.
3831 // StartOffset >= OffsetB && StepOffset > 0?
3832 // StartOffset <= OffsetB && StepOffset < 0?
3833 // Is non-equal if above are true.
3834 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3835 // optimisation to inbounds GEPs only.
3836 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3837 APInt StartOffset(IndexWidth, 0);
3838 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3839 APInt StepOffset(IndexWidth, 0);
3840 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3841
3842 // Check if Base Pointer of Step matches the PHI.
3843 if (Step != PN)
3844 return false;
3845 APInt OffsetB(IndexWidth, 0);
3846 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3847 return Start == B &&
3848 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
3849 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
3850}
3851
3852static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
3853 const SimplifyQuery &Q, unsigned Depth) {
3854 if (!Q.CxtI)
3855 return false;
3856
3857 // Try to infer NonEqual based on information from dominating conditions.
3858 if (Q.DC && Q.DT) {
3859 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
3860 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
3861 Value *Cond = BI->getCondition();
3862 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
3863 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
3865 /*LHSIsTrue=*/true, Depth)
3866 .value_or(false))
3867 return true;
3868
3869 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
3870 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
3872 /*LHSIsTrue=*/false, Depth)
3873 .value_or(false))
3874 return true;
3875 }
3876
3877 return false;
3878 };
3879
3880 if (IsKnownNonEqualFromDominatingCondition(V1) ||
3881 IsKnownNonEqualFromDominatingCondition(V2))
3882 return true;
3883 }
3884
3885 if (!Q.AC)
3886 return false;
3887
3888 // Try to infer NonEqual based on information from assumptions.
3889 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
3890 if (!AssumeVH)
3891 continue;
3892 CallInst *I = cast<CallInst>(AssumeVH);
3893
3894 assert(I->getFunction() == Q.CxtI->getFunction() &&
3895 "Got assumption for the wrong function!");
3896 assert(I->getIntrinsicID() == Intrinsic::assume &&
3897 "must be an assume intrinsic");
3898
3899 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
3900 /*LHSIsTrue=*/true, Depth)
3901 .value_or(false) &&
3903 return true;
3904 }
3905
3906 return false;
3907}
3908
3909/// Return true if it is known that V1 != V2.
3910static bool isKnownNonEqual(const Value *V1, const Value *V2,
3911 const APInt &DemandedElts, const SimplifyQuery &Q,
3912 unsigned Depth) {
3913 if (V1 == V2)
3914 return false;
3915 if (V1->getType() != V2->getType())
3916 // We can't look through casts yet.
3917 return false;
3918
3920 return false;
3921
3922 // See if we can recurse through (exactly one of) our operands. This
3923 // requires our operation be 1-to-1 and map every input value to exactly
3924 // one output value. Such an operation is invertible.
3925 auto *O1 = dyn_cast<Operator>(V1);
3926 auto *O2 = dyn_cast<Operator>(V2);
3927 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3928 if (auto Values = getInvertibleOperands(O1, O2))
3929 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
3930 Depth + 1);
3931
3932 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3933 const PHINode *PN2 = cast<PHINode>(V2);
3934 // FIXME: This is missing a generalization to handle the case where one is
3935 // a PHI and another one isn't.
3936 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
3937 return true;
3938 };
3939 }
3940
3941 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
3942 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
3943 return true;
3944
3945 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
3946 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
3947 return true;
3948
3949 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
3950 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
3951 return true;
3952
3953 if (V1->getType()->isIntOrIntVectorTy()) {
3954 // Are any known bits in V1 contradictory to known bits in V2? If V1
3955 // has a known zero where V2 has a known one, they must not be equal.
3956 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
3957 if (!Known1.isUnknown()) {
3958 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
3959 if (Known1.Zero.intersects(Known2.One) ||
3960 Known2.Zero.intersects(Known1.One))
3961 return true;
3962 }
3963 }
3964
3965 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
3966 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
3967 return true;
3968
3969 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3971 return true;
3972
3973 Value *A, *B;
3974 // PtrToInts are NonEqual if their Ptrs are NonEqual.
3975 // Check PtrToInt type matches the pointer size.
3976 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
3978 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
3979
3980 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
3981 return true;
3982
3983 return false;
3984}
3985
3986/// For vector constants, loop over the elements and find the constant with the
3987/// minimum number of sign bits. Return 0 if the value is not a vector constant
3988/// or if any element was not analyzed; otherwise, return the count for the
3989/// element with the minimum number of sign bits.
3991 const APInt &DemandedElts,
3992 unsigned TyBits) {
3993 const auto *CV = dyn_cast<Constant>(V);
3994 if (!CV || !isa<FixedVectorType>(CV->getType()))
3995 return 0;
3996
3997 unsigned MinSignBits = TyBits;
3998 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3999 for (unsigned i = 0; i != NumElts; ++i) {
4000 if (!DemandedElts[i])
4001 continue;
4002 // If we find a non-ConstantInt, bail out.
4003 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4004 if (!Elt)
4005 return 0;
4006
4007 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4008 }
4009
4010 return MinSignBits;
4011}
4012
4013static unsigned ComputeNumSignBitsImpl(const Value *V,
4014 const APInt &DemandedElts,
4015 const SimplifyQuery &Q, unsigned Depth);
4016
4017static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4018 const SimplifyQuery &Q, unsigned Depth) {
4019 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4020 assert(Result > 0 && "At least one sign bit needs to be present!");
4021 return Result;
4022}
4023
4024/// Return the number of times the sign bit of the register is replicated into
4025/// the other bits. We know that at least 1 bit is always equal to the sign bit
4026/// (itself), but other cases can give us information. For example, immediately
4027/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4028/// other, so we return 3. For vectors, return the number of sign bits for the
4029/// vector element with the minimum number of known sign bits of the demanded
4030/// elements in the vector specified by DemandedElts.
4031static unsigned ComputeNumSignBitsImpl(const Value *V,
4032 const APInt &DemandedElts,
4033 const SimplifyQuery &Q, unsigned Depth) {
4034 Type *Ty = V->getType();
4035#ifndef NDEBUG
4036 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4037
4038 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4039 assert(
4040 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4041 "DemandedElt width should equal the fixed vector number of elements");
4042 } else {
4043 assert(DemandedElts == APInt(1, 1) &&
4044 "DemandedElt width should be 1 for scalars");
4045 }
4046#endif
4047
4048 // We return the minimum number of sign bits that are guaranteed to be present
4049 // in V, so for undef we have to conservatively return 1. We don't have the
4050 // same behavior for poison though -- that's a FIXME today.
4051
4052 Type *ScalarTy = Ty->getScalarType();
4053 unsigned TyBits = ScalarTy->isPointerTy() ?
4054 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4055 Q.DL.getTypeSizeInBits(ScalarTy);
4056
4057 unsigned Tmp, Tmp2;
4058 unsigned FirstAnswer = 1;
4059
4060 // Note that ConstantInt is handled by the general computeKnownBits case
4061 // below.
4062
4064 return 1;
4065
4066 if (auto *U = dyn_cast<Operator>(V)) {
4067 switch (Operator::getOpcode(V)) {
4068 default: break;
4069 case Instruction::BitCast: {
4070 Value *Src = U->getOperand(0);
4071 Type *SrcTy = Src->getType();
4072
4073 // Skip if the source type is not an integer or integer vector type
4074 // This ensures we only process integer-like types
4075 if (!SrcTy->isIntOrIntVectorTy())
4076 break;
4077
4078 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4079
4080 // Bitcast 'large element' scalar/vector to 'small element' vector.
4081 if ((SrcBits % TyBits) != 0)
4082 break;
4083
4084 // Only proceed if the destination type is a fixed-size vector
4085 if (isa<FixedVectorType>(Ty)) {
4086 // Fast case - sign splat can be simply split across the small elements.
4087 // This works for both vector and scalar sources
4088 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4089 if (Tmp == SrcBits)
4090 return TyBits;
4091 }
4092 break;
4093 }
4094 case Instruction::SExt:
4095 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4096 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4097 Tmp;
4098
4099 case Instruction::SDiv: {
4100 const APInt *Denominator;
4101 // sdiv X, C -> adds log(C) sign bits.
4102 if (match(U->getOperand(1), m_APInt(Denominator))) {
4103
4104 // Ignore non-positive denominator.
4105 if (!Denominator->isStrictlyPositive())
4106 break;
4107
4108 // Calculate the incoming numerator bits.
4109 unsigned NumBits =
4110 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4111
4112 // Add floor(log(C)) bits to the numerator bits.
4113 return std::min(TyBits, NumBits + Denominator->logBase2());
4114 }
4115 break;
4116 }
4117
4118 case Instruction::SRem: {
4119 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4120
4121 const APInt *Denominator;
4122 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4123 // positive constant. This let us put a lower bound on the number of sign
4124 // bits.
4125 if (match(U->getOperand(1), m_APInt(Denominator))) {
4126
4127 // Ignore non-positive denominator.
4128 if (Denominator->isStrictlyPositive()) {
4129 // Calculate the leading sign bit constraints by examining the
4130 // denominator. Given that the denominator is positive, there are two
4131 // cases:
4132 //
4133 // 1. The numerator is positive. The result range is [0,C) and
4134 // [0,C) u< (1 << ceilLogBase2(C)).
4135 //
4136 // 2. The numerator is negative. Then the result range is (-C,0] and
4137 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4138 //
4139 // Thus a lower bound on the number of sign bits is `TyBits -
4140 // ceilLogBase2(C)`.
4141
4142 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4143 Tmp = std::max(Tmp, ResBits);
4144 }
4145 }
4146 return Tmp;
4147 }
4148
4149 case Instruction::AShr: {
4150 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4151 // ashr X, C -> adds C sign bits. Vectors too.
4152 const APInt *ShAmt;
4153 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4154 if (ShAmt->uge(TyBits))
4155 break; // Bad shift.
4156 unsigned ShAmtLimited = ShAmt->getZExtValue();
4157 Tmp += ShAmtLimited;
4158 if (Tmp > TyBits) Tmp = TyBits;
4159 }
4160 return Tmp;
4161 }
4162 case Instruction::Shl: {
4163 const APInt *ShAmt;
4164 Value *X = nullptr;
4165 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4166 // shl destroys sign bits.
4167 if (ShAmt->uge(TyBits))
4168 break; // Bad shift.
4169 // We can look through a zext (more or less treating it as a sext) if
4170 // all extended bits are shifted out.
4171 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4172 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4173 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4174 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4175 } else
4176 Tmp =
4177 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4178 if (ShAmt->uge(Tmp))
4179 break; // Shifted all sign bits out.
4180 Tmp2 = ShAmt->getZExtValue();
4181 return Tmp - Tmp2;
4182 }
4183 break;
4184 }
4185 case Instruction::And:
4186 case Instruction::Or:
4187 case Instruction::Xor: // NOT is handled here.
4188 // Logical binary ops preserve the number of sign bits at the worst.
4189 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4190 if (Tmp != 1) {
4191 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4192 FirstAnswer = std::min(Tmp, Tmp2);
4193 // We computed what we know about the sign bits as our first
4194 // answer. Now proceed to the generic code that uses
4195 // computeKnownBits, and pick whichever answer is better.
4196 }
4197 break;
4198
4199 case Instruction::Select: {
4200 // If we have a clamp pattern, we know that the number of sign bits will
4201 // be the minimum of the clamp min/max range.
4202 const Value *X;
4203 const APInt *CLow, *CHigh;
4204 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4205 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4206
4207 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4208 if (Tmp == 1)
4209 break;
4210 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4211 return std::min(Tmp, Tmp2);
4212 }
4213
4214 case Instruction::Add:
4215 // Add can have at most one carry bit. Thus we know that the output
4216 // is, at worst, one more bit than the inputs.
4217 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4218 if (Tmp == 1) break;
4219
4220 // Special case decrementing a value (ADD X, -1):
4221 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4222 if (CRHS->isAllOnesValue()) {
4223 KnownBits Known(TyBits);
4224 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4225
4226 // If the input is known to be 0 or 1, the output is 0/-1, which is
4227 // all sign bits set.
4228 if ((Known.Zero | 1).isAllOnes())
4229 return TyBits;
4230
4231 // If we are subtracting one from a positive number, there is no carry
4232 // out of the result.
4233 if (Known.isNonNegative())
4234 return Tmp;
4235 }
4236
4237 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4238 if (Tmp2 == 1)
4239 break;
4240 return std::min(Tmp, Tmp2) - 1;
4241
4242 case Instruction::Sub:
4243 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4244 if (Tmp2 == 1)
4245 break;
4246
4247 // Handle NEG.
4248 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4249 if (CLHS->isNullValue()) {
4250 KnownBits Known(TyBits);
4251 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4252 // If the input is known to be 0 or 1, the output is 0/-1, which is
4253 // all sign bits set.
4254 if ((Known.Zero | 1).isAllOnes())
4255 return TyBits;
4256
4257 // If the input is known to be positive (the sign bit is known clear),
4258 // the output of the NEG has the same number of sign bits as the
4259 // input.
4260 if (Known.isNonNegative())
4261 return Tmp2;
4262
4263 // Otherwise, we treat this like a SUB.
4264 }
4265
4266 // Sub can have at most one carry bit. Thus we know that the output
4267 // is, at worst, one more bit than the inputs.
4268 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4269 if (Tmp == 1)
4270 break;
4271 return std::min(Tmp, Tmp2) - 1;
4272
4273 case Instruction::Mul: {
4274 // The output of the Mul can be at most twice the valid bits in the
4275 // inputs.
4276 unsigned SignBitsOp0 =
4277 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4278 if (SignBitsOp0 == 1)
4279 break;
4280 unsigned SignBitsOp1 =
4281 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4282 if (SignBitsOp1 == 1)
4283 break;
4284 unsigned OutValidBits =
4285 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4286 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4287 }
4288
4289 case Instruction::PHI: {
4290 const PHINode *PN = cast<PHINode>(U);
4291 unsigned NumIncomingValues = PN->getNumIncomingValues();
4292 // Don't analyze large in-degree PHIs.
4293 if (NumIncomingValues > 4) break;
4294 // Unreachable blocks may have zero-operand PHI nodes.
4295 if (NumIncomingValues == 0) break;
4296
4297 // Take the minimum of all incoming values. This can't infinitely loop
4298 // because of our depth threshold.
4300 Tmp = TyBits;
4301 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4302 if (Tmp == 1) return Tmp;
4303 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4304 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4305 DemandedElts, RecQ, Depth + 1));
4306 }
4307 return Tmp;
4308 }
4309
4310 case Instruction::Trunc: {
4311 // If the input contained enough sign bits that some remain after the
4312 // truncation, then we can make use of that. Otherwise we don't know
4313 // anything.
4314 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4315 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4316 if (Tmp > (OperandTyBits - TyBits))
4317 return Tmp - (OperandTyBits - TyBits);
4318
4319 return 1;
4320 }
4321
4322 case Instruction::ExtractElement:
4323 // Look through extract element. At the moment we keep this simple and
4324 // skip tracking the specific element. But at least we might find
4325 // information valid for all elements of the vector (for example if vector
4326 // is sign extended, shifted, etc).
4327 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4328
4329 case Instruction::ShuffleVector: {
4330 // Collect the minimum number of sign bits that are shared by every vector
4331 // element referenced by the shuffle.
4332 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4333 if (!Shuf) {
4334 // FIXME: Add support for shufflevector constant expressions.
4335 return 1;
4336 }
4337 APInt DemandedLHS, DemandedRHS;
4338 // For undef elements, we don't know anything about the common state of
4339 // the shuffle result.
4340 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4341 return 1;
4342 Tmp = std::numeric_limits<unsigned>::max();
4343 if (!!DemandedLHS) {
4344 const Value *LHS = Shuf->getOperand(0);
4345 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4346 }
4347 // If we don't know anything, early out and try computeKnownBits
4348 // fall-back.
4349 if (Tmp == 1)
4350 break;
4351 if (!!DemandedRHS) {
4352 const Value *RHS = Shuf->getOperand(1);
4353 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4354 Tmp = std::min(Tmp, Tmp2);
4355 }
4356 // If we don't know anything, early out and try computeKnownBits
4357 // fall-back.
4358 if (Tmp == 1)
4359 break;
4360 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4361 return Tmp;
4362 }
4363 case Instruction::Call: {
4364 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4365 switch (II->getIntrinsicID()) {
4366 default:
4367 break;
4368 case Intrinsic::abs:
4369 Tmp =
4370 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4371 if (Tmp == 1)
4372 break;
4373
4374 // Absolute value reduces number of sign bits by at most 1.
4375 return Tmp - 1;
4376 case Intrinsic::smin:
4377 case Intrinsic::smax: {
4378 const APInt *CLow, *CHigh;
4379 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4380 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4381 }
4382 }
4383 }
4384 }
4385 }
4386 }
4387
4388 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4389 // use this information.
4390
4391 // If we can examine all elements of a vector constant successfully, we're
4392 // done (we can't do any better than that). If not, keep trying.
4393 if (unsigned VecSignBits =
4394 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4395 return VecSignBits;
4396
4397 KnownBits Known(TyBits);
4398 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4399
4400 // If we know that the sign bit is either zero or one, determine the number of
4401 // identical bits in the top of the input value.
4402 return std::max(FirstAnswer, Known.countMinSignBits());
4403}
4404
4406 const TargetLibraryInfo *TLI) {
4407 const Function *F = CB.getCalledFunction();
4408 if (!F)
4410
4411 if (F->isIntrinsic())
4412 return F->getIntrinsicID();
4413
4414 // We are going to infer semantics of a library function based on mapping it
4415 // to an LLVM intrinsic. Check that the library function is available from
4416 // this callbase and in this environment.
4417 LibFunc Func;
4418 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4419 !CB.onlyReadsMemory())
4421
4422 switch (Func) {
4423 default:
4424 break;
4425 case LibFunc_sin:
4426 case LibFunc_sinf:
4427 case LibFunc_sinl:
4428 return Intrinsic::sin;
4429 case LibFunc_cos:
4430 case LibFunc_cosf:
4431 case LibFunc_cosl:
4432 return Intrinsic::cos;
4433 case LibFunc_tan:
4434 case LibFunc_tanf:
4435 case LibFunc_tanl:
4436 return Intrinsic::tan;
4437 case LibFunc_asin:
4438 case LibFunc_asinf:
4439 case LibFunc_asinl:
4440 return Intrinsic::asin;
4441 case LibFunc_acos:
4442 case LibFunc_acosf:
4443 case LibFunc_acosl:
4444 return Intrinsic::acos;
4445 case LibFunc_atan:
4446 case LibFunc_atanf:
4447 case LibFunc_atanl:
4448 return Intrinsic::atan;
4449 case LibFunc_atan2:
4450 case LibFunc_atan2f:
4451 case LibFunc_atan2l:
4452 return Intrinsic::atan2;
4453 case LibFunc_sinh:
4454 case LibFunc_sinhf:
4455 case LibFunc_sinhl:
4456 return Intrinsic::sinh;
4457 case LibFunc_cosh:
4458 case LibFunc_coshf:
4459 case LibFunc_coshl:
4460 return Intrinsic::cosh;
4461 case LibFunc_tanh:
4462 case LibFunc_tanhf:
4463 case LibFunc_tanhl:
4464 return Intrinsic::tanh;
4465 case LibFunc_exp:
4466 case LibFunc_expf:
4467 case LibFunc_expl:
4468 return Intrinsic::exp;
4469 case LibFunc_exp2:
4470 case LibFunc_exp2f:
4471 case LibFunc_exp2l:
4472 return Intrinsic::exp2;
4473 case LibFunc_exp10:
4474 case LibFunc_exp10f:
4475 case LibFunc_exp10l:
4476 return Intrinsic::exp10;
4477 case LibFunc_log:
4478 case LibFunc_logf:
4479 case LibFunc_logl:
4480 return Intrinsic::log;
4481 case LibFunc_log10:
4482 case LibFunc_log10f:
4483 case LibFunc_log10l:
4484 return Intrinsic::log10;
4485 case LibFunc_log2:
4486 case LibFunc_log2f:
4487 case LibFunc_log2l:
4488 return Intrinsic::log2;
4489 case LibFunc_fabs:
4490 case LibFunc_fabsf:
4491 case LibFunc_fabsl:
4492 return Intrinsic::fabs;
4493 case LibFunc_fmin:
4494 case LibFunc_fminf:
4495 case LibFunc_fminl:
4496 return Intrinsic::minnum;
4497 case LibFunc_fmax:
4498 case LibFunc_fmaxf:
4499 case LibFunc_fmaxl:
4500 return Intrinsic::maxnum;
4501 case LibFunc_copysign:
4502 case LibFunc_copysignf:
4503 case LibFunc_copysignl:
4504 return Intrinsic::copysign;
4505 case LibFunc_floor:
4506 case LibFunc_floorf:
4507 case LibFunc_floorl:
4508 return Intrinsic::floor;
4509 case LibFunc_ceil:
4510 case LibFunc_ceilf:
4511 case LibFunc_ceill:
4512 return Intrinsic::ceil;
4513 case LibFunc_trunc:
4514 case LibFunc_truncf:
4515 case LibFunc_truncl:
4516 return Intrinsic::trunc;
4517 case LibFunc_rint:
4518 case LibFunc_rintf:
4519 case LibFunc_rintl:
4520 return Intrinsic::rint;
4521 case LibFunc_nearbyint:
4522 case LibFunc_nearbyintf:
4523 case LibFunc_nearbyintl:
4524 return Intrinsic::nearbyint;
4525 case LibFunc_round:
4526 case LibFunc_roundf:
4527 case LibFunc_roundl:
4528 return Intrinsic::round;
4529 case LibFunc_roundeven:
4530 case LibFunc_roundevenf:
4531 case LibFunc_roundevenl:
4532 return Intrinsic::roundeven;
4533 case LibFunc_pow:
4534 case LibFunc_powf:
4535 case LibFunc_powl:
4536 return Intrinsic::pow;
4537 case LibFunc_sqrt:
4538 case LibFunc_sqrtf:
4539 case LibFunc_sqrtl:
4540 return Intrinsic::sqrt;
4541 }
4542
4544}
4545
4546static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4547 Ty = Ty->getScalarType();
4548 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4549 return Mode.Output == DenormalMode::IEEE ||
4551}
4552/// Given an exploded icmp instruction, return true if the comparison only
4553/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4554/// the result of the comparison is true when the input value is signed.
4556 bool &TrueIfSigned) {
4557 switch (Pred) {
4558 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4559 TrueIfSigned = true;
4560 return RHS.isZero();
4561 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4562 TrueIfSigned = true;
4563 return RHS.isAllOnes();
4564 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4565 TrueIfSigned = false;
4566 return RHS.isAllOnes();
4567 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4568 TrueIfSigned = false;
4569 return RHS.isZero();
4570 case ICmpInst::ICMP_UGT:
4571 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4572 TrueIfSigned = true;
4573 return RHS.isMaxSignedValue();
4574 case ICmpInst::ICMP_UGE:
4575 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4576 TrueIfSigned = true;
4577 return RHS.isMinSignedValue();
4578 case ICmpInst::ICMP_ULT:
4579 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4580 TrueIfSigned = false;
4581 return RHS.isMinSignedValue();
4582 case ICmpInst::ICMP_ULE:
4583 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4584 TrueIfSigned = false;
4585 return RHS.isMaxSignedValue();
4586 default:
4587 return false;
4588 }
4589}
4590
4592 bool CondIsTrue,
4593 const Instruction *CxtI,
4594 KnownFPClass &KnownFromContext,
4595 unsigned Depth = 0) {
4596 Value *A, *B;
4598 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4599 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4600 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4601 Depth + 1);
4602 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4603 Depth + 1);
4604 return;
4605 }
4607 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4608 Depth + 1);
4609 return;
4610 }
4611 CmpPredicate Pred;
4612 Value *LHS;
4613 uint64_t ClassVal = 0;
4614 const APFloat *CRHS;
4615 const APInt *RHS;
4616 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4617 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4618 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4619 if (CmpVal == V)
4620 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4622 m_Specific(V), m_ConstantInt(ClassVal)))) {
4623 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4624 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4625 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4626 m_APInt(RHS)))) {
4627 bool TrueIfSigned;
4628 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4629 return;
4630 if (TrueIfSigned == CondIsTrue)
4631 KnownFromContext.signBitMustBeOne();
4632 else
4633 KnownFromContext.signBitMustBeZero();
4634 }
4635}
4636
4638 const SimplifyQuery &Q) {
4639 KnownFPClass KnownFromContext;
4640
4641 if (Q.CC && Q.CC->AffectedValues.contains(V))
4643 KnownFromContext);
4644
4645 if (!Q.CxtI)
4646 return KnownFromContext;
4647
4648 if (Q.DC && Q.DT) {
4649 // Handle dominating conditions.
4650 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4651 Value *Cond = BI->getCondition();
4652
4653 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4654 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4655 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4656 KnownFromContext);
4657
4658 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4659 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4660 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4661 KnownFromContext);
4662 }
4663 }
4664
4665 if (!Q.AC)
4666 return KnownFromContext;
4667
4668 // Try to restrict the floating-point classes based on information from
4669 // assumptions.
4670 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4671 if (!AssumeVH)
4672 continue;
4673 CallInst *I = cast<CallInst>(AssumeVH);
4674
4675 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4676 "Got assumption for the wrong function!");
4677 assert(I->getIntrinsicID() == Intrinsic::assume &&
4678 "must be an assume intrinsic");
4679
4680 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4681 continue;
4682
4683 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4684 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4685 }
4686
4687 return KnownFromContext;
4688}
4689
4690void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4691 FPClassTest InterestedClasses, KnownFPClass &Known,
4692 const SimplifyQuery &Q, unsigned Depth);
4693
4694static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4695 FPClassTest InterestedClasses,
4696 const SimplifyQuery &Q, unsigned Depth) {
4697 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4698 APInt DemandedElts =
4699 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4700 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4701}
4702
4704 const APInt &DemandedElts,
4705 FPClassTest InterestedClasses,
4706 KnownFPClass &Known,
4707 const SimplifyQuery &Q,
4708 unsigned Depth) {
4709 if ((InterestedClasses &
4711 return;
4712
4713 KnownFPClass KnownSrc;
4714 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4715 KnownSrc, Q, Depth + 1);
4716
4717 // Sign should be preserved
4718 // TODO: Handle cannot be ordered greater than zero
4719 if (KnownSrc.cannotBeOrderedLessThanZero())
4721
4722 Known.propagateNaN(KnownSrc, true);
4723
4724 // Infinity needs a range check.
4725}
4726
4727void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4728 FPClassTest InterestedClasses, KnownFPClass &Known,
4729 const SimplifyQuery &Q, unsigned Depth) {
4730 assert(Known.isUnknown() && "should not be called with known information");
4731
4732 if (!DemandedElts) {
4733 // No demanded elts, better to assume we don't know anything.
4734 Known.resetAll();
4735 return;
4736 }
4737
4738 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4739
4740 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4741 Known.KnownFPClasses = CFP->getValueAPF().classify();
4742 Known.SignBit = CFP->isNegative();
4743 return;
4744 }
4745
4747 Known.KnownFPClasses = fcPosZero;
4748 Known.SignBit = false;
4749 return;
4750 }
4751
4752 if (isa<PoisonValue>(V)) {
4753 Known.KnownFPClasses = fcNone;
4754 Known.SignBit = false;
4755 return;
4756 }
4757
4758 // Try to handle fixed width vector constants
4759 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
4760 const Constant *CV = dyn_cast<Constant>(V);
4761 if (VFVTy && CV) {
4762 Known.KnownFPClasses = fcNone;
4763 bool SignBitAllZero = true;
4764 bool SignBitAllOne = true;
4765
4766 // For vectors, verify that each element is not NaN.
4767 unsigned NumElts = VFVTy->getNumElements();
4768 for (unsigned i = 0; i != NumElts; ++i) {
4769 if (!DemandedElts[i])
4770 continue;
4771
4772 Constant *Elt = CV->getAggregateElement(i);
4773 if (!Elt) {
4774 Known = KnownFPClass();
4775 return;
4776 }
4777 if (isa<PoisonValue>(Elt))
4778 continue;
4779 auto *CElt = dyn_cast<ConstantFP>(Elt);
4780 if (!CElt) {
4781 Known = KnownFPClass();
4782 return;
4783 }
4784
4785 const APFloat &C = CElt->getValueAPF();
4786 Known.KnownFPClasses |= C.classify();
4787 if (C.isNegative())
4788 SignBitAllZero = false;
4789 else
4790 SignBitAllOne = false;
4791 }
4792 if (SignBitAllOne != SignBitAllZero)
4793 Known.SignBit = SignBitAllOne;
4794 return;
4795 }
4796
4797 FPClassTest KnownNotFromFlags = fcNone;
4798 if (const auto *CB = dyn_cast<CallBase>(V))
4799 KnownNotFromFlags |= CB->getRetNoFPClass();
4800 else if (const auto *Arg = dyn_cast<Argument>(V))
4801 KnownNotFromFlags |= Arg->getNoFPClass();
4802
4803 const Operator *Op = dyn_cast<Operator>(V);
4805 if (FPOp->hasNoNaNs())
4806 KnownNotFromFlags |= fcNan;
4807 if (FPOp->hasNoInfs())
4808 KnownNotFromFlags |= fcInf;
4809 }
4810
4811 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
4812 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
4813
4814 // We no longer need to find out about these bits from inputs if we can
4815 // assume this from flags/attributes.
4816 InterestedClasses &= ~KnownNotFromFlags;
4817
4818 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
4819 Known.knownNot(KnownNotFromFlags);
4820 if (!Known.SignBit && AssumedClasses.SignBit) {
4821 if (*AssumedClasses.SignBit)
4822 Known.signBitMustBeOne();
4823 else
4824 Known.signBitMustBeZero();
4825 }
4826 });
4827
4828 if (!Op)
4829 return;
4830
4831 // All recursive calls that increase depth must come after this.
4833 return;
4834
4835 const unsigned Opc = Op->getOpcode();
4836 switch (Opc) {
4837 case Instruction::FNeg: {
4838 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4839 Known, Q, Depth + 1);
4840 Known.fneg();
4841 break;
4842 }
4843 case Instruction::Select: {
4844 Value *Cond = Op->getOperand(0);
4845 Value *LHS = Op->getOperand(1);
4846 Value *RHS = Op->getOperand(2);
4847
4848 FPClassTest FilterLHS = fcAllFlags;
4849 FPClassTest FilterRHS = fcAllFlags;
4850
4851 Value *TestedValue = nullptr;
4852 FPClassTest MaskIfTrue = fcAllFlags;
4853 FPClassTest MaskIfFalse = fcAllFlags;
4854 uint64_t ClassVal = 0;
4855 const Function *F = cast<Instruction>(Op)->getFunction();
4856 CmpPredicate Pred;
4857 Value *CmpLHS, *CmpRHS;
4858 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
4859 // If the select filters out a value based on the class, it no longer
4860 // participates in the class of the result
4861
4862 // TODO: In some degenerate cases we can infer something if we try again
4863 // without looking through sign operations.
4864 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
4865 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
4866 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
4867 } else if (match(Cond,
4869 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
4870 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
4871 MaskIfTrue = TestedMask;
4872 MaskIfFalse = ~TestedMask;
4873 }
4874
4875 if (TestedValue == LHS) {
4876 // match !isnan(x) ? x : y
4877 FilterLHS = MaskIfTrue;
4878 } else if (TestedValue == RHS) { // && IsExactClass
4879 // match !isnan(x) ? y : x
4880 FilterRHS = MaskIfFalse;
4881 }
4882
4883 KnownFPClass Known2;
4884 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
4885 Q, Depth + 1);
4886 Known.KnownFPClasses &= FilterLHS;
4887
4888 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
4889 Known2, Q, Depth + 1);
4890 Known2.KnownFPClasses &= FilterRHS;
4891
4892 Known |= Known2;
4893 break;
4894 }
4895 case Instruction::Call: {
4896 const CallInst *II = cast<CallInst>(Op);
4897 const Intrinsic::ID IID = II->getIntrinsicID();
4898 switch (IID) {
4899 case Intrinsic::fabs: {
4900 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
4901 // If we only care about the sign bit we don't need to inspect the
4902 // operand.
4903 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
4904 InterestedClasses, Known, Q, Depth + 1);
4905 }
4906
4907 Known.fabs();
4908 break;
4909 }
4910 case Intrinsic::copysign: {
4911 KnownFPClass KnownSign;
4912
4913 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4914 Known, Q, Depth + 1);
4915 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4916 KnownSign, Q, Depth + 1);
4917 Known.copysign(KnownSign);
4918 break;
4919 }
4920 case Intrinsic::fma:
4921 case Intrinsic::fmuladd: {
4922 if ((InterestedClasses & fcNegative) == fcNone)
4923 break;
4924
4925 if (II->getArgOperand(0) != II->getArgOperand(1))
4926 break;
4927
4928 // The multiply cannot be -0 and therefore the add can't be -0
4929 Known.knownNot(fcNegZero);
4930
4931 // x * x + y is non-negative if y is non-negative.
4932 KnownFPClass KnownAddend;
4933 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
4934 KnownAddend, Q, Depth + 1);
4935
4936 if (KnownAddend.cannotBeOrderedLessThanZero())
4937 Known.knownNot(fcNegative);
4938 break;
4939 }
4940 case Intrinsic::sqrt:
4941 case Intrinsic::experimental_constrained_sqrt: {
4942 KnownFPClass KnownSrc;
4943 FPClassTest InterestedSrcs = InterestedClasses;
4944 if (InterestedClasses & fcNan)
4945 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
4946
4947 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
4948 KnownSrc, Q, Depth + 1);
4949
4950 if (KnownSrc.isKnownNeverPosInfinity())
4951 Known.knownNot(fcPosInf);
4952 if (KnownSrc.isKnownNever(fcSNan))
4953 Known.knownNot(fcSNan);
4954
4955 // Any negative value besides -0 returns a nan.
4956 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
4957 Known.knownNot(fcNan);
4958
4959 // The only negative value that can be returned is -0 for -0 inputs.
4961
4962 // If the input denormal mode could be PreserveSign, a negative
4963 // subnormal input could produce a negative zero output.
4964 const Function *F = II->getFunction();
4965 const fltSemantics &FltSem =
4966 II->getType()->getScalarType()->getFltSemantics();
4967
4968 if (Q.IIQ.hasNoSignedZeros(II) ||
4969 (F &&
4970 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem))))
4971 Known.knownNot(fcNegZero);
4972
4973 break;
4974 }
4975 case Intrinsic::sin:
4976 case Intrinsic::cos: {
4977 // Return NaN on infinite inputs.
4978 KnownFPClass KnownSrc;
4979 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4980 KnownSrc, Q, Depth + 1);
4981 Known.knownNot(fcInf);
4982 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
4983 Known.knownNot(fcNan);
4984 break;
4985 }
4986 case Intrinsic::maxnum:
4987 case Intrinsic::minnum:
4988 case Intrinsic::minimum:
4989 case Intrinsic::maximum:
4990 case Intrinsic::minimumnum:
4991 case Intrinsic::maximumnum: {
4992 KnownFPClass KnownLHS, KnownRHS;
4993 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4994 KnownLHS, Q, Depth + 1);
4995 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4996 KnownRHS, Q, Depth + 1);
4997
4998 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
4999 Known = KnownLHS | KnownRHS;
5000
5001 // If either operand is not NaN, the result is not NaN.
5002 if (NeverNaN &&
5003 (IID == Intrinsic::minnum || IID == Intrinsic::maxnum ||
5004 IID == Intrinsic::minimumnum || IID == Intrinsic::maximumnum))
5005 Known.knownNot(fcNan);
5006
5007 if (IID == Intrinsic::maxnum || IID == Intrinsic::maximumnum) {
5008 // If at least one operand is known to be positive, the result must be
5009 // positive.
5010 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
5011 KnownLHS.isKnownNeverNaN()) ||
5012 (KnownRHS.cannotBeOrderedLessThanZero() &&
5013 KnownRHS.isKnownNeverNaN()))
5015 } else if (IID == Intrinsic::maximum) {
5016 // If at least one operand is known to be positive, the result must be
5017 // positive.
5018 if (KnownLHS.cannotBeOrderedLessThanZero() ||
5019 KnownRHS.cannotBeOrderedLessThanZero())
5021 } else if (IID == Intrinsic::minnum || IID == Intrinsic::minimumnum) {
5022 // If at least one operand is known to be negative, the result must be
5023 // negative.
5024 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
5025 KnownLHS.isKnownNeverNaN()) ||
5026 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
5027 KnownRHS.isKnownNeverNaN()))
5029 } else if (IID == Intrinsic::minimum) {
5030 // If at least one operand is known to be negative, the result must be
5031 // negative.
5032 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
5035 } else
5036 llvm_unreachable("unhandled intrinsic");
5037
5038 // Fixup zero handling if denormals could be returned as a zero.
5039 //
5040 // As there's no spec for denormal flushing, be conservative with the
5041 // treatment of denormals that could be flushed to zero. For older
5042 // subtargets on AMDGPU the min/max instructions would not flush the
5043 // output and return the original value.
5044 //
5045 if ((Known.KnownFPClasses & fcZero) != fcNone &&
5046 !Known.isKnownNeverSubnormal()) {
5047 const Function *Parent = II->getFunction();
5048 if (!Parent)
5049 break;
5050
5052 II->getType()->getScalarType()->getFltSemantics());
5053 if (Mode != DenormalMode::getIEEE())
5054 Known.KnownFPClasses |= fcZero;
5055 }
5056
5057 if (Known.isKnownNeverNaN()) {
5058 if (KnownLHS.SignBit && KnownRHS.SignBit &&
5059 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5060 if (*KnownLHS.SignBit)
5061 Known.signBitMustBeOne();
5062 else
5063 Known.signBitMustBeZero();
5064 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum ||
5065 IID == Intrinsic::maximumnum ||
5066 IID == Intrinsic::minimumnum) ||
5067 // FIXME: Should be using logical zero versions
5068 ((KnownLHS.isKnownNeverNegZero() ||
5069 KnownRHS.isKnownNeverPosZero()) &&
5070 (KnownLHS.isKnownNeverPosZero() ||
5071 KnownRHS.isKnownNeverNegZero()))) {
5072 // Don't take sign bit from NaN operands.
5073 if (!KnownLHS.isKnownNeverNaN())
5074 KnownLHS.SignBit = std::nullopt;
5075 if (!KnownRHS.isKnownNeverNaN())
5076 KnownRHS.SignBit = std::nullopt;
5077 if ((IID == Intrinsic::maximum || IID == Intrinsic::maximumnum ||
5078 IID == Intrinsic::maxnum) &&
5079 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5080 Known.signBitMustBeZero();
5081 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minimumnum ||
5082 IID == Intrinsic::minnum) &&
5083 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5084 Known.signBitMustBeOne();
5085 }
5086 }
5087 break;
5088 }
5089 case Intrinsic::canonicalize: {
5090 KnownFPClass KnownSrc;
5091 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5092 KnownSrc, Q, Depth + 1);
5093
5094 // This is essentially a stronger form of
5095 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5096 // actually have an IR canonicalization guarantee.
5097
5098 // Canonicalize may flush denormals to zero, so we have to consider the
5099 // denormal mode to preserve known-not-0 knowledge.
5100 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5101
5102 // Stronger version of propagateNaN
5103 // Canonicalize is guaranteed to quiet signaling nans.
5104 if (KnownSrc.isKnownNeverNaN())
5105 Known.knownNot(fcNan);
5106 else
5107 Known.knownNot(fcSNan);
5108
5109 const Function *F = II->getFunction();
5110 if (!F)
5111 break;
5112
5113 // If the parent function flushes denormals, the canonical output cannot
5114 // be a denormal.
5115 const fltSemantics &FPType =
5116 II->getType()->getScalarType()->getFltSemantics();
5117 DenormalMode DenormMode = F->getDenormalMode(FPType);
5118 if (DenormMode == DenormalMode::getIEEE()) {
5119 if (KnownSrc.isKnownNever(fcPosZero))
5120 Known.knownNot(fcPosZero);
5121 if (KnownSrc.isKnownNever(fcNegZero))
5122 Known.knownNot(fcNegZero);
5123 break;
5124 }
5125
5126 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5127 Known.knownNot(fcSubnormal);
5128
5129 if (DenormMode.Input == DenormalMode::PositiveZero ||
5130 (DenormMode.Output == DenormalMode::PositiveZero &&
5131 DenormMode.Input == DenormalMode::IEEE))
5132 Known.knownNot(fcNegZero);
5133
5134 break;
5135 }
5136 case Intrinsic::vector_reduce_fmax:
5137 case Intrinsic::vector_reduce_fmin:
5138 case Intrinsic::vector_reduce_fmaximum:
5139 case Intrinsic::vector_reduce_fminimum: {
5140 // reduce min/max will choose an element from one of the vector elements,
5141 // so we can infer and class information that is common to all elements.
5142 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5143 InterestedClasses, Q, Depth + 1);
5144 // Can only propagate sign if output is never NaN.
5145 if (!Known.isKnownNeverNaN())
5146 Known.SignBit.reset();
5147 break;
5148 }
5149 // reverse preserves all characteristics of the input vec's element.
5150 case Intrinsic::vector_reverse:
5151 Known = computeKnownFPClass(
5152 II->getArgOperand(0), DemandedElts.reverseBits(),
5153 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5154 break;
5155 case Intrinsic::trunc:
5156 case Intrinsic::floor:
5157 case Intrinsic::ceil:
5158 case Intrinsic::rint:
5159 case Intrinsic::nearbyint:
5160 case Intrinsic::round:
5161 case Intrinsic::roundeven: {
5162 KnownFPClass KnownSrc;
5163 FPClassTest InterestedSrcs = InterestedClasses;
5164 if (InterestedSrcs & fcPosFinite)
5165 InterestedSrcs |= fcPosFinite;
5166 if (InterestedSrcs & fcNegFinite)
5167 InterestedSrcs |= fcNegFinite;
5168 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5169 KnownSrc, Q, Depth + 1);
5170
5171 // Integer results cannot be subnormal.
5172 Known.knownNot(fcSubnormal);
5173
5174 Known.propagateNaN(KnownSrc, true);
5175
5176 // Pass through infinities, except PPC_FP128 is a special case for
5177 // intrinsics other than trunc.
5178 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5179 if (KnownSrc.isKnownNeverPosInfinity())
5180 Known.knownNot(fcPosInf);
5181 if (KnownSrc.isKnownNeverNegInfinity())
5182 Known.knownNot(fcNegInf);
5183 }
5184
5185 // Negative round ups to 0 produce -0
5186 if (KnownSrc.isKnownNever(fcPosFinite))
5187 Known.knownNot(fcPosFinite);
5188 if (KnownSrc.isKnownNever(fcNegFinite))
5189 Known.knownNot(fcNegFinite);
5190
5191 break;
5192 }
5193 case Intrinsic::exp:
5194 case Intrinsic::exp2:
5195 case Intrinsic::exp10: {
5196 Known.knownNot(fcNegative);
5197 if ((InterestedClasses & fcNan) == fcNone)
5198 break;
5199
5200 KnownFPClass KnownSrc;
5201 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5202 KnownSrc, Q, Depth + 1);
5203 if (KnownSrc.isKnownNeverNaN()) {
5204 Known.knownNot(fcNan);
5205 Known.signBitMustBeZero();
5206 }
5207
5208 break;
5209 }
5210 case Intrinsic::fptrunc_round: {
5211 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5212 Q, Depth);
5213 break;
5214 }
5215 case Intrinsic::log:
5216 case Intrinsic::log10:
5217 case Intrinsic::log2:
5218 case Intrinsic::experimental_constrained_log:
5219 case Intrinsic::experimental_constrained_log10:
5220 case Intrinsic::experimental_constrained_log2: {
5221 // log(+inf) -> +inf
5222 // log([+-]0.0) -> -inf
5223 // log(-inf) -> nan
5224 // log(-x) -> nan
5225 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5226 break;
5227
5228 FPClassTest InterestedSrcs = InterestedClasses;
5229 if ((InterestedClasses & fcNegInf) != fcNone)
5230 InterestedSrcs |= fcZero | fcSubnormal;
5231 if ((InterestedClasses & fcNan) != fcNone)
5232 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5233
5234 KnownFPClass KnownSrc;
5235 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5236 KnownSrc, Q, Depth + 1);
5237
5238 if (KnownSrc.isKnownNeverPosInfinity())
5239 Known.knownNot(fcPosInf);
5240
5241 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5242 Known.knownNot(fcNan);
5243
5244 const Function *F = II->getFunction();
5245
5246 if (!F)
5247 break;
5248
5249 const fltSemantics &FltSem =
5250 II->getType()->getScalarType()->getFltSemantics();
5251 DenormalMode Mode = F->getDenormalMode(FltSem);
5252
5253 if (KnownSrc.isKnownNeverLogicalZero(Mode))
5254 Known.knownNot(fcNegInf);
5255
5256 break;
5257 }
5258 case Intrinsic::powi: {
5259 if ((InterestedClasses & fcNegative) == fcNone)
5260 break;
5261
5262 const Value *Exp = II->getArgOperand(1);
5263 Type *ExpTy = Exp->getType();
5264 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5265 KnownBits ExponentKnownBits(BitWidth);
5266 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5267 ExponentKnownBits, Q, Depth + 1);
5268
5269 if (ExponentKnownBits.Zero[0]) { // Is even
5270 Known.knownNot(fcNegative);
5271 break;
5272 }
5273
5274 // Given that exp is an integer, here are the
5275 // ways that pow can return a negative value:
5276 //
5277 // pow(-x, exp) --> negative if exp is odd and x is negative.
5278 // pow(-0, exp) --> -inf if exp is negative odd.
5279 // pow(-0, exp) --> -0 if exp is positive odd.
5280 // pow(-inf, exp) --> -0 if exp is negative odd.
5281 // pow(-inf, exp) --> -inf if exp is positive odd.
5282 KnownFPClass KnownSrc;
5283 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5284 KnownSrc, Q, Depth + 1);
5285 if (KnownSrc.isKnownNever(fcNegative))
5286 Known.knownNot(fcNegative);
5287 break;
5288 }
5289 case Intrinsic::ldexp: {
5290 KnownFPClass KnownSrc;
5291 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5292 KnownSrc, Q, Depth + 1);
5293 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5294
5295 // Sign is preserved, but underflows may produce zeroes.
5296 if (KnownSrc.isKnownNever(fcNegative))
5297 Known.knownNot(fcNegative);
5298 else if (KnownSrc.cannotBeOrderedLessThanZero())
5300
5301 if (KnownSrc.isKnownNever(fcPositive))
5302 Known.knownNot(fcPositive);
5303 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5305
5306 // Can refine inf/zero handling based on the exponent operand.
5307 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5308 if ((InterestedClasses & ExpInfoMask) == fcNone)
5309 break;
5310 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5311 break;
5312
5313 const fltSemantics &Flt =
5314 II->getType()->getScalarType()->getFltSemantics();
5315 unsigned Precision = APFloat::semanticsPrecision(Flt);
5316 const Value *ExpArg = II->getArgOperand(1);
5318 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5319
5320 const int MantissaBits = Precision - 1;
5321 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5322 Known.knownNot(fcSubnormal);
5323
5324 const Function *F = II->getFunction();
5325 const APInt *ConstVal = ExpRange.getSingleElement();
5326 const fltSemantics &FltSem =
5327 II->getType()->getScalarType()->getFltSemantics();
5328 if (ConstVal && ConstVal->isZero()) {
5329 // ldexp(x, 0) -> x, so propagate everything.
5330 Known.propagateCanonicalizingSrc(KnownSrc, F->getDenormalMode(FltSem));
5331 } else if (ExpRange.isAllNegative()) {
5332 // If we know the power is <= 0, can't introduce inf
5333 if (KnownSrc.isKnownNeverPosInfinity())
5334 Known.knownNot(fcPosInf);
5335 if (KnownSrc.isKnownNeverNegInfinity())
5336 Known.knownNot(fcNegInf);
5337 } else if (ExpRange.isAllNonNegative()) {
5338 // If we know the power is >= 0, can't introduce subnormal or zero
5339 if (KnownSrc.isKnownNeverPosSubnormal())
5340 Known.knownNot(fcPosSubnormal);
5341 if (KnownSrc.isKnownNeverNegSubnormal())
5342 Known.knownNot(fcNegSubnormal);
5343 if (F &&
5344 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5345 Known.knownNot(fcPosZero);
5346 if (F &&
5347 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5348 Known.knownNot(fcNegZero);
5349 }
5350
5351 break;
5352 }
5353 case Intrinsic::arithmetic_fence: {
5354 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5355 Known, Q, Depth + 1);
5356 break;
5357 }
5358 case Intrinsic::experimental_constrained_sitofp:
5359 case Intrinsic::experimental_constrained_uitofp:
5360 // Cannot produce nan
5361 Known.knownNot(fcNan);
5362
5363 // sitofp and uitofp turn into +0.0 for zero.
5364 Known.knownNot(fcNegZero);
5365
5366 // Integers cannot be subnormal
5367 Known.knownNot(fcSubnormal);
5368
5369 if (IID == Intrinsic::experimental_constrained_uitofp)
5370 Known.signBitMustBeZero();
5371
5372 // TODO: Copy inf handling from instructions
5373 break;
5374 default:
5375 break;
5376 }
5377
5378 break;
5379 }
5380 case Instruction::FAdd:
5381 case Instruction::FSub: {
5382 KnownFPClass KnownLHS, KnownRHS;
5383 bool WantNegative =
5384 Op->getOpcode() == Instruction::FAdd &&
5385 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5386 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5387 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5388
5389 if (!WantNaN && !WantNegative && !WantNegZero)
5390 break;
5391
5392 FPClassTest InterestedSrcs = InterestedClasses;
5393 if (WantNegative)
5394 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5395 if (InterestedClasses & fcNan)
5396 InterestedSrcs |= fcInf;
5397 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5398 KnownRHS, Q, Depth + 1);
5399
5400 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5401 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5402 WantNegZero || Opc == Instruction::FSub) {
5403
5404 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5405 // there's no point.
5406 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5407 KnownLHS, Q, Depth + 1);
5408 // Adding positive and negative infinity produces NaN.
5409 // TODO: Check sign of infinities.
5410 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5411 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5412 Known.knownNot(fcNan);
5413
5414 // FIXME: Context function should always be passed in separately
5415 const Function *F = cast<Instruction>(Op)->getFunction();
5416
5417 if (Op->getOpcode() == Instruction::FAdd) {
5418 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5419 KnownRHS.cannotBeOrderedLessThanZero())
5421 if (!F)
5422 break;
5423
5424 const fltSemantics &FltSem =
5425 Op->getType()->getScalarType()->getFltSemantics();
5426 DenormalMode Mode = F->getDenormalMode(FltSem);
5427
5428 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5429 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5430 KnownRHS.isKnownNeverLogicalNegZero(Mode)) &&
5431 // Make sure output negative denormal can't flush to -0
5432 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5433 Known.knownNot(fcNegZero);
5434 } else {
5435 if (!F)
5436 break;
5437
5438 const fltSemantics &FltSem =
5439 Op->getType()->getScalarType()->getFltSemantics();
5440 DenormalMode Mode = F->getDenormalMode(FltSem);
5441
5442 // Only fsub -0, +0 can return -0
5443 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5444 KnownRHS.isKnownNeverLogicalPosZero(Mode)) &&
5445 // Make sure output negative denormal can't flush to -0
5446 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5447 Known.knownNot(fcNegZero);
5448 }
5449 }
5450
5451 break;
5452 }
5453 case Instruction::FMul: {
5454 // X * X is always non-negative or a NaN.
5455 if (Op->getOperand(0) == Op->getOperand(1))
5456 Known.knownNot(fcNegative);
5457
5458 if ((InterestedClasses & fcNan) != fcNan)
5459 break;
5460
5461 // fcSubnormal is only needed in case of DAZ.
5462 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5463
5464 KnownFPClass KnownLHS, KnownRHS;
5465 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5466 Q, Depth + 1);
5467 if (!KnownRHS.isKnownNeverNaN())
5468 break;
5469
5470 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5471 Q, Depth + 1);
5472 if (!KnownLHS.isKnownNeverNaN())
5473 break;
5474
5475 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5476 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5477 Known.signBitMustBeZero();
5478 else
5479 Known.signBitMustBeOne();
5480 }
5481
5482 // If 0 * +/-inf produces NaN.
5483 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5484 Known.knownNot(fcNan);
5485 break;
5486 }
5487
5488 const Function *F = cast<Instruction>(Op)->getFunction();
5489 if (!F)
5490 break;
5491
5492 Type *OpTy = Op->getType()->getScalarType();
5493 const fltSemantics &FltSem = OpTy->getFltSemantics();
5494 DenormalMode Mode = F->getDenormalMode(FltSem);
5495
5496 if ((KnownRHS.isKnownNeverInfinity() ||
5497 KnownLHS.isKnownNeverLogicalZero(Mode)) &&
5498 (KnownLHS.isKnownNeverInfinity() ||
5499 KnownRHS.isKnownNeverLogicalZero(Mode)))
5500 Known.knownNot(fcNan);
5501
5502 break;
5503 }
5504 case Instruction::FDiv:
5505 case Instruction::FRem: {
5506 if (Op->getOperand(0) == Op->getOperand(1)) {
5507 // TODO: Could filter out snan if we inspect the operand
5508 if (Op->getOpcode() == Instruction::FDiv) {
5509 // X / X is always exactly 1.0 or a NaN.
5511 } else {
5512 // X % X is always exactly [+-]0.0 or a NaN.
5513 Known.KnownFPClasses = fcNan | fcZero;
5514 }
5515
5516 break;
5517 }
5518
5519 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5520 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5521 const bool WantPositive =
5522 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5523 if (!WantNan && !WantNegative && !WantPositive)
5524 break;
5525
5526 KnownFPClass KnownLHS, KnownRHS;
5527
5528 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5529 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5530 Depth + 1);
5531
5532 bool KnowSomethingUseful =
5533 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5534
5535 if (KnowSomethingUseful || WantPositive) {
5536 const FPClassTest InterestedLHS =
5537 WantPositive ? fcAllFlags
5539
5540 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5541 InterestedClasses & InterestedLHS, KnownLHS, Q,
5542 Depth + 1);
5543 }
5544
5545 const Function *F = cast<Instruction>(Op)->getFunction();
5546 const fltSemantics &FltSem =
5547 Op->getType()->getScalarType()->getFltSemantics();
5548
5549 if (Op->getOpcode() == Instruction::FDiv) {
5550 // Only 0/0, Inf/Inf produce NaN.
5551 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5552 (KnownLHS.isKnownNeverInfinity() ||
5553 KnownRHS.isKnownNeverInfinity()) &&
5554 ((F &&
5555 KnownLHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) ||
5556 (F &&
5557 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))))) {
5558 Known.knownNot(fcNan);
5559 }
5560
5561 // X / -0.0 is -Inf (or NaN).
5562 // +X / +X is +X
5563 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5564 Known.knownNot(fcNegative);
5565 } else {
5566 // Inf REM x and x REM 0 produce NaN.
5567 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5568 KnownLHS.isKnownNeverInfinity() && F &&
5569 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5570 Known.knownNot(fcNan);
5571 }
5572
5573 // The sign for frem is the same as the first operand.
5574 if (KnownLHS.cannotBeOrderedLessThanZero())
5576 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5578
5579 // See if we can be more aggressive about the sign of 0.
5580 if (KnownLHS.isKnownNever(fcNegative))
5581 Known.knownNot(fcNegative);
5582 if (KnownLHS.isKnownNever(fcPositive))
5583 Known.knownNot(fcPositive);
5584 }
5585
5586 break;
5587 }
5588 case Instruction::FPExt: {
5589 // Infinity, nan and zero propagate from source.
5590 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5591 Known, Q, Depth + 1);
5592
5593 const fltSemantics &DstTy =
5594 Op->getType()->getScalarType()->getFltSemantics();
5595 const fltSemantics &SrcTy =
5596 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5597
5598 // All subnormal inputs should be in the normal range in the result type.
5599 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5600 if (Known.KnownFPClasses & fcPosSubnormal)
5601 Known.KnownFPClasses |= fcPosNormal;
5602 if (Known.KnownFPClasses & fcNegSubnormal)
5603 Known.KnownFPClasses |= fcNegNormal;
5604 Known.knownNot(fcSubnormal);
5605 }
5606
5607 // Sign bit of a nan isn't guaranteed.
5608 if (!Known.isKnownNeverNaN())
5609 Known.SignBit = std::nullopt;
5610 break;
5611 }
5612 case Instruction::FPTrunc: {
5613 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5614 Depth);
5615 break;
5616 }
5617 case Instruction::SIToFP:
5618 case Instruction::UIToFP: {
5619 // Cannot produce nan
5620 Known.knownNot(fcNan);
5621
5622 // Integers cannot be subnormal
5623 Known.knownNot(fcSubnormal);
5624
5625 // sitofp and uitofp turn into +0.0 for zero.
5626 Known.knownNot(fcNegZero);
5627 if (Op->getOpcode() == Instruction::UIToFP)
5628 Known.signBitMustBeZero();
5629
5630 if (InterestedClasses & fcInf) {
5631 // Get width of largest magnitude integer (remove a bit if signed).
5632 // This still works for a signed minimum value because the largest FP
5633 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5634 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5635 if (Op->getOpcode() == Instruction::SIToFP)
5636 --IntSize;
5637
5638 // If the exponent of the largest finite FP value can hold the largest
5639 // integer, the result of the cast must be finite.
5640 Type *FPTy = Op->getType()->getScalarType();
5641 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5642 Known.knownNot(fcInf);
5643 }
5644
5645 break;
5646 }
5647 case Instruction::ExtractElement: {
5648 // Look through extract element. If the index is non-constant or
5649 // out-of-range demand all elements, otherwise just the extracted element.
5650 const Value *Vec = Op->getOperand(0);
5651
5652 APInt DemandedVecElts;
5653 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5654 unsigned NumElts = VecTy->getNumElements();
5655 DemandedVecElts = APInt::getAllOnes(NumElts);
5656 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5657 if (CIdx && CIdx->getValue().ult(NumElts))
5658 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5659 } else {
5660 DemandedVecElts = APInt(1, 1);
5661 }
5662
5663 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5664 Q, Depth + 1);
5665 }
5666 case Instruction::InsertElement: {
5667 if (isa<ScalableVectorType>(Op->getType()))
5668 return;
5669
5670 const Value *Vec = Op->getOperand(0);
5671 const Value *Elt = Op->getOperand(1);
5672 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5673 unsigned NumElts = DemandedElts.getBitWidth();
5674 APInt DemandedVecElts = DemandedElts;
5675 bool NeedsElt = true;
5676 // If we know the index we are inserting to, clear it from Vec check.
5677 if (CIdx && CIdx->getValue().ult(NumElts)) {
5678 DemandedVecElts.clearBit(CIdx->getZExtValue());
5679 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5680 }
5681
5682 // Do we demand the inserted element?
5683 if (NeedsElt) {
5684 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5685 // If we don't know any bits, early out.
5686 if (Known.isUnknown())
5687 break;
5688 } else {
5689 Known.KnownFPClasses = fcNone;
5690 }
5691
5692 // Do we need anymore elements from Vec?
5693 if (!DemandedVecElts.isZero()) {
5694 KnownFPClass Known2;
5695 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5696 Depth + 1);
5697 Known |= Known2;
5698 }
5699
5700 break;
5701 }
5702 case Instruction::ShuffleVector: {
5703 // For undef elements, we don't know anything about the common state of
5704 // the shuffle result.
5705 APInt DemandedLHS, DemandedRHS;
5706 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5707 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5708 return;
5709
5710 if (!!DemandedLHS) {
5711 const Value *LHS = Shuf->getOperand(0);
5712 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5713 Depth + 1);
5714
5715 // If we don't know any bits, early out.
5716 if (Known.isUnknown())
5717 break;
5718 } else {
5719 Known.KnownFPClasses = fcNone;
5720 }
5721
5722 if (!!DemandedRHS) {
5723 KnownFPClass Known2;
5724 const Value *RHS = Shuf->getOperand(1);
5725 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5726 Depth + 1);
5727 Known |= Known2;
5728 }
5729
5730 break;
5731 }
5732 case Instruction::ExtractValue: {
5733 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5734 ArrayRef<unsigned> Indices = Extract->getIndices();
5735 const Value *Src = Extract->getAggregateOperand();
5736 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5737 Indices[0] == 0) {
5738 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5739 switch (II->getIntrinsicID()) {
5740 case Intrinsic::frexp: {
5741 Known.knownNot(fcSubnormal);
5742
5743 KnownFPClass KnownSrc;
5744 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5745 InterestedClasses, KnownSrc, Q, Depth + 1);
5746
5747 const Function *F = cast<Instruction>(Op)->getFunction();
5748 const fltSemantics &FltSem =
5749 Op->getType()->getScalarType()->getFltSemantics();
5750
5751 if (KnownSrc.isKnownNever(fcNegative))
5752 Known.knownNot(fcNegative);
5753 else {
5754 if (F &&
5755 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5756 Known.knownNot(fcNegZero);
5757 if (KnownSrc.isKnownNever(fcNegInf))
5758 Known.knownNot(fcNegInf);
5759 }
5760
5761 if (KnownSrc.isKnownNever(fcPositive))
5762 Known.knownNot(fcPositive);
5763 else {
5764 if (F &&
5765 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5766 Known.knownNot(fcPosZero);
5767 if (KnownSrc.isKnownNever(fcPosInf))
5768 Known.knownNot(fcPosInf);
5769 }
5770
5771 Known.propagateNaN(KnownSrc);
5772 return;
5773 }
5774 default:
5775 break;
5776 }
5777 }
5778 }
5779
5780 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
5781 Depth + 1);
5782 break;
5783 }
5784 case Instruction::PHI: {
5785 const PHINode *P = cast<PHINode>(Op);
5786 // Unreachable blocks may have zero-operand PHI nodes.
5787 if (P->getNumIncomingValues() == 0)
5788 break;
5789
5790 // Otherwise take the unions of the known bit sets of the operands,
5791 // taking conservative care to avoid excessive recursion.
5792 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5793
5794 if (Depth < PhiRecursionLimit) {
5795 // Skip if every incoming value references to ourself.
5796 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5797 break;
5798
5799 bool First = true;
5800
5801 for (const Use &U : P->operands()) {
5802 Value *IncValue;
5803 Instruction *CxtI;
5804 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
5805 // Skip direct self references.
5806 if (IncValue == P)
5807 continue;
5808
5809 KnownFPClass KnownSrc;
5810 // Recurse, but cap the recursion to two levels, because we don't want
5811 // to waste time spinning around in loops. We need at least depth 2 to
5812 // detect known sign bits.
5813 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
5815 PhiRecursionLimit);
5816
5817 if (First) {
5818 Known = KnownSrc;
5819 First = false;
5820 } else {
5821 Known |= KnownSrc;
5822 }
5823
5824 if (Known.KnownFPClasses == fcAllFlags)
5825 break;
5826 }
5827 }
5828
5829 break;
5830 }
5831 case Instruction::BitCast: {
5832 const Value *Src;
5833 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
5834 !Src->getType()->isIntOrIntVectorTy())
5835 break;
5836
5837 const Type *Ty = Op->getType()->getScalarType();
5838 KnownBits Bits(Ty->getScalarSizeInBits());
5839 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
5840
5841 // Transfer information from the sign bit.
5842 if (Bits.isNonNegative())
5843 Known.signBitMustBeZero();
5844 else if (Bits.isNegative())
5845 Known.signBitMustBeOne();
5846
5847 if (Ty->isIEEELikeFPTy()) {
5848 // IEEE floats are NaN when all bits of the exponent plus at least one of
5849 // the fraction bits are 1. This means:
5850 // - If we assume unknown bits are 0 and the value is NaN, it will
5851 // always be NaN
5852 // - If we assume unknown bits are 1 and the value is not NaN, it can
5853 // never be NaN
5854 // Note: They do not hold for x86_fp80 format.
5855 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
5856 Known.KnownFPClasses = fcNan;
5857 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
5858 Known.knownNot(fcNan);
5859
5860 // Build KnownBits representing Inf and check if it must be equal or
5861 // unequal to this value.
5862 auto InfKB = KnownBits::makeConstant(
5863 APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());
5864 InfKB.Zero.clearSignBit();
5865 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
5866 assert(!InfResult.value());
5867 Known.knownNot(fcInf);
5868 } else if (Bits == InfKB) {
5869 Known.KnownFPClasses = fcInf;
5870 }
5871
5872 // Build KnownBits representing Zero and check if it must be equal or
5873 // unequal to this value.
5874 auto ZeroKB = KnownBits::makeConstant(
5875 APFloat::getZero(Ty->getFltSemantics()).bitcastToAPInt());
5876 ZeroKB.Zero.clearSignBit();
5877 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
5878 assert(!ZeroResult.value());
5879 Known.knownNot(fcZero);
5880 } else if (Bits == ZeroKB) {
5881 Known.KnownFPClasses = fcZero;
5882 }
5883 }
5884
5885 break;
5886 }
5887 default:
5888 break;
5889 }
5890}
5891
5893 const APInt &DemandedElts,
5894 FPClassTest InterestedClasses,
5895 const SimplifyQuery &SQ,
5896 unsigned Depth) {
5897 KnownFPClass KnownClasses;
5898 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
5899 Depth);
5900 return KnownClasses;
5901}
5902
5904 FPClassTest InterestedClasses,
5905 const SimplifyQuery &SQ,
5906 unsigned Depth) {
5907 KnownFPClass Known;
5908 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
5909 return Known;
5910}
5911
5913 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
5914 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
5915 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
5916 return computeKnownFPClass(V, InterestedClasses,
5917 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
5918 Depth);
5919}
5920
5922llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5923 FastMathFlags FMF, FPClassTest InterestedClasses,
5924 const SimplifyQuery &SQ, unsigned Depth) {
5925 if (FMF.noNaNs())
5926 InterestedClasses &= ~fcNan;
5927 if (FMF.noInfs())
5928 InterestedClasses &= ~fcInf;
5929
5930 KnownFPClass Result =
5931 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
5932
5933 if (FMF.noNaNs())
5934 Result.KnownFPClasses &= ~fcNan;
5935 if (FMF.noInfs())
5936 Result.KnownFPClasses &= ~fcInf;
5937 return Result;
5938}
5939
5941 FPClassTest InterestedClasses,
5942 const SimplifyQuery &SQ,
5943 unsigned Depth) {
5944 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5945 APInt DemandedElts =
5946 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5947 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
5948 Depth);
5949}
5950
5952 unsigned Depth) {
5954 return Known.isKnownNeverNegZero();
5955}
5956
5963
5965 unsigned Depth) {
5967 return Known.isKnownNeverInfinity();
5968}
5969
5970/// Return true if the floating-point value can never contain a NaN or infinity.
5972 unsigned Depth) {
5974 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
5975}
5976
5977/// Return true if the floating-point scalar value is not a NaN or if the
5978/// floating-point vector value has no NaN elements. Return false if a value
5979/// could ever be NaN.
5981 unsigned Depth) {
5983 return Known.isKnownNeverNaN();
5984}
5985
5986/// Return false if we can prove that the specified FP value's sign bit is 0.
5987/// Return true if we can prove that the specified FP value's sign bit is 1.
5988/// Otherwise return std::nullopt.
5989std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
5990 const SimplifyQuery &SQ,
5991 unsigned Depth) {
5993 return Known.SignBit;
5994}
5995
5997 auto *User = cast<Instruction>(U.getUser());
5998 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
5999 if (FPOp->hasNoSignedZeros())
6000 return true;
6001 }
6002
6003 switch (User->getOpcode()) {
6004 case Instruction::FPToSI:
6005 case Instruction::FPToUI:
6006 return true;
6007 case Instruction::FCmp:
6008 // fcmp treats both positive and negative zero as equal.
6009 return true;
6010 case Instruction::Call:
6011 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6012 switch (II->getIntrinsicID()) {
6013 case Intrinsic::fabs:
6014 return true;
6015 case Intrinsic::copysign:
6016 return U.getOperandNo() == 0;
6017 case Intrinsic::is_fpclass:
6018 case Intrinsic::vp_is_fpclass: {
6019 auto Test =
6020 static_cast<FPClassTest>(
6021 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6024 }
6025 default:
6026 return false;
6027 }
6028 }
6029 return false;
6030 default:
6031 return false;
6032 }
6033}
6034
6036 auto *User = cast<Instruction>(U.getUser());
6037 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6038 if (FPOp->hasNoNaNs())
6039 return true;
6040 }
6041
6042 switch (User->getOpcode()) {
6043 case Instruction::FPToSI:
6044 case Instruction::FPToUI:
6045 return true;
6046 // Proper FP math operations ignore the sign bit of NaN.
6047 case Instruction::FAdd:
6048 case Instruction::FSub:
6049 case Instruction::FMul:
6050 case Instruction::FDiv:
6051 case Instruction::FRem:
6052 case Instruction::FPTrunc:
6053 case Instruction::FPExt:
6054 case Instruction::FCmp:
6055 return true;
6056 // Bitwise FP operations should preserve the sign bit of NaN.
6057 case Instruction::FNeg:
6058 case Instruction::Select:
6059 case Instruction::PHI:
6060 return false;
6061 case Instruction::Ret:
6062 return User->getFunction()->getAttributes().getRetNoFPClass() &
6064 case Instruction::Call:
6065 case Instruction::Invoke: {
6066 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6067 switch (II->getIntrinsicID()) {
6068 case Intrinsic::fabs:
6069 return true;
6070 case Intrinsic::copysign:
6071 return U.getOperandNo() == 0;
6072 // Other proper FP math intrinsics ignore the sign bit of NaN.
6073 case Intrinsic::maxnum:
6074 case Intrinsic::minnum:
6075 case Intrinsic::maximum:
6076 case Intrinsic::minimum:
6077 case Intrinsic::maximumnum:
6078 case Intrinsic::minimumnum:
6079 case Intrinsic::canonicalize:
6080 case Intrinsic::fma:
6081 case Intrinsic::fmuladd:
6082 case Intrinsic::sqrt:
6083 case Intrinsic::pow:
6084 case Intrinsic::powi:
6085 case Intrinsic::fptoui_sat:
6086 case Intrinsic::fptosi_sat:
6087 case Intrinsic::is_fpclass:
6088 case Intrinsic::vp_is_fpclass:
6089 return true;
6090 default:
6091 return false;
6092 }
6093 }
6094
6095 FPClassTest NoFPClass =
6096 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6097 return NoFPClass & FPClassTest::fcNan;
6098 }
6099 default:
6100 return false;
6101 }
6102}
6103
6105
6106 // All byte-wide stores are splatable, even of arbitrary variables.
6107 if (V->getType()->isIntegerTy(8))
6108 return V;
6109
6110 LLVMContext &Ctx = V->getContext();
6111
6112 // Undef don't care.
6113 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6114 if (isa<UndefValue>(V))
6115 return UndefInt8;
6116
6117 // Return poison for zero-sized type.
6118 if (DL.getTypeStoreSize(V->getType()).isZero())
6119 return PoisonValue::get(Type::getInt8Ty(Ctx));
6120
6122 if (!C) {
6123 // Conceptually, we could handle things like:
6124 // %a = zext i8 %X to i16
6125 // %b = shl i16 %a, 8
6126 // %c = or i16 %a, %b
6127 // but until there is an example that actually needs this, it doesn't seem
6128 // worth worrying about.
6129 return nullptr;
6130 }
6131
6132 // Handle 'null' ConstantArrayZero etc.
6133 if (C->isNullValue())
6135
6136 // Constant floating-point values can be handled as integer values if the
6137 // corresponding integer value is "byteable". An important case is 0.0.
6138 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6139 Type *Ty = nullptr;
6140 if (CFP->getType()->isHalfTy())
6141 Ty = Type::getInt16Ty(Ctx);
6142 else if (CFP->getType()->isFloatTy())
6143 Ty = Type::getInt32Ty(Ctx);
6144 else if (CFP->getType()->isDoubleTy())
6145 Ty = Type::getInt64Ty(Ctx);
6146 // Don't handle long double formats, which have strange constraints.
6147 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6148 : nullptr;
6149 }
6150
6151 // We can handle constant integers that are multiple of 8 bits.
6152 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6153 if (CI->getBitWidth() % 8 == 0) {
6154 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6155 if (!CI->getValue().isSplat(8))
6156 return nullptr;
6157 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6158 }
6159 }
6160
6161 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6162 if (CE->getOpcode() == Instruction::IntToPtr) {
6163 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6164 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6166 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6167 return isBytewiseValue(Op, DL);
6168 }
6169 }
6170 }
6171
6172 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6173 if (LHS == RHS)
6174 return LHS;
6175 if (!LHS || !RHS)
6176 return nullptr;
6177 if (LHS == UndefInt8)
6178 return RHS;
6179 if (RHS == UndefInt8)
6180 return LHS;
6181 return nullptr;
6182 };
6183
6185 Value *Val = UndefInt8;
6186 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6187 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6188 return nullptr;
6189 return Val;
6190 }
6191
6193 Value *Val = UndefInt8;
6194 for (Value *Op : C->operands())
6195 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6196 return nullptr;
6197 return Val;
6198 }
6199
6200 // Don't try to handle the handful of other constants.
6201 return nullptr;
6202}
6203
6204// This is the recursive version of BuildSubAggregate. It takes a few different
6205// arguments. Idxs is the index within the nested struct From that we are
6206// looking at now (which is of type IndexedType). IdxSkip is the number of
6207// indices from Idxs that should be left out when inserting into the resulting
6208// struct. To is the result struct built so far, new insertvalue instructions
6209// build on that.
6210static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6212 unsigned IdxSkip,
6213 BasicBlock::iterator InsertBefore) {
6214 StructType *STy = dyn_cast<StructType>(IndexedType);
6215 if (STy) {
6216 // Save the original To argument so we can modify it
6217 Value *OrigTo = To;
6218 // General case, the type indexed by Idxs is a struct
6219 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6220 // Process each struct element recursively
6221 Idxs.push_back(i);
6222 Value *PrevTo = To;
6223 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6224 InsertBefore);
6225 Idxs.pop_back();
6226 if (!To) {
6227 // Couldn't find any inserted value for this index? Cleanup
6228 while (PrevTo != OrigTo) {
6230 PrevTo = Del->getAggregateOperand();
6231 Del->eraseFromParent();
6232 }
6233 // Stop processing elements
6234 break;
6235 }
6236 }
6237 // If we successfully found a value for each of our subaggregates
6238 if (To)
6239 return To;
6240 }
6241 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6242 // the struct's elements had a value that was inserted directly. In the latter
6243 // case, perhaps we can't determine each of the subelements individually, but
6244 // we might be able to find the complete struct somewhere.
6245
6246 // Find the value that is at that particular spot
6247 Value *V = FindInsertedValue(From, Idxs);
6248
6249 if (!V)
6250 return nullptr;
6251
6252 // Insert the value in the new (sub) aggregate
6253 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6254 InsertBefore);
6255}
6256
6257// This helper takes a nested struct and extracts a part of it (which is again a
6258// struct) into a new value. For example, given the struct:
6259// { a, { b, { c, d }, e } }
6260// and the indices "1, 1" this returns
6261// { c, d }.
6262//
6263// It does this by inserting an insertvalue for each element in the resulting
6264// struct, as opposed to just inserting a single struct. This will only work if
6265// each of the elements of the substruct are known (ie, inserted into From by an
6266// insertvalue instruction somewhere).
6267//
6268// All inserted insertvalue instructions are inserted before InsertBefore
6270 BasicBlock::iterator InsertBefore) {
6271 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6272 idx_range);
6273 Value *To = PoisonValue::get(IndexedType);
6274 SmallVector<unsigned, 10> Idxs(idx_range);
6275 unsigned IdxSkip = Idxs.size();
6276
6277 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6278}
6279
6280/// Given an aggregate and a sequence of indices, see if the scalar value
6281/// indexed is already around as a register, for example if it was inserted
6282/// directly into the aggregate.
6283///
6284/// If InsertBefore is not null, this function will duplicate (modified)
6285/// insertvalues when a part of a nested struct is extracted.
6286Value *
6288 std::optional<BasicBlock::iterator> InsertBefore) {
6289 // Nothing to index? Just return V then (this is useful at the end of our
6290 // recursion).
6291 if (idx_range.empty())
6292 return V;
6293 // We have indices, so V should have an indexable type.
6294 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6295 "Not looking at a struct or array?");
6296 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6297 "Invalid indices for type?");
6298
6299 if (Constant *C = dyn_cast<Constant>(V)) {
6300 C = C->getAggregateElement(idx_range[0]);
6301 if (!C) return nullptr;
6302 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6303 }
6304
6306 // Loop the indices for the insertvalue instruction in parallel with the
6307 // requested indices
6308 const unsigned *req_idx = idx_range.begin();
6309 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6310 i != e; ++i, ++req_idx) {
6311 if (req_idx == idx_range.end()) {
6312 // We can't handle this without inserting insertvalues
6313 if (!InsertBefore)
6314 return nullptr;
6315
6316 // The requested index identifies a part of a nested aggregate. Handle
6317 // this specially. For example,
6318 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6319 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6320 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6321 // This can be changed into
6322 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6323 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6324 // which allows the unused 0,0 element from the nested struct to be
6325 // removed.
6326 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6327 *InsertBefore);
6328 }
6329
6330 // This insert value inserts something else than what we are looking for.
6331 // See if the (aggregate) value inserted into has the value we are
6332 // looking for, then.
6333 if (*req_idx != *i)
6334 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6335 InsertBefore);
6336 }
6337 // If we end up here, the indices of the insertvalue match with those
6338 // requested (though possibly only partially). Now we recursively look at
6339 // the inserted value, passing any remaining indices.
6340 return FindInsertedValue(I->getInsertedValueOperand(),
6341 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6342 }
6343
6345 // If we're extracting a value from an aggregate that was extracted from
6346 // something else, we can extract from that something else directly instead.
6347 // However, we will need to chain I's indices with the requested indices.
6348
6349 // Calculate the number of indices required
6350 unsigned size = I->getNumIndices() + idx_range.size();
6351 // Allocate some space to put the new indices in
6353 Idxs.reserve(size);
6354 // Add indices from the extract value instruction
6355 Idxs.append(I->idx_begin(), I->idx_end());
6356
6357 // Add requested indices
6358 Idxs.append(idx_range.begin(), idx_range.end());
6359
6360 assert(Idxs.size() == size
6361 && "Number of indices added not correct?");
6362
6363 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6364 }
6365 // Otherwise, we don't know (such as, extracting from a function return value
6366 // or load instruction)
6367 return nullptr;
6368}
6369
6370// If V refers to an initialized global constant, set Slice either to
6371// its initializer if the size of its elements equals ElementSize, or,
6372// for ElementSize == 8, to its representation as an array of unsiged
6373// char. Return true on success.
6374// Offset is in the unit "nr of ElementSize sized elements".
6377 unsigned ElementSize, uint64_t Offset) {
6378 assert(V && "V should not be null.");
6379 assert((ElementSize % 8) == 0 &&
6380 "ElementSize expected to be a multiple of the size of a byte.");
6381 unsigned ElementSizeInBytes = ElementSize / 8;
6382
6383 // Drill down into the pointer expression V, ignoring any intervening
6384 // casts, and determine the identity of the object it references along
6385 // with the cumulative byte offset into it.
6386 const GlobalVariable *GV =
6388 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6389 // Fail if V is not based on constant global object.
6390 return false;
6391
6392 const DataLayout &DL = GV->getDataLayout();
6393 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6394
6395 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6396 /*AllowNonInbounds*/ true))
6397 // Fail if a constant offset could not be determined.
6398 return false;
6399
6400 uint64_t StartIdx = Off.getLimitedValue();
6401 if (StartIdx == UINT64_MAX)
6402 // Fail if the constant offset is excessive.
6403 return false;
6404
6405 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6406 // elements. Simply bail out if that isn't possible.
6407 if ((StartIdx % ElementSizeInBytes) != 0)
6408 return false;
6409
6410 Offset += StartIdx / ElementSizeInBytes;
6411 ConstantDataArray *Array = nullptr;
6412 ArrayType *ArrayTy = nullptr;
6413
6414 if (GV->getInitializer()->isNullValue()) {
6415 Type *GVTy = GV->getValueType();
6416 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6417 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6418
6419 Slice.Array = nullptr;
6420 Slice.Offset = 0;
6421 // Return an empty Slice for undersized constants to let callers
6422 // transform even undefined library calls into simpler, well-defined
6423 // expressions. This is preferable to making the calls although it
6424 // prevents sanitizers from detecting such calls.
6425 Slice.Length = Length < Offset ? 0 : Length - Offset;
6426 return true;
6427 }
6428
6429 auto *Init = const_cast<Constant *>(GV->getInitializer());
6430 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6431 Type *InitElTy = ArrayInit->getElementType();
6432 if (InitElTy->isIntegerTy(ElementSize)) {
6433 // If Init is an initializer for an array of the expected type
6434 // and size, use it as is.
6435 Array = ArrayInit;
6436 ArrayTy = ArrayInit->getType();
6437 }
6438 }
6439
6440 if (!Array) {
6441 if (ElementSize != 8)
6442 // TODO: Handle conversions to larger integral types.
6443 return false;
6444
6445 // Otherwise extract the portion of the initializer starting
6446 // at Offset as an array of bytes, and reset Offset.
6448 if (!Init)
6449 return false;
6450
6451 Offset = 0;
6453 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6454 }
6455
6456 uint64_t NumElts = ArrayTy->getArrayNumElements();
6457 if (Offset > NumElts)
6458 return false;
6459
6460 Slice.Array = Array;
6461 Slice.Offset = Offset;
6462 Slice.Length = NumElts - Offset;
6463 return true;
6464}
6465
6466/// Extract bytes from the initializer of the constant array V, which need
6467/// not be a nul-terminated string. On success, store the bytes in Str and
6468/// return true. When TrimAtNul is set, Str will contain only the bytes up
6469/// to but not including the first nul. Return false on failure.
6471 bool TrimAtNul) {
6473 if (!getConstantDataArrayInfo(V, Slice, 8))
6474 return false;
6475
6476 if (Slice.Array == nullptr) {
6477 if (TrimAtNul) {
6478 // Return a nul-terminated string even for an empty Slice. This is
6479 // safe because all existing SimplifyLibcalls callers require string
6480 // arguments and the behavior of the functions they fold is undefined
6481 // otherwise. Folding the calls this way is preferable to making
6482 // the undefined library calls, even though it prevents sanitizers
6483 // from reporting such calls.
6484 Str = StringRef();
6485 return true;
6486 }
6487 if (Slice.Length == 1) {
6488 Str = StringRef("", 1);
6489 return true;
6490 }
6491 // We cannot instantiate a StringRef as we do not have an appropriate string
6492 // of 0s at hand.
6493 return false;
6494 }
6495
6496 // Start out with the entire array in the StringRef.
6497 Str = Slice.Array->getAsString();
6498 // Skip over 'offset' bytes.
6499 Str = Str.substr(Slice.Offset);
6500
6501 if (TrimAtNul) {
6502 // Trim off the \0 and anything after it. If the array is not nul
6503 // terminated, we just return the whole end of string. The client may know
6504 // some other way that the string is length-bound.
6505 Str = Str.substr(0, Str.find('\0'));
6506 }
6507 return true;
6508}
6509
6510// These next two are very similar to the above, but also look through PHI
6511// nodes.
6512// TODO: See if we can integrate these two together.
6513
6514/// If we can compute the length of the string pointed to by
6515/// the specified pointer, return 'len+1'. If we can't, return 0.
6518 unsigned CharSize) {
6519 // Look through noop bitcast instructions.
6520 V = V->stripPointerCasts();
6521
6522 // If this is a PHI node, there are two cases: either we have already seen it
6523 // or we haven't.
6524 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6525 if (!PHIs.insert(PN).second)
6526 return ~0ULL; // already in the set.
6527
6528 // If it was new, see if all the input strings are the same length.
6529 uint64_t LenSoFar = ~0ULL;
6530 for (Value *IncValue : PN->incoming_values()) {
6531 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6532 if (Len == 0) return 0; // Unknown length -> unknown.
6533
6534 if (Len == ~0ULL) continue;
6535
6536 if (Len != LenSoFar && LenSoFar != ~0ULL)
6537 return 0; // Disagree -> unknown.
6538 LenSoFar = Len;
6539 }
6540
6541 // Success, all agree.
6542 return LenSoFar;
6543 }
6544
6545 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6546 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6547 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6548 if (Len1 == 0) return 0;
6549 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6550 if (Len2 == 0) return 0;
6551 if (Len1 == ~0ULL) return Len2;
6552 if (Len2 == ~0ULL) return Len1;
6553 if (Len1 != Len2) return 0;
6554 return Len1;
6555 }
6556
6557 // Otherwise, see if we can read the string.
6559 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6560 return 0;
6561
6562 if (Slice.Array == nullptr)
6563 // Zeroinitializer (including an empty one).
6564 return 1;
6565
6566 // Search for the first nul character. Return a conservative result even
6567 // when there is no nul. This is safe since otherwise the string function
6568 // being folded such as strlen is undefined, and can be preferable to
6569 // making the undefined library call.
6570 unsigned NullIndex = 0;
6571 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6572 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6573 break;
6574 }
6575
6576 return NullIndex + 1;
6577}
6578
6579/// If we can compute the length of the string pointed to by
6580/// the specified pointer, return 'len+1'. If we can't, return 0.
6581uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6582 if (!V->getType()->isPointerTy())
6583 return 0;
6584
6586 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6587 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6588 // an empty string as a length.
6589 return Len == ~0ULL ? 1 : Len;
6590}
6591
6592const Value *
6594 bool MustPreserveNullness) {
6595 assert(Call &&
6596 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6597 if (const Value *RV = Call->getReturnedArgOperand())
6598 return RV;
6599 // This can be used only as a aliasing property.
6601 Call, MustPreserveNullness))
6602 return Call->getArgOperand(0);
6603 return nullptr;
6604}
6605
6607 const CallBase *Call, bool MustPreserveNullness) {
6608 switch (Call->getIntrinsicID()) {
6609 case Intrinsic::launder_invariant_group:
6610 case Intrinsic::strip_invariant_group:
6611 case Intrinsic::aarch64_irg:
6612 case Intrinsic::aarch64_tagp:
6613 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6614 // input pointer (and thus preserve null-ness for the purposes of escape
6615 // analysis, which is where the MustPreserveNullness flag comes in to play).
6616 // However, it will not necessarily map ptr addrspace(N) null to ptr
6617 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6618 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6619 // list, no one should be relying on such a strict interpretation of
6620 // MustPreserveNullness (and, at time of writing, they are not), but we
6621 // document this fact out of an abundance of caution.
6622 case Intrinsic::amdgcn_make_buffer_rsrc:
6623 return true;
6624 case Intrinsic::ptrmask:
6625 return !MustPreserveNullness;
6626 case Intrinsic::threadlocal_address:
6627 // The underlying variable changes with thread ID. The Thread ID may change
6628 // at coroutine suspend points.
6629 return !Call->getParent()->getParent()->isPresplitCoroutine();
6630 default:
6631 return false;
6632 }
6633}
6634
6635/// \p PN defines a loop-variant pointer to an object. Check if the
6636/// previous iteration of the loop was referring to the same object as \p PN.
6638 const LoopInfo *LI) {
6639 // Find the loop-defined value.
6640 Loop *L = LI->getLoopFor(PN->getParent());
6641 if (PN->getNumIncomingValues() != 2)
6642 return true;
6643
6644 // Find the value from previous iteration.
6645 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6646 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6647 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6648 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6649 return true;
6650
6651 // If a new pointer is loaded in the loop, the pointer references a different
6652 // object in every iteration. E.g.:
6653 // for (i)
6654 // int *p = a[i];
6655 // ...
6656 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6657 if (!L->isLoopInvariant(Load->getPointerOperand()))
6658 return false;
6659 return true;
6660}
6661
6662const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6663 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6664 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6665 const Value *PtrOp = GEP->getPointerOperand();
6666 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6667 return V;
6668 V = PtrOp;
6669 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6670 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6671 Value *NewV = cast<Operator>(V)->getOperand(0);
6672 if (!NewV->getType()->isPointerTy())
6673 return V;
6674 V = NewV;
6675 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6676 if (GA->isInterposable())
6677 return V;
6678 V = GA->getAliasee();
6679 } else {
6680 if (auto *PHI = dyn_cast<PHINode>(V)) {
6681 // Look through single-arg phi nodes created by LCSSA.
6682 if (PHI->getNumIncomingValues() == 1) {
6683 V = PHI->getIncomingValue(0);
6684 continue;
6685 }
6686 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6687 // CaptureTracking can know about special capturing properties of some
6688 // intrinsics like launder.invariant.group, that can't be expressed with
6689 // the attributes, but have properties like returning aliasing pointer.
6690 // Because some analysis may assume that nocaptured pointer is not
6691 // returned from some special intrinsic (because function would have to
6692 // be marked with returns attribute), it is crucial to use this function
6693 // because it should be in sync with CaptureTracking. Not using it may
6694 // cause weird miscompilations where 2 aliasing pointers are assumed to
6695 // noalias.
6696 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6697 V = RP;
6698 continue;
6699 }
6700 }
6701
6702 return V;
6703 }
6704 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6705 }
6706 return V;
6707}
6708
6711 const LoopInfo *LI, unsigned MaxLookup) {
6714 Worklist.push_back(V);
6715 do {
6716 const Value *P = Worklist.pop_back_val();
6717 P = getUnderlyingObject(P, MaxLookup);
6718
6719 if (!Visited.insert(P).second)
6720 continue;
6721
6722 if (auto *SI = dyn_cast<SelectInst>(P)) {
6723 Worklist.push_back(SI->getTrueValue());
6724 Worklist.push_back(SI->getFalseValue());
6725 continue;
6726 }
6727
6728 if (auto *PN = dyn_cast<PHINode>(P)) {
6729 // If this PHI changes the underlying object in every iteration of the
6730 // loop, don't look through it. Consider:
6731 // int **A;
6732 // for (i) {
6733 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6734 // Curr = A[i];
6735 // *Prev, *Curr;
6736 //
6737 // Prev is tracking Curr one iteration behind so they refer to different
6738 // underlying objects.
6739 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6741 append_range(Worklist, PN->incoming_values());
6742 else
6743 Objects.push_back(P);
6744 continue;
6745 }
6746
6747 Objects.push_back(P);
6748 } while (!Worklist.empty());
6749}
6750
6752 const unsigned MaxVisited = 8;
6753
6756 Worklist.push_back(V);
6757 const Value *Object = nullptr;
6758 // Used as fallback if we can't find a common underlying object through
6759 // recursion.
6760 bool First = true;
6761 const Value *FirstObject = getUnderlyingObject(V);
6762 do {
6763 const Value *P = Worklist.pop_back_val();
6764 P = First ? FirstObject : getUnderlyingObject(P);
6765 First = false;
6766
6767 if (!Visited.insert(P).second)
6768 continue;
6769
6770 if (Visited.size() == MaxVisited)
6771 return FirstObject;
6772
6773 if (auto *SI = dyn_cast<SelectInst>(P)) {
6774 Worklist.push_back(SI->getTrueValue());
6775 Worklist.push_back(SI->getFalseValue());
6776 continue;
6777 }
6778
6779 if (auto *PN = dyn_cast<PHINode>(P)) {
6780 append_range(Worklist, PN->incoming_values());
6781 continue;
6782 }
6783
6784 if (!Object)
6785 Object = P;
6786 else if (Object != P)
6787 return FirstObject;
6788 } while (!Worklist.empty());
6789
6790 return Object ? Object : FirstObject;
6791}
6792
6793/// This is the function that does the work of looking through basic
6794/// ptrtoint+arithmetic+inttoptr sequences.
6795static const Value *getUnderlyingObjectFromInt(const Value *V) {
6796 do {
6797 if (const Operator *U = dyn_cast<Operator>(V)) {
6798 // If we find a ptrtoint, we can transfer control back to the
6799 // regular getUnderlyingObjectFromInt.
6800 if (U->getOpcode() == Instruction::PtrToInt)
6801 return U->getOperand(0);
6802 // If we find an add of a constant, a multiplied value, or a phi, it's
6803 // likely that the other operand will lead us to the base
6804 // object. We don't have to worry about the case where the
6805 // object address is somehow being computed by the multiply,
6806 // because our callers only care when the result is an
6807 // identifiable object.
6808 if (U->getOpcode() != Instruction::Add ||
6809 (!isa<ConstantInt>(U->getOperand(1)) &&
6810 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6811 !isa<PHINode>(U->getOperand(1))))
6812 return V;
6813 V = U->getOperand(0);
6814 } else {
6815 return V;
6816 }
6817 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6818 } while (true);
6819}
6820
6821/// This is a wrapper around getUnderlyingObjects and adds support for basic
6822/// ptrtoint+arithmetic+inttoptr sequences.
6823/// It returns false if unidentified object is found in getUnderlyingObjects.
6825 SmallVectorImpl<Value *> &Objects) {
6827 SmallVector<const Value *, 4> Working(1, V);
6828 do {
6829 V = Working.pop_back_val();
6830
6832 getUnderlyingObjects(V, Objs);
6833
6834 for (const Value *V : Objs) {
6835 if (!Visited.insert(V).second)
6836 continue;
6837 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6838 const Value *O =
6839 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6840 if (O->getType()->isPointerTy()) {
6841 Working.push_back(O);
6842 continue;
6843 }
6844 }
6845 // If getUnderlyingObjects fails to find an identifiable object,
6846 // getUnderlyingObjectsForCodeGen also fails for safety.
6847 if (!isIdentifiedObject(V)) {
6848 Objects.clear();
6849 return false;
6850 }
6851 Objects.push_back(const_cast<Value *>(V));
6852 }
6853 } while (!Working.empty());
6854 return true;
6855}
6856
6858 AllocaInst *Result = nullptr;
6860 SmallVector<Value *, 4> Worklist;
6861
6862 auto AddWork = [&](Value *V) {
6863 if (Visited.insert(V).second)
6864 Worklist.push_back(V);
6865 };
6866
6867 AddWork(V);
6868 do {
6869 V = Worklist.pop_back_val();
6870 assert(Visited.count(V));
6871
6872 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6873 if (Result && Result != AI)
6874 return nullptr;
6875 Result = AI;
6876 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
6877 AddWork(CI->getOperand(0));
6878 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
6879 for (Value *IncValue : PN->incoming_values())
6880 AddWork(IncValue);
6881 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
6882 AddWork(SI->getTrueValue());
6883 AddWork(SI->getFalseValue());
6885 if (OffsetZero && !GEP->hasAllZeroIndices())
6886 return nullptr;
6887 AddWork(GEP->getPointerOperand());
6888 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
6889 Value *Returned = CB->getReturnedArgOperand();
6890 if (Returned)
6891 AddWork(Returned);
6892 else
6893 return nullptr;
6894 } else {
6895 return nullptr;
6896 }
6897 } while (!Worklist.empty());
6898
6899 return Result;
6900}
6901
6903 const Value *V, bool AllowLifetime, bool AllowDroppable) {
6904 for (const User *U : V->users()) {
6906 if (!II)
6907 return false;
6908
6909 if (AllowLifetime && II->isLifetimeStartOrEnd())
6910 continue;
6911
6912 if (AllowDroppable && II->isDroppable())
6913 continue;
6914
6915 return false;
6916 }
6917 return true;
6918}
6919
6922 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
6923}
6926 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
6927}
6928
6930 if (auto *II = dyn_cast<IntrinsicInst>(I))
6931 return isTriviallyVectorizable(II->getIntrinsicID());
6932 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
6933 return (!Shuffle || Shuffle->isSelect()) &&
6935}
6936
6938 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
6939 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
6940 bool IgnoreUBImplyingAttrs) {
6941 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
6942 AC, DT, TLI, UseVariableInfo,
6943 IgnoreUBImplyingAttrs);
6944}
6945
6947 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
6948 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
6949 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
6950#ifndef NDEBUG
6951 if (Inst->getOpcode() != Opcode) {
6952 // Check that the operands are actually compatible with the Opcode override.
6953 auto hasEqualReturnAndLeadingOperandTypes =
6954 [](const Instruction *Inst, unsigned NumLeadingOperands) {
6955 if (Inst->getNumOperands() < NumLeadingOperands)
6956 return false;
6957 const Type *ExpectedType = Inst->getType();
6958 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
6959 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
6960 return false;
6961 return true;
6962 };
6964 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
6965 assert(!Instruction::isUnaryOp(Opcode) ||
6966 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
6967 }
6968#endif
6969
6970 switch (Opcode) {
6971 default:
6972 return true;
6973 case Instruction::UDiv:
6974 case Instruction::URem: {
6975 // x / y is undefined if y == 0.
6976 const APInt *V;
6977 if (match(Inst->getOperand(1), m_APInt(V)))
6978 return *V != 0;
6979 return false;
6980 }
6981 case Instruction::SDiv:
6982 case Instruction::SRem: {
6983 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
6984 const APInt *Numerator, *Denominator;
6985 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
6986 return false;
6987 // We cannot hoist this division if the denominator is 0.
6988 if (*Denominator == 0)
6989 return false;
6990 // It's safe to hoist if the denominator is not 0 or -1.
6991 if (!Denominator->isAllOnes())
6992 return true;
6993 // At this point we know that the denominator is -1. It is safe to hoist as
6994 // long we know that the numerator is not INT_MIN.
6995 if (match(Inst->getOperand(0), m_APInt(Numerator)))
6996 return !Numerator->isMinSignedValue();
6997 // The numerator *might* be MinSignedValue.
6998 return false;
6999 }
7000 case Instruction::Load: {
7001 if (!UseVariableInfo)
7002 return false;
7003
7004 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7005 if (!LI)
7006 return false;
7007 if (mustSuppressSpeculation(*LI))
7008 return false;
7009 const DataLayout &DL = LI->getDataLayout();
7011 LI->getType(), LI->getAlign(), DL,
7012 CtxI, AC, DT, TLI);
7013 }
7014 case Instruction::Call: {
7015 auto *CI = dyn_cast<const CallInst>(Inst);
7016 if (!CI)
7017 return false;
7018 const Function *Callee = CI->getCalledFunction();
7019
7020 // The called function could have undefined behavior or side-effects, even
7021 // if marked readnone nounwind.
7022 if (!Callee || !Callee->isSpeculatable())
7023 return false;
7024 // Since the operands may be changed after hoisting, undefined behavior may
7025 // be triggered by some UB-implying attributes.
7026 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7027 }
7028 case Instruction::VAArg:
7029 case Instruction::Alloca:
7030 case Instruction::Invoke:
7031 case Instruction::CallBr:
7032 case Instruction::PHI:
7033 case Instruction::Store:
7034 case Instruction::Ret:
7035 case Instruction::Br:
7036 case Instruction::IndirectBr:
7037 case Instruction::Switch:
7038 case Instruction::Unreachable:
7039 case Instruction::Fence:
7040 case Instruction::AtomicRMW:
7041 case Instruction::AtomicCmpXchg:
7042 case Instruction::LandingPad:
7043 case Instruction::Resume:
7044 case Instruction::CatchSwitch:
7045 case Instruction::CatchPad:
7046 case Instruction::CatchRet:
7047 case Instruction::CleanupPad:
7048 case Instruction::CleanupRet:
7049 return false; // Misc instructions which have effects
7050 }
7051}
7052
7054 if (I.mayReadOrWriteMemory())
7055 // Memory dependency possible
7056 return true;
7058 // Can't move above a maythrow call or infinite loop. Or if an
7059 // inalloca alloca, above a stacksave call.
7060 return true;
7062 // 1) Can't reorder two inf-loop calls, even if readonly
7063 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7064 // safe to speculative execute. (Inverse of above)
7065 return true;
7066 return false;
7067}
7068
7069/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7083
7084/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7087 bool ForSigned,
7088 const SimplifyQuery &SQ) {
7089 ConstantRange CR1 =
7090 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7091 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7094 return CR1.intersectWith(CR2, RangeType);
7095}
7096
7098 const Value *RHS,
7099 const SimplifyQuery &SQ,
7100 bool IsNSW) {
7101 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7102 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7103
7104 // mul nsw of two non-negative numbers is also nuw.
7105 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
7107
7108 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
7109 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
7110 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7111}
7112
7114 const Value *RHS,
7115 const SimplifyQuery &SQ) {
7116 // Multiplying n * m significant bits yields a result of n + m significant
7117 // bits. If the total number of significant bits does not exceed the
7118 // result bit width (minus 1), there is no overflow.
7119 // This means if we have enough leading sign bits in the operands
7120 // we can guarantee that the result does not overflow.
7121 // Ref: "Hacker's Delight" by Henry Warren
7122 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7123
7124 // Note that underestimating the number of sign bits gives a more
7125 // conservative answer.
7126 unsigned SignBits =
7127 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7128
7129 // First handle the easy case: if we have enough sign bits there's
7130 // definitely no overflow.
7131 if (SignBits > BitWidth + 1)
7133
7134 // There are two ambiguous cases where there can be no overflow:
7135 // SignBits == BitWidth + 1 and
7136 // SignBits == BitWidth
7137 // The second case is difficult to check, therefore we only handle the
7138 // first case.
7139 if (SignBits == BitWidth + 1) {
7140 // It overflows only when both arguments are negative and the true
7141 // product is exactly the minimum negative number.
7142 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7143 // For simplicity we just check if at least one side is not negative.
7144 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7145 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7146 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7148 }
7150}
7151
7154 const WithCache<const Value *> &RHS,
7155 const SimplifyQuery &SQ) {
7156 ConstantRange LHSRange =
7157 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7158 ConstantRange RHSRange =
7159 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7160 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7161}
7162
7163static OverflowResult
7166 const AddOperator *Add, const SimplifyQuery &SQ) {
7167 if (Add && Add->hasNoSignedWrap()) {
7169 }
7170
7171 // If LHS and RHS each have at least two sign bits, the addition will look
7172 // like
7173 //
7174 // XX..... +
7175 // YY.....
7176 //
7177 // If the carry into the most significant position is 0, X and Y can't both
7178 // be 1 and therefore the carry out of the addition is also 0.
7179 //
7180 // If the carry into the most significant position is 1, X and Y can't both
7181 // be 0 and therefore the carry out of the addition is also 1.
7182 //
7183 // Since the carry into the most significant position is always equal to
7184 // the carry out of the addition, there is no signed overflow.
7185 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7187
7188 ConstantRange LHSRange =
7189 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7190 ConstantRange RHSRange =
7191 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7192 OverflowResult OR =
7193 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7195 return OR;
7196
7197 // The remaining code needs Add to be available. Early returns if not so.
7198 if (!Add)
7200
7201 // If the sign of Add is the same as at least one of the operands, this add
7202 // CANNOT overflow. If this can be determined from the known bits of the
7203 // operands the above signedAddMayOverflow() check will have already done so.
7204 // The only other way to improve on the known bits is from an assumption, so
7205 // call computeKnownBitsFromContext() directly.
7206 bool LHSOrRHSKnownNonNegative =
7207 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7208 bool LHSOrRHSKnownNegative =
7209 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7210 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7211 KnownBits AddKnown(LHSRange.getBitWidth());
7212 computeKnownBitsFromContext(Add, AddKnown, SQ);
7213 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7214 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7216 }
7217
7219}
7220
7222 const Value *RHS,
7223 const SimplifyQuery &SQ) {
7224 // X - (X % ?)
7225 // The remainder of a value can't have greater magnitude than itself,
7226 // so the subtraction can't overflow.
7227
7228 // X - (X -nuw ?)
7229 // In the minimal case, this would simplify to "?", so there's no subtract
7230 // at all. But if this analysis is used to peek through casts, for example,
7231 // then determining no-overflow may allow other transforms.
7232
7233 // TODO: There are other patterns like this.
7234 // See simplifyICmpWithBinOpOnLHS() for candidates.
7235 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7236 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7237 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7239
7240 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7241 SQ.DL)) {
7242 if (*C)
7245 }
7246
7247 ConstantRange LHSRange =
7248 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7249 ConstantRange RHSRange =
7250 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7251 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7252}
7253
7255 const Value *RHS,
7256 const SimplifyQuery &SQ) {
7257 // X - (X % ?)
7258 // The remainder of a value can't have greater magnitude than itself,
7259 // so the subtraction can't overflow.
7260
7261 // X - (X -nsw ?)
7262 // In the minimal case, this would simplify to "?", so there's no subtract
7263 // at all. But if this analysis is used to peek through casts, for example,
7264 // then determining no-overflow may allow other transforms.
7265 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7266 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7267 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7269
7270 // If LHS and RHS each have at least two sign bits, the subtraction
7271 // cannot overflow.
7272 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7274
7275 ConstantRange LHSRange =
7276 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7277 ConstantRange RHSRange =
7278 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7279 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7280}
7281
7283 const DominatorTree &DT) {
7284 SmallVector<const BranchInst *, 2> GuardingBranches;
7286
7287 for (const User *U : WO->users()) {
7288 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7289 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7290
7291 if (EVI->getIndices()[0] == 0)
7292 Results.push_back(EVI);
7293 else {
7294 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7295
7296 for (const auto *U : EVI->users())
7297 if (const auto *B = dyn_cast<BranchInst>(U)) {
7298 assert(B->isConditional() && "How else is it using an i1?");
7299 GuardingBranches.push_back(B);
7300 }
7301 }
7302 } else {
7303 // We are using the aggregate directly in a way we don't want to analyze
7304 // here (storing it to a global, say).
7305 return false;
7306 }
7307 }
7308
7309 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7310 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7311 if (!NoWrapEdge.isSingleEdge())
7312 return false;
7313
7314 // Check if all users of the add are provably no-wrap.
7315 for (const auto *Result : Results) {
7316 // If the extractvalue itself is not executed on overflow, the we don't
7317 // need to check each use separately, since domination is transitive.
7318 if (DT.dominates(NoWrapEdge, Result->getParent()))
7319 continue;
7320
7321 for (const auto &RU : Result->uses())
7322 if (!DT.dominates(NoWrapEdge, RU))
7323 return false;
7324 }
7325
7326 return true;
7327 };
7328
7329 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7330}
7331
7332/// Shifts return poison if shiftwidth is larger than the bitwidth.
7333static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7334 auto *C = dyn_cast<Constant>(ShiftAmount);
7335 if (!C)
7336 return false;
7337
7338 // Shifts return poison if shiftwidth is larger than the bitwidth.
7340 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7341 unsigned NumElts = FVTy->getNumElements();
7342 for (unsigned i = 0; i < NumElts; ++i)
7343 ShiftAmounts.push_back(C->getAggregateElement(i));
7344 } else if (isa<ScalableVectorType>(C->getType()))
7345 return false; // Can't tell, just return false to be safe
7346 else
7347 ShiftAmounts.push_back(C);
7348
7349 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7350 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7351 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7352 });
7353
7354 return Safe;
7355}
7356
7362
7364 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7365}
7366
7368 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7369}
7370
7372 bool ConsiderFlagsAndMetadata) {
7373
7374 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7375 Op->hasPoisonGeneratingAnnotations())
7376 return true;
7377
7378 unsigned Opcode = Op->getOpcode();
7379
7380 // Check whether opcode is a poison/undef-generating operation
7381 switch (Opcode) {
7382 case Instruction::Shl:
7383 case Instruction::AShr:
7384 case Instruction::LShr:
7385 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7386 case Instruction::FPToSI:
7387 case Instruction::FPToUI:
7388 // fptosi/ui yields poison if the resulting value does not fit in the
7389 // destination type.
7390 return true;
7391 case Instruction::Call:
7392 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7393 switch (II->getIntrinsicID()) {
7394 // TODO: Add more intrinsics.
7395 case Intrinsic::ctlz:
7396 case Intrinsic::cttz:
7397 case Intrinsic::abs:
7398 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7399 return false;
7400 break;
7401 case Intrinsic::ctpop:
7402 case Intrinsic::bswap:
7403 case Intrinsic::bitreverse:
7404 case Intrinsic::fshl:
7405 case Intrinsic::fshr:
7406 case Intrinsic::smax:
7407 case Intrinsic::smin:
7408 case Intrinsic::scmp:
7409 case Intrinsic::umax:
7410 case Intrinsic::umin:
7411 case Intrinsic::ucmp:
7412 case Intrinsic::ptrmask:
7413 case Intrinsic::fptoui_sat:
7414 case Intrinsic::fptosi_sat:
7415 case Intrinsic::sadd_with_overflow:
7416 case Intrinsic::ssub_with_overflow:
7417 case Intrinsic::smul_with_overflow:
7418 case Intrinsic::uadd_with_overflow:
7419 case Intrinsic::usub_with_overflow:
7420 case Intrinsic::umul_with_overflow:
7421 case Intrinsic::sadd_sat:
7422 case Intrinsic::uadd_sat:
7423 case Intrinsic::ssub_sat:
7424 case Intrinsic::usub_sat:
7425 return false;
7426 case Intrinsic::sshl_sat:
7427 case Intrinsic::ushl_sat:
7428 return includesPoison(Kind) &&
7429 !shiftAmountKnownInRange(II->getArgOperand(1));
7430 case Intrinsic::fma:
7431 case Intrinsic::fmuladd:
7432 case Intrinsic::sqrt:
7433 case Intrinsic::powi:
7434 case Intrinsic::sin:
7435 case Intrinsic::cos:
7436 case Intrinsic::pow:
7437 case Intrinsic::log:
7438 case Intrinsic::log10:
7439 case Intrinsic::log2:
7440 case Intrinsic::exp:
7441 case Intrinsic::exp2:
7442 case Intrinsic::exp10:
7443 case Intrinsic::fabs:
7444 case Intrinsic::copysign:
7445 case Intrinsic::floor:
7446 case Intrinsic::ceil:
7447 case Intrinsic::trunc:
7448 case Intrinsic::rint:
7449 case Intrinsic::nearbyint:
7450 case Intrinsic::round:
7451 case Intrinsic::roundeven:
7452 case Intrinsic::fptrunc_round:
7453 case Intrinsic::canonicalize:
7454 case Intrinsic::arithmetic_fence:
7455 case Intrinsic::minnum:
7456 case Intrinsic::maxnum:
7457 case Intrinsic::minimum:
7458 case Intrinsic::maximum:
7459 case Intrinsic::minimumnum:
7460 case Intrinsic::maximumnum:
7461 case Intrinsic::is_fpclass:
7462 case Intrinsic::ldexp:
7463 case Intrinsic::frexp:
7464 return false;
7465 case Intrinsic::lround:
7466 case Intrinsic::llround:
7467 case Intrinsic::lrint:
7468 case Intrinsic::llrint:
7469 // If the value doesn't fit an unspecified value is returned (but this
7470 // is not poison).
7471 return false;
7472 }
7473 }
7474 [[fallthrough]];
7475 case Instruction::CallBr:
7476 case Instruction::Invoke: {
7477 const auto *CB = cast<CallBase>(Op);
7478 return !CB->hasRetAttr(Attribute::NoUndef);
7479 }
7480 case Instruction::InsertElement:
7481 case Instruction::ExtractElement: {
7482 // If index exceeds the length of the vector, it returns poison
7483 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7484 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7485 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7486 if (includesPoison(Kind))
7487 return !Idx ||
7488 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7489 return false;
7490 }
7491 case Instruction::ShuffleVector: {
7493 ? cast<ConstantExpr>(Op)->getShuffleMask()
7494 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7495 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7496 }
7497 case Instruction::FNeg:
7498 case Instruction::PHI:
7499 case Instruction::Select:
7500 case Instruction::ExtractValue:
7501 case Instruction::InsertValue:
7502 case Instruction::Freeze:
7503 case Instruction::ICmp:
7504 case Instruction::FCmp:
7505 case Instruction::GetElementPtr:
7506 return false;
7507 case Instruction::AddrSpaceCast:
7508 return true;
7509 default: {
7510 const auto *CE = dyn_cast<ConstantExpr>(Op);
7511 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7512 return false;
7513 else if (Instruction::isBinaryOp(Opcode))
7514 return false;
7515 // Be conservative and return true.
7516 return true;
7517 }
7518 }
7519}
7520
7522 bool ConsiderFlagsAndMetadata) {
7523 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7524 ConsiderFlagsAndMetadata);
7525}
7526
7527bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7528 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7529 ConsiderFlagsAndMetadata);
7530}
7531
7532static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7533 unsigned Depth) {
7534 if (ValAssumedPoison == V)
7535 return true;
7536
7537 const unsigned MaxDepth = 2;
7538 if (Depth >= MaxDepth)
7539 return false;
7540
7541 if (const auto *I = dyn_cast<Instruction>(V)) {
7542 if (any_of(I->operands(), [=](const Use &Op) {
7543 return propagatesPoison(Op) &&
7544 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7545 }))
7546 return true;
7547
7548 // V = extractvalue V0, idx
7549 // V2 = extractvalue V0, idx2
7550 // V0's elements are all poison or not. (e.g., add_with_overflow)
7551 const WithOverflowInst *II;
7553 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7554 llvm::is_contained(II->args(), ValAssumedPoison)))
7555 return true;
7556 }
7557 return false;
7558}
7559
7560static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7561 unsigned Depth) {
7562 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7563 return true;
7564
7565 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7566 return true;
7567
7568 const unsigned MaxDepth = 2;
7569 if (Depth >= MaxDepth)
7570 return false;
7571
7572 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7573 if (I && !canCreatePoison(cast<Operator>(I))) {
7574 return all_of(I->operands(), [=](const Value *Op) {
7575 return impliesPoison(Op, V, Depth + 1);
7576 });
7577 }
7578 return false;
7579}
7580
7581bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7582 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7583}
7584
7585static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7586
7588 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7589 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7591 return false;
7592
7593 if (isa<MetadataAsValue>(V))
7594 return false;
7595
7596 if (const auto *A = dyn_cast<Argument>(V)) {
7597 if (A->hasAttribute(Attribute::NoUndef) ||
7598 A->hasAttribute(Attribute::Dereferenceable) ||
7599 A->hasAttribute(Attribute::DereferenceableOrNull))
7600 return true;
7601 }
7602
7603 if (auto *C = dyn_cast<Constant>(V)) {
7604 if (isa<PoisonValue>(C))
7605 return !includesPoison(Kind);
7606
7607 if (isa<UndefValue>(C))
7608 return !includesUndef(Kind);
7609
7612 return true;
7613
7614 if (C->getType()->isVectorTy()) {
7615 if (isa<ConstantExpr>(C)) {
7616 // Scalable vectors can use a ConstantExpr to build a splat.
7617 if (Constant *SplatC = C->getSplatValue())
7618 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7619 return true;
7620 } else {
7621 if (includesUndef(Kind) && C->containsUndefElement())
7622 return false;
7623 if (includesPoison(Kind) && C->containsPoisonElement())
7624 return false;
7625 return !C->containsConstantExpression();
7626 }
7627 }
7628 }
7629
7630 // Strip cast operations from a pointer value.
7631 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7632 // inbounds with zero offset. To guarantee that the result isn't poison, the
7633 // stripped pointer is checked as it has to be pointing into an allocated
7634 // object or be null `null` to ensure `inbounds` getelement pointers with a
7635 // zero offset could not produce poison.
7636 // It can strip off addrspacecast that do not change bit representation as
7637 // well. We believe that such addrspacecast is equivalent to no-op.
7638 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7639 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7640 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7641 return true;
7642
7643 auto OpCheck = [&](const Value *V) {
7644 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7645 };
7646
7647 if (auto *Opr = dyn_cast<Operator>(V)) {
7648 // If the value is a freeze instruction, then it can never
7649 // be undef or poison.
7650 if (isa<FreezeInst>(V))
7651 return true;
7652
7653 if (const auto *CB = dyn_cast<CallBase>(V)) {
7654 if (CB->hasRetAttr(Attribute::NoUndef) ||
7655 CB->hasRetAttr(Attribute::Dereferenceable) ||
7656 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7657 return true;
7658 }
7659
7660 if (const auto *PN = dyn_cast<PHINode>(V)) {
7661 unsigned Num = PN->getNumIncomingValues();
7662 bool IsWellDefined = true;
7663 for (unsigned i = 0; i < Num; ++i) {
7664 if (PN == PN->getIncomingValue(i))
7665 continue;
7666 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7667 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7668 DT, Depth + 1, Kind)) {
7669 IsWellDefined = false;
7670 break;
7671 }
7672 }
7673 if (IsWellDefined)
7674 return true;
7675 } else if (!::canCreateUndefOrPoison(Opr, Kind,
7676 /*ConsiderFlagsAndMetadata*/ true) &&
7677 all_of(Opr->operands(), OpCheck))
7678 return true;
7679 }
7680
7681 if (auto *I = dyn_cast<LoadInst>(V))
7682 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7683 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7684 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7685 return true;
7686
7688 return true;
7689
7690 // CxtI may be null or a cloned instruction.
7691 if (!CtxI || !CtxI->getParent() || !DT)
7692 return false;
7693
7694 auto *DNode = DT->getNode(CtxI->getParent());
7695 if (!DNode)
7696 // Unreachable block
7697 return false;
7698
7699 // If V is used as a branch condition before reaching CtxI, V cannot be
7700 // undef or poison.
7701 // br V, BB1, BB2
7702 // BB1:
7703 // CtxI ; V cannot be undef or poison here
7704 auto *Dominator = DNode->getIDom();
7705 // This check is purely for compile time reasons: we can skip the IDom walk
7706 // if what we are checking for includes undef and the value is not an integer.
7707 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7708 while (Dominator) {
7709 auto *TI = Dominator->getBlock()->getTerminator();
7710
7711 Value *Cond = nullptr;
7712 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7713 if (BI->isConditional())
7714 Cond = BI->getCondition();
7715 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7716 Cond = SI->getCondition();
7717 }
7718
7719 if (Cond) {
7720 if (Cond == V)
7721 return true;
7722 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7723 // For poison, we can analyze further
7724 auto *Opr = cast<Operator>(Cond);
7725 if (any_of(Opr->operands(), [V](const Use &U) {
7726 return V == U && propagatesPoison(U);
7727 }))
7728 return true;
7729 }
7730 }
7731
7732 Dominator = Dominator->getIDom();
7733 }
7734
7735 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7736 return true;
7737
7738 return false;
7739}
7740
7742 const Instruction *CtxI,
7743 const DominatorTree *DT,
7744 unsigned Depth) {
7745 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7747}
7748
7750 const Instruction *CtxI,
7751 const DominatorTree *DT, unsigned Depth) {
7752 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7754}
7755
7757 const Instruction *CtxI,
7758 const DominatorTree *DT, unsigned Depth) {
7759 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7761}
7762
7763/// Return true if undefined behavior would provably be executed on the path to
7764/// OnPathTo if Root produced a posion result. Note that this doesn't say
7765/// anything about whether OnPathTo is actually executed or whether Root is
7766/// actually poison. This can be used to assess whether a new use of Root can
7767/// be added at a location which is control equivalent with OnPathTo (such as
7768/// immediately before it) without introducing UB which didn't previously
7769/// exist. Note that a false result conveys no information.
7771 Instruction *OnPathTo,
7772 DominatorTree *DT) {
7773 // Basic approach is to assume Root is poison, propagate poison forward
7774 // through all users we can easily track, and then check whether any of those
7775 // users are provable UB and must execute before out exiting block might
7776 // exit.
7777
7778 // The set of all recursive users we've visited (which are assumed to all be
7779 // poison because of said visit)
7782 Worklist.push_back(Root);
7783 while (!Worklist.empty()) {
7784 const Instruction *I = Worklist.pop_back_val();
7785
7786 // If we know this must trigger UB on a path leading our target.
7787 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7788 return true;
7789
7790 // If we can't analyze propagation through this instruction, just skip it
7791 // and transitive users. Safe as false is a conservative result.
7792 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7793 return KnownPoison.contains(U) && propagatesPoison(U);
7794 }))
7795 continue;
7796
7797 if (KnownPoison.insert(I).second)
7798 for (const User *User : I->users())
7799 Worklist.push_back(cast<Instruction>(User));
7800 }
7801
7802 // Might be non-UB, or might have a path we couldn't prove must execute on
7803 // way to exiting bb.
7804 return false;
7805}
7806
7808 const SimplifyQuery &SQ) {
7809 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7810 Add, SQ);
7811}
7812
7815 const WithCache<const Value *> &RHS,
7816 const SimplifyQuery &SQ) {
7817 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7818}
7819
7821 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7822 // of time because it's possible for another thread to interfere with it for an
7823 // arbitrary length of time, but programs aren't allowed to rely on that.
7824
7825 // If there is no successor, then execution can't transfer to it.
7826 if (isa<ReturnInst>(I))
7827 return false;
7829 return false;
7830
7831 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7832 // Instruction::willReturn.
7833 //
7834 // FIXME: Move this check into Instruction::willReturn.
7835 if (isa<CatchPadInst>(I)) {
7836 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7837 default:
7838 // A catchpad may invoke exception object constructors and such, which
7839 // in some languages can be arbitrary code, so be conservative by default.
7840 return false;
7842 // For CoreCLR, it just involves a type test.
7843 return true;
7844 }
7845 }
7846
7847 // An instruction that returns without throwing must transfer control flow
7848 // to a successor.
7849 return !I->mayThrow() && I->willReturn();
7850}
7851
7853 // TODO: This is slightly conservative for invoke instruction since exiting
7854 // via an exception *is* normal control for them.
7855 for (const Instruction &I : *BB)
7857 return false;
7858 return true;
7859}
7860
7867
7870 assert(ScanLimit && "scan limit must be non-zero");
7871 for (const Instruction &I : Range) {
7872 if (--ScanLimit == 0)
7873 return false;
7875 return false;
7876 }
7877 return true;
7878}
7879
7881 const Loop *L) {
7882 // The loop header is guaranteed to be executed for every iteration.
7883 //
7884 // FIXME: Relax this constraint to cover all basic blocks that are
7885 // guaranteed to be executed at every iteration.
7886 if (I->getParent() != L->getHeader()) return false;
7887
7888 for (const Instruction &LI : *L->getHeader()) {
7889 if (&LI == I) return true;
7890 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7891 }
7892 llvm_unreachable("Instruction not contained in its own parent basic block.");
7893}
7894
7896 switch (IID) {
7897 // TODO: Add more intrinsics.
7898 case Intrinsic::sadd_with_overflow:
7899 case Intrinsic::ssub_with_overflow:
7900 case Intrinsic::smul_with_overflow:
7901 case Intrinsic::uadd_with_overflow:
7902 case Intrinsic::usub_with_overflow:
7903 case Intrinsic::umul_with_overflow:
7904 // If an input is a vector containing a poison element, the
7905 // two output vectors (calculated results, overflow bits)'
7906 // corresponding lanes are poison.
7907 return true;
7908 case Intrinsic::ctpop:
7909 case Intrinsic::ctlz:
7910 case Intrinsic::cttz:
7911 case Intrinsic::abs:
7912 case Intrinsic::smax:
7913 case Intrinsic::smin:
7914 case Intrinsic::umax:
7915 case Intrinsic::umin:
7916 case Intrinsic::scmp:
7917 case Intrinsic::is_fpclass:
7918 case Intrinsic::ptrmask:
7919 case Intrinsic::ucmp:
7920 case Intrinsic::bitreverse:
7921 case Intrinsic::bswap:
7922 case Intrinsic::sadd_sat:
7923 case Intrinsic::ssub_sat:
7924 case Intrinsic::sshl_sat:
7925 case Intrinsic::uadd_sat:
7926 case Intrinsic::usub_sat:
7927 case Intrinsic::ushl_sat:
7928 case Intrinsic::smul_fix:
7929 case Intrinsic::smul_fix_sat:
7930 case Intrinsic::umul_fix:
7931 case Intrinsic::umul_fix_sat:
7932 case Intrinsic::pow:
7933 case Intrinsic::powi:
7934 case Intrinsic::sin:
7935 case Intrinsic::sinh:
7936 case Intrinsic::cos:
7937 case Intrinsic::cosh:
7938 case Intrinsic::sincos:
7939 case Intrinsic::sincospi:
7940 case Intrinsic::tan:
7941 case Intrinsic::tanh:
7942 case Intrinsic::asin:
7943 case Intrinsic::acos:
7944 case Intrinsic::atan:
7945 case Intrinsic::atan2:
7946 case Intrinsic::canonicalize:
7947 case Intrinsic::sqrt:
7948 case Intrinsic::exp:
7949 case Intrinsic::exp2:
7950 case Intrinsic::exp10:
7951 case Intrinsic::log:
7952 case Intrinsic::log2:
7953 case Intrinsic::log10:
7954 case Intrinsic::modf:
7955 case Intrinsic::floor:
7956 case Intrinsic::ceil:
7957 case Intrinsic::trunc:
7958 case Intrinsic::rint:
7959 case Intrinsic::nearbyint:
7960 case Intrinsic::round:
7961 case Intrinsic::roundeven:
7962 case Intrinsic::lrint:
7963 case Intrinsic::llrint:
7964 return true;
7965 default:
7966 return false;
7967 }
7968}
7969
7970bool llvm::propagatesPoison(const Use &PoisonOp) {
7971 const Operator *I = cast<Operator>(PoisonOp.getUser());
7972 switch (I->getOpcode()) {
7973 case Instruction::Freeze:
7974 case Instruction::PHI:
7975 case Instruction::Invoke:
7976 return false;
7977 case Instruction::Select:
7978 return PoisonOp.getOperandNo() == 0;
7979 case Instruction::Call:
7980 if (auto *II = dyn_cast<IntrinsicInst>(I))
7981 return intrinsicPropagatesPoison(II->getIntrinsicID());
7982 return false;
7983 case Instruction::ICmp:
7984 case Instruction::FCmp:
7985 case Instruction::GetElementPtr:
7986 return true;
7987 default:
7989 return true;
7990
7991 // Be conservative and return false.
7992 return false;
7993 }
7994}
7995
7996/// Enumerates all operands of \p I that are guaranteed to not be undef or
7997/// poison. If the callback \p Handle returns true, stop processing and return
7998/// true. Otherwise, return false.
7999template <typename CallableT>
8001 const CallableT &Handle) {
8002 switch (I->getOpcode()) {
8003 case Instruction::Store:
8004 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8005 return true;
8006 break;
8007
8008 case Instruction::Load:
8009 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8010 return true;
8011 break;
8012
8013 // Since dereferenceable attribute imply noundef, atomic operations
8014 // also implicitly have noundef pointers too
8015 case Instruction::AtomicCmpXchg:
8017 return true;
8018 break;
8019
8020 case Instruction::AtomicRMW:
8021 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8022 return true;
8023 break;
8024
8025 case Instruction::Call:
8026 case Instruction::Invoke: {
8027 const CallBase *CB = cast<CallBase>(I);
8028 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8029 return true;
8030 for (unsigned i = 0; i < CB->arg_size(); ++i)
8031 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8032 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8033 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8034 Handle(CB->getArgOperand(i)))
8035 return true;
8036 break;
8037 }
8038 case Instruction::Ret:
8039 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8040 Handle(I->getOperand(0)))
8041 return true;
8042 break;
8043 case Instruction::Switch:
8044 if (Handle(cast<SwitchInst>(I)->getCondition()))
8045 return true;
8046 break;
8047 case Instruction::Br: {
8048 auto *BR = cast<BranchInst>(I);
8049 if (BR->isConditional() && Handle(BR->getCondition()))
8050 return true;
8051 break;
8052 }
8053 default:
8054 break;
8055 }
8056
8057 return false;
8058}
8059
8060/// Enumerates all operands of \p I that are guaranteed to not be poison.
8061template <typename CallableT>
8063 const CallableT &Handle) {
8064 if (handleGuaranteedWellDefinedOps(I, Handle))
8065 return true;
8066 switch (I->getOpcode()) {
8067 // Divisors of these operations are allowed to be partially undef.
8068 case Instruction::UDiv:
8069 case Instruction::SDiv:
8070 case Instruction::URem:
8071 case Instruction::SRem:
8072 return Handle(I->getOperand(1));
8073 default:
8074 return false;
8075 }
8076}
8077
8079 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8081 I, [&](const Value *V) { return KnownPoison.count(V); });
8082}
8083
8085 bool PoisonOnly) {
8086 // We currently only look for uses of values within the same basic
8087 // block, as that makes it easier to guarantee that the uses will be
8088 // executed given that Inst is executed.
8089 //
8090 // FIXME: Expand this to consider uses beyond the same basic block. To do
8091 // this, look out for the distinction between post-dominance and strong
8092 // post-dominance.
8093 const BasicBlock *BB = nullptr;
8095 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8096 BB = Inst->getParent();
8097 Begin = Inst->getIterator();
8098 Begin++;
8099 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8100 if (Arg->getParent()->isDeclaration())
8101 return false;
8102 BB = &Arg->getParent()->getEntryBlock();
8103 Begin = BB->begin();
8104 } else {
8105 return false;
8106 }
8107
8108 // Limit number of instructions we look at, to avoid scanning through large
8109 // blocks. The current limit is chosen arbitrarily.
8110 unsigned ScanLimit = 32;
8111 BasicBlock::const_iterator End = BB->end();
8112
8113 if (!PoisonOnly) {
8114 // Since undef does not propagate eagerly, be conservative & just check
8115 // whether a value is directly passed to an instruction that must take
8116 // well-defined operands.
8117
8118 for (const auto &I : make_range(Begin, End)) {
8119 if (--ScanLimit == 0)
8120 break;
8121
8122 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8123 return WellDefinedOp == V;
8124 }))
8125 return true;
8126
8128 break;
8129 }
8130 return false;
8131 }
8132
8133 // Set of instructions that we have proved will yield poison if Inst
8134 // does.
8135 SmallPtrSet<const Value *, 16> YieldsPoison;
8137
8138 YieldsPoison.insert(V);
8139 Visited.insert(BB);
8140
8141 while (true) {
8142 for (const auto &I : make_range(Begin, End)) {
8143 if (--ScanLimit == 0)
8144 return false;
8145 if (mustTriggerUB(&I, YieldsPoison))
8146 return true;
8148 return false;
8149
8150 // If an operand is poison and propagates it, mark I as yielding poison.
8151 for (const Use &Op : I.operands()) {
8152 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8153 YieldsPoison.insert(&I);
8154 break;
8155 }
8156 }
8157
8158 // Special handling for select, which returns poison if its operand 0 is
8159 // poison (handled in the loop above) *or* if both its true/false operands
8160 // are poison (handled here).
8161 if (I.getOpcode() == Instruction::Select &&
8162 YieldsPoison.count(I.getOperand(1)) &&
8163 YieldsPoison.count(I.getOperand(2))) {
8164 YieldsPoison.insert(&I);
8165 }
8166 }
8167
8168 BB = BB->getSingleSuccessor();
8169 if (!BB || !Visited.insert(BB).second)
8170 break;
8171
8172 Begin = BB->getFirstNonPHIIt();
8173 End = BB->end();
8174 }
8175 return false;
8176}
8177
8179 return ::programUndefinedIfUndefOrPoison(Inst, false);
8180}
8181
8183 return ::programUndefinedIfUndefOrPoison(Inst, true);
8184}
8185
8186static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8187 if (FMF.noNaNs())
8188 return true;
8189
8190 if (auto *C = dyn_cast<ConstantFP>(V))
8191 return !C->isNaN();
8192
8193 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8194 if (!C->getElementType()->isFloatingPointTy())
8195 return false;
8196 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8197 if (C->getElementAsAPFloat(I).isNaN())
8198 return false;
8199 }
8200 return true;
8201 }
8202
8204 return true;
8205
8206 return false;
8207}
8208
8209static bool isKnownNonZero(const Value *V) {
8210 if (auto *C = dyn_cast<ConstantFP>(V))
8211 return !C->isZero();
8212
8213 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8214 if (!C->getElementType()->isFloatingPointTy())
8215 return false;
8216 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8217 if (C->getElementAsAPFloat(I).isZero())
8218 return false;
8219 }
8220 return true;
8221 }
8222
8223 return false;
8224}
8225
8226/// Match clamp pattern for float types without care about NaNs or signed zeros.
8227/// Given non-min/max outer cmp/select from the clamp pattern this
8228/// function recognizes if it can be substitued by a "canonical" min/max
8229/// pattern.
8231 Value *CmpLHS, Value *CmpRHS,
8232 Value *TrueVal, Value *FalseVal,
8233 Value *&LHS, Value *&RHS) {
8234 // Try to match
8235 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8236 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8237 // and return description of the outer Max/Min.
8238
8239 // First, check if select has inverse order:
8240 if (CmpRHS == FalseVal) {
8241 std::swap(TrueVal, FalseVal);
8242 Pred = CmpInst::getInversePredicate(Pred);
8243 }
8244
8245 // Assume success now. If there's no match, callers should not use these anyway.
8246 LHS = TrueVal;
8247 RHS = FalseVal;
8248
8249 const APFloat *FC1;
8250 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8251 return {SPF_UNKNOWN, SPNB_NA, false};
8252
8253 const APFloat *FC2;
8254 switch (Pred) {
8255 case CmpInst::FCMP_OLT:
8256 case CmpInst::FCMP_OLE:
8257 case CmpInst::FCMP_ULT:
8258 case CmpInst::FCMP_ULE:
8259 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8260 *FC1 < *FC2)
8261 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8262 break;
8263 case CmpInst::FCMP_OGT:
8264 case CmpInst::FCMP_OGE:
8265 case CmpInst::FCMP_UGT:
8266 case CmpInst::FCMP_UGE:
8267 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8268 *FC1 > *FC2)
8269 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8270 break;
8271 default:
8272 break;
8273 }
8274
8275 return {SPF_UNKNOWN, SPNB_NA, false};
8276}
8277
8278/// Recognize variations of:
8279/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8281 Value *CmpLHS, Value *CmpRHS,
8282 Value *TrueVal, Value *FalseVal) {
8283 // Swap the select operands and predicate to match the patterns below.
8284 if (CmpRHS != TrueVal) {
8285 Pred = ICmpInst::getSwappedPredicate(Pred);
8286 std::swap(TrueVal, FalseVal);
8287 }
8288 const APInt *C1;
8289 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8290 const APInt *C2;
8291 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8292 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8293 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8294 return {SPF_SMAX, SPNB_NA, false};
8295
8296 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8297 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8298 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8299 return {SPF_SMIN, SPNB_NA, false};
8300
8301 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8302 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8303 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8304 return {SPF_UMAX, SPNB_NA, false};
8305
8306 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8307 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8308 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8309 return {SPF_UMIN, SPNB_NA, false};
8310 }
8311 return {SPF_UNKNOWN, SPNB_NA, false};
8312}
8313
8314/// Recognize variations of:
8315/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8317 Value *CmpLHS, Value *CmpRHS,
8318 Value *TVal, Value *FVal,
8319 unsigned Depth) {
8320 // TODO: Allow FP min/max with nnan/nsz.
8321 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8322
8323 Value *A = nullptr, *B = nullptr;
8324 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8325 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8326 return {SPF_UNKNOWN, SPNB_NA, false};
8327
8328 Value *C = nullptr, *D = nullptr;
8329 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8330 if (L.Flavor != R.Flavor)
8331 return {SPF_UNKNOWN, SPNB_NA, false};
8332
8333 // We have something like: x Pred y ? min(a, b) : min(c, d).
8334 // Try to match the compare to the min/max operations of the select operands.
8335 // First, make sure we have the right compare predicate.
8336 switch (L.Flavor) {
8337 case SPF_SMIN:
8338 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8339 Pred = ICmpInst::getSwappedPredicate(Pred);
8340 std::swap(CmpLHS, CmpRHS);
8341 }
8342 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8343 break;
8344 return {SPF_UNKNOWN, SPNB_NA, false};
8345 case SPF_SMAX:
8346 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8347 Pred = ICmpInst::getSwappedPredicate(Pred);
8348 std::swap(CmpLHS, CmpRHS);
8349 }
8350 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8351 break;
8352 return {SPF_UNKNOWN, SPNB_NA, false};
8353 case SPF_UMIN:
8354 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8355 Pred = ICmpInst::getSwappedPredicate(Pred);
8356 std::swap(CmpLHS, CmpRHS);
8357 }
8358 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8359 break;
8360 return {SPF_UNKNOWN, SPNB_NA, false};
8361 case SPF_UMAX:
8362 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8363 Pred = ICmpInst::getSwappedPredicate(Pred);
8364 std::swap(CmpLHS, CmpRHS);
8365 }
8366 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8367 break;
8368 return {SPF_UNKNOWN, SPNB_NA, false};
8369 default:
8370 return {SPF_UNKNOWN, SPNB_NA, false};
8371 }
8372
8373 // If there is a common operand in the already matched min/max and the other
8374 // min/max operands match the compare operands (either directly or inverted),
8375 // then this is min/max of the same flavor.
8376
8377 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8378 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8379 if (D == B) {
8380 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8381 match(A, m_Not(m_Specific(CmpRHS)))))
8382 return {L.Flavor, SPNB_NA, false};
8383 }
8384 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8385 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8386 if (C == B) {
8387 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8388 match(A, m_Not(m_Specific(CmpRHS)))))
8389 return {L.Flavor, SPNB_NA, false};
8390 }
8391 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8392 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8393 if (D == A) {
8394 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8395 match(B, m_Not(m_Specific(CmpRHS)))))
8396 return {L.Flavor, SPNB_NA, false};
8397 }
8398 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8399 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8400 if (C == A) {
8401 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8402 match(B, m_Not(m_Specific(CmpRHS)))))
8403 return {L.Flavor, SPNB_NA, false};
8404 }
8405
8406 return {SPF_UNKNOWN, SPNB_NA, false};
8407}
8408
8409/// If the input value is the result of a 'not' op, constant integer, or vector
8410/// splat of a constant integer, return the bitwise-not source value.
8411/// TODO: This could be extended to handle non-splat vector integer constants.
8413 Value *NotV;
8414 if (match(V, m_Not(m_Value(NotV))))
8415 return NotV;
8416
8417 const APInt *C;
8418 if (match(V, m_APInt(C)))
8419 return ConstantInt::get(V->getType(), ~(*C));
8420
8421 return nullptr;
8422}
8423
8424/// Match non-obvious integer minimum and maximum sequences.
8426 Value *CmpLHS, Value *CmpRHS,
8427 Value *TrueVal, Value *FalseVal,
8428 Value *&LHS, Value *&RHS,
8429 unsigned Depth) {
8430 // Assume success. If there's no match, callers should not use these anyway.
8431 LHS = TrueVal;
8432 RHS = FalseVal;
8433
8434 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8436 return SPR;
8437
8438 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8440 return SPR;
8441
8442 // Look through 'not' ops to find disguised min/max.
8443 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8444 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8445 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8446 switch (Pred) {
8447 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8448 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8449 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8450 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8451 default: break;
8452 }
8453 }
8454
8455 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8456 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8457 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8458 switch (Pred) {
8459 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8460 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8461 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8462 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8463 default: break;
8464 }
8465 }
8466
8467 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8468 return {SPF_UNKNOWN, SPNB_NA, false};
8469
8470 const APInt *C1;
8471 if (!match(CmpRHS, m_APInt(C1)))
8472 return {SPF_UNKNOWN, SPNB_NA, false};
8473
8474 // An unsigned min/max can be written with a signed compare.
8475 const APInt *C2;
8476 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8477 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8478 // Is the sign bit set?
8479 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8480 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8481 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8482 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8483
8484 // Is the sign bit clear?
8485 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8486 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8487 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8488 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8489 }
8490
8491 return {SPF_UNKNOWN, SPNB_NA, false};
8492}
8493
8494bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8495 bool AllowPoison) {
8496 assert(X && Y && "Invalid operand");
8497
8498 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8499 if (!match(X, m_Neg(m_Specific(Y))))
8500 return false;
8501
8502 auto *BO = cast<BinaryOperator>(X);
8503 if (NeedNSW && !BO->hasNoSignedWrap())
8504 return false;
8505
8506 auto *Zero = cast<Constant>(BO->getOperand(0));
8507 if (!AllowPoison && !Zero->isNullValue())
8508 return false;
8509
8510 return true;
8511 };
8512
8513 // X = -Y or Y = -X
8514 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8515 return true;
8516
8517 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8518 Value *A, *B;
8519 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8520 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8521 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8523}
8524
8525bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8526 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8527 Value *A, *B, *C;
8528 CmpPredicate Pred1, Pred2;
8529 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8530 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8531 return false;
8532
8533 // They must both have samesign flag or not.
8534 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8535 return false;
8536
8537 if (B == C)
8538 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8539
8540 // Try to infer the relationship from constant ranges.
8541 const APInt *RHSC1, *RHSC2;
8542 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8543 return false;
8544
8545 // Sign bits of two RHSCs should match.
8546 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8547 return false;
8548
8549 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8550 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8551
8552 return CR1.inverse() == CR2;
8553}
8554
8556 SelectPatternNaNBehavior NaNBehavior,
8557 bool Ordered) {
8558 switch (Pred) {
8559 default:
8560 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8561 case ICmpInst::ICMP_UGT:
8562 case ICmpInst::ICMP_UGE:
8563 return {SPF_UMAX, SPNB_NA, false};
8564 case ICmpInst::ICMP_SGT:
8565 case ICmpInst::ICMP_SGE:
8566 return {SPF_SMAX, SPNB_NA, false};
8567 case ICmpInst::ICMP_ULT:
8568 case ICmpInst::ICMP_ULE:
8569 return {SPF_UMIN, SPNB_NA, false};
8570 case ICmpInst::ICMP_SLT:
8571 case ICmpInst::ICMP_SLE:
8572 return {SPF_SMIN, SPNB_NA, false};
8573 case FCmpInst::FCMP_UGT:
8574 case FCmpInst::FCMP_UGE:
8575 case FCmpInst::FCMP_OGT:
8576 case FCmpInst::FCMP_OGE:
8577 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8578 case FCmpInst::FCMP_ULT:
8579 case FCmpInst::FCMP_ULE:
8580 case FCmpInst::FCMP_OLT:
8581 case FCmpInst::FCMP_OLE:
8582 return {SPF_FMINNUM, NaNBehavior, Ordered};
8583 }
8584}
8585
8586std::optional<std::pair<CmpPredicate, Constant *>>
8589 "Only for relational integer predicates.");
8590 if (isa<UndefValue>(C))
8591 return std::nullopt;
8592
8593 Type *Type = C->getType();
8594 bool IsSigned = ICmpInst::isSigned(Pred);
8595
8597 bool WillIncrement =
8598 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8599
8600 // Check if the constant operand can be safely incremented/decremented
8601 // without overflowing/underflowing.
8602 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8603 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8604 };
8605
8606 Constant *SafeReplacementConstant = nullptr;
8607 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8608 // Bail out if the constant can't be safely incremented/decremented.
8609 if (!ConstantIsOk(CI))
8610 return std::nullopt;
8611 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8612 unsigned NumElts = FVTy->getNumElements();
8613 for (unsigned i = 0; i != NumElts; ++i) {
8614 Constant *Elt = C->getAggregateElement(i);
8615 if (!Elt)
8616 return std::nullopt;
8617
8618 if (isa<UndefValue>(Elt))
8619 continue;
8620
8621 // Bail out if we can't determine if this constant is min/max or if we
8622 // know that this constant is min/max.
8623 auto *CI = dyn_cast<ConstantInt>(Elt);
8624 if (!CI || !ConstantIsOk(CI))
8625 return std::nullopt;
8626
8627 if (!SafeReplacementConstant)
8628 SafeReplacementConstant = CI;
8629 }
8630 } else if (isa<VectorType>(C->getType())) {
8631 // Handle scalable splat
8632 Value *SplatC = C->getSplatValue();
8633 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8634 // Bail out if the constant can't be safely incremented/decremented.
8635 if (!CI || !ConstantIsOk(CI))
8636 return std::nullopt;
8637 } else {
8638 // ConstantExpr?
8639 return std::nullopt;
8640 }
8641
8642 // It may not be safe to change a compare predicate in the presence of
8643 // undefined elements, so replace those elements with the first safe constant
8644 // that we found.
8645 // TODO: in case of poison, it is safe; let's replace undefs only.
8646 if (C->containsUndefOrPoisonElement()) {
8647 assert(SafeReplacementConstant && "Replacement constant not set");
8648 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8649 }
8650
8652
8653 // Increment or decrement the constant.
8654 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8655 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8656
8657 return std::make_pair(NewPred, NewC);
8658}
8659
8661 FastMathFlags FMF,
8662 Value *CmpLHS, Value *CmpRHS,
8663 Value *TrueVal, Value *FalseVal,
8664 Value *&LHS, Value *&RHS,
8665 unsigned Depth) {
8666 bool HasMismatchedZeros = false;
8667 if (CmpInst::isFPPredicate(Pred)) {
8668 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8669 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8670 // purpose of identifying min/max. Disregard vector constants with undefined
8671 // elements because those can not be back-propagated for analysis.
8672 Value *OutputZeroVal = nullptr;
8673 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8674 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8675 OutputZeroVal = TrueVal;
8676 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8677 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8678 OutputZeroVal = FalseVal;
8679
8680 if (OutputZeroVal) {
8681 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8682 HasMismatchedZeros = true;
8683 CmpLHS = OutputZeroVal;
8684 }
8685 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8686 HasMismatchedZeros = true;
8687 CmpRHS = OutputZeroVal;
8688 }
8689 }
8690 }
8691
8692 LHS = CmpLHS;
8693 RHS = CmpRHS;
8694
8695 // Signed zero may return inconsistent results between implementations.
8696 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8697 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8698 // Therefore, we behave conservatively and only proceed if at least one of the
8699 // operands is known to not be zero or if we don't care about signed zero.
8700 switch (Pred) {
8701 default: break;
8704 if (!HasMismatchedZeros)
8705 break;
8706 [[fallthrough]];
8709 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8710 !isKnownNonZero(CmpRHS))
8711 return {SPF_UNKNOWN, SPNB_NA, false};
8712 }
8713
8714 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8715 bool Ordered = false;
8716
8717 // When given one NaN and one non-NaN input:
8718 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8719 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8720 // ordered comparison fails), which could be NaN or non-NaN.
8721 // so here we discover exactly what NaN behavior is required/accepted.
8722 if (CmpInst::isFPPredicate(Pred)) {
8723 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8724 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8725
8726 if (LHSSafe && RHSSafe) {
8727 // Both operands are known non-NaN.
8728 NaNBehavior = SPNB_RETURNS_ANY;
8729 Ordered = CmpInst::isOrdered(Pred);
8730 } else if (CmpInst::isOrdered(Pred)) {
8731 // An ordered comparison will return false when given a NaN, so it
8732 // returns the RHS.
8733 Ordered = true;
8734 if (LHSSafe)
8735 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8736 NaNBehavior = SPNB_RETURNS_NAN;
8737 else if (RHSSafe)
8738 NaNBehavior = SPNB_RETURNS_OTHER;
8739 else
8740 // Completely unsafe.
8741 return {SPF_UNKNOWN, SPNB_NA, false};
8742 } else {
8743 Ordered = false;
8744 // An unordered comparison will return true when given a NaN, so it
8745 // returns the LHS.
8746 if (LHSSafe)
8747 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8748 NaNBehavior = SPNB_RETURNS_OTHER;
8749 else if (RHSSafe)
8750 NaNBehavior = SPNB_RETURNS_NAN;
8751 else
8752 // Completely unsafe.
8753 return {SPF_UNKNOWN, SPNB_NA, false};
8754 }
8755 }
8756
8757 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8758 std::swap(CmpLHS, CmpRHS);
8759 Pred = CmpInst::getSwappedPredicate(Pred);
8760 if (NaNBehavior == SPNB_RETURNS_NAN)
8761 NaNBehavior = SPNB_RETURNS_OTHER;
8762 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8763 NaNBehavior = SPNB_RETURNS_NAN;
8764 Ordered = !Ordered;
8765 }
8766
8767 // ([if]cmp X, Y) ? X : Y
8768 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8769 return getSelectPattern(Pred, NaNBehavior, Ordered);
8770
8771 if (isKnownNegation(TrueVal, FalseVal)) {
8772 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8773 // match against either LHS or sext(LHS).
8774 auto MaybeSExtCmpLHS =
8775 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8776 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8777 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8778 if (match(TrueVal, MaybeSExtCmpLHS)) {
8779 // Set the return values. If the compare uses the negated value (-X >s 0),
8780 // swap the return values because the negated value is always 'RHS'.
8781 LHS = TrueVal;
8782 RHS = FalseVal;
8783 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8784 std::swap(LHS, RHS);
8785
8786 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8787 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8788 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8789 return {SPF_ABS, SPNB_NA, false};
8790
8791 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8792 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8793 return {SPF_ABS, SPNB_NA, false};
8794
8795 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8796 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8797 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8798 return {SPF_NABS, SPNB_NA, false};
8799 }
8800 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8801 // Set the return values. If the compare uses the negated value (-X >s 0),
8802 // swap the return values because the negated value is always 'RHS'.
8803 LHS = FalseVal;
8804 RHS = TrueVal;
8805 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8806 std::swap(LHS, RHS);
8807
8808 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8809 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8810 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8811 return {SPF_NABS, SPNB_NA, false};
8812
8813 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8814 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8815 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8816 return {SPF_ABS, SPNB_NA, false};
8817 }
8818 }
8819
8820 if (CmpInst::isIntPredicate(Pred))
8821 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8822
8823 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8824 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8825 // semantics than minNum. Be conservative in such case.
8826 if (NaNBehavior != SPNB_RETURNS_ANY ||
8827 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8828 !isKnownNonZero(CmpRHS)))
8829 return {SPF_UNKNOWN, SPNB_NA, false};
8830
8831 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8832}
8833
8835 Instruction::CastOps *CastOp) {
8836 const DataLayout &DL = CmpI->getDataLayout();
8837
8838 Constant *CastedTo = nullptr;
8839 switch (*CastOp) {
8840 case Instruction::ZExt:
8841 if (CmpI->isUnsigned())
8842 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8843 break;
8844 case Instruction::SExt:
8845 if (CmpI->isSigned())
8846 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8847 break;
8848 case Instruction::Trunc:
8849 Constant *CmpConst;
8850 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8851 CmpConst->getType() == SrcTy) {
8852 // Here we have the following case:
8853 //
8854 // %cond = cmp iN %x, CmpConst
8855 // %tr = trunc iN %x to iK
8856 // %narrowsel = select i1 %cond, iK %t, iK C
8857 //
8858 // We can always move trunc after select operation:
8859 //
8860 // %cond = cmp iN %x, CmpConst
8861 // %widesel = select i1 %cond, iN %x, iN CmpConst
8862 // %tr = trunc iN %widesel to iK
8863 //
8864 // Note that C could be extended in any way because we don't care about
8865 // upper bits after truncation. It can't be abs pattern, because it would
8866 // look like:
8867 //
8868 // select i1 %cond, x, -x.
8869 //
8870 // So only min/max pattern could be matched. Such match requires widened C
8871 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8872 // CmpConst == C is checked below.
8873 CastedTo = CmpConst;
8874 } else {
8875 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8876 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8877 }
8878 break;
8879 case Instruction::FPTrunc:
8880 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8881 break;
8882 case Instruction::FPExt:
8883 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8884 break;
8885 case Instruction::FPToUI:
8886 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8887 break;
8888 case Instruction::FPToSI:
8889 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8890 break;
8891 case Instruction::UIToFP:
8892 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8893 break;
8894 case Instruction::SIToFP:
8895 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8896 break;
8897 default:
8898 break;
8899 }
8900
8901 if (!CastedTo)
8902 return nullptr;
8903
8904 // Make sure the cast doesn't lose any information.
8905 Constant *CastedBack =
8906 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8907 if (CastedBack && CastedBack != C)
8908 return nullptr;
8909
8910 return CastedTo;
8911}
8912
8913/// Helps to match a select pattern in case of a type mismatch.
8914///
8915/// The function processes the case when type of true and false values of a
8916/// select instruction differs from type of the cmp instruction operands because
8917/// of a cast instruction. The function checks if it is legal to move the cast
8918/// operation after "select". If yes, it returns the new second value of
8919/// "select" (with the assumption that cast is moved):
8920/// 1. As operand of cast instruction when both values of "select" are same cast
8921/// instructions.
8922/// 2. As restored constant (by applying reverse cast operation) when the first
8923/// value of the "select" is a cast operation and the second value is a
8924/// constant. It is implemented in lookThroughCastConst().
8925/// 3. As one operand is cast instruction and the other is not. The operands in
8926/// sel(cmp) are in different type integer.
8927/// NOTE: We return only the new second value because the first value could be
8928/// accessed as operand of cast instruction.
8929static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8930 Instruction::CastOps *CastOp) {
8931 auto *Cast1 = dyn_cast<CastInst>(V1);
8932 if (!Cast1)
8933 return nullptr;
8934
8935 *CastOp = Cast1->getOpcode();
8936 Type *SrcTy = Cast1->getSrcTy();
8937 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8938 // If V1 and V2 are both the same cast from the same type, look through V1.
8939 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
8940 return Cast2->getOperand(0);
8941 return nullptr;
8942 }
8943
8944 auto *C = dyn_cast<Constant>(V2);
8945 if (C)
8946 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
8947
8948 Value *CastedTo = nullptr;
8949 if (*CastOp == Instruction::Trunc) {
8950 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
8951 // Here we have the following case:
8952 // %y_ext = sext iK %y to iN
8953 // %cond = cmp iN %x, %y_ext
8954 // %tr = trunc iN %x to iK
8955 // %narrowsel = select i1 %cond, iK %tr, iK %y
8956 //
8957 // We can always move trunc after select operation:
8958 // %y_ext = sext iK %y to iN
8959 // %cond = cmp iN %x, %y_ext
8960 // %widesel = select i1 %cond, iN %x, iN %y_ext
8961 // %tr = trunc iN %widesel to iK
8962 assert(V2->getType() == Cast1->getType() &&
8963 "V2 and Cast1 should be the same type.");
8964 CastedTo = CmpI->getOperand(1);
8965 }
8966 }
8967
8968 return CastedTo;
8969}
8971 Instruction::CastOps *CastOp,
8972 unsigned Depth) {
8974 return {SPF_UNKNOWN, SPNB_NA, false};
8975
8977 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
8978
8979 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
8980 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
8981
8982 Value *TrueVal = SI->getTrueValue();
8983 Value *FalseVal = SI->getFalseValue();
8984
8986 CmpI, TrueVal, FalseVal, LHS, RHS,
8987 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
8988 CastOp, Depth);
8989}
8990
8992 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
8993 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
8994 CmpInst::Predicate Pred = CmpI->getPredicate();
8995 Value *CmpLHS = CmpI->getOperand(0);
8996 Value *CmpRHS = CmpI->getOperand(1);
8997 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
8998 FMF.setNoNaNs();
8999
9000 // Bail out early.
9001 if (CmpI->isEquality())
9002 return {SPF_UNKNOWN, SPNB_NA, false};
9003
9004 // Deal with type mismatches.
9005 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9006 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9007 // If this is a potential fmin/fmax with a cast to integer, then ignore
9008 // -0.0 because there is no corresponding integer value.
9009 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9010 FMF.setNoSignedZeros();
9011 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9012 cast<CastInst>(TrueVal)->getOperand(0), C,
9013 LHS, RHS, Depth);
9014 }
9015 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9016 // If this is a potential fmin/fmax with a cast to integer, then ignore
9017 // -0.0 because there is no corresponding integer value.
9018 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9019 FMF.setNoSignedZeros();
9020 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9021 C, cast<CastInst>(FalseVal)->getOperand(0),
9022 LHS, RHS, Depth);
9023 }
9024 }
9025 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9026 LHS, RHS, Depth);
9027}
9028
9030 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9031 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9032 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9033 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9034 if (SPF == SPF_FMINNUM)
9035 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9036 if (SPF == SPF_FMAXNUM)
9037 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9038 llvm_unreachable("unhandled!");
9039}
9040
9042 switch (SPF) {
9044 return Intrinsic::umin;
9046 return Intrinsic::umax;
9048 return Intrinsic::smin;
9050 return Intrinsic::smax;
9051 default:
9052 llvm_unreachable("Unexpected SPF");
9053 }
9054}
9055
9057 if (SPF == SPF_SMIN) return SPF_SMAX;
9058 if (SPF == SPF_UMIN) return SPF_UMAX;
9059 if (SPF == SPF_SMAX) return SPF_SMIN;
9060 if (SPF == SPF_UMAX) return SPF_UMIN;
9061 llvm_unreachable("unhandled!");
9062}
9063
9065 switch (MinMaxID) {
9066 case Intrinsic::smax: return Intrinsic::smin;
9067 case Intrinsic::smin: return Intrinsic::smax;
9068 case Intrinsic::umax: return Intrinsic::umin;
9069 case Intrinsic::umin: return Intrinsic::umax;
9070 // Please note that next four intrinsics may produce the same result for
9071 // original and inverted case even if X != Y due to NaN is handled specially.
9072 case Intrinsic::maximum: return Intrinsic::minimum;
9073 case Intrinsic::minimum: return Intrinsic::maximum;
9074 case Intrinsic::maxnum: return Intrinsic::minnum;
9075 case Intrinsic::minnum: return Intrinsic::maxnum;
9076 default: llvm_unreachable("Unexpected intrinsic");
9077 }
9078}
9079
9081 switch (SPF) {
9084 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9085 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9086 default: llvm_unreachable("Unexpected flavor");
9087 }
9088}
9089
9090std::pair<Intrinsic::ID, bool>
9092 // Check if VL contains select instructions that can be folded into a min/max
9093 // vector intrinsic and return the intrinsic if it is possible.
9094 // TODO: Support floating point min/max.
9095 bool AllCmpSingleUse = true;
9096 SelectPatternResult SelectPattern;
9097 SelectPattern.Flavor = SPF_UNKNOWN;
9098 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9099 Value *LHS, *RHS;
9100 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9101 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9102 return false;
9103 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9104 SelectPattern.Flavor != CurrentPattern.Flavor)
9105 return false;
9106 SelectPattern = CurrentPattern;
9107 AllCmpSingleUse &=
9109 return true;
9110 })) {
9111 switch (SelectPattern.Flavor) {
9112 case SPF_SMIN:
9113 return {Intrinsic::smin, AllCmpSingleUse};
9114 case SPF_UMIN:
9115 return {Intrinsic::umin, AllCmpSingleUse};
9116 case SPF_SMAX:
9117 return {Intrinsic::smax, AllCmpSingleUse};
9118 case SPF_UMAX:
9119 return {Intrinsic::umax, AllCmpSingleUse};
9120 case SPF_FMAXNUM:
9121 return {Intrinsic::maxnum, AllCmpSingleUse};
9122 case SPF_FMINNUM:
9123 return {Intrinsic::minnum, AllCmpSingleUse};
9124 default:
9125 llvm_unreachable("unexpected select pattern flavor");
9126 }
9127 }
9128 return {Intrinsic::not_intrinsic, false};
9129}
9130
9131template <typename InstTy>
9132static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9133 Value *&Init, Value *&OtherOp) {
9134 // Handle the case of a simple two-predecessor recurrence PHI.
9135 // There's a lot more that could theoretically be done here, but
9136 // this is sufficient to catch some interesting cases.
9137 // TODO: Expand list -- gep, uadd.sat etc.
9138 if (PN->getNumIncomingValues() != 2)
9139 return false;
9140
9141 for (unsigned I = 0; I != 2; ++I) {
9142 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9143 Operation && Operation->getNumOperands() >= 2) {
9144 Value *LHS = Operation->getOperand(0);
9145 Value *RHS = Operation->getOperand(1);
9146 if (LHS != PN && RHS != PN)
9147 continue;
9148
9149 Inst = Operation;
9150 Init = PN->getIncomingValue(!I);
9151 OtherOp = (LHS == PN) ? RHS : LHS;
9152 return true;
9153 }
9154 }
9155 return false;
9156}
9157
9159 Value *&Start, Value *&Step) {
9160 // We try to match a recurrence of the form:
9161 // %iv = [Start, %entry], [%iv.next, %backedge]
9162 // %iv.next = binop %iv, Step
9163 // Or:
9164 // %iv = [Start, %entry], [%iv.next, %backedge]
9165 // %iv.next = binop Step, %iv
9166 return matchTwoInputRecurrence(P, BO, Start, Step);
9167}
9168
9170 Value *&Start, Value *&Step) {
9171 BinaryOperator *BO = nullptr;
9172 P = dyn_cast<PHINode>(I->getOperand(0));
9173 if (!P)
9174 P = dyn_cast<PHINode>(I->getOperand(1));
9175 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9176}
9177
9179 PHINode *&P, Value *&Init,
9180 Value *&OtherOp) {
9181 // Binary intrinsics only supported for now.
9182 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9183 I->getType() != I->getArgOperand(1)->getType())
9184 return false;
9185
9186 IntrinsicInst *II = nullptr;
9187 P = dyn_cast<PHINode>(I->getArgOperand(0));
9188 if (!P)
9189 P = dyn_cast<PHINode>(I->getArgOperand(1));
9190
9191 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9192}
9193
9194/// Return true if "icmp Pred LHS RHS" is always true.
9196 const Value *RHS) {
9197 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9198 return true;
9199
9200 switch (Pred) {
9201 default:
9202 return false;
9203
9204 case CmpInst::ICMP_SLE: {
9205 const APInt *C;
9206
9207 // LHS s<= LHS +_{nsw} C if C >= 0
9208 // LHS s<= LHS | C if C >= 0
9209 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9211 return !C->isNegative();
9212
9213 // LHS s<= smax(LHS, V) for any V
9215 return true;
9216
9217 // smin(RHS, V) s<= RHS for any V
9219 return true;
9220
9221 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9222 const Value *X;
9223 const APInt *CLHS, *CRHS;
9224 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9226 return CLHS->sle(*CRHS);
9227
9228 return false;
9229 }
9230
9231 case CmpInst::ICMP_ULE: {
9232 // LHS u<= LHS +_{nuw} V for any V
9233 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9235 return true;
9236
9237 // LHS u<= LHS | V for any V
9238 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9239 return true;
9240
9241 // LHS u<= umax(LHS, V) for any V
9243 return true;
9244
9245 // RHS >> V u<= RHS for any V
9246 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9247 return true;
9248
9249 // RHS u/ C_ugt_1 u<= RHS
9250 const APInt *C;
9251 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9252 return true;
9253
9254 // RHS & V u<= RHS for any V
9256 return true;
9257
9258 // umin(RHS, V) u<= RHS for any V
9260 return true;
9261
9262 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9263 const Value *X;
9264 const APInt *CLHS, *CRHS;
9265 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9267 return CLHS->ule(*CRHS);
9268
9269 return false;
9270 }
9271 }
9272}
9273
9274/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9275/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9276static std::optional<bool>
9278 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9279 switch (Pred) {
9280 default:
9281 return std::nullopt;
9282
9283 case CmpInst::ICMP_SLT:
9284 case CmpInst::ICMP_SLE:
9285 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9287 return true;
9288 return std::nullopt;
9289
9290 case CmpInst::ICMP_SGT:
9291 case CmpInst::ICMP_SGE:
9292 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9294 return true;
9295 return std::nullopt;
9296
9297 case CmpInst::ICMP_ULT:
9298 case CmpInst::ICMP_ULE:
9299 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9301 return true;
9302 return std::nullopt;
9303
9304 case CmpInst::ICMP_UGT:
9305 case CmpInst::ICMP_UGE:
9306 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9308 return true;
9309 return std::nullopt;
9310 }
9311}
9312
9313/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9314/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9315/// Otherwise, return std::nullopt if we can't infer anything.
9316static std::optional<bool>
9318 CmpPredicate RPred, const ConstantRange &RCR) {
9319 auto CRImpliesPred = [&](ConstantRange CR,
9320 CmpInst::Predicate Pred) -> std::optional<bool> {
9321 // If all true values for lhs and true for rhs, lhs implies rhs
9322 if (CR.icmp(Pred, RCR))
9323 return true;
9324
9325 // If there is no overlap, lhs implies not rhs
9326 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9327 return false;
9328
9329 return std::nullopt;
9330 };
9331 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9332 RPred))
9333 return Res;
9334 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9336 : LPred.dropSameSign();
9338 : RPred.dropSameSign();
9339 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9340 RPred);
9341 }
9342 return std::nullopt;
9343}
9344
9345/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9346/// is true. Return false if LHS implies RHS is false. Otherwise, return
9347/// std::nullopt if we can't infer anything.
9348static std::optional<bool>
9349isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9350 CmpPredicate RPred, const Value *R0, const Value *R1,
9351 const DataLayout &DL, bool LHSIsTrue) {
9352 // The rest of the logic assumes the LHS condition is true. If that's not the
9353 // case, invert the predicate to make it so.
9354 if (!LHSIsTrue)
9355 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9356
9357 // We can have non-canonical operands, so try to normalize any common operand
9358 // to L0/R0.
9359 if (L0 == R1) {
9360 std::swap(R0, R1);
9361 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9362 }
9363 if (R0 == L1) {
9364 std::swap(L0, L1);
9365 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9366 }
9367 if (L1 == R1) {
9368 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9369 if (L0 != R0 || match(L0, m_ImmConstant())) {
9370 std::swap(L0, L1);
9371 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9372 std::swap(R0, R1);
9373 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9374 }
9375 }
9376
9377 // See if we can infer anything if operand-0 matches and we have at least one
9378 // constant.
9379 const APInt *Unused;
9380 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9381 // Potential TODO: We could also further use the constant range of L0/R0 to
9382 // further constraint the constant ranges. At the moment this leads to
9383 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9384 // C1` (see discussion: D58633).
9386 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9387 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9389 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9390 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9391 // Even if L1/R1 are not both constant, we can still sometimes deduce
9392 // relationship from a single constant. For example X u> Y implies X != 0.
9393 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9394 return R;
9395 // If both L1/R1 were exact constant ranges and we didn't get anything
9396 // here, we won't be able to deduce this.
9397 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9398 return std::nullopt;
9399 }
9400
9401 // Can we infer anything when the two compares have matching operands?
9402 if (L0 == R0 && L1 == R1)
9403 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9404
9405 // It only really makes sense in the context of signed comparison for "X - Y
9406 // must be positive if X >= Y and no overflow".
9407 // Take SGT as an example: L0:x > L1:y and C >= 0
9408 // ==> R0:(x -nsw y) < R1:(-C) is false
9409 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9410 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9411 SignedLPred == ICmpInst::ICMP_SGE) &&
9412 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9413 if (match(R1, m_NonPositive()) &&
9414 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9415 return false;
9416 }
9417
9418 // Take SLT as an example: L0:x < L1:y and C <= 0
9419 // ==> R0:(x -nsw y) < R1:(-C) is true
9420 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9421 SignedLPred == ICmpInst::ICMP_SLE) &&
9422 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9423 if (match(R1, m_NonNegative()) &&
9424 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9425 return true;
9426 }
9427
9428 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9429 if (L0 == R0 &&
9430 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9431 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9432 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9433 return CmpPredicate::getMatching(LPred, RPred).has_value();
9434
9435 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9436 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9437
9438 return std::nullopt;
9439}
9440
9441/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9442/// false. Otherwise, return std::nullopt if we can't infer anything. We
9443/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9444/// instruction.
9445static std::optional<bool>
9447 const Value *RHSOp0, const Value *RHSOp1,
9448 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9449 // The LHS must be an 'or', 'and', or a 'select' instruction.
9450 assert((LHS->getOpcode() == Instruction::And ||
9451 LHS->getOpcode() == Instruction::Or ||
9452 LHS->getOpcode() == Instruction::Select) &&
9453 "Expected LHS to be 'and', 'or', or 'select'.");
9454
9455 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9456
9457 // If the result of an 'or' is false, then we know both legs of the 'or' are
9458 // false. Similarly, if the result of an 'and' is true, then we know both
9459 // legs of the 'and' are true.
9460 const Value *ALHS, *ARHS;
9461 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9462 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9463 // FIXME: Make this non-recursion.
9464 if (std::optional<bool> Implication = isImpliedCondition(
9465 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9466 return Implication;
9467 if (std::optional<bool> Implication = isImpliedCondition(
9468 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9469 return Implication;
9470 return std::nullopt;
9471 }
9472 return std::nullopt;
9473}
9474
9475std::optional<bool>
9477 const Value *RHSOp0, const Value *RHSOp1,
9478 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9479 // Bail out when we hit the limit.
9481 return std::nullopt;
9482
9483 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9484 // example.
9485 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9486 return std::nullopt;
9487
9488 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9489 "Expected integer type only!");
9490
9491 // Match not
9492 if (match(LHS, m_Not(m_Value(LHS))))
9493 LHSIsTrue = !LHSIsTrue;
9494
9495 // Both LHS and RHS are icmps.
9496 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9497 return isImpliedCondICmps(LHSCmp->getCmpPredicate(), LHSCmp->getOperand(0),
9498 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9499 DL, LHSIsTrue);
9500 const Value *V;
9501 if (match(LHS, m_NUWTrunc(m_Value(V))))
9503 ConstantInt::get(V->getType(), 0), RHSPred,
9504 RHSOp0, RHSOp1, DL, LHSIsTrue);
9505
9506 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9507 /// the RHS to be an icmp.
9508 /// FIXME: Add support for and/or/select on the RHS.
9509 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9510 if ((LHSI->getOpcode() == Instruction::And ||
9511 LHSI->getOpcode() == Instruction::Or ||
9512 LHSI->getOpcode() == Instruction::Select))
9513 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9514 Depth);
9515 }
9516 return std::nullopt;
9517}
9518
9519std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9520 const DataLayout &DL,
9521 bool LHSIsTrue, unsigned Depth) {
9522 // LHS ==> RHS by definition
9523 if (LHS == RHS)
9524 return LHSIsTrue;
9525
9526 // Match not
9527 bool InvertRHS = false;
9528 if (match(RHS, m_Not(m_Value(RHS)))) {
9529 if (LHS == RHS)
9530 return !LHSIsTrue;
9531 InvertRHS = true;
9532 }
9533
9534 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9535 if (auto Implied = isImpliedCondition(
9536 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9537 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9538 return InvertRHS ? !*Implied : *Implied;
9539 return std::nullopt;
9540 }
9541
9542 const Value *V;
9543 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9544 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9545 ConstantInt::get(V->getType(), 0), DL,
9546 LHSIsTrue, Depth))
9547 return InvertRHS ? !*Implied : *Implied;
9548 return std::nullopt;
9549 }
9550
9552 return std::nullopt;
9553
9554 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9555 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9556 const Value *RHS1, *RHS2;
9557 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9558 if (std::optional<bool> Imp =
9559 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9560 if (*Imp == true)
9561 return !InvertRHS;
9562 if (std::optional<bool> Imp =
9563 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9564 if (*Imp == true)
9565 return !InvertRHS;
9566 }
9567 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9568 if (std::optional<bool> Imp =
9569 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9570 if (*Imp == false)
9571 return InvertRHS;
9572 if (std::optional<bool> Imp =
9573 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9574 if (*Imp == false)
9575 return InvertRHS;
9576 }
9577
9578 return std::nullopt;
9579}
9580
9581// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9582// condition dominating ContextI or nullptr, if no condition is found.
9583static std::pair<Value *, bool>
9585 if (!ContextI || !ContextI->getParent())
9586 return {nullptr, false};
9587
9588 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9589 // dominator tree (eg, from a SimplifyQuery) instead?
9590 const BasicBlock *ContextBB = ContextI->getParent();
9591 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9592 if (!PredBB)
9593 return {nullptr, false};
9594
9595 // We need a conditional branch in the predecessor.
9596 Value *PredCond;
9597 BasicBlock *TrueBB, *FalseBB;
9598 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9599 return {nullptr, false};
9600
9601 // The branch should get simplified. Don't bother simplifying this condition.
9602 if (TrueBB == FalseBB)
9603 return {nullptr, false};
9604
9605 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9606 "Predecessor block does not point to successor?");
9607
9608 // Is this condition implied by the predecessor condition?
9609 return {PredCond, TrueBB == ContextBB};
9610}
9611
9612std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9613 const Instruction *ContextI,
9614 const DataLayout &DL) {
9615 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9616 auto PredCond = getDomPredecessorCondition(ContextI);
9617 if (PredCond.first)
9618 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9619 return std::nullopt;
9620}
9621
9623 const Value *LHS,
9624 const Value *RHS,
9625 const Instruction *ContextI,
9626 const DataLayout &DL) {
9627 auto PredCond = getDomPredecessorCondition(ContextI);
9628 if (PredCond.first)
9629 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9630 PredCond.second);
9631 return std::nullopt;
9632}
9633
9635 APInt &Upper, const InstrInfoQuery &IIQ,
9636 bool PreferSignedRange) {
9637 unsigned Width = Lower.getBitWidth();
9638 const APInt *C;
9639 switch (BO.getOpcode()) {
9640 case Instruction::Sub:
9641 if (match(BO.getOperand(0), m_APInt(C))) {
9642 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9643 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9644
9645 // If the caller expects a signed compare, then try to use a signed range.
9646 // Otherwise if both no-wraps are set, use the unsigned range because it
9647 // is never larger than the signed range. Example:
9648 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9649 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9650 if (PreferSignedRange && HasNSW && HasNUW)
9651 HasNUW = false;
9652
9653 if (HasNUW) {
9654 // 'sub nuw c, x' produces [0, C].
9655 Upper = *C + 1;
9656 } else if (HasNSW) {
9657 if (C->isNegative()) {
9658 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9660 Upper = *C - APInt::getSignedMaxValue(Width);
9661 } else {
9662 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9663 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9664 Lower = *C - APInt::getSignedMaxValue(Width);
9666 }
9667 }
9668 }
9669 break;
9670 case Instruction::Add:
9671 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9672 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9673 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9674
9675 // If the caller expects a signed compare, then try to use a signed
9676 // range. Otherwise if both no-wraps are set, use the unsigned range
9677 // because it is never larger than the signed range. Example: "add nuw
9678 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9679 if (PreferSignedRange && HasNSW && HasNUW)
9680 HasNUW = false;
9681
9682 if (HasNUW) {
9683 // 'add nuw x, C' produces [C, UINT_MAX].
9684 Lower = *C;
9685 } else if (HasNSW) {
9686 if (C->isNegative()) {
9687 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9689 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9690 } else {
9691 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9692 Lower = APInt::getSignedMinValue(Width) + *C;
9693 Upper = APInt::getSignedMaxValue(Width) + 1;
9694 }
9695 }
9696 }
9697 break;
9698
9699 case Instruction::And:
9700 if (match(BO.getOperand(1), m_APInt(C)))
9701 // 'and x, C' produces [0, C].
9702 Upper = *C + 1;
9703 // X & -X is a power of two or zero. So we can cap the value at max power of
9704 // two.
9705 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9706 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9707 Upper = APInt::getSignedMinValue(Width) + 1;
9708 break;
9709
9710 case Instruction::Or:
9711 if (match(BO.getOperand(1), m_APInt(C)))
9712 // 'or x, C' produces [C, UINT_MAX].
9713 Lower = *C;
9714 break;
9715
9716 case Instruction::AShr:
9717 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9718 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9720 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9721 } else if (match(BO.getOperand(0), m_APInt(C))) {
9722 unsigned ShiftAmount = Width - 1;
9723 if (!C->isZero() && IIQ.isExact(&BO))
9724 ShiftAmount = C->countr_zero();
9725 if (C->isNegative()) {
9726 // 'ashr C, x' produces [C, C >> (Width-1)]
9727 Lower = *C;
9728 Upper = C->ashr(ShiftAmount) + 1;
9729 } else {
9730 // 'ashr C, x' produces [C >> (Width-1), C]
9731 Lower = C->ashr(ShiftAmount);
9732 Upper = *C + 1;
9733 }
9734 }
9735 break;
9736
9737 case Instruction::LShr:
9738 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9739 // 'lshr x, C' produces [0, UINT_MAX >> C].
9740 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9741 } else if (match(BO.getOperand(0), m_APInt(C))) {
9742 // 'lshr C, x' produces [C >> (Width-1), C].
9743 unsigned ShiftAmount = Width - 1;
9744 if (!C->isZero() && IIQ.isExact(&BO))
9745 ShiftAmount = C->countr_zero();
9746 Lower = C->lshr(ShiftAmount);
9747 Upper = *C + 1;
9748 }
9749 break;
9750
9751 case Instruction::Shl:
9752 if (match(BO.getOperand(0), m_APInt(C))) {
9753 if (IIQ.hasNoUnsignedWrap(&BO)) {
9754 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9755 Lower = *C;
9756 Upper = Lower.shl(Lower.countl_zero()) + 1;
9757 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9758 if (C->isNegative()) {
9759 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9760 unsigned ShiftAmount = C->countl_one() - 1;
9761 Lower = C->shl(ShiftAmount);
9762 Upper = *C + 1;
9763 } else {
9764 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9765 unsigned ShiftAmount = C->countl_zero() - 1;
9766 Lower = *C;
9767 Upper = C->shl(ShiftAmount) + 1;
9768 }
9769 } else {
9770 // If lowbit is set, value can never be zero.
9771 if ((*C)[0])
9772 Lower = APInt::getOneBitSet(Width, 0);
9773 // If we are shifting a constant the largest it can be is if the longest
9774 // sequence of consecutive ones is shifted to the highbits (breaking
9775 // ties for which sequence is higher). At the moment we take a liberal
9776 // upper bound on this by just popcounting the constant.
9777 // TODO: There may be a bitwise trick for it longest/highest
9778 // consecutative sequence of ones (naive method is O(Width) loop).
9779 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9780 }
9781 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9782 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9783 }
9784 break;
9785
9786 case Instruction::SDiv:
9787 if (match(BO.getOperand(1), m_APInt(C))) {
9788 APInt IntMin = APInt::getSignedMinValue(Width);
9789 APInt IntMax = APInt::getSignedMaxValue(Width);
9790 if (C->isAllOnes()) {
9791 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9792 // where C != -1 and C != 0 and C != 1
9793 Lower = IntMin + 1;
9794 Upper = IntMax + 1;
9795 } else if (C->countl_zero() < Width - 1) {
9796 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9797 // where C != -1 and C != 0 and C != 1
9798 Lower = IntMin.sdiv(*C);
9799 Upper = IntMax.sdiv(*C);
9800 if (Lower.sgt(Upper))
9802 Upper = Upper + 1;
9803 assert(Upper != Lower && "Upper part of range has wrapped!");
9804 }
9805 } else if (match(BO.getOperand(0), m_APInt(C))) {
9806 if (C->isMinSignedValue()) {
9807 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9808 Lower = *C;
9809 Upper = Lower.lshr(1) + 1;
9810 } else {
9811 // 'sdiv C, x' produces [-|C|, |C|].
9812 Upper = C->abs() + 1;
9813 Lower = (-Upper) + 1;
9814 }
9815 }
9816 break;
9817
9818 case Instruction::UDiv:
9819 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9820 // 'udiv x, C' produces [0, UINT_MAX / C].
9821 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9822 } else if (match(BO.getOperand(0), m_APInt(C))) {
9823 // 'udiv C, x' produces [0, C].
9824 Upper = *C + 1;
9825 }
9826 break;
9827
9828 case Instruction::SRem:
9829 if (match(BO.getOperand(1), m_APInt(C))) {
9830 // 'srem x, C' produces (-|C|, |C|).
9831 Upper = C->abs();
9832 Lower = (-Upper) + 1;
9833 } else if (match(BO.getOperand(0), m_APInt(C))) {
9834 if (C->isNegative()) {
9835 // 'srem -|C|, x' produces [-|C|, 0].
9836 Upper = 1;
9837 Lower = *C;
9838 } else {
9839 // 'srem |C|, x' produces [0, |C|].
9840 Upper = *C + 1;
9841 }
9842 }
9843 break;
9844
9845 case Instruction::URem:
9846 if (match(BO.getOperand(1), m_APInt(C)))
9847 // 'urem x, C' produces [0, C).
9848 Upper = *C;
9849 else if (match(BO.getOperand(0), m_APInt(C)))
9850 // 'urem C, x' produces [0, C].
9851 Upper = *C + 1;
9852 break;
9853
9854 default:
9855 break;
9856 }
9857}
9858
9860 bool UseInstrInfo) {
9861 unsigned Width = II.getType()->getScalarSizeInBits();
9862 const APInt *C;
9863 switch (II.getIntrinsicID()) {
9864 case Intrinsic::ctlz:
9865 case Intrinsic::cttz: {
9866 APInt Upper(Width, Width);
9867 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
9868 Upper += 1;
9869 // Maximum of set/clear bits is the bit width.
9871 }
9872 case Intrinsic::ctpop:
9873 // Maximum of set/clear bits is the bit width.
9875 APInt(Width, Width) + 1);
9876 case Intrinsic::uadd_sat:
9877 // uadd.sat(x, C) produces [C, UINT_MAX].
9878 if (match(II.getOperand(0), m_APInt(C)) ||
9879 match(II.getOperand(1), m_APInt(C)))
9881 break;
9882 case Intrinsic::sadd_sat:
9883 if (match(II.getOperand(0), m_APInt(C)) ||
9884 match(II.getOperand(1), m_APInt(C))) {
9885 if (C->isNegative())
9886 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9888 APInt::getSignedMaxValue(Width) + *C +
9889 1);
9890
9891 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9893 APInt::getSignedMaxValue(Width) + 1);
9894 }
9895 break;
9896 case Intrinsic::usub_sat:
9897 // usub.sat(C, x) produces [0, C].
9898 if (match(II.getOperand(0), m_APInt(C)))
9899 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9900
9901 // usub.sat(x, C) produces [0, UINT_MAX - C].
9902 if (match(II.getOperand(1), m_APInt(C)))
9904 APInt::getMaxValue(Width) - *C + 1);
9905 break;
9906 case Intrinsic::ssub_sat:
9907 if (match(II.getOperand(0), m_APInt(C))) {
9908 if (C->isNegative())
9909 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9911 *C - APInt::getSignedMinValue(Width) +
9912 1);
9913
9914 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9916 APInt::getSignedMaxValue(Width) + 1);
9917 } else if (match(II.getOperand(1), m_APInt(C))) {
9918 if (C->isNegative())
9919 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9921 APInt::getSignedMaxValue(Width) + 1);
9922
9923 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9925 APInt::getSignedMaxValue(Width) - *C +
9926 1);
9927 }
9928 break;
9929 case Intrinsic::umin:
9930 case Intrinsic::umax:
9931 case Intrinsic::smin:
9932 case Intrinsic::smax:
9933 if (!match(II.getOperand(0), m_APInt(C)) &&
9934 !match(II.getOperand(1), m_APInt(C)))
9935 break;
9936
9937 switch (II.getIntrinsicID()) {
9938 case Intrinsic::umin:
9939 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9940 case Intrinsic::umax:
9942 case Intrinsic::smin:
9944 *C + 1);
9945 case Intrinsic::smax:
9947 APInt::getSignedMaxValue(Width) + 1);
9948 default:
9949 llvm_unreachable("Must be min/max intrinsic");
9950 }
9951 break;
9952 case Intrinsic::abs:
9953 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
9954 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9955 if (match(II.getOperand(1), m_One()))
9957 APInt::getSignedMaxValue(Width) + 1);
9958
9960 APInt::getSignedMinValue(Width) + 1);
9961 case Intrinsic::vscale:
9962 if (!II.getParent() || !II.getFunction())
9963 break;
9964 return getVScaleRange(II.getFunction(), Width);
9965 default:
9966 break;
9967 }
9968
9969 return ConstantRange::getFull(Width);
9970}
9971
9973 const InstrInfoQuery &IIQ) {
9974 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
9975 const Value *LHS = nullptr, *RHS = nullptr;
9977 if (R.Flavor == SPF_UNKNOWN)
9978 return ConstantRange::getFull(BitWidth);
9979
9980 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
9981 // If the negation part of the abs (in RHS) has the NSW flag,
9982 // then the result of abs(X) is [0..SIGNED_MAX],
9983 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9984 if (match(RHS, m_Neg(m_Specific(LHS))) &&
9988
9991 }
9992
9993 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
9994 // The result of -abs(X) is <= 0.
9996 APInt(BitWidth, 1));
9997 }
9998
9999 const APInt *C;
10000 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10001 return ConstantRange::getFull(BitWidth);
10002
10003 switch (R.Flavor) {
10004 case SPF_UMIN:
10006 case SPF_UMAX:
10008 case SPF_SMIN:
10010 *C + 1);
10011 case SPF_SMAX:
10014 default:
10015 return ConstantRange::getFull(BitWidth);
10016 }
10017}
10018
10020 // The maximum representable value of a half is 65504. For floats the maximum
10021 // value is 3.4e38 which requires roughly 129 bits.
10022 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10023 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10024 return;
10025 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10026 Lower = APInt(BitWidth, -65504, true);
10027 Upper = APInt(BitWidth, 65505);
10028 }
10029
10030 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10031 // For a fptoui the lower limit is left as 0.
10032 Upper = APInt(BitWidth, 65505);
10033 }
10034}
10035
10037 bool UseInstrInfo, AssumptionCache *AC,
10038 const Instruction *CtxI,
10039 const DominatorTree *DT,
10040 unsigned Depth) {
10041 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10042
10044 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10045
10046 if (auto *C = dyn_cast<Constant>(V))
10047 return C->toConstantRange();
10048
10049 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10050 InstrInfoQuery IIQ(UseInstrInfo);
10051 ConstantRange CR = ConstantRange::getFull(BitWidth);
10052 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10053 APInt Lower = APInt(BitWidth, 0);
10054 APInt Upper = APInt(BitWidth, 0);
10055 // TODO: Return ConstantRange.
10056 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10058 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10059 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10060 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10062 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10064 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10065 CR = CRTrue.unionWith(CRFalse);
10067 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10068 APInt Lower = APInt(BitWidth, 0);
10069 APInt Upper = APInt(BitWidth, 0);
10070 // TODO: Return ConstantRange.
10073 } else if (const auto *A = dyn_cast<Argument>(V))
10074 if (std::optional<ConstantRange> Range = A->getRange())
10075 CR = *Range;
10076
10077 if (auto *I = dyn_cast<Instruction>(V)) {
10078 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10080
10081 if (const auto *CB = dyn_cast<CallBase>(V))
10082 if (std::optional<ConstantRange> Range = CB->getRange())
10083 CR = CR.intersectWith(*Range);
10084 }
10085
10086 if (CtxI && AC) {
10087 // Try to restrict the range based on information from assumptions.
10088 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10089 if (!AssumeVH)
10090 continue;
10091 CallInst *I = cast<CallInst>(AssumeVH);
10092 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10093 "Got assumption for the wrong function!");
10094 assert(I->getIntrinsicID() == Intrinsic::assume &&
10095 "must be an assume intrinsic");
10096
10097 if (!isValidAssumeForContext(I, CtxI, DT))
10098 continue;
10099 Value *Arg = I->getArgOperand(0);
10100 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10101 // Currently we just use information from comparisons.
10102 if (!Cmp || Cmp->getOperand(0) != V)
10103 continue;
10104 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10105 ConstantRange RHS =
10106 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10107 UseInstrInfo, AC, I, DT, Depth + 1);
10108 CR = CR.intersectWith(
10109 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10110 }
10111 }
10112
10113 return CR;
10114}
10115
10116static void
10118 function_ref<void(Value *)> InsertAffected) {
10119 assert(V != nullptr);
10120 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10121 InsertAffected(V);
10122 } else if (auto *I = dyn_cast<Instruction>(V)) {
10123 InsertAffected(V);
10124
10125 // Peek through unary operators to find the source of the condition.
10126 Value *Op;
10129 InsertAffected(Op);
10130 }
10131 }
10132}
10133
10135 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10136 auto AddAffected = [&InsertAffected](Value *V) {
10137 addValueAffectedByCondition(V, InsertAffected);
10138 };
10139
10140 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10141 if (IsAssume) {
10142 AddAffected(LHS);
10143 AddAffected(RHS);
10144 } else if (match(RHS, m_Constant()))
10145 AddAffected(LHS);
10146 };
10147
10148 SmallVector<Value *, 8> Worklist;
10150 Worklist.push_back(Cond);
10151 while (!Worklist.empty()) {
10152 Value *V = Worklist.pop_back_val();
10153 if (!Visited.insert(V).second)
10154 continue;
10155
10156 CmpPredicate Pred;
10157 Value *A, *B, *X;
10158
10159 if (IsAssume) {
10160 AddAffected(V);
10161 if (match(V, m_Not(m_Value(X))))
10162 AddAffected(X);
10163 }
10164
10165 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10166 // assume(A && B) is split to -> assume(A); assume(B);
10167 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10168 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10169 // enough information to be worth handling (intersection of information as
10170 // opposed to union).
10171 if (!IsAssume) {
10172 Worklist.push_back(A);
10173 Worklist.push_back(B);
10174 }
10175 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10176 bool HasRHSC = match(B, m_ConstantInt());
10177 if (ICmpInst::isEquality(Pred)) {
10178 AddAffected(A);
10179 if (IsAssume)
10180 AddAffected(B);
10181 if (HasRHSC) {
10182 Value *Y;
10183 // (X & C) or (X | C).
10184 // (X << C) or (X >>_s C) or (X >>_u C).
10185 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10186 AddAffected(X);
10187 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10188 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10189 AddAffected(X);
10190 AddAffected(Y);
10191 }
10192 }
10193 } else {
10194 AddCmpOperands(A, B);
10195 if (HasRHSC) {
10196 // Handle (A + C1) u< C2, which is the canonical form of
10197 // A > C3 && A < C4.
10199 AddAffected(X);
10200
10201 if (ICmpInst::isUnsigned(Pred)) {
10202 Value *Y;
10203 // X & Y u> C -> X >u C && Y >u C
10204 // X | Y u< C -> X u< C && Y u< C
10205 // X nuw+ Y u< C -> X u< C && Y u< C
10206 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10207 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10208 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10209 AddAffected(X);
10210 AddAffected(Y);
10211 }
10212 // X nuw- Y u> C -> X u> C
10213 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10214 AddAffected(X);
10215 }
10216 }
10217
10218 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10219 // by computeKnownFPClass().
10221 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10222 InsertAffected(X);
10223 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10224 InsertAffected(X);
10225 }
10226 }
10227
10228 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10229 AddAffected(X);
10230 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10231 AddCmpOperands(A, B);
10232
10233 // fcmp fneg(x), y
10234 // fcmp fabs(x), y
10235 // fcmp fneg(fabs(x)), y
10236 if (match(A, m_FNeg(m_Value(A))))
10237 AddAffected(A);
10238 if (match(A, m_FAbs(m_Value(A))))
10239 AddAffected(A);
10240
10242 m_Value()))) {
10243 // Handle patterns that computeKnownFPClass() support.
10244 AddAffected(A);
10245 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10246 // Assume is checked here as X is already added above for assumes in
10247 // addValueAffectedByCondition
10248 AddAffected(X);
10249 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10250 // Assume is checked here to avoid issues with ephemeral values
10251 Worklist.push_back(X);
10252 }
10253 }
10254}
10255
10257 // (X >> C) or/add (X & mask(C) != 0)
10258 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10259 if (BO->getOpcode() == Instruction::Add ||
10260 BO->getOpcode() == Instruction::Or) {
10261 const Value *X;
10262 const APInt *C1, *C2;
10263 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10267 m_Zero())))) &&
10268 C2->popcount() == C1->getZExtValue())
10269 return X;
10270 }
10271 }
10272 return nullptr;
10273}
10274
10276 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10277}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
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...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Utilities for dealing with flags related to floating point properties and mode controls.
Hexagon Common GEP
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
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
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 SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton 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:247
static SmallVector< VPValue *, 4 > getOperands(ArrayRef< VPValue * > Values, unsigned OperandIndex)
Definition VPlanSLP.cpp:210
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext, unsigned Depth=0)
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, SimplifyQuery &Q, unsigned Depth)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
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 isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V1 == (binop V2, X), where X is known non-zero.
static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q, unsigned Depth)
Test whether a GEP's result is known to be non-null.
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static bool 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 isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
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 KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
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 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 isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
UndefPoisonKind
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static bool includesPoison(UndefPoisonKind Kind)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, 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 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 ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, 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 void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, 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 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 bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, 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 ?
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 bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
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 unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
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 KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
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 bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
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 PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
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 void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
bool isFinite() const
Definition APFloat.h:1454
bool isNaN() const
Definition APFloat.h:1447
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1138
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1098
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1079
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
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:1406
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:1540
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1391
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1670
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1385
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:1764
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
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
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
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1396
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
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:1628
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:219
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1041
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:356
unsigned logBase2() const
Definition APInt.h:1761
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
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:1388
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
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:1449
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
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:136
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
iterator begin() const
Definition ArrayRef.h:135
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
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:191
Class to represent array types.
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.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:223
LLVM_ABI bool isSingleEdge() const
Check if this is the only edge between Start and End.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:472
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
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:233
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:374
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
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:448
This class is the base class for the comparison instructions.
Definition InstrTypes.h:666
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:678
@ ICMP_SLT
signed less than
Definition InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:708
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:684
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:693
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:682
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:683
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:702
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:701
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:705
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:692
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:703
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:690
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:685
@ ICMP_NE
not equal
Definition InstrTypes.h:700
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:706
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:704
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:691
bool isSigned() const
Definition InstrTypes.h:932
static LLVM_ABI 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:829
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:944
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:772
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:791
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:767
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:895
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:778
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:938
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:702
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:593
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:668
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:776
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
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:163
This class represents a range of values.
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 LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI bool isAllNegative() const
Return true if all values in this range are negative.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI 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...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI 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...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI 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.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI 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:43
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition Constants.cpp:76
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
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:198
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:669
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() 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:165
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static LLVM_ABI 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:200
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:22
bool noSignedZeros() const
Definition FMF.h:67
bool noInfs() const
Definition FMF.h:66
void setNoSignedZeros(bool B=true)
Definition FMF.h:84
void setNoNaNs(bool B=true)
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:65
const BasicBlock & getEntryBlock() const
Definition Function.h:807
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition Function.cpp:803
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
PointerType * getType() const
Global values are always pointers.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:132
Type * getValueType() const
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
CmpPredicate getInverseCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static LLVM_ABI 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.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isUnaryOp() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
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:40
Metadata node.
Definition Metadata.h:1077
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
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 LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A udiv, sdiv, lshr, or ashr instruction, which can be marked as "exact", indicating that no bits are ...
Definition Operator.h:154
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition Operator.h:173
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 LLVM_ABI 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:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:581
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:621
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:652
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Type * getElementType(unsigned N) const
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
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:298
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:246
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:295
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:296
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:311
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:231
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:270
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:301
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:107
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
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:61
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
op_range operands()
Definition User.h:292
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
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:759
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition WithCache.h:59
PointerType getValue() const
Definition WithCache.h:57
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:130
A range adaptor for a pair of iterators.
CallInst * Call
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.
LLVM_ABI APInt ScaleBitMask(const APInt &A, unsigned NewBitWidth, bool MatchAllBits=false)
Splat/Merge neighboring bits to widen/narrow the bitmask represented by.
Definition APInt.cpp:3009
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
BinaryOp_match< SpecificConstantMatch, SrcTy, TargetOpcode::G_SUB > m_Neg(const SrcTy &&Src)
Matches a register negated by a G_SUB.
BinaryOp_match< SrcTy, SpecificConstantMatch, TargetOpcode::G_XOR, true > m_Not(const SrcTy &&Src)
Matches a register not-ed by a G_XOR.
OneUse_match< SubPat > m_OneUse(const SubPat &SP)
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
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.
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.
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.
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.
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
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.
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
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.
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
IntrinsicID_match m_VScale()
Matches a call to llvm.vscale().
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.
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()...
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.
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)
match_immconstant_ty m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
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.
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.
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.
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.
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...
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.
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.
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.
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".
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.
MatchFunctor< Val, Pattern > match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
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.
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:666
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI 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.
LLVM_ABI 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...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
@ Offset
Definition DWP.cpp:477
@ Length
Definition DWP.cpp:477
@ 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.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
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:1705
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI 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:1657
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI 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...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition ScopeExit.h:59
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI 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.
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
LLVM_ABI 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.
LLVM_ABI 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.
LLVM_ABI 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:229
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI 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.
LLVM_ABI 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:289
LLVM_ABI 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:2116
LLVM_ABI 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:416
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:293
gep_type_iterator gep_type_end(const User *GEP)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1534
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:682
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:348
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI 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.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
LLVM_ABI 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.
LLVM_ABI 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:186
LLVM_ABI 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...
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
LLVM_ABI 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,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:759
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:1712
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI 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:342
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
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.
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI 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.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI 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...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI 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...
generic_gep_type_iterator<> gep_type_iterator
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI 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'.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI 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...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
LLVM_ABI 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
LLVM_ABI RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
LLVM_ABI 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, bool IgnoreUBImplyingAttrs=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
LLVM_ABI bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
LLVM_ABI Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
LLVM_ABI const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
LLVM_ABI Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), 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...
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
LLVM_ABI 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.
LLVM_ABI 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.
LLVM_ABI 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
LLVM_ABI 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.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
constexpr unsigned BitWidth
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &SQ, unsigned Depth=0)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point value can never contain a NaN or infinity.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
gep_type_iterator gep_type_begin(const User *GEP)
LLVM_ABI 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...
LLVM_ABI 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...
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1877
LLVM_ABI 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
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI 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...
LLVM_ABI 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.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI 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...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI 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...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI 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.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI 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:853
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:324
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:374
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
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.
@ PositiveZero
Denormals are flushed to positive zero.
@ IEEE
IEEE-754 denormal numbers preserved.
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...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:294
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static LLVM_ABI std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:179
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:248
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:101
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:117
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:244
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:235
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:267
LLVM_ABI 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...
void makeNegative()
Make this value negative.
Definition KnownBits.h:112
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:154
KnownBits byteSwap() const
Definition KnownBits.h:507
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:282
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:511
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:165
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
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:314
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:104
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:218
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:304
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:173
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:238
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:340
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:189
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:241
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:138
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI 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 LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:319
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:98
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:346
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:273
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:92
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:212
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
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:160
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:199
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)
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a 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.
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.
LLVM_ABI void propagateCanonicalizingSrc(const KnownFPClass &Src, DenormalMode Mode)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void signBitMustBeZero()
Assume the sign bit is zero.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a negative zero.
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
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC