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