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() && all_of(I->users(), [](const User *U) {
254 return match(U, m_ICmp(m_Value(), m_Zero()));
255 });
256}
257
259 return !I->user_empty() && all_of(I->users(), [](const User *U) {
260 CmpPredicate P;
261 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
262 });
263}
264
266 bool OrZero, AssumptionCache *AC,
267 const Instruction *CxtI,
268 const DominatorTree *DT, bool UseInstrInfo,
269 unsigned Depth) {
270 return ::isKnownToBeAPowerOfTwo(
271 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
272 Depth);
273}
274
275static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
276 const SimplifyQuery &Q, unsigned Depth);
277
279 unsigned Depth) {
280 return computeKnownBits(V, SQ, Depth).isNonNegative();
281}
282
284 unsigned Depth) {
285 if (auto *CI = dyn_cast<ConstantInt>(V))
286 return CI->getValue().isStrictlyPositive();
287
288 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
289 // this updated.
290 KnownBits Known = computeKnownBits(V, SQ, Depth);
291 return Known.isNonNegative() &&
292 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
293}
294
296 unsigned Depth) {
297 return computeKnownBits(V, SQ, Depth).isNegative();
298}
299
300static bool isKnownNonEqual(const Value *V1, const Value *V2,
301 const APInt &DemandedElts, const SimplifyQuery &Q,
302 unsigned Depth);
303
304bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
305 const SimplifyQuery &Q, unsigned Depth) {
306 // We don't support looking through casts.
307 if (V1 == V2 || V1->getType() != V2->getType())
308 return false;
309 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
310 APInt DemandedElts =
311 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
312 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
313}
314
315bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
316 const SimplifyQuery &SQ, unsigned Depth) {
317 KnownBits Known(Mask.getBitWidth());
318 computeKnownBits(V, Known, SQ, Depth);
319 return Mask.isSubsetOf(Known.Zero);
320}
321
322static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
323 const SimplifyQuery &Q, unsigned Depth);
324
325static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
326 unsigned Depth = 0) {
327 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
328 APInt DemandedElts =
329 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
330 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
331}
332
333unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
334 AssumptionCache *AC, const Instruction *CxtI,
335 const DominatorTree *DT, bool UseInstrInfo,
336 unsigned Depth) {
337 return ::ComputeNumSignBits(
338 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
339}
340
342 AssumptionCache *AC,
343 const Instruction *CxtI,
344 const DominatorTree *DT,
345 unsigned Depth) {
346 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
347 return V->getType()->getScalarSizeInBits() - SignBits + 1;
348}
349
350static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
351 bool NSW, bool NUW,
352 const APInt &DemandedElts,
353 KnownBits &KnownOut, KnownBits &Known2,
354 const SimplifyQuery &Q, unsigned Depth) {
355 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
356
357 // If one operand is unknown and we have no nowrap information,
358 // the result will be unknown independently of the second operand.
359 if (KnownOut.isUnknown() && !NSW && !NUW)
360 return;
361
362 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
363 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
364
365 if (!Add && NSW && !KnownOut.isNonNegative() &&
367 .value_or(false))
368 KnownOut.makeNonNegative();
369}
370
371static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
372 bool NUW, const APInt &DemandedElts,
373 KnownBits &Known, KnownBits &Known2,
374 const SimplifyQuery &Q, unsigned Depth) {
375 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
376 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
377
378 bool isKnownNegative = false;
379 bool isKnownNonNegative = false;
380 // If the multiplication is known not to overflow, compute the sign bit.
381 if (NSW) {
382 if (Op0 == Op1) {
383 // The product of a number with itself is non-negative.
384 isKnownNonNegative = true;
385 } else {
386 bool isKnownNonNegativeOp1 = Known.isNonNegative();
387 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
388 bool isKnownNegativeOp1 = Known.isNegative();
389 bool isKnownNegativeOp0 = Known2.isNegative();
390 // The product of two numbers with the same sign is non-negative.
391 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
392 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
393 if (!isKnownNonNegative && NUW) {
394 // mul nuw nsw with a factor > 1 is non-negative.
396 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
397 KnownBits::sgt(Known2, One).value_or(false);
398 }
399
400 // The product of a negative number and a non-negative number is either
401 // negative or zero.
404 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
405 Known2.isNonZero()) ||
406 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
407 }
408 }
409
410 bool SelfMultiply = Op0 == Op1;
411 if (SelfMultiply)
412 SelfMultiply &=
413 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
414 Known = KnownBits::mul(Known, Known2, SelfMultiply);
415
416 if (SelfMultiply) {
417 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
418 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
419 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
420
421 if (OutValidBits < TyBits) {
422 APInt KnownZeroMask =
423 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
424 Known.Zero |= KnownZeroMask;
425 }
426 }
427
428 // Only make use of no-wrap flags if we failed to compute the sign bit
429 // directly. This matters if the multiplication always overflows, in
430 // which case we prefer to follow the result of the direct computation,
431 // though as the program is invoking undefined behaviour we can choose
432 // whatever we like here.
433 if (isKnownNonNegative && !Known.isNegative())
434 Known.makeNonNegative();
435 else if (isKnownNegative && !Known.isNonNegative())
436 Known.makeNegative();
437}
438
440 KnownBits &Known) {
441 unsigned BitWidth = Known.getBitWidth();
442 unsigned NumRanges = Ranges.getNumOperands() / 2;
443 assert(NumRanges >= 1);
444
445 Known.Zero.setAllBits();
446 Known.One.setAllBits();
447
448 for (unsigned i = 0; i < NumRanges; ++i) {
450 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
452 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
453 ConstantRange Range(Lower->getValue(), Upper->getValue());
454 // BitWidth must equal the Ranges BitWidth for the correct number of high
455 // bits to be set.
456 assert(BitWidth == Range.getBitWidth() &&
457 "Known bit width must match range bit width!");
458
459 // The first CommonPrefixBits of all values in Range are equal.
460 unsigned CommonPrefixBits =
461 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
462 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
463 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
464 Known.One &= UnsignedMax & Mask;
465 Known.Zero &= ~UnsignedMax & Mask;
466 }
467}
468
469static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
473
474 // The instruction defining an assumption's condition itself is always
475 // considered ephemeral to that assumption (even if it has other
476 // non-ephemeral users). See r246696's test case for an example.
477 if (is_contained(I->operands(), E))
478 return true;
479
480 while (!WorkSet.empty()) {
481 const Instruction *V = WorkSet.pop_back_val();
482 if (!Visited.insert(V).second)
483 continue;
484
485 // If all uses of this value are ephemeral, then so is this value.
486 if (all_of(V->users(), [&](const User *U) {
487 return EphValues.count(cast<Instruction>(U));
488 })) {
489 if (V == E)
490 return true;
491
492 if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {
493 EphValues.insert(V);
494
495 if (const User *U = dyn_cast<User>(V)) {
496 for (const Use &U : U->operands()) {
497 if (const auto *I = dyn_cast<Instruction>(U.get()))
498 WorkSet.push_back(I);
499 }
500 }
501 }
502 }
503 }
504
505 return false;
506}
507
508// Is this an intrinsic that cannot be speculated but also cannot trap?
510 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
511 return CI->isAssumeLikeIntrinsic();
512
513 return false;
514}
515
517 const Instruction *CxtI,
518 const DominatorTree *DT,
519 bool AllowEphemerals) {
520 // There are two restrictions on the use of an assume:
521 // 1. The assume must dominate the context (or the control flow must
522 // reach the assume whenever it reaches the context).
523 // 2. The context must not be in the assume's set of ephemeral values
524 // (otherwise we will use the assume to prove that the condition
525 // feeding the assume is trivially true, thus causing the removal of
526 // the assume).
527
528 if (Inv->getParent() == CxtI->getParent()) {
529 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
530 // in the BB.
531 if (Inv->comesBefore(CxtI))
532 return true;
533
534 // Don't let an assume affect itself - this would cause the problems
535 // `isEphemeralValueOf` is trying to prevent, and it would also make
536 // the loop below go out of bounds.
537 if (!AllowEphemerals && Inv == CxtI)
538 return false;
539
540 // The context comes first, but they're both in the same block.
541 // Make sure there is nothing in between that might interrupt
542 // the control flow, not even CxtI itself.
543 // We limit the scan distance between the assume and its context instruction
544 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
545 // it can be adjusted if needed (could be turned into a cl::opt).
546 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
548 return false;
549
550 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
551 }
552
553 // Inv and CxtI are in different blocks.
554 if (DT) {
555 if (DT->dominates(Inv, CxtI))
556 return true;
557 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
558 Inv->getParent()->isEntryBlock()) {
559 // We don't have a DT, but this trivially dominates.
560 return true;
561 }
562
563 return false;
564}
565
566// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
567// we still have enough information about `RHS` to conclude non-zero. For
568// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
569// so the extra compile time may not be worth it, but possibly a second API
570// should be created for use outside of loops.
571static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
572 // v u> y implies v != 0.
573 if (Pred == ICmpInst::ICMP_UGT)
574 return true;
575
576 // Special-case v != 0 to also handle v != null.
577 if (Pred == ICmpInst::ICMP_NE)
578 return match(RHS, m_Zero());
579
580 // All other predicates - rely on generic ConstantRange handling.
581 const APInt *C;
582 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
583 if (match(RHS, m_APInt(C))) {
585 return !TrueValues.contains(Zero);
586 }
587
589 if (VC == nullptr)
590 return false;
591
592 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
593 ++ElemIdx) {
595 Pred, VC->getElementAsAPInt(ElemIdx));
596 if (TrueValues.contains(Zero))
597 return false;
598 }
599 return true;
600}
601
602static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
603 Value *&ValOut, Instruction *&CtxIOut,
604 const PHINode **PhiOut = nullptr) {
605 ValOut = U->get();
606 if (ValOut == PHI)
607 return;
608 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
609 if (PhiOut)
610 *PhiOut = PHI;
611 Value *V;
612 // If the Use is a select of this phi, compute analysis on other arm to break
613 // recursion.
614 // TODO: Min/Max
615 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
616 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
617 ValOut = V;
618
619 // Same for select, if this phi is 2-operand phi, compute analysis on other
620 // incoming value to break recursion.
621 // TODO: We could handle any number of incoming edges as long as we only have
622 // two unique values.
623 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
624 IncPhi && IncPhi->getNumIncomingValues() == 2) {
625 for (int Idx = 0; Idx < 2; ++Idx) {
626 if (IncPhi->getIncomingValue(Idx) == PHI) {
627 ValOut = IncPhi->getIncomingValue(1 - Idx);
628 if (PhiOut)
629 *PhiOut = IncPhi;
630 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
631 break;
632 }
633 }
634 }
635}
636
637static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
638 // Use of assumptions is context-sensitive. If we don't have a context, we
639 // cannot use them!
640 if (!Q.AC || !Q.CxtI)
641 return false;
642
643 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
644 if (!Elem.Assume)
645 continue;
646
647 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
648 assert(I->getFunction() == Q.CxtI->getFunction() &&
649 "Got assumption for the wrong function!");
650
651 if (Elem.Index != AssumptionCache::ExprResultIdx) {
652 if (!V->getType()->isPointerTy())
653 continue;
655 *I, I->bundle_op_info_begin()[Elem.Index])) {
656 if (RK.WasOn == V &&
657 (RK.AttrKind == Attribute::NonNull ||
658 (RK.AttrKind == Attribute::Dereferenceable &&
660 V->getType()->getPointerAddressSpace()))) &&
662 return true;
663 }
664 continue;
665 }
666
667 // Warning: This loop can end up being somewhat performance sensitive.
668 // We're running this loop for once for each value queried resulting in a
669 // runtime of ~O(#assumes * #values).
670
671 Value *RHS;
672 CmpPredicate Pred;
673 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
674 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
675 continue;
676
677 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
678 return true;
679 }
680
681 return false;
682}
683
685 Value *LHS, Value *RHS, KnownBits &Known,
686 const SimplifyQuery &Q) {
687 if (RHS->getType()->isPointerTy()) {
688 // Handle comparison of pointer to null explicitly, as it will not be
689 // covered by the m_APInt() logic below.
690 if (LHS == V && match(RHS, m_Zero())) {
691 switch (Pred) {
693 Known.setAllZero();
694 break;
697 Known.makeNonNegative();
698 break;
700 Known.makeNegative();
701 break;
702 default:
703 break;
704 }
705 }
706 return;
707 }
708
709 unsigned BitWidth = Known.getBitWidth();
710 auto m_V =
712
713 Value *Y;
714 const APInt *Mask, *C;
715 if (!match(RHS, m_APInt(C)))
716 return;
717
718 uint64_t ShAmt;
719 switch (Pred) {
721 // assume(V = C)
722 if (match(LHS, m_V)) {
723 Known = Known.unionWith(KnownBits::makeConstant(*C));
724 // assume(V & Mask = C)
725 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
726 // For one bits in Mask, we can propagate bits from C to V.
727 Known.One |= *C;
728 if (match(Y, m_APInt(Mask)))
729 Known.Zero |= ~*C & *Mask;
730 // assume(V | Mask = C)
731 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
732 // For zero bits in Mask, we can propagate bits from C to V.
733 Known.Zero |= ~*C;
734 if (match(Y, m_APInt(Mask)))
735 Known.One |= *C & ~*Mask;
736 // assume(V << ShAmt = C)
737 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
738 ShAmt < BitWidth) {
739 // For those bits in C that are known, we can propagate them to known
740 // bits in V shifted to the right by ShAmt.
742 RHSKnown >>= ShAmt;
743 Known = Known.unionWith(RHSKnown);
744 // assume(V >> ShAmt = C)
745 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
746 ShAmt < BitWidth) {
747 // For those bits in RHS that are known, we can propagate them to known
748 // bits in V shifted to the right by C.
750 RHSKnown <<= ShAmt;
751 Known = Known.unionWith(RHSKnown);
752 }
753 break;
754 case ICmpInst::ICMP_NE: {
755 // assume (V & B != 0) where B is a power of 2
756 const APInt *BPow2;
757 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
758 Known.One |= *BPow2;
759 break;
760 }
761 default: {
762 const APInt *Offset = nullptr;
763 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
765 if (Offset)
766 LHSRange = LHSRange.sub(*Offset);
767 Known = Known.unionWith(LHSRange.toKnownBits());
768 }
769 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
770 // X & Y u> C -> X u> C && Y u> C
771 // X nuw- Y u> C -> X u> C
772 if (match(LHS, m_c_And(m_V, m_Value())) ||
773 match(LHS, m_NUWSub(m_V, m_Value())))
774 Known.One.setHighBits(
775 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
776 }
777 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
778 // X | Y u< C -> X u< C && Y u< C
779 // X nuw+ Y u< C -> X u< C && Y u< C
780 if (match(LHS, m_c_Or(m_V, m_Value())) ||
781 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
782 Known.Zero.setHighBits(
783 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
784 }
785 }
786 } break;
787 }
788}
789
790static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
791 KnownBits &Known,
792 const SimplifyQuery &SQ, bool Invert) {
794 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
795 Value *LHS = Cmp->getOperand(0);
796 Value *RHS = Cmp->getOperand(1);
797
798 // Handle icmp pred (trunc V), C
799 if (match(LHS, m_Trunc(m_Specific(V)))) {
800 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
801 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
803 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
804 else
805 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
806 return;
807 }
808
809 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
810}
811
813 KnownBits &Known, const SimplifyQuery &SQ,
814 bool Invert, unsigned Depth) {
815 Value *A, *B;
818 KnownBits Known2(Known.getBitWidth());
819 KnownBits Known3(Known.getBitWidth());
820 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
821 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
822 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
824 Known2 = Known2.unionWith(Known3);
825 else
826 Known2 = Known2.intersectWith(Known3);
827 Known = Known.unionWith(Known2);
828 return;
829 }
830
831 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
832 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
833 return;
834 }
835
836 if (match(Cond, m_Trunc(m_Specific(V)))) {
837 KnownBits DstKnown(1);
838 if (Invert) {
839 DstKnown.setAllZero();
840 } else {
841 DstKnown.setAllOnes();
842 }
844 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
845 return;
846 }
847 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
848 return;
849 }
850
852 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
853}
854
856 const SimplifyQuery &Q, unsigned Depth) {
857 // Handle injected condition.
858 if (Q.CC && Q.CC->AffectedValues.contains(V))
859 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
860
861 if (!Q.CxtI)
862 return;
863
864 if (Q.DC && Q.DT) {
865 // Handle dominating conditions.
866 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
867 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
868 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
869 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
870 /*Invert*/ false, Depth);
871
872 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
873 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
874 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
875 /*Invert*/ true, Depth);
876 }
877
878 if (Known.hasConflict())
879 Known.resetAll();
880 }
881
882 if (!Q.AC)
883 return;
884
885 unsigned BitWidth = Known.getBitWidth();
886
887 // Note that the patterns below need to be kept in sync with the code
888 // in AssumptionCache::updateAffectedValues.
889
890 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
891 if (!Elem.Assume)
892 continue;
893
894 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
895 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
896 "Got assumption for the wrong function!");
897
898 if (Elem.Index != AssumptionCache::ExprResultIdx) {
899 if (!V->getType()->isPointerTy())
900 continue;
902 *I, I->bundle_op_info_begin()[Elem.Index])) {
903 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
904 // be the producer of the pointer in the bundle. At the moment, align
905 // assumptions aren't optimized away.
906 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
907 isPowerOf2_64(RK.ArgValue) &&
908 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
909 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
910 }
911 continue;
912 }
913
914 // Warning: This loop can end up being somewhat performance sensitive.
915 // We're running this loop for once for each value queried resulting in a
916 // runtime of ~O(#assumes * #values).
917
918 Value *Arg = I->getArgOperand(0);
919
920 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
921 assert(BitWidth == 1 && "assume operand is not i1?");
922 (void)BitWidth;
923 Known.setAllOnes();
924 return;
925 }
926 if (match(Arg, m_Not(m_Specific(V))) &&
928 assert(BitWidth == 1 && "assume operand is not i1?");
929 (void)BitWidth;
930 Known.setAllZero();
931 return;
932 }
933 auto *Trunc = dyn_cast<TruncInst>(Arg);
934 if (Trunc && Trunc->getOperand(0) == V &&
936 if (Trunc->hasNoUnsignedWrap()) {
938 return;
939 }
940 Known.One.setBit(0);
941 return;
942 }
943
944 // The remaining tests are all recursive, so bail out if we hit the limit.
946 continue;
947
948 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
949 if (!Cmp)
950 continue;
951
952 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
953 continue;
954
955 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
956 }
957
958 // Conflicting assumption: Undefined behavior will occur on this execution
959 // path.
960 if (Known.hasConflict())
961 Known.resetAll();
962}
963
964/// Compute known bits from a shift operator, including those with a
965/// non-constant shift amount. Known is the output of this function. Known2 is a
966/// pre-allocated temporary with the same bit width as Known and on return
967/// contains the known bit of the shift value source. KF is an
968/// operator-specific function that, given the known-bits and a shift amount,
969/// compute the implied known-bits of the shift operator's result respectively
970/// for that shift amount. The results from calling KF are conservatively
971/// combined for all permitted shift amounts.
973 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
974 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
975 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
976 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
977 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
978 // To limit compile-time impact, only query isKnownNonZero() if we know at
979 // least something about the shift amount.
980 bool ShAmtNonZero =
981 Known.isNonZero() ||
982 (Known.getMaxValue().ult(Known.getBitWidth()) &&
983 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
984 Known = KF(Known2, Known, ShAmtNonZero);
985}
986
987static KnownBits
988getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
989 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
990 const SimplifyQuery &Q, unsigned Depth) {
991 unsigned BitWidth = KnownLHS.getBitWidth();
992 KnownBits KnownOut(BitWidth);
993 bool IsAnd = false;
994 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
995 Value *X = nullptr, *Y = nullptr;
996
997 switch (I->getOpcode()) {
998 case Instruction::And:
999 KnownOut = KnownLHS & KnownRHS;
1000 IsAnd = true;
1001 // and(x, -x) is common idioms that will clear all but lowest set
1002 // bit. If we have a single known bit in x, we can clear all bits
1003 // above it.
1004 // TODO: instcombine often reassociates independent `and` which can hide
1005 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1006 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1007 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1008 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1009 KnownOut = KnownLHS.blsi();
1010 else
1011 KnownOut = KnownRHS.blsi();
1012 }
1013 break;
1014 case Instruction::Or:
1015 KnownOut = KnownLHS | KnownRHS;
1016 break;
1017 case Instruction::Xor:
1018 KnownOut = KnownLHS ^ KnownRHS;
1019 // xor(x, x-1) is common idioms that will clear all but lowest set
1020 // bit. If we have a single known bit in x, we can clear all bits
1021 // above it.
1022 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1023 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1024 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1025 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1026 if (HasKnownOne &&
1028 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1029 KnownOut = XBits.blsmsk();
1030 }
1031 break;
1032 default:
1033 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1034 }
1035
1036 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1037 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1038 // here we handle the more general case of adding any odd number by
1039 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1040 // TODO: This could be generalized to clearing any bit set in y where the
1041 // following bit is known to be unset in y.
1042 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1046 KnownBits KnownY(BitWidth);
1047 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1048 if (KnownY.countMinTrailingOnes() > 0) {
1049 if (IsAnd)
1050 KnownOut.Zero.setBit(0);
1051 else
1052 KnownOut.One.setBit(0);
1053 }
1054 }
1055 return KnownOut;
1056}
1057
1059 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1060 unsigned Depth,
1061 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1062 KnownBitsFunc) {
1063 APInt DemandedEltsLHS, DemandedEltsRHS;
1065 DemandedElts, DemandedEltsLHS,
1066 DemandedEltsRHS);
1067
1068 const auto ComputeForSingleOpFunc =
1069 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1070 return KnownBitsFunc(
1071 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1072 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1073 };
1074
1075 if (DemandedEltsRHS.isZero())
1076 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1077 if (DemandedEltsLHS.isZero())
1078 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1079
1080 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1081 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1082}
1083
1084// Public so this can be used in `SimplifyDemandedUseBits`.
1086 const KnownBits &KnownLHS,
1087 const KnownBits &KnownRHS,
1088 const SimplifyQuery &SQ,
1089 unsigned Depth) {
1090 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1091 APInt DemandedElts =
1092 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1093
1094 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1095 Depth);
1096}
1097
1099 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1100 // Without vscale_range, we only know that vscale is non-zero.
1101 if (!Attr.isValid())
1103
1104 unsigned AttrMin = Attr.getVScaleRangeMin();
1105 // Minimum is larger than vscale width, result is always poison.
1106 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1107 return ConstantRange::getEmpty(BitWidth);
1108
1109 APInt Min(BitWidth, AttrMin);
1110 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1111 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1113
1114 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1115}
1116
1118 Value *Arm, bool Invert,
1119 const SimplifyQuery &Q, unsigned Depth) {
1120 // If we have a constant arm, we are done.
1121 if (Known.isConstant())
1122 return;
1123
1124 // See what condition implies about the bits of the select arm.
1125 KnownBits CondRes(Known.getBitWidth());
1126 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1127 // If we don't get any information from the condition, no reason to
1128 // proceed.
1129 if (CondRes.isUnknown())
1130 return;
1131
1132 // We can have conflict if the condition is dead. I.e if we have
1133 // (x | 64) < 32 ? (x | 64) : y
1134 // we will have conflict at bit 6 from the condition/the `or`.
1135 // In that case just return. Its not particularly important
1136 // what we do, as this select is going to be simplified soon.
1137 CondRes = CondRes.unionWith(Known);
1138 if (CondRes.hasConflict())
1139 return;
1140
1141 // Finally make sure the information we found is valid. This is relatively
1142 // expensive so it's left for the very end.
1143 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1144 return;
1145
1146 // Finally, we know we get information from the condition and its valid,
1147 // so return it.
1148 Known = CondRes;
1149}
1150
1151// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1152// Returns the input and lower/upper bounds.
1153static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1154 const APInt *&CLow, const APInt *&CHigh) {
1156 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1157 "Input should be a Select!");
1158
1159 const Value *LHS = nullptr, *RHS = nullptr;
1161 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1162 return false;
1163
1164 if (!match(RHS, m_APInt(CLow)))
1165 return false;
1166
1167 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1169 if (getInverseMinMaxFlavor(SPF) != SPF2)
1170 return false;
1171
1172 if (!match(RHS2, m_APInt(CHigh)))
1173 return false;
1174
1175 if (SPF == SPF_SMIN)
1176 std::swap(CLow, CHigh);
1177
1178 In = LHS2;
1179 return CLow->sle(*CHigh);
1180}
1181
1183 const APInt *&CLow,
1184 const APInt *&CHigh) {
1185 assert((II->getIntrinsicID() == Intrinsic::smin ||
1186 II->getIntrinsicID() == Intrinsic::smax) &&
1187 "Must be smin/smax");
1188
1189 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1190 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1191 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1192 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1193 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1194 return false;
1195
1196 if (II->getIntrinsicID() == Intrinsic::smin)
1197 std::swap(CLow, CHigh);
1198 return CLow->sle(*CHigh);
1199}
1200
1202 KnownBits &Known) {
1203 const APInt *CLow, *CHigh;
1204 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1205 Known = Known.unionWith(
1206 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1207}
1208
1210 const APInt &DemandedElts,
1211 KnownBits &Known,
1212 const SimplifyQuery &Q,
1213 unsigned Depth) {
1214 unsigned BitWidth = Known.getBitWidth();
1215
1216 KnownBits Known2(BitWidth);
1217 switch (I->getOpcode()) {
1218 default: break;
1219 case Instruction::Load:
1220 if (MDNode *MD =
1221 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1223 break;
1224 case Instruction::And:
1225 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1226 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1227
1228 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1229 break;
1230 case Instruction::Or:
1231 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1232 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1233
1234 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1235 break;
1236 case Instruction::Xor:
1237 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1238 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1239
1240 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1241 break;
1242 case Instruction::Mul: {
1245 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1246 DemandedElts, Known, Known2, Q, Depth);
1247 break;
1248 }
1249 case Instruction::UDiv: {
1250 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1251 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1252 Known =
1253 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1254 break;
1255 }
1256 case Instruction::SDiv: {
1257 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1258 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1259 Known =
1260 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1261 break;
1262 }
1263 case Instruction::Select: {
1264 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1265 KnownBits Res(Known.getBitWidth());
1266 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1267 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1268 return Res;
1269 };
1270 // Only known if known in both the LHS and RHS.
1271 Known =
1272 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1273 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1274 break;
1275 }
1276 case Instruction::FPTrunc:
1277 case Instruction::FPExt:
1278 case Instruction::FPToUI:
1279 case Instruction::FPToSI:
1280 case Instruction::SIToFP:
1281 case Instruction::UIToFP:
1282 break; // Can't work with floating point.
1283 case Instruction::PtrToInt:
1284 case Instruction::IntToPtr:
1285 // Fall through and handle them the same as zext/trunc.
1286 [[fallthrough]];
1287 case Instruction::ZExt:
1288 case Instruction::Trunc: {
1289 Type *SrcTy = I->getOperand(0)->getType();
1290
1291 unsigned SrcBitWidth;
1292 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1293 // which fall through here.
1294 Type *ScalarTy = SrcTy->getScalarType();
1295 SrcBitWidth = ScalarTy->isPointerTy() ?
1296 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1297 Q.DL.getTypeSizeInBits(ScalarTy);
1298
1299 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1300 Known = Known.anyextOrTrunc(SrcBitWidth);
1301 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1302 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1303 Inst && Inst->hasNonNeg() && !Known.isNegative())
1304 Known.makeNonNegative();
1305 Known = Known.zextOrTrunc(BitWidth);
1306 break;
1307 }
1308 case Instruction::BitCast: {
1309 Type *SrcTy = I->getOperand(0)->getType();
1310 if (SrcTy->isIntOrPtrTy() &&
1311 // TODO: For now, not handling conversions like:
1312 // (bitcast i64 %x to <2 x i32>)
1313 !I->getType()->isVectorTy()) {
1314 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1315 break;
1316 }
1317
1318 const Value *V;
1319 // Handle bitcast from floating point to integer.
1320 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1321 V->getType()->isFPOrFPVectorTy()) {
1322 Type *FPType = V->getType()->getScalarType();
1323 KnownFPClass Result =
1324 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1325 FPClassTest FPClasses = Result.KnownFPClasses;
1326
1327 // TODO: Treat it as zero/poison if the use of I is unreachable.
1328 if (FPClasses == fcNone)
1329 break;
1330
1331 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1332 Known.Zero.setAllBits();
1333 Known.One.setAllBits();
1334
1335 if (FPClasses & fcInf)
1337 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1338
1339 if (FPClasses & fcZero)
1341 APInt::getZero(FPType->getScalarSizeInBits())));
1342
1343 Known.Zero.clearSignBit();
1344 Known.One.clearSignBit();
1345 }
1346
1347 if (Result.SignBit) {
1348 if (*Result.SignBit)
1349 Known.makeNegative();
1350 else
1351 Known.makeNonNegative();
1352 }
1353
1354 break;
1355 }
1356
1357 // Handle cast from vector integer type to scalar or vector integer.
1358 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1359 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1360 !I->getType()->isIntOrIntVectorTy() ||
1361 isa<ScalableVectorType>(I->getType()))
1362 break;
1363
1364 unsigned NumElts = DemandedElts.getBitWidth();
1365 bool IsLE = Q.DL.isLittleEndian();
1366 // Look through a cast from narrow vector elements to wider type.
1367 // Examples: v4i32 -> v2i64, v3i8 -> v24
1368 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1369 if (BitWidth % SubBitWidth == 0) {
1370 // Known bits are automatically intersected across demanded elements of a
1371 // vector. So for example, if a bit is computed as known zero, it must be
1372 // zero across all demanded elements of the vector.
1373 //
1374 // For this bitcast, each demanded element of the output is sub-divided
1375 // across a set of smaller vector elements in the source vector. To get
1376 // the known bits for an entire element of the output, compute the known
1377 // bits for each sub-element sequentially. This is done by shifting the
1378 // one-set-bit demanded elements parameter across the sub-elements for
1379 // consecutive calls to computeKnownBits. We are using the demanded
1380 // elements parameter as a mask operator.
1381 //
1382 // The known bits of each sub-element are then inserted into place
1383 // (dependent on endian) to form the full result of known bits.
1384 unsigned SubScale = BitWidth / SubBitWidth;
1385 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1386 for (unsigned i = 0; i != NumElts; ++i) {
1387 if (DemandedElts[i])
1388 SubDemandedElts.setBit(i * SubScale);
1389 }
1390
1391 KnownBits KnownSrc(SubBitWidth);
1392 for (unsigned i = 0; i != SubScale; ++i) {
1393 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1394 Depth + 1);
1395 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1396 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1397 }
1398 }
1399 // Look through a cast from wider vector elements to narrow type.
1400 // Examples: v2i64 -> v4i32
1401 if (SubBitWidth % BitWidth == 0) {
1402 unsigned SubScale = SubBitWidth / BitWidth;
1403 KnownBits KnownSrc(SubBitWidth);
1404 APInt SubDemandedElts =
1405 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1406 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1407 Depth + 1);
1408
1409 Known.Zero.setAllBits();
1410 Known.One.setAllBits();
1411 for (unsigned i = 0; i != NumElts; ++i) {
1412 if (DemandedElts[i]) {
1413 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1414 unsigned Offset = (Shifts % SubScale) * BitWidth;
1415 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1416 if (Known.isUnknown())
1417 break;
1418 }
1419 }
1420 }
1421 break;
1422 }
1423 case Instruction::SExt: {
1424 // Compute the bits in the result that are not present in the input.
1425 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1426
1427 Known = Known.trunc(SrcBitWidth);
1428 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1429 // If the sign bit of the input is known set or clear, then we know the
1430 // top bits of the result.
1431 Known = Known.sext(BitWidth);
1432 break;
1433 }
1434 case Instruction::Shl: {
1437 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1438 bool ShAmtNonZero) {
1439 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1440 };
1441 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1442 KF);
1443 // Trailing zeros of a right-shifted constant never decrease.
1444 const APInt *C;
1445 if (match(I->getOperand(0), m_APInt(C)))
1446 Known.Zero.setLowBits(C->countr_zero());
1447 break;
1448 }
1449 case Instruction::LShr: {
1450 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1451 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1452 bool ShAmtNonZero) {
1453 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1454 };
1455 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1456 KF);
1457 // Leading zeros of a left-shifted constant never decrease.
1458 const APInt *C;
1459 if (match(I->getOperand(0), m_APInt(C)))
1460 Known.Zero.setHighBits(C->countl_zero());
1461 break;
1462 }
1463 case Instruction::AShr: {
1464 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1465 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1466 bool ShAmtNonZero) {
1467 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1468 };
1469 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1470 KF);
1471 break;
1472 }
1473 case Instruction::Sub: {
1476 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1477 DemandedElts, Known, Known2, Q, Depth);
1478 break;
1479 }
1480 case Instruction::Add: {
1483 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1484 DemandedElts, Known, Known2, Q, Depth);
1485 break;
1486 }
1487 case Instruction::SRem:
1488 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1489 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1490 Known = KnownBits::srem(Known, Known2);
1491 break;
1492
1493 case Instruction::URem:
1494 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1495 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1496 Known = KnownBits::urem(Known, Known2);
1497 break;
1498 case Instruction::Alloca:
1500 break;
1501 case Instruction::GetElementPtr: {
1502 // Analyze all of the subscripts of this getelementptr instruction
1503 // to determine if we can prove known low zero bits.
1504 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1505 // Accumulate the constant indices in a separate variable
1506 // to minimize the number of calls to computeForAddSub.
1507 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1508 APInt AccConstIndices(IndexWidth, 0);
1509
1510 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1511 if (IndexWidth == BitWidth) {
1512 // Note that inbounds does *not* guarantee nsw for the addition, as only
1513 // the offset is signed, while the base address is unsigned.
1514 Known = KnownBits::add(Known, IndexBits);
1515 } else {
1516 // If the index width is smaller than the pointer width, only add the
1517 // value to the low bits.
1518 assert(IndexWidth < BitWidth &&
1519 "Index width can't be larger than pointer width");
1520 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1521 }
1522 };
1523
1525 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1526 // TrailZ can only become smaller, short-circuit if we hit zero.
1527 if (Known.isUnknown())
1528 break;
1529
1530 Value *Index = I->getOperand(i);
1531
1532 // Handle case when index is zero.
1533 Constant *CIndex = dyn_cast<Constant>(Index);
1534 if (CIndex && CIndex->isZeroValue())
1535 continue;
1536
1537 if (StructType *STy = GTI.getStructTypeOrNull()) {
1538 // Handle struct member offset arithmetic.
1539
1540 assert(CIndex &&
1541 "Access to structure field must be known at compile time");
1542
1543 if (CIndex->getType()->isVectorTy())
1544 Index = CIndex->getSplatValue();
1545
1546 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1547 const StructLayout *SL = Q.DL.getStructLayout(STy);
1548 uint64_t Offset = SL->getElementOffset(Idx);
1549 AccConstIndices += Offset;
1550 continue;
1551 }
1552
1553 // Handle array index arithmetic.
1554 Type *IndexedTy = GTI.getIndexedType();
1555 if (!IndexedTy->isSized()) {
1556 Known.resetAll();
1557 break;
1558 }
1559
1560 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1561 uint64_t StrideInBytes = Stride.getKnownMinValue();
1562 if (!Stride.isScalable()) {
1563 // Fast path for constant offset.
1564 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1565 AccConstIndices +=
1566 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1567 continue;
1568 }
1569 }
1570
1571 KnownBits IndexBits =
1572 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1573 KnownBits ScalingFactor(IndexWidth);
1574 // Multiply by current sizeof type.
1575 // &A[i] == A + i * sizeof(*A[i]).
1576 if (Stride.isScalable()) {
1577 // For scalable types the only thing we know about sizeof is
1578 // that this is a multiple of the minimum size.
1579 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1580 } else {
1581 ScalingFactor =
1582 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1583 }
1584 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1585 }
1586 if (!Known.isUnknown() && !AccConstIndices.isZero())
1587 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1588 break;
1589 }
1590 case Instruction::PHI: {
1591 const PHINode *P = cast<PHINode>(I);
1592 BinaryOperator *BO = nullptr;
1593 Value *R = nullptr, *L = nullptr;
1594 if (matchSimpleRecurrence(P, BO, R, L)) {
1595 // Handle the case of a simple two-predecessor recurrence PHI.
1596 // There's a lot more that could theoretically be done here, but
1597 // this is sufficient to catch some interesting cases.
1598 unsigned Opcode = BO->getOpcode();
1599
1600 switch (Opcode) {
1601 // If this is a shift recurrence, we know the bits being shifted in. We
1602 // can combine that with information about the start value of the
1603 // recurrence to conclude facts about the result. If this is a udiv
1604 // recurrence, we know that the result can never exceed either the
1605 // numerator or the start value, whichever is greater.
1606 case Instruction::LShr:
1607 case Instruction::AShr:
1608 case Instruction::Shl:
1609 case Instruction::UDiv:
1610 if (BO->getOperand(0) != I)
1611 break;
1612 [[fallthrough]];
1613
1614 // For a urem recurrence, the result can never exceed the start value. The
1615 // phi could either be the numerator or the denominator.
1616 case Instruction::URem: {
1617 // We have matched a recurrence of the form:
1618 // %iv = [R, %entry], [%iv.next, %backedge]
1619 // %iv.next = shift_op %iv, L
1620
1621 // Recurse with the phi context to avoid concern about whether facts
1622 // inferred hold at original context instruction. TODO: It may be
1623 // correct to use the original context. IF warranted, explore and
1624 // add sufficient tests to cover.
1626 RecQ.CxtI = P;
1627 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1628 switch (Opcode) {
1629 case Instruction::Shl:
1630 // A shl recurrence will only increase the tailing zeros
1631 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1632 break;
1633 case Instruction::LShr:
1634 case Instruction::UDiv:
1635 case Instruction::URem:
1636 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1637 // the start value.
1638 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1639 break;
1640 case Instruction::AShr:
1641 // An ashr recurrence will extend the initial sign bit
1642 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1643 Known.One.setHighBits(Known2.countMinLeadingOnes());
1644 break;
1645 }
1646 break;
1647 }
1648
1649 // Check for operations that have the property that if
1650 // both their operands have low zero bits, the result
1651 // will have low zero bits.
1652 case Instruction::Add:
1653 case Instruction::Sub:
1654 case Instruction::And:
1655 case Instruction::Or:
1656 case Instruction::Mul: {
1657 // Change the context instruction to the "edge" that flows into the
1658 // phi. This is important because that is where the value is actually
1659 // "evaluated" even though it is used later somewhere else. (see also
1660 // D69571).
1662
1663 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1664 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1665 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1666
1667 // Ok, we have a PHI of the form L op= R. Check for low
1668 // zero bits.
1669 RecQ.CxtI = RInst;
1670 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1671
1672 // We need to take the minimum number of known bits
1673 KnownBits Known3(BitWidth);
1674 RecQ.CxtI = LInst;
1675 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1676
1677 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1678 Known3.countMinTrailingZeros()));
1679
1680 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1681 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1682 break;
1683
1684 switch (Opcode) {
1685 // If initial value of recurrence is nonnegative, and we are adding
1686 // a nonnegative number with nsw, the result can only be nonnegative
1687 // or poison value regardless of the number of times we execute the
1688 // add in phi recurrence. If initial value is negative and we are
1689 // adding a negative number with nsw, the result can only be
1690 // negative or poison value. Similar arguments apply to sub and mul.
1691 //
1692 // (add non-negative, non-negative) --> non-negative
1693 // (add negative, negative) --> negative
1694 case Instruction::Add: {
1695 if (Known2.isNonNegative() && Known3.isNonNegative())
1696 Known.makeNonNegative();
1697 else if (Known2.isNegative() && Known3.isNegative())
1698 Known.makeNegative();
1699 break;
1700 }
1701
1702 // (sub nsw non-negative, negative) --> non-negative
1703 // (sub nsw negative, non-negative) --> negative
1704 case Instruction::Sub: {
1705 if (BO->getOperand(0) != I)
1706 break;
1707 if (Known2.isNonNegative() && Known3.isNegative())
1708 Known.makeNonNegative();
1709 else if (Known2.isNegative() && Known3.isNonNegative())
1710 Known.makeNegative();
1711 break;
1712 }
1713
1714 // (mul nsw non-negative, non-negative) --> non-negative
1715 case Instruction::Mul:
1716 if (Known2.isNonNegative() && Known3.isNonNegative())
1717 Known.makeNonNegative();
1718 break;
1719
1720 default:
1721 break;
1722 }
1723 break;
1724 }
1725
1726 default:
1727 break;
1728 }
1729 }
1730
1731 // Unreachable blocks may have zero-operand PHI nodes.
1732 if (P->getNumIncomingValues() == 0)
1733 break;
1734
1735 // Otherwise take the unions of the known bit sets of the operands,
1736 // taking conservative care to avoid excessive recursion.
1737 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1738 // Skip if every incoming value references to ourself.
1739 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1740 break;
1741
1742 Known.Zero.setAllBits();
1743 Known.One.setAllBits();
1744 for (const Use &U : P->operands()) {
1745 Value *IncValue;
1746 const PHINode *CxtPhi;
1747 Instruction *CxtI;
1748 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1749 // Skip direct self references.
1750 if (IncValue == P)
1751 continue;
1752
1753 // Change the context instruction to the "edge" that flows into the
1754 // phi. This is important because that is where the value is actually
1755 // "evaluated" even though it is used later somewhere else. (see also
1756 // D69571).
1758
1759 Known2 = KnownBits(BitWidth);
1760
1761 // Recurse, but cap the recursion to one level, because we don't
1762 // want to waste time spinning around in loops.
1763 // TODO: See if we can base recursion limiter on number of incoming phi
1764 // edges so we don't overly clamp analysis.
1765 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1767
1768 // See if we can further use a conditional branch into the phi
1769 // to help us determine the range of the value.
1770 if (!Known2.isConstant()) {
1771 CmpPredicate Pred;
1772 const APInt *RHSC;
1773 BasicBlock *TrueSucc, *FalseSucc;
1774 // TODO: Use RHS Value and compute range from its known bits.
1775 if (match(RecQ.CxtI,
1776 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1777 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1778 // Check for cases of duplicate successors.
1779 if ((TrueSucc == CxtPhi->getParent()) !=
1780 (FalseSucc == CxtPhi->getParent())) {
1781 // If we're using the false successor, invert the predicate.
1782 if (FalseSucc == CxtPhi->getParent())
1783 Pred = CmpInst::getInversePredicate(Pred);
1784 // Get the knownbits implied by the incoming phi condition.
1785 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1786 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1787 // We can have conflicts here if we are analyzing deadcode (its
1788 // impossible for us reach this BB based the icmp).
1789 if (KnownUnion.hasConflict()) {
1790 // No reason to continue analyzing in a known dead region, so
1791 // just resetAll and break. This will cause us to also exit the
1792 // outer loop.
1793 Known.resetAll();
1794 break;
1795 }
1796 Known2 = KnownUnion;
1797 }
1798 }
1799 }
1800
1801 Known = Known.intersectWith(Known2);
1802 // If all bits have been ruled out, there's no need to check
1803 // more operands.
1804 if (Known.isUnknown())
1805 break;
1806 }
1807 }
1808 break;
1809 }
1810 case Instruction::Call:
1811 case Instruction::Invoke: {
1812 // If range metadata is attached to this call, set known bits from that,
1813 // and then intersect with known bits based on other properties of the
1814 // function.
1815 if (MDNode *MD =
1816 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1818
1819 const auto *CB = cast<CallBase>(I);
1820
1821 if (std::optional<ConstantRange> Range = CB->getRange())
1822 Known = Known.unionWith(Range->toKnownBits());
1823
1824 if (const Value *RV = CB->getReturnedArgOperand()) {
1825 if (RV->getType() == I->getType()) {
1826 computeKnownBits(RV, Known2, Q, Depth + 1);
1827 Known = Known.unionWith(Known2);
1828 // If the function doesn't return properly for all input values
1829 // (e.g. unreachable exits) then there might be conflicts between the
1830 // argument value and the range metadata. Simply discard the known bits
1831 // in case of conflicts.
1832 if (Known.hasConflict())
1833 Known.resetAll();
1834 }
1835 }
1836 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1837 switch (II->getIntrinsicID()) {
1838 default:
1839 break;
1840 case Intrinsic::abs: {
1841 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1842 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
1843 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
1844 break;
1845 }
1846 case Intrinsic::bitreverse:
1847 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1848 Known = Known.unionWith(Known2.reverseBits());
1849 break;
1850 case Intrinsic::bswap:
1851 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1852 Known = Known.unionWith(Known2.byteSwap());
1853 break;
1854 case Intrinsic::ctlz: {
1855 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1856 // If we have a known 1, its position is our upper bound.
1857 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
1858 // If this call is poison for 0 input, the result will be less than 2^n.
1859 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1860 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1861 unsigned LowBits = llvm::bit_width(PossibleLZ);
1862 Known.Zero.setBitsFrom(LowBits);
1863 break;
1864 }
1865 case Intrinsic::cttz: {
1866 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1867 // If we have a known 1, its position is our upper bound.
1868 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
1869 // If this call is poison for 0 input, the result will be less than 2^n.
1870 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1871 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1872 unsigned LowBits = llvm::bit_width(PossibleTZ);
1873 Known.Zero.setBitsFrom(LowBits);
1874 break;
1875 }
1876 case Intrinsic::ctpop: {
1877 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1878 // We can bound the space the count needs. Also, bits known to be zero
1879 // can't contribute to the population.
1880 unsigned BitsPossiblySet = Known2.countMaxPopulation();
1881 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
1882 Known.Zero.setBitsFrom(LowBits);
1883 // TODO: we could bound KnownOne using the lower bound on the number
1884 // of bits which might be set provided by popcnt KnownOne2.
1885 break;
1886 }
1887 case Intrinsic::fshr:
1888 case Intrinsic::fshl: {
1889 const APInt *SA;
1890 if (!match(I->getOperand(2), m_APInt(SA)))
1891 break;
1892
1893 // Normalize to funnel shift left.
1894 uint64_t ShiftAmt = SA->urem(BitWidth);
1895 if (II->getIntrinsicID() == Intrinsic::fshr)
1896 ShiftAmt = BitWidth - ShiftAmt;
1897
1898 KnownBits Known3(BitWidth);
1899 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1900 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
1901
1902 Known2 <<= ShiftAmt;
1903 Known3 >>= BitWidth - ShiftAmt;
1904 Known = Known2.unionWith(Known3);
1905 break;
1906 }
1907 case Intrinsic::uadd_sat:
1908 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1909 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1910 Known = KnownBits::uadd_sat(Known, Known2);
1911 break;
1912 case Intrinsic::usub_sat:
1913 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1914 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1915 Known = KnownBits::usub_sat(Known, Known2);
1916 break;
1917 case Intrinsic::sadd_sat:
1918 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1919 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1920 Known = KnownBits::sadd_sat(Known, Known2);
1921 break;
1922 case Intrinsic::ssub_sat:
1923 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1924 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1925 Known = KnownBits::ssub_sat(Known, Known2);
1926 break;
1927 // Vec reverse preserves bits from input vec.
1928 case Intrinsic::vector_reverse:
1929 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
1930 Depth + 1);
1931 break;
1932 // for min/max/and/or reduce, any bit common to each element in the
1933 // input vec is set in the output.
1934 case Intrinsic::vector_reduce_and:
1935 case Intrinsic::vector_reduce_or:
1936 case Intrinsic::vector_reduce_umax:
1937 case Intrinsic::vector_reduce_umin:
1938 case Intrinsic::vector_reduce_smax:
1939 case Intrinsic::vector_reduce_smin:
1940 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1941 break;
1942 case Intrinsic::vector_reduce_xor: {
1943 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1944 // The zeros common to all vecs are zero in the output.
1945 // If the number of elements is odd, then the common ones remain. If the
1946 // number of elements is even, then the common ones becomes zeros.
1947 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
1948 // Even, so the ones become zeros.
1949 bool EvenCnt = VecTy->getElementCount().isKnownEven();
1950 if (EvenCnt)
1951 Known.Zero |= Known.One;
1952 // Maybe even element count so need to clear ones.
1953 if (VecTy->isScalableTy() || EvenCnt)
1954 Known.One.clearAllBits();
1955 break;
1956 }
1957 case Intrinsic::umin:
1958 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1959 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1960 Known = KnownBits::umin(Known, Known2);
1961 break;
1962 case Intrinsic::umax:
1963 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1964 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1965 Known = KnownBits::umax(Known, Known2);
1966 break;
1967 case Intrinsic::smin:
1968 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1969 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1970 Known = KnownBits::smin(Known, Known2);
1972 break;
1973 case Intrinsic::smax:
1974 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1975 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1976 Known = KnownBits::smax(Known, Known2);
1978 break;
1979 case Intrinsic::ptrmask: {
1980 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1981
1982 const Value *Mask = I->getOperand(1);
1983 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
1984 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
1985 // TODO: 1-extend would be more precise.
1986 Known &= Known2.anyextOrTrunc(BitWidth);
1987 break;
1988 }
1989 case Intrinsic::x86_sse2_pmulh_w:
1990 case Intrinsic::x86_avx2_pmulh_w:
1991 case Intrinsic::x86_avx512_pmulh_w_512:
1992 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1993 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1994 Known = KnownBits::mulhs(Known, Known2);
1995 break;
1996 case Intrinsic::x86_sse2_pmulhu_w:
1997 case Intrinsic::x86_avx2_pmulhu_w:
1998 case Intrinsic::x86_avx512_pmulhu_w_512:
1999 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2000 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2001 Known = KnownBits::mulhu(Known, Known2);
2002 break;
2003 case Intrinsic::x86_sse42_crc32_64_64:
2004 Known.Zero.setBitsFrom(32);
2005 break;
2006 case Intrinsic::x86_ssse3_phadd_d_128:
2007 case Intrinsic::x86_ssse3_phadd_w_128:
2008 case Intrinsic::x86_avx2_phadd_d:
2009 case Intrinsic::x86_avx2_phadd_w: {
2011 I, DemandedElts, Q, Depth,
2012 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2013 return KnownBits::add(KnownLHS, KnownRHS);
2014 });
2015 break;
2016 }
2017 case Intrinsic::x86_ssse3_phadd_sw_128:
2018 case Intrinsic::x86_avx2_phadd_sw: {
2020 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2021 break;
2022 }
2023 case Intrinsic::x86_ssse3_phsub_d_128:
2024 case Intrinsic::x86_ssse3_phsub_w_128:
2025 case Intrinsic::x86_avx2_phsub_d:
2026 case Intrinsic::x86_avx2_phsub_w: {
2028 I, DemandedElts, Q, Depth,
2029 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2030 return KnownBits::sub(KnownLHS, KnownRHS);
2031 });
2032 break;
2033 }
2034 case Intrinsic::x86_ssse3_phsub_sw_128:
2035 case Intrinsic::x86_avx2_phsub_sw: {
2037 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2038 break;
2039 }
2040 case Intrinsic::riscv_vsetvli:
2041 case Intrinsic::riscv_vsetvlimax: {
2042 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2043 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2045 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2046 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2047 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2048 uint64_t MaxVLEN =
2049 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2050 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2051
2052 // Result of vsetvli must be not larger than AVL.
2053 if (HasAVL)
2054 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2055 MaxVL = std::min(MaxVL, CI->getZExtValue());
2056
2057 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2058 if (BitWidth > KnownZeroFirstBit)
2059 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2060 break;
2061 }
2062 case Intrinsic::vscale: {
2063 if (!II->getParent() || !II->getFunction())
2064 break;
2065
2066 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2067 break;
2068 }
2069 }
2070 }
2071 break;
2072 }
2073 case Instruction::ShuffleVector: {
2074 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2075 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2076 if (!Shuf) {
2077 Known.resetAll();
2078 return;
2079 }
2080 // For undef elements, we don't know anything about the common state of
2081 // the shuffle result.
2082 APInt DemandedLHS, DemandedRHS;
2083 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2084 Known.resetAll();
2085 return;
2086 }
2087 Known.One.setAllBits();
2088 Known.Zero.setAllBits();
2089 if (!!DemandedLHS) {
2090 const Value *LHS = Shuf->getOperand(0);
2091 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2092 // If we don't know any bits, early out.
2093 if (Known.isUnknown())
2094 break;
2095 }
2096 if (!!DemandedRHS) {
2097 const Value *RHS = Shuf->getOperand(1);
2098 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2099 Known = Known.intersectWith(Known2);
2100 }
2101 break;
2102 }
2103 case Instruction::InsertElement: {
2104 if (isa<ScalableVectorType>(I->getType())) {
2105 Known.resetAll();
2106 return;
2107 }
2108 const Value *Vec = I->getOperand(0);
2109 const Value *Elt = I->getOperand(1);
2110 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2111 unsigned NumElts = DemandedElts.getBitWidth();
2112 APInt DemandedVecElts = DemandedElts;
2113 bool NeedsElt = true;
2114 // If we know the index we are inserting too, clear it from Vec check.
2115 if (CIdx && CIdx->getValue().ult(NumElts)) {
2116 DemandedVecElts.clearBit(CIdx->getZExtValue());
2117 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2118 }
2119
2120 Known.One.setAllBits();
2121 Known.Zero.setAllBits();
2122 if (NeedsElt) {
2123 computeKnownBits(Elt, Known, Q, Depth + 1);
2124 // If we don't know any bits, early out.
2125 if (Known.isUnknown())
2126 break;
2127 }
2128
2129 if (!DemandedVecElts.isZero()) {
2130 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2131 Known = Known.intersectWith(Known2);
2132 }
2133 break;
2134 }
2135 case Instruction::ExtractElement: {
2136 // Look through extract element. If the index is non-constant or
2137 // out-of-range demand all elements, otherwise just the extracted element.
2138 const Value *Vec = I->getOperand(0);
2139 const Value *Idx = I->getOperand(1);
2140 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2141 if (isa<ScalableVectorType>(Vec->getType())) {
2142 // FIXME: there's probably *something* we can do with scalable vectors
2143 Known.resetAll();
2144 break;
2145 }
2146 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2147 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2148 if (CIdx && CIdx->getValue().ult(NumElts))
2149 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2150 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2151 break;
2152 }
2153 case Instruction::ExtractValue:
2154 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2156 if (EVI->getNumIndices() != 1) break;
2157 if (EVI->getIndices()[0] == 0) {
2158 switch (II->getIntrinsicID()) {
2159 default: break;
2160 case Intrinsic::uadd_with_overflow:
2161 case Intrinsic::sadd_with_overflow:
2163 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2164 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2165 break;
2166 case Intrinsic::usub_with_overflow:
2167 case Intrinsic::ssub_with_overflow:
2169 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2170 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2171 break;
2172 case Intrinsic::umul_with_overflow:
2173 case Intrinsic::smul_with_overflow:
2174 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2175 false, DemandedElts, Known, Known2, Q, Depth);
2176 break;
2177 }
2178 }
2179 }
2180 break;
2181 case Instruction::Freeze:
2182 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2183 Depth + 1))
2184 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2185 break;
2186 }
2187}
2188
2189/// Determine which bits of V are known to be either zero or one and return
2190/// them.
2191KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2192 const SimplifyQuery &Q, unsigned Depth) {
2193 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2194 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2195 return Known;
2196}
2197
2198/// Determine which bits of V are known to be either zero or one and return
2199/// them.
2201 unsigned Depth) {
2202 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2203 computeKnownBits(V, Known, Q, Depth);
2204 return Known;
2205}
2206
2207/// Determine which bits of V are known to be either zero or one and return
2208/// them in the Known bit set.
2209///
2210/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2211/// we cannot optimize based on the assumption that it is zero without changing
2212/// it to be an explicit zero. If we don't change it to zero, other code could
2213/// optimized based on the contradictory assumption that it is non-zero.
2214/// Because instcombine aggressively folds operations with undef args anyway,
2215/// this won't lose us code quality.
2216///
2217/// This function is defined on values with integer type, values with pointer
2218/// type, and vectors of integers. In the case
2219/// where V is a vector, known zero, and known one values are the
2220/// same width as the vector element, and the bit is set only if it is true
2221/// for all of the demanded elements in the vector specified by DemandedElts.
2222void computeKnownBits(const Value *V, const APInt &DemandedElts,
2223 KnownBits &Known, const SimplifyQuery &Q,
2224 unsigned Depth) {
2225 if (!DemandedElts) {
2226 // No demanded elts, better to assume we don't know anything.
2227 Known.resetAll();
2228 return;
2229 }
2230
2231 assert(V && "No Value?");
2232 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2233
2234#ifndef NDEBUG
2235 Type *Ty = V->getType();
2236 unsigned BitWidth = Known.getBitWidth();
2237
2238 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2239 "Not integer or pointer type!");
2240
2241 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2242 assert(
2243 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2244 "DemandedElt width should equal the fixed vector number of elements");
2245 } else {
2246 assert(DemandedElts == APInt(1, 1) &&
2247 "DemandedElt width should be 1 for scalars or scalable vectors");
2248 }
2249
2250 Type *ScalarTy = Ty->getScalarType();
2251 if (ScalarTy->isPointerTy()) {
2252 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2253 "V and Known should have same BitWidth");
2254 } else {
2255 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2256 "V and Known should have same BitWidth");
2257 }
2258#endif
2259
2260 const APInt *C;
2261 if (match(V, m_APInt(C))) {
2262 // We know all of the bits for a scalar constant or a splat vector constant!
2263 Known = KnownBits::makeConstant(*C);
2264 return;
2265 }
2266 // Null and aggregate-zero are all-zeros.
2268 Known.setAllZero();
2269 return;
2270 }
2271 // Handle a constant vector by taking the intersection of the known bits of
2272 // each element.
2274 assert(!isa<ScalableVectorType>(V->getType()));
2275 // We know that CDV must be a vector of integers. Take the intersection of
2276 // each element.
2277 Known.Zero.setAllBits(); Known.One.setAllBits();
2278 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2279 if (!DemandedElts[i])
2280 continue;
2281 APInt Elt = CDV->getElementAsAPInt(i);
2282 Known.Zero &= ~Elt;
2283 Known.One &= Elt;
2284 }
2285 if (Known.hasConflict())
2286 Known.resetAll();
2287 return;
2288 }
2289
2290 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2291 assert(!isa<ScalableVectorType>(V->getType()));
2292 // We know that CV must be a vector of integers. Take the intersection of
2293 // each element.
2294 Known.Zero.setAllBits(); Known.One.setAllBits();
2295 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2296 if (!DemandedElts[i])
2297 continue;
2298 Constant *Element = CV->getAggregateElement(i);
2299 if (isa<PoisonValue>(Element))
2300 continue;
2301 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2302 if (!ElementCI) {
2303 Known.resetAll();
2304 return;
2305 }
2306 const APInt &Elt = ElementCI->getValue();
2307 Known.Zero &= ~Elt;
2308 Known.One &= Elt;
2309 }
2310 if (Known.hasConflict())
2311 Known.resetAll();
2312 return;
2313 }
2314
2315 // Start out not knowing anything.
2316 Known.resetAll();
2317
2318 // We can't imply anything about undefs.
2319 if (isa<UndefValue>(V))
2320 return;
2321
2322 // There's no point in looking through other users of ConstantData for
2323 // assumptions. Confirm that we've handled them all.
2324 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2325
2326 if (const auto *A = dyn_cast<Argument>(V))
2327 if (std::optional<ConstantRange> Range = A->getRange())
2328 Known = Range->toKnownBits();
2329
2330 // All recursive calls that increase depth must come after this.
2332 return;
2333
2334 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2335 // the bits of its aliasee.
2336 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2337 if (!GA->isInterposable())
2338 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2339 return;
2340 }
2341
2342 if (const Operator *I = dyn_cast<Operator>(V))
2343 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2344 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2345 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2346 Known = CR->toKnownBits();
2347 }
2348
2349 // Aligned pointers have trailing zeros - refine Known.Zero set
2350 if (isa<PointerType>(V->getType())) {
2351 Align Alignment = V->getPointerAlignment(Q.DL);
2352 Known.Zero.setLowBits(Log2(Alignment));
2353 }
2354
2355 // computeKnownBitsFromContext strictly refines Known.
2356 // Therefore, we run them after computeKnownBitsFromOperator.
2357
2358 // Check whether we can determine known bits from context such as assumes.
2359 computeKnownBitsFromContext(V, Known, Q, Depth);
2360}
2361
2362/// Try to detect a recurrence that the value of the induction variable is
2363/// always a power of two (or zero).
2364static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2365 SimplifyQuery &Q, unsigned Depth) {
2366 BinaryOperator *BO = nullptr;
2367 Value *Start = nullptr, *Step = nullptr;
2368 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2369 return false;
2370
2371 // Initial value must be a power of two.
2372 for (const Use &U : PN->operands()) {
2373 if (U.get() == Start) {
2374 // Initial value comes from a different BB, need to adjust context
2375 // instruction for analysis.
2376 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2377 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2378 return false;
2379 }
2380 }
2381
2382 // Except for Mul, the induction variable must be on the left side of the
2383 // increment expression, otherwise its value can be arbitrary.
2384 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2385 return false;
2386
2387 Q.CxtI = BO->getParent()->getTerminator();
2388 switch (BO->getOpcode()) {
2389 case Instruction::Mul:
2390 // Power of two is closed under multiplication.
2391 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2392 Q.IIQ.hasNoSignedWrap(BO)) &&
2393 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2394 case Instruction::SDiv:
2395 // Start value must not be signmask for signed division, so simply being a
2396 // power of two is not sufficient, and it has to be a constant.
2397 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2398 return false;
2399 [[fallthrough]];
2400 case Instruction::UDiv:
2401 // Divisor must be a power of two.
2402 // If OrZero is false, cannot guarantee induction variable is non-zero after
2403 // division, same for Shr, unless it is exact division.
2404 return (OrZero || Q.IIQ.isExact(BO)) &&
2405 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2406 case Instruction::Shl:
2407 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2408 case Instruction::AShr:
2409 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2410 return false;
2411 [[fallthrough]];
2412 case Instruction::LShr:
2413 return OrZero || Q.IIQ.isExact(BO);
2414 default:
2415 return false;
2416 }
2417}
2418
2419/// Return true if we can infer that \p V is known to be a power of 2 from
2420/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2421static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2422 const Value *Cond,
2423 bool CondIsTrue) {
2424 CmpPredicate Pred;
2425 const APInt *RHSC;
2427 m_APInt(RHSC))))
2428 return false;
2429 if (!CondIsTrue)
2430 Pred = ICmpInst::getInversePredicate(Pred);
2431 // ctpop(V) u< 2
2432 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2433 return true;
2434 // ctpop(V) == 1
2435 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2436}
2437
2438/// Return true if the given value is known to have exactly one
2439/// bit set when defined. For vectors return true if every element is known to
2440/// be a power of two when defined. Supports values with integer or pointer
2441/// types and vectors of integers.
2442bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2443 const SimplifyQuery &Q, unsigned Depth) {
2444 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2445
2446 if (isa<Constant>(V))
2447 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2448
2449 // i1 is by definition a power of 2 or zero.
2450 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2451 return true;
2452
2453 // Try to infer from assumptions.
2454 if (Q.AC && Q.CxtI) {
2455 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2456 if (!AssumeVH)
2457 continue;
2458 CallInst *I = cast<CallInst>(AssumeVH);
2459 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2460 /*CondIsTrue=*/true) &&
2462 return true;
2463 }
2464 }
2465
2466 // Handle dominating conditions.
2467 if (Q.DC && Q.CxtI && Q.DT) {
2468 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2469 Value *Cond = BI->getCondition();
2470
2471 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2473 /*CondIsTrue=*/true) &&
2474 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2475 return true;
2476
2477 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2479 /*CondIsTrue=*/false) &&
2480 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2481 return true;
2482 }
2483 }
2484
2485 auto *I = dyn_cast<Instruction>(V);
2486 if (!I)
2487 return false;
2488
2489 if (Q.CxtI && match(V, m_VScale())) {
2490 const Function *F = Q.CxtI->getFunction();
2491 // The vscale_range indicates vscale is a power-of-two.
2492 return F->hasFnAttribute(Attribute::VScaleRange);
2493 }
2494
2495 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2496 // it is shifted off the end then the result is undefined.
2497 if (match(I, m_Shl(m_One(), m_Value())))
2498 return true;
2499
2500 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2501 // the bottom. If it is shifted off the bottom then the result is undefined.
2502 if (match(I, m_LShr(m_SignMask(), m_Value())))
2503 return true;
2504
2505 // The remaining tests are all recursive, so bail out if we hit the limit.
2507 return false;
2508
2509 switch (I->getOpcode()) {
2510 case Instruction::ZExt:
2511 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2512 case Instruction::Trunc:
2513 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2514 case Instruction::Shl:
2515 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2516 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2517 return false;
2518 case Instruction::LShr:
2519 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2520 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2521 return false;
2522 case Instruction::UDiv:
2524 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2525 return false;
2526 case Instruction::Mul:
2527 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2528 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2529 (OrZero || isKnownNonZero(I, Q, Depth));
2530 case Instruction::And:
2531 // A power of two and'd with anything is a power of two or zero.
2532 if (OrZero &&
2533 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2534 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2535 return true;
2536 // X & (-X) is always a power of two or zero.
2537 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2538 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2539 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2540 return false;
2541 case Instruction::Add: {
2542 // Adding a power-of-two or zero to the same power-of-two or zero yields
2543 // either the original power-of-two, a larger power-of-two or zero.
2545 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2546 Q.IIQ.hasNoSignedWrap(VOBO)) {
2547 if (match(I->getOperand(0),
2548 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2549 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2550 return true;
2551 if (match(I->getOperand(1),
2552 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2553 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2554 return true;
2555
2556 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2557 KnownBits LHSBits(BitWidth);
2558 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2559
2560 KnownBits RHSBits(BitWidth);
2561 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2562 // If i8 V is a power of two or zero:
2563 // ZeroBits: 1 1 1 0 1 1 1 1
2564 // ~ZeroBits: 0 0 0 1 0 0 0 0
2565 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2566 // If OrZero isn't set, we cannot give back a zero result.
2567 // Make sure either the LHS or RHS has a bit set.
2568 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2569 return true;
2570 }
2571
2572 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2573 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2574 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2575 return true;
2576 return false;
2577 }
2578 case Instruction::Select:
2579 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2580 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2581 case Instruction::PHI: {
2582 // A PHI node is power of two if all incoming values are power of two, or if
2583 // it is an induction variable where in each step its value is a power of
2584 // two.
2585 auto *PN = cast<PHINode>(I);
2587
2588 // Check if it is an induction variable and always power of two.
2589 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2590 return true;
2591
2592 // Recursively check all incoming values. Limit recursion to 2 levels, so
2593 // that search complexity is limited to number of operands^2.
2594 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2595 return llvm::all_of(PN->operands(), [&](const Use &U) {
2596 // Value is power of 2 if it is coming from PHI node itself by induction.
2597 if (U.get() == PN)
2598 return true;
2599
2600 // Change the context instruction to the incoming block where it is
2601 // evaluated.
2602 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2603 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2604 });
2605 }
2606 case Instruction::Invoke:
2607 case Instruction::Call: {
2608 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2609 switch (II->getIntrinsicID()) {
2610 case Intrinsic::umax:
2611 case Intrinsic::smax:
2612 case Intrinsic::umin:
2613 case Intrinsic::smin:
2614 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2615 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2616 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2617 // thus dont change pow2/non-pow2 status.
2618 case Intrinsic::bitreverse:
2619 case Intrinsic::bswap:
2620 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2621 case Intrinsic::fshr:
2622 case Intrinsic::fshl:
2623 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2624 if (II->getArgOperand(0) == II->getArgOperand(1))
2625 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2626 break;
2627 default:
2628 break;
2629 }
2630 }
2631 return false;
2632 }
2633 default:
2634 return false;
2635 }
2636}
2637
2638/// Test whether a GEP's result is known to be non-null.
2639///
2640/// Uses properties inherent in a GEP to try to determine whether it is known
2641/// to be non-null.
2642///
2643/// Currently this routine does not support vector GEPs.
2644static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2645 unsigned Depth) {
2646 const Function *F = nullptr;
2647 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2648 F = I->getFunction();
2649
2650 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2651 // may be null iff the base pointer is null and the offset is zero.
2652 if (!GEP->hasNoUnsignedWrap() &&
2653 !(GEP->isInBounds() &&
2654 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2655 return false;
2656
2657 // FIXME: Support vector-GEPs.
2658 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2659
2660 // If the base pointer is non-null, we cannot walk to a null address with an
2661 // inbounds GEP in address space zero.
2662 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2663 return true;
2664
2665 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2666 // If so, then the GEP cannot produce a null pointer, as doing so would
2667 // inherently violate the inbounds contract within address space zero.
2669 GTI != GTE; ++GTI) {
2670 // Struct types are easy -- they must always be indexed by a constant.
2671 if (StructType *STy = GTI.getStructTypeOrNull()) {
2672 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2673 unsigned ElementIdx = OpC->getZExtValue();
2674 const StructLayout *SL = Q.DL.getStructLayout(STy);
2675 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2676 if (ElementOffset > 0)
2677 return true;
2678 continue;
2679 }
2680
2681 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2682 if (GTI.getSequentialElementStride(Q.DL).isZero())
2683 continue;
2684
2685 // Fast path the constant operand case both for efficiency and so we don't
2686 // increment Depth when just zipping down an all-constant GEP.
2687 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2688 if (!OpC->isZero())
2689 return true;
2690 continue;
2691 }
2692
2693 // We post-increment Depth here because while isKnownNonZero increments it
2694 // as well, when we pop back up that increment won't persist. We don't want
2695 // to recurse 10k times just because we have 10k GEP operands. We don't
2696 // bail completely out because we want to handle constant GEPs regardless
2697 // of depth.
2699 continue;
2700
2701 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2702 return true;
2703 }
2704
2705 return false;
2706}
2707
2709 const Instruction *CtxI,
2710 const DominatorTree *DT) {
2711 assert(!isa<Constant>(V) && "Called for constant?");
2712
2713 if (!CtxI || !DT)
2714 return false;
2715
2716 unsigned NumUsesExplored = 0;
2717 for (auto &U : V->uses()) {
2718 // Avoid massive lists
2719 if (NumUsesExplored >= DomConditionsMaxUses)
2720 break;
2721 NumUsesExplored++;
2722
2723 const Instruction *UI = cast<Instruction>(U.getUser());
2724 // If the value is used as an argument to a call or invoke, then argument
2725 // attributes may provide an answer about null-ness.
2726 if (V->getType()->isPointerTy()) {
2727 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2728 if (CB->isArgOperand(&U) &&
2729 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2730 /*AllowUndefOrPoison=*/false) &&
2731 DT->dominates(CB, CtxI))
2732 return true;
2733 }
2734 }
2735
2736 // If the value is used as a load/store, then the pointer must be non null.
2737 if (V == getLoadStorePointerOperand(UI)) {
2740 DT->dominates(UI, CtxI))
2741 return true;
2742 }
2743
2744 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2745 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2746 isValidAssumeForContext(UI, CtxI, DT))
2747 return true;
2748
2749 // Consider only compare instructions uniquely controlling a branch
2750 Value *RHS;
2751 CmpPredicate Pred;
2752 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2753 continue;
2754
2755 bool NonNullIfTrue;
2756 if (cmpExcludesZero(Pred, RHS))
2757 NonNullIfTrue = true;
2759 NonNullIfTrue = false;
2760 else
2761 continue;
2762
2765 for (const auto *CmpU : UI->users()) {
2766 assert(WorkList.empty() && "Should be!");
2767 if (Visited.insert(CmpU).second)
2768 WorkList.push_back(CmpU);
2769
2770 while (!WorkList.empty()) {
2771 auto *Curr = WorkList.pop_back_val();
2772
2773 // If a user is an AND, add all its users to the work list. We only
2774 // propagate "pred != null" condition through AND because it is only
2775 // correct to assume that all conditions of AND are met in true branch.
2776 // TODO: Support similar logic of OR and EQ predicate?
2777 if (NonNullIfTrue)
2778 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2779 for (const auto *CurrU : Curr->users())
2780 if (Visited.insert(CurrU).second)
2781 WorkList.push_back(CurrU);
2782 continue;
2783 }
2784
2785 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2786 assert(BI->isConditional() && "uses a comparison!");
2787
2788 BasicBlock *NonNullSuccessor =
2789 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2790 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2791 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2792 return true;
2793 } else if (NonNullIfTrue && isGuard(Curr) &&
2794 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2795 return true;
2796 }
2797 }
2798 }
2799 }
2800
2801 return false;
2802}
2803
2804/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2805/// ensure that the value it's attached to is never Value? 'RangeType' is
2806/// is the type of the value described by the range.
2807static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2808 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2809 assert(NumRanges >= 1);
2810 for (unsigned i = 0; i < NumRanges; ++i) {
2812 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2814 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2815 ConstantRange Range(Lower->getValue(), Upper->getValue());
2816 if (Range.contains(Value))
2817 return false;
2818 }
2819 return true;
2820}
2821
2822/// Try to detect a recurrence that monotonically increases/decreases from a
2823/// non-zero starting value. These are common as induction variables.
2824static bool isNonZeroRecurrence(const PHINode *PN) {
2825 BinaryOperator *BO = nullptr;
2826 Value *Start = nullptr, *Step = nullptr;
2827 const APInt *StartC, *StepC;
2828 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2829 !match(Start, m_APInt(StartC)) || StartC->isZero())
2830 return false;
2831
2832 switch (BO->getOpcode()) {
2833 case Instruction::Add:
2834 // Starting from non-zero and stepping away from zero can never wrap back
2835 // to zero.
2836 return BO->hasNoUnsignedWrap() ||
2837 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
2838 StartC->isNegative() == StepC->isNegative());
2839 case Instruction::Mul:
2840 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2841 match(Step, m_APInt(StepC)) && !StepC->isZero();
2842 case Instruction::Shl:
2843 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
2844 case Instruction::AShr:
2845 case Instruction::LShr:
2846 return BO->isExact();
2847 default:
2848 return false;
2849 }
2850}
2851
2852static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
2854 m_Specific(Op1), m_Zero()))) ||
2856 m_Specific(Op0), m_Zero())));
2857}
2858
2859static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
2860 unsigned BitWidth, Value *X, Value *Y, bool NSW,
2861 bool NUW, unsigned Depth) {
2862 // (X + (X != 0)) is non zero
2863 if (matchOpWithOpEqZero(X, Y))
2864 return true;
2865
2866 if (NUW)
2867 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2868 isKnownNonZero(X, DemandedElts, Q, Depth);
2869
2870 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
2871 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
2872
2873 // If X and Y are both non-negative (as signed values) then their sum is not
2874 // zero unless both X and Y are zero.
2875 if (XKnown.isNonNegative() && YKnown.isNonNegative())
2876 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2877 isKnownNonZero(X, DemandedElts, Q, Depth))
2878 return true;
2879
2880 // If X and Y are both negative (as signed values) then their sum is not
2881 // zero unless both X and Y equal INT_MIN.
2882 if (XKnown.isNegative() && YKnown.isNegative()) {
2884 // The sign bit of X is set. If some other bit is set then X is not equal
2885 // to INT_MIN.
2886 if (XKnown.One.intersects(Mask))
2887 return true;
2888 // The sign bit of Y is set. If some other bit is set then Y is not equal
2889 // to INT_MIN.
2890 if (YKnown.One.intersects(Mask))
2891 return true;
2892 }
2893
2894 // The sum of a non-negative number and a power of two is not zero.
2895 if (XKnown.isNonNegative() &&
2896 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
2897 return true;
2898 if (YKnown.isNonNegative() &&
2899 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
2900 return true;
2901
2902 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
2903}
2904
2905static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
2906 unsigned BitWidth, Value *X, Value *Y,
2907 unsigned Depth) {
2908 // (X - (X != 0)) is non zero
2909 // ((X != 0) - X) is non zero
2910 if (matchOpWithOpEqZero(X, Y))
2911 return true;
2912
2913 // TODO: Move this case into isKnownNonEqual().
2914 if (auto *C = dyn_cast<Constant>(X))
2915 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
2916 return true;
2917
2918 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
2919}
2920
2921static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
2922 unsigned BitWidth, Value *X, Value *Y, bool NSW,
2923 bool NUW, unsigned Depth) {
2924 // If X and Y are non-zero then so is X * Y as long as the multiplication
2925 // does not overflow.
2926 if (NSW || NUW)
2927 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
2928 isKnownNonZero(Y, DemandedElts, Q, Depth);
2929
2930 // If either X or Y is odd, then if the other is non-zero the result can't
2931 // be zero.
2932 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
2933 if (XKnown.One[0])
2934 return isKnownNonZero(Y, DemandedElts, Q, Depth);
2935
2936 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
2937 if (YKnown.One[0])
2938 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
2939
2940 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
2941 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
2942 // the lowest known One of X and Y. If they are non-zero, the result
2943 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
2944 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
2945 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
2946 BitWidth;
2947}
2948
2949static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
2950 const SimplifyQuery &Q, const KnownBits &KnownVal,
2951 unsigned Depth) {
2952 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2953 switch (I->getOpcode()) {
2954 case Instruction::Shl:
2955 return Lhs.shl(Rhs);
2956 case Instruction::LShr:
2957 return Lhs.lshr(Rhs);
2958 case Instruction::AShr:
2959 return Lhs.ashr(Rhs);
2960 default:
2961 llvm_unreachable("Unknown Shift Opcode");
2962 }
2963 };
2964
2965 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2966 switch (I->getOpcode()) {
2967 case Instruction::Shl:
2968 return Lhs.lshr(Rhs);
2969 case Instruction::LShr:
2970 case Instruction::AShr:
2971 return Lhs.shl(Rhs);
2972 default:
2973 llvm_unreachable("Unknown Shift Opcode");
2974 }
2975 };
2976
2977 if (KnownVal.isUnknown())
2978 return false;
2979
2980 KnownBits KnownCnt =
2981 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
2982 APInt MaxShift = KnownCnt.getMaxValue();
2983 unsigned NumBits = KnownVal.getBitWidth();
2984 if (MaxShift.uge(NumBits))
2985 return false;
2986
2987 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
2988 return true;
2989
2990 // If all of the bits shifted out are known to be zero, and Val is known
2991 // non-zero then at least one non-zero bit must remain.
2992 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
2993 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
2994 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
2995 return true;
2996
2997 return false;
2998}
2999
3001 const APInt &DemandedElts,
3002 const SimplifyQuery &Q, unsigned Depth) {
3003 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3004 switch (I->getOpcode()) {
3005 case Instruction::Alloca:
3006 // Alloca never returns null, malloc might.
3007 return I->getType()->getPointerAddressSpace() == 0;
3008 case Instruction::GetElementPtr:
3009 if (I->getType()->isPointerTy())
3011 break;
3012 case Instruction::BitCast: {
3013 // We need to be a bit careful here. We can only peek through the bitcast
3014 // if the scalar size of elements in the operand are smaller than and a
3015 // multiple of the size they are casting too. Take three cases:
3016 //
3017 // 1) Unsafe:
3018 // bitcast <2 x i16> %NonZero to <4 x i8>
3019 //
3020 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3021 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3022 // guranteed (imagine just sign bit set in the 2 i16 elements).
3023 //
3024 // 2) Unsafe:
3025 // bitcast <4 x i3> %NonZero to <3 x i4>
3026 //
3027 // Even though the scalar size of the src (`i3`) is smaller than the
3028 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3029 // its possible for the `3 x i4` elements to be zero because there are
3030 // some elements in the destination that don't contain any full src
3031 // element.
3032 //
3033 // 3) Safe:
3034 // bitcast <4 x i8> %NonZero to <2 x i16>
3035 //
3036 // This is always safe as non-zero in the 4 i8 elements implies
3037 // non-zero in the combination of any two adjacent ones. Since i8 is a
3038 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3039 // This all implies the 2 i16 elements are non-zero.
3040 Type *FromTy = I->getOperand(0)->getType();
3041 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3042 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3043 return isKnownNonZero(I->getOperand(0), Q, Depth);
3044 } break;
3045 case Instruction::IntToPtr:
3046 // Note that we have to take special care to avoid looking through
3047 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3048 // as casts that can alter the value, e.g., AddrSpaceCasts.
3049 if (!isa<ScalableVectorType>(I->getType()) &&
3050 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3051 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3052 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3053 break;
3054 case Instruction::PtrToInt:
3055 // Similar to int2ptr above, we can look through ptr2int here if the cast
3056 // is a no-op or an extend and not a truncate.
3057 if (!isa<ScalableVectorType>(I->getType()) &&
3058 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3059 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3060 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3061 break;
3062 case Instruction::Trunc:
3063 // nuw/nsw trunc preserves zero/non-zero status of input.
3064 if (auto *TI = dyn_cast<TruncInst>(I))
3065 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3066 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3067 break;
3068
3069 // Iff x - y != 0, then x ^ y != 0
3070 // Therefore we can do the same exact checks
3071 case Instruction::Xor:
3072 case Instruction::Sub:
3073 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3074 I->getOperand(1), Depth);
3075 case Instruction::Or:
3076 // (X | (X != 0)) is non zero
3077 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3078 return true;
3079 // X | Y != 0 if X != Y.
3080 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3081 Depth))
3082 return true;
3083 // X | Y != 0 if X != 0 or Y != 0.
3084 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3085 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3086 case Instruction::SExt:
3087 case Instruction::ZExt:
3088 // ext X != 0 if X != 0.
3089 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3090
3091 case Instruction::Shl: {
3092 // shl nsw/nuw can't remove any non-zero bits.
3094 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3095 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3096
3097 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3098 // if the lowest bit is shifted off the end.
3099 KnownBits Known(BitWidth);
3100 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3101 if (Known.One[0])
3102 return true;
3103
3104 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3105 }
3106 case Instruction::LShr:
3107 case Instruction::AShr: {
3108 // shr exact can only shift out zero bits.
3110 if (BO->isExact())
3111 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3112
3113 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3114 // defined if the sign bit is shifted off the end.
3115 KnownBits Known =
3116 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3117 if (Known.isNegative())
3118 return true;
3119
3120 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3121 }
3122 case Instruction::UDiv:
3123 case Instruction::SDiv: {
3124 // X / Y
3125 // div exact can only produce a zero if the dividend is zero.
3126 if (cast<PossiblyExactOperator>(I)->isExact())
3127 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3128
3129 KnownBits XKnown =
3130 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3131 // If X is fully unknown we won't be able to figure anything out so don't
3132 // both computing knownbits for Y.
3133 if (XKnown.isUnknown())
3134 return false;
3135
3136 KnownBits YKnown =
3137 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3138 if (I->getOpcode() == Instruction::SDiv) {
3139 // For signed division need to compare abs value of the operands.
3140 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3141 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3142 }
3143 // If X u>= Y then div is non zero (0/0 is UB).
3144 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3145 // If X is total unknown or X u< Y we won't be able to prove non-zero
3146 // with compute known bits so just return early.
3147 return XUgeY && *XUgeY;
3148 }
3149 case Instruction::Add: {
3150 // X + Y.
3151
3152 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3153 // non-zero.
3155 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3156 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3157 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3158 }
3159 case Instruction::Mul: {
3161 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3162 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3163 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3164 }
3165 case Instruction::Select: {
3166 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3167
3168 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3169 // then see if the select condition implies the arm is non-zero. For example
3170 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3171 // dominated by `X != 0`.
3172 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3173 Value *Op;
3174 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3175 // Op is trivially non-zero.
3176 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3177 return true;
3178
3179 // The condition of the select dominates the true/false arm. Check if the
3180 // condition implies that a given arm is non-zero.
3181 Value *X;
3182 CmpPredicate Pred;
3183 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3184 return false;
3185
3186 if (!IsTrueArm)
3187 Pred = ICmpInst::getInversePredicate(Pred);
3188
3189 return cmpExcludesZero(Pred, X);
3190 };
3191
3192 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3193 SelectArmIsNonZero(/* IsTrueArm */ false))
3194 return true;
3195 break;
3196 }
3197 case Instruction::PHI: {
3198 auto *PN = cast<PHINode>(I);
3200 return true;
3201
3202 // Check if all incoming values are non-zero using recursion.
3204 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3205 return llvm::all_of(PN->operands(), [&](const Use &U) {
3206 if (U.get() == PN)
3207 return true;
3208 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3209 // Check if the branch on the phi excludes zero.
3210 CmpPredicate Pred;
3211 Value *X;
3212 BasicBlock *TrueSucc, *FalseSucc;
3213 if (match(RecQ.CxtI,
3214 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3215 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3216 // Check for cases of duplicate successors.
3217 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3218 // If we're using the false successor, invert the predicate.
3219 if (FalseSucc == PN->getParent())
3220 Pred = CmpInst::getInversePredicate(Pred);
3221 if (cmpExcludesZero(Pred, X))
3222 return true;
3223 }
3224 }
3225 // Finally recurse on the edge and check it directly.
3226 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3227 });
3228 }
3229 case Instruction::InsertElement: {
3230 if (isa<ScalableVectorType>(I->getType()))
3231 break;
3232
3233 const Value *Vec = I->getOperand(0);
3234 const Value *Elt = I->getOperand(1);
3235 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3236
3237 unsigned NumElts = DemandedElts.getBitWidth();
3238 APInt DemandedVecElts = DemandedElts;
3239 bool SkipElt = false;
3240 // If we know the index we are inserting too, clear it from Vec check.
3241 if (CIdx && CIdx->getValue().ult(NumElts)) {
3242 DemandedVecElts.clearBit(CIdx->getZExtValue());
3243 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3244 }
3245
3246 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3247 // are non-zero.
3248 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3249 (DemandedVecElts.isZero() ||
3250 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3251 }
3252 case Instruction::ExtractElement:
3253 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3254 const Value *Vec = EEI->getVectorOperand();
3255 const Value *Idx = EEI->getIndexOperand();
3256 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3257 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3258 unsigned NumElts = VecTy->getNumElements();
3259 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3260 if (CIdx && CIdx->getValue().ult(NumElts))
3261 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3262 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3263 }
3264 }
3265 break;
3266 case Instruction::ShuffleVector: {
3267 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3268 if (!Shuf)
3269 break;
3270 APInt DemandedLHS, DemandedRHS;
3271 // For undef elements, we don't know anything about the common state of
3272 // the shuffle result.
3273 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3274 break;
3275 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3276 return (DemandedRHS.isZero() ||
3277 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3278 (DemandedLHS.isZero() ||
3279 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3280 }
3281 case Instruction::Freeze:
3282 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3283 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3284 Depth);
3285 case Instruction::Load: {
3286 auto *LI = cast<LoadInst>(I);
3287 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3288 // is never null.
3289 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3290 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3291 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3292 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3293 return true;
3294 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3296 }
3297
3298 // No need to fall through to computeKnownBits as range metadata is already
3299 // handled in isKnownNonZero.
3300 return false;
3301 }
3302 case Instruction::ExtractValue: {
3303 const WithOverflowInst *WO;
3305 switch (WO->getBinaryOp()) {
3306 default:
3307 break;
3308 case Instruction::Add:
3309 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3310 WO->getArgOperand(1),
3311 /*NSW=*/false,
3312 /*NUW=*/false, Depth);
3313 case Instruction::Sub:
3314 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3315 WO->getArgOperand(1), Depth);
3316 case Instruction::Mul:
3317 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3318 WO->getArgOperand(1),
3319 /*NSW=*/false, /*NUW=*/false, Depth);
3320 break;
3321 }
3322 }
3323 break;
3324 }
3325 case Instruction::Call:
3326 case Instruction::Invoke: {
3327 const auto *Call = cast<CallBase>(I);
3328 if (I->getType()->isPointerTy()) {
3329 if (Call->isReturnNonNull())
3330 return true;
3331 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3332 return isKnownNonZero(RP, Q, Depth);
3333 } else {
3334 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3336 if (std::optional<ConstantRange> Range = Call->getRange()) {
3337 const APInt ZeroValue(Range->getBitWidth(), 0);
3338 if (!Range->contains(ZeroValue))
3339 return true;
3340 }
3341 if (const Value *RV = Call->getReturnedArgOperand())
3342 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3343 return true;
3344 }
3345
3346 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3347 switch (II->getIntrinsicID()) {
3348 case Intrinsic::sshl_sat:
3349 case Intrinsic::ushl_sat:
3350 case Intrinsic::abs:
3351 case Intrinsic::bitreverse:
3352 case Intrinsic::bswap:
3353 case Intrinsic::ctpop:
3354 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3355 // NB: We don't do usub_sat here as in any case we can prove its
3356 // non-zero, we will fold it to `sub nuw` in InstCombine.
3357 case Intrinsic::ssub_sat:
3358 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3359 II->getArgOperand(1), Depth);
3360 case Intrinsic::sadd_sat:
3361 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3362 II->getArgOperand(1),
3363 /*NSW=*/true, /* NUW=*/false, Depth);
3364 // Vec reverse preserves zero/non-zero status from input vec.
3365 case Intrinsic::vector_reverse:
3366 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3367 Q, Depth);
3368 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3369 case Intrinsic::vector_reduce_or:
3370 case Intrinsic::vector_reduce_umax:
3371 case Intrinsic::vector_reduce_umin:
3372 case Intrinsic::vector_reduce_smax:
3373 case Intrinsic::vector_reduce_smin:
3374 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3375 case Intrinsic::umax:
3376 case Intrinsic::uadd_sat:
3377 // umax(X, (X != 0)) is non zero
3378 // X +usat (X != 0) is non zero
3379 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3380 return true;
3381
3382 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3383 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3384 case Intrinsic::smax: {
3385 // If either arg is strictly positive the result is non-zero. Otherwise
3386 // the result is non-zero if both ops are non-zero.
3387 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3388 const KnownBits &OpKnown) {
3389 if (!OpNonZero.has_value())
3390 OpNonZero = OpKnown.isNonZero() ||
3391 isKnownNonZero(Op, DemandedElts, Q, Depth);
3392 return *OpNonZero;
3393 };
3394 // Avoid re-computing isKnownNonZero.
3395 std::optional<bool> Op0NonZero, Op1NonZero;
3396 KnownBits Op1Known =
3397 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3398 if (Op1Known.isNonNegative() &&
3399 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3400 return true;
3401 KnownBits Op0Known =
3402 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3403 if (Op0Known.isNonNegative() &&
3404 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3405 return true;
3406 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3407 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3408 }
3409 case Intrinsic::smin: {
3410 // If either arg is negative the result is non-zero. Otherwise
3411 // the result is non-zero if both ops are non-zero.
3412 KnownBits Op1Known =
3413 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3414 if (Op1Known.isNegative())
3415 return true;
3416 KnownBits Op0Known =
3417 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3418 if (Op0Known.isNegative())
3419 return true;
3420
3421 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3422 return true;
3423 }
3424 [[fallthrough]];
3425 case Intrinsic::umin:
3426 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3427 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3428 case Intrinsic::cttz:
3429 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3430 .Zero[0];
3431 case Intrinsic::ctlz:
3432 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3433 .isNonNegative();
3434 case Intrinsic::fshr:
3435 case Intrinsic::fshl:
3436 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3437 if (II->getArgOperand(0) == II->getArgOperand(1))
3438 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3439 break;
3440 case Intrinsic::vscale:
3441 return true;
3442 case Intrinsic::experimental_get_vector_length:
3443 return isKnownNonZero(I->getOperand(0), Q, Depth);
3444 default:
3445 break;
3446 }
3447 break;
3448 }
3449
3450 return false;
3451 }
3452 }
3453
3454 KnownBits Known(BitWidth);
3455 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3456 return Known.One != 0;
3457}
3458
3459/// Return true if the given value is known to be non-zero when defined. For
3460/// vectors, return true if every demanded element is known to be non-zero when
3461/// defined. For pointers, if the context instruction and dominator tree are
3462/// specified, perform context-sensitive analysis and return true if the
3463/// pointer couldn't possibly be null at the specified instruction.
3464/// Supports values with integer or pointer type and vectors of integers.
3465bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3466 const SimplifyQuery &Q, unsigned Depth) {
3467 Type *Ty = V->getType();
3468
3469#ifndef NDEBUG
3470 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3471
3472 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3473 assert(
3474 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3475 "DemandedElt width should equal the fixed vector number of elements");
3476 } else {
3477 assert(DemandedElts == APInt(1, 1) &&
3478 "DemandedElt width should be 1 for scalars");
3479 }
3480#endif
3481
3482 if (auto *C = dyn_cast<Constant>(V)) {
3483 if (C->isNullValue())
3484 return false;
3485 if (isa<ConstantInt>(C))
3486 // Must be non-zero due to null test above.
3487 return true;
3488
3489 // For constant vectors, check that all elements are poison or known
3490 // non-zero to determine that the whole vector is known non-zero.
3491 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3492 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3493 if (!DemandedElts[i])
3494 continue;
3495 Constant *Elt = C->getAggregateElement(i);
3496 if (!Elt || Elt->isNullValue())
3497 return false;
3498 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3499 return false;
3500 }
3501 return true;
3502 }
3503
3504 // Constant ptrauth can be null, iff the base pointer can be.
3505 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3506 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3507
3508 // A global variable in address space 0 is non null unless extern weak
3509 // or an absolute symbol reference. Other address spaces may have null as a
3510 // valid address for a global, so we can't assume anything.
3511 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3512 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3513 GV->getType()->getAddressSpace() == 0)
3514 return true;
3515 }
3516
3517 // For constant expressions, fall through to the Operator code below.
3518 if (!isa<ConstantExpr>(V))
3519 return false;
3520 }
3521
3522 if (const auto *A = dyn_cast<Argument>(V))
3523 if (std::optional<ConstantRange> Range = A->getRange()) {
3524 const APInt ZeroValue(Range->getBitWidth(), 0);
3525 if (!Range->contains(ZeroValue))
3526 return true;
3527 }
3528
3529 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3530 return true;
3531
3532 // Some of the tests below are recursive, so bail out if we hit the limit.
3534 return false;
3535
3536 // Check for pointer simplifications.
3537
3538 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3539 // A byval, inalloca may not be null in a non-default addres space. A
3540 // nonnull argument is assumed never 0.
3541 if (const Argument *A = dyn_cast<Argument>(V)) {
3542 if (((A->hasPassPointeeByValueCopyAttr() &&
3543 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3544 A->hasNonNullAttr()))
3545 return true;
3546 }
3547 }
3548
3549 if (const auto *I = dyn_cast<Operator>(V))
3550 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3551 return true;
3552
3553 if (!isa<Constant>(V) &&
3555 return true;
3556
3557 if (const Value *Stripped = stripNullTest(V))
3558 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3559
3560 return false;
3561}
3562
3564 unsigned Depth) {
3565 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3566 APInt DemandedElts =
3567 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3568 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3569}
3570
3571/// If the pair of operators are the same invertible function, return the
3572/// the operands of the function corresponding to each input. Otherwise,
3573/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3574/// every input value to exactly one output value. This is equivalent to
3575/// saying that Op1 and Op2 are equal exactly when the specified pair of
3576/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3577static std::optional<std::pair<Value*, Value*>>
3579 const Operator *Op2) {
3580 if (Op1->getOpcode() != Op2->getOpcode())
3581 return std::nullopt;
3582
3583 auto getOperands = [&](unsigned OpNum) -> auto {
3584 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3585 };
3586
3587 switch (Op1->getOpcode()) {
3588 default:
3589 break;
3590 case Instruction::Or:
3591 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3592 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3593 break;
3594 [[fallthrough]];
3595 case Instruction::Xor:
3596 case Instruction::Add: {
3597 Value *Other;
3598 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3599 return std::make_pair(Op1->getOperand(1), Other);
3600 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3601 return std::make_pair(Op1->getOperand(0), Other);
3602 break;
3603 }
3604 case Instruction::Sub:
3605 if (Op1->getOperand(0) == Op2->getOperand(0))
3606 return getOperands(1);
3607 if (Op1->getOperand(1) == Op2->getOperand(1))
3608 return getOperands(0);
3609 break;
3610 case Instruction::Mul: {
3611 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3612 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3613 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3614 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3615 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3616 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3617 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3618 break;
3619
3620 // Assume operand order has been canonicalized
3621 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3622 isa<ConstantInt>(Op1->getOperand(1)) &&
3623 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3624 return getOperands(0);
3625 break;
3626 }
3627 case Instruction::Shl: {
3628 // Same as multiplies, with the difference that we don't need to check
3629 // for a non-zero multiply. Shifts always multiply by non-zero.
3630 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3631 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3632 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3633 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3634 break;
3635
3636 if (Op1->getOperand(1) == Op2->getOperand(1))
3637 return getOperands(0);
3638 break;
3639 }
3640 case Instruction::AShr:
3641 case Instruction::LShr: {
3642 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3643 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3644 if (!PEO1->isExact() || !PEO2->isExact())
3645 break;
3646
3647 if (Op1->getOperand(1) == Op2->getOperand(1))
3648 return getOperands(0);
3649 break;
3650 }
3651 case Instruction::SExt:
3652 case Instruction::ZExt:
3653 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3654 return getOperands(0);
3655 break;
3656 case Instruction::PHI: {
3657 const PHINode *PN1 = cast<PHINode>(Op1);
3658 const PHINode *PN2 = cast<PHINode>(Op2);
3659
3660 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3661 // are a single invertible function of the start values? Note that repeated
3662 // application of an invertible function is also invertible
3663 BinaryOperator *BO1 = nullptr;
3664 Value *Start1 = nullptr, *Step1 = nullptr;
3665 BinaryOperator *BO2 = nullptr;
3666 Value *Start2 = nullptr, *Step2 = nullptr;
3667 if (PN1->getParent() != PN2->getParent() ||
3668 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3669 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3670 break;
3671
3672 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3673 cast<Operator>(BO2));
3674 if (!Values)
3675 break;
3676
3677 // We have to be careful of mutually defined recurrences here. Ex:
3678 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3679 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3680 // The invertibility of these is complicated, and not worth reasoning
3681 // about (yet?).
3682 if (Values->first != PN1 || Values->second != PN2)
3683 break;
3684
3685 return std::make_pair(Start1, Start2);
3686 }
3687 }
3688 return std::nullopt;
3689}
3690
3691/// Return true if V1 == (binop V2, X), where X is known non-zero.
3692/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3693/// implies V2 != V1.
3694static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3695 const APInt &DemandedElts,
3696 const SimplifyQuery &Q, unsigned Depth) {
3698 if (!BO)
3699 return false;
3700 switch (BO->getOpcode()) {
3701 default:
3702 break;
3703 case Instruction::Or:
3704 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3705 break;
3706 [[fallthrough]];
3707 case Instruction::Xor:
3708 case Instruction::Add:
3709 Value *Op = nullptr;
3710 if (V2 == BO->getOperand(0))
3711 Op = BO->getOperand(1);
3712 else if (V2 == BO->getOperand(1))
3713 Op = BO->getOperand(0);
3714 else
3715 return false;
3716 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3717 }
3718 return false;
3719}
3720
3721/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3722/// the multiplication is nuw or nsw.
3723static bool isNonEqualMul(const Value *V1, const Value *V2,
3724 const APInt &DemandedElts, const SimplifyQuery &Q,
3725 unsigned Depth) {
3726 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3727 const APInt *C;
3728 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3729 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3730 !C->isZero() && !C->isOne() &&
3731 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3732 }
3733 return false;
3734}
3735
3736/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3737/// the shift is nuw or nsw.
3738static bool isNonEqualShl(const Value *V1, const Value *V2,
3739 const APInt &DemandedElts, const SimplifyQuery &Q,
3740 unsigned Depth) {
3741 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3742 const APInt *C;
3743 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3744 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3745 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3746 }
3747 return false;
3748}
3749
3750static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3751 const APInt &DemandedElts, const SimplifyQuery &Q,
3752 unsigned Depth) {
3753 // Check two PHIs are in same block.
3754 if (PN1->getParent() != PN2->getParent())
3755 return false;
3756
3758 bool UsedFullRecursion = false;
3759 for (const BasicBlock *IncomBB : PN1->blocks()) {
3760 if (!VisitedBBs.insert(IncomBB).second)
3761 continue; // Don't reprocess blocks that we have dealt with already.
3762 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3763 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3764 const APInt *C1, *C2;
3765 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3766 continue;
3767
3768 // Only one pair of phi operands is allowed for full recursion.
3769 if (UsedFullRecursion)
3770 return false;
3771
3773 RecQ.CxtI = IncomBB->getTerminator();
3774 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3775 return false;
3776 UsedFullRecursion = true;
3777 }
3778 return true;
3779}
3780
3781static bool isNonEqualSelect(const Value *V1, const Value *V2,
3782 const APInt &DemandedElts, const SimplifyQuery &Q,
3783 unsigned Depth) {
3784 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3785 if (!SI1)
3786 return false;
3787
3788 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3789 const Value *Cond1 = SI1->getCondition();
3790 const Value *Cond2 = SI2->getCondition();
3791 if (Cond1 == Cond2)
3792 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3793 DemandedElts, Q, Depth + 1) &&
3794 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3795 DemandedElts, Q, Depth + 1);
3796 }
3797 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
3798 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
3799}
3800
3801// Check to see if A is both a GEP and is the incoming value for a PHI in the
3802// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3803// one of them being the recursive GEP A and the other a ptr at same base and at
3804// the same/higher offset than B we are only incrementing the pointer further in
3805// loop if offset of recursive GEP is greater than 0.
3807 const SimplifyQuery &Q) {
3808 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3809 return false;
3810
3811 auto *GEPA = dyn_cast<GEPOperator>(A);
3812 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3813 return false;
3814
3815 // Handle 2 incoming PHI values with one being a recursive GEP.
3816 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3817 if (!PN || PN->getNumIncomingValues() != 2)
3818 return false;
3819
3820 // Search for the recursive GEP as an incoming operand, and record that as
3821 // Step.
3822 Value *Start = nullptr;
3823 Value *Step = const_cast<Value *>(A);
3824 if (PN->getIncomingValue(0) == Step)
3825 Start = PN->getIncomingValue(1);
3826 else if (PN->getIncomingValue(1) == Step)
3827 Start = PN->getIncomingValue(0);
3828 else
3829 return false;
3830
3831 // Other incoming node base should match the B base.
3832 // StartOffset >= OffsetB && StepOffset > 0?
3833 // StartOffset <= OffsetB && StepOffset < 0?
3834 // Is non-equal if above are true.
3835 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3836 // optimisation to inbounds GEPs only.
3837 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3838 APInt StartOffset(IndexWidth, 0);
3839 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3840 APInt StepOffset(IndexWidth, 0);
3841 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3842
3843 // Check if Base Pointer of Step matches the PHI.
3844 if (Step != PN)
3845 return false;
3846 APInt OffsetB(IndexWidth, 0);
3847 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3848 return Start == B &&
3849 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
3850 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
3851}
3852
3853static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
3854 const SimplifyQuery &Q, unsigned Depth) {
3855 if (!Q.CxtI)
3856 return false;
3857
3858 // Try to infer NonEqual based on information from dominating conditions.
3859 if (Q.DC && Q.DT) {
3860 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
3861 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
3862 Value *Cond = BI->getCondition();
3863 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
3864 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
3866 /*LHSIsTrue=*/true, Depth)
3867 .value_or(false))
3868 return true;
3869
3870 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
3871 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
3873 /*LHSIsTrue=*/false, Depth)
3874 .value_or(false))
3875 return true;
3876 }
3877
3878 return false;
3879 };
3880
3881 if (IsKnownNonEqualFromDominatingCondition(V1) ||
3882 IsKnownNonEqualFromDominatingCondition(V2))
3883 return true;
3884 }
3885
3886 if (!Q.AC)
3887 return false;
3888
3889 // Try to infer NonEqual based on information from assumptions.
3890 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
3891 if (!AssumeVH)
3892 continue;
3893 CallInst *I = cast<CallInst>(AssumeVH);
3894
3895 assert(I->getFunction() == Q.CxtI->getFunction() &&
3896 "Got assumption for the wrong function!");
3897 assert(I->getIntrinsicID() == Intrinsic::assume &&
3898 "must be an assume intrinsic");
3899
3900 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
3901 /*LHSIsTrue=*/true, Depth)
3902 .value_or(false) &&
3904 return true;
3905 }
3906
3907 return false;
3908}
3909
3910/// Return true if it is known that V1 != V2.
3911static bool isKnownNonEqual(const Value *V1, const Value *V2,
3912 const APInt &DemandedElts, const SimplifyQuery &Q,
3913 unsigned Depth) {
3914 if (V1 == V2)
3915 return false;
3916 if (V1->getType() != V2->getType())
3917 // We can't look through casts yet.
3918 return false;
3919
3921 return false;
3922
3923 // See if we can recurse through (exactly one of) our operands. This
3924 // requires our operation be 1-to-1 and map every input value to exactly
3925 // one output value. Such an operation is invertible.
3926 auto *O1 = dyn_cast<Operator>(V1);
3927 auto *O2 = dyn_cast<Operator>(V2);
3928 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3929 if (auto Values = getInvertibleOperands(O1, O2))
3930 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
3931 Depth + 1);
3932
3933 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3934 const PHINode *PN2 = cast<PHINode>(V2);
3935 // FIXME: This is missing a generalization to handle the case where one is
3936 // a PHI and another one isn't.
3937 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
3938 return true;
3939 };
3940 }
3941
3942 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
3943 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
3944 return true;
3945
3946 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
3947 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
3948 return true;
3949
3950 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
3951 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
3952 return true;
3953
3954 if (V1->getType()->isIntOrIntVectorTy()) {
3955 // Are any known bits in V1 contradictory to known bits in V2? If V1
3956 // has a known zero where V2 has a known one, they must not be equal.
3957 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
3958 if (!Known1.isUnknown()) {
3959 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
3960 if (Known1.Zero.intersects(Known2.One) ||
3961 Known2.Zero.intersects(Known1.One))
3962 return true;
3963 }
3964 }
3965
3966 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
3967 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
3968 return true;
3969
3970 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3972 return true;
3973
3974 Value *A, *B;
3975 // PtrToInts are NonEqual if their Ptrs are NonEqual.
3976 // Check PtrToInt type matches the pointer size.
3977 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
3979 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
3980
3981 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
3982 return true;
3983
3984 return false;
3985}
3986
3987/// For vector constants, loop over the elements and find the constant with the
3988/// minimum number of sign bits. Return 0 if the value is not a vector constant
3989/// or if any element was not analyzed; otherwise, return the count for the
3990/// element with the minimum number of sign bits.
3992 const APInt &DemandedElts,
3993 unsigned TyBits) {
3994 const auto *CV = dyn_cast<Constant>(V);
3995 if (!CV || !isa<FixedVectorType>(CV->getType()))
3996 return 0;
3997
3998 unsigned MinSignBits = TyBits;
3999 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4000 for (unsigned i = 0; i != NumElts; ++i) {
4001 if (!DemandedElts[i])
4002 continue;
4003 // If we find a non-ConstantInt, bail out.
4004 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4005 if (!Elt)
4006 return 0;
4007
4008 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4009 }
4010
4011 return MinSignBits;
4012}
4013
4014static unsigned ComputeNumSignBitsImpl(const Value *V,
4015 const APInt &DemandedElts,
4016 const SimplifyQuery &Q, unsigned Depth);
4017
4018static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4019 const SimplifyQuery &Q, unsigned Depth) {
4020 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4021 assert(Result > 0 && "At least one sign bit needs to be present!");
4022 return Result;
4023}
4024
4025/// Return the number of times the sign bit of the register is replicated into
4026/// the other bits. We know that at least 1 bit is always equal to the sign bit
4027/// (itself), but other cases can give us information. For example, immediately
4028/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4029/// other, so we return 3. For vectors, return the number of sign bits for the
4030/// vector element with the minimum number of known sign bits of the demanded
4031/// elements in the vector specified by DemandedElts.
4032static unsigned ComputeNumSignBitsImpl(const Value *V,
4033 const APInt &DemandedElts,
4034 const SimplifyQuery &Q, unsigned Depth) {
4035 Type *Ty = V->getType();
4036#ifndef NDEBUG
4037 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4038
4039 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4040 assert(
4041 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4042 "DemandedElt width should equal the fixed vector number of elements");
4043 } else {
4044 assert(DemandedElts == APInt(1, 1) &&
4045 "DemandedElt width should be 1 for scalars");
4046 }
4047#endif
4048
4049 // We return the minimum number of sign bits that are guaranteed to be present
4050 // in V, so for undef we have to conservatively return 1. We don't have the
4051 // same behavior for poison though -- that's a FIXME today.
4052
4053 Type *ScalarTy = Ty->getScalarType();
4054 unsigned TyBits = ScalarTy->isPointerTy() ?
4055 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4056 Q.DL.getTypeSizeInBits(ScalarTy);
4057
4058 unsigned Tmp, Tmp2;
4059 unsigned FirstAnswer = 1;
4060
4061 // Note that ConstantInt is handled by the general computeKnownBits case
4062 // below.
4063
4065 return 1;
4066
4067 if (auto *U = dyn_cast<Operator>(V)) {
4068 switch (Operator::getOpcode(V)) {
4069 default: break;
4070 case Instruction::BitCast: {
4071 Value *Src = U->getOperand(0);
4072 Type *SrcTy = Src->getType();
4073
4074 // Skip if the source type is not an integer or integer vector type
4075 // This ensures we only process integer-like types
4076 if (!SrcTy->isIntOrIntVectorTy())
4077 break;
4078
4079 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4080
4081 // Bitcast 'large element' scalar/vector to 'small element' vector.
4082 if ((SrcBits % TyBits) != 0)
4083 break;
4084
4085 // Only proceed if the destination type is a fixed-size vector
4086 if (isa<FixedVectorType>(Ty)) {
4087 // Fast case - sign splat can be simply split across the small elements.
4088 // This works for both vector and scalar sources
4089 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4090 if (Tmp == SrcBits)
4091 return TyBits;
4092 }
4093 break;
4094 }
4095 case Instruction::SExt:
4096 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4097 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4098 Tmp;
4099
4100 case Instruction::SDiv: {
4101 const APInt *Denominator;
4102 // sdiv X, C -> adds log(C) sign bits.
4103 if (match(U->getOperand(1), m_APInt(Denominator))) {
4104
4105 // Ignore non-positive denominator.
4106 if (!Denominator->isStrictlyPositive())
4107 break;
4108
4109 // Calculate the incoming numerator bits.
4110 unsigned NumBits =
4111 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4112
4113 // Add floor(log(C)) bits to the numerator bits.
4114 return std::min(TyBits, NumBits + Denominator->logBase2());
4115 }
4116 break;
4117 }
4118
4119 case Instruction::SRem: {
4120 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4121
4122 const APInt *Denominator;
4123 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4124 // positive constant. This let us put a lower bound on the number of sign
4125 // bits.
4126 if (match(U->getOperand(1), m_APInt(Denominator))) {
4127
4128 // Ignore non-positive denominator.
4129 if (Denominator->isStrictlyPositive()) {
4130 // Calculate the leading sign bit constraints by examining the
4131 // denominator. Given that the denominator is positive, there are two
4132 // cases:
4133 //
4134 // 1. The numerator is positive. The result range is [0,C) and
4135 // [0,C) u< (1 << ceilLogBase2(C)).
4136 //
4137 // 2. The numerator is negative. Then the result range is (-C,0] and
4138 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4139 //
4140 // Thus a lower bound on the number of sign bits is `TyBits -
4141 // ceilLogBase2(C)`.
4142
4143 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4144 Tmp = std::max(Tmp, ResBits);
4145 }
4146 }
4147 return Tmp;
4148 }
4149
4150 case Instruction::AShr: {
4151 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4152 // ashr X, C -> adds C sign bits. Vectors too.
4153 const APInt *ShAmt;
4154 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4155 if (ShAmt->uge(TyBits))
4156 break; // Bad shift.
4157 unsigned ShAmtLimited = ShAmt->getZExtValue();
4158 Tmp += ShAmtLimited;
4159 if (Tmp > TyBits) Tmp = TyBits;
4160 }
4161 return Tmp;
4162 }
4163 case Instruction::Shl: {
4164 const APInt *ShAmt;
4165 Value *X = nullptr;
4166 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4167 // shl destroys sign bits.
4168 if (ShAmt->uge(TyBits))
4169 break; // Bad shift.
4170 // We can look through a zext (more or less treating it as a sext) if
4171 // all extended bits are shifted out.
4172 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4173 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4174 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4175 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4176 } else
4177 Tmp =
4178 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4179 if (ShAmt->uge(Tmp))
4180 break; // Shifted all sign bits out.
4181 Tmp2 = ShAmt->getZExtValue();
4182 return Tmp - Tmp2;
4183 }
4184 break;
4185 }
4186 case Instruction::And:
4187 case Instruction::Or:
4188 case Instruction::Xor: // NOT is handled here.
4189 // Logical binary ops preserve the number of sign bits at the worst.
4190 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4191 if (Tmp != 1) {
4192 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4193 FirstAnswer = std::min(Tmp, Tmp2);
4194 // We computed what we know about the sign bits as our first
4195 // answer. Now proceed to the generic code that uses
4196 // computeKnownBits, and pick whichever answer is better.
4197 }
4198 break;
4199
4200 case Instruction::Select: {
4201 // If we have a clamp pattern, we know that the number of sign bits will
4202 // be the minimum of the clamp min/max range.
4203 const Value *X;
4204 const APInt *CLow, *CHigh;
4205 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4206 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4207
4208 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4209 if (Tmp == 1)
4210 break;
4211 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4212 return std::min(Tmp, Tmp2);
4213 }
4214
4215 case Instruction::Add:
4216 // Add can have at most one carry bit. Thus we know that the output
4217 // is, at worst, one more bit than the inputs.
4218 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4219 if (Tmp == 1) break;
4220
4221 // Special case decrementing a value (ADD X, -1):
4222 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4223 if (CRHS->isAllOnesValue()) {
4224 KnownBits Known(TyBits);
4225 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4226
4227 // If the input is known to be 0 or 1, the output is 0/-1, which is
4228 // all sign bits set.
4229 if ((Known.Zero | 1).isAllOnes())
4230 return TyBits;
4231
4232 // If we are subtracting one from a positive number, there is no carry
4233 // out of the result.
4234 if (Known.isNonNegative())
4235 return Tmp;
4236 }
4237
4238 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4239 if (Tmp2 == 1)
4240 break;
4241 return std::min(Tmp, Tmp2) - 1;
4242
4243 case Instruction::Sub:
4244 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4245 if (Tmp2 == 1)
4246 break;
4247
4248 // Handle NEG.
4249 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4250 if (CLHS->isNullValue()) {
4251 KnownBits Known(TyBits);
4252 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4253 // If the input is known to be 0 or 1, the output is 0/-1, which is
4254 // all sign bits set.
4255 if ((Known.Zero | 1).isAllOnes())
4256 return TyBits;
4257
4258 // If the input is known to be positive (the sign bit is known clear),
4259 // the output of the NEG has the same number of sign bits as the
4260 // input.
4261 if (Known.isNonNegative())
4262 return Tmp2;
4263
4264 // Otherwise, we treat this like a SUB.
4265 }
4266
4267 // Sub can have at most one carry bit. Thus we know that the output
4268 // is, at worst, one more bit than the inputs.
4269 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4270 if (Tmp == 1)
4271 break;
4272 return std::min(Tmp, Tmp2) - 1;
4273
4274 case Instruction::Mul: {
4275 // The output of the Mul can be at most twice the valid bits in the
4276 // inputs.
4277 unsigned SignBitsOp0 =
4278 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4279 if (SignBitsOp0 == 1)
4280 break;
4281 unsigned SignBitsOp1 =
4282 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4283 if (SignBitsOp1 == 1)
4284 break;
4285 unsigned OutValidBits =
4286 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4287 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4288 }
4289
4290 case Instruction::PHI: {
4291 const PHINode *PN = cast<PHINode>(U);
4292 unsigned NumIncomingValues = PN->getNumIncomingValues();
4293 // Don't analyze large in-degree PHIs.
4294 if (NumIncomingValues > 4) break;
4295 // Unreachable blocks may have zero-operand PHI nodes.
4296 if (NumIncomingValues == 0) break;
4297
4298 // Take the minimum of all incoming values. This can't infinitely loop
4299 // because of our depth threshold.
4301 Tmp = TyBits;
4302 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4303 if (Tmp == 1) return Tmp;
4304 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4305 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4306 DemandedElts, RecQ, Depth + 1));
4307 }
4308 return Tmp;
4309 }
4310
4311 case Instruction::Trunc: {
4312 // If the input contained enough sign bits that some remain after the
4313 // truncation, then we can make use of that. Otherwise we don't know
4314 // anything.
4315 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4316 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4317 if (Tmp > (OperandTyBits - TyBits))
4318 return Tmp - (OperandTyBits - TyBits);
4319
4320 return 1;
4321 }
4322
4323 case Instruction::ExtractElement:
4324 // Look through extract element. At the moment we keep this simple and
4325 // skip tracking the specific element. But at least we might find
4326 // information valid for all elements of the vector (for example if vector
4327 // is sign extended, shifted, etc).
4328 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4329
4330 case Instruction::ShuffleVector: {
4331 // Collect the minimum number of sign bits that are shared by every vector
4332 // element referenced by the shuffle.
4333 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4334 if (!Shuf) {
4335 // FIXME: Add support for shufflevector constant expressions.
4336 return 1;
4337 }
4338 APInt DemandedLHS, DemandedRHS;
4339 // For undef elements, we don't know anything about the common state of
4340 // the shuffle result.
4341 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4342 return 1;
4343 Tmp = std::numeric_limits<unsigned>::max();
4344 if (!!DemandedLHS) {
4345 const Value *LHS = Shuf->getOperand(0);
4346 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4347 }
4348 // If we don't know anything, early out and try computeKnownBits
4349 // fall-back.
4350 if (Tmp == 1)
4351 break;
4352 if (!!DemandedRHS) {
4353 const Value *RHS = Shuf->getOperand(1);
4354 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4355 Tmp = std::min(Tmp, Tmp2);
4356 }
4357 // If we don't know anything, early out and try computeKnownBits
4358 // fall-back.
4359 if (Tmp == 1)
4360 break;
4361 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4362 return Tmp;
4363 }
4364 case Instruction::Call: {
4365 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4366 switch (II->getIntrinsicID()) {
4367 default:
4368 break;
4369 case Intrinsic::abs:
4370 Tmp =
4371 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4372 if (Tmp == 1)
4373 break;
4374
4375 // Absolute value reduces number of sign bits by at most 1.
4376 return Tmp - 1;
4377 case Intrinsic::smin:
4378 case Intrinsic::smax: {
4379 const APInt *CLow, *CHigh;
4380 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4381 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4382 }
4383 }
4384 }
4385 }
4386 }
4387 }
4388
4389 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4390 // use this information.
4391
4392 // If we can examine all elements of a vector constant successfully, we're
4393 // done (we can't do any better than that). If not, keep trying.
4394 if (unsigned VecSignBits =
4395 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4396 return VecSignBits;
4397
4398 KnownBits Known(TyBits);
4399 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4400
4401 // If we know that the sign bit is either zero or one, determine the number of
4402 // identical bits in the top of the input value.
4403 return std::max(FirstAnswer, Known.countMinSignBits());
4404}
4405
4407 const TargetLibraryInfo *TLI) {
4408 const Function *F = CB.getCalledFunction();
4409 if (!F)
4411
4412 if (F->isIntrinsic())
4413 return F->getIntrinsicID();
4414
4415 // We are going to infer semantics of a library function based on mapping it
4416 // to an LLVM intrinsic. Check that the library function is available from
4417 // this callbase and in this environment.
4418 LibFunc Func;
4419 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4420 !CB.onlyReadsMemory())
4422
4423 switch (Func) {
4424 default:
4425 break;
4426 case LibFunc_sin:
4427 case LibFunc_sinf:
4428 case LibFunc_sinl:
4429 return Intrinsic::sin;
4430 case LibFunc_cos:
4431 case LibFunc_cosf:
4432 case LibFunc_cosl:
4433 return Intrinsic::cos;
4434 case LibFunc_tan:
4435 case LibFunc_tanf:
4436 case LibFunc_tanl:
4437 return Intrinsic::tan;
4438 case LibFunc_asin:
4439 case LibFunc_asinf:
4440 case LibFunc_asinl:
4441 return Intrinsic::asin;
4442 case LibFunc_acos:
4443 case LibFunc_acosf:
4444 case LibFunc_acosl:
4445 return Intrinsic::acos;
4446 case LibFunc_atan:
4447 case LibFunc_atanf:
4448 case LibFunc_atanl:
4449 return Intrinsic::atan;
4450 case LibFunc_atan2:
4451 case LibFunc_atan2f:
4452 case LibFunc_atan2l:
4453 return Intrinsic::atan2;
4454 case LibFunc_sinh:
4455 case LibFunc_sinhf:
4456 case LibFunc_sinhl:
4457 return Intrinsic::sinh;
4458 case LibFunc_cosh:
4459 case LibFunc_coshf:
4460 case LibFunc_coshl:
4461 return Intrinsic::cosh;
4462 case LibFunc_tanh:
4463 case LibFunc_tanhf:
4464 case LibFunc_tanhl:
4465 return Intrinsic::tanh;
4466 case LibFunc_exp:
4467 case LibFunc_expf:
4468 case LibFunc_expl:
4469 return Intrinsic::exp;
4470 case LibFunc_exp2:
4471 case LibFunc_exp2f:
4472 case LibFunc_exp2l:
4473 return Intrinsic::exp2;
4474 case LibFunc_exp10:
4475 case LibFunc_exp10f:
4476 case LibFunc_exp10l:
4477 return Intrinsic::exp10;
4478 case LibFunc_log:
4479 case LibFunc_logf:
4480 case LibFunc_logl:
4481 return Intrinsic::log;
4482 case LibFunc_log10:
4483 case LibFunc_log10f:
4484 case LibFunc_log10l:
4485 return Intrinsic::log10;
4486 case LibFunc_log2:
4487 case LibFunc_log2f:
4488 case LibFunc_log2l:
4489 return Intrinsic::log2;
4490 case LibFunc_fabs:
4491 case LibFunc_fabsf:
4492 case LibFunc_fabsl:
4493 return Intrinsic::fabs;
4494 case LibFunc_fmin:
4495 case LibFunc_fminf:
4496 case LibFunc_fminl:
4497 return Intrinsic::minnum;
4498 case LibFunc_fmax:
4499 case LibFunc_fmaxf:
4500 case LibFunc_fmaxl:
4501 return Intrinsic::maxnum;
4502 case LibFunc_copysign:
4503 case LibFunc_copysignf:
4504 case LibFunc_copysignl:
4505 return Intrinsic::copysign;
4506 case LibFunc_floor:
4507 case LibFunc_floorf:
4508 case LibFunc_floorl:
4509 return Intrinsic::floor;
4510 case LibFunc_ceil:
4511 case LibFunc_ceilf:
4512 case LibFunc_ceill:
4513 return Intrinsic::ceil;
4514 case LibFunc_trunc:
4515 case LibFunc_truncf:
4516 case LibFunc_truncl:
4517 return Intrinsic::trunc;
4518 case LibFunc_rint:
4519 case LibFunc_rintf:
4520 case LibFunc_rintl:
4521 return Intrinsic::rint;
4522 case LibFunc_nearbyint:
4523 case LibFunc_nearbyintf:
4524 case LibFunc_nearbyintl:
4525 return Intrinsic::nearbyint;
4526 case LibFunc_round:
4527 case LibFunc_roundf:
4528 case LibFunc_roundl:
4529 return Intrinsic::round;
4530 case LibFunc_roundeven:
4531 case LibFunc_roundevenf:
4532 case LibFunc_roundevenl:
4533 return Intrinsic::roundeven;
4534 case LibFunc_pow:
4535 case LibFunc_powf:
4536 case LibFunc_powl:
4537 return Intrinsic::pow;
4538 case LibFunc_sqrt:
4539 case LibFunc_sqrtf:
4540 case LibFunc_sqrtl:
4541 return Intrinsic::sqrt;
4542 }
4543
4545}
4546
4547static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4548 Ty = Ty->getScalarType();
4549 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4550 return Mode.Output == DenormalMode::IEEE ||
4552}
4553/// Given an exploded icmp instruction, return true if the comparison only
4554/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4555/// the result of the comparison is true when the input value is signed.
4557 bool &TrueIfSigned) {
4558 switch (Pred) {
4559 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4560 TrueIfSigned = true;
4561 return RHS.isZero();
4562 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4563 TrueIfSigned = true;
4564 return RHS.isAllOnes();
4565 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4566 TrueIfSigned = false;
4567 return RHS.isAllOnes();
4568 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4569 TrueIfSigned = false;
4570 return RHS.isZero();
4571 case ICmpInst::ICMP_UGT:
4572 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4573 TrueIfSigned = true;
4574 return RHS.isMaxSignedValue();
4575 case ICmpInst::ICMP_UGE:
4576 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4577 TrueIfSigned = true;
4578 return RHS.isMinSignedValue();
4579 case ICmpInst::ICMP_ULT:
4580 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4581 TrueIfSigned = false;
4582 return RHS.isMinSignedValue();
4583 case ICmpInst::ICMP_ULE:
4584 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4585 TrueIfSigned = false;
4586 return RHS.isMaxSignedValue();
4587 default:
4588 return false;
4589 }
4590}
4591
4593 bool CondIsTrue,
4594 const Instruction *CxtI,
4595 KnownFPClass &KnownFromContext,
4596 unsigned Depth = 0) {
4597 Value *A, *B;
4599 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4600 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4601 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4602 Depth + 1);
4603 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4604 Depth + 1);
4605 return;
4606 }
4608 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4609 Depth + 1);
4610 return;
4611 }
4612 CmpPredicate Pred;
4613 Value *LHS;
4614 uint64_t ClassVal = 0;
4615 const APFloat *CRHS;
4616 const APInt *RHS;
4617 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4618 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4619 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4620 if (CmpVal == V)
4621 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4623 m_Specific(V), m_ConstantInt(ClassVal)))) {
4624 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4625 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4626 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4627 m_APInt(RHS)))) {
4628 bool TrueIfSigned;
4629 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4630 return;
4631 if (TrueIfSigned == CondIsTrue)
4632 KnownFromContext.signBitMustBeOne();
4633 else
4634 KnownFromContext.signBitMustBeZero();
4635 }
4636}
4637
4639 const SimplifyQuery &Q) {
4640 KnownFPClass KnownFromContext;
4641
4642 if (Q.CC && Q.CC->AffectedValues.contains(V))
4644 KnownFromContext);
4645
4646 if (!Q.CxtI)
4647 return KnownFromContext;
4648
4649 if (Q.DC && Q.DT) {
4650 // Handle dominating conditions.
4651 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4652 Value *Cond = BI->getCondition();
4653
4654 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4655 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4656 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4657 KnownFromContext);
4658
4659 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4660 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4661 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4662 KnownFromContext);
4663 }
4664 }
4665
4666 if (!Q.AC)
4667 return KnownFromContext;
4668
4669 // Try to restrict the floating-point classes based on information from
4670 // assumptions.
4671 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4672 if (!AssumeVH)
4673 continue;
4674 CallInst *I = cast<CallInst>(AssumeVH);
4675
4676 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4677 "Got assumption for the wrong function!");
4678 assert(I->getIntrinsicID() == Intrinsic::assume &&
4679 "must be an assume intrinsic");
4680
4681 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4682 continue;
4683
4684 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4685 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4686 }
4687
4688 return KnownFromContext;
4689}
4690
4691void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4692 FPClassTest InterestedClasses, KnownFPClass &Known,
4693 const SimplifyQuery &Q, unsigned Depth);
4694
4695static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4696 FPClassTest InterestedClasses,
4697 const SimplifyQuery &Q, unsigned Depth) {
4698 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4699 APInt DemandedElts =
4700 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4701 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4702}
4703
4705 const APInt &DemandedElts,
4706 FPClassTest InterestedClasses,
4707 KnownFPClass &Known,
4708 const SimplifyQuery &Q,
4709 unsigned Depth) {
4710 if ((InterestedClasses &
4712 return;
4713
4714 KnownFPClass KnownSrc;
4715 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4716 KnownSrc, Q, Depth + 1);
4717
4718 // Sign should be preserved
4719 // TODO: Handle cannot be ordered greater than zero
4720 if (KnownSrc.cannotBeOrderedLessThanZero())
4722
4723 Known.propagateNaN(KnownSrc, true);
4724
4725 // Infinity needs a range check.
4726}
4727
4728void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4729 FPClassTest InterestedClasses, KnownFPClass &Known,
4730 const SimplifyQuery &Q, unsigned Depth) {
4731 assert(Known.isUnknown() && "should not be called with known information");
4732
4733 if (!DemandedElts) {
4734 // No demanded elts, better to assume we don't know anything.
4735 Known.resetAll();
4736 return;
4737 }
4738
4739 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4740
4741 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4742 Known.KnownFPClasses = CFP->getValueAPF().classify();
4743 Known.SignBit = CFP->isNegative();
4744 return;
4745 }
4746
4748 Known.KnownFPClasses = fcPosZero;
4749 Known.SignBit = false;
4750 return;
4751 }
4752
4753 if (isa<PoisonValue>(V)) {
4754 Known.KnownFPClasses = fcNone;
4755 Known.SignBit = false;
4756 return;
4757 }
4758
4759 // Try to handle fixed width vector constants
4760 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
4761 const Constant *CV = dyn_cast<Constant>(V);
4762 if (VFVTy && CV) {
4763 Known.KnownFPClasses = fcNone;
4764 bool SignBitAllZero = true;
4765 bool SignBitAllOne = true;
4766
4767 // For vectors, verify that each element is not NaN.
4768 unsigned NumElts = VFVTy->getNumElements();
4769 for (unsigned i = 0; i != NumElts; ++i) {
4770 if (!DemandedElts[i])
4771 continue;
4772
4773 Constant *Elt = CV->getAggregateElement(i);
4774 if (!Elt) {
4775 Known = KnownFPClass();
4776 return;
4777 }
4778 if (isa<PoisonValue>(Elt))
4779 continue;
4780 auto *CElt = dyn_cast<ConstantFP>(Elt);
4781 if (!CElt) {
4782 Known = KnownFPClass();
4783 return;
4784 }
4785
4786 const APFloat &C = CElt->getValueAPF();
4787 Known.KnownFPClasses |= C.classify();
4788 if (C.isNegative())
4789 SignBitAllZero = false;
4790 else
4791 SignBitAllOne = false;
4792 }
4793 if (SignBitAllOne != SignBitAllZero)
4794 Known.SignBit = SignBitAllOne;
4795 return;
4796 }
4797
4798 FPClassTest KnownNotFromFlags = fcNone;
4799 if (const auto *CB = dyn_cast<CallBase>(V))
4800 KnownNotFromFlags |= CB->getRetNoFPClass();
4801 else if (const auto *Arg = dyn_cast<Argument>(V))
4802 KnownNotFromFlags |= Arg->getNoFPClass();
4803
4804 const Operator *Op = dyn_cast<Operator>(V);
4806 if (FPOp->hasNoNaNs())
4807 KnownNotFromFlags |= fcNan;
4808 if (FPOp->hasNoInfs())
4809 KnownNotFromFlags |= fcInf;
4810 }
4811
4812 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
4813 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
4814
4815 // We no longer need to find out about these bits from inputs if we can
4816 // assume this from flags/attributes.
4817 InterestedClasses &= ~KnownNotFromFlags;
4818
4819 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
4820 Known.knownNot(KnownNotFromFlags);
4821 if (!Known.SignBit && AssumedClasses.SignBit) {
4822 if (*AssumedClasses.SignBit)
4823 Known.signBitMustBeOne();
4824 else
4825 Known.signBitMustBeZero();
4826 }
4827 });
4828
4829 if (!Op)
4830 return;
4831
4832 // All recursive calls that increase depth must come after this.
4834 return;
4835
4836 const unsigned Opc = Op->getOpcode();
4837 switch (Opc) {
4838 case Instruction::FNeg: {
4839 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4840 Known, Q, Depth + 1);
4841 Known.fneg();
4842 break;
4843 }
4844 case Instruction::Select: {
4845 Value *Cond = Op->getOperand(0);
4846 Value *LHS = Op->getOperand(1);
4847 Value *RHS = Op->getOperand(2);
4848
4849 FPClassTest FilterLHS = fcAllFlags;
4850 FPClassTest FilterRHS = fcAllFlags;
4851
4852 Value *TestedValue = nullptr;
4853 FPClassTest MaskIfTrue = fcAllFlags;
4854 FPClassTest MaskIfFalse = fcAllFlags;
4855 uint64_t ClassVal = 0;
4856 const Function *F = cast<Instruction>(Op)->getFunction();
4857 CmpPredicate Pred;
4858 Value *CmpLHS, *CmpRHS;
4859 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
4860 // If the select filters out a value based on the class, it no longer
4861 // participates in the class of the result
4862
4863 // TODO: In some degenerate cases we can infer something if we try again
4864 // without looking through sign operations.
4865 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
4866 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
4867 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
4868 } else if (match(Cond,
4870 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
4871 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
4872 MaskIfTrue = TestedMask;
4873 MaskIfFalse = ~TestedMask;
4874 }
4875
4876 if (TestedValue == LHS) {
4877 // match !isnan(x) ? x : y
4878 FilterLHS = MaskIfTrue;
4879 } else if (TestedValue == RHS) { // && IsExactClass
4880 // match !isnan(x) ? y : x
4881 FilterRHS = MaskIfFalse;
4882 }
4883
4884 KnownFPClass Known2;
4885 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
4886 Q, Depth + 1);
4887 Known.KnownFPClasses &= FilterLHS;
4888
4889 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
4890 Known2, Q, Depth + 1);
4891 Known2.KnownFPClasses &= FilterRHS;
4892
4893 Known |= Known2;
4894 break;
4895 }
4896 case Instruction::Call: {
4897 const CallInst *II = cast<CallInst>(Op);
4898 const Intrinsic::ID IID = II->getIntrinsicID();
4899 switch (IID) {
4900 case Intrinsic::fabs: {
4901 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
4902 // If we only care about the sign bit we don't need to inspect the
4903 // operand.
4904 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
4905 InterestedClasses, Known, Q, Depth + 1);
4906 }
4907
4908 Known.fabs();
4909 break;
4910 }
4911 case Intrinsic::copysign: {
4912 KnownFPClass KnownSign;
4913
4914 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4915 Known, Q, Depth + 1);
4916 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4917 KnownSign, Q, Depth + 1);
4918 Known.copysign(KnownSign);
4919 break;
4920 }
4921 case Intrinsic::fma:
4922 case Intrinsic::fmuladd: {
4923 if ((InterestedClasses & fcNegative) == fcNone)
4924 break;
4925
4926 if (II->getArgOperand(0) != II->getArgOperand(1))
4927 break;
4928
4929 // The multiply cannot be -0 and therefore the add can't be -0
4930 Known.knownNot(fcNegZero);
4931
4932 // x * x + y is non-negative if y is non-negative.
4933 KnownFPClass KnownAddend;
4934 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
4935 KnownAddend, Q, Depth + 1);
4936
4937 if (KnownAddend.cannotBeOrderedLessThanZero())
4938 Known.knownNot(fcNegative);
4939 break;
4940 }
4941 case Intrinsic::sqrt:
4942 case Intrinsic::experimental_constrained_sqrt: {
4943 KnownFPClass KnownSrc;
4944 FPClassTest InterestedSrcs = InterestedClasses;
4945 if (InterestedClasses & fcNan)
4946 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
4947
4948 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
4949 KnownSrc, Q, Depth + 1);
4950
4951 if (KnownSrc.isKnownNeverPosInfinity())
4952 Known.knownNot(fcPosInf);
4953 if (KnownSrc.isKnownNever(fcSNan))
4954 Known.knownNot(fcSNan);
4955
4956 // Any negative value besides -0 returns a nan.
4957 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
4958 Known.knownNot(fcNan);
4959
4960 // The only negative value that can be returned is -0 for -0 inputs.
4962
4963 // If the input denormal mode could be PreserveSign, a negative
4964 // subnormal input could produce a negative zero output.
4965 const Function *F = II->getFunction();
4966 const fltSemantics &FltSem =
4967 II->getType()->getScalarType()->getFltSemantics();
4968
4969 if (Q.IIQ.hasNoSignedZeros(II) ||
4970 (F &&
4971 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem))))
4972 Known.knownNot(fcNegZero);
4973
4974 break;
4975 }
4976 case Intrinsic::sin:
4977 case Intrinsic::cos: {
4978 // Return NaN on infinite inputs.
4979 KnownFPClass KnownSrc;
4980 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4981 KnownSrc, Q, Depth + 1);
4982 Known.knownNot(fcInf);
4983 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
4984 Known.knownNot(fcNan);
4985 break;
4986 }
4987 case Intrinsic::maxnum:
4988 case Intrinsic::minnum:
4989 case Intrinsic::minimum:
4990 case Intrinsic::maximum:
4991 case Intrinsic::minimumnum:
4992 case Intrinsic::maximumnum: {
4993 KnownFPClass KnownLHS, KnownRHS;
4994 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4995 KnownLHS, Q, Depth + 1);
4996 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4997 KnownRHS, Q, Depth + 1);
4998
4999 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
5000 Known = KnownLHS | KnownRHS;
5001
5002 // If either operand is not NaN, the result is not NaN.
5003 if (NeverNaN &&
5004 (IID == Intrinsic::minnum || IID == Intrinsic::maxnum ||
5005 IID == Intrinsic::minimumnum || IID == Intrinsic::maximumnum))
5006 Known.knownNot(fcNan);
5007
5008 if (IID == Intrinsic::maxnum || IID == Intrinsic::maximumnum) {
5009 // If at least one operand is known to be positive, the result must be
5010 // positive.
5011 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
5012 KnownLHS.isKnownNeverNaN()) ||
5013 (KnownRHS.cannotBeOrderedLessThanZero() &&
5014 KnownRHS.isKnownNeverNaN()))
5016 } else if (IID == Intrinsic::maximum) {
5017 // If at least one operand is known to be positive, the result must be
5018 // positive.
5019 if (KnownLHS.cannotBeOrderedLessThanZero() ||
5020 KnownRHS.cannotBeOrderedLessThanZero())
5022 } else if (IID == Intrinsic::minnum || IID == Intrinsic::minimumnum) {
5023 // If at least one operand is known to be negative, the result must be
5024 // negative.
5025 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
5026 KnownLHS.isKnownNeverNaN()) ||
5027 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
5028 KnownRHS.isKnownNeverNaN()))
5030 } else if (IID == Intrinsic::minimum) {
5031 // If at least one operand is known to be negative, the result must be
5032 // negative.
5033 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
5036 } else
5037 llvm_unreachable("unhandled intrinsic");
5038
5039 // Fixup zero handling if denormals could be returned as a zero.
5040 //
5041 // As there's no spec for denormal flushing, be conservative with the
5042 // treatment of denormals that could be flushed to zero. For older
5043 // subtargets on AMDGPU the min/max instructions would not flush the
5044 // output and return the original value.
5045 //
5046 if ((Known.KnownFPClasses & fcZero) != fcNone &&
5047 !Known.isKnownNeverSubnormal()) {
5048 const Function *Parent = II->getFunction();
5049 if (!Parent)
5050 break;
5051
5053 II->getType()->getScalarType()->getFltSemantics());
5054 if (Mode != DenormalMode::getIEEE())
5055 Known.KnownFPClasses |= fcZero;
5056 }
5057
5058 if (Known.isKnownNeverNaN()) {
5059 if (KnownLHS.SignBit && KnownRHS.SignBit &&
5060 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5061 if (*KnownLHS.SignBit)
5062 Known.signBitMustBeOne();
5063 else
5064 Known.signBitMustBeZero();
5065 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum ||
5066 IID == Intrinsic::maximumnum ||
5067 IID == Intrinsic::minimumnum) ||
5068 // FIXME: Should be using logical zero versions
5069 ((KnownLHS.isKnownNeverNegZero() ||
5070 KnownRHS.isKnownNeverPosZero()) &&
5071 (KnownLHS.isKnownNeverPosZero() ||
5072 KnownRHS.isKnownNeverNegZero()))) {
5073 // Don't take sign bit from NaN operands.
5074 if (!KnownLHS.isKnownNeverNaN())
5075 KnownLHS.SignBit = std::nullopt;
5076 if (!KnownRHS.isKnownNeverNaN())
5077 KnownRHS.SignBit = std::nullopt;
5078 if ((IID == Intrinsic::maximum || IID == Intrinsic::maximumnum ||
5079 IID == Intrinsic::maxnum) &&
5080 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5081 Known.signBitMustBeZero();
5082 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minimumnum ||
5083 IID == Intrinsic::minnum) &&
5084 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5085 Known.signBitMustBeOne();
5086 }
5087 }
5088 break;
5089 }
5090 case Intrinsic::canonicalize: {
5091 KnownFPClass KnownSrc;
5092 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5093 KnownSrc, Q, Depth + 1);
5094
5095 // This is essentially a stronger form of
5096 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5097 // actually have an IR canonicalization guarantee.
5098
5099 // Canonicalize may flush denormals to zero, so we have to consider the
5100 // denormal mode to preserve known-not-0 knowledge.
5101 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5102
5103 // Stronger version of propagateNaN
5104 // Canonicalize is guaranteed to quiet signaling nans.
5105 if (KnownSrc.isKnownNeverNaN())
5106 Known.knownNot(fcNan);
5107 else
5108 Known.knownNot(fcSNan);
5109
5110 const Function *F = II->getFunction();
5111 if (!F)
5112 break;
5113
5114 // If the parent function flushes denormals, the canonical output cannot
5115 // be a denormal.
5116 const fltSemantics &FPType =
5117 II->getType()->getScalarType()->getFltSemantics();
5118 DenormalMode DenormMode = F->getDenormalMode(FPType);
5119 if (DenormMode == DenormalMode::getIEEE()) {
5120 if (KnownSrc.isKnownNever(fcPosZero))
5121 Known.knownNot(fcPosZero);
5122 if (KnownSrc.isKnownNever(fcNegZero))
5123 Known.knownNot(fcNegZero);
5124 break;
5125 }
5126
5127 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5128 Known.knownNot(fcSubnormal);
5129
5130 if (DenormMode.Input == DenormalMode::PositiveZero ||
5131 (DenormMode.Output == DenormalMode::PositiveZero &&
5132 DenormMode.Input == DenormalMode::IEEE))
5133 Known.knownNot(fcNegZero);
5134
5135 break;
5136 }
5137 case Intrinsic::vector_reduce_fmax:
5138 case Intrinsic::vector_reduce_fmin:
5139 case Intrinsic::vector_reduce_fmaximum:
5140 case Intrinsic::vector_reduce_fminimum: {
5141 // reduce min/max will choose an element from one of the vector elements,
5142 // so we can infer and class information that is common to all elements.
5143 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5144 InterestedClasses, Q, Depth + 1);
5145 // Can only propagate sign if output is never NaN.
5146 if (!Known.isKnownNeverNaN())
5147 Known.SignBit.reset();
5148 break;
5149 }
5150 // reverse preserves all characteristics of the input vec's element.
5151 case Intrinsic::vector_reverse:
5152 Known = computeKnownFPClass(
5153 II->getArgOperand(0), DemandedElts.reverseBits(),
5154 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5155 break;
5156 case Intrinsic::trunc:
5157 case Intrinsic::floor:
5158 case Intrinsic::ceil:
5159 case Intrinsic::rint:
5160 case Intrinsic::nearbyint:
5161 case Intrinsic::round:
5162 case Intrinsic::roundeven: {
5163 KnownFPClass KnownSrc;
5164 FPClassTest InterestedSrcs = InterestedClasses;
5165 if (InterestedSrcs & fcPosFinite)
5166 InterestedSrcs |= fcPosFinite;
5167 if (InterestedSrcs & fcNegFinite)
5168 InterestedSrcs |= fcNegFinite;
5169 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5170 KnownSrc, Q, Depth + 1);
5171
5172 // Integer results cannot be subnormal.
5173 Known.knownNot(fcSubnormal);
5174
5175 Known.propagateNaN(KnownSrc, true);
5176
5177 // Pass through infinities, except PPC_FP128 is a special case for
5178 // intrinsics other than trunc.
5179 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5180 if (KnownSrc.isKnownNeverPosInfinity())
5181 Known.knownNot(fcPosInf);
5182 if (KnownSrc.isKnownNeverNegInfinity())
5183 Known.knownNot(fcNegInf);
5184 }
5185
5186 // Negative round ups to 0 produce -0
5187 if (KnownSrc.isKnownNever(fcPosFinite))
5188 Known.knownNot(fcPosFinite);
5189 if (KnownSrc.isKnownNever(fcNegFinite))
5190 Known.knownNot(fcNegFinite);
5191
5192 break;
5193 }
5194 case Intrinsic::exp:
5195 case Intrinsic::exp2:
5196 case Intrinsic::exp10: {
5197 Known.knownNot(fcNegative);
5198 if ((InterestedClasses & fcNan) == fcNone)
5199 break;
5200
5201 KnownFPClass KnownSrc;
5202 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5203 KnownSrc, Q, Depth + 1);
5204 if (KnownSrc.isKnownNeverNaN()) {
5205 Known.knownNot(fcNan);
5206 Known.signBitMustBeZero();
5207 }
5208
5209 break;
5210 }
5211 case Intrinsic::fptrunc_round: {
5212 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5213 Q, Depth);
5214 break;
5215 }
5216 case Intrinsic::log:
5217 case Intrinsic::log10:
5218 case Intrinsic::log2:
5219 case Intrinsic::experimental_constrained_log:
5220 case Intrinsic::experimental_constrained_log10:
5221 case Intrinsic::experimental_constrained_log2: {
5222 // log(+inf) -> +inf
5223 // log([+-]0.0) -> -inf
5224 // log(-inf) -> nan
5225 // log(-x) -> nan
5226 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5227 break;
5228
5229 FPClassTest InterestedSrcs = InterestedClasses;
5230 if ((InterestedClasses & fcNegInf) != fcNone)
5231 InterestedSrcs |= fcZero | fcSubnormal;
5232 if ((InterestedClasses & fcNan) != fcNone)
5233 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5234
5235 KnownFPClass KnownSrc;
5236 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5237 KnownSrc, Q, Depth + 1);
5238
5239 if (KnownSrc.isKnownNeverPosInfinity())
5240 Known.knownNot(fcPosInf);
5241
5242 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5243 Known.knownNot(fcNan);
5244
5245 const Function *F = II->getFunction();
5246
5247 if (!F)
5248 break;
5249
5250 const fltSemantics &FltSem =
5251 II->getType()->getScalarType()->getFltSemantics();
5252 DenormalMode Mode = F->getDenormalMode(FltSem);
5253
5254 if (KnownSrc.isKnownNeverLogicalZero(Mode))
5255 Known.knownNot(fcNegInf);
5256
5257 break;
5258 }
5259 case Intrinsic::powi: {
5260 if ((InterestedClasses & fcNegative) == fcNone)
5261 break;
5262
5263 const Value *Exp = II->getArgOperand(1);
5264 Type *ExpTy = Exp->getType();
5265 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5266 KnownBits ExponentKnownBits(BitWidth);
5267 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5268 ExponentKnownBits, Q, Depth + 1);
5269
5270 if (ExponentKnownBits.Zero[0]) { // Is even
5271 Known.knownNot(fcNegative);
5272 break;
5273 }
5274
5275 // Given that exp is an integer, here are the
5276 // ways that pow can return a negative value:
5277 //
5278 // pow(-x, exp) --> negative if exp is odd and x is negative.
5279 // pow(-0, exp) --> -inf if exp is negative odd.
5280 // pow(-0, exp) --> -0 if exp is positive odd.
5281 // pow(-inf, exp) --> -0 if exp is negative odd.
5282 // pow(-inf, exp) --> -inf if exp is positive odd.
5283 KnownFPClass KnownSrc;
5284 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5285 KnownSrc, Q, Depth + 1);
5286 if (KnownSrc.isKnownNever(fcNegative))
5287 Known.knownNot(fcNegative);
5288 break;
5289 }
5290 case Intrinsic::ldexp: {
5291 KnownFPClass KnownSrc;
5292 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5293 KnownSrc, Q, Depth + 1);
5294 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5295
5296 // Sign is preserved, but underflows may produce zeroes.
5297 if (KnownSrc.isKnownNever(fcNegative))
5298 Known.knownNot(fcNegative);
5299 else if (KnownSrc.cannotBeOrderedLessThanZero())
5301
5302 if (KnownSrc.isKnownNever(fcPositive))
5303 Known.knownNot(fcPositive);
5304 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5306
5307 // Can refine inf/zero handling based on the exponent operand.
5308 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5309 if ((InterestedClasses & ExpInfoMask) == fcNone)
5310 break;
5311 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5312 break;
5313
5314 const fltSemantics &Flt =
5315 II->getType()->getScalarType()->getFltSemantics();
5316 unsigned Precision = APFloat::semanticsPrecision(Flt);
5317 const Value *ExpArg = II->getArgOperand(1);
5319 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5320
5321 const int MantissaBits = Precision - 1;
5322 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5323 Known.knownNot(fcSubnormal);
5324
5325 const Function *F = II->getFunction();
5326 const APInt *ConstVal = ExpRange.getSingleElement();
5327 const fltSemantics &FltSem =
5328 II->getType()->getScalarType()->getFltSemantics();
5329 if (ConstVal && ConstVal->isZero()) {
5330 // ldexp(x, 0) -> x, so propagate everything.
5331 Known.propagateCanonicalizingSrc(KnownSrc, F->getDenormalMode(FltSem));
5332 } else if (ExpRange.isAllNegative()) {
5333 // If we know the power is <= 0, can't introduce inf
5334 if (KnownSrc.isKnownNeverPosInfinity())
5335 Known.knownNot(fcPosInf);
5336 if (KnownSrc.isKnownNeverNegInfinity())
5337 Known.knownNot(fcNegInf);
5338 } else if (ExpRange.isAllNonNegative()) {
5339 // If we know the power is >= 0, can't introduce subnormal or zero
5340 if (KnownSrc.isKnownNeverPosSubnormal())
5341 Known.knownNot(fcPosSubnormal);
5342 if (KnownSrc.isKnownNeverNegSubnormal())
5343 Known.knownNot(fcNegSubnormal);
5344 if (F &&
5345 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5346 Known.knownNot(fcPosZero);
5347 if (F &&
5348 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5349 Known.knownNot(fcNegZero);
5350 }
5351
5352 break;
5353 }
5354 case Intrinsic::arithmetic_fence: {
5355 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5356 Known, Q, Depth + 1);
5357 break;
5358 }
5359 case Intrinsic::experimental_constrained_sitofp:
5360 case Intrinsic::experimental_constrained_uitofp:
5361 // Cannot produce nan
5362 Known.knownNot(fcNan);
5363
5364 // sitofp and uitofp turn into +0.0 for zero.
5365 Known.knownNot(fcNegZero);
5366
5367 // Integers cannot be subnormal
5368 Known.knownNot(fcSubnormal);
5369
5370 if (IID == Intrinsic::experimental_constrained_uitofp)
5371 Known.signBitMustBeZero();
5372
5373 // TODO: Copy inf handling from instructions
5374 break;
5375 default:
5376 break;
5377 }
5378
5379 break;
5380 }
5381 case Instruction::FAdd:
5382 case Instruction::FSub: {
5383 KnownFPClass KnownLHS, KnownRHS;
5384 bool WantNegative =
5385 Op->getOpcode() == Instruction::FAdd &&
5386 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5387 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5388 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5389
5390 if (!WantNaN && !WantNegative && !WantNegZero)
5391 break;
5392
5393 FPClassTest InterestedSrcs = InterestedClasses;
5394 if (WantNegative)
5395 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5396 if (InterestedClasses & fcNan)
5397 InterestedSrcs |= fcInf;
5398 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5399 KnownRHS, Q, Depth + 1);
5400
5401 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5402 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5403 WantNegZero || Opc == Instruction::FSub) {
5404
5405 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5406 // there's no point.
5407 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5408 KnownLHS, Q, Depth + 1);
5409 // Adding positive and negative infinity produces NaN.
5410 // TODO: Check sign of infinities.
5411 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5412 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5413 Known.knownNot(fcNan);
5414
5415 // FIXME: Context function should always be passed in separately
5416 const Function *F = cast<Instruction>(Op)->getFunction();
5417
5418 if (Op->getOpcode() == Instruction::FAdd) {
5419 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5420 KnownRHS.cannotBeOrderedLessThanZero())
5422 if (!F)
5423 break;
5424
5425 const fltSemantics &FltSem =
5426 Op->getType()->getScalarType()->getFltSemantics();
5427 DenormalMode Mode = F->getDenormalMode(FltSem);
5428
5429 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5430 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5431 KnownRHS.isKnownNeverLogicalNegZero(Mode)) &&
5432 // Make sure output negative denormal can't flush to -0
5433 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5434 Known.knownNot(fcNegZero);
5435 } else {
5436 if (!F)
5437 break;
5438
5439 const fltSemantics &FltSem =
5440 Op->getType()->getScalarType()->getFltSemantics();
5441 DenormalMode Mode = F->getDenormalMode(FltSem);
5442
5443 // Only fsub -0, +0 can return -0
5444 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5445 KnownRHS.isKnownNeverLogicalPosZero(Mode)) &&
5446 // Make sure output negative denormal can't flush to -0
5447 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5448 Known.knownNot(fcNegZero);
5449 }
5450 }
5451
5452 break;
5453 }
5454 case Instruction::FMul: {
5455 // X * X is always non-negative or a NaN.
5456 if (Op->getOperand(0) == Op->getOperand(1))
5457 Known.knownNot(fcNegative);
5458
5459 if ((InterestedClasses & fcNan) != fcNan)
5460 break;
5461
5462 // fcSubnormal is only needed in case of DAZ.
5463 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5464
5465 KnownFPClass KnownLHS, KnownRHS;
5466 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5467 Q, Depth + 1);
5468 if (!KnownRHS.isKnownNeverNaN())
5469 break;
5470
5471 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5472 Q, Depth + 1);
5473 if (!KnownLHS.isKnownNeverNaN())
5474 break;
5475
5476 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5477 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5478 Known.signBitMustBeZero();
5479 else
5480 Known.signBitMustBeOne();
5481 }
5482
5483 // If 0 * +/-inf produces NaN.
5484 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5485 Known.knownNot(fcNan);
5486 break;
5487 }
5488
5489 const Function *F = cast<Instruction>(Op)->getFunction();
5490 if (!F)
5491 break;
5492
5493 Type *OpTy = Op->getType()->getScalarType();
5494 const fltSemantics &FltSem = OpTy->getFltSemantics();
5495 DenormalMode Mode = F->getDenormalMode(FltSem);
5496
5497 if ((KnownRHS.isKnownNeverInfinity() ||
5498 KnownLHS.isKnownNeverLogicalZero(Mode)) &&
5499 (KnownLHS.isKnownNeverInfinity() ||
5500 KnownRHS.isKnownNeverLogicalZero(Mode)))
5501 Known.knownNot(fcNan);
5502
5503 break;
5504 }
5505 case Instruction::FDiv:
5506 case Instruction::FRem: {
5507 if (Op->getOperand(0) == Op->getOperand(1)) {
5508 // TODO: Could filter out snan if we inspect the operand
5509 if (Op->getOpcode() == Instruction::FDiv) {
5510 // X / X is always exactly 1.0 or a NaN.
5512 } else {
5513 // X % X is always exactly [+-]0.0 or a NaN.
5514 Known.KnownFPClasses = fcNan | fcZero;
5515 }
5516
5517 break;
5518 }
5519
5520 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5521 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5522 const bool WantPositive =
5523 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5524 if (!WantNan && !WantNegative && !WantPositive)
5525 break;
5526
5527 KnownFPClass KnownLHS, KnownRHS;
5528
5529 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5530 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5531 Depth + 1);
5532
5533 bool KnowSomethingUseful =
5534 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5535
5536 if (KnowSomethingUseful || WantPositive) {
5537 const FPClassTest InterestedLHS =
5538 WantPositive ? fcAllFlags
5540
5541 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5542 InterestedClasses & InterestedLHS, KnownLHS, Q,
5543 Depth + 1);
5544 }
5545
5546 const Function *F = cast<Instruction>(Op)->getFunction();
5547 const fltSemantics &FltSem =
5548 Op->getType()->getScalarType()->getFltSemantics();
5549
5550 if (Op->getOpcode() == Instruction::FDiv) {
5551 // Only 0/0, Inf/Inf produce NaN.
5552 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5553 (KnownLHS.isKnownNeverInfinity() ||
5554 KnownRHS.isKnownNeverInfinity()) &&
5555 ((F &&
5556 KnownLHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) ||
5557 (F &&
5558 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))))) {
5559 Known.knownNot(fcNan);
5560 }
5561
5562 // X / -0.0 is -Inf (or NaN).
5563 // +X / +X is +X
5564 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5565 Known.knownNot(fcNegative);
5566 } else {
5567 // Inf REM x and x REM 0 produce NaN.
5568 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5569 KnownLHS.isKnownNeverInfinity() && F &&
5570 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5571 Known.knownNot(fcNan);
5572 }
5573
5574 // The sign for frem is the same as the first operand.
5575 if (KnownLHS.cannotBeOrderedLessThanZero())
5577 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5579
5580 // See if we can be more aggressive about the sign of 0.
5581 if (KnownLHS.isKnownNever(fcNegative))
5582 Known.knownNot(fcNegative);
5583 if (KnownLHS.isKnownNever(fcPositive))
5584 Known.knownNot(fcPositive);
5585 }
5586
5587 break;
5588 }
5589 case Instruction::FPExt: {
5590 // Infinity, nan and zero propagate from source.
5591 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5592 Known, Q, Depth + 1);
5593
5594 const fltSemantics &DstTy =
5595 Op->getType()->getScalarType()->getFltSemantics();
5596 const fltSemantics &SrcTy =
5597 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5598
5599 // All subnormal inputs should be in the normal range in the result type.
5600 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5601 if (Known.KnownFPClasses & fcPosSubnormal)
5602 Known.KnownFPClasses |= fcPosNormal;
5603 if (Known.KnownFPClasses & fcNegSubnormal)
5604 Known.KnownFPClasses |= fcNegNormal;
5605 Known.knownNot(fcSubnormal);
5606 }
5607
5608 // Sign bit of a nan isn't guaranteed.
5609 if (!Known.isKnownNeverNaN())
5610 Known.SignBit = std::nullopt;
5611 break;
5612 }
5613 case Instruction::FPTrunc: {
5614 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5615 Depth);
5616 break;
5617 }
5618 case Instruction::SIToFP:
5619 case Instruction::UIToFP: {
5620 // Cannot produce nan
5621 Known.knownNot(fcNan);
5622
5623 // Integers cannot be subnormal
5624 Known.knownNot(fcSubnormal);
5625
5626 // sitofp and uitofp turn into +0.0 for zero.
5627 Known.knownNot(fcNegZero);
5628 if (Op->getOpcode() == Instruction::UIToFP)
5629 Known.signBitMustBeZero();
5630
5631 if (InterestedClasses & fcInf) {
5632 // Get width of largest magnitude integer (remove a bit if signed).
5633 // This still works for a signed minimum value because the largest FP
5634 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5635 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5636 if (Op->getOpcode() == Instruction::SIToFP)
5637 --IntSize;
5638
5639 // If the exponent of the largest finite FP value can hold the largest
5640 // integer, the result of the cast must be finite.
5641 Type *FPTy = Op->getType()->getScalarType();
5642 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5643 Known.knownNot(fcInf);
5644 }
5645
5646 break;
5647 }
5648 case Instruction::ExtractElement: {
5649 // Look through extract element. If the index is non-constant or
5650 // out-of-range demand all elements, otherwise just the extracted element.
5651 const Value *Vec = Op->getOperand(0);
5652
5653 APInt DemandedVecElts;
5654 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5655 unsigned NumElts = VecTy->getNumElements();
5656 DemandedVecElts = APInt::getAllOnes(NumElts);
5657 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5658 if (CIdx && CIdx->getValue().ult(NumElts))
5659 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5660 } else {
5661 DemandedVecElts = APInt(1, 1);
5662 }
5663
5664 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5665 Q, Depth + 1);
5666 }
5667 case Instruction::InsertElement: {
5668 if (isa<ScalableVectorType>(Op->getType()))
5669 return;
5670
5671 const Value *Vec = Op->getOperand(0);
5672 const Value *Elt = Op->getOperand(1);
5673 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5674 unsigned NumElts = DemandedElts.getBitWidth();
5675 APInt DemandedVecElts = DemandedElts;
5676 bool NeedsElt = true;
5677 // If we know the index we are inserting to, clear it from Vec check.
5678 if (CIdx && CIdx->getValue().ult(NumElts)) {
5679 DemandedVecElts.clearBit(CIdx->getZExtValue());
5680 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5681 }
5682
5683 // Do we demand the inserted element?
5684 if (NeedsElt) {
5685 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5686 // If we don't know any bits, early out.
5687 if (Known.isUnknown())
5688 break;
5689 } else {
5690 Known.KnownFPClasses = fcNone;
5691 }
5692
5693 // Do we need anymore elements from Vec?
5694 if (!DemandedVecElts.isZero()) {
5695 KnownFPClass Known2;
5696 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5697 Depth + 1);
5698 Known |= Known2;
5699 }
5700
5701 break;
5702 }
5703 case Instruction::ShuffleVector: {
5704 // For undef elements, we don't know anything about the common state of
5705 // the shuffle result.
5706 APInt DemandedLHS, DemandedRHS;
5707 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5708 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5709 return;
5710
5711 if (!!DemandedLHS) {
5712 const Value *LHS = Shuf->getOperand(0);
5713 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5714 Depth + 1);
5715
5716 // If we don't know any bits, early out.
5717 if (Known.isUnknown())
5718 break;
5719 } else {
5720 Known.KnownFPClasses = fcNone;
5721 }
5722
5723 if (!!DemandedRHS) {
5724 KnownFPClass Known2;
5725 const Value *RHS = Shuf->getOperand(1);
5726 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5727 Depth + 1);
5728 Known |= Known2;
5729 }
5730
5731 break;
5732 }
5733 case Instruction::ExtractValue: {
5734 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5735 ArrayRef<unsigned> Indices = Extract->getIndices();
5736 const Value *Src = Extract->getAggregateOperand();
5737 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5738 Indices[0] == 0) {
5739 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5740 switch (II->getIntrinsicID()) {
5741 case Intrinsic::frexp: {
5742 Known.knownNot(fcSubnormal);
5743
5744 KnownFPClass KnownSrc;
5745 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5746 InterestedClasses, KnownSrc, Q, Depth + 1);
5747
5748 const Function *F = cast<Instruction>(Op)->getFunction();
5749 const fltSemantics &FltSem =
5750 Op->getType()->getScalarType()->getFltSemantics();
5751
5752 if (KnownSrc.isKnownNever(fcNegative))
5753 Known.knownNot(fcNegative);
5754 else {
5755 if (F &&
5756 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5757 Known.knownNot(fcNegZero);
5758 if (KnownSrc.isKnownNever(fcNegInf))
5759 Known.knownNot(fcNegInf);
5760 }
5761
5762 if (KnownSrc.isKnownNever(fcPositive))
5763 Known.knownNot(fcPositive);
5764 else {
5765 if (F &&
5766 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5767 Known.knownNot(fcPosZero);
5768 if (KnownSrc.isKnownNever(fcPosInf))
5769 Known.knownNot(fcPosInf);
5770 }
5771
5772 Known.propagateNaN(KnownSrc);
5773 return;
5774 }
5775 default:
5776 break;
5777 }
5778 }
5779 }
5780
5781 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
5782 Depth + 1);
5783 break;
5784 }
5785 case Instruction::PHI: {
5786 const PHINode *P = cast<PHINode>(Op);
5787 // Unreachable blocks may have zero-operand PHI nodes.
5788 if (P->getNumIncomingValues() == 0)
5789 break;
5790
5791 // Otherwise take the unions of the known bit sets of the operands,
5792 // taking conservative care to avoid excessive recursion.
5793 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5794
5795 if (Depth < PhiRecursionLimit) {
5796 // Skip if every incoming value references to ourself.
5797 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5798 break;
5799
5800 bool First = true;
5801
5802 for (const Use &U : P->operands()) {
5803 Value *IncValue;
5804 Instruction *CxtI;
5805 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
5806 // Skip direct self references.
5807 if (IncValue == P)
5808 continue;
5809
5810 KnownFPClass KnownSrc;
5811 // Recurse, but cap the recursion to two levels, because we don't want
5812 // to waste time spinning around in loops. We need at least depth 2 to
5813 // detect known sign bits.
5814 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
5816 PhiRecursionLimit);
5817
5818 if (First) {
5819 Known = KnownSrc;
5820 First = false;
5821 } else {
5822 Known |= KnownSrc;
5823 }
5824
5825 if (Known.KnownFPClasses == fcAllFlags)
5826 break;
5827 }
5828 }
5829
5830 break;
5831 }
5832 case Instruction::BitCast: {
5833 const Value *Src;
5834 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
5835 !Src->getType()->isIntOrIntVectorTy())
5836 break;
5837
5838 const Type *Ty = Op->getType()->getScalarType();
5839 KnownBits Bits(Ty->getScalarSizeInBits());
5840 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
5841
5842 // Transfer information from the sign bit.
5843 if (Bits.isNonNegative())
5844 Known.signBitMustBeZero();
5845 else if (Bits.isNegative())
5846 Known.signBitMustBeOne();
5847
5848 if (Ty->isIEEELikeFPTy()) {
5849 // IEEE floats are NaN when all bits of the exponent plus at least one of
5850 // the fraction bits are 1. This means:
5851 // - If we assume unknown bits are 0 and the value is NaN, it will
5852 // always be NaN
5853 // - If we assume unknown bits are 1 and the value is not NaN, it can
5854 // never be NaN
5855 // Note: They do not hold for x86_fp80 format.
5856 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
5857 Known.KnownFPClasses = fcNan;
5858 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
5859 Known.knownNot(fcNan);
5860
5861 // Build KnownBits representing Inf and check if it must be equal or
5862 // unequal to this value.
5863 auto InfKB = KnownBits::makeConstant(
5864 APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());
5865 InfKB.Zero.clearSignBit();
5866 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
5867 assert(!InfResult.value());
5868 Known.knownNot(fcInf);
5869 } else if (Bits == InfKB) {
5870 Known.KnownFPClasses = fcInf;
5871 }
5872
5873 // Build KnownBits representing Zero and check if it must be equal or
5874 // unequal to this value.
5875 auto ZeroKB = KnownBits::makeConstant(
5876 APFloat::getZero(Ty->getFltSemantics()).bitcastToAPInt());
5877 ZeroKB.Zero.clearSignBit();
5878 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
5879 assert(!ZeroResult.value());
5880 Known.knownNot(fcZero);
5881 } else if (Bits == ZeroKB) {
5882 Known.KnownFPClasses = fcZero;
5883 }
5884 }
5885
5886 break;
5887 }
5888 default:
5889 break;
5890 }
5891}
5892
5894 const APInt &DemandedElts,
5895 FPClassTest InterestedClasses,
5896 const SimplifyQuery &SQ,
5897 unsigned Depth) {
5898 KnownFPClass KnownClasses;
5899 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
5900 Depth);
5901 return KnownClasses;
5902}
5903
5905 FPClassTest InterestedClasses,
5906 const SimplifyQuery &SQ,
5907 unsigned Depth) {
5908 KnownFPClass Known;
5909 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
5910 return Known;
5911}
5912
5914 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
5915 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
5916 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
5917 return computeKnownFPClass(V, InterestedClasses,
5918 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
5919 Depth);
5920}
5921
5923llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5924 FastMathFlags FMF, FPClassTest InterestedClasses,
5925 const SimplifyQuery &SQ, unsigned Depth) {
5926 if (FMF.noNaNs())
5927 InterestedClasses &= ~fcNan;
5928 if (FMF.noInfs())
5929 InterestedClasses &= ~fcInf;
5930
5931 KnownFPClass Result =
5932 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
5933
5934 if (FMF.noNaNs())
5935 Result.KnownFPClasses &= ~fcNan;
5936 if (FMF.noInfs())
5937 Result.KnownFPClasses &= ~fcInf;
5938 return Result;
5939}
5940
5942 FPClassTest InterestedClasses,
5943 const SimplifyQuery &SQ,
5944 unsigned Depth) {
5945 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5946 APInt DemandedElts =
5947 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5948 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
5949 Depth);
5950}
5951
5953 unsigned Depth) {
5955 return Known.isKnownNeverNegZero();
5956}
5957
5964
5966 unsigned Depth) {
5968 return Known.isKnownNeverInfinity();
5969}
5970
5971/// Return true if the floating-point value can never contain a NaN or infinity.
5973 unsigned Depth) {
5975 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
5976}
5977
5978/// Return true if the floating-point scalar value is not a NaN or if the
5979/// floating-point vector value has no NaN elements. Return false if a value
5980/// could ever be NaN.
5982 unsigned Depth) {
5984 return Known.isKnownNeverNaN();
5985}
5986
5987/// Return false if we can prove that the specified FP value's sign bit is 0.
5988/// Return true if we can prove that the specified FP value's sign bit is 1.
5989/// Otherwise return std::nullopt.
5990std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
5991 const SimplifyQuery &SQ,
5992 unsigned Depth) {
5994 return Known.SignBit;
5995}
5996
5998 auto *User = cast<Instruction>(U.getUser());
5999 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6000 if (FPOp->hasNoSignedZeros())
6001 return true;
6002 }
6003
6004 switch (User->getOpcode()) {
6005 case Instruction::FPToSI:
6006 case Instruction::FPToUI:
6007 return true;
6008 case Instruction::FCmp:
6009 // fcmp treats both positive and negative zero as equal.
6010 return true;
6011 case Instruction::Call:
6012 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6013 switch (II->getIntrinsicID()) {
6014 case Intrinsic::fabs:
6015 return true;
6016 case Intrinsic::copysign:
6017 return U.getOperandNo() == 0;
6018 case Intrinsic::is_fpclass:
6019 case Intrinsic::vp_is_fpclass: {
6020 auto Test =
6021 static_cast<FPClassTest>(
6022 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6025 }
6026 default:
6027 return false;
6028 }
6029 }
6030 return false;
6031 default:
6032 return false;
6033 }
6034}
6035
6037 auto *User = cast<Instruction>(U.getUser());
6038 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6039 if (FPOp->hasNoNaNs())
6040 return true;
6041 }
6042
6043 switch (User->getOpcode()) {
6044 case Instruction::FPToSI:
6045 case Instruction::FPToUI:
6046 return true;
6047 // Proper FP math operations ignore the sign bit of NaN.
6048 case Instruction::FAdd:
6049 case Instruction::FSub:
6050 case Instruction::FMul:
6051 case Instruction::FDiv:
6052 case Instruction::FRem:
6053 case Instruction::FPTrunc:
6054 case Instruction::FPExt:
6055 case Instruction::FCmp:
6056 return true;
6057 // Bitwise FP operations should preserve the sign bit of NaN.
6058 case Instruction::FNeg:
6059 case Instruction::Select:
6060 case Instruction::PHI:
6061 return false;
6062 case Instruction::Ret:
6063 return User->getFunction()->getAttributes().getRetNoFPClass() &
6065 case Instruction::Call:
6066 case Instruction::Invoke: {
6067 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6068 switch (II->getIntrinsicID()) {
6069 case Intrinsic::fabs:
6070 return true;
6071 case Intrinsic::copysign:
6072 return U.getOperandNo() == 0;
6073 // Other proper FP math intrinsics ignore the sign bit of NaN.
6074 case Intrinsic::maxnum:
6075 case Intrinsic::minnum:
6076 case Intrinsic::maximum:
6077 case Intrinsic::minimum:
6078 case Intrinsic::maximumnum:
6079 case Intrinsic::minimumnum:
6080 case Intrinsic::canonicalize:
6081 case Intrinsic::fma:
6082 case Intrinsic::fmuladd:
6083 case Intrinsic::sqrt:
6084 case Intrinsic::pow:
6085 case Intrinsic::powi:
6086 case Intrinsic::fptoui_sat:
6087 case Intrinsic::fptosi_sat:
6088 case Intrinsic::is_fpclass:
6089 case Intrinsic::vp_is_fpclass:
6090 return true;
6091 default:
6092 return false;
6093 }
6094 }
6095
6096 FPClassTest NoFPClass =
6097 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6098 return NoFPClass & FPClassTest::fcNan;
6099 }
6100 default:
6101 return false;
6102 }
6103}
6104
6106
6107 // All byte-wide stores are splatable, even of arbitrary variables.
6108 if (V->getType()->isIntegerTy(8))
6109 return V;
6110
6111 LLVMContext &Ctx = V->getContext();
6112
6113 // Undef don't care.
6114 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6115 if (isa<UndefValue>(V))
6116 return UndefInt8;
6117
6118 // Return poison for zero-sized type.
6119 if (DL.getTypeStoreSize(V->getType()).isZero())
6120 return PoisonValue::get(Type::getInt8Ty(Ctx));
6121
6123 if (!C) {
6124 // Conceptually, we could handle things like:
6125 // %a = zext i8 %X to i16
6126 // %b = shl i16 %a, 8
6127 // %c = or i16 %a, %b
6128 // but until there is an example that actually needs this, it doesn't seem
6129 // worth worrying about.
6130 return nullptr;
6131 }
6132
6133 // Handle 'null' ConstantArrayZero etc.
6134 if (C->isNullValue())
6136
6137 // Constant floating-point values can be handled as integer values if the
6138 // corresponding integer value is "byteable". An important case is 0.0.
6139 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6140 Type *Ty = nullptr;
6141 if (CFP->getType()->isHalfTy())
6142 Ty = Type::getInt16Ty(Ctx);
6143 else if (CFP->getType()->isFloatTy())
6144 Ty = Type::getInt32Ty(Ctx);
6145 else if (CFP->getType()->isDoubleTy())
6146 Ty = Type::getInt64Ty(Ctx);
6147 // Don't handle long double formats, which have strange constraints.
6148 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6149 : nullptr;
6150 }
6151
6152 // We can handle constant integers that are multiple of 8 bits.
6153 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6154 if (CI->getBitWidth() % 8 == 0) {
6155 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6156 if (!CI->getValue().isSplat(8))
6157 return nullptr;
6158 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6159 }
6160 }
6161
6162 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6163 if (CE->getOpcode() == Instruction::IntToPtr) {
6164 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6165 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6167 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6168 return isBytewiseValue(Op, DL);
6169 }
6170 }
6171 }
6172
6173 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6174 if (LHS == RHS)
6175 return LHS;
6176 if (!LHS || !RHS)
6177 return nullptr;
6178 if (LHS == UndefInt8)
6179 return RHS;
6180 if (RHS == UndefInt8)
6181 return LHS;
6182 return nullptr;
6183 };
6184
6186 Value *Val = UndefInt8;
6187 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6188 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6189 return nullptr;
6190 return Val;
6191 }
6192
6194 Value *Val = UndefInt8;
6195 for (Value *Op : C->operands())
6196 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6197 return nullptr;
6198 return Val;
6199 }
6200
6201 // Don't try to handle the handful of other constants.
6202 return nullptr;
6203}
6204
6205// This is the recursive version of BuildSubAggregate. It takes a few different
6206// arguments. Idxs is the index within the nested struct From that we are
6207// looking at now (which is of type IndexedType). IdxSkip is the number of
6208// indices from Idxs that should be left out when inserting into the resulting
6209// struct. To is the result struct built so far, new insertvalue instructions
6210// build on that.
6211static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6213 unsigned IdxSkip,
6214 BasicBlock::iterator InsertBefore) {
6215 StructType *STy = dyn_cast<StructType>(IndexedType);
6216 if (STy) {
6217 // Save the original To argument so we can modify it
6218 Value *OrigTo = To;
6219 // General case, the type indexed by Idxs is a struct
6220 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6221 // Process each struct element recursively
6222 Idxs.push_back(i);
6223 Value *PrevTo = To;
6224 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6225 InsertBefore);
6226 Idxs.pop_back();
6227 if (!To) {
6228 // Couldn't find any inserted value for this index? Cleanup
6229 while (PrevTo != OrigTo) {
6231 PrevTo = Del->getAggregateOperand();
6232 Del->eraseFromParent();
6233 }
6234 // Stop processing elements
6235 break;
6236 }
6237 }
6238 // If we successfully found a value for each of our subaggregates
6239 if (To)
6240 return To;
6241 }
6242 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6243 // the struct's elements had a value that was inserted directly. In the latter
6244 // case, perhaps we can't determine each of the subelements individually, but
6245 // we might be able to find the complete struct somewhere.
6246
6247 // Find the value that is at that particular spot
6248 Value *V = FindInsertedValue(From, Idxs);
6249
6250 if (!V)
6251 return nullptr;
6252
6253 // Insert the value in the new (sub) aggregate
6254 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6255 InsertBefore);
6256}
6257
6258// This helper takes a nested struct and extracts a part of it (which is again a
6259// struct) into a new value. For example, given the struct:
6260// { a, { b, { c, d }, e } }
6261// and the indices "1, 1" this returns
6262// { c, d }.
6263//
6264// It does this by inserting an insertvalue for each element in the resulting
6265// struct, as opposed to just inserting a single struct. This will only work if
6266// each of the elements of the substruct are known (ie, inserted into From by an
6267// insertvalue instruction somewhere).
6268//
6269// All inserted insertvalue instructions are inserted before InsertBefore
6271 BasicBlock::iterator InsertBefore) {
6272 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6273 idx_range);
6274 Value *To = PoisonValue::get(IndexedType);
6275 SmallVector<unsigned, 10> Idxs(idx_range);
6276 unsigned IdxSkip = Idxs.size();
6277
6278 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6279}
6280
6281/// Given an aggregate and a sequence of indices, see if the scalar value
6282/// indexed is already around as a register, for example if it was inserted
6283/// directly into the aggregate.
6284///
6285/// If InsertBefore is not null, this function will duplicate (modified)
6286/// insertvalues when a part of a nested struct is extracted.
6287Value *
6289 std::optional<BasicBlock::iterator> InsertBefore) {
6290 // Nothing to index? Just return V then (this is useful at the end of our
6291 // recursion).
6292 if (idx_range.empty())
6293 return V;
6294 // We have indices, so V should have an indexable type.
6295 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6296 "Not looking at a struct or array?");
6297 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6298 "Invalid indices for type?");
6299
6300 if (Constant *C = dyn_cast<Constant>(V)) {
6301 C = C->getAggregateElement(idx_range[0]);
6302 if (!C) return nullptr;
6303 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6304 }
6305
6307 // Loop the indices for the insertvalue instruction in parallel with the
6308 // requested indices
6309 const unsigned *req_idx = idx_range.begin();
6310 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6311 i != e; ++i, ++req_idx) {
6312 if (req_idx == idx_range.end()) {
6313 // We can't handle this without inserting insertvalues
6314 if (!InsertBefore)
6315 return nullptr;
6316
6317 // The requested index identifies a part of a nested aggregate. Handle
6318 // this specially. For example,
6319 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6320 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6321 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6322 // This can be changed into
6323 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6324 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6325 // which allows the unused 0,0 element from the nested struct to be
6326 // removed.
6327 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6328 *InsertBefore);
6329 }
6330
6331 // This insert value inserts something else than what we are looking for.
6332 // See if the (aggregate) value inserted into has the value we are
6333 // looking for, then.
6334 if (*req_idx != *i)
6335 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6336 InsertBefore);
6337 }
6338 // If we end up here, the indices of the insertvalue match with those
6339 // requested (though possibly only partially). Now we recursively look at
6340 // the inserted value, passing any remaining indices.
6341 return FindInsertedValue(I->getInsertedValueOperand(),
6342 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6343 }
6344
6346 // If we're extracting a value from an aggregate that was extracted from
6347 // something else, we can extract from that something else directly instead.
6348 // However, we will need to chain I's indices with the requested indices.
6349
6350 // Calculate the number of indices required
6351 unsigned size = I->getNumIndices() + idx_range.size();
6352 // Allocate some space to put the new indices in
6354 Idxs.reserve(size);
6355 // Add indices from the extract value instruction
6356 Idxs.append(I->idx_begin(), I->idx_end());
6357
6358 // Add requested indices
6359 Idxs.append(idx_range.begin(), idx_range.end());
6360
6361 assert(Idxs.size() == size
6362 && "Number of indices added not correct?");
6363
6364 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6365 }
6366 // Otherwise, we don't know (such as, extracting from a function return value
6367 // or load instruction)
6368 return nullptr;
6369}
6370
6371// If V refers to an initialized global constant, set Slice either to
6372// its initializer if the size of its elements equals ElementSize, or,
6373// for ElementSize == 8, to its representation as an array of unsiged
6374// char. Return true on success.
6375// Offset is in the unit "nr of ElementSize sized elements".
6378 unsigned ElementSize, uint64_t Offset) {
6379 assert(V && "V should not be null.");
6380 assert((ElementSize % 8) == 0 &&
6381 "ElementSize expected to be a multiple of the size of a byte.");
6382 unsigned ElementSizeInBytes = ElementSize / 8;
6383
6384 // Drill down into the pointer expression V, ignoring any intervening
6385 // casts, and determine the identity of the object it references along
6386 // with the cumulative byte offset into it.
6387 const GlobalVariable *GV =
6389 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6390 // Fail if V is not based on constant global object.
6391 return false;
6392
6393 const DataLayout &DL = GV->getDataLayout();
6394 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6395
6396 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6397 /*AllowNonInbounds*/ true))
6398 // Fail if a constant offset could not be determined.
6399 return false;
6400
6401 uint64_t StartIdx = Off.getLimitedValue();
6402 if (StartIdx == UINT64_MAX)
6403 // Fail if the constant offset is excessive.
6404 return false;
6405
6406 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6407 // elements. Simply bail out if that isn't possible.
6408 if ((StartIdx % ElementSizeInBytes) != 0)
6409 return false;
6410
6411 Offset += StartIdx / ElementSizeInBytes;
6412 ConstantDataArray *Array = nullptr;
6413 ArrayType *ArrayTy = nullptr;
6414
6415 if (GV->getInitializer()->isNullValue()) {
6416 Type *GVTy = GV->getValueType();
6417 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6418 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6419
6420 Slice.Array = nullptr;
6421 Slice.Offset = 0;
6422 // Return an empty Slice for undersized constants to let callers
6423 // transform even undefined library calls into simpler, well-defined
6424 // expressions. This is preferable to making the calls although it
6425 // prevents sanitizers from detecting such calls.
6426 Slice.Length = Length < Offset ? 0 : Length - Offset;
6427 return true;
6428 }
6429
6430 auto *Init = const_cast<Constant *>(GV->getInitializer());
6431 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6432 Type *InitElTy = ArrayInit->getElementType();
6433 if (InitElTy->isIntegerTy(ElementSize)) {
6434 // If Init is an initializer for an array of the expected type
6435 // and size, use it as is.
6436 Array = ArrayInit;
6437 ArrayTy = ArrayInit->getType();
6438 }
6439 }
6440
6441 if (!Array) {
6442 if (ElementSize != 8)
6443 // TODO: Handle conversions to larger integral types.
6444 return false;
6445
6446 // Otherwise extract the portion of the initializer starting
6447 // at Offset as an array of bytes, and reset Offset.
6449 if (!Init)
6450 return false;
6451
6452 Offset = 0;
6454 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6455 }
6456
6457 uint64_t NumElts = ArrayTy->getArrayNumElements();
6458 if (Offset > NumElts)
6459 return false;
6460
6461 Slice.Array = Array;
6462 Slice.Offset = Offset;
6463 Slice.Length = NumElts - Offset;
6464 return true;
6465}
6466
6467/// Extract bytes from the initializer of the constant array V, which need
6468/// not be a nul-terminated string. On success, store the bytes in Str and
6469/// return true. When TrimAtNul is set, Str will contain only the bytes up
6470/// to but not including the first nul. Return false on failure.
6472 bool TrimAtNul) {
6474 if (!getConstantDataArrayInfo(V, Slice, 8))
6475 return false;
6476
6477 if (Slice.Array == nullptr) {
6478 if (TrimAtNul) {
6479 // Return a nul-terminated string even for an empty Slice. This is
6480 // safe because all existing SimplifyLibcalls callers require string
6481 // arguments and the behavior of the functions they fold is undefined
6482 // otherwise. Folding the calls this way is preferable to making
6483 // the undefined library calls, even though it prevents sanitizers
6484 // from reporting such calls.
6485 Str = StringRef();
6486 return true;
6487 }
6488 if (Slice.Length == 1) {
6489 Str = StringRef("", 1);
6490 return true;
6491 }
6492 // We cannot instantiate a StringRef as we do not have an appropriate string
6493 // of 0s at hand.
6494 return false;
6495 }
6496
6497 // Start out with the entire array in the StringRef.
6498 Str = Slice.Array->getAsString();
6499 // Skip over 'offset' bytes.
6500 Str = Str.substr(Slice.Offset);
6501
6502 if (TrimAtNul) {
6503 // Trim off the \0 and anything after it. If the array is not nul
6504 // terminated, we just return the whole end of string. The client may know
6505 // some other way that the string is length-bound.
6506 Str = Str.substr(0, Str.find('\0'));
6507 }
6508 return true;
6509}
6510
6511// These next two are very similar to the above, but also look through PHI
6512// nodes.
6513// TODO: See if we can integrate these two together.
6514
6515/// If we can compute the length of the string pointed to by
6516/// the specified pointer, return 'len+1'. If we can't, return 0.
6519 unsigned CharSize) {
6520 // Look through noop bitcast instructions.
6521 V = V->stripPointerCasts();
6522
6523 // If this is a PHI node, there are two cases: either we have already seen it
6524 // or we haven't.
6525 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6526 if (!PHIs.insert(PN).second)
6527 return ~0ULL; // already in the set.
6528
6529 // If it was new, see if all the input strings are the same length.
6530 uint64_t LenSoFar = ~0ULL;
6531 for (Value *IncValue : PN->incoming_values()) {
6532 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6533 if (Len == 0) return 0; // Unknown length -> unknown.
6534
6535 if (Len == ~0ULL) continue;
6536
6537 if (Len != LenSoFar && LenSoFar != ~0ULL)
6538 return 0; // Disagree -> unknown.
6539 LenSoFar = Len;
6540 }
6541
6542 // Success, all agree.
6543 return LenSoFar;
6544 }
6545
6546 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6547 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6548 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6549 if (Len1 == 0) return 0;
6550 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6551 if (Len2 == 0) return 0;
6552 if (Len1 == ~0ULL) return Len2;
6553 if (Len2 == ~0ULL) return Len1;
6554 if (Len1 != Len2) return 0;
6555 return Len1;
6556 }
6557
6558 // Otherwise, see if we can read the string.
6560 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6561 return 0;
6562
6563 if (Slice.Array == nullptr)
6564 // Zeroinitializer (including an empty one).
6565 return 1;
6566
6567 // Search for the first nul character. Return a conservative result even
6568 // when there is no nul. This is safe since otherwise the string function
6569 // being folded such as strlen is undefined, and can be preferable to
6570 // making the undefined library call.
6571 unsigned NullIndex = 0;
6572 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6573 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6574 break;
6575 }
6576
6577 return NullIndex + 1;
6578}
6579
6580/// If we can compute the length of the string pointed to by
6581/// the specified pointer, return 'len+1'. If we can't, return 0.
6582uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6583 if (!V->getType()->isPointerTy())
6584 return 0;
6585
6587 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6588 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6589 // an empty string as a length.
6590 return Len == ~0ULL ? 1 : Len;
6591}
6592
6593const Value *
6595 bool MustPreserveNullness) {
6596 assert(Call &&
6597 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6598 if (const Value *RV = Call->getReturnedArgOperand())
6599 return RV;
6600 // This can be used only as a aliasing property.
6602 Call, MustPreserveNullness))
6603 return Call->getArgOperand(0);
6604 return nullptr;
6605}
6606
6608 const CallBase *Call, bool MustPreserveNullness) {
6609 switch (Call->getIntrinsicID()) {
6610 case Intrinsic::launder_invariant_group:
6611 case Intrinsic::strip_invariant_group:
6612 case Intrinsic::aarch64_irg:
6613 case Intrinsic::aarch64_tagp:
6614 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6615 // input pointer (and thus preserve null-ness for the purposes of escape
6616 // analysis, which is where the MustPreserveNullness flag comes in to play).
6617 // However, it will not necessarily map ptr addrspace(N) null to ptr
6618 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6619 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6620 // list, no one should be relying on such a strict interpretation of
6621 // MustPreserveNullness (and, at time of writing, they are not), but we
6622 // document this fact out of an abundance of caution.
6623 case Intrinsic::amdgcn_make_buffer_rsrc:
6624 return true;
6625 case Intrinsic::ptrmask:
6626 return !MustPreserveNullness;
6627 case Intrinsic::threadlocal_address:
6628 // The underlying variable changes with thread ID. The Thread ID may change
6629 // at coroutine suspend points.
6630 return !Call->getParent()->getParent()->isPresplitCoroutine();
6631 default:
6632 return false;
6633 }
6634}
6635
6636/// \p PN defines a loop-variant pointer to an object. Check if the
6637/// previous iteration of the loop was referring to the same object as \p PN.
6639 const LoopInfo *LI) {
6640 // Find the loop-defined value.
6641 Loop *L = LI->getLoopFor(PN->getParent());
6642 if (PN->getNumIncomingValues() != 2)
6643 return true;
6644
6645 // Find the value from previous iteration.
6646 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6647 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6648 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6649 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6650 return true;
6651
6652 // If a new pointer is loaded in the loop, the pointer references a different
6653 // object in every iteration. E.g.:
6654 // for (i)
6655 // int *p = a[i];
6656 // ...
6657 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6658 if (!L->isLoopInvariant(Load->getPointerOperand()))
6659 return false;
6660 return true;
6661}
6662
6663const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6664 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6665 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6666 const Value *PtrOp = GEP->getPointerOperand();
6667 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6668 return V;
6669 V = PtrOp;
6670 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6671 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6672 Value *NewV = cast<Operator>(V)->getOperand(0);
6673 if (!NewV->getType()->isPointerTy())
6674 return V;
6675 V = NewV;
6676 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6677 if (GA->isInterposable())
6678 return V;
6679 V = GA->getAliasee();
6680 } else {
6681 if (auto *PHI = dyn_cast<PHINode>(V)) {
6682 // Look through single-arg phi nodes created by LCSSA.
6683 if (PHI->getNumIncomingValues() == 1) {
6684 V = PHI->getIncomingValue(0);
6685 continue;
6686 }
6687 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6688 // CaptureTracking can know about special capturing properties of some
6689 // intrinsics like launder.invariant.group, that can't be expressed with
6690 // the attributes, but have properties like returning aliasing pointer.
6691 // Because some analysis may assume that nocaptured pointer is not
6692 // returned from some special intrinsic (because function would have to
6693 // be marked with returns attribute), it is crucial to use this function
6694 // because it should be in sync with CaptureTracking. Not using it may
6695 // cause weird miscompilations where 2 aliasing pointers are assumed to
6696 // noalias.
6697 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6698 V = RP;
6699 continue;
6700 }
6701 }
6702
6703 return V;
6704 }
6705 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6706 }
6707 return V;
6708}
6709
6712 const LoopInfo *LI, unsigned MaxLookup) {
6715 Worklist.push_back(V);
6716 do {
6717 const Value *P = Worklist.pop_back_val();
6718 P = getUnderlyingObject(P, MaxLookup);
6719
6720 if (!Visited.insert(P).second)
6721 continue;
6722
6723 if (auto *SI = dyn_cast<SelectInst>(P)) {
6724 Worklist.push_back(SI->getTrueValue());
6725 Worklist.push_back(SI->getFalseValue());
6726 continue;
6727 }
6728
6729 if (auto *PN = dyn_cast<PHINode>(P)) {
6730 // If this PHI changes the underlying object in every iteration of the
6731 // loop, don't look through it. Consider:
6732 // int **A;
6733 // for (i) {
6734 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6735 // Curr = A[i];
6736 // *Prev, *Curr;
6737 //
6738 // Prev is tracking Curr one iteration behind so they refer to different
6739 // underlying objects.
6740 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6742 append_range(Worklist, PN->incoming_values());
6743 else
6744 Objects.push_back(P);
6745 continue;
6746 }
6747
6748 Objects.push_back(P);
6749 } while (!Worklist.empty());
6750}
6751
6753 const unsigned MaxVisited = 8;
6754
6757 Worklist.push_back(V);
6758 const Value *Object = nullptr;
6759 // Used as fallback if we can't find a common underlying object through
6760 // recursion.
6761 bool First = true;
6762 const Value *FirstObject = getUnderlyingObject(V);
6763 do {
6764 const Value *P = Worklist.pop_back_val();
6765 P = First ? FirstObject : getUnderlyingObject(P);
6766 First = false;
6767
6768 if (!Visited.insert(P).second)
6769 continue;
6770
6771 if (Visited.size() == MaxVisited)
6772 return FirstObject;
6773
6774 if (auto *SI = dyn_cast<SelectInst>(P)) {
6775 Worklist.push_back(SI->getTrueValue());
6776 Worklist.push_back(SI->getFalseValue());
6777 continue;
6778 }
6779
6780 if (auto *PN = dyn_cast<PHINode>(P)) {
6781 append_range(Worklist, PN->incoming_values());
6782 continue;
6783 }
6784
6785 if (!Object)
6786 Object = P;
6787 else if (Object != P)
6788 return FirstObject;
6789 } while (!Worklist.empty());
6790
6791 return Object ? Object : FirstObject;
6792}
6793
6794/// This is the function that does the work of looking through basic
6795/// ptrtoint+arithmetic+inttoptr sequences.
6796static const Value *getUnderlyingObjectFromInt(const Value *V) {
6797 do {
6798 if (const Operator *U = dyn_cast<Operator>(V)) {
6799 // If we find a ptrtoint, we can transfer control back to the
6800 // regular getUnderlyingObjectFromInt.
6801 if (U->getOpcode() == Instruction::PtrToInt)
6802 return U->getOperand(0);
6803 // If we find an add of a constant, a multiplied value, or a phi, it's
6804 // likely that the other operand will lead us to the base
6805 // object. We don't have to worry about the case where the
6806 // object address is somehow being computed by the multiply,
6807 // because our callers only care when the result is an
6808 // identifiable object.
6809 if (U->getOpcode() != Instruction::Add ||
6810 (!isa<ConstantInt>(U->getOperand(1)) &&
6811 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6812 !isa<PHINode>(U->getOperand(1))))
6813 return V;
6814 V = U->getOperand(0);
6815 } else {
6816 return V;
6817 }
6818 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6819 } while (true);
6820}
6821
6822/// This is a wrapper around getUnderlyingObjects and adds support for basic
6823/// ptrtoint+arithmetic+inttoptr sequences.
6824/// It returns false if unidentified object is found in getUnderlyingObjects.
6826 SmallVectorImpl<Value *> &Objects) {
6828 SmallVector<const Value *, 4> Working(1, V);
6829 do {
6830 V = Working.pop_back_val();
6831
6833 getUnderlyingObjects(V, Objs);
6834
6835 for (const Value *V : Objs) {
6836 if (!Visited.insert(V).second)
6837 continue;
6838 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6839 const Value *O =
6840 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6841 if (O->getType()->isPointerTy()) {
6842 Working.push_back(O);
6843 continue;
6844 }
6845 }
6846 // If getUnderlyingObjects fails to find an identifiable object,
6847 // getUnderlyingObjectsForCodeGen also fails for safety.
6848 if (!isIdentifiedObject(V)) {
6849 Objects.clear();
6850 return false;
6851 }
6852 Objects.push_back(const_cast<Value *>(V));
6853 }
6854 } while (!Working.empty());
6855 return true;
6856}
6857
6859 AllocaInst *Result = nullptr;
6861 SmallVector<Value *, 4> Worklist;
6862
6863 auto AddWork = [&](Value *V) {
6864 if (Visited.insert(V).second)
6865 Worklist.push_back(V);
6866 };
6867
6868 AddWork(V);
6869 do {
6870 V = Worklist.pop_back_val();
6871 assert(Visited.count(V));
6872
6873 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6874 if (Result && Result != AI)
6875 return nullptr;
6876 Result = AI;
6877 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
6878 AddWork(CI->getOperand(0));
6879 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
6880 for (Value *IncValue : PN->incoming_values())
6881 AddWork(IncValue);
6882 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
6883 AddWork(SI->getTrueValue());
6884 AddWork(SI->getFalseValue());
6886 if (OffsetZero && !GEP->hasAllZeroIndices())
6887 return nullptr;
6888 AddWork(GEP->getPointerOperand());
6889 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
6890 Value *Returned = CB->getReturnedArgOperand();
6891 if (Returned)
6892 AddWork(Returned);
6893 else
6894 return nullptr;
6895 } else {
6896 return nullptr;
6897 }
6898 } while (!Worklist.empty());
6899
6900 return Result;
6901}
6902
6904 const Value *V, bool AllowLifetime, bool AllowDroppable) {
6905 for (const User *U : V->users()) {
6907 if (!II)
6908 return false;
6909
6910 if (AllowLifetime && II->isLifetimeStartOrEnd())
6911 continue;
6912
6913 if (AllowDroppable && II->isDroppable())
6914 continue;
6915
6916 return false;
6917 }
6918 return true;
6919}
6920
6923 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
6924}
6927 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
6928}
6929
6931 if (auto *II = dyn_cast<IntrinsicInst>(I))
6932 return isTriviallyVectorizable(II->getIntrinsicID());
6933 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
6934 return (!Shuffle || Shuffle->isSelect()) &&
6936}
6937
6939 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
6940 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
6941 bool IgnoreUBImplyingAttrs) {
6942 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
6943 AC, DT, TLI, UseVariableInfo,
6944 IgnoreUBImplyingAttrs);
6945}
6946
6948 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
6949 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
6950 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
6951#ifndef NDEBUG
6952 if (Inst->getOpcode() != Opcode) {
6953 // Check that the operands are actually compatible with the Opcode override.
6954 auto hasEqualReturnAndLeadingOperandTypes =
6955 [](const Instruction *Inst, unsigned NumLeadingOperands) {
6956 if (Inst->getNumOperands() < NumLeadingOperands)
6957 return false;
6958 const Type *ExpectedType = Inst->getType();
6959 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
6960 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
6961 return false;
6962 return true;
6963 };
6965 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
6966 assert(!Instruction::isUnaryOp(Opcode) ||
6967 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
6968 }
6969#endif
6970
6971 switch (Opcode) {
6972 default:
6973 return true;
6974 case Instruction::UDiv:
6975 case Instruction::URem: {
6976 // x / y is undefined if y == 0.
6977 const APInt *V;
6978 if (match(Inst->getOperand(1), m_APInt(V)))
6979 return *V != 0;
6980 return false;
6981 }
6982 case Instruction::SDiv:
6983 case Instruction::SRem: {
6984 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
6985 const APInt *Numerator, *Denominator;
6986 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
6987 return false;
6988 // We cannot hoist this division if the denominator is 0.
6989 if (*Denominator == 0)
6990 return false;
6991 // It's safe to hoist if the denominator is not 0 or -1.
6992 if (!Denominator->isAllOnes())
6993 return true;
6994 // At this point we know that the denominator is -1. It is safe to hoist as
6995 // long we know that the numerator is not INT_MIN.
6996 if (match(Inst->getOperand(0), m_APInt(Numerator)))
6997 return !Numerator->isMinSignedValue();
6998 // The numerator *might* be MinSignedValue.
6999 return false;
7000 }
7001 case Instruction::Load: {
7002 if (!UseVariableInfo)
7003 return false;
7004
7005 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7006 if (!LI)
7007 return false;
7008 if (mustSuppressSpeculation(*LI))
7009 return false;
7010 const DataLayout &DL = LI->getDataLayout();
7012 LI->getType(), LI->getAlign(), DL,
7013 CtxI, AC, DT, TLI);
7014 }
7015 case Instruction::Call: {
7016 auto *CI = dyn_cast<const CallInst>(Inst);
7017 if (!CI)
7018 return false;
7019 const Function *Callee = CI->getCalledFunction();
7020
7021 // The called function could have undefined behavior or side-effects, even
7022 // if marked readnone nounwind.
7023 if (!Callee || !Callee->isSpeculatable())
7024 return false;
7025 // Since the operands may be changed after hoisting, undefined behavior may
7026 // be triggered by some UB-implying attributes.
7027 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7028 }
7029 case Instruction::VAArg:
7030 case Instruction::Alloca:
7031 case Instruction::Invoke:
7032 case Instruction::CallBr:
7033 case Instruction::PHI:
7034 case Instruction::Store:
7035 case Instruction::Ret:
7036 case Instruction::Br:
7037 case Instruction::IndirectBr:
7038 case Instruction::Switch:
7039 case Instruction::Unreachable:
7040 case Instruction::Fence:
7041 case Instruction::AtomicRMW:
7042 case Instruction::AtomicCmpXchg:
7043 case Instruction::LandingPad:
7044 case Instruction::Resume:
7045 case Instruction::CatchSwitch:
7046 case Instruction::CatchPad:
7047 case Instruction::CatchRet:
7048 case Instruction::CleanupPad:
7049 case Instruction::CleanupRet:
7050 return false; // Misc instructions which have effects
7051 }
7052}
7053
7055 if (I.mayReadOrWriteMemory())
7056 // Memory dependency possible
7057 return true;
7059 // Can't move above a maythrow call or infinite loop. Or if an
7060 // inalloca alloca, above a stacksave call.
7061 return true;
7063 // 1) Can't reorder two inf-loop calls, even if readonly
7064 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7065 // safe to speculative execute. (Inverse of above)
7066 return true;
7067 return false;
7068}
7069
7070/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7084
7085/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7088 bool ForSigned,
7089 const SimplifyQuery &SQ) {
7090 ConstantRange CR1 =
7091 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7092 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7095 return CR1.intersectWith(CR2, RangeType);
7096}
7097
7099 const Value *RHS,
7100 const SimplifyQuery &SQ,
7101 bool IsNSW) {
7102 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7103 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7104
7105 // mul nsw of two non-negative numbers is also nuw.
7106 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
7108
7109 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
7110 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
7111 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7112}
7113
7115 const Value *RHS,
7116 const SimplifyQuery &SQ) {
7117 // Multiplying n * m significant bits yields a result of n + m significant
7118 // bits. If the total number of significant bits does not exceed the
7119 // result bit width (minus 1), there is no overflow.
7120 // This means if we have enough leading sign bits in the operands
7121 // we can guarantee that the result does not overflow.
7122 // Ref: "Hacker's Delight" by Henry Warren
7123 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7124
7125 // Note that underestimating the number of sign bits gives a more
7126 // conservative answer.
7127 unsigned SignBits =
7128 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7129
7130 // First handle the easy case: if we have enough sign bits there's
7131 // definitely no overflow.
7132 if (SignBits > BitWidth + 1)
7134
7135 // There are two ambiguous cases where there can be no overflow:
7136 // SignBits == BitWidth + 1 and
7137 // SignBits == BitWidth
7138 // The second case is difficult to check, therefore we only handle the
7139 // first case.
7140 if (SignBits == BitWidth + 1) {
7141 // It overflows only when both arguments are negative and the true
7142 // product is exactly the minimum negative number.
7143 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7144 // For simplicity we just check if at least one side is not negative.
7145 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7146 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7147 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7149 }
7151}
7152
7155 const WithCache<const Value *> &RHS,
7156 const SimplifyQuery &SQ) {
7157 ConstantRange LHSRange =
7158 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7159 ConstantRange RHSRange =
7160 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7161 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7162}
7163
7164static OverflowResult
7167 const AddOperator *Add, const SimplifyQuery &SQ) {
7168 if (Add && Add->hasNoSignedWrap()) {
7170 }
7171
7172 // If LHS and RHS each have at least two sign bits, the addition will look
7173 // like
7174 //
7175 // XX..... +
7176 // YY.....
7177 //
7178 // If the carry into the most significant position is 0, X and Y can't both
7179 // be 1 and therefore the carry out of the addition is also 0.
7180 //
7181 // If the carry into the most significant position is 1, X and Y can't both
7182 // be 0 and therefore the carry out of the addition is also 1.
7183 //
7184 // Since the carry into the most significant position is always equal to
7185 // the carry out of the addition, there is no signed overflow.
7186 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7188
7189 ConstantRange LHSRange =
7190 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7191 ConstantRange RHSRange =
7192 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7193 OverflowResult OR =
7194 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7196 return OR;
7197
7198 // The remaining code needs Add to be available. Early returns if not so.
7199 if (!Add)
7201
7202 // If the sign of Add is the same as at least one of the operands, this add
7203 // CANNOT overflow. If this can be determined from the known bits of the
7204 // operands the above signedAddMayOverflow() check will have already done so.
7205 // The only other way to improve on the known bits is from an assumption, so
7206 // call computeKnownBitsFromContext() directly.
7207 bool LHSOrRHSKnownNonNegative =
7208 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7209 bool LHSOrRHSKnownNegative =
7210 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7211 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7212 KnownBits AddKnown(LHSRange.getBitWidth());
7213 computeKnownBitsFromContext(Add, AddKnown, SQ);
7214 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7215 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7217 }
7218
7220}
7221
7223 const Value *RHS,
7224 const SimplifyQuery &SQ) {
7225 // X - (X % ?)
7226 // The remainder of a value can't have greater magnitude than itself,
7227 // so the subtraction can't overflow.
7228
7229 // X - (X -nuw ?)
7230 // In the minimal case, this would simplify to "?", so there's no subtract
7231 // at all. But if this analysis is used to peek through casts, for example,
7232 // then determining no-overflow may allow other transforms.
7233
7234 // TODO: There are other patterns like this.
7235 // See simplifyICmpWithBinOpOnLHS() for candidates.
7236 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7237 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7238 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7240
7241 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7242 SQ.DL)) {
7243 if (*C)
7246 }
7247
7248 ConstantRange LHSRange =
7249 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7250 ConstantRange RHSRange =
7251 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7252 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7253}
7254
7256 const Value *RHS,
7257 const SimplifyQuery &SQ) {
7258 // X - (X % ?)
7259 // The remainder of a value can't have greater magnitude than itself,
7260 // so the subtraction can't overflow.
7261
7262 // X - (X -nsw ?)
7263 // In the minimal case, this would simplify to "?", so there's no subtract
7264 // at all. But if this analysis is used to peek through casts, for example,
7265 // then determining no-overflow may allow other transforms.
7266 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7267 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7268 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7270
7271 // If LHS and RHS each have at least two sign bits, the subtraction
7272 // cannot overflow.
7273 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7275
7276 ConstantRange LHSRange =
7277 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7278 ConstantRange RHSRange =
7279 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7280 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7281}
7282
7284 const DominatorTree &DT) {
7285 SmallVector<const BranchInst *, 2> GuardingBranches;
7287
7288 for (const User *U : WO->users()) {
7289 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7290 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7291
7292 if (EVI->getIndices()[0] == 0)
7293 Results.push_back(EVI);
7294 else {
7295 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7296
7297 for (const auto *U : EVI->users())
7298 if (const auto *B = dyn_cast<BranchInst>(U)) {
7299 assert(B->isConditional() && "How else is it using an i1?");
7300 GuardingBranches.push_back(B);
7301 }
7302 }
7303 } else {
7304 // We are using the aggregate directly in a way we don't want to analyze
7305 // here (storing it to a global, say).
7306 return false;
7307 }
7308 }
7309
7310 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7311 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7312 if (!NoWrapEdge.isSingleEdge())
7313 return false;
7314
7315 // Check if all users of the add are provably no-wrap.
7316 for (const auto *Result : Results) {
7317 // If the extractvalue itself is not executed on overflow, the we don't
7318 // need to check each use separately, since domination is transitive.
7319 if (DT.dominates(NoWrapEdge, Result->getParent()))
7320 continue;
7321
7322 for (const auto &RU : Result->uses())
7323 if (!DT.dominates(NoWrapEdge, RU))
7324 return false;
7325 }
7326
7327 return true;
7328 };
7329
7330 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7331}
7332
7333/// Shifts return poison if shiftwidth is larger than the bitwidth.
7334static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7335 auto *C = dyn_cast<Constant>(ShiftAmount);
7336 if (!C)
7337 return false;
7338
7339 // Shifts return poison if shiftwidth is larger than the bitwidth.
7341 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7342 unsigned NumElts = FVTy->getNumElements();
7343 for (unsigned i = 0; i < NumElts; ++i)
7344 ShiftAmounts.push_back(C->getAggregateElement(i));
7345 } else if (isa<ScalableVectorType>(C->getType()))
7346 return false; // Can't tell, just return false to be safe
7347 else
7348 ShiftAmounts.push_back(C);
7349
7350 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7351 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7352 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7353 });
7354
7355 return Safe;
7356}
7357
7363
7365 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7366}
7367
7369 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7370}
7371
7373 bool ConsiderFlagsAndMetadata) {
7374
7375 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7376 Op->hasPoisonGeneratingAnnotations())
7377 return true;
7378
7379 unsigned Opcode = Op->getOpcode();
7380
7381 // Check whether opcode is a poison/undef-generating operation
7382 switch (Opcode) {
7383 case Instruction::Shl:
7384 case Instruction::AShr:
7385 case Instruction::LShr:
7386 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7387 case Instruction::FPToSI:
7388 case Instruction::FPToUI:
7389 // fptosi/ui yields poison if the resulting value does not fit in the
7390 // destination type.
7391 return true;
7392 case Instruction::Call:
7393 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7394 switch (II->getIntrinsicID()) {
7395 // TODO: Add more intrinsics.
7396 case Intrinsic::ctlz:
7397 case Intrinsic::cttz:
7398 case Intrinsic::abs:
7399 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7400 return false;
7401 break;
7402 case Intrinsic::ctpop:
7403 case Intrinsic::bswap:
7404 case Intrinsic::bitreverse:
7405 case Intrinsic::fshl:
7406 case Intrinsic::fshr:
7407 case Intrinsic::smax:
7408 case Intrinsic::smin:
7409 case Intrinsic::scmp:
7410 case Intrinsic::umax:
7411 case Intrinsic::umin:
7412 case Intrinsic::ucmp:
7413 case Intrinsic::ptrmask:
7414 case Intrinsic::fptoui_sat:
7415 case Intrinsic::fptosi_sat:
7416 case Intrinsic::sadd_with_overflow:
7417 case Intrinsic::ssub_with_overflow:
7418 case Intrinsic::smul_with_overflow:
7419 case Intrinsic::uadd_with_overflow:
7420 case Intrinsic::usub_with_overflow:
7421 case Intrinsic::umul_with_overflow:
7422 case Intrinsic::sadd_sat:
7423 case Intrinsic::uadd_sat:
7424 case Intrinsic::ssub_sat:
7425 case Intrinsic::usub_sat:
7426 return false;
7427 case Intrinsic::sshl_sat:
7428 case Intrinsic::ushl_sat:
7429 return includesPoison(Kind) &&
7430 !shiftAmountKnownInRange(II->getArgOperand(1));
7431 case Intrinsic::fma:
7432 case Intrinsic::fmuladd:
7433 case Intrinsic::sqrt:
7434 case Intrinsic::powi:
7435 case Intrinsic::sin:
7436 case Intrinsic::cos:
7437 case Intrinsic::pow:
7438 case Intrinsic::log:
7439 case Intrinsic::log10:
7440 case Intrinsic::log2:
7441 case Intrinsic::exp:
7442 case Intrinsic::exp2:
7443 case Intrinsic::exp10:
7444 case Intrinsic::fabs:
7445 case Intrinsic::copysign:
7446 case Intrinsic::floor:
7447 case Intrinsic::ceil:
7448 case Intrinsic::trunc:
7449 case Intrinsic::rint:
7450 case Intrinsic::nearbyint:
7451 case Intrinsic::round:
7452 case Intrinsic::roundeven:
7453 case Intrinsic::fptrunc_round:
7454 case Intrinsic::canonicalize:
7455 case Intrinsic::arithmetic_fence:
7456 case Intrinsic::minnum:
7457 case Intrinsic::maxnum:
7458 case Intrinsic::minimum:
7459 case Intrinsic::maximum:
7460 case Intrinsic::minimumnum:
7461 case Intrinsic::maximumnum:
7462 case Intrinsic::is_fpclass:
7463 case Intrinsic::ldexp:
7464 case Intrinsic::frexp:
7465 return false;
7466 case Intrinsic::lround:
7467 case Intrinsic::llround:
7468 case Intrinsic::lrint:
7469 case Intrinsic::llrint:
7470 // If the value doesn't fit an unspecified value is returned (but this
7471 // is not poison).
7472 return false;
7473 }
7474 }
7475 [[fallthrough]];
7476 case Instruction::CallBr:
7477 case Instruction::Invoke: {
7478 const auto *CB = cast<CallBase>(Op);
7479 return !CB->hasRetAttr(Attribute::NoUndef);
7480 }
7481 case Instruction::InsertElement:
7482 case Instruction::ExtractElement: {
7483 // If index exceeds the length of the vector, it returns poison
7484 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7485 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7486 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7487 if (includesPoison(Kind))
7488 return !Idx ||
7489 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7490 return false;
7491 }
7492 case Instruction::ShuffleVector: {
7494 ? cast<ConstantExpr>(Op)->getShuffleMask()
7495 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7496 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7497 }
7498 case Instruction::FNeg:
7499 case Instruction::PHI:
7500 case Instruction::Select:
7501 case Instruction::ExtractValue:
7502 case Instruction::InsertValue:
7503 case Instruction::Freeze:
7504 case Instruction::ICmp:
7505 case Instruction::FCmp:
7506 case Instruction::GetElementPtr:
7507 return false;
7508 case Instruction::AddrSpaceCast:
7509 return true;
7510 default: {
7511 const auto *CE = dyn_cast<ConstantExpr>(Op);
7512 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7513 return false;
7514 else if (Instruction::isBinaryOp(Opcode))
7515 return false;
7516 // Be conservative and return true.
7517 return true;
7518 }
7519 }
7520}
7521
7523 bool ConsiderFlagsAndMetadata) {
7524 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7525 ConsiderFlagsAndMetadata);
7526}
7527
7528bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7529 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7530 ConsiderFlagsAndMetadata);
7531}
7532
7533static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7534 unsigned Depth) {
7535 if (ValAssumedPoison == V)
7536 return true;
7537
7538 const unsigned MaxDepth = 2;
7539 if (Depth >= MaxDepth)
7540 return false;
7541
7542 if (const auto *I = dyn_cast<Instruction>(V)) {
7543 if (any_of(I->operands(), [=](const Use &Op) {
7544 return propagatesPoison(Op) &&
7545 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7546 }))
7547 return true;
7548
7549 // V = extractvalue V0, idx
7550 // V2 = extractvalue V0, idx2
7551 // V0's elements are all poison or not. (e.g., add_with_overflow)
7552 const WithOverflowInst *II;
7554 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7555 llvm::is_contained(II->args(), ValAssumedPoison)))
7556 return true;
7557 }
7558 return false;
7559}
7560
7561static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7562 unsigned Depth) {
7563 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7564 return true;
7565
7566 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7567 return true;
7568
7569 const unsigned MaxDepth = 2;
7570 if (Depth >= MaxDepth)
7571 return false;
7572
7573 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7574 if (I && !canCreatePoison(cast<Operator>(I))) {
7575 return all_of(I->operands(), [=](const Value *Op) {
7576 return impliesPoison(Op, V, Depth + 1);
7577 });
7578 }
7579 return false;
7580}
7581
7582bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7583 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7584}
7585
7586static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7587
7589 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7590 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7592 return false;
7593
7594 if (isa<MetadataAsValue>(V))
7595 return false;
7596
7597 if (const auto *A = dyn_cast<Argument>(V)) {
7598 if (A->hasAttribute(Attribute::NoUndef) ||
7599 A->hasAttribute(Attribute::Dereferenceable) ||
7600 A->hasAttribute(Attribute::DereferenceableOrNull))
7601 return true;
7602 }
7603
7604 if (auto *C = dyn_cast<Constant>(V)) {
7605 if (isa<PoisonValue>(C))
7606 return !includesPoison(Kind);
7607
7608 if (isa<UndefValue>(C))
7609 return !includesUndef(Kind);
7610
7613 return true;
7614
7615 if (C->getType()->isVectorTy()) {
7616 if (isa<ConstantExpr>(C)) {
7617 // Scalable vectors can use a ConstantExpr to build a splat.
7618 if (Constant *SplatC = C->getSplatValue())
7619 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7620 return true;
7621 } else {
7622 if (includesUndef(Kind) && C->containsUndefElement())
7623 return false;
7624 if (includesPoison(Kind) && C->containsPoisonElement())
7625 return false;
7626 return !C->containsConstantExpression();
7627 }
7628 }
7629 }
7630
7631 // Strip cast operations from a pointer value.
7632 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7633 // inbounds with zero offset. To guarantee that the result isn't poison, the
7634 // stripped pointer is checked as it has to be pointing into an allocated
7635 // object or be null `null` to ensure `inbounds` getelement pointers with a
7636 // zero offset could not produce poison.
7637 // It can strip off addrspacecast that do not change bit representation as
7638 // well. We believe that such addrspacecast is equivalent to no-op.
7639 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7640 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7641 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7642 return true;
7643
7644 auto OpCheck = [&](const Value *V) {
7645 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7646 };
7647
7648 if (auto *Opr = dyn_cast<Operator>(V)) {
7649 // If the value is a freeze instruction, then it can never
7650 // be undef or poison.
7651 if (isa<FreezeInst>(V))
7652 return true;
7653
7654 if (const auto *CB = dyn_cast<CallBase>(V)) {
7655 if (CB->hasRetAttr(Attribute::NoUndef) ||
7656 CB->hasRetAttr(Attribute::Dereferenceable) ||
7657 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7658 return true;
7659 }
7660
7661 if (const auto *PN = dyn_cast<PHINode>(V)) {
7662 unsigned Num = PN->getNumIncomingValues();
7663 bool IsWellDefined = true;
7664 for (unsigned i = 0; i < Num; ++i) {
7665 if (PN == PN->getIncomingValue(i))
7666 continue;
7667 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7668 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7669 DT, Depth + 1, Kind)) {
7670 IsWellDefined = false;
7671 break;
7672 }
7673 }
7674 if (IsWellDefined)
7675 return true;
7676 } else if (!::canCreateUndefOrPoison(Opr, Kind,
7677 /*ConsiderFlagsAndMetadata*/ true) &&
7678 all_of(Opr->operands(), OpCheck))
7679 return true;
7680 }
7681
7682 if (auto *I = dyn_cast<LoadInst>(V))
7683 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7684 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7685 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7686 return true;
7687
7689 return true;
7690
7691 // CxtI may be null or a cloned instruction.
7692 if (!CtxI || !CtxI->getParent() || !DT)
7693 return false;
7694
7695 auto *DNode = DT->getNode(CtxI->getParent());
7696 if (!DNode)
7697 // Unreachable block
7698 return false;
7699
7700 // If V is used as a branch condition before reaching CtxI, V cannot be
7701 // undef or poison.
7702 // br V, BB1, BB2
7703 // BB1:
7704 // CtxI ; V cannot be undef or poison here
7705 auto *Dominator = DNode->getIDom();
7706 // This check is purely for compile time reasons: we can skip the IDom walk
7707 // if what we are checking for includes undef and the value is not an integer.
7708 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7709 while (Dominator) {
7710 auto *TI = Dominator->getBlock()->getTerminator();
7711
7712 Value *Cond = nullptr;
7713 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7714 if (BI->isConditional())
7715 Cond = BI->getCondition();
7716 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7717 Cond = SI->getCondition();
7718 }
7719
7720 if (Cond) {
7721 if (Cond == V)
7722 return true;
7723 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7724 // For poison, we can analyze further
7725 auto *Opr = cast<Operator>(Cond);
7726 if (any_of(Opr->operands(), [V](const Use &U) {
7727 return V == U && propagatesPoison(U);
7728 }))
7729 return true;
7730 }
7731 }
7732
7733 Dominator = Dominator->getIDom();
7734 }
7735
7736 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7737 return true;
7738
7739 return false;
7740}
7741
7743 const Instruction *CtxI,
7744 const DominatorTree *DT,
7745 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
7758 const Instruction *CtxI,
7759 const DominatorTree *DT, unsigned Depth) {
7760 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7762}
7763
7764/// Return true if undefined behavior would provably be executed on the path to
7765/// OnPathTo if Root produced a posion result. Note that this doesn't say
7766/// anything about whether OnPathTo is actually executed or whether Root is
7767/// actually poison. This can be used to assess whether a new use of Root can
7768/// be added at a location which is control equivalent with OnPathTo (such as
7769/// immediately before it) without introducing UB which didn't previously
7770/// exist. Note that a false result conveys no information.
7772 Instruction *OnPathTo,
7773 DominatorTree *DT) {
7774 // Basic approach is to assume Root is poison, propagate poison forward
7775 // through all users we can easily track, and then check whether any of those
7776 // users are provable UB and must execute before out exiting block might
7777 // exit.
7778
7779 // The set of all recursive users we've visited (which are assumed to all be
7780 // poison because of said visit)
7783 Worklist.push_back(Root);
7784 while (!Worklist.empty()) {
7785 const Instruction *I = Worklist.pop_back_val();
7786
7787 // If we know this must trigger UB on a path leading our target.
7788 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7789 return true;
7790
7791 // If we can't analyze propagation through this instruction, just skip it
7792 // and transitive users. Safe as false is a conservative result.
7793 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7794 return KnownPoison.contains(U) && propagatesPoison(U);
7795 }))
7796 continue;
7797
7798 if (KnownPoison.insert(I).second)
7799 for (const User *User : I->users())
7800 Worklist.push_back(cast<Instruction>(User));
7801 }
7802
7803 // Might be non-UB, or might have a path we couldn't prove must execute on
7804 // way to exiting bb.
7805 return false;
7806}
7807
7809 const SimplifyQuery &SQ) {
7810 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7811 Add, SQ);
7812}
7813
7816 const WithCache<const Value *> &RHS,
7817 const SimplifyQuery &SQ) {
7818 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7819}
7820
7822 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7823 // of time because it's possible for another thread to interfere with it for an
7824 // arbitrary length of time, but programs aren't allowed to rely on that.
7825
7826 // If there is no successor, then execution can't transfer to it.
7827 if (isa<ReturnInst>(I))
7828 return false;
7830 return false;
7831
7832 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7833 // Instruction::willReturn.
7834 //
7835 // FIXME: Move this check into Instruction::willReturn.
7836 if (isa<CatchPadInst>(I)) {
7837 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7838 default:
7839 // A catchpad may invoke exception object constructors and such, which
7840 // in some languages can be arbitrary code, so be conservative by default.
7841 return false;
7843 // For CoreCLR, it just involves a type test.
7844 return true;
7845 }
7846 }
7847
7848 // An instruction that returns without throwing must transfer control flow
7849 // to a successor.
7850 return !I->mayThrow() && I->willReturn();
7851}
7852
7854 // TODO: This is slightly conservative for invoke instruction since exiting
7855 // via an exception *is* normal control for them.
7856 for (const Instruction &I : *BB)
7858 return false;
7859 return true;
7860}
7861
7868
7871 assert(ScanLimit && "scan limit must be non-zero");
7872 for (const Instruction &I : Range) {
7873 if (--ScanLimit == 0)
7874 return false;
7876 return false;
7877 }
7878 return true;
7879}
7880
7882 const Loop *L) {
7883 // The loop header is guaranteed to be executed for every iteration.
7884 //
7885 // FIXME: Relax this constraint to cover all basic blocks that are
7886 // guaranteed to be executed at every iteration.
7887 if (I->getParent() != L->getHeader()) return false;
7888
7889 for (const Instruction &LI : *L->getHeader()) {
7890 if (&LI == I) return true;
7891 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7892 }
7893 llvm_unreachable("Instruction not contained in its own parent basic block.");
7894}
7895
7897 switch (IID) {
7898 // TODO: Add more intrinsics.
7899 case Intrinsic::sadd_with_overflow:
7900 case Intrinsic::ssub_with_overflow:
7901 case Intrinsic::smul_with_overflow:
7902 case Intrinsic::uadd_with_overflow:
7903 case Intrinsic::usub_with_overflow:
7904 case Intrinsic::umul_with_overflow:
7905 // If an input is a vector containing a poison element, the
7906 // two output vectors (calculated results, overflow bits)'
7907 // corresponding lanes are poison.
7908 return true;
7909 case Intrinsic::ctpop:
7910 case Intrinsic::ctlz:
7911 case Intrinsic::cttz:
7912 case Intrinsic::abs:
7913 case Intrinsic::smax:
7914 case Intrinsic::smin:
7915 case Intrinsic::umax:
7916 case Intrinsic::umin:
7917 case Intrinsic::scmp:
7918 case Intrinsic::is_fpclass:
7919 case Intrinsic::ptrmask:
7920 case Intrinsic::ucmp:
7921 case Intrinsic::bitreverse:
7922 case Intrinsic::bswap:
7923 case Intrinsic::sadd_sat:
7924 case Intrinsic::ssub_sat:
7925 case Intrinsic::sshl_sat:
7926 case Intrinsic::uadd_sat:
7927 case Intrinsic::usub_sat:
7928 case Intrinsic::ushl_sat:
7929 case Intrinsic::smul_fix:
7930 case Intrinsic::smul_fix_sat:
7931 case Intrinsic::umul_fix:
7932 case Intrinsic::umul_fix_sat:
7933 case Intrinsic::pow:
7934 case Intrinsic::powi:
7935 case Intrinsic::sin:
7936 case Intrinsic::sinh:
7937 case Intrinsic::cos:
7938 case Intrinsic::cosh:
7939 case Intrinsic::sincos:
7940 case Intrinsic::sincospi:
7941 case Intrinsic::tan:
7942 case Intrinsic::tanh:
7943 case Intrinsic::asin:
7944 case Intrinsic::acos:
7945 case Intrinsic::atan:
7946 case Intrinsic::atan2:
7947 case Intrinsic::canonicalize:
7948 case Intrinsic::sqrt:
7949 case Intrinsic::exp:
7950 case Intrinsic::exp2:
7951 case Intrinsic::exp10:
7952 case Intrinsic::log:
7953 case Intrinsic::log2:
7954 case Intrinsic::log10:
7955 case Intrinsic::modf:
7956 case Intrinsic::floor:
7957 case Intrinsic::ceil:
7958 case Intrinsic::trunc:
7959 case Intrinsic::rint:
7960 case Intrinsic::nearbyint:
7961 case Intrinsic::round:
7962 case Intrinsic::roundeven:
7963 case Intrinsic::lrint:
7964 case Intrinsic::llrint:
7965 return true;
7966 default:
7967 return false;
7968 }
7969}
7970
7971bool llvm::propagatesPoison(const Use &PoisonOp) {
7972 const Operator *I = cast<Operator>(PoisonOp.getUser());
7973 switch (I->getOpcode()) {
7974 case Instruction::Freeze:
7975 case Instruction::PHI:
7976 case Instruction::Invoke:
7977 return false;
7978 case Instruction::Select:
7979 return PoisonOp.getOperandNo() == 0;
7980 case Instruction::Call:
7981 if (auto *II = dyn_cast<IntrinsicInst>(I))
7982 return intrinsicPropagatesPoison(II->getIntrinsicID());
7983 return false;
7984 case Instruction::ICmp:
7985 case Instruction::FCmp:
7986 case Instruction::GetElementPtr:
7987 return true;
7988 default:
7990 return true;
7991
7992 // Be conservative and return false.
7993 return false;
7994 }
7995}
7996
7997/// Enumerates all operands of \p I that are guaranteed to not be undef or
7998/// poison. If the callback \p Handle returns true, stop processing and return
7999/// true. Otherwise, return false.
8000template <typename CallableT>
8002 const CallableT &Handle) {
8003 switch (I->getOpcode()) {
8004 case Instruction::Store:
8005 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8006 return true;
8007 break;
8008
8009 case Instruction::Load:
8010 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8011 return true;
8012 break;
8013
8014 // Since dereferenceable attribute imply noundef, atomic operations
8015 // also implicitly have noundef pointers too
8016 case Instruction::AtomicCmpXchg:
8018 return true;
8019 break;
8020
8021 case Instruction::AtomicRMW:
8022 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8023 return true;
8024 break;
8025
8026 case Instruction::Call:
8027 case Instruction::Invoke: {
8028 const CallBase *CB = cast<CallBase>(I);
8029 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8030 return true;
8031 for (unsigned i = 0; i < CB->arg_size(); ++i)
8032 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8033 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8034 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8035 Handle(CB->getArgOperand(i)))
8036 return true;
8037 break;
8038 }
8039 case Instruction::Ret:
8040 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8041 Handle(I->getOperand(0)))
8042 return true;
8043 break;
8044 case Instruction::Switch:
8045 if (Handle(cast<SwitchInst>(I)->getCondition()))
8046 return true;
8047 break;
8048 case Instruction::Br: {
8049 auto *BR = cast<BranchInst>(I);
8050 if (BR->isConditional() && Handle(BR->getCondition()))
8051 return true;
8052 break;
8053 }
8054 default:
8055 break;
8056 }
8057
8058 return false;
8059}
8060
8061/// Enumerates all operands of \p I that are guaranteed to not be poison.
8062template <typename CallableT>
8064 const CallableT &Handle) {
8065 if (handleGuaranteedWellDefinedOps(I, Handle))
8066 return true;
8067 switch (I->getOpcode()) {
8068 // Divisors of these operations are allowed to be partially undef.
8069 case Instruction::UDiv:
8070 case Instruction::SDiv:
8071 case Instruction::URem:
8072 case Instruction::SRem:
8073 return Handle(I->getOperand(1));
8074 default:
8075 return false;
8076 }
8077}
8078
8080 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8082 I, [&](const Value *V) { return KnownPoison.count(V); });
8083}
8084
8086 bool PoisonOnly) {
8087 // We currently only look for uses of values within the same basic
8088 // block, as that makes it easier to guarantee that the uses will be
8089 // executed given that Inst is executed.
8090 //
8091 // FIXME: Expand this to consider uses beyond the same basic block. To do
8092 // this, look out for the distinction between post-dominance and strong
8093 // post-dominance.
8094 const BasicBlock *BB = nullptr;
8096 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8097 BB = Inst->getParent();
8098 Begin = Inst->getIterator();
8099 Begin++;
8100 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8101 if (Arg->getParent()->isDeclaration())
8102 return false;
8103 BB = &Arg->getParent()->getEntryBlock();
8104 Begin = BB->begin();
8105 } else {
8106 return false;
8107 }
8108
8109 // Limit number of instructions we look at, to avoid scanning through large
8110 // blocks. The current limit is chosen arbitrarily.
8111 unsigned ScanLimit = 32;
8112 BasicBlock::const_iterator End = BB->end();
8113
8114 if (!PoisonOnly) {
8115 // Since undef does not propagate eagerly, be conservative & just check
8116 // whether a value is directly passed to an instruction that must take
8117 // well-defined operands.
8118
8119 for (const auto &I : make_range(Begin, End)) {
8120 if (--ScanLimit == 0)
8121 break;
8122
8123 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8124 return WellDefinedOp == V;
8125 }))
8126 return true;
8127
8129 break;
8130 }
8131 return false;
8132 }
8133
8134 // Set of instructions that we have proved will yield poison if Inst
8135 // does.
8136 SmallPtrSet<const Value *, 16> YieldsPoison;
8138
8139 YieldsPoison.insert(V);
8140 Visited.insert(BB);
8141
8142 while (true) {
8143 for (const auto &I : make_range(Begin, End)) {
8144 if (--ScanLimit == 0)
8145 return false;
8146 if (mustTriggerUB(&I, YieldsPoison))
8147 return true;
8149 return false;
8150
8151 // If an operand is poison and propagates it, mark I as yielding poison.
8152 for (const Use &Op : I.operands()) {
8153 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8154 YieldsPoison.insert(&I);
8155 break;
8156 }
8157 }
8158
8159 // Special handling for select, which returns poison if its operand 0 is
8160 // poison (handled in the loop above) *or* if both its true/false operands
8161 // are poison (handled here).
8162 if (I.getOpcode() == Instruction::Select &&
8163 YieldsPoison.count(I.getOperand(1)) &&
8164 YieldsPoison.count(I.getOperand(2))) {
8165 YieldsPoison.insert(&I);
8166 }
8167 }
8168
8169 BB = BB->getSingleSuccessor();
8170 if (!BB || !Visited.insert(BB).second)
8171 break;
8172
8173 Begin = BB->getFirstNonPHIIt();
8174 End = BB->end();
8175 }
8176 return false;
8177}
8178
8180 return ::programUndefinedIfUndefOrPoison(Inst, false);
8181}
8182
8184 return ::programUndefinedIfUndefOrPoison(Inst, true);
8185}
8186
8187static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8188 if (FMF.noNaNs())
8189 return true;
8190
8191 if (auto *C = dyn_cast<ConstantFP>(V))
8192 return !C->isNaN();
8193
8194 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8195 if (!C->getElementType()->isFloatingPointTy())
8196 return false;
8197 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8198 if (C->getElementAsAPFloat(I).isNaN())
8199 return false;
8200 }
8201 return true;
8202 }
8203
8205 return true;
8206
8207 return false;
8208}
8209
8210static bool isKnownNonZero(const Value *V) {
8211 if (auto *C = dyn_cast<ConstantFP>(V))
8212 return !C->isZero();
8213
8214 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8215 if (!C->getElementType()->isFloatingPointTy())
8216 return false;
8217 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8218 if (C->getElementAsAPFloat(I).isZero())
8219 return false;
8220 }
8221 return true;
8222 }
8223
8224 return false;
8225}
8226
8227/// Match clamp pattern for float types without care about NaNs or signed zeros.
8228/// Given non-min/max outer cmp/select from the clamp pattern this
8229/// function recognizes if it can be substitued by a "canonical" min/max
8230/// pattern.
8232 Value *CmpLHS, Value *CmpRHS,
8233 Value *TrueVal, Value *FalseVal,
8234 Value *&LHS, Value *&RHS) {
8235 // Try to match
8236 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8237 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8238 // and return description of the outer Max/Min.
8239
8240 // First, check if select has inverse order:
8241 if (CmpRHS == FalseVal) {
8242 std::swap(TrueVal, FalseVal);
8243 Pred = CmpInst::getInversePredicate(Pred);
8244 }
8245
8246 // Assume success now. If there's no match, callers should not use these anyway.
8247 LHS = TrueVal;
8248 RHS = FalseVal;
8249
8250 const APFloat *FC1;
8251 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8252 return {SPF_UNKNOWN, SPNB_NA, false};
8253
8254 const APFloat *FC2;
8255 switch (Pred) {
8256 case CmpInst::FCMP_OLT:
8257 case CmpInst::FCMP_OLE:
8258 case CmpInst::FCMP_ULT:
8259 case CmpInst::FCMP_ULE:
8260 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8261 *FC1 < *FC2)
8262 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8263 break;
8264 case CmpInst::FCMP_OGT:
8265 case CmpInst::FCMP_OGE:
8266 case CmpInst::FCMP_UGT:
8267 case CmpInst::FCMP_UGE:
8268 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8269 *FC1 > *FC2)
8270 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8271 break;
8272 default:
8273 break;
8274 }
8275
8276 return {SPF_UNKNOWN, SPNB_NA, false};
8277}
8278
8279/// Recognize variations of:
8280/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8282 Value *CmpLHS, Value *CmpRHS,
8283 Value *TrueVal, Value *FalseVal) {
8284 // Swap the select operands and predicate to match the patterns below.
8285 if (CmpRHS != TrueVal) {
8286 Pred = ICmpInst::getSwappedPredicate(Pred);
8287 std::swap(TrueVal, FalseVal);
8288 }
8289 const APInt *C1;
8290 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8291 const APInt *C2;
8292 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8293 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8294 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8295 return {SPF_SMAX, SPNB_NA, false};
8296
8297 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8298 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8299 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8300 return {SPF_SMIN, SPNB_NA, false};
8301
8302 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8303 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8304 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8305 return {SPF_UMAX, SPNB_NA, false};
8306
8307 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8308 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8309 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8310 return {SPF_UMIN, SPNB_NA, false};
8311 }
8312 return {SPF_UNKNOWN, SPNB_NA, false};
8313}
8314
8315/// Recognize variations of:
8316/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8318 Value *CmpLHS, Value *CmpRHS,
8319 Value *TVal, Value *FVal,
8320 unsigned Depth) {
8321 // TODO: Allow FP min/max with nnan/nsz.
8322 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8323
8324 Value *A = nullptr, *B = nullptr;
8325 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8326 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8327 return {SPF_UNKNOWN, SPNB_NA, false};
8328
8329 Value *C = nullptr, *D = nullptr;
8330 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8331 if (L.Flavor != R.Flavor)
8332 return {SPF_UNKNOWN, SPNB_NA, false};
8333
8334 // We have something like: x Pred y ? min(a, b) : min(c, d).
8335 // Try to match the compare to the min/max operations of the select operands.
8336 // First, make sure we have the right compare predicate.
8337 switch (L.Flavor) {
8338 case SPF_SMIN:
8339 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8340 Pred = ICmpInst::getSwappedPredicate(Pred);
8341 std::swap(CmpLHS, CmpRHS);
8342 }
8343 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8344 break;
8345 return {SPF_UNKNOWN, SPNB_NA, false};
8346 case SPF_SMAX:
8347 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8348 Pred = ICmpInst::getSwappedPredicate(Pred);
8349 std::swap(CmpLHS, CmpRHS);
8350 }
8351 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8352 break;
8353 return {SPF_UNKNOWN, SPNB_NA, false};
8354 case SPF_UMIN:
8355 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8356 Pred = ICmpInst::getSwappedPredicate(Pred);
8357 std::swap(CmpLHS, CmpRHS);
8358 }
8359 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8360 break;
8361 return {SPF_UNKNOWN, SPNB_NA, false};
8362 case SPF_UMAX:
8363 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8364 Pred = ICmpInst::getSwappedPredicate(Pred);
8365 std::swap(CmpLHS, CmpRHS);
8366 }
8367 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8368 break;
8369 return {SPF_UNKNOWN, SPNB_NA, false};
8370 default:
8371 return {SPF_UNKNOWN, SPNB_NA, false};
8372 }
8373
8374 // If there is a common operand in the already matched min/max and the other
8375 // min/max operands match the compare operands (either directly or inverted),
8376 // then this is min/max of the same flavor.
8377
8378 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8379 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8380 if (D == B) {
8381 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8382 match(A, m_Not(m_Specific(CmpRHS)))))
8383 return {L.Flavor, SPNB_NA, false};
8384 }
8385 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8386 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8387 if (C == B) {
8388 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8389 match(A, m_Not(m_Specific(CmpRHS)))))
8390 return {L.Flavor, SPNB_NA, false};
8391 }
8392 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8393 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8394 if (D == A) {
8395 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8396 match(B, m_Not(m_Specific(CmpRHS)))))
8397 return {L.Flavor, SPNB_NA, false};
8398 }
8399 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8400 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8401 if (C == A) {
8402 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8403 match(B, m_Not(m_Specific(CmpRHS)))))
8404 return {L.Flavor, SPNB_NA, false};
8405 }
8406
8407 return {SPF_UNKNOWN, SPNB_NA, false};
8408}
8409
8410/// If the input value is the result of a 'not' op, constant integer, or vector
8411/// splat of a constant integer, return the bitwise-not source value.
8412/// TODO: This could be extended to handle non-splat vector integer constants.
8414 Value *NotV;
8415 if (match(V, m_Not(m_Value(NotV))))
8416 return NotV;
8417
8418 const APInt *C;
8419 if (match(V, m_APInt(C)))
8420 return ConstantInt::get(V->getType(), ~(*C));
8421
8422 return nullptr;
8423}
8424
8425/// Match non-obvious integer minimum and maximum sequences.
8427 Value *CmpLHS, Value *CmpRHS,
8428 Value *TrueVal, Value *FalseVal,
8429 Value *&LHS, Value *&RHS,
8430 unsigned Depth) {
8431 // Assume success. If there's no match, callers should not use these anyway.
8432 LHS = TrueVal;
8433 RHS = FalseVal;
8434
8435 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8437 return SPR;
8438
8439 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8441 return SPR;
8442
8443 // Look through 'not' ops to find disguised min/max.
8444 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8445 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8446 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8447 switch (Pred) {
8448 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8449 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8450 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8451 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8452 default: break;
8453 }
8454 }
8455
8456 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8457 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8458 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8459 switch (Pred) {
8460 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8461 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8462 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8463 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8464 default: break;
8465 }
8466 }
8467
8468 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8469 return {SPF_UNKNOWN, SPNB_NA, false};
8470
8471 const APInt *C1;
8472 if (!match(CmpRHS, m_APInt(C1)))
8473 return {SPF_UNKNOWN, SPNB_NA, false};
8474
8475 // An unsigned min/max can be written with a signed compare.
8476 const APInt *C2;
8477 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8478 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8479 // Is the sign bit set?
8480 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8481 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8482 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8483 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8484
8485 // Is the sign bit clear?
8486 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8487 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8488 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8489 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8490 }
8491
8492 return {SPF_UNKNOWN, SPNB_NA, false};
8493}
8494
8495bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8496 bool AllowPoison) {
8497 assert(X && Y && "Invalid operand");
8498
8499 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8500 if (!match(X, m_Neg(m_Specific(Y))))
8501 return false;
8502
8503 auto *BO = cast<BinaryOperator>(X);
8504 if (NeedNSW && !BO->hasNoSignedWrap())
8505 return false;
8506
8507 auto *Zero = cast<Constant>(BO->getOperand(0));
8508 if (!AllowPoison && !Zero->isNullValue())
8509 return false;
8510
8511 return true;
8512 };
8513
8514 // X = -Y or Y = -X
8515 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8516 return true;
8517
8518 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8519 Value *A, *B;
8520 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8521 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8522 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8524}
8525
8526bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8527 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8528 Value *A, *B, *C;
8529 CmpPredicate Pred1, Pred2;
8530 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8531 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8532 return false;
8533
8534 // They must both have samesign flag or not.
8535 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8536 return false;
8537
8538 if (B == C)
8539 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8540
8541 // Try to infer the relationship from constant ranges.
8542 const APInt *RHSC1, *RHSC2;
8543 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8544 return false;
8545
8546 // Sign bits of two RHSCs should match.
8547 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8548 return false;
8549
8550 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8551 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8552
8553 return CR1.inverse() == CR2;
8554}
8555
8557 SelectPatternNaNBehavior NaNBehavior,
8558 bool Ordered) {
8559 switch (Pred) {
8560 default:
8561 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8562 case ICmpInst::ICMP_UGT:
8563 case ICmpInst::ICMP_UGE:
8564 return {SPF_UMAX, SPNB_NA, false};
8565 case ICmpInst::ICMP_SGT:
8566 case ICmpInst::ICMP_SGE:
8567 return {SPF_SMAX, SPNB_NA, false};
8568 case ICmpInst::ICMP_ULT:
8569 case ICmpInst::ICMP_ULE:
8570 return {SPF_UMIN, SPNB_NA, false};
8571 case ICmpInst::ICMP_SLT:
8572 case ICmpInst::ICMP_SLE:
8573 return {SPF_SMIN, SPNB_NA, false};
8574 case FCmpInst::FCMP_UGT:
8575 case FCmpInst::FCMP_UGE:
8576 case FCmpInst::FCMP_OGT:
8577 case FCmpInst::FCMP_OGE:
8578 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8579 case FCmpInst::FCMP_ULT:
8580 case FCmpInst::FCMP_ULE:
8581 case FCmpInst::FCMP_OLT:
8582 case FCmpInst::FCMP_OLE:
8583 return {SPF_FMINNUM, NaNBehavior, Ordered};
8584 }
8585}
8586
8587std::optional<std::pair<CmpPredicate, Constant *>>
8590 "Only for relational integer predicates.");
8591 if (isa<UndefValue>(C))
8592 return std::nullopt;
8593
8594 Type *Type = C->getType();
8595 bool IsSigned = ICmpInst::isSigned(Pred);
8596
8598 bool WillIncrement =
8599 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8600
8601 // Check if the constant operand can be safely incremented/decremented
8602 // without overflowing/underflowing.
8603 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8604 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8605 };
8606
8607 Constant *SafeReplacementConstant = nullptr;
8608 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8609 // Bail out if the constant can't be safely incremented/decremented.
8610 if (!ConstantIsOk(CI))
8611 return std::nullopt;
8612 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8613 unsigned NumElts = FVTy->getNumElements();
8614 for (unsigned i = 0; i != NumElts; ++i) {
8615 Constant *Elt = C->getAggregateElement(i);
8616 if (!Elt)
8617 return std::nullopt;
8618
8619 if (isa<UndefValue>(Elt))
8620 continue;
8621
8622 // Bail out if we can't determine if this constant is min/max or if we
8623 // know that this constant is min/max.
8624 auto *CI = dyn_cast<ConstantInt>(Elt);
8625 if (!CI || !ConstantIsOk(CI))
8626 return std::nullopt;
8627
8628 if (!SafeReplacementConstant)
8629 SafeReplacementConstant = CI;
8630 }
8631 } else if (isa<VectorType>(C->getType())) {
8632 // Handle scalable splat
8633 Value *SplatC = C->getSplatValue();
8634 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8635 // Bail out if the constant can't be safely incremented/decremented.
8636 if (!CI || !ConstantIsOk(CI))
8637 return std::nullopt;
8638 } else {
8639 // ConstantExpr?
8640 return std::nullopt;
8641 }
8642
8643 // It may not be safe to change a compare predicate in the presence of
8644 // undefined elements, so replace those elements with the first safe constant
8645 // that we found.
8646 // TODO: in case of poison, it is safe; let's replace undefs only.
8647 if (C->containsUndefOrPoisonElement()) {
8648 assert(SafeReplacementConstant && "Replacement constant not set");
8649 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8650 }
8651
8653
8654 // Increment or decrement the constant.
8655 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8656 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8657
8658 return std::make_pair(NewPred, NewC);
8659}
8660
8662 FastMathFlags FMF,
8663 Value *CmpLHS, Value *CmpRHS,
8664 Value *TrueVal, Value *FalseVal,
8665 Value *&LHS, Value *&RHS,
8666 unsigned Depth) {
8667 bool HasMismatchedZeros = false;
8668 if (CmpInst::isFPPredicate(Pred)) {
8669 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8670 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8671 // purpose of identifying min/max. Disregard vector constants with undefined
8672 // elements because those can not be back-propagated for analysis.
8673 Value *OutputZeroVal = nullptr;
8674 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8675 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8676 OutputZeroVal = TrueVal;
8677 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8678 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8679 OutputZeroVal = FalseVal;
8680
8681 if (OutputZeroVal) {
8682 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8683 HasMismatchedZeros = true;
8684 CmpLHS = OutputZeroVal;
8685 }
8686 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8687 HasMismatchedZeros = true;
8688 CmpRHS = OutputZeroVal;
8689 }
8690 }
8691 }
8692
8693 LHS = CmpLHS;
8694 RHS = CmpRHS;
8695
8696 // Signed zero may return inconsistent results between implementations.
8697 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8698 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8699 // Therefore, we behave conservatively and only proceed if at least one of the
8700 // operands is known to not be zero or if we don't care about signed zero.
8701 switch (Pred) {
8702 default: break;
8705 if (!HasMismatchedZeros)
8706 break;
8707 [[fallthrough]];
8710 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8711 !isKnownNonZero(CmpRHS))
8712 return {SPF_UNKNOWN, SPNB_NA, false};
8713 }
8714
8715 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8716 bool Ordered = false;
8717
8718 // When given one NaN and one non-NaN input:
8719 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8720 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8721 // ordered comparison fails), which could be NaN or non-NaN.
8722 // so here we discover exactly what NaN behavior is required/accepted.
8723 if (CmpInst::isFPPredicate(Pred)) {
8724 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8725 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8726
8727 if (LHSSafe && RHSSafe) {
8728 // Both operands are known non-NaN.
8729 NaNBehavior = SPNB_RETURNS_ANY;
8730 Ordered = CmpInst::isOrdered(Pred);
8731 } else if (CmpInst::isOrdered(Pred)) {
8732 // An ordered comparison will return false when given a NaN, so it
8733 // returns the RHS.
8734 Ordered = true;
8735 if (LHSSafe)
8736 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8737 NaNBehavior = SPNB_RETURNS_NAN;
8738 else if (RHSSafe)
8739 NaNBehavior = SPNB_RETURNS_OTHER;
8740 else
8741 // Completely unsafe.
8742 return {SPF_UNKNOWN, SPNB_NA, false};
8743 } else {
8744 Ordered = false;
8745 // An unordered comparison will return true when given a NaN, so it
8746 // returns the LHS.
8747 if (LHSSafe)
8748 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8749 NaNBehavior = SPNB_RETURNS_OTHER;
8750 else if (RHSSafe)
8751 NaNBehavior = SPNB_RETURNS_NAN;
8752 else
8753 // Completely unsafe.
8754 return {SPF_UNKNOWN, SPNB_NA, false};
8755 }
8756 }
8757
8758 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8759 std::swap(CmpLHS, CmpRHS);
8760 Pred = CmpInst::getSwappedPredicate(Pred);
8761 if (NaNBehavior == SPNB_RETURNS_NAN)
8762 NaNBehavior = SPNB_RETURNS_OTHER;
8763 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8764 NaNBehavior = SPNB_RETURNS_NAN;
8765 Ordered = !Ordered;
8766 }
8767
8768 // ([if]cmp X, Y) ? X : Y
8769 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8770 return getSelectPattern(Pred, NaNBehavior, Ordered);
8771
8772 if (isKnownNegation(TrueVal, FalseVal)) {
8773 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8774 // match against either LHS or sext(LHS).
8775 auto MaybeSExtCmpLHS =
8776 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8777 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8778 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8779 if (match(TrueVal, MaybeSExtCmpLHS)) {
8780 // Set the return values. If the compare uses the negated value (-X >s 0),
8781 // swap the return values because the negated value is always 'RHS'.
8782 LHS = TrueVal;
8783 RHS = FalseVal;
8784 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8785 std::swap(LHS, RHS);
8786
8787 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8788 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8789 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8790 return {SPF_ABS, SPNB_NA, false};
8791
8792 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8793 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8794 return {SPF_ABS, SPNB_NA, false};
8795
8796 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8797 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8798 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8799 return {SPF_NABS, SPNB_NA, false};
8800 }
8801 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8802 // Set the return values. If the compare uses the negated value (-X >s 0),
8803 // swap the return values because the negated value is always 'RHS'.
8804 LHS = FalseVal;
8805 RHS = TrueVal;
8806 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8807 std::swap(LHS, RHS);
8808
8809 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8810 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8811 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8812 return {SPF_NABS, SPNB_NA, false};
8813
8814 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8815 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8816 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8817 return {SPF_ABS, SPNB_NA, false};
8818 }
8819 }
8820
8821 if (CmpInst::isIntPredicate(Pred))
8822 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8823
8824 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8825 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8826 // semantics than minNum. Be conservative in such case.
8827 if (NaNBehavior != SPNB_RETURNS_ANY ||
8828 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8829 !isKnownNonZero(CmpRHS)))
8830 return {SPF_UNKNOWN, SPNB_NA, false};
8831
8832 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8833}
8834
8836 Instruction::CastOps *CastOp) {
8837 const DataLayout &DL = CmpI->getDataLayout();
8838
8839 Constant *CastedTo = nullptr;
8840 switch (*CastOp) {
8841 case Instruction::ZExt:
8842 if (CmpI->isUnsigned())
8843 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8844 break;
8845 case Instruction::SExt:
8846 if (CmpI->isSigned())
8847 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8848 break;
8849 case Instruction::Trunc:
8850 Constant *CmpConst;
8851 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8852 CmpConst->getType() == SrcTy) {
8853 // Here we have the following case:
8854 //
8855 // %cond = cmp iN %x, CmpConst
8856 // %tr = trunc iN %x to iK
8857 // %narrowsel = select i1 %cond, iK %t, iK C
8858 //
8859 // We can always move trunc after select operation:
8860 //
8861 // %cond = cmp iN %x, CmpConst
8862 // %widesel = select i1 %cond, iN %x, iN CmpConst
8863 // %tr = trunc iN %widesel to iK
8864 //
8865 // Note that C could be extended in any way because we don't care about
8866 // upper bits after truncation. It can't be abs pattern, because it would
8867 // look like:
8868 //
8869 // select i1 %cond, x, -x.
8870 //
8871 // So only min/max pattern could be matched. Such match requires widened C
8872 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8873 // CmpConst == C is checked below.
8874 CastedTo = CmpConst;
8875 } else {
8876 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8877 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8878 }
8879 break;
8880 case Instruction::FPTrunc:
8881 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8882 break;
8883 case Instruction::FPExt:
8884 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8885 break;
8886 case Instruction::FPToUI:
8887 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8888 break;
8889 case Instruction::FPToSI:
8890 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8891 break;
8892 case Instruction::UIToFP:
8893 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8894 break;
8895 case Instruction::SIToFP:
8896 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8897 break;
8898 default:
8899 break;
8900 }
8901
8902 if (!CastedTo)
8903 return nullptr;
8904
8905 // Make sure the cast doesn't lose any information.
8906 Constant *CastedBack =
8907 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8908 if (CastedBack && CastedBack != C)
8909 return nullptr;
8910
8911 return CastedTo;
8912}
8913
8914/// Helps to match a select pattern in case of a type mismatch.
8915///
8916/// The function processes the case when type of true and false values of a
8917/// select instruction differs from type of the cmp instruction operands because
8918/// of a cast instruction. The function checks if it is legal to move the cast
8919/// operation after "select". If yes, it returns the new second value of
8920/// "select" (with the assumption that cast is moved):
8921/// 1. As operand of cast instruction when both values of "select" are same cast
8922/// instructions.
8923/// 2. As restored constant (by applying reverse cast operation) when the first
8924/// value of the "select" is a cast operation and the second value is a
8925/// constant. It is implemented in lookThroughCastConst().
8926/// 3. As one operand is cast instruction and the other is not. The operands in
8927/// sel(cmp) are in different type integer.
8928/// NOTE: We return only the new second value because the first value could be
8929/// accessed as operand of cast instruction.
8930static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8931 Instruction::CastOps *CastOp) {
8932 auto *Cast1 = dyn_cast<CastInst>(V1);
8933 if (!Cast1)
8934 return nullptr;
8935
8936 *CastOp = Cast1->getOpcode();
8937 Type *SrcTy = Cast1->getSrcTy();
8938 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8939 // If V1 and V2 are both the same cast from the same type, look through V1.
8940 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
8941 return Cast2->getOperand(0);
8942 return nullptr;
8943 }
8944
8945 auto *C = dyn_cast<Constant>(V2);
8946 if (C)
8947 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
8948
8949 Value *CastedTo = nullptr;
8950 if (*CastOp == Instruction::Trunc) {
8951 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
8952 // Here we have the following case:
8953 // %y_ext = sext iK %y to iN
8954 // %cond = cmp iN %x, %y_ext
8955 // %tr = trunc iN %x to iK
8956 // %narrowsel = select i1 %cond, iK %tr, iK %y
8957 //
8958 // We can always move trunc after select operation:
8959 // %y_ext = sext iK %y to iN
8960 // %cond = cmp iN %x, %y_ext
8961 // %widesel = select i1 %cond, iN %x, iN %y_ext
8962 // %tr = trunc iN %widesel to iK
8963 assert(V2->getType() == Cast1->getType() &&
8964 "V2 and Cast1 should be the same type.");
8965 CastedTo = CmpI->getOperand(1);
8966 }
8967 }
8968
8969 return CastedTo;
8970}
8972 Instruction::CastOps *CastOp,
8973 unsigned Depth) {
8975 return {SPF_UNKNOWN, SPNB_NA, false};
8976
8978 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
8979
8980 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
8981 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
8982
8983 Value *TrueVal = SI->getTrueValue();
8984 Value *FalseVal = SI->getFalseValue();
8985
8987 CmpI, TrueVal, FalseVal, LHS, RHS,
8988 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
8989 CastOp, Depth);
8990}
8991
8993 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
8994 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
8995 CmpInst::Predicate Pred = CmpI->getPredicate();
8996 Value *CmpLHS = CmpI->getOperand(0);
8997 Value *CmpRHS = CmpI->getOperand(1);
8998 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
8999 FMF.setNoNaNs();
9000
9001 // Bail out early.
9002 if (CmpI->isEquality())
9003 return {SPF_UNKNOWN, SPNB_NA, false};
9004
9005 // Deal with type mismatches.
9006 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9007 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9008 // If this is a potential fmin/fmax with a cast to integer, then ignore
9009 // -0.0 because there is no corresponding integer value.
9010 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9011 FMF.setNoSignedZeros();
9012 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9013 cast<CastInst>(TrueVal)->getOperand(0), C,
9014 LHS, RHS, Depth);
9015 }
9016 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9017 // If this is a potential fmin/fmax with a cast to integer, then ignore
9018 // -0.0 because there is no corresponding integer value.
9019 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9020 FMF.setNoSignedZeros();
9021 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9022 C, cast<CastInst>(FalseVal)->getOperand(0),
9023 LHS, RHS, Depth);
9024 }
9025 }
9026 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9027 LHS, RHS, Depth);
9028}
9029
9031 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9032 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9033 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9034 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9035 if (SPF == SPF_FMINNUM)
9036 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9037 if (SPF == SPF_FMAXNUM)
9038 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9039 llvm_unreachable("unhandled!");
9040}
9041
9043 switch (SPF) {
9045 return Intrinsic::umin;
9047 return Intrinsic::umax;
9049 return Intrinsic::smin;
9051 return Intrinsic::smax;
9052 default:
9053 llvm_unreachable("Unexpected SPF");
9054 }
9055}
9056
9058 if (SPF == SPF_SMIN) return SPF_SMAX;
9059 if (SPF == SPF_UMIN) return SPF_UMAX;
9060 if (SPF == SPF_SMAX) return SPF_SMIN;
9061 if (SPF == SPF_UMAX) return SPF_UMIN;
9062 llvm_unreachable("unhandled!");
9063}
9064
9066 switch (MinMaxID) {
9067 case Intrinsic::smax: return Intrinsic::smin;
9068 case Intrinsic::smin: return Intrinsic::smax;
9069 case Intrinsic::umax: return Intrinsic::umin;
9070 case Intrinsic::umin: return Intrinsic::umax;
9071 // Please note that next four intrinsics may produce the same result for
9072 // original and inverted case even if X != Y due to NaN is handled specially.
9073 case Intrinsic::maximum: return Intrinsic::minimum;
9074 case Intrinsic::minimum: return Intrinsic::maximum;
9075 case Intrinsic::maxnum: return Intrinsic::minnum;
9076 case Intrinsic::minnum: return Intrinsic::maxnum;
9077 default: llvm_unreachable("Unexpected intrinsic");
9078 }
9079}
9080
9082 switch (SPF) {
9085 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9086 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9087 default: llvm_unreachable("Unexpected flavor");
9088 }
9089}
9090
9091std::pair<Intrinsic::ID, bool>
9093 // Check if VL contains select instructions that can be folded into a min/max
9094 // vector intrinsic and return the intrinsic if it is possible.
9095 // TODO: Support floating point min/max.
9096 bool AllCmpSingleUse = true;
9097 SelectPatternResult SelectPattern;
9098 SelectPattern.Flavor = SPF_UNKNOWN;
9099 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9100 Value *LHS, *RHS;
9101 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9102 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9103 return false;
9104 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9105 SelectPattern.Flavor != CurrentPattern.Flavor)
9106 return false;
9107 SelectPattern = CurrentPattern;
9108 AllCmpSingleUse &=
9110 return true;
9111 })) {
9112 switch (SelectPattern.Flavor) {
9113 case SPF_SMIN:
9114 return {Intrinsic::smin, AllCmpSingleUse};
9115 case SPF_UMIN:
9116 return {Intrinsic::umin, AllCmpSingleUse};
9117 case SPF_SMAX:
9118 return {Intrinsic::smax, AllCmpSingleUse};
9119 case SPF_UMAX:
9120 return {Intrinsic::umax, AllCmpSingleUse};
9121 case SPF_FMAXNUM:
9122 return {Intrinsic::maxnum, AllCmpSingleUse};
9123 case SPF_FMINNUM:
9124 return {Intrinsic::minnum, AllCmpSingleUse};
9125 default:
9126 llvm_unreachable("unexpected select pattern flavor");
9127 }
9128 }
9129 return {Intrinsic::not_intrinsic, false};
9130}
9131
9132template <typename InstTy>
9133static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9134 Value *&Init, Value *&OtherOp) {
9135 // Handle the case of a simple two-predecessor recurrence PHI.
9136 // There's a lot more that could theoretically be done here, but
9137 // this is sufficient to catch some interesting cases.
9138 // TODO: Expand list -- gep, uadd.sat etc.
9139 if (PN->getNumIncomingValues() != 2)
9140 return false;
9141
9142 for (unsigned I = 0; I != 2; ++I) {
9143 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9144 Operation && Operation->getNumOperands() >= 2) {
9145 Value *LHS = Operation->getOperand(0);
9146 Value *RHS = Operation->getOperand(1);
9147 if (LHS != PN && RHS != PN)
9148 continue;
9149
9150 Inst = Operation;
9151 Init = PN->getIncomingValue(!I);
9152 OtherOp = (LHS == PN) ? RHS : LHS;
9153 return true;
9154 }
9155 }
9156 return false;
9157}
9158
9160 Value *&Start, Value *&Step) {
9161 // We try to match a recurrence of the form:
9162 // %iv = [Start, %entry], [%iv.next, %backedge]
9163 // %iv.next = binop %iv, Step
9164 // Or:
9165 // %iv = [Start, %entry], [%iv.next, %backedge]
9166 // %iv.next = binop Step, %iv
9167 return matchTwoInputRecurrence(P, BO, Start, Step);
9168}
9169
9171 Value *&Start, Value *&Step) {
9172 BinaryOperator *BO = nullptr;
9173 P = dyn_cast<PHINode>(I->getOperand(0));
9174 if (!P)
9175 P = dyn_cast<PHINode>(I->getOperand(1));
9176 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9177}
9178
9180 PHINode *&P, Value *&Init,
9181 Value *&OtherOp) {
9182 // Binary intrinsics only supported for now.
9183 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9184 I->getType() != I->getArgOperand(1)->getType())
9185 return false;
9186
9187 IntrinsicInst *II = nullptr;
9188 P = dyn_cast<PHINode>(I->getArgOperand(0));
9189 if (!P)
9190 P = dyn_cast<PHINode>(I->getArgOperand(1));
9191
9192 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9193}
9194
9195/// Return true if "icmp Pred LHS RHS" is always true.
9197 const Value *RHS) {
9198 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9199 return true;
9200
9201 switch (Pred) {
9202 default:
9203 return false;
9204
9205 case CmpInst::ICMP_SLE: {
9206 const APInt *C;
9207
9208 // LHS s<= LHS +_{nsw} C if C >= 0
9209 // LHS s<= LHS | C if C >= 0
9210 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9212 return !C->isNegative();
9213
9214 // LHS s<= smax(LHS, V) for any V
9216 return true;
9217
9218 // smin(RHS, V) s<= RHS for any V
9220 return true;
9221
9222 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9223 const Value *X;
9224 const APInt *CLHS, *CRHS;
9225 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9227 return CLHS->sle(*CRHS);
9228
9229 return false;
9230 }
9231
9232 case CmpInst::ICMP_ULE: {
9233 // LHS u<= LHS +_{nuw} V for any V
9234 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9236 return true;
9237
9238 // LHS u<= LHS | V for any V
9239 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9240 return true;
9241
9242 // LHS u<= umax(LHS, V) for any V
9244 return true;
9245
9246 // RHS >> V u<= RHS for any V
9247 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9248 return true;
9249
9250 // RHS u/ C_ugt_1 u<= RHS
9251 const APInt *C;
9252 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9253 return true;
9254
9255 // RHS & V u<= RHS for any V
9257 return true;
9258
9259 // umin(RHS, V) u<= RHS for any V
9261 return true;
9262
9263 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9264 const Value *X;
9265 const APInt *CLHS, *CRHS;
9266 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9268 return CLHS->ule(*CRHS);
9269
9270 return false;
9271 }
9272 }
9273}
9274
9275/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9276/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9277static std::optional<bool>
9279 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9280 switch (Pred) {
9281 default:
9282 return std::nullopt;
9283
9284 case CmpInst::ICMP_SLT:
9285 case CmpInst::ICMP_SLE:
9286 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9288 return true;
9289 return std::nullopt;
9290
9291 case CmpInst::ICMP_SGT:
9292 case CmpInst::ICMP_SGE:
9293 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9295 return true;
9296 return std::nullopt;
9297
9298 case CmpInst::ICMP_ULT:
9299 case CmpInst::ICMP_ULE:
9300 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9302 return true;
9303 return std::nullopt;
9304
9305 case CmpInst::ICMP_UGT:
9306 case CmpInst::ICMP_UGE:
9307 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9309 return true;
9310 return std::nullopt;
9311 }
9312}
9313
9314/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9315/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9316/// Otherwise, return std::nullopt if we can't infer anything.
9317static std::optional<bool>
9319 CmpPredicate RPred, const ConstantRange &RCR) {
9320 auto CRImpliesPred = [&](ConstantRange CR,
9321 CmpInst::Predicate Pred) -> std::optional<bool> {
9322 // If all true values for lhs and true for rhs, lhs implies rhs
9323 if (CR.icmp(Pred, RCR))
9324 return true;
9325
9326 // If there is no overlap, lhs implies not rhs
9327 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9328 return false;
9329
9330 return std::nullopt;
9331 };
9332 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9333 RPred))
9334 return Res;
9335 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9337 : LPred.dropSameSign();
9339 : RPred.dropSameSign();
9340 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9341 RPred);
9342 }
9343 return std::nullopt;
9344}
9345
9346/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9347/// is true. Return false if LHS implies RHS is false. Otherwise, return
9348/// std::nullopt if we can't infer anything.
9349static std::optional<bool>
9350isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9351 CmpPredicate RPred, const Value *R0, const Value *R1,
9352 const DataLayout &DL, bool LHSIsTrue) {
9353 // The rest of the logic assumes the LHS condition is true. If that's not the
9354 // case, invert the predicate to make it so.
9355 if (!LHSIsTrue)
9356 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9357
9358 // We can have non-canonical operands, so try to normalize any common operand
9359 // to L0/R0.
9360 if (L0 == R1) {
9361 std::swap(R0, R1);
9362 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9363 }
9364 if (R0 == L1) {
9365 std::swap(L0, L1);
9366 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9367 }
9368 if (L1 == R1) {
9369 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9370 if (L0 != R0 || match(L0, m_ImmConstant())) {
9371 std::swap(L0, L1);
9372 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9373 std::swap(R0, R1);
9374 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9375 }
9376 }
9377
9378 // See if we can infer anything if operand-0 matches and we have at least one
9379 // constant.
9380 const APInt *Unused;
9381 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9382 // Potential TODO: We could also further use the constant range of L0/R0 to
9383 // further constraint the constant ranges. At the moment this leads to
9384 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9385 // C1` (see discussion: D58633).
9387 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9388 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9390 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9391 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9392 // Even if L1/R1 are not both constant, we can still sometimes deduce
9393 // relationship from a single constant. For example X u> Y implies X != 0.
9394 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9395 return R;
9396 // If both L1/R1 were exact constant ranges and we didn't get anything
9397 // here, we won't be able to deduce this.
9398 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9399 return std::nullopt;
9400 }
9401
9402 // Can we infer anything when the two compares have matching operands?
9403 if (L0 == R0 && L1 == R1)
9404 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9405
9406 // It only really makes sense in the context of signed comparison for "X - Y
9407 // must be positive if X >= Y and no overflow".
9408 // Take SGT as an example: L0:x > L1:y and C >= 0
9409 // ==> R0:(x -nsw y) < R1:(-C) is false
9410 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9411 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9412 SignedLPred == ICmpInst::ICMP_SGE) &&
9413 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9414 if (match(R1, m_NonPositive()) &&
9415 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9416 return false;
9417 }
9418
9419 // Take SLT as an example: L0:x < L1:y and C <= 0
9420 // ==> R0:(x -nsw y) < R1:(-C) is true
9421 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9422 SignedLPred == ICmpInst::ICMP_SLE) &&
9423 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9424 if (match(R1, m_NonNegative()) &&
9425 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9426 return true;
9427 }
9428
9429 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9430 if (L0 == R0 &&
9431 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9432 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9433 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9434 return CmpPredicate::getMatching(LPred, RPred).has_value();
9435
9436 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9437 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9438
9439 return std::nullopt;
9440}
9441
9442/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9443/// false. Otherwise, return std::nullopt if we can't infer anything. We
9444/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9445/// instruction.
9446static std::optional<bool>
9448 const Value *RHSOp0, const Value *RHSOp1,
9449 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9450 // The LHS must be an 'or', 'and', or a 'select' instruction.
9451 assert((LHS->getOpcode() == Instruction::And ||
9452 LHS->getOpcode() == Instruction::Or ||
9453 LHS->getOpcode() == Instruction::Select) &&
9454 "Expected LHS to be 'and', 'or', or 'select'.");
9455
9456 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9457
9458 // If the result of an 'or' is false, then we know both legs of the 'or' are
9459 // false. Similarly, if the result of an 'and' is true, then we know both
9460 // legs of the 'and' are true.
9461 const Value *ALHS, *ARHS;
9462 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9463 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9464 // FIXME: Make this non-recursion.
9465 if (std::optional<bool> Implication = isImpliedCondition(
9466 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9467 return Implication;
9468 if (std::optional<bool> Implication = isImpliedCondition(
9469 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9470 return Implication;
9471 return std::nullopt;
9472 }
9473 return std::nullopt;
9474}
9475
9476std::optional<bool>
9478 const Value *RHSOp0, const Value *RHSOp1,
9479 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9480 // Bail out when we hit the limit.
9482 return std::nullopt;
9483
9484 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9485 // example.
9486 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9487 return std::nullopt;
9488
9489 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9490 "Expected integer type only!");
9491
9492 // Match not
9493 if (match(LHS, m_Not(m_Value(LHS))))
9494 LHSIsTrue = !LHSIsTrue;
9495
9496 // Both LHS and RHS are icmps.
9497 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9498 return isImpliedCondICmps(LHSCmp->getCmpPredicate(), LHSCmp->getOperand(0),
9499 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9500 DL, LHSIsTrue);
9501 const Value *V;
9502 if (match(LHS, m_NUWTrunc(m_Value(V))))
9504 ConstantInt::get(V->getType(), 0), RHSPred,
9505 RHSOp0, RHSOp1, DL, LHSIsTrue);
9506
9507 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9508 /// the RHS to be an icmp.
9509 /// FIXME: Add support for and/or/select on the RHS.
9510 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9511 if ((LHSI->getOpcode() == Instruction::And ||
9512 LHSI->getOpcode() == Instruction::Or ||
9513 LHSI->getOpcode() == Instruction::Select))
9514 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9515 Depth);
9516 }
9517 return std::nullopt;
9518}
9519
9520std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9521 const DataLayout &DL,
9522 bool LHSIsTrue, unsigned Depth) {
9523 // LHS ==> RHS by definition
9524 if (LHS == RHS)
9525 return LHSIsTrue;
9526
9527 // Match not
9528 bool InvertRHS = false;
9529 if (match(RHS, m_Not(m_Value(RHS)))) {
9530 if (LHS == RHS)
9531 return !LHSIsTrue;
9532 InvertRHS = true;
9533 }
9534
9535 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9536 if (auto Implied = isImpliedCondition(
9537 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9538 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9539 return InvertRHS ? !*Implied : *Implied;
9540 return std::nullopt;
9541 }
9542
9543 const Value *V;
9544 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9545 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9546 ConstantInt::get(V->getType(), 0), DL,
9547 LHSIsTrue, Depth))
9548 return InvertRHS ? !*Implied : *Implied;
9549 return std::nullopt;
9550 }
9551
9553 return std::nullopt;
9554
9555 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9556 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9557 const Value *RHS1, *RHS2;
9558 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9559 if (std::optional<bool> Imp =
9560 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9561 if (*Imp == true)
9562 return !InvertRHS;
9563 if (std::optional<bool> Imp =
9564 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9565 if (*Imp == true)
9566 return !InvertRHS;
9567 }
9568 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9569 if (std::optional<bool> Imp =
9570 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9571 if (*Imp == false)
9572 return InvertRHS;
9573 if (std::optional<bool> Imp =
9574 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9575 if (*Imp == false)
9576 return InvertRHS;
9577 }
9578
9579 return std::nullopt;
9580}
9581
9582// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9583// condition dominating ContextI or nullptr, if no condition is found.
9584static std::pair<Value *, bool>
9586 if (!ContextI || !ContextI->getParent())
9587 return {nullptr, false};
9588
9589 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9590 // dominator tree (eg, from a SimplifyQuery) instead?
9591 const BasicBlock *ContextBB = ContextI->getParent();
9592 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9593 if (!PredBB)
9594 return {nullptr, false};
9595
9596 // We need a conditional branch in the predecessor.
9597 Value *PredCond;
9598 BasicBlock *TrueBB, *FalseBB;
9599 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9600 return {nullptr, false};
9601
9602 // The branch should get simplified. Don't bother simplifying this condition.
9603 if (TrueBB == FalseBB)
9604 return {nullptr, false};
9605
9606 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9607 "Predecessor block does not point to successor?");
9608
9609 // Is this condition implied by the predecessor condition?
9610 return {PredCond, TrueBB == ContextBB};
9611}
9612
9613std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9614 const Instruction *ContextI,
9615 const DataLayout &DL) {
9616 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9617 auto PredCond = getDomPredecessorCondition(ContextI);
9618 if (PredCond.first)
9619 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9620 return std::nullopt;
9621}
9622
9624 const Value *LHS,
9625 const Value *RHS,
9626 const Instruction *ContextI,
9627 const DataLayout &DL) {
9628 auto PredCond = getDomPredecessorCondition(ContextI);
9629 if (PredCond.first)
9630 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9631 PredCond.second);
9632 return std::nullopt;
9633}
9634
9636 APInt &Upper, const InstrInfoQuery &IIQ,
9637 bool PreferSignedRange) {
9638 unsigned Width = Lower.getBitWidth();
9639 const APInt *C;
9640 switch (BO.getOpcode()) {
9641 case Instruction::Sub:
9642 if (match(BO.getOperand(0), m_APInt(C))) {
9643 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9644 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9645
9646 // If the caller expects a signed compare, then try to use a signed range.
9647 // Otherwise if both no-wraps are set, use the unsigned range because it
9648 // is never larger than the signed range. Example:
9649 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9650 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9651 if (PreferSignedRange && HasNSW && HasNUW)
9652 HasNUW = false;
9653
9654 if (HasNUW) {
9655 // 'sub nuw c, x' produces [0, C].
9656 Upper = *C + 1;
9657 } else if (HasNSW) {
9658 if (C->isNegative()) {
9659 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9661 Upper = *C - APInt::getSignedMaxValue(Width);
9662 } else {
9663 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9664 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9665 Lower = *C - APInt::getSignedMaxValue(Width);
9667 }
9668 }
9669 }
9670 break;
9671 case Instruction::Add:
9672 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9673 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9674 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9675
9676 // If the caller expects a signed compare, then try to use a signed
9677 // range. Otherwise if both no-wraps are set, use the unsigned range
9678 // because it is never larger than the signed range. Example: "add nuw
9679 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9680 if (PreferSignedRange && HasNSW && HasNUW)
9681 HasNUW = false;
9682
9683 if (HasNUW) {
9684 // 'add nuw x, C' produces [C, UINT_MAX].
9685 Lower = *C;
9686 } else if (HasNSW) {
9687 if (C->isNegative()) {
9688 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9690 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9691 } else {
9692 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9693 Lower = APInt::getSignedMinValue(Width) + *C;
9694 Upper = APInt::getSignedMaxValue(Width) + 1;
9695 }
9696 }
9697 }
9698 break;
9699
9700 case Instruction::And:
9701 if (match(BO.getOperand(1), m_APInt(C)))
9702 // 'and x, C' produces [0, C].
9703 Upper = *C + 1;
9704 // X & -X is a power of two or zero. So we can cap the value at max power of
9705 // two.
9706 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9707 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9708 Upper = APInt::getSignedMinValue(Width) + 1;
9709 break;
9710
9711 case Instruction::Or:
9712 if (match(BO.getOperand(1), m_APInt(C)))
9713 // 'or x, C' produces [C, UINT_MAX].
9714 Lower = *C;
9715 break;
9716
9717 case Instruction::AShr:
9718 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9719 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9721 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9722 } else if (match(BO.getOperand(0), m_APInt(C))) {
9723 unsigned ShiftAmount = Width - 1;
9724 if (!C->isZero() && IIQ.isExact(&BO))
9725 ShiftAmount = C->countr_zero();
9726 if (C->isNegative()) {
9727 // 'ashr C, x' produces [C, C >> (Width-1)]
9728 Lower = *C;
9729 Upper = C->ashr(ShiftAmount) + 1;
9730 } else {
9731 // 'ashr C, x' produces [C >> (Width-1), C]
9732 Lower = C->ashr(ShiftAmount);
9733 Upper = *C + 1;
9734 }
9735 }
9736 break;
9737
9738 case Instruction::LShr:
9739 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9740 // 'lshr x, C' produces [0, UINT_MAX >> C].
9741 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9742 } else if (match(BO.getOperand(0), m_APInt(C))) {
9743 // 'lshr C, x' produces [C >> (Width-1), C].
9744 unsigned ShiftAmount = Width - 1;
9745 if (!C->isZero() && IIQ.isExact(&BO))
9746 ShiftAmount = C->countr_zero();
9747 Lower = C->lshr(ShiftAmount);
9748 Upper = *C + 1;
9749 }
9750 break;
9751
9752 case Instruction::Shl:
9753 if (match(BO.getOperand(0), m_APInt(C))) {
9754 if (IIQ.hasNoUnsignedWrap(&BO)) {
9755 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9756 Lower = *C;
9757 Upper = Lower.shl(Lower.countl_zero()) + 1;
9758 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9759 if (C->isNegative()) {
9760 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9761 unsigned ShiftAmount = C->countl_one() - 1;
9762 Lower = C->shl(ShiftAmount);
9763 Upper = *C + 1;
9764 } else {
9765 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9766 unsigned ShiftAmount = C->countl_zero() - 1;
9767 Lower = *C;
9768 Upper = C->shl(ShiftAmount) + 1;
9769 }
9770 } else {
9771 // If lowbit is set, value can never be zero.
9772 if ((*C)[0])
9773 Lower = APInt::getOneBitSet(Width, 0);
9774 // If we are shifting a constant the largest it can be is if the longest
9775 // sequence of consecutive ones is shifted to the highbits (breaking
9776 // ties for which sequence is higher). At the moment we take a liberal
9777 // upper bound on this by just popcounting the constant.
9778 // TODO: There may be a bitwise trick for it longest/highest
9779 // consecutative sequence of ones (naive method is O(Width) loop).
9780 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9781 }
9782 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9783 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9784 }
9785 break;
9786
9787 case Instruction::SDiv:
9788 if (match(BO.getOperand(1), m_APInt(C))) {
9789 APInt IntMin = APInt::getSignedMinValue(Width);
9790 APInt IntMax = APInt::getSignedMaxValue(Width);
9791 if (C->isAllOnes()) {
9792 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9793 // where C != -1 and C != 0 and C != 1
9794 Lower = IntMin + 1;
9795 Upper = IntMax + 1;
9796 } else if (C->countl_zero() < Width - 1) {
9797 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9798 // where C != -1 and C != 0 and C != 1
9799 Lower = IntMin.sdiv(*C);
9800 Upper = IntMax.sdiv(*C);
9801 if (Lower.sgt(Upper))
9803 Upper = Upper + 1;
9804 assert(Upper != Lower && "Upper part of range has wrapped!");
9805 }
9806 } else if (match(BO.getOperand(0), m_APInt(C))) {
9807 if (C->isMinSignedValue()) {
9808 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9809 Lower = *C;
9810 Upper = Lower.lshr(1) + 1;
9811 } else {
9812 // 'sdiv C, x' produces [-|C|, |C|].
9813 Upper = C->abs() + 1;
9814 Lower = (-Upper) + 1;
9815 }
9816 }
9817 break;
9818
9819 case Instruction::UDiv:
9820 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9821 // 'udiv x, C' produces [0, UINT_MAX / C].
9822 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9823 } else if (match(BO.getOperand(0), m_APInt(C))) {
9824 // 'udiv C, x' produces [0, C].
9825 Upper = *C + 1;
9826 }
9827 break;
9828
9829 case Instruction::SRem:
9830 if (match(BO.getOperand(1), m_APInt(C))) {
9831 // 'srem x, C' produces (-|C|, |C|).
9832 Upper = C->abs();
9833 Lower = (-Upper) + 1;
9834 } else if (match(BO.getOperand(0), m_APInt(C))) {
9835 if (C->isNegative()) {
9836 // 'srem -|C|, x' produces [-|C|, 0].
9837 Upper = 1;
9838 Lower = *C;
9839 } else {
9840 // 'srem |C|, x' produces [0, |C|].
9841 Upper = *C + 1;
9842 }
9843 }
9844 break;
9845
9846 case Instruction::URem:
9847 if (match(BO.getOperand(1), m_APInt(C)))
9848 // 'urem x, C' produces [0, C).
9849 Upper = *C;
9850 else if (match(BO.getOperand(0), m_APInt(C)))
9851 // 'urem C, x' produces [0, C].
9852 Upper = *C + 1;
9853 break;
9854
9855 default:
9856 break;
9857 }
9858}
9859
9861 bool UseInstrInfo) {
9862 unsigned Width = II.getType()->getScalarSizeInBits();
9863 const APInt *C;
9864 switch (II.getIntrinsicID()) {
9865 case Intrinsic::ctlz:
9866 case Intrinsic::cttz: {
9867 APInt Upper(Width, Width);
9868 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
9869 Upper += 1;
9870 // Maximum of set/clear bits is the bit width.
9872 }
9873 case Intrinsic::ctpop:
9874 // Maximum of set/clear bits is the bit width.
9876 APInt(Width, Width) + 1);
9877 case Intrinsic::uadd_sat:
9878 // uadd.sat(x, C) produces [C, UINT_MAX].
9879 if (match(II.getOperand(0), m_APInt(C)) ||
9880 match(II.getOperand(1), m_APInt(C)))
9882 break;
9883 case Intrinsic::sadd_sat:
9884 if (match(II.getOperand(0), m_APInt(C)) ||
9885 match(II.getOperand(1), m_APInt(C))) {
9886 if (C->isNegative())
9887 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9889 APInt::getSignedMaxValue(Width) + *C +
9890 1);
9891
9892 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9894 APInt::getSignedMaxValue(Width) + 1);
9895 }
9896 break;
9897 case Intrinsic::usub_sat:
9898 // usub.sat(C, x) produces [0, C].
9899 if (match(II.getOperand(0), m_APInt(C)))
9900 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9901
9902 // usub.sat(x, C) produces [0, UINT_MAX - C].
9903 if (match(II.getOperand(1), m_APInt(C)))
9905 APInt::getMaxValue(Width) - *C + 1);
9906 break;
9907 case Intrinsic::ssub_sat:
9908 if (match(II.getOperand(0), m_APInt(C))) {
9909 if (C->isNegative())
9910 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9912 *C - APInt::getSignedMinValue(Width) +
9913 1);
9914
9915 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9917 APInt::getSignedMaxValue(Width) + 1);
9918 } else if (match(II.getOperand(1), m_APInt(C))) {
9919 if (C->isNegative())
9920 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9922 APInt::getSignedMaxValue(Width) + 1);
9923
9924 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9926 APInt::getSignedMaxValue(Width) - *C +
9927 1);
9928 }
9929 break;
9930 case Intrinsic::umin:
9931 case Intrinsic::umax:
9932 case Intrinsic::smin:
9933 case Intrinsic::smax:
9934 if (!match(II.getOperand(0), m_APInt(C)) &&
9935 !match(II.getOperand(1), m_APInt(C)))
9936 break;
9937
9938 switch (II.getIntrinsicID()) {
9939 case Intrinsic::umin:
9940 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9941 case Intrinsic::umax:
9943 case Intrinsic::smin:
9945 *C + 1);
9946 case Intrinsic::smax:
9948 APInt::getSignedMaxValue(Width) + 1);
9949 default:
9950 llvm_unreachable("Must be min/max intrinsic");
9951 }
9952 break;
9953 case Intrinsic::abs:
9954 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
9955 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9956 if (match(II.getOperand(1), m_One()))
9958 APInt::getSignedMaxValue(Width) + 1);
9959
9961 APInt::getSignedMinValue(Width) + 1);
9962 case Intrinsic::vscale:
9963 if (!II.getParent() || !II.getFunction())
9964 break;
9965 return getVScaleRange(II.getFunction(), Width);
9966 default:
9967 break;
9968 }
9969
9970 return ConstantRange::getFull(Width);
9971}
9972
9974 const InstrInfoQuery &IIQ) {
9975 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
9976 const Value *LHS = nullptr, *RHS = nullptr;
9978 if (R.Flavor == SPF_UNKNOWN)
9979 return ConstantRange::getFull(BitWidth);
9980
9981 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
9982 // If the negation part of the abs (in RHS) has the NSW flag,
9983 // then the result of abs(X) is [0..SIGNED_MAX],
9984 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9985 if (match(RHS, m_Neg(m_Specific(LHS))) &&
9989
9992 }
9993
9994 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
9995 // The result of -abs(X) is <= 0.
9997 APInt(BitWidth, 1));
9998 }
9999
10000 const APInt *C;
10001 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10002 return ConstantRange::getFull(BitWidth);
10003
10004 switch (R.Flavor) {
10005 case SPF_UMIN:
10007 case SPF_UMAX:
10009 case SPF_SMIN:
10011 *C + 1);
10012 case SPF_SMAX:
10015 default:
10016 return ConstantRange::getFull(BitWidth);
10017 }
10018}
10019
10021 // The maximum representable value of a half is 65504. For floats the maximum
10022 // value is 3.4e38 which requires roughly 129 bits.
10023 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10024 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10025 return;
10026 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10027 Lower = APInt(BitWidth, -65504, true);
10028 Upper = APInt(BitWidth, 65505);
10029 }
10030
10031 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10032 // For a fptoui the lower limit is left as 0.
10033 Upper = APInt(BitWidth, 65505);
10034 }
10035}
10036
10038 bool UseInstrInfo, AssumptionCache *AC,
10039 const Instruction *CtxI,
10040 const DominatorTree *DT,
10041 unsigned Depth) {
10042 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10043
10045 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10046
10047 if (auto *C = dyn_cast<Constant>(V))
10048 return C->toConstantRange();
10049
10050 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10051 InstrInfoQuery IIQ(UseInstrInfo);
10052 ConstantRange CR = ConstantRange::getFull(BitWidth);
10053 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10054 APInt Lower = APInt(BitWidth, 0);
10055 APInt Upper = APInt(BitWidth, 0);
10056 // TODO: Return ConstantRange.
10057 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10059 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10060 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10061 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10063 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10065 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10066 CR = CRTrue.unionWith(CRFalse);
10068 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10069 APInt Lower = APInt(BitWidth, 0);
10070 APInt Upper = APInt(BitWidth, 0);
10071 // TODO: Return ConstantRange.
10074 } else if (const auto *A = dyn_cast<Argument>(V))
10075 if (std::optional<ConstantRange> Range = A->getRange())
10076 CR = *Range;
10077
10078 if (auto *I = dyn_cast<Instruction>(V)) {
10079 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10081
10082 if (const auto *CB = dyn_cast<CallBase>(V))
10083 if (std::optional<ConstantRange> Range = CB->getRange())
10084 CR = CR.intersectWith(*Range);
10085 }
10086
10087 if (CtxI && AC) {
10088 // Try to restrict the range based on information from assumptions.
10089 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10090 if (!AssumeVH)
10091 continue;
10092 CallInst *I = cast<CallInst>(AssumeVH);
10093 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10094 "Got assumption for the wrong function!");
10095 assert(I->getIntrinsicID() == Intrinsic::assume &&
10096 "must be an assume intrinsic");
10097
10098 if (!isValidAssumeForContext(I, CtxI, DT))
10099 continue;
10100 Value *Arg = I->getArgOperand(0);
10101 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10102 // Currently we just use information from comparisons.
10103 if (!Cmp || Cmp->getOperand(0) != V)
10104 continue;
10105 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10106 ConstantRange RHS =
10107 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10108 UseInstrInfo, AC, I, DT, Depth + 1);
10109 CR = CR.intersectWith(
10110 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10111 }
10112 }
10113
10114 return CR;
10115}
10116
10117static void
10119 function_ref<void(Value *)> InsertAffected) {
10120 assert(V != nullptr);
10121 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10122 InsertAffected(V);
10123 } else if (auto *I = dyn_cast<Instruction>(V)) {
10124 InsertAffected(V);
10125
10126 // Peek through unary operators to find the source of the condition.
10127 Value *Op;
10130 InsertAffected(Op);
10131 }
10132 }
10133}
10134
10136 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10137 auto AddAffected = [&InsertAffected](Value *V) {
10138 addValueAffectedByCondition(V, InsertAffected);
10139 };
10140
10141 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10142 if (IsAssume) {
10143 AddAffected(LHS);
10144 AddAffected(RHS);
10145 } else if (match(RHS, m_Constant()))
10146 AddAffected(LHS);
10147 };
10148
10149 SmallVector<Value *, 8> Worklist;
10151 Worklist.push_back(Cond);
10152 while (!Worklist.empty()) {
10153 Value *V = Worklist.pop_back_val();
10154 if (!Visited.insert(V).second)
10155 continue;
10156
10157 CmpPredicate Pred;
10158 Value *A, *B, *X;
10159
10160 if (IsAssume) {
10161 AddAffected(V);
10162 if (match(V, m_Not(m_Value(X))))
10163 AddAffected(X);
10164 }
10165
10166 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10167 // assume(A && B) is split to -> assume(A); assume(B);
10168 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10169 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10170 // enough information to be worth handling (intersection of information as
10171 // opposed to union).
10172 if (!IsAssume) {
10173 Worklist.push_back(A);
10174 Worklist.push_back(B);
10175 }
10176 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10177 bool HasRHSC = match(B, m_ConstantInt());
10178 if (ICmpInst::isEquality(Pred)) {
10179 AddAffected(A);
10180 if (IsAssume)
10181 AddAffected(B);
10182 if (HasRHSC) {
10183 Value *Y;
10184 // (X & C) or (X | C).
10185 // (X << C) or (X >>_s C) or (X >>_u C).
10186 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10187 AddAffected(X);
10188 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10189 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10190 AddAffected(X);
10191 AddAffected(Y);
10192 }
10193 }
10194 } else {
10195 AddCmpOperands(A, B);
10196 if (HasRHSC) {
10197 // Handle (A + C1) u< C2, which is the canonical form of
10198 // A > C3 && A < C4.
10200 AddAffected(X);
10201
10202 if (ICmpInst::isUnsigned(Pred)) {
10203 Value *Y;
10204 // X & Y u> C -> X >u C && Y >u C
10205 // X | Y u< C -> X u< C && Y u< C
10206 // X nuw+ Y u< C -> X u< C && Y u< C
10207 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10208 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10209 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10210 AddAffected(X);
10211 AddAffected(Y);
10212 }
10213 // X nuw- Y u> C -> X u> C
10214 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10215 AddAffected(X);
10216 }
10217 }
10218
10219 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10220 // by computeKnownFPClass().
10222 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10223 InsertAffected(X);
10224 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10225 InsertAffected(X);
10226 }
10227 }
10228
10229 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10230 AddAffected(X);
10231 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10232 AddCmpOperands(A, B);
10233
10234 // fcmp fneg(x), y
10235 // fcmp fabs(x), y
10236 // fcmp fneg(fabs(x)), y
10237 if (match(A, m_FNeg(m_Value(A))))
10238 AddAffected(A);
10239 if (match(A, m_FAbs(m_Value(A))))
10240 AddAffected(A);
10241
10243 m_Value()))) {
10244 // Handle patterns that computeKnownFPClass() support.
10245 AddAffected(A);
10246 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10247 // Assume is checked here as X is already added above for assumes in
10248 // addValueAffectedByCondition
10249 AddAffected(X);
10250 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10251 // Assume is checked here to avoid issues with ephemeral values
10252 Worklist.push_back(X);
10253 }
10254 }
10255}
10256
10258 // (X >> C) or/add (X & mask(C) != 0)
10259 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10260 if (BO->getOpcode() == Instruction::Add ||
10261 BO->getOpcode() == Instruction::Or) {
10262 const Value *X;
10263 const APInt *C1, *C2;
10264 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10268 m_Zero())))) &&
10269 C2->popcount() == C1->getZExtValue())
10270 return X;
10271 }
10272 }
10273 return nullptr;
10274}
10275
10277 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10278}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Utilities for dealing with flags related to floating point properties and mode controls.
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static SmallVector< VPValue *, 4 > getOperands(ArrayRef< VPValue * > Values, unsigned OperandIndex)
Definition VPlanSLP.cpp:210
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext, unsigned Depth=0)
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, SimplifyQuery &Q, unsigned Depth)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
static unsigned computeNumSignBitsVectorConstant(const Value *V, const APInt &DemandedElts, unsigned TyBits)
For vector constants, loop over the elements and find the constant with the minimum number of sign bi...
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V1 == (binop V2, X), where X is known non-zero.
static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q, unsigned Depth)
Test whether a GEP's result is known to be non-null.
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
UndefPoisonKind
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static bool includesPoison(UndefPoisonKind Kind)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool includesUndef(UndefPoisonKind Kind)
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ?
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
bool isFinite() const
Definition APFloat.h:1454
bool isNaN() const
Definition APFloat.h:1447
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1138
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1098
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1079
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1406
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:423
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1540
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1391
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1670
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1385
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:206
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1330
unsigned ceilLogBase2() const
Definition APInt.h:1764
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1201
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:371
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:380
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:209
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:216
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:329
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1249
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1396
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1166
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1628
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:219
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1041
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:356
unsigned logBase2() const
Definition APInt.h:1761
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:827
void setAllBits()
Set every bit to 1.
Definition APInt.h:1319
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:471
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:405
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1150
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:873
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1130
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:296
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:200
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1388
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1237
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:239
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1221
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1449
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
iterator end() const
Definition ArrayRef.h:136
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
iterator begin() const
Definition ArrayRef.h:135
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:191
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:223
LLVM_ABI bool isSingleEdge() const
Check if this is the only edge between Start and End.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:472
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:374
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
This class is the base class for the comparison instructions.
Definition InstrTypes.h:666
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:678
@ ICMP_SLT
signed less than
Definition InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:708
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:684
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:693
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:682
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:683
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:702
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:701
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:705
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:692
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:703
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:690
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:685
@ ICMP_NE
not equal
Definition InstrTypes.h:700
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:706
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:704
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:691
bool isSigned() const
Definition InstrTypes.h:932
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:829
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:944
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:772
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:791
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:767
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:895
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:778
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:938
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:702
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:593
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:668
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:776
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:163
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI bool isAllNegative() const
Return true if all values in this range are negative.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition Constants.cpp:76
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:198
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition DataLayout.h:669
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:165
LLVM_ABI bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition Operator.h:200
Convenience struct for specifying and reasoning about fast-math flags.
Definition FMF.h:22
bool noSignedZeros() const
Definition FMF.h:67
bool noInfs() const
Definition FMF.h:66
void setNoSignedZeros(bool B=true)
Definition FMF.h:84
void setNoNaNs(bool B=true)
Definition FMF.h:78
bool noNaNs() const
Definition FMF.h:65
const BasicBlock & getEntryBlock() const
Definition Function.h:807
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition Function.cpp:803
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
PointerType * getType() const
Global values are always pointers.
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition Globals.cpp:132
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
CmpPredicate getInverseCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static LLVM_ABI std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
This instruction inserts a struct field of array element value into an aggregate value.
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
LLVM_ABI bool hasNoNaNs() const LLVM_READONLY
Determine whether the no-NaNs flag is set.
LLVM_ABI bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
LLVM_ABI bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
LLVM_ABI bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
bool isUnaryOp() const
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
A wrapper class for inspecting calls to intrinsic functions.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
An instruction for reading from memory.
Value * getPointerOperand()
Align getAlign() const
Return the alignment of the access that is being performed.
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
Metadata node.
Definition Metadata.h:1077
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition Operator.h:78
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A udiv, sdiv, lshr, or ashr instruction, which can be marked as "exact", indicating that no bits are ...
Definition Operator.h:154
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition Operator.h:173
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static LLVM_ABI void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
size_type size() const
Definition SmallPtrSet.h:99
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void reserve(size_type N)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition StringRef.h:581
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:621
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:652
Class to represent struct types.
unsigned getNumElements() const
Random access to the elements.
Type * getElementType(unsigned N) const
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:45
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
Definition Type.cpp:298
LLVM_ABI unsigned getIntegerBitWidth() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:297
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition Type.h:246
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI uint64_t getArrayNumElements() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:295
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:352
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
Definition Type.cpp:296
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition Type.h:311
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:231
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:270
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:240
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
Definition Type.cpp:301
LLVM_ABI const fltSemantics & getFltSemantics() const
Definition Type.cpp:107
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
User * getUser() const
Returns the User that contains this Use.
Definition Use.h:61
LLVM_ABI unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition Use.cpp:35
op_range operands()
Definition User.h:292
Value * getOperand(unsigned i) const
Definition User.h:232
unsigned getNumOperands() const
Definition User.h:254
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition Value.h:759
iterator_range< user_iterator > users()
Definition Value.h:426
LLVM_ABI const Value * stripAndAccumulateConstantOffsets(const DataLayout &DL, APInt &Offset, bool AllowNonInbounds, bool AllowInvariantGroup=false, function_ref< bool(Value &Value, APInt &Offset)> ExternalAnalysis=nullptr, bool LookThroughIntToPtr=false) const
Accumulate the constant offset this value has compared to a base pointer.
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition WithCache.h:59
PointerType getValue() const
Definition WithCache.h:57
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition TypeSize.h:200
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition TypeSize.h:169
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition TypeSize.h:166
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:134
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.
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:1707
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:1665
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:2118
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:1714
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:1879
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:208
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:853
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:324
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:374
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
constexpr bool outputsAreZero() const
Return true if output denormals should be flushed to 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ IEEE
IEEE-754 denormal numbers preserved.
constexpr bool inputsAreZero() const
Return true if input denormals must be implicitly treated as 0.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getIEEE()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:294
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static LLVM_ABI std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:179
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:248
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:101
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:117
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:244
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:235
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:267
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
Definition KnownBits.h:112
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:154
KnownBits byteSwap() const
Definition KnownBits.h:507
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:282
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:511
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:165
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:314
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:104
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:218
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:304
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:173
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:238
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:340
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:189
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:241
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:138
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:319
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:98
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:346
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:273
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:92
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:212
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:160
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:199
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a zero.
bool isUnknown() const
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
LLVM_ABI void propagateCanonicalizingSrc(const KnownFPClass &Src, DenormalMode Mode)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void signBitMustBeZero()
Assume the sign bit is zero.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a negative zero.
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC