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 if ((IID == Intrinsic::maximum || IID == Intrinsic::maximumnum ||
5074 IID == Intrinsic::maxnum) &&
5075 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5076 Known.signBitMustBeZero();
5077 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minimumnum ||
5078 IID == Intrinsic::minnum) &&
5079 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5080 Known.signBitMustBeOne();
5081 }
5082 }
5083 break;
5084 }
5085 case Intrinsic::canonicalize: {
5086 KnownFPClass KnownSrc;
5087 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5088 KnownSrc, Q, Depth + 1);
5089
5090 // This is essentially a stronger form of
5091 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5092 // actually have an IR canonicalization guarantee.
5093
5094 // Canonicalize may flush denormals to zero, so we have to consider the
5095 // denormal mode to preserve known-not-0 knowledge.
5096 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5097
5098 // Stronger version of propagateNaN
5099 // Canonicalize is guaranteed to quiet signaling nans.
5100 if (KnownSrc.isKnownNeverNaN())
5101 Known.knownNot(fcNan);
5102 else
5103 Known.knownNot(fcSNan);
5104
5105 const Function *F = II->getFunction();
5106 if (!F)
5107 break;
5108
5109 // If the parent function flushes denormals, the canonical output cannot
5110 // be a denormal.
5111 const fltSemantics &FPType =
5112 II->getType()->getScalarType()->getFltSemantics();
5113 DenormalMode DenormMode = F->getDenormalMode(FPType);
5114 if (DenormMode == DenormalMode::getIEEE()) {
5115 if (KnownSrc.isKnownNever(fcPosZero))
5116 Known.knownNot(fcPosZero);
5117 if (KnownSrc.isKnownNever(fcNegZero))
5118 Known.knownNot(fcNegZero);
5119 break;
5120 }
5121
5122 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5123 Known.knownNot(fcSubnormal);
5124
5125 if (DenormMode.Input == DenormalMode::PositiveZero ||
5126 (DenormMode.Output == DenormalMode::PositiveZero &&
5127 DenormMode.Input == DenormalMode::IEEE))
5128 Known.knownNot(fcNegZero);
5129
5130 break;
5131 }
5132 case Intrinsic::vector_reduce_fmax:
5133 case Intrinsic::vector_reduce_fmin:
5134 case Intrinsic::vector_reduce_fmaximum:
5135 case Intrinsic::vector_reduce_fminimum: {
5136 // reduce min/max will choose an element from one of the vector elements,
5137 // so we can infer and class information that is common to all elements.
5138 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5139 InterestedClasses, Q, Depth + 1);
5140 // Can only propagate sign if output is never NaN.
5141 if (!Known.isKnownNeverNaN())
5142 Known.SignBit.reset();
5143 break;
5144 }
5145 // reverse preserves all characteristics of the input vec's element.
5146 case Intrinsic::vector_reverse:
5147 Known = computeKnownFPClass(
5148 II->getArgOperand(0), DemandedElts.reverseBits(),
5149 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5150 break;
5151 case Intrinsic::trunc:
5152 case Intrinsic::floor:
5153 case Intrinsic::ceil:
5154 case Intrinsic::rint:
5155 case Intrinsic::nearbyint:
5156 case Intrinsic::round:
5157 case Intrinsic::roundeven: {
5158 KnownFPClass KnownSrc;
5159 FPClassTest InterestedSrcs = InterestedClasses;
5160 if (InterestedSrcs & fcPosFinite)
5161 InterestedSrcs |= fcPosFinite;
5162 if (InterestedSrcs & fcNegFinite)
5163 InterestedSrcs |= fcNegFinite;
5164 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5165 KnownSrc, Q, Depth + 1);
5166
5167 // Integer results cannot be subnormal.
5168 Known.knownNot(fcSubnormal);
5169
5170 Known.propagateNaN(KnownSrc, true);
5171
5172 // Pass through infinities, except PPC_FP128 is a special case for
5173 // intrinsics other than trunc.
5174 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5175 if (KnownSrc.isKnownNeverPosInfinity())
5176 Known.knownNot(fcPosInf);
5177 if (KnownSrc.isKnownNeverNegInfinity())
5178 Known.knownNot(fcNegInf);
5179 }
5180
5181 // Negative round ups to 0 produce -0
5182 if (KnownSrc.isKnownNever(fcPosFinite))
5183 Known.knownNot(fcPosFinite);
5184 if (KnownSrc.isKnownNever(fcNegFinite))
5185 Known.knownNot(fcNegFinite);
5186
5187 break;
5188 }
5189 case Intrinsic::exp:
5190 case Intrinsic::exp2:
5191 case Intrinsic::exp10: {
5192 Known.knownNot(fcNegative);
5193 if ((InterestedClasses & fcNan) == fcNone)
5194 break;
5195
5196 KnownFPClass KnownSrc;
5197 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5198 KnownSrc, Q, Depth + 1);
5199 if (KnownSrc.isKnownNeverNaN()) {
5200 Known.knownNot(fcNan);
5201 Known.signBitMustBeZero();
5202 }
5203
5204 break;
5205 }
5206 case Intrinsic::fptrunc_round: {
5207 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5208 Q, Depth);
5209 break;
5210 }
5211 case Intrinsic::log:
5212 case Intrinsic::log10:
5213 case Intrinsic::log2:
5214 case Intrinsic::experimental_constrained_log:
5215 case Intrinsic::experimental_constrained_log10:
5216 case Intrinsic::experimental_constrained_log2: {
5217 // log(+inf) -> +inf
5218 // log([+-]0.0) -> -inf
5219 // log(-inf) -> nan
5220 // log(-x) -> nan
5221 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5222 break;
5223
5224 FPClassTest InterestedSrcs = InterestedClasses;
5225 if ((InterestedClasses & fcNegInf) != fcNone)
5226 InterestedSrcs |= fcZero | fcSubnormal;
5227 if ((InterestedClasses & fcNan) != fcNone)
5228 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5229
5230 KnownFPClass KnownSrc;
5231 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5232 KnownSrc, Q, Depth + 1);
5233
5234 if (KnownSrc.isKnownNeverPosInfinity())
5235 Known.knownNot(fcPosInf);
5236
5237 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5238 Known.knownNot(fcNan);
5239
5240 const Function *F = II->getFunction();
5241
5242 if (!F)
5243 break;
5244
5245 const fltSemantics &FltSem =
5246 II->getType()->getScalarType()->getFltSemantics();
5247 DenormalMode Mode = F->getDenormalMode(FltSem);
5248
5249 if (KnownSrc.isKnownNeverLogicalZero(Mode))
5250 Known.knownNot(fcNegInf);
5251
5252 break;
5253 }
5254 case Intrinsic::powi: {
5255 if ((InterestedClasses & fcNegative) == fcNone)
5256 break;
5257
5258 const Value *Exp = II->getArgOperand(1);
5259 Type *ExpTy = Exp->getType();
5260 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5261 KnownBits ExponentKnownBits(BitWidth);
5262 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5263 ExponentKnownBits, Q, Depth + 1);
5264
5265 if (ExponentKnownBits.Zero[0]) { // Is even
5266 Known.knownNot(fcNegative);
5267 break;
5268 }
5269
5270 // Given that exp is an integer, here are the
5271 // ways that pow can return a negative value:
5272 //
5273 // pow(-x, exp) --> negative if exp is odd and x is negative.
5274 // pow(-0, exp) --> -inf if exp is negative odd.
5275 // pow(-0, exp) --> -0 if exp is positive odd.
5276 // pow(-inf, exp) --> -0 if exp is negative odd.
5277 // pow(-inf, exp) --> -inf if exp is positive odd.
5278 KnownFPClass KnownSrc;
5279 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5280 KnownSrc, Q, Depth + 1);
5281 if (KnownSrc.isKnownNever(fcNegative))
5282 Known.knownNot(fcNegative);
5283 break;
5284 }
5285 case Intrinsic::ldexp: {
5286 KnownFPClass KnownSrc;
5287 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5288 KnownSrc, Q, Depth + 1);
5289 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5290
5291 // Sign is preserved, but underflows may produce zeroes.
5292 if (KnownSrc.isKnownNever(fcNegative))
5293 Known.knownNot(fcNegative);
5294 else if (KnownSrc.cannotBeOrderedLessThanZero())
5296
5297 if (KnownSrc.isKnownNever(fcPositive))
5298 Known.knownNot(fcPositive);
5299 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5301
5302 // Can refine inf/zero handling based on the exponent operand.
5303 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5304 if ((InterestedClasses & ExpInfoMask) == fcNone)
5305 break;
5306 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5307 break;
5308
5309 const fltSemantics &Flt =
5310 II->getType()->getScalarType()->getFltSemantics();
5311 unsigned Precision = APFloat::semanticsPrecision(Flt);
5312 const Value *ExpArg = II->getArgOperand(1);
5314 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5315
5316 const int MantissaBits = Precision - 1;
5317 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5318 Known.knownNot(fcSubnormal);
5319
5320 const Function *F = II->getFunction();
5321 const APInt *ConstVal = ExpRange.getSingleElement();
5322 const fltSemantics &FltSem =
5323 II->getType()->getScalarType()->getFltSemantics();
5324 if (ConstVal && ConstVal->isZero()) {
5325 // ldexp(x, 0) -> x, so propagate everything.
5326 Known.propagateCanonicalizingSrc(KnownSrc, F->getDenormalMode(FltSem));
5327 } else if (ExpRange.isAllNegative()) {
5328 // If we know the power is <= 0, can't introduce inf
5329 if (KnownSrc.isKnownNeverPosInfinity())
5330 Known.knownNot(fcPosInf);
5331 if (KnownSrc.isKnownNeverNegInfinity())
5332 Known.knownNot(fcNegInf);
5333 } else if (ExpRange.isAllNonNegative()) {
5334 // If we know the power is >= 0, can't introduce subnormal or zero
5335 if (KnownSrc.isKnownNeverPosSubnormal())
5336 Known.knownNot(fcPosSubnormal);
5337 if (KnownSrc.isKnownNeverNegSubnormal())
5338 Known.knownNot(fcNegSubnormal);
5339 if (F &&
5340 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5341 Known.knownNot(fcPosZero);
5342 if (F &&
5343 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5344 Known.knownNot(fcNegZero);
5345 }
5346
5347 break;
5348 }
5349 case Intrinsic::arithmetic_fence: {
5350 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5351 Known, Q, Depth + 1);
5352 break;
5353 }
5354 case Intrinsic::experimental_constrained_sitofp:
5355 case Intrinsic::experimental_constrained_uitofp:
5356 // Cannot produce nan
5357 Known.knownNot(fcNan);
5358
5359 // sitofp and uitofp turn into +0.0 for zero.
5360 Known.knownNot(fcNegZero);
5361
5362 // Integers cannot be subnormal
5363 Known.knownNot(fcSubnormal);
5364
5365 if (IID == Intrinsic::experimental_constrained_uitofp)
5366 Known.signBitMustBeZero();
5367
5368 // TODO: Copy inf handling from instructions
5369 break;
5370 default:
5371 break;
5372 }
5373
5374 break;
5375 }
5376 case Instruction::FAdd:
5377 case Instruction::FSub: {
5378 KnownFPClass KnownLHS, KnownRHS;
5379 bool WantNegative =
5380 Op->getOpcode() == Instruction::FAdd &&
5381 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5382 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5383 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5384
5385 if (!WantNaN && !WantNegative && !WantNegZero)
5386 break;
5387
5388 FPClassTest InterestedSrcs = InterestedClasses;
5389 if (WantNegative)
5390 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5391 if (InterestedClasses & fcNan)
5392 InterestedSrcs |= fcInf;
5393 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5394 KnownRHS, Q, Depth + 1);
5395
5396 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5397 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5398 WantNegZero || Opc == Instruction::FSub) {
5399
5400 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5401 // there's no point.
5402 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5403 KnownLHS, Q, Depth + 1);
5404 // Adding positive and negative infinity produces NaN.
5405 // TODO: Check sign of infinities.
5406 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5407 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5408 Known.knownNot(fcNan);
5409
5410 // FIXME: Context function should always be passed in separately
5411 const Function *F = cast<Instruction>(Op)->getFunction();
5412
5413 if (Op->getOpcode() == Instruction::FAdd) {
5414 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5415 KnownRHS.cannotBeOrderedLessThanZero())
5417 if (!F)
5418 break;
5419
5420 const fltSemantics &FltSem =
5421 Op->getType()->getScalarType()->getFltSemantics();
5422 DenormalMode Mode = F->getDenormalMode(FltSem);
5423
5424 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5425 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5426 KnownRHS.isKnownNeverLogicalNegZero(Mode)) &&
5427 // Make sure output negative denormal can't flush to -0
5428 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5429 Known.knownNot(fcNegZero);
5430 } else {
5431 if (!F)
5432 break;
5433
5434 const fltSemantics &FltSem =
5435 Op->getType()->getScalarType()->getFltSemantics();
5436 DenormalMode Mode = F->getDenormalMode(FltSem);
5437
5438 // Only fsub -0, +0 can return -0
5439 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5440 KnownRHS.isKnownNeverLogicalPosZero(Mode)) &&
5441 // Make sure output negative denormal can't flush to -0
5442 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5443 Known.knownNot(fcNegZero);
5444 }
5445 }
5446
5447 break;
5448 }
5449 case Instruction::FMul: {
5450 // X * X is always non-negative or a NaN.
5451 if (Op->getOperand(0) == Op->getOperand(1))
5452 Known.knownNot(fcNegative);
5453
5454 if ((InterestedClasses & fcNan) != fcNan)
5455 break;
5456
5457 // fcSubnormal is only needed in case of DAZ.
5458 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5459
5460 KnownFPClass KnownLHS, KnownRHS;
5461 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5462 Q, Depth + 1);
5463 if (!KnownRHS.isKnownNeverNaN())
5464 break;
5465
5466 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5467 Q, Depth + 1);
5468 if (!KnownLHS.isKnownNeverNaN())
5469 break;
5470
5471 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5472 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5473 Known.signBitMustBeZero();
5474 else
5475 Known.signBitMustBeOne();
5476 }
5477
5478 // If 0 * +/-inf produces NaN.
5479 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5480 Known.knownNot(fcNan);
5481 break;
5482 }
5483
5484 const Function *F = cast<Instruction>(Op)->getFunction();
5485 if (!F)
5486 break;
5487
5488 Type *OpTy = Op->getType()->getScalarType();
5489 const fltSemantics &FltSem = OpTy->getFltSemantics();
5490 DenormalMode Mode = F->getDenormalMode(FltSem);
5491
5492 if ((KnownRHS.isKnownNeverInfinity() ||
5493 KnownLHS.isKnownNeverLogicalZero(Mode)) &&
5494 (KnownLHS.isKnownNeverInfinity() ||
5495 KnownRHS.isKnownNeverLogicalZero(Mode)))
5496 Known.knownNot(fcNan);
5497
5498 break;
5499 }
5500 case Instruction::FDiv:
5501 case Instruction::FRem: {
5502 if (Op->getOperand(0) == Op->getOperand(1)) {
5503 // TODO: Could filter out snan if we inspect the operand
5504 if (Op->getOpcode() == Instruction::FDiv) {
5505 // X / X is always exactly 1.0 or a NaN.
5507 } else {
5508 // X % X is always exactly [+-]0.0 or a NaN.
5509 Known.KnownFPClasses = fcNan | fcZero;
5510 }
5511
5512 break;
5513 }
5514
5515 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5516 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5517 const bool WantPositive =
5518 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5519 if (!WantNan && !WantNegative && !WantPositive)
5520 break;
5521
5522 KnownFPClass KnownLHS, KnownRHS;
5523
5524 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5525 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5526 Depth + 1);
5527
5528 bool KnowSomethingUseful =
5529 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5530
5531 if (KnowSomethingUseful || WantPositive) {
5532 const FPClassTest InterestedLHS =
5533 WantPositive ? fcAllFlags
5535
5536 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5537 InterestedClasses & InterestedLHS, KnownLHS, Q,
5538 Depth + 1);
5539 }
5540
5541 const Function *F = cast<Instruction>(Op)->getFunction();
5542 const fltSemantics &FltSem =
5543 Op->getType()->getScalarType()->getFltSemantics();
5544
5545 if (Op->getOpcode() == Instruction::FDiv) {
5546 // Only 0/0, Inf/Inf produce NaN.
5547 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5548 (KnownLHS.isKnownNeverInfinity() ||
5549 KnownRHS.isKnownNeverInfinity()) &&
5550 ((F &&
5551 KnownLHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) ||
5552 (F &&
5553 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))))) {
5554 Known.knownNot(fcNan);
5555 }
5556
5557 // X / -0.0 is -Inf (or NaN).
5558 // +X / +X is +X
5559 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5560 Known.knownNot(fcNegative);
5561 } else {
5562 // Inf REM x and x REM 0 produce NaN.
5563 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5564 KnownLHS.isKnownNeverInfinity() && F &&
5565 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5566 Known.knownNot(fcNan);
5567 }
5568
5569 // The sign for frem is the same as the first operand.
5570 if (KnownLHS.cannotBeOrderedLessThanZero())
5572 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5574
5575 // See if we can be more aggressive about the sign of 0.
5576 if (KnownLHS.isKnownNever(fcNegative))
5577 Known.knownNot(fcNegative);
5578 if (KnownLHS.isKnownNever(fcPositive))
5579 Known.knownNot(fcPositive);
5580 }
5581
5582 break;
5583 }
5584 case Instruction::FPExt: {
5585 // Infinity, nan and zero propagate from source.
5586 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5587 Known, Q, Depth + 1);
5588
5589 const fltSemantics &DstTy =
5590 Op->getType()->getScalarType()->getFltSemantics();
5591 const fltSemantics &SrcTy =
5592 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5593
5594 // All subnormal inputs should be in the normal range in the result type.
5595 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5596 if (Known.KnownFPClasses & fcPosSubnormal)
5597 Known.KnownFPClasses |= fcPosNormal;
5598 if (Known.KnownFPClasses & fcNegSubnormal)
5599 Known.KnownFPClasses |= fcNegNormal;
5600 Known.knownNot(fcSubnormal);
5601 }
5602
5603 // Sign bit of a nan isn't guaranteed.
5604 if (!Known.isKnownNeverNaN())
5605 Known.SignBit = std::nullopt;
5606 break;
5607 }
5608 case Instruction::FPTrunc: {
5609 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5610 Depth);
5611 break;
5612 }
5613 case Instruction::SIToFP:
5614 case Instruction::UIToFP: {
5615 // Cannot produce nan
5616 Known.knownNot(fcNan);
5617
5618 // Integers cannot be subnormal
5619 Known.knownNot(fcSubnormal);
5620
5621 // sitofp and uitofp turn into +0.0 for zero.
5622 Known.knownNot(fcNegZero);
5623 if (Op->getOpcode() == Instruction::UIToFP)
5624 Known.signBitMustBeZero();
5625
5626 if (InterestedClasses & fcInf) {
5627 // Get width of largest magnitude integer (remove a bit if signed).
5628 // This still works for a signed minimum value because the largest FP
5629 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5630 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5631 if (Op->getOpcode() == Instruction::SIToFP)
5632 --IntSize;
5633
5634 // If the exponent of the largest finite FP value can hold the largest
5635 // integer, the result of the cast must be finite.
5636 Type *FPTy = Op->getType()->getScalarType();
5637 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5638 Known.knownNot(fcInf);
5639 }
5640
5641 break;
5642 }
5643 case Instruction::ExtractElement: {
5644 // Look through extract element. If the index is non-constant or
5645 // out-of-range demand all elements, otherwise just the extracted element.
5646 const Value *Vec = Op->getOperand(0);
5647
5648 APInt DemandedVecElts;
5649 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5650 unsigned NumElts = VecTy->getNumElements();
5651 DemandedVecElts = APInt::getAllOnes(NumElts);
5652 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5653 if (CIdx && CIdx->getValue().ult(NumElts))
5654 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5655 } else {
5656 DemandedVecElts = APInt(1, 1);
5657 }
5658
5659 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5660 Q, Depth + 1);
5661 }
5662 case Instruction::InsertElement: {
5663 if (isa<ScalableVectorType>(Op->getType()))
5664 return;
5665
5666 const Value *Vec = Op->getOperand(0);
5667 const Value *Elt = Op->getOperand(1);
5668 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5669 unsigned NumElts = DemandedElts.getBitWidth();
5670 APInt DemandedVecElts = DemandedElts;
5671 bool NeedsElt = true;
5672 // If we know the index we are inserting to, clear it from Vec check.
5673 if (CIdx && CIdx->getValue().ult(NumElts)) {
5674 DemandedVecElts.clearBit(CIdx->getZExtValue());
5675 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5676 }
5677
5678 // Do we demand the inserted element?
5679 if (NeedsElt) {
5680 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5681 // If we don't know any bits, early out.
5682 if (Known.isUnknown())
5683 break;
5684 } else {
5685 Known.KnownFPClasses = fcNone;
5686 }
5687
5688 // Do we need anymore elements from Vec?
5689 if (!DemandedVecElts.isZero()) {
5690 KnownFPClass Known2;
5691 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5692 Depth + 1);
5693 Known |= Known2;
5694 }
5695
5696 break;
5697 }
5698 case Instruction::ShuffleVector: {
5699 // For undef elements, we don't know anything about the common state of
5700 // the shuffle result.
5701 APInt DemandedLHS, DemandedRHS;
5702 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5703 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5704 return;
5705
5706 if (!!DemandedLHS) {
5707 const Value *LHS = Shuf->getOperand(0);
5708 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5709 Depth + 1);
5710
5711 // If we don't know any bits, early out.
5712 if (Known.isUnknown())
5713 break;
5714 } else {
5715 Known.KnownFPClasses = fcNone;
5716 }
5717
5718 if (!!DemandedRHS) {
5719 KnownFPClass Known2;
5720 const Value *RHS = Shuf->getOperand(1);
5721 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5722 Depth + 1);
5723 Known |= Known2;
5724 }
5725
5726 break;
5727 }
5728 case Instruction::ExtractValue: {
5729 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5730 ArrayRef<unsigned> Indices = Extract->getIndices();
5731 const Value *Src = Extract->getAggregateOperand();
5732 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5733 Indices[0] == 0) {
5734 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5735 switch (II->getIntrinsicID()) {
5736 case Intrinsic::frexp: {
5737 Known.knownNot(fcSubnormal);
5738
5739 KnownFPClass KnownSrc;
5740 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5741 InterestedClasses, KnownSrc, Q, Depth + 1);
5742
5743 const Function *F = cast<Instruction>(Op)->getFunction();
5744 const fltSemantics &FltSem =
5745 Op->getType()->getScalarType()->getFltSemantics();
5746
5747 if (KnownSrc.isKnownNever(fcNegative))
5748 Known.knownNot(fcNegative);
5749 else {
5750 if (F &&
5751 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5752 Known.knownNot(fcNegZero);
5753 if (KnownSrc.isKnownNever(fcNegInf))
5754 Known.knownNot(fcNegInf);
5755 }
5756
5757 if (KnownSrc.isKnownNever(fcPositive))
5758 Known.knownNot(fcPositive);
5759 else {
5760 if (F &&
5761 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5762 Known.knownNot(fcPosZero);
5763 if (KnownSrc.isKnownNever(fcPosInf))
5764 Known.knownNot(fcPosInf);
5765 }
5766
5767 Known.propagateNaN(KnownSrc);
5768 return;
5769 }
5770 default:
5771 break;
5772 }
5773 }
5774 }
5775
5776 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
5777 Depth + 1);
5778 break;
5779 }
5780 case Instruction::PHI: {
5781 const PHINode *P = cast<PHINode>(Op);
5782 // Unreachable blocks may have zero-operand PHI nodes.
5783 if (P->getNumIncomingValues() == 0)
5784 break;
5785
5786 // Otherwise take the unions of the known bit sets of the operands,
5787 // taking conservative care to avoid excessive recursion.
5788 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5789
5790 if (Depth < PhiRecursionLimit) {
5791 // Skip if every incoming value references to ourself.
5792 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5793 break;
5794
5795 bool First = true;
5796
5797 for (const Use &U : P->operands()) {
5798 Value *IncValue;
5799 Instruction *CxtI;
5800 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
5801 // Skip direct self references.
5802 if (IncValue == P)
5803 continue;
5804
5805 KnownFPClass KnownSrc;
5806 // Recurse, but cap the recursion to two levels, because we don't want
5807 // to waste time spinning around in loops. We need at least depth 2 to
5808 // detect known sign bits.
5809 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
5811 PhiRecursionLimit);
5812
5813 if (First) {
5814 Known = KnownSrc;
5815 First = false;
5816 } else {
5817 Known |= KnownSrc;
5818 }
5819
5820 if (Known.KnownFPClasses == fcAllFlags)
5821 break;
5822 }
5823 }
5824
5825 break;
5826 }
5827 case Instruction::BitCast: {
5828 const Value *Src;
5829 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
5830 !Src->getType()->isIntOrIntVectorTy())
5831 break;
5832
5833 const Type *Ty = Op->getType()->getScalarType();
5834 KnownBits Bits(Ty->getScalarSizeInBits());
5835 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
5836
5837 // Transfer information from the sign bit.
5838 if (Bits.isNonNegative())
5839 Known.signBitMustBeZero();
5840 else if (Bits.isNegative())
5841 Known.signBitMustBeOne();
5842
5843 if (Ty->isIEEELikeFPTy()) {
5844 // IEEE floats are NaN when all bits of the exponent plus at least one of
5845 // the fraction bits are 1. This means:
5846 // - If we assume unknown bits are 0 and the value is NaN, it will
5847 // always be NaN
5848 // - If we assume unknown bits are 1 and the value is not NaN, it can
5849 // never be NaN
5850 // Note: They do not hold for x86_fp80 format.
5851 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
5852 Known.KnownFPClasses = fcNan;
5853 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
5854 Known.knownNot(fcNan);
5855
5856 // Build KnownBits representing Inf and check if it must be equal or
5857 // unequal to this value.
5858 auto InfKB = KnownBits::makeConstant(
5859 APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());
5860 InfKB.Zero.clearSignBit();
5861 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
5862 assert(!InfResult.value());
5863 Known.knownNot(fcInf);
5864 } else if (Bits == InfKB) {
5865 Known.KnownFPClasses = fcInf;
5866 }
5867
5868 // Build KnownBits representing Zero and check if it must be equal or
5869 // unequal to this value.
5870 auto ZeroKB = KnownBits::makeConstant(
5871 APFloat::getZero(Ty->getFltSemantics()).bitcastToAPInt());
5872 ZeroKB.Zero.clearSignBit();
5873 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
5874 assert(!ZeroResult.value());
5875 Known.knownNot(fcZero);
5876 } else if (Bits == ZeroKB) {
5877 Known.KnownFPClasses = fcZero;
5878 }
5879 }
5880
5881 break;
5882 }
5883 default:
5884 break;
5885 }
5886}
5887
5889 const APInt &DemandedElts,
5890 FPClassTest InterestedClasses,
5891 const SimplifyQuery &SQ,
5892 unsigned Depth) {
5893 KnownFPClass KnownClasses;
5894 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
5895 Depth);
5896 return KnownClasses;
5897}
5898
5900 FPClassTest InterestedClasses,
5901 const SimplifyQuery &SQ,
5902 unsigned Depth) {
5903 KnownFPClass Known;
5904 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
5905 return Known;
5906}
5907
5909 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
5910 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
5911 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
5912 return computeKnownFPClass(V, InterestedClasses,
5913 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
5914 Depth);
5915}
5916
5918llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5919 FastMathFlags FMF, FPClassTest InterestedClasses,
5920 const SimplifyQuery &SQ, unsigned Depth) {
5921 if (FMF.noNaNs())
5922 InterestedClasses &= ~fcNan;
5923 if (FMF.noInfs())
5924 InterestedClasses &= ~fcInf;
5925
5926 KnownFPClass Result =
5927 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
5928
5929 if (FMF.noNaNs())
5930 Result.KnownFPClasses &= ~fcNan;
5931 if (FMF.noInfs())
5932 Result.KnownFPClasses &= ~fcInf;
5933 return Result;
5934}
5935
5937 FPClassTest InterestedClasses,
5938 const SimplifyQuery &SQ,
5939 unsigned Depth) {
5940 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5941 APInt DemandedElts =
5942 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5943 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
5944 Depth);
5945}
5946
5948 unsigned Depth) {
5950 return Known.isKnownNeverNegZero();
5951}
5952
5959
5961 unsigned Depth) {
5963 return Known.isKnownNeverInfinity();
5964}
5965
5966/// Return true if the floating-point value can never contain a NaN or infinity.
5968 unsigned Depth) {
5970 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
5971}
5972
5973/// Return true if the floating-point scalar value is not a NaN or if the
5974/// floating-point vector value has no NaN elements. Return false if a value
5975/// could ever be NaN.
5977 unsigned Depth) {
5979 return Known.isKnownNeverNaN();
5980}
5981
5982/// Return false if we can prove that the specified FP value's sign bit is 0.
5983/// Return true if we can prove that the specified FP value's sign bit is 1.
5984/// Otherwise return std::nullopt.
5985std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
5986 const SimplifyQuery &SQ,
5987 unsigned Depth) {
5989 return Known.SignBit;
5990}
5991
5993 auto *User = cast<Instruction>(U.getUser());
5994 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
5995 if (FPOp->hasNoSignedZeros())
5996 return true;
5997 }
5998
5999 switch (User->getOpcode()) {
6000 case Instruction::FPToSI:
6001 case Instruction::FPToUI:
6002 return true;
6003 case Instruction::FCmp:
6004 // fcmp treats both positive and negative zero as equal.
6005 return true;
6006 case Instruction::Call:
6007 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6008 switch (II->getIntrinsicID()) {
6009 case Intrinsic::fabs:
6010 return true;
6011 case Intrinsic::copysign:
6012 return U.getOperandNo() == 0;
6013 case Intrinsic::is_fpclass:
6014 case Intrinsic::vp_is_fpclass: {
6015 auto Test =
6016 static_cast<FPClassTest>(
6017 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6020 }
6021 default:
6022 return false;
6023 }
6024 }
6025 return false;
6026 default:
6027 return false;
6028 }
6029}
6030
6032 auto *User = cast<Instruction>(U.getUser());
6033 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6034 if (FPOp->hasNoNaNs())
6035 return true;
6036 }
6037
6038 switch (User->getOpcode()) {
6039 case Instruction::FPToSI:
6040 case Instruction::FPToUI:
6041 return true;
6042 // Proper FP math operations ignore the sign bit of NaN.
6043 case Instruction::FAdd:
6044 case Instruction::FSub:
6045 case Instruction::FMul:
6046 case Instruction::FDiv:
6047 case Instruction::FRem:
6048 case Instruction::FPTrunc:
6049 case Instruction::FPExt:
6050 case Instruction::FCmp:
6051 return true;
6052 // Bitwise FP operations should preserve the sign bit of NaN.
6053 case Instruction::FNeg:
6054 case Instruction::Select:
6055 case Instruction::PHI:
6056 return false;
6057 case Instruction::Ret:
6058 return User->getFunction()->getAttributes().getRetNoFPClass() &
6060 case Instruction::Call:
6061 case Instruction::Invoke: {
6062 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6063 switch (II->getIntrinsicID()) {
6064 case Intrinsic::fabs:
6065 return true;
6066 case Intrinsic::copysign:
6067 return U.getOperandNo() == 0;
6068 // Other proper FP math intrinsics ignore the sign bit of NaN.
6069 case Intrinsic::maxnum:
6070 case Intrinsic::minnum:
6071 case Intrinsic::maximum:
6072 case Intrinsic::minimum:
6073 case Intrinsic::maximumnum:
6074 case Intrinsic::minimumnum:
6075 case Intrinsic::canonicalize:
6076 case Intrinsic::fma:
6077 case Intrinsic::fmuladd:
6078 case Intrinsic::sqrt:
6079 case Intrinsic::pow:
6080 case Intrinsic::powi:
6081 case Intrinsic::fptoui_sat:
6082 case Intrinsic::fptosi_sat:
6083 case Intrinsic::is_fpclass:
6084 case Intrinsic::vp_is_fpclass:
6085 return true;
6086 default:
6087 return false;
6088 }
6089 }
6090
6091 FPClassTest NoFPClass =
6092 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6093 return NoFPClass & FPClassTest::fcNan;
6094 }
6095 default:
6096 return false;
6097 }
6098}
6099
6101
6102 // All byte-wide stores are splatable, even of arbitrary variables.
6103 if (V->getType()->isIntegerTy(8))
6104 return V;
6105
6106 LLVMContext &Ctx = V->getContext();
6107
6108 // Undef don't care.
6109 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6110 if (isa<UndefValue>(V))
6111 return UndefInt8;
6112
6113 // Return poison for zero-sized type.
6114 if (DL.getTypeStoreSize(V->getType()).isZero())
6115 return PoisonValue::get(Type::getInt8Ty(Ctx));
6116
6118 if (!C) {
6119 // Conceptually, we could handle things like:
6120 // %a = zext i8 %X to i16
6121 // %b = shl i16 %a, 8
6122 // %c = or i16 %a, %b
6123 // but until there is an example that actually needs this, it doesn't seem
6124 // worth worrying about.
6125 return nullptr;
6126 }
6127
6128 // Handle 'null' ConstantArrayZero etc.
6129 if (C->isNullValue())
6131
6132 // Constant floating-point values can be handled as integer values if the
6133 // corresponding integer value is "byteable". An important case is 0.0.
6134 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6135 Type *Ty = nullptr;
6136 if (CFP->getType()->isHalfTy())
6137 Ty = Type::getInt16Ty(Ctx);
6138 else if (CFP->getType()->isFloatTy())
6139 Ty = Type::getInt32Ty(Ctx);
6140 else if (CFP->getType()->isDoubleTy())
6141 Ty = Type::getInt64Ty(Ctx);
6142 // Don't handle long double formats, which have strange constraints.
6143 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6144 : nullptr;
6145 }
6146
6147 // We can handle constant integers that are multiple of 8 bits.
6148 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6149 if (CI->getBitWidth() % 8 == 0) {
6150 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6151 if (!CI->getValue().isSplat(8))
6152 return nullptr;
6153 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6154 }
6155 }
6156
6157 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6158 if (CE->getOpcode() == Instruction::IntToPtr) {
6159 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6160 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6162 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6163 return isBytewiseValue(Op, DL);
6164 }
6165 }
6166 }
6167
6168 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6169 if (LHS == RHS)
6170 return LHS;
6171 if (!LHS || !RHS)
6172 return nullptr;
6173 if (LHS == UndefInt8)
6174 return RHS;
6175 if (RHS == UndefInt8)
6176 return LHS;
6177 return nullptr;
6178 };
6179
6181 Value *Val = UndefInt8;
6182 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6183 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6184 return nullptr;
6185 return Val;
6186 }
6187
6189 Value *Val = UndefInt8;
6190 for (Value *Op : C->operands())
6191 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6192 return nullptr;
6193 return Val;
6194 }
6195
6196 // Don't try to handle the handful of other constants.
6197 return nullptr;
6198}
6199
6200// This is the recursive version of BuildSubAggregate. It takes a few different
6201// arguments. Idxs is the index within the nested struct From that we are
6202// looking at now (which is of type IndexedType). IdxSkip is the number of
6203// indices from Idxs that should be left out when inserting into the resulting
6204// struct. To is the result struct built so far, new insertvalue instructions
6205// build on that.
6206static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6208 unsigned IdxSkip,
6209 BasicBlock::iterator InsertBefore) {
6210 StructType *STy = dyn_cast<StructType>(IndexedType);
6211 if (STy) {
6212 // Save the original To argument so we can modify it
6213 Value *OrigTo = To;
6214 // General case, the type indexed by Idxs is a struct
6215 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6216 // Process each struct element recursively
6217 Idxs.push_back(i);
6218 Value *PrevTo = To;
6219 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6220 InsertBefore);
6221 Idxs.pop_back();
6222 if (!To) {
6223 // Couldn't find any inserted value for this index? Cleanup
6224 while (PrevTo != OrigTo) {
6226 PrevTo = Del->getAggregateOperand();
6227 Del->eraseFromParent();
6228 }
6229 // Stop processing elements
6230 break;
6231 }
6232 }
6233 // If we successfully found a value for each of our subaggregates
6234 if (To)
6235 return To;
6236 }
6237 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6238 // the struct's elements had a value that was inserted directly. In the latter
6239 // case, perhaps we can't determine each of the subelements individually, but
6240 // we might be able to find the complete struct somewhere.
6241
6242 // Find the value that is at that particular spot
6243 Value *V = FindInsertedValue(From, Idxs);
6244
6245 if (!V)
6246 return nullptr;
6247
6248 // Insert the value in the new (sub) aggregate
6249 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6250 InsertBefore);
6251}
6252
6253// This helper takes a nested struct and extracts a part of it (which is again a
6254// struct) into a new value. For example, given the struct:
6255// { a, { b, { c, d }, e } }
6256// and the indices "1, 1" this returns
6257// { c, d }.
6258//
6259// It does this by inserting an insertvalue for each element in the resulting
6260// struct, as opposed to just inserting a single struct. This will only work if
6261// each of the elements of the substruct are known (ie, inserted into From by an
6262// insertvalue instruction somewhere).
6263//
6264// All inserted insertvalue instructions are inserted before InsertBefore
6266 BasicBlock::iterator InsertBefore) {
6267 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6268 idx_range);
6269 Value *To = PoisonValue::get(IndexedType);
6270 SmallVector<unsigned, 10> Idxs(idx_range);
6271 unsigned IdxSkip = Idxs.size();
6272
6273 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6274}
6275
6276/// Given an aggregate and a sequence of indices, see if the scalar value
6277/// indexed is already around as a register, for example if it was inserted
6278/// directly into the aggregate.
6279///
6280/// If InsertBefore is not null, this function will duplicate (modified)
6281/// insertvalues when a part of a nested struct is extracted.
6282Value *
6284 std::optional<BasicBlock::iterator> InsertBefore) {
6285 // Nothing to index? Just return V then (this is useful at the end of our
6286 // recursion).
6287 if (idx_range.empty())
6288 return V;
6289 // We have indices, so V should have an indexable type.
6290 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6291 "Not looking at a struct or array?");
6292 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6293 "Invalid indices for type?");
6294
6295 if (Constant *C = dyn_cast<Constant>(V)) {
6296 C = C->getAggregateElement(idx_range[0]);
6297 if (!C) return nullptr;
6298 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6299 }
6300
6302 // Loop the indices for the insertvalue instruction in parallel with the
6303 // requested indices
6304 const unsigned *req_idx = idx_range.begin();
6305 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6306 i != e; ++i, ++req_idx) {
6307 if (req_idx == idx_range.end()) {
6308 // We can't handle this without inserting insertvalues
6309 if (!InsertBefore)
6310 return nullptr;
6311
6312 // The requested index identifies a part of a nested aggregate. Handle
6313 // this specially. For example,
6314 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6315 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6316 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6317 // This can be changed into
6318 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6319 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6320 // which allows the unused 0,0 element from the nested struct to be
6321 // removed.
6322 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6323 *InsertBefore);
6324 }
6325
6326 // This insert value inserts something else than what we are looking for.
6327 // See if the (aggregate) value inserted into has the value we are
6328 // looking for, then.
6329 if (*req_idx != *i)
6330 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6331 InsertBefore);
6332 }
6333 // If we end up here, the indices of the insertvalue match with those
6334 // requested (though possibly only partially). Now we recursively look at
6335 // the inserted value, passing any remaining indices.
6336 return FindInsertedValue(I->getInsertedValueOperand(),
6337 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6338 }
6339
6341 // If we're extracting a value from an aggregate that was extracted from
6342 // something else, we can extract from that something else directly instead.
6343 // However, we will need to chain I's indices with the requested indices.
6344
6345 // Calculate the number of indices required
6346 unsigned size = I->getNumIndices() + idx_range.size();
6347 // Allocate some space to put the new indices in
6349 Idxs.reserve(size);
6350 // Add indices from the extract value instruction
6351 Idxs.append(I->idx_begin(), I->idx_end());
6352
6353 // Add requested indices
6354 Idxs.append(idx_range.begin(), idx_range.end());
6355
6356 assert(Idxs.size() == size
6357 && "Number of indices added not correct?");
6358
6359 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6360 }
6361 // Otherwise, we don't know (such as, extracting from a function return value
6362 // or load instruction)
6363 return nullptr;
6364}
6365
6366// If V refers to an initialized global constant, set Slice either to
6367// its initializer if the size of its elements equals ElementSize, or,
6368// for ElementSize == 8, to its representation as an array of unsiged
6369// char. Return true on success.
6370// Offset is in the unit "nr of ElementSize sized elements".
6373 unsigned ElementSize, uint64_t Offset) {
6374 assert(V && "V should not be null.");
6375 assert((ElementSize % 8) == 0 &&
6376 "ElementSize expected to be a multiple of the size of a byte.");
6377 unsigned ElementSizeInBytes = ElementSize / 8;
6378
6379 // Drill down into the pointer expression V, ignoring any intervening
6380 // casts, and determine the identity of the object it references along
6381 // with the cumulative byte offset into it.
6382 const GlobalVariable *GV =
6384 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6385 // Fail if V is not based on constant global object.
6386 return false;
6387
6388 const DataLayout &DL = GV->getDataLayout();
6389 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6390
6391 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6392 /*AllowNonInbounds*/ true))
6393 // Fail if a constant offset could not be determined.
6394 return false;
6395
6396 uint64_t StartIdx = Off.getLimitedValue();
6397 if (StartIdx == UINT64_MAX)
6398 // Fail if the constant offset is excessive.
6399 return false;
6400
6401 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6402 // elements. Simply bail out if that isn't possible.
6403 if ((StartIdx % ElementSizeInBytes) != 0)
6404 return false;
6405
6406 Offset += StartIdx / ElementSizeInBytes;
6407 ConstantDataArray *Array = nullptr;
6408 ArrayType *ArrayTy = nullptr;
6409
6410 if (GV->getInitializer()->isNullValue()) {
6411 Type *GVTy = GV->getValueType();
6412 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6413 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6414
6415 Slice.Array = nullptr;
6416 Slice.Offset = 0;
6417 // Return an empty Slice for undersized constants to let callers
6418 // transform even undefined library calls into simpler, well-defined
6419 // expressions. This is preferable to making the calls although it
6420 // prevents sanitizers from detecting such calls.
6421 Slice.Length = Length < Offset ? 0 : Length - Offset;
6422 return true;
6423 }
6424
6425 auto *Init = const_cast<Constant *>(GV->getInitializer());
6426 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6427 Type *InitElTy = ArrayInit->getElementType();
6428 if (InitElTy->isIntegerTy(ElementSize)) {
6429 // If Init is an initializer for an array of the expected type
6430 // and size, use it as is.
6431 Array = ArrayInit;
6432 ArrayTy = ArrayInit->getType();
6433 }
6434 }
6435
6436 if (!Array) {
6437 if (ElementSize != 8)
6438 // TODO: Handle conversions to larger integral types.
6439 return false;
6440
6441 // Otherwise extract the portion of the initializer starting
6442 // at Offset as an array of bytes, and reset Offset.
6444 if (!Init)
6445 return false;
6446
6447 Offset = 0;
6449 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6450 }
6451
6452 uint64_t NumElts = ArrayTy->getArrayNumElements();
6453 if (Offset > NumElts)
6454 return false;
6455
6456 Slice.Array = Array;
6457 Slice.Offset = Offset;
6458 Slice.Length = NumElts - Offset;
6459 return true;
6460}
6461
6462/// Extract bytes from the initializer of the constant array V, which need
6463/// not be a nul-terminated string. On success, store the bytes in Str and
6464/// return true. When TrimAtNul is set, Str will contain only the bytes up
6465/// to but not including the first nul. Return false on failure.
6467 bool TrimAtNul) {
6469 if (!getConstantDataArrayInfo(V, Slice, 8))
6470 return false;
6471
6472 if (Slice.Array == nullptr) {
6473 if (TrimAtNul) {
6474 // Return a nul-terminated string even for an empty Slice. This is
6475 // safe because all existing SimplifyLibcalls callers require string
6476 // arguments and the behavior of the functions they fold is undefined
6477 // otherwise. Folding the calls this way is preferable to making
6478 // the undefined library calls, even though it prevents sanitizers
6479 // from reporting such calls.
6480 Str = StringRef();
6481 return true;
6482 }
6483 if (Slice.Length == 1) {
6484 Str = StringRef("", 1);
6485 return true;
6486 }
6487 // We cannot instantiate a StringRef as we do not have an appropriate string
6488 // of 0s at hand.
6489 return false;
6490 }
6491
6492 // Start out with the entire array in the StringRef.
6493 Str = Slice.Array->getAsString();
6494 // Skip over 'offset' bytes.
6495 Str = Str.substr(Slice.Offset);
6496
6497 if (TrimAtNul) {
6498 // Trim off the \0 and anything after it. If the array is not nul
6499 // terminated, we just return the whole end of string. The client may know
6500 // some other way that the string is length-bound.
6501 Str = Str.substr(0, Str.find('\0'));
6502 }
6503 return true;
6504}
6505
6506// These next two are very similar to the above, but also look through PHI
6507// nodes.
6508// TODO: See if we can integrate these two together.
6509
6510/// If we can compute the length of the string pointed to by
6511/// the specified pointer, return 'len+1'. If we can't, return 0.
6514 unsigned CharSize) {
6515 // Look through noop bitcast instructions.
6516 V = V->stripPointerCasts();
6517
6518 // If this is a PHI node, there are two cases: either we have already seen it
6519 // or we haven't.
6520 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6521 if (!PHIs.insert(PN).second)
6522 return ~0ULL; // already in the set.
6523
6524 // If it was new, see if all the input strings are the same length.
6525 uint64_t LenSoFar = ~0ULL;
6526 for (Value *IncValue : PN->incoming_values()) {
6527 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6528 if (Len == 0) return 0; // Unknown length -> unknown.
6529
6530 if (Len == ~0ULL) continue;
6531
6532 if (Len != LenSoFar && LenSoFar != ~0ULL)
6533 return 0; // Disagree -> unknown.
6534 LenSoFar = Len;
6535 }
6536
6537 // Success, all agree.
6538 return LenSoFar;
6539 }
6540
6541 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6542 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6543 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6544 if (Len1 == 0) return 0;
6545 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6546 if (Len2 == 0) return 0;
6547 if (Len1 == ~0ULL) return Len2;
6548 if (Len2 == ~0ULL) return Len1;
6549 if (Len1 != Len2) return 0;
6550 return Len1;
6551 }
6552
6553 // Otherwise, see if we can read the string.
6555 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6556 return 0;
6557
6558 if (Slice.Array == nullptr)
6559 // Zeroinitializer (including an empty one).
6560 return 1;
6561
6562 // Search for the first nul character. Return a conservative result even
6563 // when there is no nul. This is safe since otherwise the string function
6564 // being folded such as strlen is undefined, and can be preferable to
6565 // making the undefined library call.
6566 unsigned NullIndex = 0;
6567 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6568 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6569 break;
6570 }
6571
6572 return NullIndex + 1;
6573}
6574
6575/// If we can compute the length of the string pointed to by
6576/// the specified pointer, return 'len+1'. If we can't, return 0.
6577uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6578 if (!V->getType()->isPointerTy())
6579 return 0;
6580
6582 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6583 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6584 // an empty string as a length.
6585 return Len == ~0ULL ? 1 : Len;
6586}
6587
6588const Value *
6590 bool MustPreserveNullness) {
6591 assert(Call &&
6592 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6593 if (const Value *RV = Call->getReturnedArgOperand())
6594 return RV;
6595 // This can be used only as a aliasing property.
6597 Call, MustPreserveNullness))
6598 return Call->getArgOperand(0);
6599 return nullptr;
6600}
6601
6603 const CallBase *Call, bool MustPreserveNullness) {
6604 switch (Call->getIntrinsicID()) {
6605 case Intrinsic::launder_invariant_group:
6606 case Intrinsic::strip_invariant_group:
6607 case Intrinsic::aarch64_irg:
6608 case Intrinsic::aarch64_tagp:
6609 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6610 // input pointer (and thus preserve null-ness for the purposes of escape
6611 // analysis, which is where the MustPreserveNullness flag comes in to play).
6612 // However, it will not necessarily map ptr addrspace(N) null to ptr
6613 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6614 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6615 // list, no one should be relying on such a strict interpretation of
6616 // MustPreserveNullness (and, at time of writing, they are not), but we
6617 // document this fact out of an abundance of caution.
6618 case Intrinsic::amdgcn_make_buffer_rsrc:
6619 return true;
6620 case Intrinsic::ptrmask:
6621 return !MustPreserveNullness;
6622 case Intrinsic::threadlocal_address:
6623 // The underlying variable changes with thread ID. The Thread ID may change
6624 // at coroutine suspend points.
6625 return !Call->getParent()->getParent()->isPresplitCoroutine();
6626 default:
6627 return false;
6628 }
6629}
6630
6631/// \p PN defines a loop-variant pointer to an object. Check if the
6632/// previous iteration of the loop was referring to the same object as \p PN.
6634 const LoopInfo *LI) {
6635 // Find the loop-defined value.
6636 Loop *L = LI->getLoopFor(PN->getParent());
6637 if (PN->getNumIncomingValues() != 2)
6638 return true;
6639
6640 // Find the value from previous iteration.
6641 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6642 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6643 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6644 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6645 return true;
6646
6647 // If a new pointer is loaded in the loop, the pointer references a different
6648 // object in every iteration. E.g.:
6649 // for (i)
6650 // int *p = a[i];
6651 // ...
6652 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6653 if (!L->isLoopInvariant(Load->getPointerOperand()))
6654 return false;
6655 return true;
6656}
6657
6658const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6659 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6660 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6661 const Value *PtrOp = GEP->getPointerOperand();
6662 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6663 return V;
6664 V = PtrOp;
6665 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6666 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6667 Value *NewV = cast<Operator>(V)->getOperand(0);
6668 if (!NewV->getType()->isPointerTy())
6669 return V;
6670 V = NewV;
6671 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6672 if (GA->isInterposable())
6673 return V;
6674 V = GA->getAliasee();
6675 } else {
6676 if (auto *PHI = dyn_cast<PHINode>(V)) {
6677 // Look through single-arg phi nodes created by LCSSA.
6678 if (PHI->getNumIncomingValues() == 1) {
6679 V = PHI->getIncomingValue(0);
6680 continue;
6681 }
6682 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6683 // CaptureTracking can know about special capturing properties of some
6684 // intrinsics like launder.invariant.group, that can't be expressed with
6685 // the attributes, but have properties like returning aliasing pointer.
6686 // Because some analysis may assume that nocaptured pointer is not
6687 // returned from some special intrinsic (because function would have to
6688 // be marked with returns attribute), it is crucial to use this function
6689 // because it should be in sync with CaptureTracking. Not using it may
6690 // cause weird miscompilations where 2 aliasing pointers are assumed to
6691 // noalias.
6692 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6693 V = RP;
6694 continue;
6695 }
6696 }
6697
6698 return V;
6699 }
6700 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6701 }
6702 return V;
6703}
6704
6707 const LoopInfo *LI, unsigned MaxLookup) {
6710 Worklist.push_back(V);
6711 do {
6712 const Value *P = Worklist.pop_back_val();
6713 P = getUnderlyingObject(P, MaxLookup);
6714
6715 if (!Visited.insert(P).second)
6716 continue;
6717
6718 if (auto *SI = dyn_cast<SelectInst>(P)) {
6719 Worklist.push_back(SI->getTrueValue());
6720 Worklist.push_back(SI->getFalseValue());
6721 continue;
6722 }
6723
6724 if (auto *PN = dyn_cast<PHINode>(P)) {
6725 // If this PHI changes the underlying object in every iteration of the
6726 // loop, don't look through it. Consider:
6727 // int **A;
6728 // for (i) {
6729 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6730 // Curr = A[i];
6731 // *Prev, *Curr;
6732 //
6733 // Prev is tracking Curr one iteration behind so they refer to different
6734 // underlying objects.
6735 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6737 append_range(Worklist, PN->incoming_values());
6738 else
6739 Objects.push_back(P);
6740 continue;
6741 }
6742
6743 Objects.push_back(P);
6744 } while (!Worklist.empty());
6745}
6746
6748 const unsigned MaxVisited = 8;
6749
6752 Worklist.push_back(V);
6753 const Value *Object = nullptr;
6754 // Used as fallback if we can't find a common underlying object through
6755 // recursion.
6756 bool First = true;
6757 const Value *FirstObject = getUnderlyingObject(V);
6758 do {
6759 const Value *P = Worklist.pop_back_val();
6760 P = First ? FirstObject : getUnderlyingObject(P);
6761 First = false;
6762
6763 if (!Visited.insert(P).second)
6764 continue;
6765
6766 if (Visited.size() == MaxVisited)
6767 return FirstObject;
6768
6769 if (auto *SI = dyn_cast<SelectInst>(P)) {
6770 Worklist.push_back(SI->getTrueValue());
6771 Worklist.push_back(SI->getFalseValue());
6772 continue;
6773 }
6774
6775 if (auto *PN = dyn_cast<PHINode>(P)) {
6776 append_range(Worklist, PN->incoming_values());
6777 continue;
6778 }
6779
6780 if (!Object)
6781 Object = P;
6782 else if (Object != P)
6783 return FirstObject;
6784 } while (!Worklist.empty());
6785
6786 return Object ? Object : FirstObject;
6787}
6788
6789/// This is the function that does the work of looking through basic
6790/// ptrtoint+arithmetic+inttoptr sequences.
6791static const Value *getUnderlyingObjectFromInt(const Value *V) {
6792 do {
6793 if (const Operator *U = dyn_cast<Operator>(V)) {
6794 // If we find a ptrtoint, we can transfer control back to the
6795 // regular getUnderlyingObjectFromInt.
6796 if (U->getOpcode() == Instruction::PtrToInt)
6797 return U->getOperand(0);
6798 // If we find an add of a constant, a multiplied value, or a phi, it's
6799 // likely that the other operand will lead us to the base
6800 // object. We don't have to worry about the case where the
6801 // object address is somehow being computed by the multiply,
6802 // because our callers only care when the result is an
6803 // identifiable object.
6804 if (U->getOpcode() != Instruction::Add ||
6805 (!isa<ConstantInt>(U->getOperand(1)) &&
6806 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6807 !isa<PHINode>(U->getOperand(1))))
6808 return V;
6809 V = U->getOperand(0);
6810 } else {
6811 return V;
6812 }
6813 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6814 } while (true);
6815}
6816
6817/// This is a wrapper around getUnderlyingObjects and adds support for basic
6818/// ptrtoint+arithmetic+inttoptr sequences.
6819/// It returns false if unidentified object is found in getUnderlyingObjects.
6821 SmallVectorImpl<Value *> &Objects) {
6823 SmallVector<const Value *, 4> Working(1, V);
6824 do {
6825 V = Working.pop_back_val();
6826
6828 getUnderlyingObjects(V, Objs);
6829
6830 for (const Value *V : Objs) {
6831 if (!Visited.insert(V).second)
6832 continue;
6833 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6834 const Value *O =
6835 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6836 if (O->getType()->isPointerTy()) {
6837 Working.push_back(O);
6838 continue;
6839 }
6840 }
6841 // If getUnderlyingObjects fails to find an identifiable object,
6842 // getUnderlyingObjectsForCodeGen also fails for safety.
6843 if (!isIdentifiedObject(V)) {
6844 Objects.clear();
6845 return false;
6846 }
6847 Objects.push_back(const_cast<Value *>(V));
6848 }
6849 } while (!Working.empty());
6850 return true;
6851}
6852
6854 AllocaInst *Result = nullptr;
6856 SmallVector<Value *, 4> Worklist;
6857
6858 auto AddWork = [&](Value *V) {
6859 if (Visited.insert(V).second)
6860 Worklist.push_back(V);
6861 };
6862
6863 AddWork(V);
6864 do {
6865 V = Worklist.pop_back_val();
6866 assert(Visited.count(V));
6867
6868 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6869 if (Result && Result != AI)
6870 return nullptr;
6871 Result = AI;
6872 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
6873 AddWork(CI->getOperand(0));
6874 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
6875 for (Value *IncValue : PN->incoming_values())
6876 AddWork(IncValue);
6877 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
6878 AddWork(SI->getTrueValue());
6879 AddWork(SI->getFalseValue());
6881 if (OffsetZero && !GEP->hasAllZeroIndices())
6882 return nullptr;
6883 AddWork(GEP->getPointerOperand());
6884 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
6885 Value *Returned = CB->getReturnedArgOperand();
6886 if (Returned)
6887 AddWork(Returned);
6888 else
6889 return nullptr;
6890 } else {
6891 return nullptr;
6892 }
6893 } while (!Worklist.empty());
6894
6895 return Result;
6896}
6897
6899 const Value *V, bool AllowLifetime, bool AllowDroppable) {
6900 for (const User *U : V->users()) {
6902 if (!II)
6903 return false;
6904
6905 if (AllowLifetime && II->isLifetimeStartOrEnd())
6906 continue;
6907
6908 if (AllowDroppable && II->isDroppable())
6909 continue;
6910
6911 return false;
6912 }
6913 return true;
6914}
6915
6918 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
6919}
6922 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
6923}
6924
6926 if (auto *II = dyn_cast<IntrinsicInst>(I))
6927 return isTriviallyVectorizable(II->getIntrinsicID());
6928 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
6929 return (!Shuffle || Shuffle->isSelect()) &&
6931}
6932
6934 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
6935 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
6936 bool IgnoreUBImplyingAttrs) {
6937 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
6938 AC, DT, TLI, UseVariableInfo,
6939 IgnoreUBImplyingAttrs);
6940}
6941
6943 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
6944 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
6945 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
6946#ifndef NDEBUG
6947 if (Inst->getOpcode() != Opcode) {
6948 // Check that the operands are actually compatible with the Opcode override.
6949 auto hasEqualReturnAndLeadingOperandTypes =
6950 [](const Instruction *Inst, unsigned NumLeadingOperands) {
6951 if (Inst->getNumOperands() < NumLeadingOperands)
6952 return false;
6953 const Type *ExpectedType = Inst->getType();
6954 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
6955 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
6956 return false;
6957 return true;
6958 };
6960 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
6961 assert(!Instruction::isUnaryOp(Opcode) ||
6962 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
6963 }
6964#endif
6965
6966 switch (Opcode) {
6967 default:
6968 return true;
6969 case Instruction::UDiv:
6970 case Instruction::URem: {
6971 // x / y is undefined if y == 0.
6972 const APInt *V;
6973 if (match(Inst->getOperand(1), m_APInt(V)))
6974 return *V != 0;
6975 return false;
6976 }
6977 case Instruction::SDiv:
6978 case Instruction::SRem: {
6979 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
6980 const APInt *Numerator, *Denominator;
6981 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
6982 return false;
6983 // We cannot hoist this division if the denominator is 0.
6984 if (*Denominator == 0)
6985 return false;
6986 // It's safe to hoist if the denominator is not 0 or -1.
6987 if (!Denominator->isAllOnes())
6988 return true;
6989 // At this point we know that the denominator is -1. It is safe to hoist as
6990 // long we know that the numerator is not INT_MIN.
6991 if (match(Inst->getOperand(0), m_APInt(Numerator)))
6992 return !Numerator->isMinSignedValue();
6993 // The numerator *might* be MinSignedValue.
6994 return false;
6995 }
6996 case Instruction::Load: {
6997 if (!UseVariableInfo)
6998 return false;
6999
7000 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7001 if (!LI)
7002 return false;
7003 if (mustSuppressSpeculation(*LI))
7004 return false;
7005 const DataLayout &DL = LI->getDataLayout();
7007 LI->getType(), LI->getAlign(), DL,
7008 CtxI, AC, DT, TLI);
7009 }
7010 case Instruction::Call: {
7011 auto *CI = dyn_cast<const CallInst>(Inst);
7012 if (!CI)
7013 return false;
7014 const Function *Callee = CI->getCalledFunction();
7015
7016 // The called function could have undefined behavior or side-effects, even
7017 // if marked readnone nounwind.
7018 if (!Callee || !Callee->isSpeculatable())
7019 return false;
7020 // Since the operands may be changed after hoisting, undefined behavior may
7021 // be triggered by some UB-implying attributes.
7022 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7023 }
7024 case Instruction::VAArg:
7025 case Instruction::Alloca:
7026 case Instruction::Invoke:
7027 case Instruction::CallBr:
7028 case Instruction::PHI:
7029 case Instruction::Store:
7030 case Instruction::Ret:
7031 case Instruction::Br:
7032 case Instruction::IndirectBr:
7033 case Instruction::Switch:
7034 case Instruction::Unreachable:
7035 case Instruction::Fence:
7036 case Instruction::AtomicRMW:
7037 case Instruction::AtomicCmpXchg:
7038 case Instruction::LandingPad:
7039 case Instruction::Resume:
7040 case Instruction::CatchSwitch:
7041 case Instruction::CatchPad:
7042 case Instruction::CatchRet:
7043 case Instruction::CleanupPad:
7044 case Instruction::CleanupRet:
7045 return false; // Misc instructions which have effects
7046 }
7047}
7048
7050 if (I.mayReadOrWriteMemory())
7051 // Memory dependency possible
7052 return true;
7054 // Can't move above a maythrow call or infinite loop. Or if an
7055 // inalloca alloca, above a stacksave call.
7056 return true;
7058 // 1) Can't reorder two inf-loop calls, even if readonly
7059 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7060 // safe to speculative execute. (Inverse of above)
7061 return true;
7062 return false;
7063}
7064
7065/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7079
7080/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7083 bool ForSigned,
7084 const SimplifyQuery &SQ) {
7085 ConstantRange CR1 =
7086 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7087 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7090 return CR1.intersectWith(CR2, RangeType);
7091}
7092
7094 const Value *RHS,
7095 const SimplifyQuery &SQ,
7096 bool IsNSW) {
7097 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7098 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7099
7100 // mul nsw of two non-negative numbers is also nuw.
7101 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
7103
7104 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
7105 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
7106 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7107}
7108
7110 const Value *RHS,
7111 const SimplifyQuery &SQ) {
7112 // Multiplying n * m significant bits yields a result of n + m significant
7113 // bits. If the total number of significant bits does not exceed the
7114 // result bit width (minus 1), there is no overflow.
7115 // This means if we have enough leading sign bits in the operands
7116 // we can guarantee that the result does not overflow.
7117 // Ref: "Hacker's Delight" by Henry Warren
7118 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7119
7120 // Note that underestimating the number of sign bits gives a more
7121 // conservative answer.
7122 unsigned SignBits =
7123 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7124
7125 // First handle the easy case: if we have enough sign bits there's
7126 // definitely no overflow.
7127 if (SignBits > BitWidth + 1)
7129
7130 // There are two ambiguous cases where there can be no overflow:
7131 // SignBits == BitWidth + 1 and
7132 // SignBits == BitWidth
7133 // The second case is difficult to check, therefore we only handle the
7134 // first case.
7135 if (SignBits == BitWidth + 1) {
7136 // It overflows only when both arguments are negative and the true
7137 // product is exactly the minimum negative number.
7138 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7139 // For simplicity we just check if at least one side is not negative.
7140 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7141 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7142 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7144 }
7146}
7147
7150 const WithCache<const Value *> &RHS,
7151 const SimplifyQuery &SQ) {
7152 ConstantRange LHSRange =
7153 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7154 ConstantRange RHSRange =
7155 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7156 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7157}
7158
7159static OverflowResult
7162 const AddOperator *Add, const SimplifyQuery &SQ) {
7163 if (Add && Add->hasNoSignedWrap()) {
7165 }
7166
7167 // If LHS and RHS each have at least two sign bits, the addition will look
7168 // like
7169 //
7170 // XX..... +
7171 // YY.....
7172 //
7173 // If the carry into the most significant position is 0, X and Y can't both
7174 // be 1 and therefore the carry out of the addition is also 0.
7175 //
7176 // If the carry into the most significant position is 1, X and Y can't both
7177 // be 0 and therefore the carry out of the addition is also 1.
7178 //
7179 // Since the carry into the most significant position is always equal to
7180 // the carry out of the addition, there is no signed overflow.
7181 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7183
7184 ConstantRange LHSRange =
7185 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7186 ConstantRange RHSRange =
7187 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7188 OverflowResult OR =
7189 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7191 return OR;
7192
7193 // The remaining code needs Add to be available. Early returns if not so.
7194 if (!Add)
7196
7197 // If the sign of Add is the same as at least one of the operands, this add
7198 // CANNOT overflow. If this can be determined from the known bits of the
7199 // operands the above signedAddMayOverflow() check will have already done so.
7200 // The only other way to improve on the known bits is from an assumption, so
7201 // call computeKnownBitsFromContext() directly.
7202 bool LHSOrRHSKnownNonNegative =
7203 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7204 bool LHSOrRHSKnownNegative =
7205 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7206 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7207 KnownBits AddKnown(LHSRange.getBitWidth());
7208 computeKnownBitsFromContext(Add, AddKnown, SQ);
7209 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7210 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7212 }
7213
7215}
7216
7218 const Value *RHS,
7219 const SimplifyQuery &SQ) {
7220 // X - (X % ?)
7221 // The remainder of a value can't have greater magnitude than itself,
7222 // so the subtraction can't overflow.
7223
7224 // X - (X -nuw ?)
7225 // In the minimal case, this would simplify to "?", so there's no subtract
7226 // at all. But if this analysis is used to peek through casts, for example,
7227 // then determining no-overflow may allow other transforms.
7228
7229 // TODO: There are other patterns like this.
7230 // See simplifyICmpWithBinOpOnLHS() for candidates.
7231 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7232 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7233 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7235
7236 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7237 SQ.DL)) {
7238 if (*C)
7241 }
7242
7243 ConstantRange LHSRange =
7244 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7245 ConstantRange RHSRange =
7246 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7247 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7248}
7249
7251 const Value *RHS,
7252 const SimplifyQuery &SQ) {
7253 // X - (X % ?)
7254 // The remainder of a value can't have greater magnitude than itself,
7255 // so the subtraction can't overflow.
7256
7257 // X - (X -nsw ?)
7258 // In the minimal case, this would simplify to "?", so there's no subtract
7259 // at all. But if this analysis is used to peek through casts, for example,
7260 // then determining no-overflow may allow other transforms.
7261 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7262 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7263 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7265
7266 // If LHS and RHS each have at least two sign bits, the subtraction
7267 // cannot overflow.
7268 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7270
7271 ConstantRange LHSRange =
7272 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7273 ConstantRange RHSRange =
7274 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7275 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7276}
7277
7279 const DominatorTree &DT) {
7280 SmallVector<const BranchInst *, 2> GuardingBranches;
7282
7283 for (const User *U : WO->users()) {
7284 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7285 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7286
7287 if (EVI->getIndices()[0] == 0)
7288 Results.push_back(EVI);
7289 else {
7290 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7291
7292 for (const auto *U : EVI->users())
7293 if (const auto *B = dyn_cast<BranchInst>(U)) {
7294 assert(B->isConditional() && "How else is it using an i1?");
7295 GuardingBranches.push_back(B);
7296 }
7297 }
7298 } else {
7299 // We are using the aggregate directly in a way we don't want to analyze
7300 // here (storing it to a global, say).
7301 return false;
7302 }
7303 }
7304
7305 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7306 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7307 if (!NoWrapEdge.isSingleEdge())
7308 return false;
7309
7310 // Check if all users of the add are provably no-wrap.
7311 for (const auto *Result : Results) {
7312 // If the extractvalue itself is not executed on overflow, the we don't
7313 // need to check each use separately, since domination is transitive.
7314 if (DT.dominates(NoWrapEdge, Result->getParent()))
7315 continue;
7316
7317 for (const auto &RU : Result->uses())
7318 if (!DT.dominates(NoWrapEdge, RU))
7319 return false;
7320 }
7321
7322 return true;
7323 };
7324
7325 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7326}
7327
7328/// Shifts return poison if shiftwidth is larger than the bitwidth.
7329static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7330 auto *C = dyn_cast<Constant>(ShiftAmount);
7331 if (!C)
7332 return false;
7333
7334 // Shifts return poison if shiftwidth is larger than the bitwidth.
7336 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7337 unsigned NumElts = FVTy->getNumElements();
7338 for (unsigned i = 0; i < NumElts; ++i)
7339 ShiftAmounts.push_back(C->getAggregateElement(i));
7340 } else if (isa<ScalableVectorType>(C->getType()))
7341 return false; // Can't tell, just return false to be safe
7342 else
7343 ShiftAmounts.push_back(C);
7344
7345 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7346 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7347 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7348 });
7349
7350 return Safe;
7351}
7352
7358
7360 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7361}
7362
7364 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7365}
7366
7368 bool ConsiderFlagsAndMetadata) {
7369
7370 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7371 Op->hasPoisonGeneratingAnnotations())
7372 return true;
7373
7374 unsigned Opcode = Op->getOpcode();
7375
7376 // Check whether opcode is a poison/undef-generating operation
7377 switch (Opcode) {
7378 case Instruction::Shl:
7379 case Instruction::AShr:
7380 case Instruction::LShr:
7381 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7382 case Instruction::FPToSI:
7383 case Instruction::FPToUI:
7384 // fptosi/ui yields poison if the resulting value does not fit in the
7385 // destination type.
7386 return true;
7387 case Instruction::Call:
7388 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7389 switch (II->getIntrinsicID()) {
7390 // TODO: Add more intrinsics.
7391 case Intrinsic::ctlz:
7392 case Intrinsic::cttz:
7393 case Intrinsic::abs:
7394 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7395 return false;
7396 break;
7397 case Intrinsic::ctpop:
7398 case Intrinsic::bswap:
7399 case Intrinsic::bitreverse:
7400 case Intrinsic::fshl:
7401 case Intrinsic::fshr:
7402 case Intrinsic::smax:
7403 case Intrinsic::smin:
7404 case Intrinsic::scmp:
7405 case Intrinsic::umax:
7406 case Intrinsic::umin:
7407 case Intrinsic::ucmp:
7408 case Intrinsic::ptrmask:
7409 case Intrinsic::fptoui_sat:
7410 case Intrinsic::fptosi_sat:
7411 case Intrinsic::sadd_with_overflow:
7412 case Intrinsic::ssub_with_overflow:
7413 case Intrinsic::smul_with_overflow:
7414 case Intrinsic::uadd_with_overflow:
7415 case Intrinsic::usub_with_overflow:
7416 case Intrinsic::umul_with_overflow:
7417 case Intrinsic::sadd_sat:
7418 case Intrinsic::uadd_sat:
7419 case Intrinsic::ssub_sat:
7420 case Intrinsic::usub_sat:
7421 return false;
7422 case Intrinsic::sshl_sat:
7423 case Intrinsic::ushl_sat:
7424 return includesPoison(Kind) &&
7425 !shiftAmountKnownInRange(II->getArgOperand(1));
7426 case Intrinsic::fma:
7427 case Intrinsic::fmuladd:
7428 case Intrinsic::sqrt:
7429 case Intrinsic::powi:
7430 case Intrinsic::sin:
7431 case Intrinsic::cos:
7432 case Intrinsic::pow:
7433 case Intrinsic::log:
7434 case Intrinsic::log10:
7435 case Intrinsic::log2:
7436 case Intrinsic::exp:
7437 case Intrinsic::exp2:
7438 case Intrinsic::exp10:
7439 case Intrinsic::fabs:
7440 case Intrinsic::copysign:
7441 case Intrinsic::floor:
7442 case Intrinsic::ceil:
7443 case Intrinsic::trunc:
7444 case Intrinsic::rint:
7445 case Intrinsic::nearbyint:
7446 case Intrinsic::round:
7447 case Intrinsic::roundeven:
7448 case Intrinsic::fptrunc_round:
7449 case Intrinsic::canonicalize:
7450 case Intrinsic::arithmetic_fence:
7451 case Intrinsic::minnum:
7452 case Intrinsic::maxnum:
7453 case Intrinsic::minimum:
7454 case Intrinsic::maximum:
7455 case Intrinsic::minimumnum:
7456 case Intrinsic::maximumnum:
7457 case Intrinsic::is_fpclass:
7458 case Intrinsic::ldexp:
7459 case Intrinsic::frexp:
7460 return false;
7461 case Intrinsic::lround:
7462 case Intrinsic::llround:
7463 case Intrinsic::lrint:
7464 case Intrinsic::llrint:
7465 // If the value doesn't fit an unspecified value is returned (but this
7466 // is not poison).
7467 return false;
7468 }
7469 }
7470 [[fallthrough]];
7471 case Instruction::CallBr:
7472 case Instruction::Invoke: {
7473 const auto *CB = cast<CallBase>(Op);
7474 return !CB->hasRetAttr(Attribute::NoUndef);
7475 }
7476 case Instruction::InsertElement:
7477 case Instruction::ExtractElement: {
7478 // If index exceeds the length of the vector, it returns poison
7479 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7480 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7481 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7482 if (includesPoison(Kind))
7483 return !Idx ||
7484 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7485 return false;
7486 }
7487 case Instruction::ShuffleVector: {
7489 ? cast<ConstantExpr>(Op)->getShuffleMask()
7490 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7491 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7492 }
7493 case Instruction::FNeg:
7494 case Instruction::PHI:
7495 case Instruction::Select:
7496 case Instruction::ExtractValue:
7497 case Instruction::InsertValue:
7498 case Instruction::Freeze:
7499 case Instruction::ICmp:
7500 case Instruction::FCmp:
7501 case Instruction::GetElementPtr:
7502 return false;
7503 case Instruction::AddrSpaceCast:
7504 return true;
7505 default: {
7506 const auto *CE = dyn_cast<ConstantExpr>(Op);
7507 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7508 return false;
7509 else if (Instruction::isBinaryOp(Opcode))
7510 return false;
7511 // Be conservative and return true.
7512 return true;
7513 }
7514 }
7515}
7516
7518 bool ConsiderFlagsAndMetadata) {
7519 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7520 ConsiderFlagsAndMetadata);
7521}
7522
7523bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7524 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7525 ConsiderFlagsAndMetadata);
7526}
7527
7528static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7529 unsigned Depth) {
7530 if (ValAssumedPoison == V)
7531 return true;
7532
7533 const unsigned MaxDepth = 2;
7534 if (Depth >= MaxDepth)
7535 return false;
7536
7537 if (const auto *I = dyn_cast<Instruction>(V)) {
7538 if (any_of(I->operands(), [=](const Use &Op) {
7539 return propagatesPoison(Op) &&
7540 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7541 }))
7542 return true;
7543
7544 // V = extractvalue V0, idx
7545 // V2 = extractvalue V0, idx2
7546 // V0's elements are all poison or not. (e.g., add_with_overflow)
7547 const WithOverflowInst *II;
7549 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7550 llvm::is_contained(II->args(), ValAssumedPoison)))
7551 return true;
7552 }
7553 return false;
7554}
7555
7556static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7557 unsigned Depth) {
7558 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7559 return true;
7560
7561 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7562 return true;
7563
7564 const unsigned MaxDepth = 2;
7565 if (Depth >= MaxDepth)
7566 return false;
7567
7568 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7569 if (I && !canCreatePoison(cast<Operator>(I))) {
7570 return all_of(I->operands(), [=](const Value *Op) {
7571 return impliesPoison(Op, V, Depth + 1);
7572 });
7573 }
7574 return false;
7575}
7576
7577bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7578 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7579}
7580
7581static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7582
7584 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7585 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7587 return false;
7588
7589 if (isa<MetadataAsValue>(V))
7590 return false;
7591
7592 if (const auto *A = dyn_cast<Argument>(V)) {
7593 if (A->hasAttribute(Attribute::NoUndef) ||
7594 A->hasAttribute(Attribute::Dereferenceable) ||
7595 A->hasAttribute(Attribute::DereferenceableOrNull))
7596 return true;
7597 }
7598
7599 if (auto *C = dyn_cast<Constant>(V)) {
7600 if (isa<PoisonValue>(C))
7601 return !includesPoison(Kind);
7602
7603 if (isa<UndefValue>(C))
7604 return !includesUndef(Kind);
7605
7608 return true;
7609
7610 if (C->getType()->isVectorTy()) {
7611 if (isa<ConstantExpr>(C)) {
7612 // Scalable vectors can use a ConstantExpr to build a splat.
7613 if (Constant *SplatC = C->getSplatValue())
7614 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7615 return true;
7616 } else {
7617 if (includesUndef(Kind) && C->containsUndefElement())
7618 return false;
7619 if (includesPoison(Kind) && C->containsPoisonElement())
7620 return false;
7621 return !C->containsConstantExpression();
7622 }
7623 }
7624 }
7625
7626 // Strip cast operations from a pointer value.
7627 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7628 // inbounds with zero offset. To guarantee that the result isn't poison, the
7629 // stripped pointer is checked as it has to be pointing into an allocated
7630 // object or be null `null` to ensure `inbounds` getelement pointers with a
7631 // zero offset could not produce poison.
7632 // It can strip off addrspacecast that do not change bit representation as
7633 // well. We believe that such addrspacecast is equivalent to no-op.
7634 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7635 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7636 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7637 return true;
7638
7639 auto OpCheck = [&](const Value *V) {
7640 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7641 };
7642
7643 if (auto *Opr = dyn_cast<Operator>(V)) {
7644 // If the value is a freeze instruction, then it can never
7645 // be undef or poison.
7646 if (isa<FreezeInst>(V))
7647 return true;
7648
7649 if (const auto *CB = dyn_cast<CallBase>(V)) {
7650 if (CB->hasRetAttr(Attribute::NoUndef) ||
7651 CB->hasRetAttr(Attribute::Dereferenceable) ||
7652 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7653 return true;
7654 }
7655
7656 if (const auto *PN = dyn_cast<PHINode>(V)) {
7657 unsigned Num = PN->getNumIncomingValues();
7658 bool IsWellDefined = true;
7659 for (unsigned i = 0; i < Num; ++i) {
7660 if (PN == PN->getIncomingValue(i))
7661 continue;
7662 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7663 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7664 DT, Depth + 1, Kind)) {
7665 IsWellDefined = false;
7666 break;
7667 }
7668 }
7669 if (IsWellDefined)
7670 return true;
7671 } else if (!::canCreateUndefOrPoison(Opr, Kind,
7672 /*ConsiderFlagsAndMetadata*/ true) &&
7673 all_of(Opr->operands(), OpCheck))
7674 return true;
7675 }
7676
7677 if (auto *I = dyn_cast<LoadInst>(V))
7678 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7679 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7680 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7681 return true;
7682
7684 return true;
7685
7686 // CxtI may be null or a cloned instruction.
7687 if (!CtxI || !CtxI->getParent() || !DT)
7688 return false;
7689
7690 auto *DNode = DT->getNode(CtxI->getParent());
7691 if (!DNode)
7692 // Unreachable block
7693 return false;
7694
7695 // If V is used as a branch condition before reaching CtxI, V cannot be
7696 // undef or poison.
7697 // br V, BB1, BB2
7698 // BB1:
7699 // CtxI ; V cannot be undef or poison here
7700 auto *Dominator = DNode->getIDom();
7701 // This check is purely for compile time reasons: we can skip the IDom walk
7702 // if what we are checking for includes undef and the value is not an integer.
7703 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7704 while (Dominator) {
7705 auto *TI = Dominator->getBlock()->getTerminator();
7706
7707 Value *Cond = nullptr;
7708 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7709 if (BI->isConditional())
7710 Cond = BI->getCondition();
7711 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7712 Cond = SI->getCondition();
7713 }
7714
7715 if (Cond) {
7716 if (Cond == V)
7717 return true;
7718 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7719 // For poison, we can analyze further
7720 auto *Opr = cast<Operator>(Cond);
7721 if (any_of(Opr->operands(), [V](const Use &U) {
7722 return V == U && propagatesPoison(U);
7723 }))
7724 return true;
7725 }
7726 }
7727
7728 Dominator = Dominator->getIDom();
7729 }
7730
7731 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7732 return true;
7733
7734 return false;
7735}
7736
7738 const Instruction *CtxI,
7739 const DominatorTree *DT,
7740 unsigned Depth) {
7741 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7743}
7744
7746 const Instruction *CtxI,
7747 const DominatorTree *DT, unsigned Depth) {
7748 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7750}
7751
7753 const Instruction *CtxI,
7754 const DominatorTree *DT, unsigned Depth) {
7755 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7757}
7758
7759/// Return true if undefined behavior would provably be executed on the path to
7760/// OnPathTo if Root produced a posion result. Note that this doesn't say
7761/// anything about whether OnPathTo is actually executed or whether Root is
7762/// actually poison. This can be used to assess whether a new use of Root can
7763/// be added at a location which is control equivalent with OnPathTo (such as
7764/// immediately before it) without introducing UB which didn't previously
7765/// exist. Note that a false result conveys no information.
7767 Instruction *OnPathTo,
7768 DominatorTree *DT) {
7769 // Basic approach is to assume Root is poison, propagate poison forward
7770 // through all users we can easily track, and then check whether any of those
7771 // users are provable UB and must execute before out exiting block might
7772 // exit.
7773
7774 // The set of all recursive users we've visited (which are assumed to all be
7775 // poison because of said visit)
7778 Worklist.push_back(Root);
7779 while (!Worklist.empty()) {
7780 const Instruction *I = Worklist.pop_back_val();
7781
7782 // If we know this must trigger UB on a path leading our target.
7783 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7784 return true;
7785
7786 // If we can't analyze propagation through this instruction, just skip it
7787 // and transitive users. Safe as false is a conservative result.
7788 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7789 return KnownPoison.contains(U) && propagatesPoison(U);
7790 }))
7791 continue;
7792
7793 if (KnownPoison.insert(I).second)
7794 for (const User *User : I->users())
7795 Worklist.push_back(cast<Instruction>(User));
7796 }
7797
7798 // Might be non-UB, or might have a path we couldn't prove must execute on
7799 // way to exiting bb.
7800 return false;
7801}
7802
7804 const SimplifyQuery &SQ) {
7805 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7806 Add, SQ);
7807}
7808
7811 const WithCache<const Value *> &RHS,
7812 const SimplifyQuery &SQ) {
7813 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7814}
7815
7817 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7818 // of time because it's possible for another thread to interfere with it for an
7819 // arbitrary length of time, but programs aren't allowed to rely on that.
7820
7821 // If there is no successor, then execution can't transfer to it.
7822 if (isa<ReturnInst>(I))
7823 return false;
7825 return false;
7826
7827 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7828 // Instruction::willReturn.
7829 //
7830 // FIXME: Move this check into Instruction::willReturn.
7831 if (isa<CatchPadInst>(I)) {
7832 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7833 default:
7834 // A catchpad may invoke exception object constructors and such, which
7835 // in some languages can be arbitrary code, so be conservative by default.
7836 return false;
7838 // For CoreCLR, it just involves a type test.
7839 return true;
7840 }
7841 }
7842
7843 // An instruction that returns without throwing must transfer control flow
7844 // to a successor.
7845 return !I->mayThrow() && I->willReturn();
7846}
7847
7849 // TODO: This is slightly conservative for invoke instruction since exiting
7850 // via an exception *is* normal control for them.
7851 for (const Instruction &I : *BB)
7853 return false;
7854 return true;
7855}
7856
7863
7866 assert(ScanLimit && "scan limit must be non-zero");
7867 for (const Instruction &I : Range) {
7868 if (--ScanLimit == 0)
7869 return false;
7871 return false;
7872 }
7873 return true;
7874}
7875
7877 const Loop *L) {
7878 // The loop header is guaranteed to be executed for every iteration.
7879 //
7880 // FIXME: Relax this constraint to cover all basic blocks that are
7881 // guaranteed to be executed at every iteration.
7882 if (I->getParent() != L->getHeader()) return false;
7883
7884 for (const Instruction &LI : *L->getHeader()) {
7885 if (&LI == I) return true;
7886 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7887 }
7888 llvm_unreachable("Instruction not contained in its own parent basic block.");
7889}
7890
7892 switch (IID) {
7893 // TODO: Add more intrinsics.
7894 case Intrinsic::sadd_with_overflow:
7895 case Intrinsic::ssub_with_overflow:
7896 case Intrinsic::smul_with_overflow:
7897 case Intrinsic::uadd_with_overflow:
7898 case Intrinsic::usub_with_overflow:
7899 case Intrinsic::umul_with_overflow:
7900 // If an input is a vector containing a poison element, the
7901 // two output vectors (calculated results, overflow bits)'
7902 // corresponding lanes are poison.
7903 return true;
7904 case Intrinsic::ctpop:
7905 case Intrinsic::ctlz:
7906 case Intrinsic::cttz:
7907 case Intrinsic::abs:
7908 case Intrinsic::smax:
7909 case Intrinsic::smin:
7910 case Intrinsic::umax:
7911 case Intrinsic::umin:
7912 case Intrinsic::scmp:
7913 case Intrinsic::is_fpclass:
7914 case Intrinsic::ptrmask:
7915 case Intrinsic::ucmp:
7916 case Intrinsic::bitreverse:
7917 case Intrinsic::bswap:
7918 case Intrinsic::sadd_sat:
7919 case Intrinsic::ssub_sat:
7920 case Intrinsic::sshl_sat:
7921 case Intrinsic::uadd_sat:
7922 case Intrinsic::usub_sat:
7923 case Intrinsic::ushl_sat:
7924 case Intrinsic::smul_fix:
7925 case Intrinsic::smul_fix_sat:
7926 case Intrinsic::umul_fix:
7927 case Intrinsic::umul_fix_sat:
7928 case Intrinsic::pow:
7929 case Intrinsic::powi:
7930 case Intrinsic::sin:
7931 case Intrinsic::sinh:
7932 case Intrinsic::cos:
7933 case Intrinsic::cosh:
7934 case Intrinsic::sincos:
7935 case Intrinsic::sincospi:
7936 case Intrinsic::tan:
7937 case Intrinsic::tanh:
7938 case Intrinsic::asin:
7939 case Intrinsic::acos:
7940 case Intrinsic::atan:
7941 case Intrinsic::atan2:
7942 case Intrinsic::canonicalize:
7943 case Intrinsic::sqrt:
7944 case Intrinsic::exp:
7945 case Intrinsic::exp2:
7946 case Intrinsic::exp10:
7947 case Intrinsic::log:
7948 case Intrinsic::log2:
7949 case Intrinsic::log10:
7950 case Intrinsic::modf:
7951 case Intrinsic::floor:
7952 case Intrinsic::ceil:
7953 case Intrinsic::trunc:
7954 case Intrinsic::rint:
7955 case Intrinsic::nearbyint:
7956 case Intrinsic::round:
7957 case Intrinsic::roundeven:
7958 case Intrinsic::lrint:
7959 case Intrinsic::llrint:
7960 return true;
7961 default:
7962 return false;
7963 }
7964}
7965
7966bool llvm::propagatesPoison(const Use &PoisonOp) {
7967 const Operator *I = cast<Operator>(PoisonOp.getUser());
7968 switch (I->getOpcode()) {
7969 case Instruction::Freeze:
7970 case Instruction::PHI:
7971 case Instruction::Invoke:
7972 return false;
7973 case Instruction::Select:
7974 return PoisonOp.getOperandNo() == 0;
7975 case Instruction::Call:
7976 if (auto *II = dyn_cast<IntrinsicInst>(I))
7977 return intrinsicPropagatesPoison(II->getIntrinsicID());
7978 return false;
7979 case Instruction::ICmp:
7980 case Instruction::FCmp:
7981 case Instruction::GetElementPtr:
7982 return true;
7983 default:
7985 return true;
7986
7987 // Be conservative and return false.
7988 return false;
7989 }
7990}
7991
7992/// Enumerates all operands of \p I that are guaranteed to not be undef or
7993/// poison. If the callback \p Handle returns true, stop processing and return
7994/// true. Otherwise, return false.
7995template <typename CallableT>
7997 const CallableT &Handle) {
7998 switch (I->getOpcode()) {
7999 case Instruction::Store:
8000 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8001 return true;
8002 break;
8003
8004 case Instruction::Load:
8005 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8006 return true;
8007 break;
8008
8009 // Since dereferenceable attribute imply noundef, atomic operations
8010 // also implicitly have noundef pointers too
8011 case Instruction::AtomicCmpXchg:
8013 return true;
8014 break;
8015
8016 case Instruction::AtomicRMW:
8017 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8018 return true;
8019 break;
8020
8021 case Instruction::Call:
8022 case Instruction::Invoke: {
8023 const CallBase *CB = cast<CallBase>(I);
8024 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8025 return true;
8026 for (unsigned i = 0; i < CB->arg_size(); ++i)
8027 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8028 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8029 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8030 Handle(CB->getArgOperand(i)))
8031 return true;
8032 break;
8033 }
8034 case Instruction::Ret:
8035 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8036 Handle(I->getOperand(0)))
8037 return true;
8038 break;
8039 case Instruction::Switch:
8040 if (Handle(cast<SwitchInst>(I)->getCondition()))
8041 return true;
8042 break;
8043 case Instruction::Br: {
8044 auto *BR = cast<BranchInst>(I);
8045 if (BR->isConditional() && Handle(BR->getCondition()))
8046 return true;
8047 break;
8048 }
8049 default:
8050 break;
8051 }
8052
8053 return false;
8054}
8055
8056/// Enumerates all operands of \p I that are guaranteed to not be poison.
8057template <typename CallableT>
8059 const CallableT &Handle) {
8060 if (handleGuaranteedWellDefinedOps(I, Handle))
8061 return true;
8062 switch (I->getOpcode()) {
8063 // Divisors of these operations are allowed to be partially undef.
8064 case Instruction::UDiv:
8065 case Instruction::SDiv:
8066 case Instruction::URem:
8067 case Instruction::SRem:
8068 return Handle(I->getOperand(1));
8069 default:
8070 return false;
8071 }
8072}
8073
8075 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8077 I, [&](const Value *V) { return KnownPoison.count(V); });
8078}
8079
8081 bool PoisonOnly) {
8082 // We currently only look for uses of values within the same basic
8083 // block, as that makes it easier to guarantee that the uses will be
8084 // executed given that Inst is executed.
8085 //
8086 // FIXME: Expand this to consider uses beyond the same basic block. To do
8087 // this, look out for the distinction between post-dominance and strong
8088 // post-dominance.
8089 const BasicBlock *BB = nullptr;
8091 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8092 BB = Inst->getParent();
8093 Begin = Inst->getIterator();
8094 Begin++;
8095 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8096 if (Arg->getParent()->isDeclaration())
8097 return false;
8098 BB = &Arg->getParent()->getEntryBlock();
8099 Begin = BB->begin();
8100 } else {
8101 return false;
8102 }
8103
8104 // Limit number of instructions we look at, to avoid scanning through large
8105 // blocks. The current limit is chosen arbitrarily.
8106 unsigned ScanLimit = 32;
8107 BasicBlock::const_iterator End = BB->end();
8108
8109 if (!PoisonOnly) {
8110 // Since undef does not propagate eagerly, be conservative & just check
8111 // whether a value is directly passed to an instruction that must take
8112 // well-defined operands.
8113
8114 for (const auto &I : make_range(Begin, End)) {
8115 if (--ScanLimit == 0)
8116 break;
8117
8118 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8119 return WellDefinedOp == V;
8120 }))
8121 return true;
8122
8124 break;
8125 }
8126 return false;
8127 }
8128
8129 // Set of instructions that we have proved will yield poison if Inst
8130 // does.
8131 SmallPtrSet<const Value *, 16> YieldsPoison;
8133
8134 YieldsPoison.insert(V);
8135 Visited.insert(BB);
8136
8137 while (true) {
8138 for (const auto &I : make_range(Begin, End)) {
8139 if (--ScanLimit == 0)
8140 return false;
8141 if (mustTriggerUB(&I, YieldsPoison))
8142 return true;
8144 return false;
8145
8146 // If an operand is poison and propagates it, mark I as yielding poison.
8147 for (const Use &Op : I.operands()) {
8148 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8149 YieldsPoison.insert(&I);
8150 break;
8151 }
8152 }
8153
8154 // Special handling for select, which returns poison if its operand 0 is
8155 // poison (handled in the loop above) *or* if both its true/false operands
8156 // are poison (handled here).
8157 if (I.getOpcode() == Instruction::Select &&
8158 YieldsPoison.count(I.getOperand(1)) &&
8159 YieldsPoison.count(I.getOperand(2))) {
8160 YieldsPoison.insert(&I);
8161 }
8162 }
8163
8164 BB = BB->getSingleSuccessor();
8165 if (!BB || !Visited.insert(BB).second)
8166 break;
8167
8168 Begin = BB->getFirstNonPHIIt();
8169 End = BB->end();
8170 }
8171 return false;
8172}
8173
8175 return ::programUndefinedIfUndefOrPoison(Inst, false);
8176}
8177
8179 return ::programUndefinedIfUndefOrPoison(Inst, true);
8180}
8181
8182static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8183 if (FMF.noNaNs())
8184 return true;
8185
8186 if (auto *C = dyn_cast<ConstantFP>(V))
8187 return !C->isNaN();
8188
8189 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8190 if (!C->getElementType()->isFloatingPointTy())
8191 return false;
8192 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8193 if (C->getElementAsAPFloat(I).isNaN())
8194 return false;
8195 }
8196 return true;
8197 }
8198
8200 return true;
8201
8202 return false;
8203}
8204
8205static bool isKnownNonZero(const Value *V) {
8206 if (auto *C = dyn_cast<ConstantFP>(V))
8207 return !C->isZero();
8208
8209 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8210 if (!C->getElementType()->isFloatingPointTy())
8211 return false;
8212 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8213 if (C->getElementAsAPFloat(I).isZero())
8214 return false;
8215 }
8216 return true;
8217 }
8218
8219 return false;
8220}
8221
8222/// Match clamp pattern for float types without care about NaNs or signed zeros.
8223/// Given non-min/max outer cmp/select from the clamp pattern this
8224/// function recognizes if it can be substitued by a "canonical" min/max
8225/// pattern.
8227 Value *CmpLHS, Value *CmpRHS,
8228 Value *TrueVal, Value *FalseVal,
8229 Value *&LHS, Value *&RHS) {
8230 // Try to match
8231 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8232 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8233 // and return description of the outer Max/Min.
8234
8235 // First, check if select has inverse order:
8236 if (CmpRHS == FalseVal) {
8237 std::swap(TrueVal, FalseVal);
8238 Pred = CmpInst::getInversePredicate(Pred);
8239 }
8240
8241 // Assume success now. If there's no match, callers should not use these anyway.
8242 LHS = TrueVal;
8243 RHS = FalseVal;
8244
8245 const APFloat *FC1;
8246 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8247 return {SPF_UNKNOWN, SPNB_NA, false};
8248
8249 const APFloat *FC2;
8250 switch (Pred) {
8251 case CmpInst::FCMP_OLT:
8252 case CmpInst::FCMP_OLE:
8253 case CmpInst::FCMP_ULT:
8254 case CmpInst::FCMP_ULE:
8255 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8256 *FC1 < *FC2)
8257 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8258 break;
8259 case CmpInst::FCMP_OGT:
8260 case CmpInst::FCMP_OGE:
8261 case CmpInst::FCMP_UGT:
8262 case CmpInst::FCMP_UGE:
8263 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8264 *FC1 > *FC2)
8265 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8266 break;
8267 default:
8268 break;
8269 }
8270
8271 return {SPF_UNKNOWN, SPNB_NA, false};
8272}
8273
8274/// Recognize variations of:
8275/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8277 Value *CmpLHS, Value *CmpRHS,
8278 Value *TrueVal, Value *FalseVal) {
8279 // Swap the select operands and predicate to match the patterns below.
8280 if (CmpRHS != TrueVal) {
8281 Pred = ICmpInst::getSwappedPredicate(Pred);
8282 std::swap(TrueVal, FalseVal);
8283 }
8284 const APInt *C1;
8285 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8286 const APInt *C2;
8287 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8288 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8289 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8290 return {SPF_SMAX, SPNB_NA, false};
8291
8292 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8293 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8294 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8295 return {SPF_SMIN, SPNB_NA, false};
8296
8297 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8298 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8299 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8300 return {SPF_UMAX, SPNB_NA, false};
8301
8302 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8303 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8304 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8305 return {SPF_UMIN, SPNB_NA, false};
8306 }
8307 return {SPF_UNKNOWN, SPNB_NA, false};
8308}
8309
8310/// Recognize variations of:
8311/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8313 Value *CmpLHS, Value *CmpRHS,
8314 Value *TVal, Value *FVal,
8315 unsigned Depth) {
8316 // TODO: Allow FP min/max with nnan/nsz.
8317 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8318
8319 Value *A = nullptr, *B = nullptr;
8320 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8321 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8322 return {SPF_UNKNOWN, SPNB_NA, false};
8323
8324 Value *C = nullptr, *D = nullptr;
8325 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8326 if (L.Flavor != R.Flavor)
8327 return {SPF_UNKNOWN, SPNB_NA, false};
8328
8329 // We have something like: x Pred y ? min(a, b) : min(c, d).
8330 // Try to match the compare to the min/max operations of the select operands.
8331 // First, make sure we have the right compare predicate.
8332 switch (L.Flavor) {
8333 case SPF_SMIN:
8334 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8335 Pred = ICmpInst::getSwappedPredicate(Pred);
8336 std::swap(CmpLHS, CmpRHS);
8337 }
8338 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8339 break;
8340 return {SPF_UNKNOWN, SPNB_NA, false};
8341 case SPF_SMAX:
8342 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8343 Pred = ICmpInst::getSwappedPredicate(Pred);
8344 std::swap(CmpLHS, CmpRHS);
8345 }
8346 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8347 break;
8348 return {SPF_UNKNOWN, SPNB_NA, false};
8349 case SPF_UMIN:
8350 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8351 Pred = ICmpInst::getSwappedPredicate(Pred);
8352 std::swap(CmpLHS, CmpRHS);
8353 }
8354 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8355 break;
8356 return {SPF_UNKNOWN, SPNB_NA, false};
8357 case SPF_UMAX:
8358 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8359 Pred = ICmpInst::getSwappedPredicate(Pred);
8360 std::swap(CmpLHS, CmpRHS);
8361 }
8362 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8363 break;
8364 return {SPF_UNKNOWN, SPNB_NA, false};
8365 default:
8366 return {SPF_UNKNOWN, SPNB_NA, false};
8367 }
8368
8369 // If there is a common operand in the already matched min/max and the other
8370 // min/max operands match the compare operands (either directly or inverted),
8371 // then this is min/max of the same flavor.
8372
8373 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8374 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8375 if (D == B) {
8376 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8377 match(A, m_Not(m_Specific(CmpRHS)))))
8378 return {L.Flavor, SPNB_NA, false};
8379 }
8380 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8381 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8382 if (C == B) {
8383 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8384 match(A, m_Not(m_Specific(CmpRHS)))))
8385 return {L.Flavor, SPNB_NA, false};
8386 }
8387 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8388 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8389 if (D == A) {
8390 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8391 match(B, m_Not(m_Specific(CmpRHS)))))
8392 return {L.Flavor, SPNB_NA, false};
8393 }
8394 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8395 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8396 if (C == A) {
8397 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8398 match(B, m_Not(m_Specific(CmpRHS)))))
8399 return {L.Flavor, SPNB_NA, false};
8400 }
8401
8402 return {SPF_UNKNOWN, SPNB_NA, false};
8403}
8404
8405/// If the input value is the result of a 'not' op, constant integer, or vector
8406/// splat of a constant integer, return the bitwise-not source value.
8407/// TODO: This could be extended to handle non-splat vector integer constants.
8409 Value *NotV;
8410 if (match(V, m_Not(m_Value(NotV))))
8411 return NotV;
8412
8413 const APInt *C;
8414 if (match(V, m_APInt(C)))
8415 return ConstantInt::get(V->getType(), ~(*C));
8416
8417 return nullptr;
8418}
8419
8420/// Match non-obvious integer minimum and maximum sequences.
8422 Value *CmpLHS, Value *CmpRHS,
8423 Value *TrueVal, Value *FalseVal,
8424 Value *&LHS, Value *&RHS,
8425 unsigned Depth) {
8426 // Assume success. If there's no match, callers should not use these anyway.
8427 LHS = TrueVal;
8428 RHS = FalseVal;
8429
8430 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8432 return SPR;
8433
8434 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8436 return SPR;
8437
8438 // Look through 'not' ops to find disguised min/max.
8439 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8440 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8441 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8442 switch (Pred) {
8443 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8444 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8445 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8446 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8447 default: break;
8448 }
8449 }
8450
8451 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8452 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8453 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8454 switch (Pred) {
8455 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8456 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8457 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8458 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8459 default: break;
8460 }
8461 }
8462
8463 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8464 return {SPF_UNKNOWN, SPNB_NA, false};
8465
8466 const APInt *C1;
8467 if (!match(CmpRHS, m_APInt(C1)))
8468 return {SPF_UNKNOWN, SPNB_NA, false};
8469
8470 // An unsigned min/max can be written with a signed compare.
8471 const APInt *C2;
8472 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8473 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8474 // Is the sign bit set?
8475 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8476 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8477 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8478 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8479
8480 // Is the sign bit clear?
8481 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8482 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8483 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8484 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8485 }
8486
8487 return {SPF_UNKNOWN, SPNB_NA, false};
8488}
8489
8490bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8491 bool AllowPoison) {
8492 assert(X && Y && "Invalid operand");
8493
8494 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8495 if (!match(X, m_Neg(m_Specific(Y))))
8496 return false;
8497
8498 auto *BO = cast<BinaryOperator>(X);
8499 if (NeedNSW && !BO->hasNoSignedWrap())
8500 return false;
8501
8502 auto *Zero = cast<Constant>(BO->getOperand(0));
8503 if (!AllowPoison && !Zero->isNullValue())
8504 return false;
8505
8506 return true;
8507 };
8508
8509 // X = -Y or Y = -X
8510 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8511 return true;
8512
8513 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8514 Value *A, *B;
8515 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8516 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8517 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8519}
8520
8521bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8522 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8523 Value *A, *B, *C;
8524 CmpPredicate Pred1, Pred2;
8525 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8526 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8527 return false;
8528
8529 // They must both have samesign flag or not.
8530 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8531 return false;
8532
8533 if (B == C)
8534 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8535
8536 // Try to infer the relationship from constant ranges.
8537 const APInt *RHSC1, *RHSC2;
8538 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8539 return false;
8540
8541 // Sign bits of two RHSCs should match.
8542 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8543 return false;
8544
8545 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8546 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8547
8548 return CR1.inverse() == CR2;
8549}
8550
8552 SelectPatternNaNBehavior NaNBehavior,
8553 bool Ordered) {
8554 switch (Pred) {
8555 default:
8556 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8557 case ICmpInst::ICMP_UGT:
8558 case ICmpInst::ICMP_UGE:
8559 return {SPF_UMAX, SPNB_NA, false};
8560 case ICmpInst::ICMP_SGT:
8561 case ICmpInst::ICMP_SGE:
8562 return {SPF_SMAX, SPNB_NA, false};
8563 case ICmpInst::ICMP_ULT:
8564 case ICmpInst::ICMP_ULE:
8565 return {SPF_UMIN, SPNB_NA, false};
8566 case ICmpInst::ICMP_SLT:
8567 case ICmpInst::ICMP_SLE:
8568 return {SPF_SMIN, SPNB_NA, false};
8569 case FCmpInst::FCMP_UGT:
8570 case FCmpInst::FCMP_UGE:
8571 case FCmpInst::FCMP_OGT:
8572 case FCmpInst::FCMP_OGE:
8573 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8574 case FCmpInst::FCMP_ULT:
8575 case FCmpInst::FCMP_ULE:
8576 case FCmpInst::FCMP_OLT:
8577 case FCmpInst::FCMP_OLE:
8578 return {SPF_FMINNUM, NaNBehavior, Ordered};
8579 }
8580}
8581
8582std::optional<std::pair<CmpPredicate, Constant *>>
8585 "Only for relational integer predicates.");
8586 if (isa<UndefValue>(C))
8587 return std::nullopt;
8588
8589 Type *Type = C->getType();
8590 bool IsSigned = ICmpInst::isSigned(Pred);
8591
8593 bool WillIncrement =
8594 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8595
8596 // Check if the constant operand can be safely incremented/decremented
8597 // without overflowing/underflowing.
8598 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8599 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8600 };
8601
8602 Constant *SafeReplacementConstant = nullptr;
8603 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8604 // Bail out if the constant can't be safely incremented/decremented.
8605 if (!ConstantIsOk(CI))
8606 return std::nullopt;
8607 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8608 unsigned NumElts = FVTy->getNumElements();
8609 for (unsigned i = 0; i != NumElts; ++i) {
8610 Constant *Elt = C->getAggregateElement(i);
8611 if (!Elt)
8612 return std::nullopt;
8613
8614 if (isa<UndefValue>(Elt))
8615 continue;
8616
8617 // Bail out if we can't determine if this constant is min/max or if we
8618 // know that this constant is min/max.
8619 auto *CI = dyn_cast<ConstantInt>(Elt);
8620 if (!CI || !ConstantIsOk(CI))
8621 return std::nullopt;
8622
8623 if (!SafeReplacementConstant)
8624 SafeReplacementConstant = CI;
8625 }
8626 } else if (isa<VectorType>(C->getType())) {
8627 // Handle scalable splat
8628 Value *SplatC = C->getSplatValue();
8629 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8630 // Bail out if the constant can't be safely incremented/decremented.
8631 if (!CI || !ConstantIsOk(CI))
8632 return std::nullopt;
8633 } else {
8634 // ConstantExpr?
8635 return std::nullopt;
8636 }
8637
8638 // It may not be safe to change a compare predicate in the presence of
8639 // undefined elements, so replace those elements with the first safe constant
8640 // that we found.
8641 // TODO: in case of poison, it is safe; let's replace undefs only.
8642 if (C->containsUndefOrPoisonElement()) {
8643 assert(SafeReplacementConstant && "Replacement constant not set");
8644 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8645 }
8646
8648
8649 // Increment or decrement the constant.
8650 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8651 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8652
8653 return std::make_pair(NewPred, NewC);
8654}
8655
8657 FastMathFlags FMF,
8658 Value *CmpLHS, Value *CmpRHS,
8659 Value *TrueVal, Value *FalseVal,
8660 Value *&LHS, Value *&RHS,
8661 unsigned Depth) {
8662 bool HasMismatchedZeros = false;
8663 if (CmpInst::isFPPredicate(Pred)) {
8664 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8665 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8666 // purpose of identifying min/max. Disregard vector constants with undefined
8667 // elements because those can not be back-propagated for analysis.
8668 Value *OutputZeroVal = nullptr;
8669 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8670 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8671 OutputZeroVal = TrueVal;
8672 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8673 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8674 OutputZeroVal = FalseVal;
8675
8676 if (OutputZeroVal) {
8677 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8678 HasMismatchedZeros = true;
8679 CmpLHS = OutputZeroVal;
8680 }
8681 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8682 HasMismatchedZeros = true;
8683 CmpRHS = OutputZeroVal;
8684 }
8685 }
8686 }
8687
8688 LHS = CmpLHS;
8689 RHS = CmpRHS;
8690
8691 // Signed zero may return inconsistent results between implementations.
8692 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8693 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8694 // Therefore, we behave conservatively and only proceed if at least one of the
8695 // operands is known to not be zero or if we don't care about signed zero.
8696 switch (Pred) {
8697 default: break;
8700 if (!HasMismatchedZeros)
8701 break;
8702 [[fallthrough]];
8705 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8706 !isKnownNonZero(CmpRHS))
8707 return {SPF_UNKNOWN, SPNB_NA, false};
8708 }
8709
8710 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8711 bool Ordered = false;
8712
8713 // When given one NaN and one non-NaN input:
8714 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8715 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8716 // ordered comparison fails), which could be NaN or non-NaN.
8717 // so here we discover exactly what NaN behavior is required/accepted.
8718 if (CmpInst::isFPPredicate(Pred)) {
8719 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8720 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8721
8722 if (LHSSafe && RHSSafe) {
8723 // Both operands are known non-NaN.
8724 NaNBehavior = SPNB_RETURNS_ANY;
8725 Ordered = CmpInst::isOrdered(Pred);
8726 } else if (CmpInst::isOrdered(Pred)) {
8727 // An ordered comparison will return false when given a NaN, so it
8728 // returns the RHS.
8729 Ordered = true;
8730 if (LHSSafe)
8731 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8732 NaNBehavior = SPNB_RETURNS_NAN;
8733 else if (RHSSafe)
8734 NaNBehavior = SPNB_RETURNS_OTHER;
8735 else
8736 // Completely unsafe.
8737 return {SPF_UNKNOWN, SPNB_NA, false};
8738 } else {
8739 Ordered = false;
8740 // An unordered comparison will return true when given a NaN, so it
8741 // returns the LHS.
8742 if (LHSSafe)
8743 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8744 NaNBehavior = SPNB_RETURNS_OTHER;
8745 else if (RHSSafe)
8746 NaNBehavior = SPNB_RETURNS_NAN;
8747 else
8748 // Completely unsafe.
8749 return {SPF_UNKNOWN, SPNB_NA, false};
8750 }
8751 }
8752
8753 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8754 std::swap(CmpLHS, CmpRHS);
8755 Pred = CmpInst::getSwappedPredicate(Pred);
8756 if (NaNBehavior == SPNB_RETURNS_NAN)
8757 NaNBehavior = SPNB_RETURNS_OTHER;
8758 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8759 NaNBehavior = SPNB_RETURNS_NAN;
8760 Ordered = !Ordered;
8761 }
8762
8763 // ([if]cmp X, Y) ? X : Y
8764 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8765 return getSelectPattern(Pred, NaNBehavior, Ordered);
8766
8767 if (isKnownNegation(TrueVal, FalseVal)) {
8768 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8769 // match against either LHS or sext(LHS).
8770 auto MaybeSExtCmpLHS =
8771 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8772 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8773 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8774 if (match(TrueVal, MaybeSExtCmpLHS)) {
8775 // Set the return values. If the compare uses the negated value (-X >s 0),
8776 // swap the return values because the negated value is always 'RHS'.
8777 LHS = TrueVal;
8778 RHS = FalseVal;
8779 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8780 std::swap(LHS, RHS);
8781
8782 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8783 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8784 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8785 return {SPF_ABS, SPNB_NA, false};
8786
8787 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8788 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8789 return {SPF_ABS, SPNB_NA, false};
8790
8791 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8792 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8793 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8794 return {SPF_NABS, SPNB_NA, false};
8795 }
8796 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8797 // Set the return values. If the compare uses the negated value (-X >s 0),
8798 // swap the return values because the negated value is always 'RHS'.
8799 LHS = FalseVal;
8800 RHS = TrueVal;
8801 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8802 std::swap(LHS, RHS);
8803
8804 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8805 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8806 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8807 return {SPF_NABS, SPNB_NA, false};
8808
8809 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8810 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8811 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8812 return {SPF_ABS, SPNB_NA, false};
8813 }
8814 }
8815
8816 if (CmpInst::isIntPredicate(Pred))
8817 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8818
8819 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8820 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8821 // semantics than minNum. Be conservative in such case.
8822 if (NaNBehavior != SPNB_RETURNS_ANY ||
8823 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8824 !isKnownNonZero(CmpRHS)))
8825 return {SPF_UNKNOWN, SPNB_NA, false};
8826
8827 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8828}
8829
8831 Instruction::CastOps *CastOp) {
8832 const DataLayout &DL = CmpI->getDataLayout();
8833
8834 Constant *CastedTo = nullptr;
8835 switch (*CastOp) {
8836 case Instruction::ZExt:
8837 if (CmpI->isUnsigned())
8838 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8839 break;
8840 case Instruction::SExt:
8841 if (CmpI->isSigned())
8842 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8843 break;
8844 case Instruction::Trunc:
8845 Constant *CmpConst;
8846 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8847 CmpConst->getType() == SrcTy) {
8848 // Here we have the following case:
8849 //
8850 // %cond = cmp iN %x, CmpConst
8851 // %tr = trunc iN %x to iK
8852 // %narrowsel = select i1 %cond, iK %t, iK C
8853 //
8854 // We can always move trunc after select operation:
8855 //
8856 // %cond = cmp iN %x, CmpConst
8857 // %widesel = select i1 %cond, iN %x, iN CmpConst
8858 // %tr = trunc iN %widesel to iK
8859 //
8860 // Note that C could be extended in any way because we don't care about
8861 // upper bits after truncation. It can't be abs pattern, because it would
8862 // look like:
8863 //
8864 // select i1 %cond, x, -x.
8865 //
8866 // So only min/max pattern could be matched. Such match requires widened C
8867 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8868 // CmpConst == C is checked below.
8869 CastedTo = CmpConst;
8870 } else {
8871 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8872 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8873 }
8874 break;
8875 case Instruction::FPTrunc:
8876 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8877 break;
8878 case Instruction::FPExt:
8879 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8880 break;
8881 case Instruction::FPToUI:
8882 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8883 break;
8884 case Instruction::FPToSI:
8885 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8886 break;
8887 case Instruction::UIToFP:
8888 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8889 break;
8890 case Instruction::SIToFP:
8891 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8892 break;
8893 default:
8894 break;
8895 }
8896
8897 if (!CastedTo)
8898 return nullptr;
8899
8900 // Make sure the cast doesn't lose any information.
8901 Constant *CastedBack =
8902 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8903 if (CastedBack && CastedBack != C)
8904 return nullptr;
8905
8906 return CastedTo;
8907}
8908
8909/// Helps to match a select pattern in case of a type mismatch.
8910///
8911/// The function processes the case when type of true and false values of a
8912/// select instruction differs from type of the cmp instruction operands because
8913/// of a cast instruction. The function checks if it is legal to move the cast
8914/// operation after "select". If yes, it returns the new second value of
8915/// "select" (with the assumption that cast is moved):
8916/// 1. As operand of cast instruction when both values of "select" are same cast
8917/// instructions.
8918/// 2. As restored constant (by applying reverse cast operation) when the first
8919/// value of the "select" is a cast operation and the second value is a
8920/// constant. It is implemented in lookThroughCastConst().
8921/// 3. As one operand is cast instruction and the other is not. The operands in
8922/// sel(cmp) are in different type integer.
8923/// NOTE: We return only the new second value because the first value could be
8924/// accessed as operand of cast instruction.
8925static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8926 Instruction::CastOps *CastOp) {
8927 auto *Cast1 = dyn_cast<CastInst>(V1);
8928 if (!Cast1)
8929 return nullptr;
8930
8931 *CastOp = Cast1->getOpcode();
8932 Type *SrcTy = Cast1->getSrcTy();
8933 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8934 // If V1 and V2 are both the same cast from the same type, look through V1.
8935 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
8936 return Cast2->getOperand(0);
8937 return nullptr;
8938 }
8939
8940 auto *C = dyn_cast<Constant>(V2);
8941 if (C)
8942 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
8943
8944 Value *CastedTo = nullptr;
8945 if (*CastOp == Instruction::Trunc) {
8946 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
8947 // Here we have the following case:
8948 // %y_ext = sext iK %y to iN
8949 // %cond = cmp iN %x, %y_ext
8950 // %tr = trunc iN %x to iK
8951 // %narrowsel = select i1 %cond, iK %tr, iK %y
8952 //
8953 // We can always move trunc after select operation:
8954 // %y_ext = sext iK %y to iN
8955 // %cond = cmp iN %x, %y_ext
8956 // %widesel = select i1 %cond, iN %x, iN %y_ext
8957 // %tr = trunc iN %widesel to iK
8958 assert(V2->getType() == Cast1->getType() &&
8959 "V2 and Cast1 should be the same type.");
8960 CastedTo = CmpI->getOperand(1);
8961 }
8962 }
8963
8964 return CastedTo;
8965}
8967 Instruction::CastOps *CastOp,
8968 unsigned Depth) {
8970 return {SPF_UNKNOWN, SPNB_NA, false};
8971
8973 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
8974
8975 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
8976 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
8977
8978 Value *TrueVal = SI->getTrueValue();
8979 Value *FalseVal = SI->getFalseValue();
8980
8982 CmpI, TrueVal, FalseVal, LHS, RHS,
8983 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
8984 CastOp, Depth);
8985}
8986
8988 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
8989 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
8990 CmpInst::Predicate Pred = CmpI->getPredicate();
8991 Value *CmpLHS = CmpI->getOperand(0);
8992 Value *CmpRHS = CmpI->getOperand(1);
8993 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
8994 FMF.setNoNaNs();
8995
8996 // Bail out early.
8997 if (CmpI->isEquality())
8998 return {SPF_UNKNOWN, SPNB_NA, false};
8999
9000 // Deal with type mismatches.
9001 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9002 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9003 // If this is a potential fmin/fmax with a cast to integer, then ignore
9004 // -0.0 because there is no corresponding integer value.
9005 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9006 FMF.setNoSignedZeros();
9007 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9008 cast<CastInst>(TrueVal)->getOperand(0), C,
9009 LHS, RHS, Depth);
9010 }
9011 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9012 // If this is a potential fmin/fmax with a cast to integer, then ignore
9013 // -0.0 because there is no corresponding integer value.
9014 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9015 FMF.setNoSignedZeros();
9016 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9017 C, cast<CastInst>(FalseVal)->getOperand(0),
9018 LHS, RHS, Depth);
9019 }
9020 }
9021 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9022 LHS, RHS, Depth);
9023}
9024
9026 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9027 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9028 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9029 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9030 if (SPF == SPF_FMINNUM)
9031 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9032 if (SPF == SPF_FMAXNUM)
9033 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9034 llvm_unreachable("unhandled!");
9035}
9036
9038 switch (SPF) {
9040 return Intrinsic::umin;
9042 return Intrinsic::umax;
9044 return Intrinsic::smin;
9046 return Intrinsic::smax;
9047 default:
9048 llvm_unreachable("Unexpected SPF");
9049 }
9050}
9051
9053 if (SPF == SPF_SMIN) return SPF_SMAX;
9054 if (SPF == SPF_UMIN) return SPF_UMAX;
9055 if (SPF == SPF_SMAX) return SPF_SMIN;
9056 if (SPF == SPF_UMAX) return SPF_UMIN;
9057 llvm_unreachable("unhandled!");
9058}
9059
9061 switch (MinMaxID) {
9062 case Intrinsic::smax: return Intrinsic::smin;
9063 case Intrinsic::smin: return Intrinsic::smax;
9064 case Intrinsic::umax: return Intrinsic::umin;
9065 case Intrinsic::umin: return Intrinsic::umax;
9066 // Please note that next four intrinsics may produce the same result for
9067 // original and inverted case even if X != Y due to NaN is handled specially.
9068 case Intrinsic::maximum: return Intrinsic::minimum;
9069 case Intrinsic::minimum: return Intrinsic::maximum;
9070 case Intrinsic::maxnum: return Intrinsic::minnum;
9071 case Intrinsic::minnum: return Intrinsic::maxnum;
9072 default: llvm_unreachable("Unexpected intrinsic");
9073 }
9074}
9075
9077 switch (SPF) {
9080 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9081 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9082 default: llvm_unreachable("Unexpected flavor");
9083 }
9084}
9085
9086std::pair<Intrinsic::ID, bool>
9088 // Check if VL contains select instructions that can be folded into a min/max
9089 // vector intrinsic and return the intrinsic if it is possible.
9090 // TODO: Support floating point min/max.
9091 bool AllCmpSingleUse = true;
9092 SelectPatternResult SelectPattern;
9093 SelectPattern.Flavor = SPF_UNKNOWN;
9094 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9095 Value *LHS, *RHS;
9096 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9097 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9098 return false;
9099 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9100 SelectPattern.Flavor != CurrentPattern.Flavor)
9101 return false;
9102 SelectPattern = CurrentPattern;
9103 AllCmpSingleUse &=
9105 return true;
9106 })) {
9107 switch (SelectPattern.Flavor) {
9108 case SPF_SMIN:
9109 return {Intrinsic::smin, AllCmpSingleUse};
9110 case SPF_UMIN:
9111 return {Intrinsic::umin, AllCmpSingleUse};
9112 case SPF_SMAX:
9113 return {Intrinsic::smax, AllCmpSingleUse};
9114 case SPF_UMAX:
9115 return {Intrinsic::umax, AllCmpSingleUse};
9116 case SPF_FMAXNUM:
9117 return {Intrinsic::maxnum, AllCmpSingleUse};
9118 case SPF_FMINNUM:
9119 return {Intrinsic::minnum, AllCmpSingleUse};
9120 default:
9121 llvm_unreachable("unexpected select pattern flavor");
9122 }
9123 }
9124 return {Intrinsic::not_intrinsic, false};
9125}
9126
9127template <typename InstTy>
9128static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9129 Value *&Init, Value *&OtherOp) {
9130 // Handle the case of a simple two-predecessor recurrence PHI.
9131 // There's a lot more that could theoretically be done here, but
9132 // this is sufficient to catch some interesting cases.
9133 // TODO: Expand list -- gep, uadd.sat etc.
9134 if (PN->getNumIncomingValues() != 2)
9135 return false;
9136
9137 for (unsigned I = 0; I != 2; ++I) {
9138 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9139 Operation && Operation->getNumOperands() >= 2) {
9140 Value *LHS = Operation->getOperand(0);
9141 Value *RHS = Operation->getOperand(1);
9142 if (LHS != PN && RHS != PN)
9143 continue;
9144
9145 Inst = Operation;
9146 Init = PN->getIncomingValue(!I);
9147 OtherOp = (LHS == PN) ? RHS : LHS;
9148 return true;
9149 }
9150 }
9151 return false;
9152}
9153
9155 Value *&Start, Value *&Step) {
9156 // We try to match a recurrence of the form:
9157 // %iv = [Start, %entry], [%iv.next, %backedge]
9158 // %iv.next = binop %iv, Step
9159 // Or:
9160 // %iv = [Start, %entry], [%iv.next, %backedge]
9161 // %iv.next = binop Step, %iv
9162 return matchTwoInputRecurrence(P, BO, Start, Step);
9163}
9164
9166 Value *&Start, Value *&Step) {
9167 BinaryOperator *BO = nullptr;
9168 P = dyn_cast<PHINode>(I->getOperand(0));
9169 if (!P)
9170 P = dyn_cast<PHINode>(I->getOperand(1));
9171 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9172}
9173
9175 PHINode *&P, Value *&Init,
9176 Value *&OtherOp) {
9177 // Binary intrinsics only supported for now.
9178 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9179 I->getType() != I->getArgOperand(1)->getType())
9180 return false;
9181
9182 IntrinsicInst *II = nullptr;
9183 P = dyn_cast<PHINode>(I->getArgOperand(0));
9184 if (!P)
9185 P = dyn_cast<PHINode>(I->getArgOperand(1));
9186
9187 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9188}
9189
9190/// Return true if "icmp Pred LHS RHS" is always true.
9192 const Value *RHS) {
9193 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9194 return true;
9195
9196 switch (Pred) {
9197 default:
9198 return false;
9199
9200 case CmpInst::ICMP_SLE: {
9201 const APInt *C;
9202
9203 // LHS s<= LHS +_{nsw} C if C >= 0
9204 // LHS s<= LHS | C if C >= 0
9205 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9207 return !C->isNegative();
9208
9209 // LHS s<= smax(LHS, V) for any V
9211 return true;
9212
9213 // smin(RHS, V) s<= RHS for any V
9215 return true;
9216
9217 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9218 const Value *X;
9219 const APInt *CLHS, *CRHS;
9220 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9222 return CLHS->sle(*CRHS);
9223
9224 return false;
9225 }
9226
9227 case CmpInst::ICMP_ULE: {
9228 // LHS u<= LHS +_{nuw} V for any V
9229 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9231 return true;
9232
9233 // LHS u<= LHS | V for any V
9234 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9235 return true;
9236
9237 // LHS u<= umax(LHS, V) for any V
9239 return true;
9240
9241 // RHS >> V u<= RHS for any V
9242 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9243 return true;
9244
9245 // RHS u/ C_ugt_1 u<= RHS
9246 const APInt *C;
9247 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9248 return true;
9249
9250 // RHS & V u<= RHS for any V
9252 return true;
9253
9254 // umin(RHS, V) u<= RHS for any V
9256 return true;
9257
9258 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9259 const Value *X;
9260 const APInt *CLHS, *CRHS;
9261 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9263 return CLHS->ule(*CRHS);
9264
9265 return false;
9266 }
9267 }
9268}
9269
9270/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9271/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9272static std::optional<bool>
9274 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9275 switch (Pred) {
9276 default:
9277 return std::nullopt;
9278
9279 case CmpInst::ICMP_SLT:
9280 case CmpInst::ICMP_SLE:
9281 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9283 return true;
9284 return std::nullopt;
9285
9286 case CmpInst::ICMP_SGT:
9287 case CmpInst::ICMP_SGE:
9288 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9290 return true;
9291 return std::nullopt;
9292
9293 case CmpInst::ICMP_ULT:
9294 case CmpInst::ICMP_ULE:
9295 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9297 return true;
9298 return std::nullopt;
9299
9300 case CmpInst::ICMP_UGT:
9301 case CmpInst::ICMP_UGE:
9302 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9304 return true;
9305 return std::nullopt;
9306 }
9307}
9308
9309/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9310/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9311/// Otherwise, return std::nullopt if we can't infer anything.
9312static std::optional<bool>
9314 CmpPredicate RPred, const ConstantRange &RCR) {
9315 auto CRImpliesPred = [&](ConstantRange CR,
9316 CmpInst::Predicate Pred) -> std::optional<bool> {
9317 // If all true values for lhs and true for rhs, lhs implies rhs
9318 if (CR.icmp(Pred, RCR))
9319 return true;
9320
9321 // If there is no overlap, lhs implies not rhs
9322 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9323 return false;
9324
9325 return std::nullopt;
9326 };
9327 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9328 RPred))
9329 return Res;
9330 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9332 : LPred.dropSameSign();
9334 : RPred.dropSameSign();
9335 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9336 RPred);
9337 }
9338 return std::nullopt;
9339}
9340
9341/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9342/// is true. Return false if LHS implies RHS is false. Otherwise, return
9343/// std::nullopt if we can't infer anything.
9344static std::optional<bool>
9345isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9346 CmpPredicate RPred, const Value *R0, const Value *R1,
9347 const DataLayout &DL, bool LHSIsTrue) {
9348 // The rest of the logic assumes the LHS condition is true. If that's not the
9349 // case, invert the predicate to make it so.
9350 if (!LHSIsTrue)
9351 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9352
9353 // We can have non-canonical operands, so try to normalize any common operand
9354 // to L0/R0.
9355 if (L0 == R1) {
9356 std::swap(R0, R1);
9357 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9358 }
9359 if (R0 == L1) {
9360 std::swap(L0, L1);
9361 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9362 }
9363 if (L1 == R1) {
9364 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9365 if (L0 != R0 || match(L0, m_ImmConstant())) {
9366 std::swap(L0, L1);
9367 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9368 std::swap(R0, R1);
9369 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9370 }
9371 }
9372
9373 // See if we can infer anything if operand-0 matches and we have at least one
9374 // constant.
9375 const APInt *Unused;
9376 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9377 // Potential TODO: We could also further use the constant range of L0/R0 to
9378 // further constraint the constant ranges. At the moment this leads to
9379 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9380 // C1` (see discussion: D58633).
9382 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9383 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9385 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9386 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9387 // Even if L1/R1 are not both constant, we can still sometimes deduce
9388 // relationship from a single constant. For example X u> Y implies X != 0.
9389 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9390 return R;
9391 // If both L1/R1 were exact constant ranges and we didn't get anything
9392 // here, we won't be able to deduce this.
9393 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9394 return std::nullopt;
9395 }
9396
9397 // Can we infer anything when the two compares have matching operands?
9398 if (L0 == R0 && L1 == R1)
9399 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9400
9401 // It only really makes sense in the context of signed comparison for "X - Y
9402 // must be positive if X >= Y and no overflow".
9403 // Take SGT as an example: L0:x > L1:y and C >= 0
9404 // ==> R0:(x -nsw y) < R1:(-C) is false
9405 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9406 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9407 SignedLPred == ICmpInst::ICMP_SGE) &&
9408 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9409 if (match(R1, m_NonPositive()) &&
9410 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9411 return false;
9412 }
9413
9414 // Take SLT as an example: L0:x < L1:y and C <= 0
9415 // ==> R0:(x -nsw y) < R1:(-C) is true
9416 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9417 SignedLPred == ICmpInst::ICMP_SLE) &&
9418 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9419 if (match(R1, m_NonNegative()) &&
9420 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9421 return true;
9422 }
9423
9424 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9425 if (L0 == R0 &&
9426 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9427 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9428 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9429 return CmpPredicate::getMatching(LPred, RPred).has_value();
9430
9431 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9432 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9433
9434 return std::nullopt;
9435}
9436
9437/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9438/// false. Otherwise, return std::nullopt if we can't infer anything. We
9439/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9440/// instruction.
9441static std::optional<bool>
9443 const Value *RHSOp0, const Value *RHSOp1,
9444 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9445 // The LHS must be an 'or', 'and', or a 'select' instruction.
9446 assert((LHS->getOpcode() == Instruction::And ||
9447 LHS->getOpcode() == Instruction::Or ||
9448 LHS->getOpcode() == Instruction::Select) &&
9449 "Expected LHS to be 'and', 'or', or 'select'.");
9450
9451 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9452
9453 // If the result of an 'or' is false, then we know both legs of the 'or' are
9454 // false. Similarly, if the result of an 'and' is true, then we know both
9455 // legs of the 'and' are true.
9456 const Value *ALHS, *ARHS;
9457 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9458 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9459 // FIXME: Make this non-recursion.
9460 if (std::optional<bool> Implication = isImpliedCondition(
9461 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9462 return Implication;
9463 if (std::optional<bool> Implication = isImpliedCondition(
9464 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9465 return Implication;
9466 return std::nullopt;
9467 }
9468 return std::nullopt;
9469}
9470
9471std::optional<bool>
9473 const Value *RHSOp0, const Value *RHSOp1,
9474 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9475 // Bail out when we hit the limit.
9477 return std::nullopt;
9478
9479 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9480 // example.
9481 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9482 return std::nullopt;
9483
9484 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9485 "Expected integer type only!");
9486
9487 // Match not
9488 if (match(LHS, m_Not(m_Value(LHS))))
9489 LHSIsTrue = !LHSIsTrue;
9490
9491 // Both LHS and RHS are icmps.
9492 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9493 return isImpliedCondICmps(LHSCmp->getCmpPredicate(), LHSCmp->getOperand(0),
9494 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9495 DL, LHSIsTrue);
9496 const Value *V;
9497 if (match(LHS, m_NUWTrunc(m_Value(V))))
9499 ConstantInt::get(V->getType(), 0), RHSPred,
9500 RHSOp0, RHSOp1, DL, LHSIsTrue);
9501
9502 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9503 /// the RHS to be an icmp.
9504 /// FIXME: Add support for and/or/select on the RHS.
9505 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9506 if ((LHSI->getOpcode() == Instruction::And ||
9507 LHSI->getOpcode() == Instruction::Or ||
9508 LHSI->getOpcode() == Instruction::Select))
9509 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9510 Depth);
9511 }
9512 return std::nullopt;
9513}
9514
9515std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9516 const DataLayout &DL,
9517 bool LHSIsTrue, unsigned Depth) {
9518 // LHS ==> RHS by definition
9519 if (LHS == RHS)
9520 return LHSIsTrue;
9521
9522 // Match not
9523 bool InvertRHS = false;
9524 if (match(RHS, m_Not(m_Value(RHS)))) {
9525 if (LHS == RHS)
9526 return !LHSIsTrue;
9527 InvertRHS = true;
9528 }
9529
9530 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9531 if (auto Implied = isImpliedCondition(
9532 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9533 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9534 return InvertRHS ? !*Implied : *Implied;
9535 return std::nullopt;
9536 }
9537
9538 const Value *V;
9539 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9540 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9541 ConstantInt::get(V->getType(), 0), DL,
9542 LHSIsTrue, Depth))
9543 return InvertRHS ? !*Implied : *Implied;
9544 return std::nullopt;
9545 }
9546
9548 return std::nullopt;
9549
9550 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9551 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9552 const Value *RHS1, *RHS2;
9553 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9554 if (std::optional<bool> Imp =
9555 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9556 if (*Imp == true)
9557 return !InvertRHS;
9558 if (std::optional<bool> Imp =
9559 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9560 if (*Imp == true)
9561 return !InvertRHS;
9562 }
9563 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9564 if (std::optional<bool> Imp =
9565 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9566 if (*Imp == false)
9567 return InvertRHS;
9568 if (std::optional<bool> Imp =
9569 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9570 if (*Imp == false)
9571 return InvertRHS;
9572 }
9573
9574 return std::nullopt;
9575}
9576
9577// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9578// condition dominating ContextI or nullptr, if no condition is found.
9579static std::pair<Value *, bool>
9581 if (!ContextI || !ContextI->getParent())
9582 return {nullptr, false};
9583
9584 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9585 // dominator tree (eg, from a SimplifyQuery) instead?
9586 const BasicBlock *ContextBB = ContextI->getParent();
9587 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9588 if (!PredBB)
9589 return {nullptr, false};
9590
9591 // We need a conditional branch in the predecessor.
9592 Value *PredCond;
9593 BasicBlock *TrueBB, *FalseBB;
9594 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9595 return {nullptr, false};
9596
9597 // The branch should get simplified. Don't bother simplifying this condition.
9598 if (TrueBB == FalseBB)
9599 return {nullptr, false};
9600
9601 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9602 "Predecessor block does not point to successor?");
9603
9604 // Is this condition implied by the predecessor condition?
9605 return {PredCond, TrueBB == ContextBB};
9606}
9607
9608std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9609 const Instruction *ContextI,
9610 const DataLayout &DL) {
9611 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9612 auto PredCond = getDomPredecessorCondition(ContextI);
9613 if (PredCond.first)
9614 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9615 return std::nullopt;
9616}
9617
9619 const Value *LHS,
9620 const Value *RHS,
9621 const Instruction *ContextI,
9622 const DataLayout &DL) {
9623 auto PredCond = getDomPredecessorCondition(ContextI);
9624 if (PredCond.first)
9625 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9626 PredCond.second);
9627 return std::nullopt;
9628}
9629
9631 APInt &Upper, const InstrInfoQuery &IIQ,
9632 bool PreferSignedRange) {
9633 unsigned Width = Lower.getBitWidth();
9634 const APInt *C;
9635 switch (BO.getOpcode()) {
9636 case Instruction::Sub:
9637 if (match(BO.getOperand(0), m_APInt(C))) {
9638 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9639 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9640
9641 // If the caller expects a signed compare, then try to use a signed range.
9642 // Otherwise if both no-wraps are set, use the unsigned range because it
9643 // is never larger than the signed range. Example:
9644 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9645 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9646 if (PreferSignedRange && HasNSW && HasNUW)
9647 HasNUW = false;
9648
9649 if (HasNUW) {
9650 // 'sub nuw c, x' produces [0, C].
9651 Upper = *C + 1;
9652 } else if (HasNSW) {
9653 if (C->isNegative()) {
9654 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9656 Upper = *C - APInt::getSignedMaxValue(Width);
9657 } else {
9658 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9659 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9660 Lower = *C - APInt::getSignedMaxValue(Width);
9662 }
9663 }
9664 }
9665 break;
9666 case Instruction::Add:
9667 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9668 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9669 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9670
9671 // If the caller expects a signed compare, then try to use a signed
9672 // range. Otherwise if both no-wraps are set, use the unsigned range
9673 // because it is never larger than the signed range. Example: "add nuw
9674 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9675 if (PreferSignedRange && HasNSW && HasNUW)
9676 HasNUW = false;
9677
9678 if (HasNUW) {
9679 // 'add nuw x, C' produces [C, UINT_MAX].
9680 Lower = *C;
9681 } else if (HasNSW) {
9682 if (C->isNegative()) {
9683 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9685 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9686 } else {
9687 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9688 Lower = APInt::getSignedMinValue(Width) + *C;
9689 Upper = APInt::getSignedMaxValue(Width) + 1;
9690 }
9691 }
9692 }
9693 break;
9694
9695 case Instruction::And:
9696 if (match(BO.getOperand(1), m_APInt(C)))
9697 // 'and x, C' produces [0, C].
9698 Upper = *C + 1;
9699 // X & -X is a power of two or zero. So we can cap the value at max power of
9700 // two.
9701 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9702 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9703 Upper = APInt::getSignedMinValue(Width) + 1;
9704 break;
9705
9706 case Instruction::Or:
9707 if (match(BO.getOperand(1), m_APInt(C)))
9708 // 'or x, C' produces [C, UINT_MAX].
9709 Lower = *C;
9710 break;
9711
9712 case Instruction::AShr:
9713 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9714 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9716 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9717 } else if (match(BO.getOperand(0), m_APInt(C))) {
9718 unsigned ShiftAmount = Width - 1;
9719 if (!C->isZero() && IIQ.isExact(&BO))
9720 ShiftAmount = C->countr_zero();
9721 if (C->isNegative()) {
9722 // 'ashr C, x' produces [C, C >> (Width-1)]
9723 Lower = *C;
9724 Upper = C->ashr(ShiftAmount) + 1;
9725 } else {
9726 // 'ashr C, x' produces [C >> (Width-1), C]
9727 Lower = C->ashr(ShiftAmount);
9728 Upper = *C + 1;
9729 }
9730 }
9731 break;
9732
9733 case Instruction::LShr:
9734 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9735 // 'lshr x, C' produces [0, UINT_MAX >> C].
9736 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9737 } else if (match(BO.getOperand(0), m_APInt(C))) {
9738 // 'lshr C, x' produces [C >> (Width-1), C].
9739 unsigned ShiftAmount = Width - 1;
9740 if (!C->isZero() && IIQ.isExact(&BO))
9741 ShiftAmount = C->countr_zero();
9742 Lower = C->lshr(ShiftAmount);
9743 Upper = *C + 1;
9744 }
9745 break;
9746
9747 case Instruction::Shl:
9748 if (match(BO.getOperand(0), m_APInt(C))) {
9749 if (IIQ.hasNoUnsignedWrap(&BO)) {
9750 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9751 Lower = *C;
9752 Upper = Lower.shl(Lower.countl_zero()) + 1;
9753 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9754 if (C->isNegative()) {
9755 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9756 unsigned ShiftAmount = C->countl_one() - 1;
9757 Lower = C->shl(ShiftAmount);
9758 Upper = *C + 1;
9759 } else {
9760 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9761 unsigned ShiftAmount = C->countl_zero() - 1;
9762 Lower = *C;
9763 Upper = C->shl(ShiftAmount) + 1;
9764 }
9765 } else {
9766 // If lowbit is set, value can never be zero.
9767 if ((*C)[0])
9768 Lower = APInt::getOneBitSet(Width, 0);
9769 // If we are shifting a constant the largest it can be is if the longest
9770 // sequence of consecutive ones is shifted to the highbits (breaking
9771 // ties for which sequence is higher). At the moment we take a liberal
9772 // upper bound on this by just popcounting the constant.
9773 // TODO: There may be a bitwise trick for it longest/highest
9774 // consecutative sequence of ones (naive method is O(Width) loop).
9775 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9776 }
9777 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9778 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9779 }
9780 break;
9781
9782 case Instruction::SDiv:
9783 if (match(BO.getOperand(1), m_APInt(C))) {
9784 APInt IntMin = APInt::getSignedMinValue(Width);
9785 APInt IntMax = APInt::getSignedMaxValue(Width);
9786 if (C->isAllOnes()) {
9787 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9788 // where C != -1 and C != 0 and C != 1
9789 Lower = IntMin + 1;
9790 Upper = IntMax + 1;
9791 } else if (C->countl_zero() < Width - 1) {
9792 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9793 // where C != -1 and C != 0 and C != 1
9794 Lower = IntMin.sdiv(*C);
9795 Upper = IntMax.sdiv(*C);
9796 if (Lower.sgt(Upper))
9798 Upper = Upper + 1;
9799 assert(Upper != Lower && "Upper part of range has wrapped!");
9800 }
9801 } else if (match(BO.getOperand(0), m_APInt(C))) {
9802 if (C->isMinSignedValue()) {
9803 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9804 Lower = *C;
9805 Upper = Lower.lshr(1) + 1;
9806 } else {
9807 // 'sdiv C, x' produces [-|C|, |C|].
9808 Upper = C->abs() + 1;
9809 Lower = (-Upper) + 1;
9810 }
9811 }
9812 break;
9813
9814 case Instruction::UDiv:
9815 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9816 // 'udiv x, C' produces [0, UINT_MAX / C].
9817 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9818 } else if (match(BO.getOperand(0), m_APInt(C))) {
9819 // 'udiv C, x' produces [0, C].
9820 Upper = *C + 1;
9821 }
9822 break;
9823
9824 case Instruction::SRem:
9825 if (match(BO.getOperand(1), m_APInt(C))) {
9826 // 'srem x, C' produces (-|C|, |C|).
9827 Upper = C->abs();
9828 Lower = (-Upper) + 1;
9829 } else if (match(BO.getOperand(0), m_APInt(C))) {
9830 if (C->isNegative()) {
9831 // 'srem -|C|, x' produces [-|C|, 0].
9832 Upper = 1;
9833 Lower = *C;
9834 } else {
9835 // 'srem |C|, x' produces [0, |C|].
9836 Upper = *C + 1;
9837 }
9838 }
9839 break;
9840
9841 case Instruction::URem:
9842 if (match(BO.getOperand(1), m_APInt(C)))
9843 // 'urem x, C' produces [0, C).
9844 Upper = *C;
9845 else if (match(BO.getOperand(0), m_APInt(C)))
9846 // 'urem C, x' produces [0, C].
9847 Upper = *C + 1;
9848 break;
9849
9850 default:
9851 break;
9852 }
9853}
9854
9856 bool UseInstrInfo) {
9857 unsigned Width = II.getType()->getScalarSizeInBits();
9858 const APInt *C;
9859 switch (II.getIntrinsicID()) {
9860 case Intrinsic::ctlz:
9861 case Intrinsic::cttz: {
9862 APInt Upper(Width, Width);
9863 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
9864 Upper += 1;
9865 // Maximum of set/clear bits is the bit width.
9867 }
9868 case Intrinsic::ctpop:
9869 // Maximum of set/clear bits is the bit width.
9871 APInt(Width, Width) + 1);
9872 case Intrinsic::uadd_sat:
9873 // uadd.sat(x, C) produces [C, UINT_MAX].
9874 if (match(II.getOperand(0), m_APInt(C)) ||
9875 match(II.getOperand(1), m_APInt(C)))
9877 break;
9878 case Intrinsic::sadd_sat:
9879 if (match(II.getOperand(0), m_APInt(C)) ||
9880 match(II.getOperand(1), m_APInt(C))) {
9881 if (C->isNegative())
9882 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9884 APInt::getSignedMaxValue(Width) + *C +
9885 1);
9886
9887 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9889 APInt::getSignedMaxValue(Width) + 1);
9890 }
9891 break;
9892 case Intrinsic::usub_sat:
9893 // usub.sat(C, x) produces [0, C].
9894 if (match(II.getOperand(0), m_APInt(C)))
9895 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9896
9897 // usub.sat(x, C) produces [0, UINT_MAX - C].
9898 if (match(II.getOperand(1), m_APInt(C)))
9900 APInt::getMaxValue(Width) - *C + 1);
9901 break;
9902 case Intrinsic::ssub_sat:
9903 if (match(II.getOperand(0), m_APInt(C))) {
9904 if (C->isNegative())
9905 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9907 *C - APInt::getSignedMinValue(Width) +
9908 1);
9909
9910 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9912 APInt::getSignedMaxValue(Width) + 1);
9913 } else if (match(II.getOperand(1), m_APInt(C))) {
9914 if (C->isNegative())
9915 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9917 APInt::getSignedMaxValue(Width) + 1);
9918
9919 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9921 APInt::getSignedMaxValue(Width) - *C +
9922 1);
9923 }
9924 break;
9925 case Intrinsic::umin:
9926 case Intrinsic::umax:
9927 case Intrinsic::smin:
9928 case Intrinsic::smax:
9929 if (!match(II.getOperand(0), m_APInt(C)) &&
9930 !match(II.getOperand(1), m_APInt(C)))
9931 break;
9932
9933 switch (II.getIntrinsicID()) {
9934 case Intrinsic::umin:
9935 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9936 case Intrinsic::umax:
9938 case Intrinsic::smin:
9940 *C + 1);
9941 case Intrinsic::smax:
9943 APInt::getSignedMaxValue(Width) + 1);
9944 default:
9945 llvm_unreachable("Must be min/max intrinsic");
9946 }
9947 break;
9948 case Intrinsic::abs:
9949 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
9950 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9951 if (match(II.getOperand(1), m_One()))
9953 APInt::getSignedMaxValue(Width) + 1);
9954
9956 APInt::getSignedMinValue(Width) + 1);
9957 case Intrinsic::vscale:
9958 if (!II.getParent() || !II.getFunction())
9959 break;
9960 return getVScaleRange(II.getFunction(), Width);
9961 default:
9962 break;
9963 }
9964
9965 return ConstantRange::getFull(Width);
9966}
9967
9969 const InstrInfoQuery &IIQ) {
9970 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
9971 const Value *LHS = nullptr, *RHS = nullptr;
9973 if (R.Flavor == SPF_UNKNOWN)
9974 return ConstantRange::getFull(BitWidth);
9975
9976 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
9977 // If the negation part of the abs (in RHS) has the NSW flag,
9978 // then the result of abs(X) is [0..SIGNED_MAX],
9979 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9980 if (match(RHS, m_Neg(m_Specific(LHS))) &&
9984
9987 }
9988
9989 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
9990 // The result of -abs(X) is <= 0.
9992 APInt(BitWidth, 1));
9993 }
9994
9995 const APInt *C;
9996 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
9997 return ConstantRange::getFull(BitWidth);
9998
9999 switch (R.Flavor) {
10000 case SPF_UMIN:
10002 case SPF_UMAX:
10004 case SPF_SMIN:
10006 *C + 1);
10007 case SPF_SMAX:
10010 default:
10011 return ConstantRange::getFull(BitWidth);
10012 }
10013}
10014
10016 // The maximum representable value of a half is 65504. For floats the maximum
10017 // value is 3.4e38 which requires roughly 129 bits.
10018 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10019 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10020 return;
10021 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10022 Lower = APInt(BitWidth, -65504, true);
10023 Upper = APInt(BitWidth, 65505);
10024 }
10025
10026 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10027 // For a fptoui the lower limit is left as 0.
10028 Upper = APInt(BitWidth, 65505);
10029 }
10030}
10031
10033 bool UseInstrInfo, AssumptionCache *AC,
10034 const Instruction *CtxI,
10035 const DominatorTree *DT,
10036 unsigned Depth) {
10037 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10038
10040 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10041
10042 if (auto *C = dyn_cast<Constant>(V))
10043 return C->toConstantRange();
10044
10045 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10046 InstrInfoQuery IIQ(UseInstrInfo);
10047 ConstantRange CR = ConstantRange::getFull(BitWidth);
10048 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10049 APInt Lower = APInt(BitWidth, 0);
10050 APInt Upper = APInt(BitWidth, 0);
10051 // TODO: Return ConstantRange.
10052 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10054 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10055 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10056 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10058 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10060 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10061 CR = CRTrue.unionWith(CRFalse);
10063 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10064 APInt Lower = APInt(BitWidth, 0);
10065 APInt Upper = APInt(BitWidth, 0);
10066 // TODO: Return ConstantRange.
10069 } else if (const auto *A = dyn_cast<Argument>(V))
10070 if (std::optional<ConstantRange> Range = A->getRange())
10071 CR = *Range;
10072
10073 if (auto *I = dyn_cast<Instruction>(V)) {
10074 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10076
10077 if (const auto *CB = dyn_cast<CallBase>(V))
10078 if (std::optional<ConstantRange> Range = CB->getRange())
10079 CR = CR.intersectWith(*Range);
10080 }
10081
10082 if (CtxI && AC) {
10083 // Try to restrict the range based on information from assumptions.
10084 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10085 if (!AssumeVH)
10086 continue;
10087 CallInst *I = cast<CallInst>(AssumeVH);
10088 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10089 "Got assumption for the wrong function!");
10090 assert(I->getIntrinsicID() == Intrinsic::assume &&
10091 "must be an assume intrinsic");
10092
10093 if (!isValidAssumeForContext(I, CtxI, DT))
10094 continue;
10095 Value *Arg = I->getArgOperand(0);
10096 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10097 // Currently we just use information from comparisons.
10098 if (!Cmp || Cmp->getOperand(0) != V)
10099 continue;
10100 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10101 ConstantRange RHS =
10102 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10103 UseInstrInfo, AC, I, DT, Depth + 1);
10104 CR = CR.intersectWith(
10105 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10106 }
10107 }
10108
10109 return CR;
10110}
10111
10112static void
10114 function_ref<void(Value *)> InsertAffected) {
10115 assert(V != nullptr);
10116 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10117 InsertAffected(V);
10118 } else if (auto *I = dyn_cast<Instruction>(V)) {
10119 InsertAffected(V);
10120
10121 // Peek through unary operators to find the source of the condition.
10122 Value *Op;
10125 InsertAffected(Op);
10126 }
10127 }
10128}
10129
10131 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10132 auto AddAffected = [&InsertAffected](Value *V) {
10133 addValueAffectedByCondition(V, InsertAffected);
10134 };
10135
10136 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10137 if (IsAssume) {
10138 AddAffected(LHS);
10139 AddAffected(RHS);
10140 } else if (match(RHS, m_Constant()))
10141 AddAffected(LHS);
10142 };
10143
10144 SmallVector<Value *, 8> Worklist;
10146 Worklist.push_back(Cond);
10147 while (!Worklist.empty()) {
10148 Value *V = Worklist.pop_back_val();
10149 if (!Visited.insert(V).second)
10150 continue;
10151
10152 CmpPredicate Pred;
10153 Value *A, *B, *X;
10154
10155 if (IsAssume) {
10156 AddAffected(V);
10157 if (match(V, m_Not(m_Value(X))))
10158 AddAffected(X);
10159 }
10160
10161 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10162 // assume(A && B) is split to -> assume(A); assume(B);
10163 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10164 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10165 // enough information to be worth handling (intersection of information as
10166 // opposed to union).
10167 if (!IsAssume) {
10168 Worklist.push_back(A);
10169 Worklist.push_back(B);
10170 }
10171 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10172 bool HasRHSC = match(B, m_ConstantInt());
10173 if (ICmpInst::isEquality(Pred)) {
10174 AddAffected(A);
10175 if (IsAssume)
10176 AddAffected(B);
10177 if (HasRHSC) {
10178 Value *Y;
10179 // (X & C) or (X | C).
10180 // (X << C) or (X >>_s C) or (X >>_u C).
10181 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10182 AddAffected(X);
10183 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10184 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10185 AddAffected(X);
10186 AddAffected(Y);
10187 }
10188 }
10189 } else {
10190 AddCmpOperands(A, B);
10191 if (HasRHSC) {
10192 // Handle (A + C1) u< C2, which is the canonical form of
10193 // A > C3 && A < C4.
10195 AddAffected(X);
10196
10197 if (ICmpInst::isUnsigned(Pred)) {
10198 Value *Y;
10199 // X & Y u> C -> X >u C && Y >u C
10200 // X | Y u< C -> X u< C && Y u< C
10201 // X nuw+ Y u< C -> X u< C && Y u< C
10202 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10203 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10204 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10205 AddAffected(X);
10206 AddAffected(Y);
10207 }
10208 // X nuw- Y u> C -> X u> C
10209 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10210 AddAffected(X);
10211 }
10212 }
10213
10214 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10215 // by computeKnownFPClass().
10217 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10218 InsertAffected(X);
10219 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10220 InsertAffected(X);
10221 }
10222 }
10223
10224 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10225 AddAffected(X);
10226 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10227 AddCmpOperands(A, B);
10228
10229 // fcmp fneg(x), y
10230 // fcmp fabs(x), y
10231 // fcmp fneg(fabs(x)), y
10232 if (match(A, m_FNeg(m_Value(A))))
10233 AddAffected(A);
10234 if (match(A, m_FAbs(m_Value(A))))
10235 AddAffected(A);
10236
10238 m_Value()))) {
10239 // Handle patterns that computeKnownFPClass() support.
10240 AddAffected(A);
10241 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10242 // Assume is checked here as X is already added above for assumes in
10243 // addValueAffectedByCondition
10244 AddAffected(X);
10245 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10246 // Assume is checked here to avoid issues with ephemeral values
10247 Worklist.push_back(X);
10248 }
10249 }
10250}
10251
10253 // (X >> C) or/add (X & mask(C) != 0)
10254 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10255 if (BO->getOpcode() == Instruction::Add ||
10256 BO->getOpcode() == Instruction::Or) {
10257 const Value *X;
10258 const APInt *C1, *C2;
10259 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10263 m_Zero())))) &&
10264 C2->popcount() == C1->getZExtValue())
10265 return X;
10266 }
10267 }
10268 return nullptr;
10269}
10270
10272 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10273}
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:671
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:623
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:654
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:1727
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:1685
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:270
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:2138
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:342
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:157
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:1734
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:336
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:1899
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