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/// Maximum number of instructions to check between assume and context
93/// instruction.
94static constexpr unsigned MaxInstrsToCheckForFree = 16;
95
96/// Returns the bitwidth of the given scalar or pointer type. For vector types,
97/// returns the element type's bitwidth.
98static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
99 if (unsigned BitWidth = Ty->getScalarSizeInBits())
100 return BitWidth;
101
102 return DL.getPointerTypeSizeInBits(Ty);
103}
104
105// Given the provided Value and, potentially, a context instruction, return
106// the preferred context instruction (if any).
107static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
108 // If we've been provided with a context instruction, then use that (provided
109 // it has been inserted).
110 if (CxtI && CxtI->getParent())
111 return CxtI;
112
113 // If the value is really an already-inserted instruction, then use that.
114 CxtI = dyn_cast<Instruction>(V);
115 if (CxtI && CxtI->getParent())
116 return CxtI;
117
118 return nullptr;
119}
120
122 const APInt &DemandedElts,
123 APInt &DemandedLHS, APInt &DemandedRHS) {
124 if (isa<ScalableVectorType>(Shuf->getType())) {
125 assert(DemandedElts == APInt(1,1));
126 DemandedLHS = DemandedRHS = DemandedElts;
127 return true;
128 }
129
130 int NumElts =
131 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
132 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
133 DemandedElts, DemandedLHS, DemandedRHS);
134}
135
136static void computeKnownBits(const Value *V, const APInt &DemandedElts,
137 KnownBits &Known, const SimplifyQuery &Q,
138 unsigned Depth);
139
141 const SimplifyQuery &Q, unsigned Depth) {
142 // Since the number of lanes in a scalable vector is unknown at compile time,
143 // we track one bit which is implicitly broadcast to all lanes. This means
144 // that all lanes in a scalable vector are considered demanded.
145 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
146 APInt DemandedElts =
147 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
148 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
149}
150
152 const DataLayout &DL, AssumptionCache *AC,
153 const Instruction *CxtI, const DominatorTree *DT,
154 bool UseInstrInfo, unsigned Depth) {
155 computeKnownBits(V, Known,
156 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
157 Depth);
158}
159
161 AssumptionCache *AC, const Instruction *CxtI,
162 const DominatorTree *DT, bool UseInstrInfo,
163 unsigned Depth) {
164 return computeKnownBits(
165 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
166}
167
168KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
169 const DataLayout &DL, AssumptionCache *AC,
170 const Instruction *CxtI,
171 const DominatorTree *DT, bool UseInstrInfo,
172 unsigned Depth) {
173 return computeKnownBits(
174 V, DemandedElts,
175 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
176}
177
179 const SimplifyQuery &SQ) {
180 // Look for an inverted mask: (X & ~M) op (Y & M).
181 {
182 Value *M;
183 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
185 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
186 return true;
187 }
188
189 // X op (Y & ~X)
192 return true;
193
194 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
195 // for constant Y.
196 Value *Y;
197 if (match(RHS,
199 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
200 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
201 return true;
202
203 // Peek through extends to find a 'not' of the other side:
204 // (ext Y) op ext(~Y)
205 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
207 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
208 return true;
209
210 // Look for: (A & B) op ~(A | B)
211 {
212 Value *A, *B;
213 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
215 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
216 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
217 return true;
218 }
219
220 // Look for: (X << V) op (Y >> (BitWidth - V))
221 // or (X >> V) op (Y << (BitWidth - V))
222 {
223 const Value *V;
224 const APInt *R;
225 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
226 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
227 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
228 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
229 R->uge(LHS->getType()->getScalarSizeInBits()))
230 return true;
231 }
232
233 return false;
234}
235
237 const WithCache<const Value *> &RHSCache,
238 const SimplifyQuery &SQ) {
239 const Value *LHS = LHSCache.getValue();
240 const Value *RHS = RHSCache.getValue();
241
242 assert(LHS->getType() == RHS->getType() &&
243 "LHS and RHS should have the same type");
244 assert(LHS->getType()->isIntOrIntVectorTy() &&
245 "LHS and RHS should be integers");
246
247 if (haveNoCommonBitsSetSpecialCases(LHS, RHS, SQ) ||
249 return true;
250
252 RHSCache.getKnownBits(SQ));
253}
254
256 return !I->user_empty() &&
257 all_of(I->users(), match_fn(m_ICmp(m_Value(), m_Zero())));
258}
259
261 return !I->user_empty() && all_of(I->users(), [](const User *U) {
262 CmpPredicate P;
263 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
264 });
265}
266
268 bool OrZero, AssumptionCache *AC,
269 const Instruction *CxtI,
270 const DominatorTree *DT, bool UseInstrInfo,
271 unsigned Depth) {
272 return ::isKnownToBeAPowerOfTwo(
273 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
274 Depth);
275}
276
277static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
278 const SimplifyQuery &Q, unsigned Depth);
279
281 unsigned Depth) {
282 return computeKnownBits(V, SQ, Depth).isNonNegative();
283}
284
286 unsigned Depth) {
287 if (auto *CI = dyn_cast<ConstantInt>(V))
288 return CI->getValue().isStrictlyPositive();
289
290 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
291 // this updated.
292 KnownBits Known = computeKnownBits(V, SQ, Depth);
293 return Known.isNonNegative() &&
294 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
295}
296
298 unsigned Depth) {
299 return computeKnownBits(V, SQ, Depth).isNegative();
300}
301
302static bool isKnownNonEqual(const Value *V1, const Value *V2,
303 const APInt &DemandedElts, const SimplifyQuery &Q,
304 unsigned Depth);
305
306bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
307 const SimplifyQuery &Q, unsigned Depth) {
308 // We don't support looking through casts.
309 if (V1 == V2 || V1->getType() != V2->getType())
310 return false;
311 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
312 APInt DemandedElts =
313 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
314 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
315}
316
317bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
318 const SimplifyQuery &SQ, unsigned Depth) {
319 KnownBits Known(Mask.getBitWidth());
320 computeKnownBits(V, Known, SQ, Depth);
321 return Mask.isSubsetOf(Known.Zero);
322}
323
324static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
325 const SimplifyQuery &Q, unsigned Depth);
326
327static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
328 unsigned Depth = 0) {
329 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
330 APInt DemandedElts =
331 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
332 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
333}
334
335unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
336 AssumptionCache *AC, const Instruction *CxtI,
337 const DominatorTree *DT, bool UseInstrInfo,
338 unsigned Depth) {
339 return ::ComputeNumSignBits(
340 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
341}
342
344 AssumptionCache *AC,
345 const Instruction *CxtI,
346 const DominatorTree *DT,
347 unsigned Depth) {
348 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
349 return V->getType()->getScalarSizeInBits() - SignBits + 1;
350}
351
352static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
353 bool NSW, bool NUW,
354 const APInt &DemandedElts,
355 KnownBits &KnownOut, KnownBits &Known2,
356 const SimplifyQuery &Q, unsigned Depth) {
357 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
358
359 // If one operand is unknown and we have no nowrap information,
360 // the result will be unknown independently of the second operand.
361 if (KnownOut.isUnknown() && !NSW && !NUW)
362 return;
363
364 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
365 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
366
367 if (!Add && NSW && !KnownOut.isNonNegative() &&
369 .value_or(false))
370 KnownOut.makeNonNegative();
371}
372
373static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
374 bool NUW, const APInt &DemandedElts,
375 KnownBits &Known, KnownBits &Known2,
376 const SimplifyQuery &Q, unsigned Depth) {
377 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
378 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
379
380 bool isKnownNegative = false;
381 bool isKnownNonNegative = false;
382 // If the multiplication is known not to overflow, compute the sign bit.
383 if (NSW) {
384 if (Op0 == Op1) {
385 // The product of a number with itself is non-negative.
386 isKnownNonNegative = true;
387 } else {
388 bool isKnownNonNegativeOp1 = Known.isNonNegative();
389 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
390 bool isKnownNegativeOp1 = Known.isNegative();
391 bool isKnownNegativeOp0 = Known2.isNegative();
392 // The product of two numbers with the same sign is non-negative.
393 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
394 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
395 if (!isKnownNonNegative && NUW) {
396 // mul nuw nsw with a factor > 1 is non-negative.
398 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
399 KnownBits::sgt(Known2, One).value_or(false);
400 }
401
402 // The product of a negative number and a non-negative number is either
403 // negative or zero.
406 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
407 Known2.isNonZero()) ||
408 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
409 }
410 }
411
412 bool SelfMultiply = Op0 == Op1;
413 if (SelfMultiply)
414 SelfMultiply &=
415 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
416 Known = KnownBits::mul(Known, Known2, SelfMultiply);
417
418 if (SelfMultiply) {
419 unsigned SignBits = ComputeNumSignBits(Op0, DemandedElts, Q, Depth + 1);
420 unsigned TyBits = Op0->getType()->getScalarSizeInBits();
421 unsigned OutValidBits = 2 * (TyBits - SignBits + 1);
422
423 if (OutValidBits < TyBits) {
424 APInt KnownZeroMask =
425 APInt::getHighBitsSet(TyBits, TyBits - OutValidBits + 1);
426 Known.Zero |= KnownZeroMask;
427 }
428 }
429
430 // Only make use of no-wrap flags if we failed to compute the sign bit
431 // directly. This matters if the multiplication always overflows, in
432 // which case we prefer to follow the result of the direct computation,
433 // though as the program is invoking undefined behaviour we can choose
434 // whatever we like here.
435 if (isKnownNonNegative && !Known.isNegative())
436 Known.makeNonNegative();
437 else if (isKnownNegative && !Known.isNonNegative())
438 Known.makeNegative();
439}
440
442 KnownBits &Known) {
443 unsigned BitWidth = Known.getBitWidth();
444 unsigned NumRanges = Ranges.getNumOperands() / 2;
445 assert(NumRanges >= 1);
446
447 Known.setAllConflict();
448
449 for (unsigned i = 0; i < NumRanges; ++i) {
451 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
453 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
454 ConstantRange Range(Lower->getValue(), Upper->getValue());
455 // BitWidth must equal the Ranges BitWidth for the correct number of high
456 // bits to be set.
457 assert(BitWidth == Range.getBitWidth() &&
458 "Known bit width must match range bit width!");
459
460 // The first CommonPrefixBits of all values in Range are equal.
461 unsigned CommonPrefixBits =
462 (Range.getUnsignedMax() ^ Range.getUnsignedMin()).countl_zero();
463 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
464 APInt UnsignedMax = Range.getUnsignedMax().zextOrTrunc(BitWidth);
465 Known.One &= UnsignedMax & Mask;
466 Known.Zero &= ~UnsignedMax & Mask;
467 }
468}
469
470static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
474
475 // The instruction defining an assumption's condition itself is always
476 // considered ephemeral to that assumption (even if it has other
477 // non-ephemeral users). See r246696's test case for an example.
478 if (is_contained(I->operands(), E))
479 return true;
480
481 while (!WorkSet.empty()) {
482 const Instruction *V = WorkSet.pop_back_val();
483 if (!Visited.insert(V).second)
484 continue;
485
486 // If all uses of this value are ephemeral, then so is this value.
487 if (all_of(V->users(), [&](const User *U) {
488 return EphValues.count(cast<Instruction>(U));
489 })) {
490 if (V == E)
491 return true;
492
493 if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {
494 EphValues.insert(V);
495
496 if (const User *U = dyn_cast<User>(V)) {
497 for (const Use &U : U->operands()) {
498 if (const auto *I = dyn_cast<Instruction>(U.get()))
499 WorkSet.push_back(I);
500 }
501 }
502 }
503 }
504 }
505
506 return false;
507}
508
509// Is this an intrinsic that cannot be speculated but also cannot trap?
511 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
512 return CI->isAssumeLikeIntrinsic();
513
514 return false;
515}
516
518 const Instruction *CxtI,
519 const DominatorTree *DT,
520 bool AllowEphemerals) {
521 // There are two restrictions on the use of an assume:
522 // 1. The assume must dominate the context (or the control flow must
523 // reach the assume whenever it reaches the context).
524 // 2. The context must not be in the assume's set of ephemeral values
525 // (otherwise we will use the assume to prove that the condition
526 // feeding the assume is trivially true, thus causing the removal of
527 // the assume).
528
529 if (Inv->getParent() == CxtI->getParent()) {
530 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
531 // in the BB.
532 if (Inv->comesBefore(CxtI))
533 return true;
534
535 // Don't let an assume affect itself - this would cause the problems
536 // `isEphemeralValueOf` is trying to prevent, and it would also make
537 // the loop below go out of bounds.
538 if (!AllowEphemerals && Inv == CxtI)
539 return false;
540
541 // The context comes first, but they're both in the same block.
542 // Make sure there is nothing in between that might interrupt
543 // the control flow, not even CxtI itself.
544 // We limit the scan distance between the assume and its context instruction
545 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
546 // it can be adjusted if needed (could be turned into a cl::opt).
547 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
549 return false;
550
551 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
552 }
553
554 // Inv and CxtI are in different blocks.
555 if (DT) {
556 if (DT->dominates(Inv, CxtI))
557 return true;
558 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
559 Inv->getParent()->isEntryBlock()) {
560 // We don't have a DT, but this trivially dominates.
561 return true;
562 }
563
564 return false;
565}
566
568 const Instruction *CtxI) {
569 if (CtxI->getParent() != Assume->getParent() || !Assume->comesBefore(CtxI))
570 return false;
571 // Make sure the current function cannot arrange for another thread to free on
572 // its behalf.
573 if (!CtxI->getFunction()->hasNoSync())
574 return false;
575
576 // Check if there are any calls between the assume and CtxI that may
577 // free memory.
578 for (const auto &[Idx, I] :
579 enumerate(make_range(Assume->getIterator(), CtxI->getIterator()))) {
580 // Limit number of instructions to walk.
581 if (Idx > MaxInstrsToCheckForFree)
582 return false;
583 if (const auto *CB = dyn_cast<CallBase>(&I))
584 if (!CB->hasFnAttr(Attribute::NoFree))
585 return false;
586 }
587 return true;
588}
589
590// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
591// we still have enough information about `RHS` to conclude non-zero. For
592// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
593// so the extra compile time may not be worth it, but possibly a second API
594// should be created for use outside of loops.
595static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
596 // v u> y implies v != 0.
597 if (Pred == ICmpInst::ICMP_UGT)
598 return true;
599
600 // Special-case v != 0 to also handle v != null.
601 if (Pred == ICmpInst::ICMP_NE)
602 return match(RHS, m_Zero());
603
604 // All other predicates - rely on generic ConstantRange handling.
605 const APInt *C;
606 auto Zero = APInt::getZero(RHS->getType()->getScalarSizeInBits());
607 if (match(RHS, m_APInt(C))) {
609 return !TrueValues.contains(Zero);
610 }
611
613 if (VC == nullptr)
614 return false;
615
616 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
617 ++ElemIdx) {
619 Pred, VC->getElementAsAPInt(ElemIdx));
620 if (TrueValues.contains(Zero))
621 return false;
622 }
623 return true;
624}
625
626static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
627 Value *&ValOut, Instruction *&CtxIOut,
628 const PHINode **PhiOut = nullptr) {
629 ValOut = U->get();
630 if (ValOut == PHI)
631 return;
632 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
633 if (PhiOut)
634 *PhiOut = PHI;
635 Value *V;
636 // If the Use is a select of this phi, compute analysis on other arm to break
637 // recursion.
638 // TODO: Min/Max
639 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
640 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
641 ValOut = V;
642
643 // Same for select, if this phi is 2-operand phi, compute analysis on other
644 // incoming value to break recursion.
645 // TODO: We could handle any number of incoming edges as long as we only have
646 // two unique values.
647 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
648 IncPhi && IncPhi->getNumIncomingValues() == 2) {
649 for (int Idx = 0; Idx < 2; ++Idx) {
650 if (IncPhi->getIncomingValue(Idx) == PHI) {
651 ValOut = IncPhi->getIncomingValue(1 - Idx);
652 if (PhiOut)
653 *PhiOut = IncPhi;
654 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
655 break;
656 }
657 }
658 }
659}
660
661static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
662 // Use of assumptions is context-sensitive. If we don't have a context, we
663 // cannot use them!
664 if (!Q.AC || !Q.CxtI)
665 return false;
666
667 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
668 if (!Elem.Assume)
669 continue;
670
671 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
672 assert(I->getFunction() == Q.CxtI->getFunction() &&
673 "Got assumption for the wrong function!");
674
675 if (Elem.Index != AssumptionCache::ExprResultIdx) {
676 if (!V->getType()->isPointerTy())
677 continue;
679 *I, I->bundle_op_info_begin()[Elem.Index])) {
680 if (RK.WasOn == V &&
681 (RK.AttrKind == Attribute::NonNull ||
682 (RK.AttrKind == Attribute::Dereferenceable &&
684 V->getType()->getPointerAddressSpace()))) &&
686 return true;
687 }
688 continue;
689 }
690
691 // Warning: This loop can end up being somewhat performance sensitive.
692 // We're running this loop for once for each value queried resulting in a
693 // runtime of ~O(#assumes * #values).
694
695 Value *RHS;
696 CmpPredicate Pred;
697 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
698 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
699 continue;
700
701 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
702 return true;
703 }
704
705 return false;
706}
707
709 Value *LHS, Value *RHS, KnownBits &Known,
710 const SimplifyQuery &Q) {
711 if (RHS->getType()->isPointerTy()) {
712 // Handle comparison of pointer to null explicitly, as it will not be
713 // covered by the m_APInt() logic below.
714 if (LHS == V && match(RHS, m_Zero())) {
715 switch (Pred) {
717 Known.setAllZero();
718 break;
721 Known.makeNonNegative();
722 break;
724 Known.makeNegative();
725 break;
726 default:
727 break;
728 }
729 }
730 return;
731 }
732
733 unsigned BitWidth = Known.getBitWidth();
734 auto m_V =
736
737 Value *Y;
738 const APInt *Mask, *C;
739 if (!match(RHS, m_APInt(C)))
740 return;
741
742 uint64_t ShAmt;
743 switch (Pred) {
745 // assume(V = C)
746 if (match(LHS, m_V)) {
747 Known = Known.unionWith(KnownBits::makeConstant(*C));
748 // assume(V & Mask = C)
749 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
750 // For one bits in Mask, we can propagate bits from C to V.
751 Known.One |= *C;
752 if (match(Y, m_APInt(Mask)))
753 Known.Zero |= ~*C & *Mask;
754 // assume(V | Mask = C)
755 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
756 // For zero bits in Mask, we can propagate bits from C to V.
757 Known.Zero |= ~*C;
758 if (match(Y, m_APInt(Mask)))
759 Known.One |= *C & ~*Mask;
760 // assume(V << ShAmt = C)
761 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
762 ShAmt < BitWidth) {
763 // For those bits in C that are known, we can propagate them to known
764 // bits in V shifted to the right by ShAmt.
766 RHSKnown >>= ShAmt;
767 Known = Known.unionWith(RHSKnown);
768 // assume(V >> ShAmt = C)
769 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
770 ShAmt < BitWidth) {
771 // For those bits in RHS that are known, we can propagate them to known
772 // bits in V shifted to the right by C.
774 RHSKnown <<= ShAmt;
775 Known = Known.unionWith(RHSKnown);
776 }
777 break;
778 case ICmpInst::ICMP_NE: {
779 // assume (V & B != 0) where B is a power of 2
780 const APInt *BPow2;
781 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
782 Known.One |= *BPow2;
783 break;
784 }
785 default: {
786 const APInt *Offset = nullptr;
787 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
789 if (Offset)
790 LHSRange = LHSRange.sub(*Offset);
791 Known = Known.unionWith(LHSRange.toKnownBits());
792 }
793 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
794 // X & Y u> C -> X u> C && Y u> C
795 // X nuw- Y u> C -> X u> C
796 if (match(LHS, m_c_And(m_V, m_Value())) ||
797 match(LHS, m_NUWSub(m_V, m_Value())))
798 Known.One.setHighBits(
799 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
800 }
801 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
802 // X | Y u< C -> X u< C && Y u< C
803 // X nuw+ Y u< C -> X u< C && Y u< C
804 if (match(LHS, m_c_Or(m_V, m_Value())) ||
805 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
806 Known.Zero.setHighBits(
807 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
808 }
809 }
810 } break;
811 }
812}
813
814static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
815 KnownBits &Known,
816 const SimplifyQuery &SQ, bool Invert) {
818 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
819 Value *LHS = Cmp->getOperand(0);
820 Value *RHS = Cmp->getOperand(1);
821
822 // Handle icmp pred (trunc V), C
823 if (match(LHS, m_Trunc(m_Specific(V)))) {
824 KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
825 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
827 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
828 else
829 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
830 return;
831 }
832
833 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
834}
835
837 KnownBits &Known, const SimplifyQuery &SQ,
838 bool Invert, unsigned Depth) {
839 Value *A, *B;
842 KnownBits Known2(Known.getBitWidth());
843 KnownBits Known3(Known.getBitWidth());
844 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
845 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
846 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
848 Known2 = Known2.unionWith(Known3);
849 else
850 Known2 = Known2.intersectWith(Known3);
851 Known = Known.unionWith(Known2);
852 return;
853 }
854
855 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
856 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
857 return;
858 }
859
860 if (match(Cond, m_Trunc(m_Specific(V)))) {
861 KnownBits DstKnown(1);
862 if (Invert) {
863 DstKnown.setAllZero();
864 } else {
865 DstKnown.setAllOnes();
866 }
868 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
869 return;
870 }
871 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
872 return;
873 }
874
876 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
877}
878
880 const SimplifyQuery &Q, unsigned Depth) {
881 // Handle injected condition.
882 if (Q.CC && Q.CC->AffectedValues.contains(V))
883 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
884
885 if (!Q.CxtI)
886 return;
887
888 if (Q.DC && Q.DT) {
889 // Handle dominating conditions.
890 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
891 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
892 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
893 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
894 /*Invert*/ false, Depth);
895
896 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
897 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
898 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
899 /*Invert*/ true, Depth);
900 }
901
902 if (Known.hasConflict())
903 Known.resetAll();
904 }
905
906 if (!Q.AC)
907 return;
908
909 unsigned BitWidth = Known.getBitWidth();
910
911 // Note that the patterns below need to be kept in sync with the code
912 // in AssumptionCache::updateAffectedValues.
913
914 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
915 if (!Elem.Assume)
916 continue;
917
918 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
919 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
920 "Got assumption for the wrong function!");
921
922 if (Elem.Index != AssumptionCache::ExprResultIdx) {
923 if (!V->getType()->isPointerTy())
924 continue;
926 *I, I->bundle_op_info_begin()[Elem.Index])) {
927 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
928 // be the producer of the pointer in the bundle. At the moment, align
929 // assumptions aren't optimized away.
930 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
931 isPowerOf2_64(RK.ArgValue) &&
932 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
933 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
934 }
935 continue;
936 }
937
938 // Warning: This loop can end up being somewhat performance sensitive.
939 // We're running this loop for once for each value queried resulting in a
940 // runtime of ~O(#assumes * #values).
941
942 Value *Arg = I->getArgOperand(0);
943
944 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
945 assert(BitWidth == 1 && "assume operand is not i1?");
946 (void)BitWidth;
947 Known.setAllOnes();
948 return;
949 }
950 if (match(Arg, m_Not(m_Specific(V))) &&
952 assert(BitWidth == 1 && "assume operand is not i1?");
953 (void)BitWidth;
954 Known.setAllZero();
955 return;
956 }
957 auto *Trunc = dyn_cast<TruncInst>(Arg);
958 if (Trunc && Trunc->getOperand(0) == V &&
960 if (Trunc->hasNoUnsignedWrap()) {
962 return;
963 }
964 Known.One.setBit(0);
965 return;
966 }
967
968 // The remaining tests are all recursive, so bail out if we hit the limit.
970 continue;
971
972 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
973 if (!Cmp)
974 continue;
975
976 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
977 continue;
978
979 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
980 }
981
982 // Conflicting assumption: Undefined behavior will occur on this execution
983 // path.
984 if (Known.hasConflict())
985 Known.resetAll();
986}
987
988/// Compute known bits from a shift operator, including those with a
989/// non-constant shift amount. Known is the output of this function. Known2 is a
990/// pre-allocated temporary with the same bit width as Known and on return
991/// contains the known bit of the shift value source. KF is an
992/// operator-specific function that, given the known-bits and a shift amount,
993/// compute the implied known-bits of the shift operator's result respectively
994/// for that shift amount. The results from calling KF are conservatively
995/// combined for all permitted shift amounts.
997 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
998 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
999 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
1000 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1001 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1002 // To limit compile-time impact, only query isKnownNonZero() if we know at
1003 // least something about the shift amount.
1004 bool ShAmtNonZero =
1005 Known.isNonZero() ||
1006 (Known.getMaxValue().ult(Known.getBitWidth()) &&
1007 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
1008 Known = KF(Known2, Known, ShAmtNonZero);
1009}
1010
1011static KnownBits
1012getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
1013 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
1014 const SimplifyQuery &Q, unsigned Depth) {
1015 unsigned BitWidth = KnownLHS.getBitWidth();
1016 KnownBits KnownOut(BitWidth);
1017 bool IsAnd = false;
1018 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
1019 Value *X = nullptr, *Y = nullptr;
1020
1021 switch (I->getOpcode()) {
1022 case Instruction::And:
1023 KnownOut = KnownLHS & KnownRHS;
1024 IsAnd = true;
1025 // and(x, -x) is common idioms that will clear all but lowest set
1026 // bit. If we have a single known bit in x, we can clear all bits
1027 // above it.
1028 // TODO: instcombine often reassociates independent `and` which can hide
1029 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
1030 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
1031 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
1032 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
1033 KnownOut = KnownLHS.blsi();
1034 else
1035 KnownOut = KnownRHS.blsi();
1036 }
1037 break;
1038 case Instruction::Or:
1039 KnownOut = KnownLHS | KnownRHS;
1040 break;
1041 case Instruction::Xor:
1042 KnownOut = KnownLHS ^ KnownRHS;
1043 // xor(x, x-1) is common idioms that will clear all but lowest set
1044 // bit. If we have a single known bit in x, we can clear all bits
1045 // above it.
1046 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1047 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1048 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1049 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1050 if (HasKnownOne &&
1052 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1053 KnownOut = XBits.blsmsk();
1054 }
1055 break;
1056 default:
1057 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1058 }
1059
1060 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1061 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1062 // here we handle the more general case of adding any odd number by
1063 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1064 // TODO: This could be generalized to clearing any bit set in y where the
1065 // following bit is known to be unset in y.
1066 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1070 KnownBits KnownY(BitWidth);
1071 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1072 if (KnownY.countMinTrailingOnes() > 0) {
1073 if (IsAnd)
1074 KnownOut.Zero.setBit(0);
1075 else
1076 KnownOut.One.setBit(0);
1077 }
1078 }
1079 return KnownOut;
1080}
1081
1083 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1084 unsigned Depth,
1085 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1086 KnownBitsFunc) {
1087 APInt DemandedEltsLHS, DemandedEltsRHS;
1089 DemandedElts, DemandedEltsLHS,
1090 DemandedEltsRHS);
1091
1092 const auto ComputeForSingleOpFunc =
1093 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1094 return KnownBitsFunc(
1095 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1096 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1097 };
1098
1099 if (DemandedEltsRHS.isZero())
1100 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1101 if (DemandedEltsLHS.isZero())
1102 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1103
1104 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1105 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1106}
1107
1108// Public so this can be used in `SimplifyDemandedUseBits`.
1110 const KnownBits &KnownLHS,
1111 const KnownBits &KnownRHS,
1112 const SimplifyQuery &SQ,
1113 unsigned Depth) {
1114 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1115 APInt DemandedElts =
1116 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1117
1118 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1119 Depth);
1120}
1121
1123 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1124 // Without vscale_range, we only know that vscale is non-zero.
1125 if (!Attr.isValid())
1127
1128 unsigned AttrMin = Attr.getVScaleRangeMin();
1129 // Minimum is larger than vscale width, result is always poison.
1130 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1131 return ConstantRange::getEmpty(BitWidth);
1132
1133 APInt Min(BitWidth, AttrMin);
1134 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1135 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1137
1138 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1139}
1140
1142 Value *Arm, bool Invert,
1143 const SimplifyQuery &Q, unsigned Depth) {
1144 // If we have a constant arm, we are done.
1145 if (Known.isConstant())
1146 return;
1147
1148 // See what condition implies about the bits of the select arm.
1149 KnownBits CondRes(Known.getBitWidth());
1150 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1151 // If we don't get any information from the condition, no reason to
1152 // proceed.
1153 if (CondRes.isUnknown())
1154 return;
1155
1156 // We can have conflict if the condition is dead. I.e if we have
1157 // (x | 64) < 32 ? (x | 64) : y
1158 // we will have conflict at bit 6 from the condition/the `or`.
1159 // In that case just return. Its not particularly important
1160 // what we do, as this select is going to be simplified soon.
1161 CondRes = CondRes.unionWith(Known);
1162 if (CondRes.hasConflict())
1163 return;
1164
1165 // Finally make sure the information we found is valid. This is relatively
1166 // expensive so it's left for the very end.
1167 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1168 return;
1169
1170 // Finally, we know we get information from the condition and its valid,
1171 // so return it.
1172 Known = CondRes;
1173}
1174
1175// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1176// Returns the input and lower/upper bounds.
1177static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1178 const APInt *&CLow, const APInt *&CHigh) {
1180 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1181 "Input should be a Select!");
1182
1183 const Value *LHS = nullptr, *RHS = nullptr;
1185 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1186 return false;
1187
1188 if (!match(RHS, m_APInt(CLow)))
1189 return false;
1190
1191 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1193 if (getInverseMinMaxFlavor(SPF) != SPF2)
1194 return false;
1195
1196 if (!match(RHS2, m_APInt(CHigh)))
1197 return false;
1198
1199 if (SPF == SPF_SMIN)
1200 std::swap(CLow, CHigh);
1201
1202 In = LHS2;
1203 return CLow->sle(*CHigh);
1204}
1205
1207 const APInt *&CLow,
1208 const APInt *&CHigh) {
1209 assert((II->getIntrinsicID() == Intrinsic::smin ||
1210 II->getIntrinsicID() == Intrinsic::smax) &&
1211 "Must be smin/smax");
1212
1213 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1214 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1215 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1216 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1217 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1218 return false;
1219
1220 if (II->getIntrinsicID() == Intrinsic::smin)
1221 std::swap(CLow, CHigh);
1222 return CLow->sle(*CHigh);
1223}
1224
1226 KnownBits &Known) {
1227 const APInt *CLow, *CHigh;
1228 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1229 Known = Known.unionWith(
1230 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1231}
1232
1234 const APInt &DemandedElts,
1235 KnownBits &Known,
1236 const SimplifyQuery &Q,
1237 unsigned Depth) {
1238 unsigned BitWidth = Known.getBitWidth();
1239
1240 KnownBits Known2(BitWidth);
1241 switch (I->getOpcode()) {
1242 default: break;
1243 case Instruction::Load:
1244 if (MDNode *MD =
1245 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1247 break;
1248 case Instruction::And:
1249 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1250 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1251
1252 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1253 break;
1254 case Instruction::Or:
1255 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1256 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1257
1258 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1259 break;
1260 case Instruction::Xor:
1261 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1262 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1263
1264 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1265 break;
1266 case Instruction::Mul: {
1269 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1270 DemandedElts, Known, Known2, Q, Depth);
1271 break;
1272 }
1273 case Instruction::UDiv: {
1274 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1275 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1276 Known =
1277 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1278 break;
1279 }
1280 case Instruction::SDiv: {
1281 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1282 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1283 Known =
1284 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1285 break;
1286 }
1287 case Instruction::Select: {
1288 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1289 KnownBits Res(Known.getBitWidth());
1290 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1291 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1292 return Res;
1293 };
1294 // Only known if known in both the LHS and RHS.
1295 Known =
1296 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1297 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1298 break;
1299 }
1300 case Instruction::FPTrunc:
1301 case Instruction::FPExt:
1302 case Instruction::FPToUI:
1303 case Instruction::FPToSI:
1304 case Instruction::SIToFP:
1305 case Instruction::UIToFP:
1306 break; // Can't work with floating point.
1307 case Instruction::PtrToInt:
1308 case Instruction::IntToPtr:
1309 // Fall through and handle them the same as zext/trunc.
1310 [[fallthrough]];
1311 case Instruction::ZExt:
1312 case Instruction::Trunc: {
1313 Type *SrcTy = I->getOperand(0)->getType();
1314
1315 unsigned SrcBitWidth;
1316 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1317 // which fall through here.
1318 Type *ScalarTy = SrcTy->getScalarType();
1319 SrcBitWidth = ScalarTy->isPointerTy() ?
1320 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1321 Q.DL.getTypeSizeInBits(ScalarTy);
1322
1323 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1324 Known = Known.anyextOrTrunc(SrcBitWidth);
1325 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1326 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1327 Inst && Inst->hasNonNeg() && !Known.isNegative())
1328 Known.makeNonNegative();
1329 Known = Known.zextOrTrunc(BitWidth);
1330 break;
1331 }
1332 case Instruction::BitCast: {
1333 Type *SrcTy = I->getOperand(0)->getType();
1334 if (SrcTy->isIntOrPtrTy() &&
1335 // TODO: For now, not handling conversions like:
1336 // (bitcast i64 %x to <2 x i32>)
1337 !I->getType()->isVectorTy()) {
1338 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1339 break;
1340 }
1341
1342 const Value *V;
1343 // Handle bitcast from floating point to integer.
1344 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1345 V->getType()->isFPOrFPVectorTy()) {
1346 Type *FPType = V->getType()->getScalarType();
1347 KnownFPClass Result =
1348 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1349 FPClassTest FPClasses = Result.KnownFPClasses;
1350
1351 // TODO: Treat it as zero/poison if the use of I is unreachable.
1352 if (FPClasses == fcNone)
1353 break;
1354
1355 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1356 Known.setAllConflict();
1357
1358 if (FPClasses & fcInf)
1360 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1361
1362 if (FPClasses & fcZero)
1364 APInt::getZero(FPType->getScalarSizeInBits())));
1365
1366 Known.Zero.clearSignBit();
1367 Known.One.clearSignBit();
1368 }
1369
1370 if (Result.SignBit) {
1371 if (*Result.SignBit)
1372 Known.makeNegative();
1373 else
1374 Known.makeNonNegative();
1375 }
1376
1377 break;
1378 }
1379
1380 // Handle cast from vector integer type to scalar or vector integer.
1381 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1382 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1383 !I->getType()->isIntOrIntVectorTy() ||
1384 isa<ScalableVectorType>(I->getType()))
1385 break;
1386
1387 unsigned NumElts = DemandedElts.getBitWidth();
1388 bool IsLE = Q.DL.isLittleEndian();
1389 // Look through a cast from narrow vector elements to wider type.
1390 // Examples: v4i32 -> v2i64, v3i8 -> v24
1391 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1392 if (BitWidth % SubBitWidth == 0) {
1393 // Known bits are automatically intersected across demanded elements of a
1394 // vector. So for example, if a bit is computed as known zero, it must be
1395 // zero across all demanded elements of the vector.
1396 //
1397 // For this bitcast, each demanded element of the output is sub-divided
1398 // across a set of smaller vector elements in the source vector. To get
1399 // the known bits for an entire element of the output, compute the known
1400 // bits for each sub-element sequentially. This is done by shifting the
1401 // one-set-bit demanded elements parameter across the sub-elements for
1402 // consecutive calls to computeKnownBits. We are using the demanded
1403 // elements parameter as a mask operator.
1404 //
1405 // The known bits of each sub-element are then inserted into place
1406 // (dependent on endian) to form the full result of known bits.
1407 unsigned SubScale = BitWidth / SubBitWidth;
1408 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1409 for (unsigned i = 0; i != NumElts; ++i) {
1410 if (DemandedElts[i])
1411 SubDemandedElts.setBit(i * SubScale);
1412 }
1413
1414 KnownBits KnownSrc(SubBitWidth);
1415 for (unsigned i = 0; i != SubScale; ++i) {
1416 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1417 Depth + 1);
1418 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1419 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1420 }
1421 }
1422 // Look through a cast from wider vector elements to narrow type.
1423 // Examples: v2i64 -> v4i32
1424 if (SubBitWidth % BitWidth == 0) {
1425 unsigned SubScale = SubBitWidth / BitWidth;
1426 KnownBits KnownSrc(SubBitWidth);
1427 APInt SubDemandedElts =
1428 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1429 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1430 Depth + 1);
1431
1432 Known.setAllConflict();
1433 for (unsigned i = 0; i != NumElts; ++i) {
1434 if (DemandedElts[i]) {
1435 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1436 unsigned Offset = (Shifts % SubScale) * BitWidth;
1437 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1438 if (Known.isUnknown())
1439 break;
1440 }
1441 }
1442 }
1443 break;
1444 }
1445 case Instruction::SExt: {
1446 // Compute the bits in the result that are not present in the input.
1447 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1448
1449 Known = Known.trunc(SrcBitWidth);
1450 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1451 // If the sign bit of the input is known set or clear, then we know the
1452 // top bits of the result.
1453 Known = Known.sext(BitWidth);
1454 break;
1455 }
1456 case Instruction::Shl: {
1459 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1460 bool ShAmtNonZero) {
1461 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1462 };
1463 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1464 KF);
1465 // Trailing zeros of a right-shifted constant never decrease.
1466 const APInt *C;
1467 if (match(I->getOperand(0), m_APInt(C)))
1468 Known.Zero.setLowBits(C->countr_zero());
1469 break;
1470 }
1471 case Instruction::LShr: {
1472 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1473 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1474 bool ShAmtNonZero) {
1475 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1476 };
1477 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1478 KF);
1479 // Leading zeros of a left-shifted constant never decrease.
1480 const APInt *C;
1481 if (match(I->getOperand(0), m_APInt(C)))
1482 Known.Zero.setHighBits(C->countl_zero());
1483 break;
1484 }
1485 case Instruction::AShr: {
1486 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1487 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1488 bool ShAmtNonZero) {
1489 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1490 };
1491 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1492 KF);
1493 break;
1494 }
1495 case Instruction::Sub: {
1498 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1499 DemandedElts, Known, Known2, Q, Depth);
1500 break;
1501 }
1502 case Instruction::Add: {
1505 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1506 DemandedElts, Known, Known2, Q, Depth);
1507 break;
1508 }
1509 case Instruction::SRem:
1510 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1511 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1512 Known = KnownBits::srem(Known, Known2);
1513 break;
1514
1515 case Instruction::URem:
1516 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1517 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1518 Known = KnownBits::urem(Known, Known2);
1519 break;
1520 case Instruction::Alloca:
1522 break;
1523 case Instruction::GetElementPtr: {
1524 // Analyze all of the subscripts of this getelementptr instruction
1525 // to determine if we can prove known low zero bits.
1526 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1527 // Accumulate the constant indices in a separate variable
1528 // to minimize the number of calls to computeForAddSub.
1529 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1530 APInt AccConstIndices(IndexWidth, 0);
1531
1532 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1533 if (IndexWidth == BitWidth) {
1534 // Note that inbounds does *not* guarantee nsw for the addition, as only
1535 // the offset is signed, while the base address is unsigned.
1536 Known = KnownBits::add(Known, IndexBits);
1537 } else {
1538 // If the index width is smaller than the pointer width, only add the
1539 // value to the low bits.
1540 assert(IndexWidth < BitWidth &&
1541 "Index width can't be larger than pointer width");
1542 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1543 }
1544 };
1545
1547 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1548 // TrailZ can only become smaller, short-circuit if we hit zero.
1549 if (Known.isUnknown())
1550 break;
1551
1552 Value *Index = I->getOperand(i);
1553
1554 // Handle case when index is zero.
1555 Constant *CIndex = dyn_cast<Constant>(Index);
1556 if (CIndex && CIndex->isZeroValue())
1557 continue;
1558
1559 if (StructType *STy = GTI.getStructTypeOrNull()) {
1560 // Handle struct member offset arithmetic.
1561
1562 assert(CIndex &&
1563 "Access to structure field must be known at compile time");
1564
1565 if (CIndex->getType()->isVectorTy())
1566 Index = CIndex->getSplatValue();
1567
1568 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1569 const StructLayout *SL = Q.DL.getStructLayout(STy);
1570 uint64_t Offset = SL->getElementOffset(Idx);
1571 AccConstIndices += Offset;
1572 continue;
1573 }
1574
1575 // Handle array index arithmetic.
1576 Type *IndexedTy = GTI.getIndexedType();
1577 if (!IndexedTy->isSized()) {
1578 Known.resetAll();
1579 break;
1580 }
1581
1582 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1583 uint64_t StrideInBytes = Stride.getKnownMinValue();
1584 if (!Stride.isScalable()) {
1585 // Fast path for constant offset.
1586 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1587 AccConstIndices +=
1588 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1589 continue;
1590 }
1591 }
1592
1593 KnownBits IndexBits =
1594 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1595 KnownBits ScalingFactor(IndexWidth);
1596 // Multiply by current sizeof type.
1597 // &A[i] == A + i * sizeof(*A[i]).
1598 if (Stride.isScalable()) {
1599 // For scalable types the only thing we know about sizeof is
1600 // that this is a multiple of the minimum size.
1601 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1602 } else {
1603 ScalingFactor =
1604 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1605 }
1606 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1607 }
1608 if (!Known.isUnknown() && !AccConstIndices.isZero())
1609 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1610 break;
1611 }
1612 case Instruction::PHI: {
1613 const PHINode *P = cast<PHINode>(I);
1614 BinaryOperator *BO = nullptr;
1615 Value *R = nullptr, *L = nullptr;
1616 if (matchSimpleRecurrence(P, BO, R, L)) {
1617 // Handle the case of a simple two-predecessor recurrence PHI.
1618 // There's a lot more that could theoretically be done here, but
1619 // this is sufficient to catch some interesting cases.
1620 unsigned Opcode = BO->getOpcode();
1621
1622 switch (Opcode) {
1623 // If this is a shift recurrence, we know the bits being shifted in. We
1624 // can combine that with information about the start value of the
1625 // recurrence to conclude facts about the result. If this is a udiv
1626 // recurrence, we know that the result can never exceed either the
1627 // numerator or the start value, whichever is greater.
1628 case Instruction::LShr:
1629 case Instruction::AShr:
1630 case Instruction::Shl:
1631 case Instruction::UDiv:
1632 if (BO->getOperand(0) != I)
1633 break;
1634 [[fallthrough]];
1635
1636 // For a urem recurrence, the result can never exceed the start value. The
1637 // phi could either be the numerator or the denominator.
1638 case Instruction::URem: {
1639 // We have matched a recurrence of the form:
1640 // %iv = [R, %entry], [%iv.next, %backedge]
1641 // %iv.next = shift_op %iv, L
1642
1643 // Recurse with the phi context to avoid concern about whether facts
1644 // inferred hold at original context instruction. TODO: It may be
1645 // correct to use the original context. IF warranted, explore and
1646 // add sufficient tests to cover.
1648 RecQ.CxtI = P;
1649 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1650 switch (Opcode) {
1651 case Instruction::Shl:
1652 // A shl recurrence will only increase the tailing zeros
1653 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1654 break;
1655 case Instruction::LShr:
1656 case Instruction::UDiv:
1657 case Instruction::URem:
1658 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1659 // the start value.
1660 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1661 break;
1662 case Instruction::AShr:
1663 // An ashr recurrence will extend the initial sign bit
1664 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1665 Known.One.setHighBits(Known2.countMinLeadingOnes());
1666 break;
1667 }
1668 break;
1669 }
1670
1671 // Check for operations that have the property that if
1672 // both their operands have low zero bits, the result
1673 // will have low zero bits.
1674 case Instruction::Add:
1675 case Instruction::Sub:
1676 case Instruction::And:
1677 case Instruction::Or:
1678 case Instruction::Mul: {
1679 // Change the context instruction to the "edge" that flows into the
1680 // phi. This is important because that is where the value is actually
1681 // "evaluated" even though it is used later somewhere else. (see also
1682 // D69571).
1684
1685 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1686 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1687 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1688
1689 // Ok, we have a PHI of the form L op= R. Check for low
1690 // zero bits.
1691 RecQ.CxtI = RInst;
1692 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1693
1694 // We need to take the minimum number of known bits
1695 KnownBits Known3(BitWidth);
1696 RecQ.CxtI = LInst;
1697 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1698
1699 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1700 Known3.countMinTrailingZeros()));
1701
1702 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1703 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1704 break;
1705
1706 switch (Opcode) {
1707 // If initial value of recurrence is nonnegative, and we are adding
1708 // a nonnegative number with nsw, the result can only be nonnegative
1709 // or poison value regardless of the number of times we execute the
1710 // add in phi recurrence. If initial value is negative and we are
1711 // adding a negative number with nsw, the result can only be
1712 // negative or poison value. Similar arguments apply to sub and mul.
1713 //
1714 // (add non-negative, non-negative) --> non-negative
1715 // (add negative, negative) --> negative
1716 case Instruction::Add: {
1717 if (Known2.isNonNegative() && Known3.isNonNegative())
1718 Known.makeNonNegative();
1719 else if (Known2.isNegative() && Known3.isNegative())
1720 Known.makeNegative();
1721 break;
1722 }
1723
1724 // (sub nsw non-negative, negative) --> non-negative
1725 // (sub nsw negative, non-negative) --> negative
1726 case Instruction::Sub: {
1727 if (BO->getOperand(0) != I)
1728 break;
1729 if (Known2.isNonNegative() && Known3.isNegative())
1730 Known.makeNonNegative();
1731 else if (Known2.isNegative() && Known3.isNonNegative())
1732 Known.makeNegative();
1733 break;
1734 }
1735
1736 // (mul nsw non-negative, non-negative) --> non-negative
1737 case Instruction::Mul:
1738 if (Known2.isNonNegative() && Known3.isNonNegative())
1739 Known.makeNonNegative();
1740 break;
1741
1742 default:
1743 break;
1744 }
1745 break;
1746 }
1747
1748 default:
1749 break;
1750 }
1751 }
1752
1753 // Unreachable blocks may have zero-operand PHI nodes.
1754 if (P->getNumIncomingValues() == 0)
1755 break;
1756
1757 // Otherwise take the unions of the known bit sets of the operands,
1758 // taking conservative care to avoid excessive recursion.
1759 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1760 // Skip if every incoming value references to ourself.
1761 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1762 break;
1763
1764 Known.setAllConflict();
1765 for (const Use &U : P->operands()) {
1766 Value *IncValue;
1767 const PHINode *CxtPhi;
1768 Instruction *CxtI;
1769 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1770 // Skip direct self references.
1771 if (IncValue == P)
1772 continue;
1773
1774 // Change the context instruction to the "edge" that flows into the
1775 // phi. This is important because that is where the value is actually
1776 // "evaluated" even though it is used later somewhere else. (see also
1777 // D69571).
1779
1780 Known2 = KnownBits(BitWidth);
1781
1782 // Recurse, but cap the recursion to one level, because we don't
1783 // want to waste time spinning around in loops.
1784 // TODO: See if we can base recursion limiter on number of incoming phi
1785 // edges so we don't overly clamp analysis.
1786 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1788
1789 // See if we can further use a conditional branch into the phi
1790 // to help us determine the range of the value.
1791 if (!Known2.isConstant()) {
1792 CmpPredicate Pred;
1793 const APInt *RHSC;
1794 BasicBlock *TrueSucc, *FalseSucc;
1795 // TODO: Use RHS Value and compute range from its known bits.
1796 if (match(RecQ.CxtI,
1797 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1798 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1799 // Check for cases of duplicate successors.
1800 if ((TrueSucc == CxtPhi->getParent()) !=
1801 (FalseSucc == CxtPhi->getParent())) {
1802 // If we're using the false successor, invert the predicate.
1803 if (FalseSucc == CxtPhi->getParent())
1804 Pred = CmpInst::getInversePredicate(Pred);
1805 // Get the knownbits implied by the incoming phi condition.
1806 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1807 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1808 // We can have conflicts here if we are analyzing deadcode (its
1809 // impossible for us reach this BB based the icmp).
1810 if (KnownUnion.hasConflict()) {
1811 // No reason to continue analyzing in a known dead region, so
1812 // just resetAll and break. This will cause us to also exit the
1813 // outer loop.
1814 Known.resetAll();
1815 break;
1816 }
1817 Known2 = KnownUnion;
1818 }
1819 }
1820 }
1821
1822 Known = Known.intersectWith(Known2);
1823 // If all bits have been ruled out, there's no need to check
1824 // more operands.
1825 if (Known.isUnknown())
1826 break;
1827 }
1828 }
1829 break;
1830 }
1831 case Instruction::Call:
1832 case Instruction::Invoke: {
1833 // If range metadata is attached to this call, set known bits from that,
1834 // and then intersect with known bits based on other properties of the
1835 // function.
1836 if (MDNode *MD =
1837 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1839
1840 const auto *CB = cast<CallBase>(I);
1841
1842 if (std::optional<ConstantRange> Range = CB->getRange())
1843 Known = Known.unionWith(Range->toKnownBits());
1844
1845 if (const Value *RV = CB->getReturnedArgOperand()) {
1846 if (RV->getType() == I->getType()) {
1847 computeKnownBits(RV, Known2, Q, Depth + 1);
1848 Known = Known.unionWith(Known2);
1849 // If the function doesn't return properly for all input values
1850 // (e.g. unreachable exits) then there might be conflicts between the
1851 // argument value and the range metadata. Simply discard the known bits
1852 // in case of conflicts.
1853 if (Known.hasConflict())
1854 Known.resetAll();
1855 }
1856 }
1857 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1858 switch (II->getIntrinsicID()) {
1859 default:
1860 break;
1861 case Intrinsic::abs: {
1862 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1863 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
1864 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
1865 break;
1866 }
1867 case Intrinsic::bitreverse:
1868 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1869 Known = Known.unionWith(Known2.reverseBits());
1870 break;
1871 case Intrinsic::bswap:
1872 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1873 Known = Known.unionWith(Known2.byteSwap());
1874 break;
1875 case Intrinsic::ctlz: {
1876 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1877 // If we have a known 1, its position is our upper bound.
1878 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
1879 // If this call is poison for 0 input, the result will be less than 2^n.
1880 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1881 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1882 unsigned LowBits = llvm::bit_width(PossibleLZ);
1883 Known.Zero.setBitsFrom(LowBits);
1884 break;
1885 }
1886 case Intrinsic::cttz: {
1887 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1888 // If we have a known 1, its position is our upper bound.
1889 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
1890 // If this call is poison for 0 input, the result will be less than 2^n.
1891 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1892 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1893 unsigned LowBits = llvm::bit_width(PossibleTZ);
1894 Known.Zero.setBitsFrom(LowBits);
1895 break;
1896 }
1897 case Intrinsic::ctpop: {
1898 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1899 // We can bound the space the count needs. Also, bits known to be zero
1900 // can't contribute to the population.
1901 unsigned BitsPossiblySet = Known2.countMaxPopulation();
1902 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
1903 Known.Zero.setBitsFrom(LowBits);
1904 // TODO: we could bound KnownOne using the lower bound on the number
1905 // of bits which might be set provided by popcnt KnownOne2.
1906 break;
1907 }
1908 case Intrinsic::fshr:
1909 case Intrinsic::fshl: {
1910 const APInt *SA;
1911 if (!match(I->getOperand(2), m_APInt(SA)))
1912 break;
1913
1914 // Normalize to funnel shift left.
1915 uint64_t ShiftAmt = SA->urem(BitWidth);
1916 if (II->getIntrinsicID() == Intrinsic::fshr)
1917 ShiftAmt = BitWidth - ShiftAmt;
1918
1919 KnownBits Known3(BitWidth);
1920 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1921 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
1922
1923 Known2 <<= ShiftAmt;
1924 Known3 >>= BitWidth - ShiftAmt;
1925 Known = Known2.unionWith(Known3);
1926 break;
1927 }
1928 case Intrinsic::uadd_sat:
1929 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1930 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1931 Known = KnownBits::uadd_sat(Known, Known2);
1932 break;
1933 case Intrinsic::usub_sat:
1934 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1935 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1936 Known = KnownBits::usub_sat(Known, Known2);
1937 break;
1938 case Intrinsic::sadd_sat:
1939 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1940 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1941 Known = KnownBits::sadd_sat(Known, Known2);
1942 break;
1943 case Intrinsic::ssub_sat:
1944 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1945 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1946 Known = KnownBits::ssub_sat(Known, Known2);
1947 break;
1948 // Vec reverse preserves bits from input vec.
1949 case Intrinsic::vector_reverse:
1950 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
1951 Depth + 1);
1952 break;
1953 // for min/max/and/or reduce, any bit common to each element in the
1954 // input vec is set in the output.
1955 case Intrinsic::vector_reduce_and:
1956 case Intrinsic::vector_reduce_or:
1957 case Intrinsic::vector_reduce_umax:
1958 case Intrinsic::vector_reduce_umin:
1959 case Intrinsic::vector_reduce_smax:
1960 case Intrinsic::vector_reduce_smin:
1961 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1962 break;
1963 case Intrinsic::vector_reduce_xor: {
1964 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1965 // The zeros common to all vecs are zero in the output.
1966 // If the number of elements is odd, then the common ones remain. If the
1967 // number of elements is even, then the common ones becomes zeros.
1968 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
1969 // Even, so the ones become zeros.
1970 bool EvenCnt = VecTy->getElementCount().isKnownEven();
1971 if (EvenCnt)
1972 Known.Zero |= Known.One;
1973 // Maybe even element count so need to clear ones.
1974 if (VecTy->isScalableTy() || EvenCnt)
1975 Known.One.clearAllBits();
1976 break;
1977 }
1978 case Intrinsic::umin:
1979 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1980 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1981 Known = KnownBits::umin(Known, Known2);
1982 break;
1983 case Intrinsic::umax:
1984 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1985 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1986 Known = KnownBits::umax(Known, Known2);
1987 break;
1988 case Intrinsic::smin:
1989 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1990 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1991 Known = KnownBits::smin(Known, Known2);
1993 break;
1994 case Intrinsic::smax:
1995 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1996 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1997 Known = KnownBits::smax(Known, Known2);
1999 break;
2000 case Intrinsic::ptrmask: {
2001 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2002
2003 const Value *Mask = I->getOperand(1);
2004 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
2005 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
2006 // TODO: 1-extend would be more precise.
2007 Known &= Known2.anyextOrTrunc(BitWidth);
2008 break;
2009 }
2010 case Intrinsic::x86_sse2_pmulh_w:
2011 case Intrinsic::x86_avx2_pmulh_w:
2012 case Intrinsic::x86_avx512_pmulh_w_512:
2013 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2014 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2015 Known = KnownBits::mulhs(Known, Known2);
2016 break;
2017 case Intrinsic::x86_sse2_pmulhu_w:
2018 case Intrinsic::x86_avx2_pmulhu_w:
2019 case Intrinsic::x86_avx512_pmulhu_w_512:
2020 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
2021 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
2022 Known = KnownBits::mulhu(Known, Known2);
2023 break;
2024 case Intrinsic::x86_sse42_crc32_64_64:
2025 Known.Zero.setBitsFrom(32);
2026 break;
2027 case Intrinsic::x86_ssse3_phadd_d_128:
2028 case Intrinsic::x86_ssse3_phadd_w_128:
2029 case Intrinsic::x86_avx2_phadd_d:
2030 case Intrinsic::x86_avx2_phadd_w: {
2032 I, DemandedElts, Q, Depth,
2033 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2034 return KnownBits::add(KnownLHS, KnownRHS);
2035 });
2036 break;
2037 }
2038 case Intrinsic::x86_ssse3_phadd_sw_128:
2039 case Intrinsic::x86_avx2_phadd_sw: {
2041 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2042 break;
2043 }
2044 case Intrinsic::x86_ssse3_phsub_d_128:
2045 case Intrinsic::x86_ssse3_phsub_w_128:
2046 case Intrinsic::x86_avx2_phsub_d:
2047 case Intrinsic::x86_avx2_phsub_w: {
2049 I, DemandedElts, Q, Depth,
2050 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2051 return KnownBits::sub(KnownLHS, KnownRHS);
2052 });
2053 break;
2054 }
2055 case Intrinsic::x86_ssse3_phsub_sw_128:
2056 case Intrinsic::x86_avx2_phsub_sw: {
2058 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2059 break;
2060 }
2061 case Intrinsic::riscv_vsetvli:
2062 case Intrinsic::riscv_vsetvlimax: {
2063 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2064 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2066 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2067 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2068 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2069 uint64_t MaxVLEN =
2070 Range.getUnsignedMax().getZExtValue() * RISCV::RVVBitsPerBlock;
2071 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2072
2073 // Result of vsetvli must be not larger than AVL.
2074 if (HasAVL)
2075 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2076 MaxVL = std::min(MaxVL, CI->getZExtValue());
2077
2078 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2079 if (BitWidth > KnownZeroFirstBit)
2080 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2081 break;
2082 }
2083 case Intrinsic::vscale: {
2084 if (!II->getParent() || !II->getFunction())
2085 break;
2086
2087 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2088 break;
2089 }
2090 }
2091 }
2092 break;
2093 }
2094 case Instruction::ShuffleVector: {
2095 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2096 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2097 if (!Shuf) {
2098 Known.resetAll();
2099 return;
2100 }
2101 // For undef elements, we don't know anything about the common state of
2102 // the shuffle result.
2103 APInt DemandedLHS, DemandedRHS;
2104 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2105 Known.resetAll();
2106 return;
2107 }
2108 Known.setAllConflict();
2109 if (!!DemandedLHS) {
2110 const Value *LHS = Shuf->getOperand(0);
2111 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2112 // If we don't know any bits, early out.
2113 if (Known.isUnknown())
2114 break;
2115 }
2116 if (!!DemandedRHS) {
2117 const Value *RHS = Shuf->getOperand(1);
2118 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2119 Known = Known.intersectWith(Known2);
2120 }
2121 break;
2122 }
2123 case Instruction::InsertElement: {
2124 if (isa<ScalableVectorType>(I->getType())) {
2125 Known.resetAll();
2126 return;
2127 }
2128 const Value *Vec = I->getOperand(0);
2129 const Value *Elt = I->getOperand(1);
2130 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2131 unsigned NumElts = DemandedElts.getBitWidth();
2132 APInt DemandedVecElts = DemandedElts;
2133 bool NeedsElt = true;
2134 // If we know the index we are inserting too, clear it from Vec check.
2135 if (CIdx && CIdx->getValue().ult(NumElts)) {
2136 DemandedVecElts.clearBit(CIdx->getZExtValue());
2137 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2138 }
2139
2140 Known.setAllConflict();
2141 if (NeedsElt) {
2142 computeKnownBits(Elt, Known, Q, Depth + 1);
2143 // If we don't know any bits, early out.
2144 if (Known.isUnknown())
2145 break;
2146 }
2147
2148 if (!DemandedVecElts.isZero()) {
2149 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2150 Known = Known.intersectWith(Known2);
2151 }
2152 break;
2153 }
2154 case Instruction::ExtractElement: {
2155 // Look through extract element. If the index is non-constant or
2156 // out-of-range demand all elements, otherwise just the extracted element.
2157 const Value *Vec = I->getOperand(0);
2158 const Value *Idx = I->getOperand(1);
2159 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2160 if (isa<ScalableVectorType>(Vec->getType())) {
2161 // FIXME: there's probably *something* we can do with scalable vectors
2162 Known.resetAll();
2163 break;
2164 }
2165 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2166 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2167 if (CIdx && CIdx->getValue().ult(NumElts))
2168 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2169 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2170 break;
2171 }
2172 case Instruction::ExtractValue:
2173 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2175 if (EVI->getNumIndices() != 1) break;
2176 if (EVI->getIndices()[0] == 0) {
2177 switch (II->getIntrinsicID()) {
2178 default: break;
2179 case Intrinsic::uadd_with_overflow:
2180 case Intrinsic::sadd_with_overflow:
2182 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2183 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2184 break;
2185 case Intrinsic::usub_with_overflow:
2186 case Intrinsic::ssub_with_overflow:
2188 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2189 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2190 break;
2191 case Intrinsic::umul_with_overflow:
2192 case Intrinsic::smul_with_overflow:
2193 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2194 false, DemandedElts, Known, Known2, Q, Depth);
2195 break;
2196 }
2197 }
2198 }
2199 break;
2200 case Instruction::Freeze:
2201 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2202 Depth + 1))
2203 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2204 break;
2205 }
2206}
2207
2208/// Determine which bits of V are known to be either zero or one and return
2209/// them.
2210KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2211 const SimplifyQuery &Q, unsigned Depth) {
2212 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2213 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2214 return Known;
2215}
2216
2217/// Determine which bits of V are known to be either zero or one and return
2218/// them.
2220 unsigned Depth) {
2221 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2222 computeKnownBits(V, Known, Q, Depth);
2223 return Known;
2224}
2225
2226/// Determine which bits of V are known to be either zero or one and return
2227/// them in the Known bit set.
2228///
2229/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2230/// we cannot optimize based on the assumption that it is zero without changing
2231/// it to be an explicit zero. If we don't change it to zero, other code could
2232/// optimized based on the contradictory assumption that it is non-zero.
2233/// Because instcombine aggressively folds operations with undef args anyway,
2234/// this won't lose us code quality.
2235///
2236/// This function is defined on values with integer type, values with pointer
2237/// type, and vectors of integers. In the case
2238/// where V is a vector, known zero, and known one values are the
2239/// same width as the vector element, and the bit is set only if it is true
2240/// for all of the demanded elements in the vector specified by DemandedElts.
2241void computeKnownBits(const Value *V, const APInt &DemandedElts,
2242 KnownBits &Known, const SimplifyQuery &Q,
2243 unsigned Depth) {
2244 if (!DemandedElts) {
2245 // No demanded elts, better to assume we don't know anything.
2246 Known.resetAll();
2247 return;
2248 }
2249
2250 assert(V && "No Value?");
2251 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2252
2253#ifndef NDEBUG
2254 Type *Ty = V->getType();
2255 unsigned BitWidth = Known.getBitWidth();
2256
2257 assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2258 "Not integer or pointer type!");
2259
2260 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2261 assert(
2262 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2263 "DemandedElt width should equal the fixed vector number of elements");
2264 } else {
2265 assert(DemandedElts == APInt(1, 1) &&
2266 "DemandedElt width should be 1 for scalars or scalable vectors");
2267 }
2268
2269 Type *ScalarTy = Ty->getScalarType();
2270 if (ScalarTy->isPointerTy()) {
2271 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2272 "V and Known should have same BitWidth");
2273 } else {
2274 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2275 "V and Known should have same BitWidth");
2276 }
2277#endif
2278
2279 const APInt *C;
2280 if (match(V, m_APInt(C))) {
2281 // We know all of the bits for a scalar constant or a splat vector constant!
2282 Known = KnownBits::makeConstant(*C);
2283 return;
2284 }
2285 // Null and aggregate-zero are all-zeros.
2287 Known.setAllZero();
2288 return;
2289 }
2290 // Handle a constant vector by taking the intersection of the known bits of
2291 // each element.
2293 assert(!isa<ScalableVectorType>(V->getType()));
2294 // We know that CDV must be a vector of integers. Take the intersection of
2295 // each element.
2296 Known.setAllConflict();
2297 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2298 if (!DemandedElts[i])
2299 continue;
2300 APInt Elt = CDV->getElementAsAPInt(i);
2301 Known.Zero &= ~Elt;
2302 Known.One &= Elt;
2303 }
2304 if (Known.hasConflict())
2305 Known.resetAll();
2306 return;
2307 }
2308
2309 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2310 assert(!isa<ScalableVectorType>(V->getType()));
2311 // We know that CV must be a vector of integers. Take the intersection of
2312 // each element.
2313 Known.setAllConflict();
2314 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2315 if (!DemandedElts[i])
2316 continue;
2317 Constant *Element = CV->getAggregateElement(i);
2318 if (isa<PoisonValue>(Element))
2319 continue;
2320 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2321 if (!ElementCI) {
2322 Known.resetAll();
2323 return;
2324 }
2325 const APInt &Elt = ElementCI->getValue();
2326 Known.Zero &= ~Elt;
2327 Known.One &= Elt;
2328 }
2329 if (Known.hasConflict())
2330 Known.resetAll();
2331 return;
2332 }
2333
2334 // Start out not knowing anything.
2335 Known.resetAll();
2336
2337 // We can't imply anything about undefs.
2338 if (isa<UndefValue>(V))
2339 return;
2340
2341 // There's no point in looking through other users of ConstantData for
2342 // assumptions. Confirm that we've handled them all.
2343 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2344
2345 if (const auto *A = dyn_cast<Argument>(V))
2346 if (std::optional<ConstantRange> Range = A->getRange())
2347 Known = Range->toKnownBits();
2348
2349 // All recursive calls that increase depth must come after this.
2351 return;
2352
2353 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2354 // the bits of its aliasee.
2355 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2356 if (!GA->isInterposable())
2357 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2358 return;
2359 }
2360
2361 if (const Operator *I = dyn_cast<Operator>(V))
2362 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2363 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2364 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2365 Known = CR->toKnownBits();
2366 }
2367
2368 // Aligned pointers have trailing zeros - refine Known.Zero set
2369 if (isa<PointerType>(V->getType())) {
2370 Align Alignment = V->getPointerAlignment(Q.DL);
2371 Known.Zero.setLowBits(Log2(Alignment));
2372 }
2373
2374 // computeKnownBitsFromContext strictly refines Known.
2375 // Therefore, we run them after computeKnownBitsFromOperator.
2376
2377 // Check whether we can determine known bits from context such as assumes.
2378 computeKnownBitsFromContext(V, Known, Q, Depth);
2379}
2380
2381/// Try to detect a recurrence that the value of the induction variable is
2382/// always a power of two (or zero).
2383static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2384 SimplifyQuery &Q, unsigned Depth) {
2385 BinaryOperator *BO = nullptr;
2386 Value *Start = nullptr, *Step = nullptr;
2387 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2388 return false;
2389
2390 // Initial value must be a power of two.
2391 for (const Use &U : PN->operands()) {
2392 if (U.get() == Start) {
2393 // Initial value comes from a different BB, need to adjust context
2394 // instruction for analysis.
2395 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2396 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2397 return false;
2398 }
2399 }
2400
2401 // Except for Mul, the induction variable must be on the left side of the
2402 // increment expression, otherwise its value can be arbitrary.
2403 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2404 return false;
2405
2406 Q.CxtI = BO->getParent()->getTerminator();
2407 switch (BO->getOpcode()) {
2408 case Instruction::Mul:
2409 // Power of two is closed under multiplication.
2410 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2411 Q.IIQ.hasNoSignedWrap(BO)) &&
2412 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2413 case Instruction::SDiv:
2414 // Start value must not be signmask for signed division, so simply being a
2415 // power of two is not sufficient, and it has to be a constant.
2416 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2417 return false;
2418 [[fallthrough]];
2419 case Instruction::UDiv:
2420 // Divisor must be a power of two.
2421 // If OrZero is false, cannot guarantee induction variable is non-zero after
2422 // division, same for Shr, unless it is exact division.
2423 return (OrZero || Q.IIQ.isExact(BO)) &&
2424 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2425 case Instruction::Shl:
2426 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2427 case Instruction::AShr:
2428 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2429 return false;
2430 [[fallthrough]];
2431 case Instruction::LShr:
2432 return OrZero || Q.IIQ.isExact(BO);
2433 default:
2434 return false;
2435 }
2436}
2437
2438/// Return true if we can infer that \p V is known to be a power of 2 from
2439/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2440static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2441 const Value *Cond,
2442 bool CondIsTrue) {
2443 CmpPredicate Pred;
2444 const APInt *RHSC;
2446 m_APInt(RHSC))))
2447 return false;
2448 if (!CondIsTrue)
2449 Pred = ICmpInst::getInversePredicate(Pred);
2450 // ctpop(V) u< 2
2451 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2452 return true;
2453 // ctpop(V) == 1
2454 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2455}
2456
2457/// Return true if the given value is known to have exactly one
2458/// bit set when defined. For vectors return true if every element is known to
2459/// be a power of two when defined. Supports values with integer or pointer
2460/// types and vectors of integers.
2461bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2462 const SimplifyQuery &Q, unsigned Depth) {
2463 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2464
2465 if (isa<Constant>(V))
2466 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2467
2468 // i1 is by definition a power of 2 or zero.
2469 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2470 return true;
2471
2472 // Try to infer from assumptions.
2473 if (Q.AC && Q.CxtI) {
2474 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2475 if (!AssumeVH)
2476 continue;
2477 CallInst *I = cast<CallInst>(AssumeVH);
2478 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2479 /*CondIsTrue=*/true) &&
2481 return true;
2482 }
2483 }
2484
2485 // Handle dominating conditions.
2486 if (Q.DC && Q.CxtI && Q.DT) {
2487 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2488 Value *Cond = BI->getCondition();
2489
2490 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2492 /*CondIsTrue=*/true) &&
2493 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2494 return true;
2495
2496 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2498 /*CondIsTrue=*/false) &&
2499 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2500 return true;
2501 }
2502 }
2503
2504 auto *I = dyn_cast<Instruction>(V);
2505 if (!I)
2506 return false;
2507
2508 if (Q.CxtI && match(V, m_VScale())) {
2509 const Function *F = Q.CxtI->getFunction();
2510 // The vscale_range indicates vscale is a power-of-two.
2511 return F->hasFnAttribute(Attribute::VScaleRange);
2512 }
2513
2514 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2515 // it is shifted off the end then the result is undefined.
2516 if (match(I, m_Shl(m_One(), m_Value())))
2517 return true;
2518
2519 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2520 // the bottom. If it is shifted off the bottom then the result is undefined.
2521 if (match(I, m_LShr(m_SignMask(), m_Value())))
2522 return true;
2523
2524 // The remaining tests are all recursive, so bail out if we hit the limit.
2526 return false;
2527
2528 switch (I->getOpcode()) {
2529 case Instruction::ZExt:
2530 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2531 case Instruction::Trunc:
2532 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2533 case Instruction::Shl:
2534 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2535 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2536 return false;
2537 case Instruction::LShr:
2538 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2539 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2540 return false;
2541 case Instruction::UDiv:
2543 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2544 return false;
2545 case Instruction::Mul:
2546 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2547 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2548 (OrZero || isKnownNonZero(I, Q, Depth));
2549 case Instruction::And:
2550 // A power of two and'd with anything is a power of two or zero.
2551 if (OrZero &&
2552 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2553 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2554 return true;
2555 // X & (-X) is always a power of two or zero.
2556 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2557 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2558 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2559 return false;
2560 case Instruction::Add: {
2561 // Adding a power-of-two or zero to the same power-of-two or zero yields
2562 // either the original power-of-two, a larger power-of-two or zero.
2564 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2565 Q.IIQ.hasNoSignedWrap(VOBO)) {
2566 if (match(I->getOperand(0),
2567 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2568 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2569 return true;
2570 if (match(I->getOperand(1),
2571 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2572 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2573 return true;
2574
2575 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2576 KnownBits LHSBits(BitWidth);
2577 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2578
2579 KnownBits RHSBits(BitWidth);
2580 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2581 // If i8 V is a power of two or zero:
2582 // ZeroBits: 1 1 1 0 1 1 1 1
2583 // ~ZeroBits: 0 0 0 1 0 0 0 0
2584 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2585 // If OrZero isn't set, we cannot give back a zero result.
2586 // Make sure either the LHS or RHS has a bit set.
2587 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2588 return true;
2589 }
2590
2591 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2592 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2593 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2594 return true;
2595 return false;
2596 }
2597 case Instruction::Select:
2598 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2599 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2600 case Instruction::PHI: {
2601 // A PHI node is power of two if all incoming values are power of two, or if
2602 // it is an induction variable where in each step its value is a power of
2603 // two.
2604 auto *PN = cast<PHINode>(I);
2606
2607 // Check if it is an induction variable and always power of two.
2608 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2609 return true;
2610
2611 // Recursively check all incoming values. Limit recursion to 2 levels, so
2612 // that search complexity is limited to number of operands^2.
2613 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2614 return llvm::all_of(PN->operands(), [&](const Use &U) {
2615 // Value is power of 2 if it is coming from PHI node itself by induction.
2616 if (U.get() == PN)
2617 return true;
2618
2619 // Change the context instruction to the incoming block where it is
2620 // evaluated.
2621 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2622 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2623 });
2624 }
2625 case Instruction::Invoke:
2626 case Instruction::Call: {
2627 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2628 switch (II->getIntrinsicID()) {
2629 case Intrinsic::umax:
2630 case Intrinsic::smax:
2631 case Intrinsic::umin:
2632 case Intrinsic::smin:
2633 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2634 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2635 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2636 // thus dont change pow2/non-pow2 status.
2637 case Intrinsic::bitreverse:
2638 case Intrinsic::bswap:
2639 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2640 case Intrinsic::fshr:
2641 case Intrinsic::fshl:
2642 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2643 if (II->getArgOperand(0) == II->getArgOperand(1))
2644 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2645 break;
2646 default:
2647 break;
2648 }
2649 }
2650 return false;
2651 }
2652 default:
2653 return false;
2654 }
2655}
2656
2657/// Test whether a GEP's result is known to be non-null.
2658///
2659/// Uses properties inherent in a GEP to try to determine whether it is known
2660/// to be non-null.
2661///
2662/// Currently this routine does not support vector GEPs.
2663static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2664 unsigned Depth) {
2665 const Function *F = nullptr;
2666 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2667 F = I->getFunction();
2668
2669 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2670 // may be null iff the base pointer is null and the offset is zero.
2671 if (!GEP->hasNoUnsignedWrap() &&
2672 !(GEP->isInBounds() &&
2673 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2674 return false;
2675
2676 // FIXME: Support vector-GEPs.
2677 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2678
2679 // If the base pointer is non-null, we cannot walk to a null address with an
2680 // inbounds GEP in address space zero.
2681 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2682 return true;
2683
2684 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2685 // If so, then the GEP cannot produce a null pointer, as doing so would
2686 // inherently violate the inbounds contract within address space zero.
2688 GTI != GTE; ++GTI) {
2689 // Struct types are easy -- they must always be indexed by a constant.
2690 if (StructType *STy = GTI.getStructTypeOrNull()) {
2691 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2692 unsigned ElementIdx = OpC->getZExtValue();
2693 const StructLayout *SL = Q.DL.getStructLayout(STy);
2694 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2695 if (ElementOffset > 0)
2696 return true;
2697 continue;
2698 }
2699
2700 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2701 if (GTI.getSequentialElementStride(Q.DL).isZero())
2702 continue;
2703
2704 // Fast path the constant operand case both for efficiency and so we don't
2705 // increment Depth when just zipping down an all-constant GEP.
2706 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2707 if (!OpC->isZero())
2708 return true;
2709 continue;
2710 }
2711
2712 // We post-increment Depth here because while isKnownNonZero increments it
2713 // as well, when we pop back up that increment won't persist. We don't want
2714 // to recurse 10k times just because we have 10k GEP operands. We don't
2715 // bail completely out because we want to handle constant GEPs regardless
2716 // of depth.
2718 continue;
2719
2720 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2721 return true;
2722 }
2723
2724 return false;
2725}
2726
2728 const Instruction *CtxI,
2729 const DominatorTree *DT) {
2730 assert(!isa<Constant>(V) && "Called for constant?");
2731
2732 if (!CtxI || !DT)
2733 return false;
2734
2735 unsigned NumUsesExplored = 0;
2736 for (auto &U : V->uses()) {
2737 // Avoid massive lists
2738 if (NumUsesExplored >= DomConditionsMaxUses)
2739 break;
2740 NumUsesExplored++;
2741
2742 const Instruction *UI = cast<Instruction>(U.getUser());
2743 // If the value is used as an argument to a call or invoke, then argument
2744 // attributes may provide an answer about null-ness.
2745 if (V->getType()->isPointerTy()) {
2746 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2747 if (CB->isArgOperand(&U) &&
2748 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2749 /*AllowUndefOrPoison=*/false) &&
2750 DT->dominates(CB, CtxI))
2751 return true;
2752 }
2753 }
2754
2755 // If the value is used as a load/store, then the pointer must be non null.
2756 if (V == getLoadStorePointerOperand(UI)) {
2759 DT->dominates(UI, CtxI))
2760 return true;
2761 }
2762
2763 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2764 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2765 isValidAssumeForContext(UI, CtxI, DT))
2766 return true;
2767
2768 // Consider only compare instructions uniquely controlling a branch
2769 Value *RHS;
2770 CmpPredicate Pred;
2771 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2772 continue;
2773
2774 bool NonNullIfTrue;
2775 if (cmpExcludesZero(Pred, RHS))
2776 NonNullIfTrue = true;
2778 NonNullIfTrue = false;
2779 else
2780 continue;
2781
2784 for (const auto *CmpU : UI->users()) {
2785 assert(WorkList.empty() && "Should be!");
2786 if (Visited.insert(CmpU).second)
2787 WorkList.push_back(CmpU);
2788
2789 while (!WorkList.empty()) {
2790 auto *Curr = WorkList.pop_back_val();
2791
2792 // If a user is an AND, add all its users to the work list. We only
2793 // propagate "pred != null" condition through AND because it is only
2794 // correct to assume that all conditions of AND are met in true branch.
2795 // TODO: Support similar logic of OR and EQ predicate?
2796 if (NonNullIfTrue)
2797 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2798 for (const auto *CurrU : Curr->users())
2799 if (Visited.insert(CurrU).second)
2800 WorkList.push_back(CurrU);
2801 continue;
2802 }
2803
2804 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2805 assert(BI->isConditional() && "uses a comparison!");
2806
2807 BasicBlock *NonNullSuccessor =
2808 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2809 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2810 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2811 return true;
2812 } else if (NonNullIfTrue && isGuard(Curr) &&
2813 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2814 return true;
2815 }
2816 }
2817 }
2818 }
2819
2820 return false;
2821}
2822
2823/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2824/// ensure that the value it's attached to is never Value? 'RangeType' is
2825/// is the type of the value described by the range.
2826static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2827 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2828 assert(NumRanges >= 1);
2829 for (unsigned i = 0; i < NumRanges; ++i) {
2831 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2833 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2834 ConstantRange Range(Lower->getValue(), Upper->getValue());
2835 if (Range.contains(Value))
2836 return false;
2837 }
2838 return true;
2839}
2840
2841/// Try to detect a recurrence that monotonically increases/decreases from a
2842/// non-zero starting value. These are common as induction variables.
2843static bool isNonZeroRecurrence(const PHINode *PN) {
2844 BinaryOperator *BO = nullptr;
2845 Value *Start = nullptr, *Step = nullptr;
2846 const APInt *StartC, *StepC;
2847 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2848 !match(Start, m_APInt(StartC)) || StartC->isZero())
2849 return false;
2850
2851 switch (BO->getOpcode()) {
2852 case Instruction::Add:
2853 // Starting from non-zero and stepping away from zero can never wrap back
2854 // to zero.
2855 return BO->hasNoUnsignedWrap() ||
2856 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
2857 StartC->isNegative() == StepC->isNegative());
2858 case Instruction::Mul:
2859 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2860 match(Step, m_APInt(StepC)) && !StepC->isZero();
2861 case Instruction::Shl:
2862 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
2863 case Instruction::AShr:
2864 case Instruction::LShr:
2865 return BO->isExact();
2866 default:
2867 return false;
2868 }
2869}
2870
2871static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
2873 m_Specific(Op1), m_Zero()))) ||
2875 m_Specific(Op0), m_Zero())));
2876}
2877
2878static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
2879 unsigned BitWidth, Value *X, Value *Y, bool NSW,
2880 bool NUW, unsigned Depth) {
2881 // (X + (X != 0)) is non zero
2882 if (matchOpWithOpEqZero(X, Y))
2883 return true;
2884
2885 if (NUW)
2886 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2887 isKnownNonZero(X, DemandedElts, Q, Depth);
2888
2889 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
2890 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
2891
2892 // If X and Y are both non-negative (as signed values) then their sum is not
2893 // zero unless both X and Y are zero.
2894 if (XKnown.isNonNegative() && YKnown.isNonNegative())
2895 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2896 isKnownNonZero(X, DemandedElts, Q, Depth))
2897 return true;
2898
2899 // If X and Y are both negative (as signed values) then their sum is not
2900 // zero unless both X and Y equal INT_MIN.
2901 if (XKnown.isNegative() && YKnown.isNegative()) {
2903 // The sign bit of X is set. If some other bit is set then X is not equal
2904 // to INT_MIN.
2905 if (XKnown.One.intersects(Mask))
2906 return true;
2907 // The sign bit of Y is set. If some other bit is set then Y is not equal
2908 // to INT_MIN.
2909 if (YKnown.One.intersects(Mask))
2910 return true;
2911 }
2912
2913 // The sum of a non-negative number and a power of two is not zero.
2914 if (XKnown.isNonNegative() &&
2915 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
2916 return true;
2917 if (YKnown.isNonNegative() &&
2918 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
2919 return true;
2920
2921 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
2922}
2923
2924static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
2925 unsigned BitWidth, Value *X, Value *Y,
2926 unsigned Depth) {
2927 // (X - (X != 0)) is non zero
2928 // ((X != 0) - X) is non zero
2929 if (matchOpWithOpEqZero(X, Y))
2930 return true;
2931
2932 // TODO: Move this case into isKnownNonEqual().
2933 if (auto *C = dyn_cast<Constant>(X))
2934 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
2935 return true;
2936
2937 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
2938}
2939
2940static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
2941 unsigned BitWidth, Value *X, Value *Y, bool NSW,
2942 bool NUW, unsigned Depth) {
2943 // If X and Y are non-zero then so is X * Y as long as the multiplication
2944 // does not overflow.
2945 if (NSW || NUW)
2946 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
2947 isKnownNonZero(Y, DemandedElts, Q, Depth);
2948
2949 // If either X or Y is odd, then if the other is non-zero the result can't
2950 // be zero.
2951 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
2952 if (XKnown.One[0])
2953 return isKnownNonZero(Y, DemandedElts, Q, Depth);
2954
2955 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
2956 if (YKnown.One[0])
2957 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
2958
2959 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
2960 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
2961 // the lowest known One of X and Y. If they are non-zero, the result
2962 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
2963 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
2964 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
2965 BitWidth;
2966}
2967
2968static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
2969 const SimplifyQuery &Q, const KnownBits &KnownVal,
2970 unsigned Depth) {
2971 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2972 switch (I->getOpcode()) {
2973 case Instruction::Shl:
2974 return Lhs.shl(Rhs);
2975 case Instruction::LShr:
2976 return Lhs.lshr(Rhs);
2977 case Instruction::AShr:
2978 return Lhs.ashr(Rhs);
2979 default:
2980 llvm_unreachable("Unknown Shift Opcode");
2981 }
2982 };
2983
2984 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2985 switch (I->getOpcode()) {
2986 case Instruction::Shl:
2987 return Lhs.lshr(Rhs);
2988 case Instruction::LShr:
2989 case Instruction::AShr:
2990 return Lhs.shl(Rhs);
2991 default:
2992 llvm_unreachable("Unknown Shift Opcode");
2993 }
2994 };
2995
2996 if (KnownVal.isUnknown())
2997 return false;
2998
2999 KnownBits KnownCnt =
3000 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3001 APInt MaxShift = KnownCnt.getMaxValue();
3002 unsigned NumBits = KnownVal.getBitWidth();
3003 if (MaxShift.uge(NumBits))
3004 return false;
3005
3006 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
3007 return true;
3008
3009 // If all of the bits shifted out are known to be zero, and Val is known
3010 // non-zero then at least one non-zero bit must remain.
3011 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
3012 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
3013 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
3014 return true;
3015
3016 return false;
3017}
3018
3020 const APInt &DemandedElts,
3021 const SimplifyQuery &Q, unsigned Depth) {
3022 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
3023 switch (I->getOpcode()) {
3024 case Instruction::Alloca:
3025 // Alloca never returns null, malloc might.
3026 return I->getType()->getPointerAddressSpace() == 0;
3027 case Instruction::GetElementPtr:
3028 if (I->getType()->isPointerTy())
3030 break;
3031 case Instruction::BitCast: {
3032 // We need to be a bit careful here. We can only peek through the bitcast
3033 // if the scalar size of elements in the operand are smaller than and a
3034 // multiple of the size they are casting too. Take three cases:
3035 //
3036 // 1) Unsafe:
3037 // bitcast <2 x i16> %NonZero to <4 x i8>
3038 //
3039 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3040 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3041 // guranteed (imagine just sign bit set in the 2 i16 elements).
3042 //
3043 // 2) Unsafe:
3044 // bitcast <4 x i3> %NonZero to <3 x i4>
3045 //
3046 // Even though the scalar size of the src (`i3`) is smaller than the
3047 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3048 // its possible for the `3 x i4` elements to be zero because there are
3049 // some elements in the destination that don't contain any full src
3050 // element.
3051 //
3052 // 3) Safe:
3053 // bitcast <4 x i8> %NonZero to <2 x i16>
3054 //
3055 // This is always safe as non-zero in the 4 i8 elements implies
3056 // non-zero in the combination of any two adjacent ones. Since i8 is a
3057 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3058 // This all implies the 2 i16 elements are non-zero.
3059 Type *FromTy = I->getOperand(0)->getType();
3060 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3061 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3062 return isKnownNonZero(I->getOperand(0), Q, Depth);
3063 } break;
3064 case Instruction::IntToPtr:
3065 // Note that we have to take special care to avoid looking through
3066 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3067 // as casts that can alter the value, e.g., AddrSpaceCasts.
3068 if (!isa<ScalableVectorType>(I->getType()) &&
3069 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3070 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3071 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3072 break;
3073 case Instruction::PtrToInt:
3074 // Similar to int2ptr above, we can look through ptr2int here if the cast
3075 // is a no-op or an extend and not a truncate.
3076 if (!isa<ScalableVectorType>(I->getType()) &&
3077 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3078 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3079 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3080 break;
3081 case Instruction::Trunc:
3082 // nuw/nsw trunc preserves zero/non-zero status of input.
3083 if (auto *TI = dyn_cast<TruncInst>(I))
3084 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3085 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3086 break;
3087
3088 // Iff x - y != 0, then x ^ y != 0
3089 // Therefore we can do the same exact checks
3090 case Instruction::Xor:
3091 case Instruction::Sub:
3092 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3093 I->getOperand(1), Depth);
3094 case Instruction::Or:
3095 // (X | (X != 0)) is non zero
3096 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3097 return true;
3098 // X | Y != 0 if X != Y.
3099 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3100 Depth))
3101 return true;
3102 // X | Y != 0 if X != 0 or Y != 0.
3103 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3104 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3105 case Instruction::SExt:
3106 case Instruction::ZExt:
3107 // ext X != 0 if X != 0.
3108 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3109
3110 case Instruction::Shl: {
3111 // shl nsw/nuw can't remove any non-zero bits.
3113 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3114 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3115
3116 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3117 // if the lowest bit is shifted off the end.
3118 KnownBits Known(BitWidth);
3119 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3120 if (Known.One[0])
3121 return true;
3122
3123 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3124 }
3125 case Instruction::LShr:
3126 case Instruction::AShr: {
3127 // shr exact can only shift out zero bits.
3129 if (BO->isExact())
3130 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3131
3132 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3133 // defined if the sign bit is shifted off the end.
3134 KnownBits Known =
3135 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3136 if (Known.isNegative())
3137 return true;
3138
3139 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3140 }
3141 case Instruction::UDiv:
3142 case Instruction::SDiv: {
3143 // X / Y
3144 // div exact can only produce a zero if the dividend is zero.
3145 if (cast<PossiblyExactOperator>(I)->isExact())
3146 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3147
3148 KnownBits XKnown =
3149 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3150 // If X is fully unknown we won't be able to figure anything out so don't
3151 // both computing knownbits for Y.
3152 if (XKnown.isUnknown())
3153 return false;
3154
3155 KnownBits YKnown =
3156 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3157 if (I->getOpcode() == Instruction::SDiv) {
3158 // For signed division need to compare abs value of the operands.
3159 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3160 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3161 }
3162 // If X u>= Y then div is non zero (0/0 is UB).
3163 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3164 // If X is total unknown or X u< Y we won't be able to prove non-zero
3165 // with compute known bits so just return early.
3166 return XUgeY && *XUgeY;
3167 }
3168 case Instruction::Add: {
3169 // X + Y.
3170
3171 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3172 // non-zero.
3174 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3175 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3176 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3177 }
3178 case Instruction::Mul: {
3180 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3181 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3182 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3183 }
3184 case Instruction::Select: {
3185 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3186
3187 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3188 // then see if the select condition implies the arm is non-zero. For example
3189 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3190 // dominated by `X != 0`.
3191 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3192 Value *Op;
3193 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3194 // Op is trivially non-zero.
3195 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3196 return true;
3197
3198 // The condition of the select dominates the true/false arm. Check if the
3199 // condition implies that a given arm is non-zero.
3200 Value *X;
3201 CmpPredicate Pred;
3202 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3203 return false;
3204
3205 if (!IsTrueArm)
3206 Pred = ICmpInst::getInversePredicate(Pred);
3207
3208 return cmpExcludesZero(Pred, X);
3209 };
3210
3211 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3212 SelectArmIsNonZero(/* IsTrueArm */ false))
3213 return true;
3214 break;
3215 }
3216 case Instruction::PHI: {
3217 auto *PN = cast<PHINode>(I);
3219 return true;
3220
3221 // Check if all incoming values are non-zero using recursion.
3223 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3224 return llvm::all_of(PN->operands(), [&](const Use &U) {
3225 if (U.get() == PN)
3226 return true;
3227 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3228 // Check if the branch on the phi excludes zero.
3229 CmpPredicate Pred;
3230 Value *X;
3231 BasicBlock *TrueSucc, *FalseSucc;
3232 if (match(RecQ.CxtI,
3233 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3234 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3235 // Check for cases of duplicate successors.
3236 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3237 // If we're using the false successor, invert the predicate.
3238 if (FalseSucc == PN->getParent())
3239 Pred = CmpInst::getInversePredicate(Pred);
3240 if (cmpExcludesZero(Pred, X))
3241 return true;
3242 }
3243 }
3244 // Finally recurse on the edge and check it directly.
3245 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3246 });
3247 }
3248 case Instruction::InsertElement: {
3249 if (isa<ScalableVectorType>(I->getType()))
3250 break;
3251
3252 const Value *Vec = I->getOperand(0);
3253 const Value *Elt = I->getOperand(1);
3254 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3255
3256 unsigned NumElts = DemandedElts.getBitWidth();
3257 APInt DemandedVecElts = DemandedElts;
3258 bool SkipElt = false;
3259 // If we know the index we are inserting too, clear it from Vec check.
3260 if (CIdx && CIdx->getValue().ult(NumElts)) {
3261 DemandedVecElts.clearBit(CIdx->getZExtValue());
3262 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3263 }
3264
3265 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3266 // are non-zero.
3267 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3268 (DemandedVecElts.isZero() ||
3269 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3270 }
3271 case Instruction::ExtractElement:
3272 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3273 const Value *Vec = EEI->getVectorOperand();
3274 const Value *Idx = EEI->getIndexOperand();
3275 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3276 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3277 unsigned NumElts = VecTy->getNumElements();
3278 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3279 if (CIdx && CIdx->getValue().ult(NumElts))
3280 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3281 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3282 }
3283 }
3284 break;
3285 case Instruction::ShuffleVector: {
3286 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3287 if (!Shuf)
3288 break;
3289 APInt DemandedLHS, DemandedRHS;
3290 // For undef elements, we don't know anything about the common state of
3291 // the shuffle result.
3292 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3293 break;
3294 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3295 return (DemandedRHS.isZero() ||
3296 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3297 (DemandedLHS.isZero() ||
3298 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3299 }
3300 case Instruction::Freeze:
3301 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3302 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3303 Depth);
3304 case Instruction::Load: {
3305 auto *LI = cast<LoadInst>(I);
3306 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3307 // is never null.
3308 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3309 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3310 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3311 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3312 return true;
3313 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3315 }
3316
3317 // No need to fall through to computeKnownBits as range metadata is already
3318 // handled in isKnownNonZero.
3319 return false;
3320 }
3321 case Instruction::ExtractValue: {
3322 const WithOverflowInst *WO;
3324 switch (WO->getBinaryOp()) {
3325 default:
3326 break;
3327 case Instruction::Add:
3328 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3329 WO->getArgOperand(1),
3330 /*NSW=*/false,
3331 /*NUW=*/false, Depth);
3332 case Instruction::Sub:
3333 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3334 WO->getArgOperand(1), Depth);
3335 case Instruction::Mul:
3336 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3337 WO->getArgOperand(1),
3338 /*NSW=*/false, /*NUW=*/false, Depth);
3339 break;
3340 }
3341 }
3342 break;
3343 }
3344 case Instruction::Call:
3345 case Instruction::Invoke: {
3346 const auto *Call = cast<CallBase>(I);
3347 if (I->getType()->isPointerTy()) {
3348 if (Call->isReturnNonNull())
3349 return true;
3350 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3351 return isKnownNonZero(RP, Q, Depth);
3352 } else {
3353 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3355 if (std::optional<ConstantRange> Range = Call->getRange()) {
3356 const APInt ZeroValue(Range->getBitWidth(), 0);
3357 if (!Range->contains(ZeroValue))
3358 return true;
3359 }
3360 if (const Value *RV = Call->getReturnedArgOperand())
3361 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3362 return true;
3363 }
3364
3365 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3366 switch (II->getIntrinsicID()) {
3367 case Intrinsic::sshl_sat:
3368 case Intrinsic::ushl_sat:
3369 case Intrinsic::abs:
3370 case Intrinsic::bitreverse:
3371 case Intrinsic::bswap:
3372 case Intrinsic::ctpop:
3373 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3374 // NB: We don't do usub_sat here as in any case we can prove its
3375 // non-zero, we will fold it to `sub nuw` in InstCombine.
3376 case Intrinsic::ssub_sat:
3377 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3378 II->getArgOperand(1), Depth);
3379 case Intrinsic::sadd_sat:
3380 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3381 II->getArgOperand(1),
3382 /*NSW=*/true, /* NUW=*/false, Depth);
3383 // Vec reverse preserves zero/non-zero status from input vec.
3384 case Intrinsic::vector_reverse:
3385 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3386 Q, Depth);
3387 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3388 case Intrinsic::vector_reduce_or:
3389 case Intrinsic::vector_reduce_umax:
3390 case Intrinsic::vector_reduce_umin:
3391 case Intrinsic::vector_reduce_smax:
3392 case Intrinsic::vector_reduce_smin:
3393 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3394 case Intrinsic::umax:
3395 case Intrinsic::uadd_sat:
3396 // umax(X, (X != 0)) is non zero
3397 // X +usat (X != 0) is non zero
3398 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3399 return true;
3400
3401 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3402 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3403 case Intrinsic::smax: {
3404 // If either arg is strictly positive the result is non-zero. Otherwise
3405 // the result is non-zero if both ops are non-zero.
3406 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3407 const KnownBits &OpKnown) {
3408 if (!OpNonZero.has_value())
3409 OpNonZero = OpKnown.isNonZero() ||
3410 isKnownNonZero(Op, DemandedElts, Q, Depth);
3411 return *OpNonZero;
3412 };
3413 // Avoid re-computing isKnownNonZero.
3414 std::optional<bool> Op0NonZero, Op1NonZero;
3415 KnownBits Op1Known =
3416 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3417 if (Op1Known.isNonNegative() &&
3418 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3419 return true;
3420 KnownBits Op0Known =
3421 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3422 if (Op0Known.isNonNegative() &&
3423 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3424 return true;
3425 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3426 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3427 }
3428 case Intrinsic::smin: {
3429 // If either arg is negative the result is non-zero. Otherwise
3430 // the result is non-zero if both ops are non-zero.
3431 KnownBits Op1Known =
3432 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3433 if (Op1Known.isNegative())
3434 return true;
3435 KnownBits Op0Known =
3436 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3437 if (Op0Known.isNegative())
3438 return true;
3439
3440 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3441 return true;
3442 }
3443 [[fallthrough]];
3444 case Intrinsic::umin:
3445 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3446 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3447 case Intrinsic::cttz:
3448 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3449 .Zero[0];
3450 case Intrinsic::ctlz:
3451 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3452 .isNonNegative();
3453 case Intrinsic::fshr:
3454 case Intrinsic::fshl:
3455 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3456 if (II->getArgOperand(0) == II->getArgOperand(1))
3457 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3458 break;
3459 case Intrinsic::vscale:
3460 return true;
3461 case Intrinsic::experimental_get_vector_length:
3462 return isKnownNonZero(I->getOperand(0), Q, Depth);
3463 default:
3464 break;
3465 }
3466 break;
3467 }
3468
3469 return false;
3470 }
3471 }
3472
3473 KnownBits Known(BitWidth);
3474 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3475 return Known.One != 0;
3476}
3477
3478/// Return true if the given value is known to be non-zero when defined. For
3479/// vectors, return true if every demanded element is known to be non-zero when
3480/// defined. For pointers, if the context instruction and dominator tree are
3481/// specified, perform context-sensitive analysis and return true if the
3482/// pointer couldn't possibly be null at the specified instruction.
3483/// Supports values with integer or pointer type and vectors of integers.
3484bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3485 const SimplifyQuery &Q, unsigned Depth) {
3486 Type *Ty = V->getType();
3487
3488#ifndef NDEBUG
3489 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3490
3491 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3492 assert(
3493 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3494 "DemandedElt width should equal the fixed vector number of elements");
3495 } else {
3496 assert(DemandedElts == APInt(1, 1) &&
3497 "DemandedElt width should be 1 for scalars");
3498 }
3499#endif
3500
3501 if (auto *C = dyn_cast<Constant>(V)) {
3502 if (C->isNullValue())
3503 return false;
3504 if (isa<ConstantInt>(C))
3505 // Must be non-zero due to null test above.
3506 return true;
3507
3508 // For constant vectors, check that all elements are poison or known
3509 // non-zero to determine that the whole vector is known non-zero.
3510 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3511 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3512 if (!DemandedElts[i])
3513 continue;
3514 Constant *Elt = C->getAggregateElement(i);
3515 if (!Elt || Elt->isNullValue())
3516 return false;
3517 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3518 return false;
3519 }
3520 return true;
3521 }
3522
3523 // Constant ptrauth can be null, iff the base pointer can be.
3524 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3525 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3526
3527 // A global variable in address space 0 is non null unless extern weak
3528 // or an absolute symbol reference. Other address spaces may have null as a
3529 // valid address for a global, so we can't assume anything.
3530 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3531 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3532 GV->getType()->getAddressSpace() == 0)
3533 return true;
3534 }
3535
3536 // For constant expressions, fall through to the Operator code below.
3537 if (!isa<ConstantExpr>(V))
3538 return false;
3539 }
3540
3541 if (const auto *A = dyn_cast<Argument>(V))
3542 if (std::optional<ConstantRange> Range = A->getRange()) {
3543 const APInt ZeroValue(Range->getBitWidth(), 0);
3544 if (!Range->contains(ZeroValue))
3545 return true;
3546 }
3547
3548 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3549 return true;
3550
3551 // Some of the tests below are recursive, so bail out if we hit the limit.
3553 return false;
3554
3555 // Check for pointer simplifications.
3556
3557 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3558 // A byval, inalloca may not be null in a non-default addres space. A
3559 // nonnull argument is assumed never 0.
3560 if (const Argument *A = dyn_cast<Argument>(V)) {
3561 if (((A->hasPassPointeeByValueCopyAttr() &&
3562 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3563 A->hasNonNullAttr()))
3564 return true;
3565 }
3566 }
3567
3568 if (const auto *I = dyn_cast<Operator>(V))
3569 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3570 return true;
3571
3572 if (!isa<Constant>(V) &&
3574 return true;
3575
3576 if (const Value *Stripped = stripNullTest(V))
3577 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3578
3579 return false;
3580}
3581
3583 unsigned Depth) {
3584 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3585 APInt DemandedElts =
3586 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3587 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3588}
3589
3590/// If the pair of operators are the same invertible function, return the
3591/// the operands of the function corresponding to each input. Otherwise,
3592/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3593/// every input value to exactly one output value. This is equivalent to
3594/// saying that Op1 and Op2 are equal exactly when the specified pair of
3595/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3596static std::optional<std::pair<Value*, Value*>>
3598 const Operator *Op2) {
3599 if (Op1->getOpcode() != Op2->getOpcode())
3600 return std::nullopt;
3601
3602 auto getOperands = [&](unsigned OpNum) -> auto {
3603 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3604 };
3605
3606 switch (Op1->getOpcode()) {
3607 default:
3608 break;
3609 case Instruction::Or:
3610 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3611 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3612 break;
3613 [[fallthrough]];
3614 case Instruction::Xor:
3615 case Instruction::Add: {
3616 Value *Other;
3617 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3618 return std::make_pair(Op1->getOperand(1), Other);
3619 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3620 return std::make_pair(Op1->getOperand(0), Other);
3621 break;
3622 }
3623 case Instruction::Sub:
3624 if (Op1->getOperand(0) == Op2->getOperand(0))
3625 return getOperands(1);
3626 if (Op1->getOperand(1) == Op2->getOperand(1))
3627 return getOperands(0);
3628 break;
3629 case Instruction::Mul: {
3630 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3631 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3632 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3633 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3634 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3635 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3636 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3637 break;
3638
3639 // Assume operand order has been canonicalized
3640 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3641 isa<ConstantInt>(Op1->getOperand(1)) &&
3642 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3643 return getOperands(0);
3644 break;
3645 }
3646 case Instruction::Shl: {
3647 // Same as multiplies, with the difference that we don't need to check
3648 // for a non-zero multiply. Shifts always multiply by non-zero.
3649 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3650 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3651 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3652 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3653 break;
3654
3655 if (Op1->getOperand(1) == Op2->getOperand(1))
3656 return getOperands(0);
3657 break;
3658 }
3659 case Instruction::AShr:
3660 case Instruction::LShr: {
3661 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3662 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3663 if (!PEO1->isExact() || !PEO2->isExact())
3664 break;
3665
3666 if (Op1->getOperand(1) == Op2->getOperand(1))
3667 return getOperands(0);
3668 break;
3669 }
3670 case Instruction::SExt:
3671 case Instruction::ZExt:
3672 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3673 return getOperands(0);
3674 break;
3675 case Instruction::PHI: {
3676 const PHINode *PN1 = cast<PHINode>(Op1);
3677 const PHINode *PN2 = cast<PHINode>(Op2);
3678
3679 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3680 // are a single invertible function of the start values? Note that repeated
3681 // application of an invertible function is also invertible
3682 BinaryOperator *BO1 = nullptr;
3683 Value *Start1 = nullptr, *Step1 = nullptr;
3684 BinaryOperator *BO2 = nullptr;
3685 Value *Start2 = nullptr, *Step2 = nullptr;
3686 if (PN1->getParent() != PN2->getParent() ||
3687 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3688 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3689 break;
3690
3691 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3692 cast<Operator>(BO2));
3693 if (!Values)
3694 break;
3695
3696 // We have to be careful of mutually defined recurrences here. Ex:
3697 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3698 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3699 // The invertibility of these is complicated, and not worth reasoning
3700 // about (yet?).
3701 if (Values->first != PN1 || Values->second != PN2)
3702 break;
3703
3704 return std::make_pair(Start1, Start2);
3705 }
3706 }
3707 return std::nullopt;
3708}
3709
3710/// Return true if V1 == (binop V2, X), where X is known non-zero.
3711/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3712/// implies V2 != V1.
3713static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3714 const APInt &DemandedElts,
3715 const SimplifyQuery &Q, unsigned Depth) {
3717 if (!BO)
3718 return false;
3719 switch (BO->getOpcode()) {
3720 default:
3721 break;
3722 case Instruction::Or:
3723 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3724 break;
3725 [[fallthrough]];
3726 case Instruction::Xor:
3727 case Instruction::Add:
3728 Value *Op = nullptr;
3729 if (V2 == BO->getOperand(0))
3730 Op = BO->getOperand(1);
3731 else if (V2 == BO->getOperand(1))
3732 Op = BO->getOperand(0);
3733 else
3734 return false;
3735 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3736 }
3737 return false;
3738}
3739
3740/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3741/// the multiplication is nuw or nsw.
3742static bool isNonEqualMul(const Value *V1, const Value *V2,
3743 const APInt &DemandedElts, const SimplifyQuery &Q,
3744 unsigned Depth) {
3745 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3746 const APInt *C;
3747 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3748 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3749 !C->isZero() && !C->isOne() &&
3750 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3751 }
3752 return false;
3753}
3754
3755/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3756/// the shift is nuw or nsw.
3757static bool isNonEqualShl(const Value *V1, const Value *V2,
3758 const APInt &DemandedElts, const SimplifyQuery &Q,
3759 unsigned Depth) {
3760 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3761 const APInt *C;
3762 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3763 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3764 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3765 }
3766 return false;
3767}
3768
3769static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3770 const APInt &DemandedElts, const SimplifyQuery &Q,
3771 unsigned Depth) {
3772 // Check two PHIs are in same block.
3773 if (PN1->getParent() != PN2->getParent())
3774 return false;
3775
3777 bool UsedFullRecursion = false;
3778 for (const BasicBlock *IncomBB : PN1->blocks()) {
3779 if (!VisitedBBs.insert(IncomBB).second)
3780 continue; // Don't reprocess blocks that we have dealt with already.
3781 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3782 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3783 const APInt *C1, *C2;
3784 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3785 continue;
3786
3787 // Only one pair of phi operands is allowed for full recursion.
3788 if (UsedFullRecursion)
3789 return false;
3790
3792 RecQ.CxtI = IncomBB->getTerminator();
3793 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3794 return false;
3795 UsedFullRecursion = true;
3796 }
3797 return true;
3798}
3799
3800static bool isNonEqualSelect(const Value *V1, const Value *V2,
3801 const APInt &DemandedElts, const SimplifyQuery &Q,
3802 unsigned Depth) {
3803 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3804 if (!SI1)
3805 return false;
3806
3807 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3808 const Value *Cond1 = SI1->getCondition();
3809 const Value *Cond2 = SI2->getCondition();
3810 if (Cond1 == Cond2)
3811 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3812 DemandedElts, Q, Depth + 1) &&
3813 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3814 DemandedElts, Q, Depth + 1);
3815 }
3816 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
3817 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
3818}
3819
3820// Check to see if A is both a GEP and is the incoming value for a PHI in the
3821// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3822// one of them being the recursive GEP A and the other a ptr at same base and at
3823// the same/higher offset than B we are only incrementing the pointer further in
3824// loop if offset of recursive GEP is greater than 0.
3826 const SimplifyQuery &Q) {
3827 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3828 return false;
3829
3830 auto *GEPA = dyn_cast<GEPOperator>(A);
3831 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3832 return false;
3833
3834 // Handle 2 incoming PHI values with one being a recursive GEP.
3835 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3836 if (!PN || PN->getNumIncomingValues() != 2)
3837 return false;
3838
3839 // Search for the recursive GEP as an incoming operand, and record that as
3840 // Step.
3841 Value *Start = nullptr;
3842 Value *Step = const_cast<Value *>(A);
3843 if (PN->getIncomingValue(0) == Step)
3844 Start = PN->getIncomingValue(1);
3845 else if (PN->getIncomingValue(1) == Step)
3846 Start = PN->getIncomingValue(0);
3847 else
3848 return false;
3849
3850 // Other incoming node base should match the B base.
3851 // StartOffset >= OffsetB && StepOffset > 0?
3852 // StartOffset <= OffsetB && StepOffset < 0?
3853 // Is non-equal if above are true.
3854 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3855 // optimisation to inbounds GEPs only.
3856 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3857 APInt StartOffset(IndexWidth, 0);
3858 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3859 APInt StepOffset(IndexWidth, 0);
3860 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3861
3862 // Check if Base Pointer of Step matches the PHI.
3863 if (Step != PN)
3864 return false;
3865 APInt OffsetB(IndexWidth, 0);
3866 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3867 return Start == B &&
3868 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
3869 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
3870}
3871
3872static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
3873 const SimplifyQuery &Q, unsigned Depth) {
3874 if (!Q.CxtI)
3875 return false;
3876
3877 // Try to infer NonEqual based on information from dominating conditions.
3878 if (Q.DC && Q.DT) {
3879 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
3880 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
3881 Value *Cond = BI->getCondition();
3882 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
3883 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
3885 /*LHSIsTrue=*/true, Depth)
3886 .value_or(false))
3887 return true;
3888
3889 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
3890 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
3892 /*LHSIsTrue=*/false, Depth)
3893 .value_or(false))
3894 return true;
3895 }
3896
3897 return false;
3898 };
3899
3900 if (IsKnownNonEqualFromDominatingCondition(V1) ||
3901 IsKnownNonEqualFromDominatingCondition(V2))
3902 return true;
3903 }
3904
3905 if (!Q.AC)
3906 return false;
3907
3908 // Try to infer NonEqual based on information from assumptions.
3909 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
3910 if (!AssumeVH)
3911 continue;
3912 CallInst *I = cast<CallInst>(AssumeVH);
3913
3914 assert(I->getFunction() == Q.CxtI->getFunction() &&
3915 "Got assumption for the wrong function!");
3916 assert(I->getIntrinsicID() == Intrinsic::assume &&
3917 "must be an assume intrinsic");
3918
3919 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
3920 /*LHSIsTrue=*/true, Depth)
3921 .value_or(false) &&
3923 return true;
3924 }
3925
3926 return false;
3927}
3928
3929/// Return true if it is known that V1 != V2.
3930static bool isKnownNonEqual(const Value *V1, const Value *V2,
3931 const APInt &DemandedElts, const SimplifyQuery &Q,
3932 unsigned Depth) {
3933 if (V1 == V2)
3934 return false;
3935 if (V1->getType() != V2->getType())
3936 // We can't look through casts yet.
3937 return false;
3938
3940 return false;
3941
3942 // See if we can recurse through (exactly one of) our operands. This
3943 // requires our operation be 1-to-1 and map every input value to exactly
3944 // one output value. Such an operation is invertible.
3945 auto *O1 = dyn_cast<Operator>(V1);
3946 auto *O2 = dyn_cast<Operator>(V2);
3947 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3948 if (auto Values = getInvertibleOperands(O1, O2))
3949 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
3950 Depth + 1);
3951
3952 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3953 const PHINode *PN2 = cast<PHINode>(V2);
3954 // FIXME: This is missing a generalization to handle the case where one is
3955 // a PHI and another one isn't.
3956 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
3957 return true;
3958 };
3959 }
3960
3961 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
3962 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
3963 return true;
3964
3965 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
3966 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
3967 return true;
3968
3969 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
3970 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
3971 return true;
3972
3973 if (V1->getType()->isIntOrIntVectorTy()) {
3974 // Are any known bits in V1 contradictory to known bits in V2? If V1
3975 // has a known zero where V2 has a known one, they must not be equal.
3976 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
3977 if (!Known1.isUnknown()) {
3978 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
3979 if (Known1.Zero.intersects(Known2.One) ||
3980 Known2.Zero.intersects(Known1.One))
3981 return true;
3982 }
3983 }
3984
3985 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
3986 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
3987 return true;
3988
3989 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3991 return true;
3992
3993 Value *A, *B;
3994 // PtrToInts are NonEqual if their Ptrs are NonEqual.
3995 // Check PtrToInt type matches the pointer size.
3996 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
3998 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
3999
4000 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
4001 return true;
4002
4003 return false;
4004}
4005
4006/// For vector constants, loop over the elements and find the constant with the
4007/// minimum number of sign bits. Return 0 if the value is not a vector constant
4008/// or if any element was not analyzed; otherwise, return the count for the
4009/// element with the minimum number of sign bits.
4011 const APInt &DemandedElts,
4012 unsigned TyBits) {
4013 const auto *CV = dyn_cast<Constant>(V);
4014 if (!CV || !isa<FixedVectorType>(CV->getType()))
4015 return 0;
4016
4017 unsigned MinSignBits = TyBits;
4018 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
4019 for (unsigned i = 0; i != NumElts; ++i) {
4020 if (!DemandedElts[i])
4021 continue;
4022 // If we find a non-ConstantInt, bail out.
4023 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
4024 if (!Elt)
4025 return 0;
4026
4027 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
4028 }
4029
4030 return MinSignBits;
4031}
4032
4033static unsigned ComputeNumSignBitsImpl(const Value *V,
4034 const APInt &DemandedElts,
4035 const SimplifyQuery &Q, unsigned Depth);
4036
4037static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4038 const SimplifyQuery &Q, unsigned Depth) {
4039 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4040 assert(Result > 0 && "At least one sign bit needs to be present!");
4041 return Result;
4042}
4043
4044/// Return the number of times the sign bit of the register is replicated into
4045/// the other bits. We know that at least 1 bit is always equal to the sign bit
4046/// (itself), but other cases can give us information. For example, immediately
4047/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4048/// other, so we return 3. For vectors, return the number of sign bits for the
4049/// vector element with the minimum number of known sign bits of the demanded
4050/// elements in the vector specified by DemandedElts.
4051static unsigned ComputeNumSignBitsImpl(const Value *V,
4052 const APInt &DemandedElts,
4053 const SimplifyQuery &Q, unsigned Depth) {
4054 Type *Ty = V->getType();
4055#ifndef NDEBUG
4056 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4057
4058 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4059 assert(
4060 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4061 "DemandedElt width should equal the fixed vector number of elements");
4062 } else {
4063 assert(DemandedElts == APInt(1, 1) &&
4064 "DemandedElt width should be 1 for scalars");
4065 }
4066#endif
4067
4068 // We return the minimum number of sign bits that are guaranteed to be present
4069 // in V, so for undef we have to conservatively return 1. We don't have the
4070 // same behavior for poison though -- that's a FIXME today.
4071
4072 Type *ScalarTy = Ty->getScalarType();
4073 unsigned TyBits = ScalarTy->isPointerTy() ?
4074 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4075 Q.DL.getTypeSizeInBits(ScalarTy);
4076
4077 unsigned Tmp, Tmp2;
4078 unsigned FirstAnswer = 1;
4079
4080 // Note that ConstantInt is handled by the general computeKnownBits case
4081 // below.
4082
4084 return 1;
4085
4086 if (auto *U = dyn_cast<Operator>(V)) {
4087 switch (Operator::getOpcode(V)) {
4088 default: break;
4089 case Instruction::BitCast: {
4090 Value *Src = U->getOperand(0);
4091 Type *SrcTy = Src->getType();
4092
4093 // Skip if the source type is not an integer or integer vector type
4094 // This ensures we only process integer-like types
4095 if (!SrcTy->isIntOrIntVectorTy())
4096 break;
4097
4098 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4099
4100 // Bitcast 'large element' scalar/vector to 'small element' vector.
4101 if ((SrcBits % TyBits) != 0)
4102 break;
4103
4104 // Only proceed if the destination type is a fixed-size vector
4105 if (isa<FixedVectorType>(Ty)) {
4106 // Fast case - sign splat can be simply split across the small elements.
4107 // This works for both vector and scalar sources
4108 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4109 if (Tmp == SrcBits)
4110 return TyBits;
4111 }
4112 break;
4113 }
4114 case Instruction::SExt:
4115 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4116 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4117 Tmp;
4118
4119 case Instruction::SDiv: {
4120 const APInt *Denominator;
4121 // sdiv X, C -> adds log(C) sign bits.
4122 if (match(U->getOperand(1), m_APInt(Denominator))) {
4123
4124 // Ignore non-positive denominator.
4125 if (!Denominator->isStrictlyPositive())
4126 break;
4127
4128 // Calculate the incoming numerator bits.
4129 unsigned NumBits =
4130 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4131
4132 // Add floor(log(C)) bits to the numerator bits.
4133 return std::min(TyBits, NumBits + Denominator->logBase2());
4134 }
4135 break;
4136 }
4137
4138 case Instruction::SRem: {
4139 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4140
4141 const APInt *Denominator;
4142 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4143 // positive constant. This let us put a lower bound on the number of sign
4144 // bits.
4145 if (match(U->getOperand(1), m_APInt(Denominator))) {
4146
4147 // Ignore non-positive denominator.
4148 if (Denominator->isStrictlyPositive()) {
4149 // Calculate the leading sign bit constraints by examining the
4150 // denominator. Given that the denominator is positive, there are two
4151 // cases:
4152 //
4153 // 1. The numerator is positive. The result range is [0,C) and
4154 // [0,C) u< (1 << ceilLogBase2(C)).
4155 //
4156 // 2. The numerator is negative. Then the result range is (-C,0] and
4157 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4158 //
4159 // Thus a lower bound on the number of sign bits is `TyBits -
4160 // ceilLogBase2(C)`.
4161
4162 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4163 Tmp = std::max(Tmp, ResBits);
4164 }
4165 }
4166 return Tmp;
4167 }
4168
4169 case Instruction::AShr: {
4170 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4171 // ashr X, C -> adds C sign bits. Vectors too.
4172 const APInt *ShAmt;
4173 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4174 if (ShAmt->uge(TyBits))
4175 break; // Bad shift.
4176 unsigned ShAmtLimited = ShAmt->getZExtValue();
4177 Tmp += ShAmtLimited;
4178 if (Tmp > TyBits) Tmp = TyBits;
4179 }
4180 return Tmp;
4181 }
4182 case Instruction::Shl: {
4183 const APInt *ShAmt;
4184 Value *X = nullptr;
4185 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4186 // shl destroys sign bits.
4187 if (ShAmt->uge(TyBits))
4188 break; // Bad shift.
4189 // We can look through a zext (more or less treating it as a sext) if
4190 // all extended bits are shifted out.
4191 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4192 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4193 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4194 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4195 } else
4196 Tmp =
4197 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4198 if (ShAmt->uge(Tmp))
4199 break; // Shifted all sign bits out.
4200 Tmp2 = ShAmt->getZExtValue();
4201 return Tmp - Tmp2;
4202 }
4203 break;
4204 }
4205 case Instruction::And:
4206 case Instruction::Or:
4207 case Instruction::Xor: // NOT is handled here.
4208 // Logical binary ops preserve the number of sign bits at the worst.
4209 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4210 if (Tmp != 1) {
4211 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4212 FirstAnswer = std::min(Tmp, Tmp2);
4213 // We computed what we know about the sign bits as our first
4214 // answer. Now proceed to the generic code that uses
4215 // computeKnownBits, and pick whichever answer is better.
4216 }
4217 break;
4218
4219 case Instruction::Select: {
4220 // If we have a clamp pattern, we know that the number of sign bits will
4221 // be the minimum of the clamp min/max range.
4222 const Value *X;
4223 const APInt *CLow, *CHigh;
4224 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4225 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4226
4227 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4228 if (Tmp == 1)
4229 break;
4230 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4231 return std::min(Tmp, Tmp2);
4232 }
4233
4234 case Instruction::Add:
4235 // Add can have at most one carry bit. Thus we know that the output
4236 // is, at worst, one more bit than the inputs.
4237 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4238 if (Tmp == 1) break;
4239
4240 // Special case decrementing a value (ADD X, -1):
4241 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4242 if (CRHS->isAllOnesValue()) {
4243 KnownBits Known(TyBits);
4244 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4245
4246 // If the input is known to be 0 or 1, the output is 0/-1, which is
4247 // all sign bits set.
4248 if ((Known.Zero | 1).isAllOnes())
4249 return TyBits;
4250
4251 // If we are subtracting one from a positive number, there is no carry
4252 // out of the result.
4253 if (Known.isNonNegative())
4254 return Tmp;
4255 }
4256
4257 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4258 if (Tmp2 == 1)
4259 break;
4260 return std::min(Tmp, Tmp2) - 1;
4261
4262 case Instruction::Sub:
4263 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4264 if (Tmp2 == 1)
4265 break;
4266
4267 // Handle NEG.
4268 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4269 if (CLHS->isNullValue()) {
4270 KnownBits Known(TyBits);
4271 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4272 // If the input is known to be 0 or 1, the output is 0/-1, which is
4273 // all sign bits set.
4274 if ((Known.Zero | 1).isAllOnes())
4275 return TyBits;
4276
4277 // If the input is known to be positive (the sign bit is known clear),
4278 // the output of the NEG has the same number of sign bits as the
4279 // input.
4280 if (Known.isNonNegative())
4281 return Tmp2;
4282
4283 // Otherwise, we treat this like a SUB.
4284 }
4285
4286 // Sub can have at most one carry bit. Thus we know that the output
4287 // is, at worst, one more bit than the inputs.
4288 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4289 if (Tmp == 1)
4290 break;
4291 return std::min(Tmp, Tmp2) - 1;
4292
4293 case Instruction::Mul: {
4294 // The output of the Mul can be at most twice the valid bits in the
4295 // inputs.
4296 unsigned SignBitsOp0 =
4297 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4298 if (SignBitsOp0 == 1)
4299 break;
4300 unsigned SignBitsOp1 =
4301 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4302 if (SignBitsOp1 == 1)
4303 break;
4304 unsigned OutValidBits =
4305 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4306 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4307 }
4308
4309 case Instruction::PHI: {
4310 const PHINode *PN = cast<PHINode>(U);
4311 unsigned NumIncomingValues = PN->getNumIncomingValues();
4312 // Don't analyze large in-degree PHIs.
4313 if (NumIncomingValues > 4) break;
4314 // Unreachable blocks may have zero-operand PHI nodes.
4315 if (NumIncomingValues == 0) break;
4316
4317 // Take the minimum of all incoming values. This can't infinitely loop
4318 // because of our depth threshold.
4320 Tmp = TyBits;
4321 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4322 if (Tmp == 1) return Tmp;
4323 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4324 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4325 DemandedElts, RecQ, Depth + 1));
4326 }
4327 return Tmp;
4328 }
4329
4330 case Instruction::Trunc: {
4331 // If the input contained enough sign bits that some remain after the
4332 // truncation, then we can make use of that. Otherwise we don't know
4333 // anything.
4334 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4335 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4336 if (Tmp > (OperandTyBits - TyBits))
4337 return Tmp - (OperandTyBits - TyBits);
4338
4339 return 1;
4340 }
4341
4342 case Instruction::ExtractElement:
4343 // Look through extract element. At the moment we keep this simple and
4344 // skip tracking the specific element. But at least we might find
4345 // information valid for all elements of the vector (for example if vector
4346 // is sign extended, shifted, etc).
4347 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4348
4349 case Instruction::ShuffleVector: {
4350 // Collect the minimum number of sign bits that are shared by every vector
4351 // element referenced by the shuffle.
4352 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4353 if (!Shuf) {
4354 // FIXME: Add support for shufflevector constant expressions.
4355 return 1;
4356 }
4357 APInt DemandedLHS, DemandedRHS;
4358 // For undef elements, we don't know anything about the common state of
4359 // the shuffle result.
4360 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4361 return 1;
4362 Tmp = std::numeric_limits<unsigned>::max();
4363 if (!!DemandedLHS) {
4364 const Value *LHS = Shuf->getOperand(0);
4365 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4366 }
4367 // If we don't know anything, early out and try computeKnownBits
4368 // fall-back.
4369 if (Tmp == 1)
4370 break;
4371 if (!!DemandedRHS) {
4372 const Value *RHS = Shuf->getOperand(1);
4373 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4374 Tmp = std::min(Tmp, Tmp2);
4375 }
4376 // If we don't know anything, early out and try computeKnownBits
4377 // fall-back.
4378 if (Tmp == 1)
4379 break;
4380 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4381 return Tmp;
4382 }
4383 case Instruction::Call: {
4384 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4385 switch (II->getIntrinsicID()) {
4386 default:
4387 break;
4388 case Intrinsic::abs:
4389 Tmp =
4390 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4391 if (Tmp == 1)
4392 break;
4393
4394 // Absolute value reduces number of sign bits by at most 1.
4395 return Tmp - 1;
4396 case Intrinsic::smin:
4397 case Intrinsic::smax: {
4398 const APInt *CLow, *CHigh;
4399 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4400 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4401 }
4402 }
4403 }
4404 }
4405 }
4406 }
4407
4408 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4409 // use this information.
4410
4411 // If we can examine all elements of a vector constant successfully, we're
4412 // done (we can't do any better than that). If not, keep trying.
4413 if (unsigned VecSignBits =
4414 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4415 return VecSignBits;
4416
4417 KnownBits Known(TyBits);
4418 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4419
4420 // If we know that the sign bit is either zero or one, determine the number of
4421 // identical bits in the top of the input value.
4422 return std::max(FirstAnswer, Known.countMinSignBits());
4423}
4424
4426 const TargetLibraryInfo *TLI) {
4427 const Function *F = CB.getCalledFunction();
4428 if (!F)
4430
4431 if (F->isIntrinsic())
4432 return F->getIntrinsicID();
4433
4434 // We are going to infer semantics of a library function based on mapping it
4435 // to an LLVM intrinsic. Check that the library function is available from
4436 // this callbase and in this environment.
4437 LibFunc Func;
4438 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4439 !CB.onlyReadsMemory())
4441
4442 switch (Func) {
4443 default:
4444 break;
4445 case LibFunc_sin:
4446 case LibFunc_sinf:
4447 case LibFunc_sinl:
4448 return Intrinsic::sin;
4449 case LibFunc_cos:
4450 case LibFunc_cosf:
4451 case LibFunc_cosl:
4452 return Intrinsic::cos;
4453 case LibFunc_tan:
4454 case LibFunc_tanf:
4455 case LibFunc_tanl:
4456 return Intrinsic::tan;
4457 case LibFunc_asin:
4458 case LibFunc_asinf:
4459 case LibFunc_asinl:
4460 return Intrinsic::asin;
4461 case LibFunc_acos:
4462 case LibFunc_acosf:
4463 case LibFunc_acosl:
4464 return Intrinsic::acos;
4465 case LibFunc_atan:
4466 case LibFunc_atanf:
4467 case LibFunc_atanl:
4468 return Intrinsic::atan;
4469 case LibFunc_atan2:
4470 case LibFunc_atan2f:
4471 case LibFunc_atan2l:
4472 return Intrinsic::atan2;
4473 case LibFunc_sinh:
4474 case LibFunc_sinhf:
4475 case LibFunc_sinhl:
4476 return Intrinsic::sinh;
4477 case LibFunc_cosh:
4478 case LibFunc_coshf:
4479 case LibFunc_coshl:
4480 return Intrinsic::cosh;
4481 case LibFunc_tanh:
4482 case LibFunc_tanhf:
4483 case LibFunc_tanhl:
4484 return Intrinsic::tanh;
4485 case LibFunc_exp:
4486 case LibFunc_expf:
4487 case LibFunc_expl:
4488 return Intrinsic::exp;
4489 case LibFunc_exp2:
4490 case LibFunc_exp2f:
4491 case LibFunc_exp2l:
4492 return Intrinsic::exp2;
4493 case LibFunc_exp10:
4494 case LibFunc_exp10f:
4495 case LibFunc_exp10l:
4496 return Intrinsic::exp10;
4497 case LibFunc_log:
4498 case LibFunc_logf:
4499 case LibFunc_logl:
4500 return Intrinsic::log;
4501 case LibFunc_log10:
4502 case LibFunc_log10f:
4503 case LibFunc_log10l:
4504 return Intrinsic::log10;
4505 case LibFunc_log2:
4506 case LibFunc_log2f:
4507 case LibFunc_log2l:
4508 return Intrinsic::log2;
4509 case LibFunc_fabs:
4510 case LibFunc_fabsf:
4511 case LibFunc_fabsl:
4512 return Intrinsic::fabs;
4513 case LibFunc_fmin:
4514 case LibFunc_fminf:
4515 case LibFunc_fminl:
4516 return Intrinsic::minnum;
4517 case LibFunc_fmax:
4518 case LibFunc_fmaxf:
4519 case LibFunc_fmaxl:
4520 return Intrinsic::maxnum;
4521 case LibFunc_copysign:
4522 case LibFunc_copysignf:
4523 case LibFunc_copysignl:
4524 return Intrinsic::copysign;
4525 case LibFunc_floor:
4526 case LibFunc_floorf:
4527 case LibFunc_floorl:
4528 return Intrinsic::floor;
4529 case LibFunc_ceil:
4530 case LibFunc_ceilf:
4531 case LibFunc_ceill:
4532 return Intrinsic::ceil;
4533 case LibFunc_trunc:
4534 case LibFunc_truncf:
4535 case LibFunc_truncl:
4536 return Intrinsic::trunc;
4537 case LibFunc_rint:
4538 case LibFunc_rintf:
4539 case LibFunc_rintl:
4540 return Intrinsic::rint;
4541 case LibFunc_nearbyint:
4542 case LibFunc_nearbyintf:
4543 case LibFunc_nearbyintl:
4544 return Intrinsic::nearbyint;
4545 case LibFunc_round:
4546 case LibFunc_roundf:
4547 case LibFunc_roundl:
4548 return Intrinsic::round;
4549 case LibFunc_roundeven:
4550 case LibFunc_roundevenf:
4551 case LibFunc_roundevenl:
4552 return Intrinsic::roundeven;
4553 case LibFunc_pow:
4554 case LibFunc_powf:
4555 case LibFunc_powl:
4556 return Intrinsic::pow;
4557 case LibFunc_sqrt:
4558 case LibFunc_sqrtf:
4559 case LibFunc_sqrtl:
4560 return Intrinsic::sqrt;
4561 }
4562
4564}
4565
4566static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4567 Ty = Ty->getScalarType();
4568 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4569 return Mode.Output == DenormalMode::IEEE ||
4571}
4572/// Given an exploded icmp instruction, return true if the comparison only
4573/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4574/// the result of the comparison is true when the input value is signed.
4576 bool &TrueIfSigned) {
4577 switch (Pred) {
4578 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4579 TrueIfSigned = true;
4580 return RHS.isZero();
4581 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4582 TrueIfSigned = true;
4583 return RHS.isAllOnes();
4584 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4585 TrueIfSigned = false;
4586 return RHS.isAllOnes();
4587 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4588 TrueIfSigned = false;
4589 return RHS.isZero();
4590 case ICmpInst::ICMP_UGT:
4591 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4592 TrueIfSigned = true;
4593 return RHS.isMaxSignedValue();
4594 case ICmpInst::ICMP_UGE:
4595 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4596 TrueIfSigned = true;
4597 return RHS.isMinSignedValue();
4598 case ICmpInst::ICMP_ULT:
4599 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4600 TrueIfSigned = false;
4601 return RHS.isMinSignedValue();
4602 case ICmpInst::ICMP_ULE:
4603 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4604 TrueIfSigned = false;
4605 return RHS.isMaxSignedValue();
4606 default:
4607 return false;
4608 }
4609}
4610
4612 bool CondIsTrue,
4613 const Instruction *CxtI,
4614 KnownFPClass &KnownFromContext,
4615 unsigned Depth = 0) {
4616 Value *A, *B;
4618 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4619 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4620 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4621 Depth + 1);
4622 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4623 Depth + 1);
4624 return;
4625 }
4627 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4628 Depth + 1);
4629 return;
4630 }
4631 CmpPredicate Pred;
4632 Value *LHS;
4633 uint64_t ClassVal = 0;
4634 const APFloat *CRHS;
4635 const APInt *RHS;
4636 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4637 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4638 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4639 if (CmpVal == V)
4640 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4642 m_Specific(V), m_ConstantInt(ClassVal)))) {
4643 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4644 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4645 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4646 m_APInt(RHS)))) {
4647 bool TrueIfSigned;
4648 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4649 return;
4650 if (TrueIfSigned == CondIsTrue)
4651 KnownFromContext.signBitMustBeOne();
4652 else
4653 KnownFromContext.signBitMustBeZero();
4654 }
4655}
4656
4658 const SimplifyQuery &Q) {
4659 KnownFPClass KnownFromContext;
4660
4661 if (Q.CC && Q.CC->AffectedValues.contains(V))
4663 KnownFromContext);
4664
4665 if (!Q.CxtI)
4666 return KnownFromContext;
4667
4668 if (Q.DC && Q.DT) {
4669 // Handle dominating conditions.
4670 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4671 Value *Cond = BI->getCondition();
4672
4673 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4674 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4675 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4676 KnownFromContext);
4677
4678 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4679 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4680 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4681 KnownFromContext);
4682 }
4683 }
4684
4685 if (!Q.AC)
4686 return KnownFromContext;
4687
4688 // Try to restrict the floating-point classes based on information from
4689 // assumptions.
4690 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4691 if (!AssumeVH)
4692 continue;
4693 CallInst *I = cast<CallInst>(AssumeVH);
4694
4695 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4696 "Got assumption for the wrong function!");
4697 assert(I->getIntrinsicID() == Intrinsic::assume &&
4698 "must be an assume intrinsic");
4699
4700 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4701 continue;
4702
4703 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4704 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4705 }
4706
4707 return KnownFromContext;
4708}
4709
4710void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4711 FPClassTest InterestedClasses, KnownFPClass &Known,
4712 const SimplifyQuery &Q, unsigned Depth);
4713
4714static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4715 FPClassTest InterestedClasses,
4716 const SimplifyQuery &Q, unsigned Depth) {
4717 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4718 APInt DemandedElts =
4719 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4720 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4721}
4722
4724 const APInt &DemandedElts,
4725 FPClassTest InterestedClasses,
4726 KnownFPClass &Known,
4727 const SimplifyQuery &Q,
4728 unsigned Depth) {
4729 if ((InterestedClasses &
4731 return;
4732
4733 KnownFPClass KnownSrc;
4734 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4735 KnownSrc, Q, Depth + 1);
4736
4737 // Sign should be preserved
4738 // TODO: Handle cannot be ordered greater than zero
4739 if (KnownSrc.cannotBeOrderedLessThanZero())
4741
4742 Known.propagateNaN(KnownSrc, true);
4743
4744 // Infinity needs a range check.
4745}
4746
4747void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4748 FPClassTest InterestedClasses, KnownFPClass &Known,
4749 const SimplifyQuery &Q, unsigned Depth) {
4750 assert(Known.isUnknown() && "should not be called with known information");
4751
4752 if (!DemandedElts) {
4753 // No demanded elts, better to assume we don't know anything.
4754 Known.resetAll();
4755 return;
4756 }
4757
4758 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4759
4760 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4761 Known.KnownFPClasses = CFP->getValueAPF().classify();
4762 Known.SignBit = CFP->isNegative();
4763 return;
4764 }
4765
4767 Known.KnownFPClasses = fcPosZero;
4768 Known.SignBit = false;
4769 return;
4770 }
4771
4772 if (isa<PoisonValue>(V)) {
4773 Known.KnownFPClasses = fcNone;
4774 Known.SignBit = false;
4775 return;
4776 }
4777
4778 // Try to handle fixed width vector constants
4779 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
4780 const Constant *CV = dyn_cast<Constant>(V);
4781 if (VFVTy && CV) {
4782 Known.KnownFPClasses = fcNone;
4783 bool SignBitAllZero = true;
4784 bool SignBitAllOne = true;
4785
4786 // For vectors, verify that each element is not NaN.
4787 unsigned NumElts = VFVTy->getNumElements();
4788 for (unsigned i = 0; i != NumElts; ++i) {
4789 if (!DemandedElts[i])
4790 continue;
4791
4792 Constant *Elt = CV->getAggregateElement(i);
4793 if (!Elt) {
4794 Known = KnownFPClass();
4795 return;
4796 }
4797 if (isa<PoisonValue>(Elt))
4798 continue;
4799 auto *CElt = dyn_cast<ConstantFP>(Elt);
4800 if (!CElt) {
4801 Known = KnownFPClass();
4802 return;
4803 }
4804
4805 const APFloat &C = CElt->getValueAPF();
4806 Known.KnownFPClasses |= C.classify();
4807 if (C.isNegative())
4808 SignBitAllZero = false;
4809 else
4810 SignBitAllOne = false;
4811 }
4812 if (SignBitAllOne != SignBitAllZero)
4813 Known.SignBit = SignBitAllOne;
4814 return;
4815 }
4816
4817 FPClassTest KnownNotFromFlags = fcNone;
4818 if (const auto *CB = dyn_cast<CallBase>(V))
4819 KnownNotFromFlags |= CB->getRetNoFPClass();
4820 else if (const auto *Arg = dyn_cast<Argument>(V))
4821 KnownNotFromFlags |= Arg->getNoFPClass();
4822
4823 const Operator *Op = dyn_cast<Operator>(V);
4825 if (FPOp->hasNoNaNs())
4826 KnownNotFromFlags |= fcNan;
4827 if (FPOp->hasNoInfs())
4828 KnownNotFromFlags |= fcInf;
4829 }
4830
4831 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
4832 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
4833
4834 // We no longer need to find out about these bits from inputs if we can
4835 // assume this from flags/attributes.
4836 InterestedClasses &= ~KnownNotFromFlags;
4837
4838 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
4839 Known.knownNot(KnownNotFromFlags);
4840 if (!Known.SignBit && AssumedClasses.SignBit) {
4841 if (*AssumedClasses.SignBit)
4842 Known.signBitMustBeOne();
4843 else
4844 Known.signBitMustBeZero();
4845 }
4846 });
4847
4848 if (!Op)
4849 return;
4850
4851 // All recursive calls that increase depth must come after this.
4853 return;
4854
4855 const unsigned Opc = Op->getOpcode();
4856 switch (Opc) {
4857 case Instruction::FNeg: {
4858 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4859 Known, Q, Depth + 1);
4860 Known.fneg();
4861 break;
4862 }
4863 case Instruction::Select: {
4864 Value *Cond = Op->getOperand(0);
4865 Value *LHS = Op->getOperand(1);
4866 Value *RHS = Op->getOperand(2);
4867
4868 FPClassTest FilterLHS = fcAllFlags;
4869 FPClassTest FilterRHS = fcAllFlags;
4870
4871 Value *TestedValue = nullptr;
4872 FPClassTest MaskIfTrue = fcAllFlags;
4873 FPClassTest MaskIfFalse = fcAllFlags;
4874 uint64_t ClassVal = 0;
4875 const Function *F = cast<Instruction>(Op)->getFunction();
4876 CmpPredicate Pred;
4877 Value *CmpLHS, *CmpRHS;
4878 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
4879 // If the select filters out a value based on the class, it no longer
4880 // participates in the class of the result
4881
4882 // TODO: In some degenerate cases we can infer something if we try again
4883 // without looking through sign operations.
4884 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
4885 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
4886 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
4887 } else if (match(Cond,
4889 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
4890 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
4891 MaskIfTrue = TestedMask;
4892 MaskIfFalse = ~TestedMask;
4893 }
4894
4895 if (TestedValue == LHS) {
4896 // match !isnan(x) ? x : y
4897 FilterLHS = MaskIfTrue;
4898 } else if (TestedValue == RHS) { // && IsExactClass
4899 // match !isnan(x) ? y : x
4900 FilterRHS = MaskIfFalse;
4901 }
4902
4903 KnownFPClass Known2;
4904 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
4905 Q, Depth + 1);
4906 Known.KnownFPClasses &= FilterLHS;
4907
4908 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
4909 Known2, Q, Depth + 1);
4910 Known2.KnownFPClasses &= FilterRHS;
4911
4912 Known |= Known2;
4913 break;
4914 }
4915 case Instruction::Call: {
4916 const CallInst *II = cast<CallInst>(Op);
4917 const Intrinsic::ID IID = II->getIntrinsicID();
4918 switch (IID) {
4919 case Intrinsic::fabs: {
4920 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
4921 // If we only care about the sign bit we don't need to inspect the
4922 // operand.
4923 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
4924 InterestedClasses, Known, Q, Depth + 1);
4925 }
4926
4927 Known.fabs();
4928 break;
4929 }
4930 case Intrinsic::copysign: {
4931 KnownFPClass KnownSign;
4932
4933 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4934 Known, Q, Depth + 1);
4935 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4936 KnownSign, Q, Depth + 1);
4937 Known.copysign(KnownSign);
4938 break;
4939 }
4940 case Intrinsic::fma:
4941 case Intrinsic::fmuladd: {
4942 if ((InterestedClasses & fcNegative) == fcNone)
4943 break;
4944
4945 if (II->getArgOperand(0) != II->getArgOperand(1))
4946 break;
4947
4948 // The multiply cannot be -0 and therefore the add can't be -0
4949 Known.knownNot(fcNegZero);
4950
4951 // x * x + y is non-negative if y is non-negative.
4952 KnownFPClass KnownAddend;
4953 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
4954 KnownAddend, Q, Depth + 1);
4955
4956 if (KnownAddend.cannotBeOrderedLessThanZero())
4957 Known.knownNot(fcNegative);
4958 break;
4959 }
4960 case Intrinsic::sqrt:
4961 case Intrinsic::experimental_constrained_sqrt: {
4962 KnownFPClass KnownSrc;
4963 FPClassTest InterestedSrcs = InterestedClasses;
4964 if (InterestedClasses & fcNan)
4965 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
4966
4967 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
4968 KnownSrc, Q, Depth + 1);
4969
4970 if (KnownSrc.isKnownNeverPosInfinity())
4971 Known.knownNot(fcPosInf);
4972 if (KnownSrc.isKnownNever(fcSNan))
4973 Known.knownNot(fcSNan);
4974
4975 // Any negative value besides -0 returns a nan.
4976 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
4977 Known.knownNot(fcNan);
4978
4979 // The only negative value that can be returned is -0 for -0 inputs.
4981
4982 // If the input denormal mode could be PreserveSign, a negative
4983 // subnormal input could produce a negative zero output.
4984 const Function *F = II->getFunction();
4985 const fltSemantics &FltSem =
4986 II->getType()->getScalarType()->getFltSemantics();
4987
4988 if (Q.IIQ.hasNoSignedZeros(II) ||
4989 (F &&
4990 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem))))
4991 Known.knownNot(fcNegZero);
4992
4993 break;
4994 }
4995 case Intrinsic::sin:
4996 case Intrinsic::cos: {
4997 // Return NaN on infinite inputs.
4998 KnownFPClass KnownSrc;
4999 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5000 KnownSrc, Q, Depth + 1);
5001 Known.knownNot(fcInf);
5002 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
5003 Known.knownNot(fcNan);
5004 break;
5005 }
5006 case Intrinsic::maxnum:
5007 case Intrinsic::minnum:
5008 case Intrinsic::minimum:
5009 case Intrinsic::maximum:
5010 case Intrinsic::minimumnum:
5011 case Intrinsic::maximumnum: {
5012 KnownFPClass KnownLHS, KnownRHS;
5013 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5014 KnownLHS, Q, Depth + 1);
5015 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5016 KnownRHS, Q, Depth + 1);
5017
5018 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
5019 Known = KnownLHS | KnownRHS;
5020
5021 // If either operand is not NaN, the result is not NaN.
5022 if (NeverNaN &&
5023 (IID == Intrinsic::minnum || IID == Intrinsic::maxnum ||
5024 IID == Intrinsic::minimumnum || IID == Intrinsic::maximumnum))
5025 Known.knownNot(fcNan);
5026
5027 if (IID == Intrinsic::maxnum || IID == Intrinsic::maximumnum) {
5028 // If at least one operand is known to be positive, the result must be
5029 // positive.
5030 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
5031 KnownLHS.isKnownNeverNaN()) ||
5032 (KnownRHS.cannotBeOrderedLessThanZero() &&
5033 KnownRHS.isKnownNeverNaN()))
5035 } else if (IID == Intrinsic::maximum) {
5036 // If at least one operand is known to be positive, the result must be
5037 // positive.
5038 if (KnownLHS.cannotBeOrderedLessThanZero() ||
5039 KnownRHS.cannotBeOrderedLessThanZero())
5041 } else if (IID == Intrinsic::minnum || IID == Intrinsic::minimumnum) {
5042 // If at least one operand is known to be negative, the result must be
5043 // negative.
5044 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
5045 KnownLHS.isKnownNeverNaN()) ||
5046 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
5047 KnownRHS.isKnownNeverNaN()))
5049 } else if (IID == Intrinsic::minimum) {
5050 // If at least one operand is known to be negative, the result must be
5051 // negative.
5052 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
5055 } else
5056 llvm_unreachable("unhandled intrinsic");
5057
5058 // Fixup zero handling if denormals could be returned as a zero.
5059 //
5060 // As there's no spec for denormal flushing, be conservative with the
5061 // treatment of denormals that could be flushed to zero. For older
5062 // subtargets on AMDGPU the min/max instructions would not flush the
5063 // output and return the original value.
5064 //
5065 if ((Known.KnownFPClasses & fcZero) != fcNone &&
5066 !Known.isKnownNeverSubnormal()) {
5067 const Function *Parent = II->getFunction();
5068 if (!Parent)
5069 break;
5070
5072 II->getType()->getScalarType()->getFltSemantics());
5073 if (Mode != DenormalMode::getIEEE())
5074 Known.KnownFPClasses |= fcZero;
5075 }
5076
5077 if (Known.isKnownNeverNaN()) {
5078 if (KnownLHS.SignBit && KnownRHS.SignBit &&
5079 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5080 if (*KnownLHS.SignBit)
5081 Known.signBitMustBeOne();
5082 else
5083 Known.signBitMustBeZero();
5084 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum ||
5085 IID == Intrinsic::maximumnum ||
5086 IID == Intrinsic::minimumnum) ||
5087 // FIXME: Should be using logical zero versions
5088 ((KnownLHS.isKnownNeverNegZero() ||
5089 KnownRHS.isKnownNeverPosZero()) &&
5090 (KnownLHS.isKnownNeverPosZero() ||
5091 KnownRHS.isKnownNeverNegZero()))) {
5092 // Don't take sign bit from NaN operands.
5093 if (!KnownLHS.isKnownNeverNaN())
5094 KnownLHS.SignBit = std::nullopt;
5095 if (!KnownRHS.isKnownNeverNaN())
5096 KnownRHS.SignBit = std::nullopt;
5097 if ((IID == Intrinsic::maximum || IID == Intrinsic::maximumnum ||
5098 IID == Intrinsic::maxnum) &&
5099 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5100 Known.signBitMustBeZero();
5101 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minimumnum ||
5102 IID == Intrinsic::minnum) &&
5103 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5104 Known.signBitMustBeOne();
5105 }
5106 }
5107 break;
5108 }
5109 case Intrinsic::canonicalize: {
5110 KnownFPClass KnownSrc;
5111 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5112 KnownSrc, Q, Depth + 1);
5113
5114 // This is essentially a stronger form of
5115 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5116 // actually have an IR canonicalization guarantee.
5117
5118 // Canonicalize may flush denormals to zero, so we have to consider the
5119 // denormal mode to preserve known-not-0 knowledge.
5120 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5121
5122 // Stronger version of propagateNaN
5123 // Canonicalize is guaranteed to quiet signaling nans.
5124 if (KnownSrc.isKnownNeverNaN())
5125 Known.knownNot(fcNan);
5126 else
5127 Known.knownNot(fcSNan);
5128
5129 const Function *F = II->getFunction();
5130 if (!F)
5131 break;
5132
5133 // If the parent function flushes denormals, the canonical output cannot
5134 // be a denormal.
5135 const fltSemantics &FPType =
5136 II->getType()->getScalarType()->getFltSemantics();
5137 DenormalMode DenormMode = F->getDenormalMode(FPType);
5138 if (DenormMode == DenormalMode::getIEEE()) {
5139 if (KnownSrc.isKnownNever(fcPosZero))
5140 Known.knownNot(fcPosZero);
5141 if (KnownSrc.isKnownNever(fcNegZero))
5142 Known.knownNot(fcNegZero);
5143 break;
5144 }
5145
5146 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5147 Known.knownNot(fcSubnormal);
5148
5149 if (DenormMode.Input == DenormalMode::PositiveZero ||
5150 (DenormMode.Output == DenormalMode::PositiveZero &&
5151 DenormMode.Input == DenormalMode::IEEE))
5152 Known.knownNot(fcNegZero);
5153
5154 break;
5155 }
5156 case Intrinsic::vector_reduce_fmax:
5157 case Intrinsic::vector_reduce_fmin:
5158 case Intrinsic::vector_reduce_fmaximum:
5159 case Intrinsic::vector_reduce_fminimum: {
5160 // reduce min/max will choose an element from one of the vector elements,
5161 // so we can infer and class information that is common to all elements.
5162 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5163 InterestedClasses, Q, Depth + 1);
5164 // Can only propagate sign if output is never NaN.
5165 if (!Known.isKnownNeverNaN())
5166 Known.SignBit.reset();
5167 break;
5168 }
5169 // reverse preserves all characteristics of the input vec's element.
5170 case Intrinsic::vector_reverse:
5171 Known = computeKnownFPClass(
5172 II->getArgOperand(0), DemandedElts.reverseBits(),
5173 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5174 break;
5175 case Intrinsic::trunc:
5176 case Intrinsic::floor:
5177 case Intrinsic::ceil:
5178 case Intrinsic::rint:
5179 case Intrinsic::nearbyint:
5180 case Intrinsic::round:
5181 case Intrinsic::roundeven: {
5182 KnownFPClass KnownSrc;
5183 FPClassTest InterestedSrcs = InterestedClasses;
5184 if (InterestedSrcs & fcPosFinite)
5185 InterestedSrcs |= fcPosFinite;
5186 if (InterestedSrcs & fcNegFinite)
5187 InterestedSrcs |= fcNegFinite;
5188 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5189 KnownSrc, Q, Depth + 1);
5190
5191 // Integer results cannot be subnormal.
5192 Known.knownNot(fcSubnormal);
5193
5194 Known.propagateNaN(KnownSrc, true);
5195
5196 // Pass through infinities, except PPC_FP128 is a special case for
5197 // intrinsics other than trunc.
5198 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5199 if (KnownSrc.isKnownNeverPosInfinity())
5200 Known.knownNot(fcPosInf);
5201 if (KnownSrc.isKnownNeverNegInfinity())
5202 Known.knownNot(fcNegInf);
5203 }
5204
5205 // Negative round ups to 0 produce -0
5206 if (KnownSrc.isKnownNever(fcPosFinite))
5207 Known.knownNot(fcPosFinite);
5208 if (KnownSrc.isKnownNever(fcNegFinite))
5209 Known.knownNot(fcNegFinite);
5210
5211 break;
5212 }
5213 case Intrinsic::exp:
5214 case Intrinsic::exp2:
5215 case Intrinsic::exp10: {
5216 Known.knownNot(fcNegative);
5217 if ((InterestedClasses & fcNan) == fcNone)
5218 break;
5219
5220 KnownFPClass KnownSrc;
5221 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5222 KnownSrc, Q, Depth + 1);
5223 if (KnownSrc.isKnownNeverNaN()) {
5224 Known.knownNot(fcNan);
5225 Known.signBitMustBeZero();
5226 }
5227
5228 break;
5229 }
5230 case Intrinsic::fptrunc_round: {
5231 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5232 Q, Depth);
5233 break;
5234 }
5235 case Intrinsic::log:
5236 case Intrinsic::log10:
5237 case Intrinsic::log2:
5238 case Intrinsic::experimental_constrained_log:
5239 case Intrinsic::experimental_constrained_log10:
5240 case Intrinsic::experimental_constrained_log2: {
5241 // log(+inf) -> +inf
5242 // log([+-]0.0) -> -inf
5243 // log(-inf) -> nan
5244 // log(-x) -> nan
5245 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5246 break;
5247
5248 FPClassTest InterestedSrcs = InterestedClasses;
5249 if ((InterestedClasses & fcNegInf) != fcNone)
5250 InterestedSrcs |= fcZero | fcSubnormal;
5251 if ((InterestedClasses & fcNan) != fcNone)
5252 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5253
5254 KnownFPClass KnownSrc;
5255 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5256 KnownSrc, Q, Depth + 1);
5257
5258 if (KnownSrc.isKnownNeverPosInfinity())
5259 Known.knownNot(fcPosInf);
5260
5261 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5262 Known.knownNot(fcNan);
5263
5264 const Function *F = II->getFunction();
5265
5266 if (!F)
5267 break;
5268
5269 const fltSemantics &FltSem =
5270 II->getType()->getScalarType()->getFltSemantics();
5271 DenormalMode Mode = F->getDenormalMode(FltSem);
5272
5273 if (KnownSrc.isKnownNeverLogicalZero(Mode))
5274 Known.knownNot(fcNegInf);
5275
5276 break;
5277 }
5278 case Intrinsic::powi: {
5279 if ((InterestedClasses & fcNegative) == fcNone)
5280 break;
5281
5282 const Value *Exp = II->getArgOperand(1);
5283 Type *ExpTy = Exp->getType();
5284 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5285 KnownBits ExponentKnownBits(BitWidth);
5286 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5287 ExponentKnownBits, Q, Depth + 1);
5288
5289 if (ExponentKnownBits.Zero[0]) { // Is even
5290 Known.knownNot(fcNegative);
5291 break;
5292 }
5293
5294 // Given that exp is an integer, here are the
5295 // ways that pow can return a negative value:
5296 //
5297 // pow(-x, exp) --> negative if exp is odd and x is negative.
5298 // pow(-0, exp) --> -inf if exp is negative odd.
5299 // pow(-0, exp) --> -0 if exp is positive odd.
5300 // pow(-inf, exp) --> -0 if exp is negative odd.
5301 // pow(-inf, exp) --> -inf if exp is positive odd.
5302 KnownFPClass KnownSrc;
5303 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5304 KnownSrc, Q, Depth + 1);
5305 if (KnownSrc.isKnownNever(fcNegative))
5306 Known.knownNot(fcNegative);
5307 break;
5308 }
5309 case Intrinsic::ldexp: {
5310 KnownFPClass KnownSrc;
5311 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5312 KnownSrc, Q, Depth + 1);
5313 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5314
5315 // Sign is preserved, but underflows may produce zeroes.
5316 if (KnownSrc.isKnownNever(fcNegative))
5317 Known.knownNot(fcNegative);
5318 else if (KnownSrc.cannotBeOrderedLessThanZero())
5320
5321 if (KnownSrc.isKnownNever(fcPositive))
5322 Known.knownNot(fcPositive);
5323 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5325
5326 // Can refine inf/zero handling based on the exponent operand.
5327 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5328 if ((InterestedClasses & ExpInfoMask) == fcNone)
5329 break;
5330 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5331 break;
5332
5333 const fltSemantics &Flt =
5334 II->getType()->getScalarType()->getFltSemantics();
5335 unsigned Precision = APFloat::semanticsPrecision(Flt);
5336 const Value *ExpArg = II->getArgOperand(1);
5338 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5339
5340 const int MantissaBits = Precision - 1;
5341 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5342 Known.knownNot(fcSubnormal);
5343
5344 const Function *F = II->getFunction();
5345 const APInt *ConstVal = ExpRange.getSingleElement();
5346 const fltSemantics &FltSem =
5347 II->getType()->getScalarType()->getFltSemantics();
5348 if (ConstVal && ConstVal->isZero()) {
5349 // ldexp(x, 0) -> x, so propagate everything.
5350 Known.propagateCanonicalizingSrc(KnownSrc, F->getDenormalMode(FltSem));
5351 } else if (ExpRange.isAllNegative()) {
5352 // If we know the power is <= 0, can't introduce inf
5353 if (KnownSrc.isKnownNeverPosInfinity())
5354 Known.knownNot(fcPosInf);
5355 if (KnownSrc.isKnownNeverNegInfinity())
5356 Known.knownNot(fcNegInf);
5357 } else if (ExpRange.isAllNonNegative()) {
5358 // If we know the power is >= 0, can't introduce subnormal or zero
5359 if (KnownSrc.isKnownNeverPosSubnormal())
5360 Known.knownNot(fcPosSubnormal);
5361 if (KnownSrc.isKnownNeverNegSubnormal())
5362 Known.knownNot(fcNegSubnormal);
5363 if (F &&
5364 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5365 Known.knownNot(fcPosZero);
5366 if (F &&
5367 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5368 Known.knownNot(fcNegZero);
5369 }
5370
5371 break;
5372 }
5373 case Intrinsic::arithmetic_fence: {
5374 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5375 Known, Q, Depth + 1);
5376 break;
5377 }
5378 case Intrinsic::experimental_constrained_sitofp:
5379 case Intrinsic::experimental_constrained_uitofp:
5380 // Cannot produce nan
5381 Known.knownNot(fcNan);
5382
5383 // sitofp and uitofp turn into +0.0 for zero.
5384 Known.knownNot(fcNegZero);
5385
5386 // Integers cannot be subnormal
5387 Known.knownNot(fcSubnormal);
5388
5389 if (IID == Intrinsic::experimental_constrained_uitofp)
5390 Known.signBitMustBeZero();
5391
5392 // TODO: Copy inf handling from instructions
5393 break;
5394 default:
5395 break;
5396 }
5397
5398 break;
5399 }
5400 case Instruction::FAdd:
5401 case Instruction::FSub: {
5402 KnownFPClass KnownLHS, KnownRHS;
5403 bool WantNegative =
5404 Op->getOpcode() == Instruction::FAdd &&
5405 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5406 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5407 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5408
5409 if (!WantNaN && !WantNegative && !WantNegZero)
5410 break;
5411
5412 FPClassTest InterestedSrcs = InterestedClasses;
5413 if (WantNegative)
5414 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5415 if (InterestedClasses & fcNan)
5416 InterestedSrcs |= fcInf;
5417 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5418 KnownRHS, Q, Depth + 1);
5419
5420 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5421 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5422 WantNegZero || Opc == Instruction::FSub) {
5423
5424 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5425 // there's no point.
5426 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5427 KnownLHS, Q, Depth + 1);
5428 // Adding positive and negative infinity produces NaN.
5429 // TODO: Check sign of infinities.
5430 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5431 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5432 Known.knownNot(fcNan);
5433
5434 // FIXME: Context function should always be passed in separately
5435 const Function *F = cast<Instruction>(Op)->getFunction();
5436
5437 if (Op->getOpcode() == Instruction::FAdd) {
5438 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5439 KnownRHS.cannotBeOrderedLessThanZero())
5441 if (!F)
5442 break;
5443
5444 const fltSemantics &FltSem =
5445 Op->getType()->getScalarType()->getFltSemantics();
5446 DenormalMode Mode = F->getDenormalMode(FltSem);
5447
5448 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5449 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5450 KnownRHS.isKnownNeverLogicalNegZero(Mode)) &&
5451 // Make sure output negative denormal can't flush to -0
5452 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5453 Known.knownNot(fcNegZero);
5454 } else {
5455 if (!F)
5456 break;
5457
5458 const fltSemantics &FltSem =
5459 Op->getType()->getScalarType()->getFltSemantics();
5460 DenormalMode Mode = F->getDenormalMode(FltSem);
5461
5462 // Only fsub -0, +0 can return -0
5463 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5464 KnownRHS.isKnownNeverLogicalPosZero(Mode)) &&
5465 // Make sure output negative denormal can't flush to -0
5466 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5467 Known.knownNot(fcNegZero);
5468 }
5469 }
5470
5471 break;
5472 }
5473 case Instruction::FMul: {
5474 // X * X is always non-negative or a NaN.
5475 if (Op->getOperand(0) == Op->getOperand(1))
5476 Known.knownNot(fcNegative);
5477
5478 if ((InterestedClasses & fcNan) != fcNan)
5479 break;
5480
5481 // fcSubnormal is only needed in case of DAZ.
5482 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5483
5484 KnownFPClass KnownLHS, KnownRHS;
5485 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5486 Q, Depth + 1);
5487 if (!KnownRHS.isKnownNeverNaN())
5488 break;
5489
5490 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5491 Q, Depth + 1);
5492 if (!KnownLHS.isKnownNeverNaN())
5493 break;
5494
5495 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5496 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5497 Known.signBitMustBeZero();
5498 else
5499 Known.signBitMustBeOne();
5500 }
5501
5502 // If 0 * +/-inf produces NaN.
5503 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5504 Known.knownNot(fcNan);
5505 break;
5506 }
5507
5508 const Function *F = cast<Instruction>(Op)->getFunction();
5509 if (!F)
5510 break;
5511
5512 Type *OpTy = Op->getType()->getScalarType();
5513 const fltSemantics &FltSem = OpTy->getFltSemantics();
5514 DenormalMode Mode = F->getDenormalMode(FltSem);
5515
5516 if ((KnownRHS.isKnownNeverInfinity() ||
5517 KnownLHS.isKnownNeverLogicalZero(Mode)) &&
5518 (KnownLHS.isKnownNeverInfinity() ||
5519 KnownRHS.isKnownNeverLogicalZero(Mode)))
5520 Known.knownNot(fcNan);
5521
5522 break;
5523 }
5524 case Instruction::FDiv:
5525 case Instruction::FRem: {
5526 if (Op->getOperand(0) == Op->getOperand(1)) {
5527 // TODO: Could filter out snan if we inspect the operand
5528 if (Op->getOpcode() == Instruction::FDiv) {
5529 // X / X is always exactly 1.0 or a NaN.
5531 } else {
5532 // X % X is always exactly [+-]0.0 or a NaN.
5533 Known.KnownFPClasses = fcNan | fcZero;
5534 }
5535
5536 break;
5537 }
5538
5539 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5540 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5541 const bool WantPositive =
5542 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5543 if (!WantNan && !WantNegative && !WantPositive)
5544 break;
5545
5546 KnownFPClass KnownLHS, KnownRHS;
5547
5548 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5549 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5550 Depth + 1);
5551
5552 bool KnowSomethingUseful =
5553 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5554
5555 if (KnowSomethingUseful || WantPositive) {
5556 const FPClassTest InterestedLHS =
5557 WantPositive ? fcAllFlags
5559
5560 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5561 InterestedClasses & InterestedLHS, KnownLHS, Q,
5562 Depth + 1);
5563 }
5564
5565 const Function *F = cast<Instruction>(Op)->getFunction();
5566 const fltSemantics &FltSem =
5567 Op->getType()->getScalarType()->getFltSemantics();
5568
5569 if (Op->getOpcode() == Instruction::FDiv) {
5570 // Only 0/0, Inf/Inf produce NaN.
5571 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5572 (KnownLHS.isKnownNeverInfinity() ||
5573 KnownRHS.isKnownNeverInfinity()) &&
5574 ((F &&
5575 KnownLHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) ||
5576 (F &&
5577 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))))) {
5578 Known.knownNot(fcNan);
5579 }
5580
5581 // X / -0.0 is -Inf (or NaN).
5582 // +X / +X is +X
5583 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5584 Known.knownNot(fcNegative);
5585 } else {
5586 // Inf REM x and x REM 0 produce NaN.
5587 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5588 KnownLHS.isKnownNeverInfinity() && F &&
5589 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5590 Known.knownNot(fcNan);
5591 }
5592
5593 // The sign for frem is the same as the first operand.
5594 if (KnownLHS.cannotBeOrderedLessThanZero())
5596 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5598
5599 // See if we can be more aggressive about the sign of 0.
5600 if (KnownLHS.isKnownNever(fcNegative))
5601 Known.knownNot(fcNegative);
5602 if (KnownLHS.isKnownNever(fcPositive))
5603 Known.knownNot(fcPositive);
5604 }
5605
5606 break;
5607 }
5608 case Instruction::FPExt: {
5609 // Infinity, nan and zero propagate from source.
5610 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5611 Known, Q, Depth + 1);
5612
5613 const fltSemantics &DstTy =
5614 Op->getType()->getScalarType()->getFltSemantics();
5615 const fltSemantics &SrcTy =
5616 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5617
5618 // All subnormal inputs should be in the normal range in the result type.
5619 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5620 if (Known.KnownFPClasses & fcPosSubnormal)
5621 Known.KnownFPClasses |= fcPosNormal;
5622 if (Known.KnownFPClasses & fcNegSubnormal)
5623 Known.KnownFPClasses |= fcNegNormal;
5624 Known.knownNot(fcSubnormal);
5625 }
5626
5627 // Sign bit of a nan isn't guaranteed.
5628 if (!Known.isKnownNeverNaN())
5629 Known.SignBit = std::nullopt;
5630 break;
5631 }
5632 case Instruction::FPTrunc: {
5633 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5634 Depth);
5635 break;
5636 }
5637 case Instruction::SIToFP:
5638 case Instruction::UIToFP: {
5639 // Cannot produce nan
5640 Known.knownNot(fcNan);
5641
5642 // Integers cannot be subnormal
5643 Known.knownNot(fcSubnormal);
5644
5645 // sitofp and uitofp turn into +0.0 for zero.
5646 Known.knownNot(fcNegZero);
5647 if (Op->getOpcode() == Instruction::UIToFP)
5648 Known.signBitMustBeZero();
5649
5650 if (InterestedClasses & fcInf) {
5651 // Get width of largest magnitude integer (remove a bit if signed).
5652 // This still works for a signed minimum value because the largest FP
5653 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5654 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5655 if (Op->getOpcode() == Instruction::SIToFP)
5656 --IntSize;
5657
5658 // If the exponent of the largest finite FP value can hold the largest
5659 // integer, the result of the cast must be finite.
5660 Type *FPTy = Op->getType()->getScalarType();
5661 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5662 Known.knownNot(fcInf);
5663 }
5664
5665 break;
5666 }
5667 case Instruction::ExtractElement: {
5668 // Look through extract element. If the index is non-constant or
5669 // out-of-range demand all elements, otherwise just the extracted element.
5670 const Value *Vec = Op->getOperand(0);
5671
5672 APInt DemandedVecElts;
5673 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5674 unsigned NumElts = VecTy->getNumElements();
5675 DemandedVecElts = APInt::getAllOnes(NumElts);
5676 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5677 if (CIdx && CIdx->getValue().ult(NumElts))
5678 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5679 } else {
5680 DemandedVecElts = APInt(1, 1);
5681 }
5682
5683 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5684 Q, Depth + 1);
5685 }
5686 case Instruction::InsertElement: {
5687 if (isa<ScalableVectorType>(Op->getType()))
5688 return;
5689
5690 const Value *Vec = Op->getOperand(0);
5691 const Value *Elt = Op->getOperand(1);
5692 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5693 unsigned NumElts = DemandedElts.getBitWidth();
5694 APInt DemandedVecElts = DemandedElts;
5695 bool NeedsElt = true;
5696 // If we know the index we are inserting to, clear it from Vec check.
5697 if (CIdx && CIdx->getValue().ult(NumElts)) {
5698 DemandedVecElts.clearBit(CIdx->getZExtValue());
5699 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5700 }
5701
5702 // Do we demand the inserted element?
5703 if (NeedsElt) {
5704 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5705 // If we don't know any bits, early out.
5706 if (Known.isUnknown())
5707 break;
5708 } else {
5709 Known.KnownFPClasses = fcNone;
5710 }
5711
5712 // Do we need anymore elements from Vec?
5713 if (!DemandedVecElts.isZero()) {
5714 KnownFPClass Known2;
5715 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5716 Depth + 1);
5717 Known |= Known2;
5718 }
5719
5720 break;
5721 }
5722 case Instruction::ShuffleVector: {
5723 // For undef elements, we don't know anything about the common state of
5724 // the shuffle result.
5725 APInt DemandedLHS, DemandedRHS;
5726 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5727 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5728 return;
5729
5730 if (!!DemandedLHS) {
5731 const Value *LHS = Shuf->getOperand(0);
5732 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5733 Depth + 1);
5734
5735 // If we don't know any bits, early out.
5736 if (Known.isUnknown())
5737 break;
5738 } else {
5739 Known.KnownFPClasses = fcNone;
5740 }
5741
5742 if (!!DemandedRHS) {
5743 KnownFPClass Known2;
5744 const Value *RHS = Shuf->getOperand(1);
5745 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5746 Depth + 1);
5747 Known |= Known2;
5748 }
5749
5750 break;
5751 }
5752 case Instruction::ExtractValue: {
5753 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5754 ArrayRef<unsigned> Indices = Extract->getIndices();
5755 const Value *Src = Extract->getAggregateOperand();
5756 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5757 Indices[0] == 0) {
5758 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5759 switch (II->getIntrinsicID()) {
5760 case Intrinsic::frexp: {
5761 Known.knownNot(fcSubnormal);
5762
5763 KnownFPClass KnownSrc;
5764 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5765 InterestedClasses, KnownSrc, Q, Depth + 1);
5766
5767 const Function *F = cast<Instruction>(Op)->getFunction();
5768 const fltSemantics &FltSem =
5769 Op->getType()->getScalarType()->getFltSemantics();
5770
5771 if (KnownSrc.isKnownNever(fcNegative))
5772 Known.knownNot(fcNegative);
5773 else {
5774 if (F &&
5775 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5776 Known.knownNot(fcNegZero);
5777 if (KnownSrc.isKnownNever(fcNegInf))
5778 Known.knownNot(fcNegInf);
5779 }
5780
5781 if (KnownSrc.isKnownNever(fcPositive))
5782 Known.knownNot(fcPositive);
5783 else {
5784 if (F &&
5785 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5786 Known.knownNot(fcPosZero);
5787 if (KnownSrc.isKnownNever(fcPosInf))
5788 Known.knownNot(fcPosInf);
5789 }
5790
5791 Known.propagateNaN(KnownSrc);
5792 return;
5793 }
5794 default:
5795 break;
5796 }
5797 }
5798 }
5799
5800 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
5801 Depth + 1);
5802 break;
5803 }
5804 case Instruction::PHI: {
5805 const PHINode *P = cast<PHINode>(Op);
5806 // Unreachable blocks may have zero-operand PHI nodes.
5807 if (P->getNumIncomingValues() == 0)
5808 break;
5809
5810 // Otherwise take the unions of the known bit sets of the operands,
5811 // taking conservative care to avoid excessive recursion.
5812 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5813
5814 if (Depth < PhiRecursionLimit) {
5815 // Skip if every incoming value references to ourself.
5816 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5817 break;
5818
5819 bool First = true;
5820
5821 for (const Use &U : P->operands()) {
5822 Value *IncValue;
5823 Instruction *CxtI;
5824 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
5825 // Skip direct self references.
5826 if (IncValue == P)
5827 continue;
5828
5829 KnownFPClass KnownSrc;
5830 // Recurse, but cap the recursion to two levels, because we don't want
5831 // to waste time spinning around in loops. We need at least depth 2 to
5832 // detect known sign bits.
5833 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
5835 PhiRecursionLimit);
5836
5837 if (First) {
5838 Known = KnownSrc;
5839 First = false;
5840 } else {
5841 Known |= KnownSrc;
5842 }
5843
5844 if (Known.KnownFPClasses == fcAllFlags)
5845 break;
5846 }
5847 }
5848
5849 break;
5850 }
5851 case Instruction::BitCast: {
5852 const Value *Src;
5853 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
5854 !Src->getType()->isIntOrIntVectorTy())
5855 break;
5856
5857 const Type *Ty = Op->getType()->getScalarType();
5858 KnownBits Bits(Ty->getScalarSizeInBits());
5859 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
5860
5861 // Transfer information from the sign bit.
5862 if (Bits.isNonNegative())
5863 Known.signBitMustBeZero();
5864 else if (Bits.isNegative())
5865 Known.signBitMustBeOne();
5866
5867 if (Ty->isIEEELikeFPTy()) {
5868 // IEEE floats are NaN when all bits of the exponent plus at least one of
5869 // the fraction bits are 1. This means:
5870 // - If we assume unknown bits are 0 and the value is NaN, it will
5871 // always be NaN
5872 // - If we assume unknown bits are 1 and the value is not NaN, it can
5873 // never be NaN
5874 // Note: They do not hold for x86_fp80 format.
5875 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
5876 Known.KnownFPClasses = fcNan;
5877 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
5878 Known.knownNot(fcNan);
5879
5880 // Build KnownBits representing Inf and check if it must be equal or
5881 // unequal to this value.
5882 auto InfKB = KnownBits::makeConstant(
5883 APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());
5884 InfKB.Zero.clearSignBit();
5885 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
5886 assert(!InfResult.value());
5887 Known.knownNot(fcInf);
5888 } else if (Bits == InfKB) {
5889 Known.KnownFPClasses = fcInf;
5890 }
5891
5892 // Build KnownBits representing Zero and check if it must be equal or
5893 // unequal to this value.
5894 auto ZeroKB = KnownBits::makeConstant(
5895 APFloat::getZero(Ty->getFltSemantics()).bitcastToAPInt());
5896 ZeroKB.Zero.clearSignBit();
5897 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
5898 assert(!ZeroResult.value());
5899 Known.knownNot(fcZero);
5900 } else if (Bits == ZeroKB) {
5901 Known.KnownFPClasses = fcZero;
5902 }
5903 }
5904
5905 break;
5906 }
5907 default:
5908 break;
5909 }
5910}
5911
5913 const APInt &DemandedElts,
5914 FPClassTest InterestedClasses,
5915 const SimplifyQuery &SQ,
5916 unsigned Depth) {
5917 KnownFPClass KnownClasses;
5918 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
5919 Depth);
5920 return KnownClasses;
5921}
5922
5924 FPClassTest InterestedClasses,
5925 const SimplifyQuery &SQ,
5926 unsigned Depth) {
5927 KnownFPClass Known;
5928 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
5929 return Known;
5930}
5931
5933 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
5934 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
5935 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
5936 return computeKnownFPClass(V, InterestedClasses,
5937 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
5938 Depth);
5939}
5940
5942llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5943 FastMathFlags FMF, FPClassTest InterestedClasses,
5944 const SimplifyQuery &SQ, unsigned Depth) {
5945 if (FMF.noNaNs())
5946 InterestedClasses &= ~fcNan;
5947 if (FMF.noInfs())
5948 InterestedClasses &= ~fcInf;
5949
5950 KnownFPClass Result =
5951 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
5952
5953 if (FMF.noNaNs())
5954 Result.KnownFPClasses &= ~fcNan;
5955 if (FMF.noInfs())
5956 Result.KnownFPClasses &= ~fcInf;
5957 return Result;
5958}
5959
5961 FPClassTest InterestedClasses,
5962 const SimplifyQuery &SQ,
5963 unsigned Depth) {
5964 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5965 APInt DemandedElts =
5966 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5967 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
5968 Depth);
5969}
5970
5972 unsigned Depth) {
5974 return Known.isKnownNeverNegZero();
5975}
5976
5983
5985 unsigned Depth) {
5987 return Known.isKnownNeverInfinity();
5988}
5989
5990/// Return true if the floating-point value can never contain a NaN or infinity.
5992 unsigned Depth) {
5994 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
5995}
5996
5997/// Return true if the floating-point scalar value is not a NaN or if the
5998/// floating-point vector value has no NaN elements. Return false if a value
5999/// could ever be NaN.
6001 unsigned Depth) {
6003 return Known.isKnownNeverNaN();
6004}
6005
6006/// Return false if we can prove that the specified FP value's sign bit is 0.
6007/// Return true if we can prove that the specified FP value's sign bit is 1.
6008/// Otherwise return std::nullopt.
6009std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
6010 const SimplifyQuery &SQ,
6011 unsigned Depth) {
6013 return Known.SignBit;
6014}
6015
6017 auto *User = cast<Instruction>(U.getUser());
6018 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6019 if (FPOp->hasNoSignedZeros())
6020 return true;
6021 }
6022
6023 switch (User->getOpcode()) {
6024 case Instruction::FPToSI:
6025 case Instruction::FPToUI:
6026 return true;
6027 case Instruction::FCmp:
6028 // fcmp treats both positive and negative zero as equal.
6029 return true;
6030 case Instruction::Call:
6031 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6032 switch (II->getIntrinsicID()) {
6033 case Intrinsic::fabs:
6034 return true;
6035 case Intrinsic::copysign:
6036 return U.getOperandNo() == 0;
6037 case Intrinsic::is_fpclass:
6038 case Intrinsic::vp_is_fpclass: {
6039 auto Test =
6040 static_cast<FPClassTest>(
6041 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6044 }
6045 default:
6046 return false;
6047 }
6048 }
6049 return false;
6050 default:
6051 return false;
6052 }
6053}
6054
6056 auto *User = cast<Instruction>(U.getUser());
6057 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6058 if (FPOp->hasNoNaNs())
6059 return true;
6060 }
6061
6062 switch (User->getOpcode()) {
6063 case Instruction::FPToSI:
6064 case Instruction::FPToUI:
6065 return true;
6066 // Proper FP math operations ignore the sign bit of NaN.
6067 case Instruction::FAdd:
6068 case Instruction::FSub:
6069 case Instruction::FMul:
6070 case Instruction::FDiv:
6071 case Instruction::FRem:
6072 case Instruction::FPTrunc:
6073 case Instruction::FPExt:
6074 case Instruction::FCmp:
6075 return true;
6076 // Bitwise FP operations should preserve the sign bit of NaN.
6077 case Instruction::FNeg:
6078 case Instruction::Select:
6079 case Instruction::PHI:
6080 return false;
6081 case Instruction::Ret:
6082 return User->getFunction()->getAttributes().getRetNoFPClass() &
6084 case Instruction::Call:
6085 case Instruction::Invoke: {
6086 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6087 switch (II->getIntrinsicID()) {
6088 case Intrinsic::fabs:
6089 return true;
6090 case Intrinsic::copysign:
6091 return U.getOperandNo() == 0;
6092 // Other proper FP math intrinsics ignore the sign bit of NaN.
6093 case Intrinsic::maxnum:
6094 case Intrinsic::minnum:
6095 case Intrinsic::maximum:
6096 case Intrinsic::minimum:
6097 case Intrinsic::maximumnum:
6098 case Intrinsic::minimumnum:
6099 case Intrinsic::canonicalize:
6100 case Intrinsic::fma:
6101 case Intrinsic::fmuladd:
6102 case Intrinsic::sqrt:
6103 case Intrinsic::pow:
6104 case Intrinsic::powi:
6105 case Intrinsic::fptoui_sat:
6106 case Intrinsic::fptosi_sat:
6107 case Intrinsic::is_fpclass:
6108 case Intrinsic::vp_is_fpclass:
6109 return true;
6110 default:
6111 return false;
6112 }
6113 }
6114
6115 FPClassTest NoFPClass =
6116 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6117 return NoFPClass & FPClassTest::fcNan;
6118 }
6119 default:
6120 return false;
6121 }
6122}
6123
6125
6126 // All byte-wide stores are splatable, even of arbitrary variables.
6127 if (V->getType()->isIntegerTy(8))
6128 return V;
6129
6130 LLVMContext &Ctx = V->getContext();
6131
6132 // Undef don't care.
6133 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6134 if (isa<UndefValue>(V))
6135 return UndefInt8;
6136
6137 // Return poison for zero-sized type.
6138 if (DL.getTypeStoreSize(V->getType()).isZero())
6139 return PoisonValue::get(Type::getInt8Ty(Ctx));
6140
6142 if (!C) {
6143 // Conceptually, we could handle things like:
6144 // %a = zext i8 %X to i16
6145 // %b = shl i16 %a, 8
6146 // %c = or i16 %a, %b
6147 // but until there is an example that actually needs this, it doesn't seem
6148 // worth worrying about.
6149 return nullptr;
6150 }
6151
6152 // Handle 'null' ConstantArrayZero etc.
6153 if (C->isNullValue())
6155
6156 // Constant floating-point values can be handled as integer values if the
6157 // corresponding integer value is "byteable". An important case is 0.0.
6158 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6159 Type *Ty = nullptr;
6160 if (CFP->getType()->isHalfTy())
6161 Ty = Type::getInt16Ty(Ctx);
6162 else if (CFP->getType()->isFloatTy())
6163 Ty = Type::getInt32Ty(Ctx);
6164 else if (CFP->getType()->isDoubleTy())
6165 Ty = Type::getInt64Ty(Ctx);
6166 // Don't handle long double formats, which have strange constraints.
6167 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6168 : nullptr;
6169 }
6170
6171 // We can handle constant integers that are multiple of 8 bits.
6172 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6173 if (CI->getBitWidth() % 8 == 0) {
6174 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6175 if (!CI->getValue().isSplat(8))
6176 return nullptr;
6177 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6178 }
6179 }
6180
6181 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6182 if (CE->getOpcode() == Instruction::IntToPtr) {
6183 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6184 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6186 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6187 return isBytewiseValue(Op, DL);
6188 }
6189 }
6190 }
6191
6192 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6193 if (LHS == RHS)
6194 return LHS;
6195 if (!LHS || !RHS)
6196 return nullptr;
6197 if (LHS == UndefInt8)
6198 return RHS;
6199 if (RHS == UndefInt8)
6200 return LHS;
6201 return nullptr;
6202 };
6203
6205 Value *Val = UndefInt8;
6206 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6207 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6208 return nullptr;
6209 return Val;
6210 }
6211
6213 Value *Val = UndefInt8;
6214 for (Value *Op : C->operands())
6215 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6216 return nullptr;
6217 return Val;
6218 }
6219
6220 // Don't try to handle the handful of other constants.
6221 return nullptr;
6222}
6223
6224// This is the recursive version of BuildSubAggregate. It takes a few different
6225// arguments. Idxs is the index within the nested struct From that we are
6226// looking at now (which is of type IndexedType). IdxSkip is the number of
6227// indices from Idxs that should be left out when inserting into the resulting
6228// struct. To is the result struct built so far, new insertvalue instructions
6229// build on that.
6230static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6232 unsigned IdxSkip,
6233 BasicBlock::iterator InsertBefore) {
6234 StructType *STy = dyn_cast<StructType>(IndexedType);
6235 if (STy) {
6236 // Save the original To argument so we can modify it
6237 Value *OrigTo = To;
6238 // General case, the type indexed by Idxs is a struct
6239 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6240 // Process each struct element recursively
6241 Idxs.push_back(i);
6242 Value *PrevTo = To;
6243 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6244 InsertBefore);
6245 Idxs.pop_back();
6246 if (!To) {
6247 // Couldn't find any inserted value for this index? Cleanup
6248 while (PrevTo != OrigTo) {
6250 PrevTo = Del->getAggregateOperand();
6251 Del->eraseFromParent();
6252 }
6253 // Stop processing elements
6254 break;
6255 }
6256 }
6257 // If we successfully found a value for each of our subaggregates
6258 if (To)
6259 return To;
6260 }
6261 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6262 // the struct's elements had a value that was inserted directly. In the latter
6263 // case, perhaps we can't determine each of the subelements individually, but
6264 // we might be able to find the complete struct somewhere.
6265
6266 // Find the value that is at that particular spot
6267 Value *V = FindInsertedValue(From, Idxs);
6268
6269 if (!V)
6270 return nullptr;
6271
6272 // Insert the value in the new (sub) aggregate
6273 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6274 InsertBefore);
6275}
6276
6277// This helper takes a nested struct and extracts a part of it (which is again a
6278// struct) into a new value. For example, given the struct:
6279// { a, { b, { c, d }, e } }
6280// and the indices "1, 1" this returns
6281// { c, d }.
6282//
6283// It does this by inserting an insertvalue for each element in the resulting
6284// struct, as opposed to just inserting a single struct. This will only work if
6285// each of the elements of the substruct are known (ie, inserted into From by an
6286// insertvalue instruction somewhere).
6287//
6288// All inserted insertvalue instructions are inserted before InsertBefore
6290 BasicBlock::iterator InsertBefore) {
6291 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6292 idx_range);
6293 Value *To = PoisonValue::get(IndexedType);
6294 SmallVector<unsigned, 10> Idxs(idx_range);
6295 unsigned IdxSkip = Idxs.size();
6296
6297 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6298}
6299
6300/// Given an aggregate and a sequence of indices, see if the scalar value
6301/// indexed is already around as a register, for example if it was inserted
6302/// directly into the aggregate.
6303///
6304/// If InsertBefore is not null, this function will duplicate (modified)
6305/// insertvalues when a part of a nested struct is extracted.
6306Value *
6308 std::optional<BasicBlock::iterator> InsertBefore) {
6309 // Nothing to index? Just return V then (this is useful at the end of our
6310 // recursion).
6311 if (idx_range.empty())
6312 return V;
6313 // We have indices, so V should have an indexable type.
6314 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6315 "Not looking at a struct or array?");
6316 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6317 "Invalid indices for type?");
6318
6319 if (Constant *C = dyn_cast<Constant>(V)) {
6320 C = C->getAggregateElement(idx_range[0]);
6321 if (!C) return nullptr;
6322 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6323 }
6324
6326 // Loop the indices for the insertvalue instruction in parallel with the
6327 // requested indices
6328 const unsigned *req_idx = idx_range.begin();
6329 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6330 i != e; ++i, ++req_idx) {
6331 if (req_idx == idx_range.end()) {
6332 // We can't handle this without inserting insertvalues
6333 if (!InsertBefore)
6334 return nullptr;
6335
6336 // The requested index identifies a part of a nested aggregate. Handle
6337 // this specially. For example,
6338 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6339 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6340 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6341 // This can be changed into
6342 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6343 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6344 // which allows the unused 0,0 element from the nested struct to be
6345 // removed.
6346 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6347 *InsertBefore);
6348 }
6349
6350 // This insert value inserts something else than what we are looking for.
6351 // See if the (aggregate) value inserted into has the value we are
6352 // looking for, then.
6353 if (*req_idx != *i)
6354 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6355 InsertBefore);
6356 }
6357 // If we end up here, the indices of the insertvalue match with those
6358 // requested (though possibly only partially). Now we recursively look at
6359 // the inserted value, passing any remaining indices.
6360 return FindInsertedValue(I->getInsertedValueOperand(),
6361 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6362 }
6363
6365 // If we're extracting a value from an aggregate that was extracted from
6366 // something else, we can extract from that something else directly instead.
6367 // However, we will need to chain I's indices with the requested indices.
6368
6369 // Calculate the number of indices required
6370 unsigned size = I->getNumIndices() + idx_range.size();
6371 // Allocate some space to put the new indices in
6373 Idxs.reserve(size);
6374 // Add indices from the extract value instruction
6375 Idxs.append(I->idx_begin(), I->idx_end());
6376
6377 // Add requested indices
6378 Idxs.append(idx_range.begin(), idx_range.end());
6379
6380 assert(Idxs.size() == size
6381 && "Number of indices added not correct?");
6382
6383 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6384 }
6385 // Otherwise, we don't know (such as, extracting from a function return value
6386 // or load instruction)
6387 return nullptr;
6388}
6389
6390// If V refers to an initialized global constant, set Slice either to
6391// its initializer if the size of its elements equals ElementSize, or,
6392// for ElementSize == 8, to its representation as an array of unsiged
6393// char. Return true on success.
6394// Offset is in the unit "nr of ElementSize sized elements".
6397 unsigned ElementSize, uint64_t Offset) {
6398 assert(V && "V should not be null.");
6399 assert((ElementSize % 8) == 0 &&
6400 "ElementSize expected to be a multiple of the size of a byte.");
6401 unsigned ElementSizeInBytes = ElementSize / 8;
6402
6403 // Drill down into the pointer expression V, ignoring any intervening
6404 // casts, and determine the identity of the object it references along
6405 // with the cumulative byte offset into it.
6406 const GlobalVariable *GV =
6408 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6409 // Fail if V is not based on constant global object.
6410 return false;
6411
6412 const DataLayout &DL = GV->getDataLayout();
6413 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6414
6415 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6416 /*AllowNonInbounds*/ true))
6417 // Fail if a constant offset could not be determined.
6418 return false;
6419
6420 uint64_t StartIdx = Off.getLimitedValue();
6421 if (StartIdx == UINT64_MAX)
6422 // Fail if the constant offset is excessive.
6423 return false;
6424
6425 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6426 // elements. Simply bail out if that isn't possible.
6427 if ((StartIdx % ElementSizeInBytes) != 0)
6428 return false;
6429
6430 Offset += StartIdx / ElementSizeInBytes;
6431 ConstantDataArray *Array = nullptr;
6432 ArrayType *ArrayTy = nullptr;
6433
6434 if (GV->getInitializer()->isNullValue()) {
6435 Type *GVTy = GV->getValueType();
6436 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6437 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6438
6439 Slice.Array = nullptr;
6440 Slice.Offset = 0;
6441 // Return an empty Slice for undersized constants to let callers
6442 // transform even undefined library calls into simpler, well-defined
6443 // expressions. This is preferable to making the calls although it
6444 // prevents sanitizers from detecting such calls.
6445 Slice.Length = Length < Offset ? 0 : Length - Offset;
6446 return true;
6447 }
6448
6449 auto *Init = const_cast<Constant *>(GV->getInitializer());
6450 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6451 Type *InitElTy = ArrayInit->getElementType();
6452 if (InitElTy->isIntegerTy(ElementSize)) {
6453 // If Init is an initializer for an array of the expected type
6454 // and size, use it as is.
6455 Array = ArrayInit;
6456 ArrayTy = ArrayInit->getType();
6457 }
6458 }
6459
6460 if (!Array) {
6461 if (ElementSize != 8)
6462 // TODO: Handle conversions to larger integral types.
6463 return false;
6464
6465 // Otherwise extract the portion of the initializer starting
6466 // at Offset as an array of bytes, and reset Offset.
6468 if (!Init)
6469 return false;
6470
6471 Offset = 0;
6473 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6474 }
6475
6476 uint64_t NumElts = ArrayTy->getArrayNumElements();
6477 if (Offset > NumElts)
6478 return false;
6479
6480 Slice.Array = Array;
6481 Slice.Offset = Offset;
6482 Slice.Length = NumElts - Offset;
6483 return true;
6484}
6485
6486/// Extract bytes from the initializer of the constant array V, which need
6487/// not be a nul-terminated string. On success, store the bytes in Str and
6488/// return true. When TrimAtNul is set, Str will contain only the bytes up
6489/// to but not including the first nul. Return false on failure.
6491 bool TrimAtNul) {
6493 if (!getConstantDataArrayInfo(V, Slice, 8))
6494 return false;
6495
6496 if (Slice.Array == nullptr) {
6497 if (TrimAtNul) {
6498 // Return a nul-terminated string even for an empty Slice. This is
6499 // safe because all existing SimplifyLibcalls callers require string
6500 // arguments and the behavior of the functions they fold is undefined
6501 // otherwise. Folding the calls this way is preferable to making
6502 // the undefined library calls, even though it prevents sanitizers
6503 // from reporting such calls.
6504 Str = StringRef();
6505 return true;
6506 }
6507 if (Slice.Length == 1) {
6508 Str = StringRef("", 1);
6509 return true;
6510 }
6511 // We cannot instantiate a StringRef as we do not have an appropriate string
6512 // of 0s at hand.
6513 return false;
6514 }
6515
6516 // Start out with the entire array in the StringRef.
6517 Str = Slice.Array->getAsString();
6518 // Skip over 'offset' bytes.
6519 Str = Str.substr(Slice.Offset);
6520
6521 if (TrimAtNul) {
6522 // Trim off the \0 and anything after it. If the array is not nul
6523 // terminated, we just return the whole end of string. The client may know
6524 // some other way that the string is length-bound.
6525 Str = Str.substr(0, Str.find('\0'));
6526 }
6527 return true;
6528}
6529
6530// These next two are very similar to the above, but also look through PHI
6531// nodes.
6532// TODO: See if we can integrate these two together.
6533
6534/// If we can compute the length of the string pointed to by
6535/// the specified pointer, return 'len+1'. If we can't, return 0.
6538 unsigned CharSize) {
6539 // Look through noop bitcast instructions.
6540 V = V->stripPointerCasts();
6541
6542 // If this is a PHI node, there are two cases: either we have already seen it
6543 // or we haven't.
6544 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6545 if (!PHIs.insert(PN).second)
6546 return ~0ULL; // already in the set.
6547
6548 // If it was new, see if all the input strings are the same length.
6549 uint64_t LenSoFar = ~0ULL;
6550 for (Value *IncValue : PN->incoming_values()) {
6551 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6552 if (Len == 0) return 0; // Unknown length -> unknown.
6553
6554 if (Len == ~0ULL) continue;
6555
6556 if (Len != LenSoFar && LenSoFar != ~0ULL)
6557 return 0; // Disagree -> unknown.
6558 LenSoFar = Len;
6559 }
6560
6561 // Success, all agree.
6562 return LenSoFar;
6563 }
6564
6565 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6566 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6567 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6568 if (Len1 == 0) return 0;
6569 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6570 if (Len2 == 0) return 0;
6571 if (Len1 == ~0ULL) return Len2;
6572 if (Len2 == ~0ULL) return Len1;
6573 if (Len1 != Len2) return 0;
6574 return Len1;
6575 }
6576
6577 // Otherwise, see if we can read the string.
6579 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6580 return 0;
6581
6582 if (Slice.Array == nullptr)
6583 // Zeroinitializer (including an empty one).
6584 return 1;
6585
6586 // Search for the first nul character. Return a conservative result even
6587 // when there is no nul. This is safe since otherwise the string function
6588 // being folded such as strlen is undefined, and can be preferable to
6589 // making the undefined library call.
6590 unsigned NullIndex = 0;
6591 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6592 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6593 break;
6594 }
6595
6596 return NullIndex + 1;
6597}
6598
6599/// If we can compute the length of the string pointed to by
6600/// the specified pointer, return 'len+1'. If we can't, return 0.
6601uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6602 if (!V->getType()->isPointerTy())
6603 return 0;
6604
6606 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6607 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6608 // an empty string as a length.
6609 return Len == ~0ULL ? 1 : Len;
6610}
6611
6612const Value *
6614 bool MustPreserveNullness) {
6615 assert(Call &&
6616 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6617 if (const Value *RV = Call->getReturnedArgOperand())
6618 return RV;
6619 // This can be used only as a aliasing property.
6621 Call, MustPreserveNullness))
6622 return Call->getArgOperand(0);
6623 return nullptr;
6624}
6625
6627 const CallBase *Call, bool MustPreserveNullness) {
6628 switch (Call->getIntrinsicID()) {
6629 case Intrinsic::launder_invariant_group:
6630 case Intrinsic::strip_invariant_group:
6631 case Intrinsic::aarch64_irg:
6632 case Intrinsic::aarch64_tagp:
6633 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6634 // input pointer (and thus preserve null-ness for the purposes of escape
6635 // analysis, which is where the MustPreserveNullness flag comes in to play).
6636 // However, it will not necessarily map ptr addrspace(N) null to ptr
6637 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6638 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6639 // list, no one should be relying on such a strict interpretation of
6640 // MustPreserveNullness (and, at time of writing, they are not), but we
6641 // document this fact out of an abundance of caution.
6642 case Intrinsic::amdgcn_make_buffer_rsrc:
6643 return true;
6644 case Intrinsic::ptrmask:
6645 return !MustPreserveNullness;
6646 case Intrinsic::threadlocal_address:
6647 // The underlying variable changes with thread ID. The Thread ID may change
6648 // at coroutine suspend points.
6649 return !Call->getParent()->getParent()->isPresplitCoroutine();
6650 default:
6651 return false;
6652 }
6653}
6654
6655/// \p PN defines a loop-variant pointer to an object. Check if the
6656/// previous iteration of the loop was referring to the same object as \p PN.
6658 const LoopInfo *LI) {
6659 // Find the loop-defined value.
6660 Loop *L = LI->getLoopFor(PN->getParent());
6661 if (PN->getNumIncomingValues() != 2)
6662 return true;
6663
6664 // Find the value from previous iteration.
6665 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6666 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6667 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6668 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6669 return true;
6670
6671 // If a new pointer is loaded in the loop, the pointer references a different
6672 // object in every iteration. E.g.:
6673 // for (i)
6674 // int *p = a[i];
6675 // ...
6676 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6677 if (!L->isLoopInvariant(Load->getPointerOperand()))
6678 return false;
6679 return true;
6680}
6681
6682const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6683 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6684 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6685 const Value *PtrOp = GEP->getPointerOperand();
6686 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6687 return V;
6688 V = PtrOp;
6689 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6690 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6691 Value *NewV = cast<Operator>(V)->getOperand(0);
6692 if (!NewV->getType()->isPointerTy())
6693 return V;
6694 V = NewV;
6695 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6696 if (GA->isInterposable())
6697 return V;
6698 V = GA->getAliasee();
6699 } else {
6700 if (auto *PHI = dyn_cast<PHINode>(V)) {
6701 // Look through single-arg phi nodes created by LCSSA.
6702 if (PHI->getNumIncomingValues() == 1) {
6703 V = PHI->getIncomingValue(0);
6704 continue;
6705 }
6706 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6707 // CaptureTracking can know about special capturing properties of some
6708 // intrinsics like launder.invariant.group, that can't be expressed with
6709 // the attributes, but have properties like returning aliasing pointer.
6710 // Because some analysis may assume that nocaptured pointer is not
6711 // returned from some special intrinsic (because function would have to
6712 // be marked with returns attribute), it is crucial to use this function
6713 // because it should be in sync with CaptureTracking. Not using it may
6714 // cause weird miscompilations where 2 aliasing pointers are assumed to
6715 // noalias.
6716 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6717 V = RP;
6718 continue;
6719 }
6720 }
6721
6722 return V;
6723 }
6724 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6725 }
6726 return V;
6727}
6728
6731 const LoopInfo *LI, unsigned MaxLookup) {
6734 Worklist.push_back(V);
6735 do {
6736 const Value *P = Worklist.pop_back_val();
6737 P = getUnderlyingObject(P, MaxLookup);
6738
6739 if (!Visited.insert(P).second)
6740 continue;
6741
6742 if (auto *SI = dyn_cast<SelectInst>(P)) {
6743 Worklist.push_back(SI->getTrueValue());
6744 Worklist.push_back(SI->getFalseValue());
6745 continue;
6746 }
6747
6748 if (auto *PN = dyn_cast<PHINode>(P)) {
6749 // If this PHI changes the underlying object in every iteration of the
6750 // loop, don't look through it. Consider:
6751 // int **A;
6752 // for (i) {
6753 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6754 // Curr = A[i];
6755 // *Prev, *Curr;
6756 //
6757 // Prev is tracking Curr one iteration behind so they refer to different
6758 // underlying objects.
6759 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6761 append_range(Worklist, PN->incoming_values());
6762 else
6763 Objects.push_back(P);
6764 continue;
6765 }
6766
6767 Objects.push_back(P);
6768 } while (!Worklist.empty());
6769}
6770
6772 const unsigned MaxVisited = 8;
6773
6776 Worklist.push_back(V);
6777 const Value *Object = nullptr;
6778 // Used as fallback if we can't find a common underlying object through
6779 // recursion.
6780 bool First = true;
6781 const Value *FirstObject = getUnderlyingObject(V);
6782 do {
6783 const Value *P = Worklist.pop_back_val();
6784 P = First ? FirstObject : getUnderlyingObject(P);
6785 First = false;
6786
6787 if (!Visited.insert(P).second)
6788 continue;
6789
6790 if (Visited.size() == MaxVisited)
6791 return FirstObject;
6792
6793 if (auto *SI = dyn_cast<SelectInst>(P)) {
6794 Worklist.push_back(SI->getTrueValue());
6795 Worklist.push_back(SI->getFalseValue());
6796 continue;
6797 }
6798
6799 if (auto *PN = dyn_cast<PHINode>(P)) {
6800 append_range(Worklist, PN->incoming_values());
6801 continue;
6802 }
6803
6804 if (!Object)
6805 Object = P;
6806 else if (Object != P)
6807 return FirstObject;
6808 } while (!Worklist.empty());
6809
6810 return Object ? Object : FirstObject;
6811}
6812
6813/// This is the function that does the work of looking through basic
6814/// ptrtoint+arithmetic+inttoptr sequences.
6815static const Value *getUnderlyingObjectFromInt(const Value *V) {
6816 do {
6817 if (const Operator *U = dyn_cast<Operator>(V)) {
6818 // If we find a ptrtoint, we can transfer control back to the
6819 // regular getUnderlyingObjectFromInt.
6820 if (U->getOpcode() == Instruction::PtrToInt)
6821 return U->getOperand(0);
6822 // If we find an add of a constant, a multiplied value, or a phi, it's
6823 // likely that the other operand will lead us to the base
6824 // object. We don't have to worry about the case where the
6825 // object address is somehow being computed by the multiply,
6826 // because our callers only care when the result is an
6827 // identifiable object.
6828 if (U->getOpcode() != Instruction::Add ||
6829 (!isa<ConstantInt>(U->getOperand(1)) &&
6830 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6831 !isa<PHINode>(U->getOperand(1))))
6832 return V;
6833 V = U->getOperand(0);
6834 } else {
6835 return V;
6836 }
6837 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6838 } while (true);
6839}
6840
6841/// This is a wrapper around getUnderlyingObjects and adds support for basic
6842/// ptrtoint+arithmetic+inttoptr sequences.
6843/// It returns false if unidentified object is found in getUnderlyingObjects.
6845 SmallVectorImpl<Value *> &Objects) {
6847 SmallVector<const Value *, 4> Working(1, V);
6848 do {
6849 V = Working.pop_back_val();
6850
6852 getUnderlyingObjects(V, Objs);
6853
6854 for (const Value *V : Objs) {
6855 if (!Visited.insert(V).second)
6856 continue;
6857 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6858 const Value *O =
6859 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6860 if (O->getType()->isPointerTy()) {
6861 Working.push_back(O);
6862 continue;
6863 }
6864 }
6865 // If getUnderlyingObjects fails to find an identifiable object,
6866 // getUnderlyingObjectsForCodeGen also fails for safety.
6867 if (!isIdentifiedObject(V)) {
6868 Objects.clear();
6869 return false;
6870 }
6871 Objects.push_back(const_cast<Value *>(V));
6872 }
6873 } while (!Working.empty());
6874 return true;
6875}
6876
6878 AllocaInst *Result = nullptr;
6880 SmallVector<Value *, 4> Worklist;
6881
6882 auto AddWork = [&](Value *V) {
6883 if (Visited.insert(V).second)
6884 Worklist.push_back(V);
6885 };
6886
6887 AddWork(V);
6888 do {
6889 V = Worklist.pop_back_val();
6890 assert(Visited.count(V));
6891
6892 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6893 if (Result && Result != AI)
6894 return nullptr;
6895 Result = AI;
6896 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
6897 AddWork(CI->getOperand(0));
6898 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
6899 for (Value *IncValue : PN->incoming_values())
6900 AddWork(IncValue);
6901 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
6902 AddWork(SI->getTrueValue());
6903 AddWork(SI->getFalseValue());
6905 if (OffsetZero && !GEP->hasAllZeroIndices())
6906 return nullptr;
6907 AddWork(GEP->getPointerOperand());
6908 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
6909 Value *Returned = CB->getReturnedArgOperand();
6910 if (Returned)
6911 AddWork(Returned);
6912 else
6913 return nullptr;
6914 } else {
6915 return nullptr;
6916 }
6917 } while (!Worklist.empty());
6918
6919 return Result;
6920}
6921
6923 const Value *V, bool AllowLifetime, bool AllowDroppable) {
6924 for (const User *U : V->users()) {
6926 if (!II)
6927 return false;
6928
6929 if (AllowLifetime && II->isLifetimeStartOrEnd())
6930 continue;
6931
6932 if (AllowDroppable && II->isDroppable())
6933 continue;
6934
6935 return false;
6936 }
6937 return true;
6938}
6939
6942 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
6943}
6946 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
6947}
6948
6950 if (auto *II = dyn_cast<IntrinsicInst>(I))
6951 return isTriviallyVectorizable(II->getIntrinsicID());
6952 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
6953 return (!Shuffle || Shuffle->isSelect()) &&
6955}
6956
6958 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
6959 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
6960 bool IgnoreUBImplyingAttrs) {
6961 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
6962 AC, DT, TLI, UseVariableInfo,
6963 IgnoreUBImplyingAttrs);
6964}
6965
6967 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
6968 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
6969 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
6970#ifndef NDEBUG
6971 if (Inst->getOpcode() != Opcode) {
6972 // Check that the operands are actually compatible with the Opcode override.
6973 auto hasEqualReturnAndLeadingOperandTypes =
6974 [](const Instruction *Inst, unsigned NumLeadingOperands) {
6975 if (Inst->getNumOperands() < NumLeadingOperands)
6976 return false;
6977 const Type *ExpectedType = Inst->getType();
6978 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
6979 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
6980 return false;
6981 return true;
6982 };
6984 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
6985 assert(!Instruction::isUnaryOp(Opcode) ||
6986 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
6987 }
6988#endif
6989
6990 switch (Opcode) {
6991 default:
6992 return true;
6993 case Instruction::UDiv:
6994 case Instruction::URem: {
6995 // x / y is undefined if y == 0.
6996 const APInt *V;
6997 if (match(Inst->getOperand(1), m_APInt(V)))
6998 return *V != 0;
6999 return false;
7000 }
7001 case Instruction::SDiv:
7002 case Instruction::SRem: {
7003 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7004 const APInt *Numerator, *Denominator;
7005 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7006 return false;
7007 // We cannot hoist this division if the denominator is 0.
7008 if (*Denominator == 0)
7009 return false;
7010 // It's safe to hoist if the denominator is not 0 or -1.
7011 if (!Denominator->isAllOnes())
7012 return true;
7013 // At this point we know that the denominator is -1. It is safe to hoist as
7014 // long we know that the numerator is not INT_MIN.
7015 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7016 return !Numerator->isMinSignedValue();
7017 // The numerator *might* be MinSignedValue.
7018 return false;
7019 }
7020 case Instruction::Load: {
7021 if (!UseVariableInfo)
7022 return false;
7023
7024 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7025 if (!LI)
7026 return false;
7027 if (mustSuppressSpeculation(*LI))
7028 return false;
7029 const DataLayout &DL = LI->getDataLayout();
7031 LI->getType(), LI->getAlign(), DL,
7032 CtxI, AC, DT, TLI);
7033 }
7034 case Instruction::Call: {
7035 auto *CI = dyn_cast<const CallInst>(Inst);
7036 if (!CI)
7037 return false;
7038 const Function *Callee = CI->getCalledFunction();
7039
7040 // The called function could have undefined behavior or side-effects, even
7041 // if marked readnone nounwind.
7042 if (!Callee || !Callee->isSpeculatable())
7043 return false;
7044 // Since the operands may be changed after hoisting, undefined behavior may
7045 // be triggered by some UB-implying attributes.
7046 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7047 }
7048 case Instruction::VAArg:
7049 case Instruction::Alloca:
7050 case Instruction::Invoke:
7051 case Instruction::CallBr:
7052 case Instruction::PHI:
7053 case Instruction::Store:
7054 case Instruction::Ret:
7055 case Instruction::Br:
7056 case Instruction::IndirectBr:
7057 case Instruction::Switch:
7058 case Instruction::Unreachable:
7059 case Instruction::Fence:
7060 case Instruction::AtomicRMW:
7061 case Instruction::AtomicCmpXchg:
7062 case Instruction::LandingPad:
7063 case Instruction::Resume:
7064 case Instruction::CatchSwitch:
7065 case Instruction::CatchPad:
7066 case Instruction::CatchRet:
7067 case Instruction::CleanupPad:
7068 case Instruction::CleanupRet:
7069 return false; // Misc instructions which have effects
7070 }
7071}
7072
7074 if (I.mayReadOrWriteMemory())
7075 // Memory dependency possible
7076 return true;
7078 // Can't move above a maythrow call or infinite loop. Or if an
7079 // inalloca alloca, above a stacksave call.
7080 return true;
7082 // 1) Can't reorder two inf-loop calls, even if readonly
7083 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7084 // safe to speculative execute. (Inverse of above)
7085 return true;
7086 return false;
7087}
7088
7089/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7103
7104/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7107 bool ForSigned,
7108 const SimplifyQuery &SQ) {
7109 ConstantRange CR1 =
7110 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7111 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7114 return CR1.intersectWith(CR2, RangeType);
7115}
7116
7118 const Value *RHS,
7119 const SimplifyQuery &SQ,
7120 bool IsNSW) {
7121 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7122 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7123
7124 // mul nsw of two non-negative numbers is also nuw.
7125 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
7127
7128 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
7129 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
7130 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7131}
7132
7134 const Value *RHS,
7135 const SimplifyQuery &SQ) {
7136 // Multiplying n * m significant bits yields a result of n + m significant
7137 // bits. If the total number of significant bits does not exceed the
7138 // result bit width (minus 1), there is no overflow.
7139 // This means if we have enough leading sign bits in the operands
7140 // we can guarantee that the result does not overflow.
7141 // Ref: "Hacker's Delight" by Henry Warren
7142 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7143
7144 // Note that underestimating the number of sign bits gives a more
7145 // conservative answer.
7146 unsigned SignBits =
7147 ::ComputeNumSignBits(LHS, SQ) + ::ComputeNumSignBits(RHS, SQ);
7148
7149 // First handle the easy case: if we have enough sign bits there's
7150 // definitely no overflow.
7151 if (SignBits > BitWidth + 1)
7153
7154 // There are two ambiguous cases where there can be no overflow:
7155 // SignBits == BitWidth + 1 and
7156 // SignBits == BitWidth
7157 // The second case is difficult to check, therefore we only handle the
7158 // first case.
7159 if (SignBits == BitWidth + 1) {
7160 // It overflows only when both arguments are negative and the true
7161 // product is exactly the minimum negative number.
7162 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7163 // For simplicity we just check if at least one side is not negative.
7164 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7165 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7166 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7168 }
7170}
7171
7174 const WithCache<const Value *> &RHS,
7175 const SimplifyQuery &SQ) {
7176 ConstantRange LHSRange =
7177 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7178 ConstantRange RHSRange =
7179 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7180 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7181}
7182
7183static OverflowResult
7186 const AddOperator *Add, const SimplifyQuery &SQ) {
7187 if (Add && Add->hasNoSignedWrap()) {
7189 }
7190
7191 // If LHS and RHS each have at least two sign bits, the addition will look
7192 // like
7193 //
7194 // XX..... +
7195 // YY.....
7196 //
7197 // If the carry into the most significant position is 0, X and Y can't both
7198 // be 1 and therefore the carry out of the addition is also 0.
7199 //
7200 // If the carry into the most significant position is 1, X and Y can't both
7201 // be 0 and therefore the carry out of the addition is also 1.
7202 //
7203 // Since the carry into the most significant position is always equal to
7204 // the carry out of the addition, there is no signed overflow.
7205 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7207
7208 ConstantRange LHSRange =
7209 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7210 ConstantRange RHSRange =
7211 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7212 OverflowResult OR =
7213 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7215 return OR;
7216
7217 // The remaining code needs Add to be available. Early returns if not so.
7218 if (!Add)
7220
7221 // If the sign of Add is the same as at least one of the operands, this add
7222 // CANNOT overflow. If this can be determined from the known bits of the
7223 // operands the above signedAddMayOverflow() check will have already done so.
7224 // The only other way to improve on the known bits is from an assumption, so
7225 // call computeKnownBitsFromContext() directly.
7226 bool LHSOrRHSKnownNonNegative =
7227 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7228 bool LHSOrRHSKnownNegative =
7229 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7230 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7231 KnownBits AddKnown(LHSRange.getBitWidth());
7232 computeKnownBitsFromContext(Add, AddKnown, SQ);
7233 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7234 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7236 }
7237
7239}
7240
7242 const Value *RHS,
7243 const SimplifyQuery &SQ) {
7244 // X - (X % ?)
7245 // The remainder of a value can't have greater magnitude than itself,
7246 // so the subtraction can't overflow.
7247
7248 // X - (X -nuw ?)
7249 // In the minimal case, this would simplify to "?", so there's no subtract
7250 // at all. But if this analysis is used to peek through casts, for example,
7251 // then determining no-overflow may allow other transforms.
7252
7253 // TODO: There are other patterns like this.
7254 // See simplifyICmpWithBinOpOnLHS() for candidates.
7255 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7256 match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
7257 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7259
7260 if (auto C = isImpliedByDomCondition(CmpInst::ICMP_UGE, LHS, RHS, SQ.CxtI,
7261 SQ.DL)) {
7262 if (*C)
7265 }
7266
7267 ConstantRange LHSRange =
7268 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7269 ConstantRange RHSRange =
7270 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7271 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7272}
7273
7275 const Value *RHS,
7276 const SimplifyQuery &SQ) {
7277 // X - (X % ?)
7278 // The remainder of a value can't have greater magnitude than itself,
7279 // so the subtraction can't overflow.
7280
7281 // X - (X -nsw ?)
7282 // In the minimal case, this would simplify to "?", so there's no subtract
7283 // at all. But if this analysis is used to peek through casts, for example,
7284 // then determining no-overflow may allow other transforms.
7285 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7286 match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
7287 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7289
7290 // If LHS and RHS each have at least two sign bits, the subtraction
7291 // cannot overflow.
7292 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7294
7295 ConstantRange LHSRange =
7296 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7297 ConstantRange RHSRange =
7298 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7299 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7300}
7301
7303 const DominatorTree &DT) {
7304 SmallVector<const BranchInst *, 2> GuardingBranches;
7306
7307 for (const User *U : WO->users()) {
7308 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7309 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7310
7311 if (EVI->getIndices()[0] == 0)
7312 Results.push_back(EVI);
7313 else {
7314 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7315
7316 for (const auto *U : EVI->users())
7317 if (const auto *B = dyn_cast<BranchInst>(U)) {
7318 assert(B->isConditional() && "How else is it using an i1?");
7319 GuardingBranches.push_back(B);
7320 }
7321 }
7322 } else {
7323 // We are using the aggregate directly in a way we don't want to analyze
7324 // here (storing it to a global, say).
7325 return false;
7326 }
7327 }
7328
7329 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7330 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7331 if (!NoWrapEdge.isSingleEdge())
7332 return false;
7333
7334 // Check if all users of the add are provably no-wrap.
7335 for (const auto *Result : Results) {
7336 // If the extractvalue itself is not executed on overflow, the we don't
7337 // need to check each use separately, since domination is transitive.
7338 if (DT.dominates(NoWrapEdge, Result->getParent()))
7339 continue;
7340
7341 for (const auto &RU : Result->uses())
7342 if (!DT.dominates(NoWrapEdge, RU))
7343 return false;
7344 }
7345
7346 return true;
7347 };
7348
7349 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7350}
7351
7352/// Shifts return poison if shiftwidth is larger than the bitwidth.
7353static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7354 auto *C = dyn_cast<Constant>(ShiftAmount);
7355 if (!C)
7356 return false;
7357
7358 // Shifts return poison if shiftwidth is larger than the bitwidth.
7360 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7361 unsigned NumElts = FVTy->getNumElements();
7362 for (unsigned i = 0; i < NumElts; ++i)
7363 ShiftAmounts.push_back(C->getAggregateElement(i));
7364 } else if (isa<ScalableVectorType>(C->getType()))
7365 return false; // Can't tell, just return false to be safe
7366 else
7367 ShiftAmounts.push_back(C);
7368
7369 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7370 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7371 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7372 });
7373
7374 return Safe;
7375}
7376
7382
7384 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7385}
7386
7388 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7389}
7390
7392 bool ConsiderFlagsAndMetadata) {
7393
7394 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7395 Op->hasPoisonGeneratingAnnotations())
7396 return true;
7397
7398 unsigned Opcode = Op->getOpcode();
7399
7400 // Check whether opcode is a poison/undef-generating operation
7401 switch (Opcode) {
7402 case Instruction::Shl:
7403 case Instruction::AShr:
7404 case Instruction::LShr:
7405 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7406 case Instruction::FPToSI:
7407 case Instruction::FPToUI:
7408 // fptosi/ui yields poison if the resulting value does not fit in the
7409 // destination type.
7410 return true;
7411 case Instruction::Call:
7412 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7413 switch (II->getIntrinsicID()) {
7414 // TODO: Add more intrinsics.
7415 case Intrinsic::ctlz:
7416 case Intrinsic::cttz:
7417 case Intrinsic::abs:
7418 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7419 return false;
7420 break;
7421 case Intrinsic::ctpop:
7422 case Intrinsic::bswap:
7423 case Intrinsic::bitreverse:
7424 case Intrinsic::fshl:
7425 case Intrinsic::fshr:
7426 case Intrinsic::smax:
7427 case Intrinsic::smin:
7428 case Intrinsic::scmp:
7429 case Intrinsic::umax:
7430 case Intrinsic::umin:
7431 case Intrinsic::ucmp:
7432 case Intrinsic::ptrmask:
7433 case Intrinsic::fptoui_sat:
7434 case Intrinsic::fptosi_sat:
7435 case Intrinsic::sadd_with_overflow:
7436 case Intrinsic::ssub_with_overflow:
7437 case Intrinsic::smul_with_overflow:
7438 case Intrinsic::uadd_with_overflow:
7439 case Intrinsic::usub_with_overflow:
7440 case Intrinsic::umul_with_overflow:
7441 case Intrinsic::sadd_sat:
7442 case Intrinsic::uadd_sat:
7443 case Intrinsic::ssub_sat:
7444 case Intrinsic::usub_sat:
7445 return false;
7446 case Intrinsic::sshl_sat:
7447 case Intrinsic::ushl_sat:
7448 return includesPoison(Kind) &&
7449 !shiftAmountKnownInRange(II->getArgOperand(1));
7450 case Intrinsic::fma:
7451 case Intrinsic::fmuladd:
7452 case Intrinsic::sqrt:
7453 case Intrinsic::powi:
7454 case Intrinsic::sin:
7455 case Intrinsic::cos:
7456 case Intrinsic::pow:
7457 case Intrinsic::log:
7458 case Intrinsic::log10:
7459 case Intrinsic::log2:
7460 case Intrinsic::exp:
7461 case Intrinsic::exp2:
7462 case Intrinsic::exp10:
7463 case Intrinsic::fabs:
7464 case Intrinsic::copysign:
7465 case Intrinsic::floor:
7466 case Intrinsic::ceil:
7467 case Intrinsic::trunc:
7468 case Intrinsic::rint:
7469 case Intrinsic::nearbyint:
7470 case Intrinsic::round:
7471 case Intrinsic::roundeven:
7472 case Intrinsic::fptrunc_round:
7473 case Intrinsic::canonicalize:
7474 case Intrinsic::arithmetic_fence:
7475 case Intrinsic::minnum:
7476 case Intrinsic::maxnum:
7477 case Intrinsic::minimum:
7478 case Intrinsic::maximum:
7479 case Intrinsic::minimumnum:
7480 case Intrinsic::maximumnum:
7481 case Intrinsic::is_fpclass:
7482 case Intrinsic::ldexp:
7483 case Intrinsic::frexp:
7484 return false;
7485 case Intrinsic::lround:
7486 case Intrinsic::llround:
7487 case Intrinsic::lrint:
7488 case Intrinsic::llrint:
7489 // If the value doesn't fit an unspecified value is returned (but this
7490 // is not poison).
7491 return false;
7492 }
7493 }
7494 [[fallthrough]];
7495 case Instruction::CallBr:
7496 case Instruction::Invoke: {
7497 const auto *CB = cast<CallBase>(Op);
7498 return !CB->hasRetAttr(Attribute::NoUndef);
7499 }
7500 case Instruction::InsertElement:
7501 case Instruction::ExtractElement: {
7502 // If index exceeds the length of the vector, it returns poison
7503 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7504 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7505 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7506 if (includesPoison(Kind))
7507 return !Idx ||
7508 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7509 return false;
7510 }
7511 case Instruction::ShuffleVector: {
7513 ? cast<ConstantExpr>(Op)->getShuffleMask()
7514 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7515 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7516 }
7517 case Instruction::FNeg:
7518 case Instruction::PHI:
7519 case Instruction::Select:
7520 case Instruction::ExtractValue:
7521 case Instruction::InsertValue:
7522 case Instruction::Freeze:
7523 case Instruction::ICmp:
7524 case Instruction::FCmp:
7525 case Instruction::GetElementPtr:
7526 return false;
7527 case Instruction::AddrSpaceCast:
7528 return true;
7529 default: {
7530 const auto *CE = dyn_cast<ConstantExpr>(Op);
7531 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7532 return false;
7533 else if (Instruction::isBinaryOp(Opcode))
7534 return false;
7535 // Be conservative and return true.
7536 return true;
7537 }
7538 }
7539}
7540
7542 bool ConsiderFlagsAndMetadata) {
7543 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7544 ConsiderFlagsAndMetadata);
7545}
7546
7547bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7548 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7549 ConsiderFlagsAndMetadata);
7550}
7551
7552static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7553 unsigned Depth) {
7554 if (ValAssumedPoison == V)
7555 return true;
7556
7557 const unsigned MaxDepth = 2;
7558 if (Depth >= MaxDepth)
7559 return false;
7560
7561 if (const auto *I = dyn_cast<Instruction>(V)) {
7562 if (any_of(I->operands(), [=](const Use &Op) {
7563 return propagatesPoison(Op) &&
7564 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7565 }))
7566 return true;
7567
7568 // V = extractvalue V0, idx
7569 // V2 = extractvalue V0, idx2
7570 // V0's elements are all poison or not. (e.g., add_with_overflow)
7571 const WithOverflowInst *II;
7573 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7574 llvm::is_contained(II->args(), ValAssumedPoison)))
7575 return true;
7576 }
7577 return false;
7578}
7579
7580static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7581 unsigned Depth) {
7582 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7583 return true;
7584
7585 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7586 return true;
7587
7588 const unsigned MaxDepth = 2;
7589 if (Depth >= MaxDepth)
7590 return false;
7591
7592 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7593 if (I && !canCreatePoison(cast<Operator>(I))) {
7594 return all_of(I->operands(), [=](const Value *Op) {
7595 return impliesPoison(Op, V, Depth + 1);
7596 });
7597 }
7598 return false;
7599}
7600
7601bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7602 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7603}
7604
7605static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7606
7608 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7609 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7611 return false;
7612
7613 if (isa<MetadataAsValue>(V))
7614 return false;
7615
7616 if (const auto *A = dyn_cast<Argument>(V)) {
7617 if (A->hasAttribute(Attribute::NoUndef) ||
7618 A->hasAttribute(Attribute::Dereferenceable) ||
7619 A->hasAttribute(Attribute::DereferenceableOrNull))
7620 return true;
7621 }
7622
7623 if (auto *C = dyn_cast<Constant>(V)) {
7624 if (isa<PoisonValue>(C))
7625 return !includesPoison(Kind);
7626
7627 if (isa<UndefValue>(C))
7628 return !includesUndef(Kind);
7629
7632 return true;
7633
7634 if (C->getType()->isVectorTy()) {
7635 if (isa<ConstantExpr>(C)) {
7636 // Scalable vectors can use a ConstantExpr to build a splat.
7637 if (Constant *SplatC = C->getSplatValue())
7638 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7639 return true;
7640 } else {
7641 if (includesUndef(Kind) && C->containsUndefElement())
7642 return false;
7643 if (includesPoison(Kind) && C->containsPoisonElement())
7644 return false;
7645 return !C->containsConstantExpression();
7646 }
7647 }
7648 }
7649
7650 // Strip cast operations from a pointer value.
7651 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7652 // inbounds with zero offset. To guarantee that the result isn't poison, the
7653 // stripped pointer is checked as it has to be pointing into an allocated
7654 // object or be null `null` to ensure `inbounds` getelement pointers with a
7655 // zero offset could not produce poison.
7656 // It can strip off addrspacecast that do not change bit representation as
7657 // well. We believe that such addrspacecast is equivalent to no-op.
7658 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7659 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7660 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7661 return true;
7662
7663 auto OpCheck = [&](const Value *V) {
7664 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7665 };
7666
7667 if (auto *Opr = dyn_cast<Operator>(V)) {
7668 // If the value is a freeze instruction, then it can never
7669 // be undef or poison.
7670 if (isa<FreezeInst>(V))
7671 return true;
7672
7673 if (const auto *CB = dyn_cast<CallBase>(V)) {
7674 if (CB->hasRetAttr(Attribute::NoUndef) ||
7675 CB->hasRetAttr(Attribute::Dereferenceable) ||
7676 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7677 return true;
7678 }
7679
7680 if (!::canCreateUndefOrPoison(Opr, Kind,
7681 /*ConsiderFlagsAndMetadata=*/true)) {
7682 if (const auto *PN = dyn_cast<PHINode>(V)) {
7683 unsigned Num = PN->getNumIncomingValues();
7684 bool IsWellDefined = true;
7685 for (unsigned i = 0; i < Num; ++i) {
7686 if (PN == PN->getIncomingValue(i))
7687 continue;
7688 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7689 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7690 DT, Depth + 1, Kind)) {
7691 IsWellDefined = false;
7692 break;
7693 }
7694 }
7695 if (IsWellDefined)
7696 return true;
7697 } else if (all_of(Opr->operands(), OpCheck))
7698 return true;
7699 }
7700 }
7701
7702 if (auto *I = dyn_cast<LoadInst>(V))
7703 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7704 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7705 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7706 return true;
7707
7709 return true;
7710
7711 // CxtI may be null or a cloned instruction.
7712 if (!CtxI || !CtxI->getParent() || !DT)
7713 return false;
7714
7715 auto *DNode = DT->getNode(CtxI->getParent());
7716 if (!DNode)
7717 // Unreachable block
7718 return false;
7719
7720 // If V is used as a branch condition before reaching CtxI, V cannot be
7721 // undef or poison.
7722 // br V, BB1, BB2
7723 // BB1:
7724 // CtxI ; V cannot be undef or poison here
7725 auto *Dominator = DNode->getIDom();
7726 // This check is purely for compile time reasons: we can skip the IDom walk
7727 // if what we are checking for includes undef and the value is not an integer.
7728 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7729 while (Dominator) {
7730 auto *TI = Dominator->getBlock()->getTerminator();
7731
7732 Value *Cond = nullptr;
7733 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7734 if (BI->isConditional())
7735 Cond = BI->getCondition();
7736 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7737 Cond = SI->getCondition();
7738 }
7739
7740 if (Cond) {
7741 if (Cond == V)
7742 return true;
7743 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7744 // For poison, we can analyze further
7745 auto *Opr = cast<Operator>(Cond);
7746 if (any_of(Opr->operands(), [V](const Use &U) {
7747 return V == U && propagatesPoison(U);
7748 }))
7749 return true;
7750 }
7751 }
7752
7753 Dominator = Dominator->getIDom();
7754 }
7755
7756 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7757 return true;
7758
7759 return false;
7760}
7761
7763 const Instruction *CtxI,
7764 const DominatorTree *DT,
7765 unsigned Depth) {
7766 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7768}
7769
7771 const Instruction *CtxI,
7772 const DominatorTree *DT, unsigned Depth) {
7773 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7775}
7776
7778 const Instruction *CtxI,
7779 const DominatorTree *DT, unsigned Depth) {
7780 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7782}
7783
7784/// Return true if undefined behavior would provably be executed on the path to
7785/// OnPathTo if Root produced a posion result. Note that this doesn't say
7786/// anything about whether OnPathTo is actually executed or whether Root is
7787/// actually poison. This can be used to assess whether a new use of Root can
7788/// be added at a location which is control equivalent with OnPathTo (such as
7789/// immediately before it) without introducing UB which didn't previously
7790/// exist. Note that a false result conveys no information.
7792 Instruction *OnPathTo,
7793 DominatorTree *DT) {
7794 // Basic approach is to assume Root is poison, propagate poison forward
7795 // through all users we can easily track, and then check whether any of those
7796 // users are provable UB and must execute before out exiting block might
7797 // exit.
7798
7799 // The set of all recursive users we've visited (which are assumed to all be
7800 // poison because of said visit)
7803 Worklist.push_back(Root);
7804 while (!Worklist.empty()) {
7805 const Instruction *I = Worklist.pop_back_val();
7806
7807 // If we know this must trigger UB on a path leading our target.
7808 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7809 return true;
7810
7811 // If we can't analyze propagation through this instruction, just skip it
7812 // and transitive users. Safe as false is a conservative result.
7813 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7814 return KnownPoison.contains(U) && propagatesPoison(U);
7815 }))
7816 continue;
7817
7818 if (KnownPoison.insert(I).second)
7819 for (const User *User : I->users())
7820 Worklist.push_back(cast<Instruction>(User));
7821 }
7822
7823 // Might be non-UB, or might have a path we couldn't prove must execute on
7824 // way to exiting bb.
7825 return false;
7826}
7827
7829 const SimplifyQuery &SQ) {
7830 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7831 Add, SQ);
7832}
7833
7836 const WithCache<const Value *> &RHS,
7837 const SimplifyQuery &SQ) {
7838 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7839}
7840
7842 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7843 // of time because it's possible for another thread to interfere with it for an
7844 // arbitrary length of time, but programs aren't allowed to rely on that.
7845
7846 // If there is no successor, then execution can't transfer to it.
7847 if (isa<ReturnInst>(I))
7848 return false;
7850 return false;
7851
7852 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7853 // Instruction::willReturn.
7854 //
7855 // FIXME: Move this check into Instruction::willReturn.
7856 if (isa<CatchPadInst>(I)) {
7857 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7858 default:
7859 // A catchpad may invoke exception object constructors and such, which
7860 // in some languages can be arbitrary code, so be conservative by default.
7861 return false;
7863 // For CoreCLR, it just involves a type test.
7864 return true;
7865 }
7866 }
7867
7868 // An instruction that returns without throwing must transfer control flow
7869 // to a successor.
7870 return !I->mayThrow() && I->willReturn();
7871}
7872
7874 // TODO: This is slightly conservative for invoke instruction since exiting
7875 // via an exception *is* normal control for them.
7876 for (const Instruction &I : *BB)
7878 return false;
7879 return true;
7880}
7881
7888
7891 assert(ScanLimit && "scan limit must be non-zero");
7892 for (const Instruction &I : Range) {
7893 if (--ScanLimit == 0)
7894 return false;
7896 return false;
7897 }
7898 return true;
7899}
7900
7902 const Loop *L) {
7903 // The loop header is guaranteed to be executed for every iteration.
7904 //
7905 // FIXME: Relax this constraint to cover all basic blocks that are
7906 // guaranteed to be executed at every iteration.
7907 if (I->getParent() != L->getHeader()) return false;
7908
7909 for (const Instruction &LI : *L->getHeader()) {
7910 if (&LI == I) return true;
7911 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7912 }
7913 llvm_unreachable("Instruction not contained in its own parent basic block.");
7914}
7915
7917 switch (IID) {
7918 // TODO: Add more intrinsics.
7919 case Intrinsic::sadd_with_overflow:
7920 case Intrinsic::ssub_with_overflow:
7921 case Intrinsic::smul_with_overflow:
7922 case Intrinsic::uadd_with_overflow:
7923 case Intrinsic::usub_with_overflow:
7924 case Intrinsic::umul_with_overflow:
7925 // If an input is a vector containing a poison element, the
7926 // two output vectors (calculated results, overflow bits)'
7927 // corresponding lanes are poison.
7928 return true;
7929 case Intrinsic::ctpop:
7930 case Intrinsic::ctlz:
7931 case Intrinsic::cttz:
7932 case Intrinsic::abs:
7933 case Intrinsic::smax:
7934 case Intrinsic::smin:
7935 case Intrinsic::umax:
7936 case Intrinsic::umin:
7937 case Intrinsic::scmp:
7938 case Intrinsic::is_fpclass:
7939 case Intrinsic::ptrmask:
7940 case Intrinsic::ucmp:
7941 case Intrinsic::bitreverse:
7942 case Intrinsic::bswap:
7943 case Intrinsic::sadd_sat:
7944 case Intrinsic::ssub_sat:
7945 case Intrinsic::sshl_sat:
7946 case Intrinsic::uadd_sat:
7947 case Intrinsic::usub_sat:
7948 case Intrinsic::ushl_sat:
7949 case Intrinsic::smul_fix:
7950 case Intrinsic::smul_fix_sat:
7951 case Intrinsic::umul_fix:
7952 case Intrinsic::umul_fix_sat:
7953 case Intrinsic::pow:
7954 case Intrinsic::powi:
7955 case Intrinsic::sin:
7956 case Intrinsic::sinh:
7957 case Intrinsic::cos:
7958 case Intrinsic::cosh:
7959 case Intrinsic::sincos:
7960 case Intrinsic::sincospi:
7961 case Intrinsic::tan:
7962 case Intrinsic::tanh:
7963 case Intrinsic::asin:
7964 case Intrinsic::acos:
7965 case Intrinsic::atan:
7966 case Intrinsic::atan2:
7967 case Intrinsic::canonicalize:
7968 case Intrinsic::sqrt:
7969 case Intrinsic::exp:
7970 case Intrinsic::exp2:
7971 case Intrinsic::exp10:
7972 case Intrinsic::log:
7973 case Intrinsic::log2:
7974 case Intrinsic::log10:
7975 case Intrinsic::modf:
7976 case Intrinsic::floor:
7977 case Intrinsic::ceil:
7978 case Intrinsic::trunc:
7979 case Intrinsic::rint:
7980 case Intrinsic::nearbyint:
7981 case Intrinsic::round:
7982 case Intrinsic::roundeven:
7983 case Intrinsic::lrint:
7984 case Intrinsic::llrint:
7985 return true;
7986 default:
7987 return false;
7988 }
7989}
7990
7991bool llvm::propagatesPoison(const Use &PoisonOp) {
7992 const Operator *I = cast<Operator>(PoisonOp.getUser());
7993 switch (I->getOpcode()) {
7994 case Instruction::Freeze:
7995 case Instruction::PHI:
7996 case Instruction::Invoke:
7997 return false;
7998 case Instruction::Select:
7999 return PoisonOp.getOperandNo() == 0;
8000 case Instruction::Call:
8001 if (auto *II = dyn_cast<IntrinsicInst>(I))
8002 return intrinsicPropagatesPoison(II->getIntrinsicID());
8003 return false;
8004 case Instruction::ICmp:
8005 case Instruction::FCmp:
8006 case Instruction::GetElementPtr:
8007 return true;
8008 default:
8010 return true;
8011
8012 // Be conservative and return false.
8013 return false;
8014 }
8015}
8016
8017/// Enumerates all operands of \p I that are guaranteed to not be undef or
8018/// poison. If the callback \p Handle returns true, stop processing and return
8019/// true. Otherwise, return false.
8020template <typename CallableT>
8022 const CallableT &Handle) {
8023 switch (I->getOpcode()) {
8024 case Instruction::Store:
8025 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8026 return true;
8027 break;
8028
8029 case Instruction::Load:
8030 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8031 return true;
8032 break;
8033
8034 // Since dereferenceable attribute imply noundef, atomic operations
8035 // also implicitly have noundef pointers too
8036 case Instruction::AtomicCmpXchg:
8038 return true;
8039 break;
8040
8041 case Instruction::AtomicRMW:
8042 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8043 return true;
8044 break;
8045
8046 case Instruction::Call:
8047 case Instruction::Invoke: {
8048 const CallBase *CB = cast<CallBase>(I);
8049 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8050 return true;
8051 for (unsigned i = 0; i < CB->arg_size(); ++i)
8052 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8053 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8054 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8055 Handle(CB->getArgOperand(i)))
8056 return true;
8057 break;
8058 }
8059 case Instruction::Ret:
8060 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8061 Handle(I->getOperand(0)))
8062 return true;
8063 break;
8064 case Instruction::Switch:
8065 if (Handle(cast<SwitchInst>(I)->getCondition()))
8066 return true;
8067 break;
8068 case Instruction::Br: {
8069 auto *BR = cast<BranchInst>(I);
8070 if (BR->isConditional() && Handle(BR->getCondition()))
8071 return true;
8072 break;
8073 }
8074 default:
8075 break;
8076 }
8077
8078 return false;
8079}
8080
8081/// Enumerates all operands of \p I that are guaranteed to not be poison.
8082template <typename CallableT>
8084 const CallableT &Handle) {
8085 if (handleGuaranteedWellDefinedOps(I, Handle))
8086 return true;
8087 switch (I->getOpcode()) {
8088 // Divisors of these operations are allowed to be partially undef.
8089 case Instruction::UDiv:
8090 case Instruction::SDiv:
8091 case Instruction::URem:
8092 case Instruction::SRem:
8093 return Handle(I->getOperand(1));
8094 default:
8095 return false;
8096 }
8097}
8098
8100 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8102 I, [&](const Value *V) { return KnownPoison.count(V); });
8103}
8104
8106 bool PoisonOnly) {
8107 // We currently only look for uses of values within the same basic
8108 // block, as that makes it easier to guarantee that the uses will be
8109 // executed given that Inst is executed.
8110 //
8111 // FIXME: Expand this to consider uses beyond the same basic block. To do
8112 // this, look out for the distinction between post-dominance and strong
8113 // post-dominance.
8114 const BasicBlock *BB = nullptr;
8116 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8117 BB = Inst->getParent();
8118 Begin = Inst->getIterator();
8119 Begin++;
8120 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8121 if (Arg->getParent()->isDeclaration())
8122 return false;
8123 BB = &Arg->getParent()->getEntryBlock();
8124 Begin = BB->begin();
8125 } else {
8126 return false;
8127 }
8128
8129 // Limit number of instructions we look at, to avoid scanning through large
8130 // blocks. The current limit is chosen arbitrarily.
8131 unsigned ScanLimit = 32;
8132 BasicBlock::const_iterator End = BB->end();
8133
8134 if (!PoisonOnly) {
8135 // Since undef does not propagate eagerly, be conservative & just check
8136 // whether a value is directly passed to an instruction that must take
8137 // well-defined operands.
8138
8139 for (const auto &I : make_range(Begin, End)) {
8140 if (--ScanLimit == 0)
8141 break;
8142
8143 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8144 return WellDefinedOp == V;
8145 }))
8146 return true;
8147
8149 break;
8150 }
8151 return false;
8152 }
8153
8154 // Set of instructions that we have proved will yield poison if Inst
8155 // does.
8156 SmallPtrSet<const Value *, 16> YieldsPoison;
8158
8159 YieldsPoison.insert(V);
8160 Visited.insert(BB);
8161
8162 while (true) {
8163 for (const auto &I : make_range(Begin, End)) {
8164 if (--ScanLimit == 0)
8165 return false;
8166 if (mustTriggerUB(&I, YieldsPoison))
8167 return true;
8169 return false;
8170
8171 // If an operand is poison and propagates it, mark I as yielding poison.
8172 for (const Use &Op : I.operands()) {
8173 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8174 YieldsPoison.insert(&I);
8175 break;
8176 }
8177 }
8178
8179 // Special handling for select, which returns poison if its operand 0 is
8180 // poison (handled in the loop above) *or* if both its true/false operands
8181 // are poison (handled here).
8182 if (I.getOpcode() == Instruction::Select &&
8183 YieldsPoison.count(I.getOperand(1)) &&
8184 YieldsPoison.count(I.getOperand(2))) {
8185 YieldsPoison.insert(&I);
8186 }
8187 }
8188
8189 BB = BB->getSingleSuccessor();
8190 if (!BB || !Visited.insert(BB).second)
8191 break;
8192
8193 Begin = BB->getFirstNonPHIIt();
8194 End = BB->end();
8195 }
8196 return false;
8197}
8198
8200 return ::programUndefinedIfUndefOrPoison(Inst, false);
8201}
8202
8204 return ::programUndefinedIfUndefOrPoison(Inst, true);
8205}
8206
8207static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8208 if (FMF.noNaNs())
8209 return true;
8210
8211 if (auto *C = dyn_cast<ConstantFP>(V))
8212 return !C->isNaN();
8213
8214 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8215 if (!C->getElementType()->isFloatingPointTy())
8216 return false;
8217 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8218 if (C->getElementAsAPFloat(I).isNaN())
8219 return false;
8220 }
8221 return true;
8222 }
8223
8225 return true;
8226
8227 return false;
8228}
8229
8230static bool isKnownNonZero(const Value *V) {
8231 if (auto *C = dyn_cast<ConstantFP>(V))
8232 return !C->isZero();
8233
8234 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8235 if (!C->getElementType()->isFloatingPointTy())
8236 return false;
8237 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8238 if (C->getElementAsAPFloat(I).isZero())
8239 return false;
8240 }
8241 return true;
8242 }
8243
8244 return false;
8245}
8246
8247/// Match clamp pattern for float types without care about NaNs or signed zeros.
8248/// Given non-min/max outer cmp/select from the clamp pattern this
8249/// function recognizes if it can be substitued by a "canonical" min/max
8250/// pattern.
8252 Value *CmpLHS, Value *CmpRHS,
8253 Value *TrueVal, Value *FalseVal,
8254 Value *&LHS, Value *&RHS) {
8255 // Try to match
8256 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8257 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8258 // and return description of the outer Max/Min.
8259
8260 // First, check if select has inverse order:
8261 if (CmpRHS == FalseVal) {
8262 std::swap(TrueVal, FalseVal);
8263 Pred = CmpInst::getInversePredicate(Pred);
8264 }
8265
8266 // Assume success now. If there's no match, callers should not use these anyway.
8267 LHS = TrueVal;
8268 RHS = FalseVal;
8269
8270 const APFloat *FC1;
8271 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8272 return {SPF_UNKNOWN, SPNB_NA, false};
8273
8274 const APFloat *FC2;
8275 switch (Pred) {
8276 case CmpInst::FCMP_OLT:
8277 case CmpInst::FCMP_OLE:
8278 case CmpInst::FCMP_ULT:
8279 case CmpInst::FCMP_ULE:
8280 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8281 *FC1 < *FC2)
8282 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8283 break;
8284 case CmpInst::FCMP_OGT:
8285 case CmpInst::FCMP_OGE:
8286 case CmpInst::FCMP_UGT:
8287 case CmpInst::FCMP_UGE:
8288 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8289 *FC1 > *FC2)
8290 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8291 break;
8292 default:
8293 break;
8294 }
8295
8296 return {SPF_UNKNOWN, SPNB_NA, false};
8297}
8298
8299/// Recognize variations of:
8300/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8302 Value *CmpLHS, Value *CmpRHS,
8303 Value *TrueVal, Value *FalseVal) {
8304 // Swap the select operands and predicate to match the patterns below.
8305 if (CmpRHS != TrueVal) {
8306 Pred = ICmpInst::getSwappedPredicate(Pred);
8307 std::swap(TrueVal, FalseVal);
8308 }
8309 const APInt *C1;
8310 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8311 const APInt *C2;
8312 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8313 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8314 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8315 return {SPF_SMAX, SPNB_NA, false};
8316
8317 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8318 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8319 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8320 return {SPF_SMIN, SPNB_NA, false};
8321
8322 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8323 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8324 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8325 return {SPF_UMAX, SPNB_NA, false};
8326
8327 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8328 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8329 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8330 return {SPF_UMIN, SPNB_NA, false};
8331 }
8332 return {SPF_UNKNOWN, SPNB_NA, false};
8333}
8334
8335/// Recognize variations of:
8336/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8338 Value *CmpLHS, Value *CmpRHS,
8339 Value *TVal, Value *FVal,
8340 unsigned Depth) {
8341 // TODO: Allow FP min/max with nnan/nsz.
8342 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8343
8344 Value *A = nullptr, *B = nullptr;
8345 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8346 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8347 return {SPF_UNKNOWN, SPNB_NA, false};
8348
8349 Value *C = nullptr, *D = nullptr;
8350 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8351 if (L.Flavor != R.Flavor)
8352 return {SPF_UNKNOWN, SPNB_NA, false};
8353
8354 // We have something like: x Pred y ? min(a, b) : min(c, d).
8355 // Try to match the compare to the min/max operations of the select operands.
8356 // First, make sure we have the right compare predicate.
8357 switch (L.Flavor) {
8358 case SPF_SMIN:
8359 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8360 Pred = ICmpInst::getSwappedPredicate(Pred);
8361 std::swap(CmpLHS, CmpRHS);
8362 }
8363 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8364 break;
8365 return {SPF_UNKNOWN, SPNB_NA, false};
8366 case SPF_SMAX:
8367 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8368 Pred = ICmpInst::getSwappedPredicate(Pred);
8369 std::swap(CmpLHS, CmpRHS);
8370 }
8371 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8372 break;
8373 return {SPF_UNKNOWN, SPNB_NA, false};
8374 case SPF_UMIN:
8375 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8376 Pred = ICmpInst::getSwappedPredicate(Pred);
8377 std::swap(CmpLHS, CmpRHS);
8378 }
8379 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8380 break;
8381 return {SPF_UNKNOWN, SPNB_NA, false};
8382 case SPF_UMAX:
8383 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8384 Pred = ICmpInst::getSwappedPredicate(Pred);
8385 std::swap(CmpLHS, CmpRHS);
8386 }
8387 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8388 break;
8389 return {SPF_UNKNOWN, SPNB_NA, false};
8390 default:
8391 return {SPF_UNKNOWN, SPNB_NA, false};
8392 }
8393
8394 // If there is a common operand in the already matched min/max and the other
8395 // min/max operands match the compare operands (either directly or inverted),
8396 // then this is min/max of the same flavor.
8397
8398 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8399 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8400 if (D == B) {
8401 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8402 match(A, m_Not(m_Specific(CmpRHS)))))
8403 return {L.Flavor, SPNB_NA, false};
8404 }
8405 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8406 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8407 if (C == B) {
8408 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8409 match(A, m_Not(m_Specific(CmpRHS)))))
8410 return {L.Flavor, SPNB_NA, false};
8411 }
8412 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8413 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8414 if (D == A) {
8415 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8416 match(B, m_Not(m_Specific(CmpRHS)))))
8417 return {L.Flavor, SPNB_NA, false};
8418 }
8419 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8420 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8421 if (C == A) {
8422 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8423 match(B, m_Not(m_Specific(CmpRHS)))))
8424 return {L.Flavor, SPNB_NA, false};
8425 }
8426
8427 return {SPF_UNKNOWN, SPNB_NA, false};
8428}
8429
8430/// If the input value is the result of a 'not' op, constant integer, or vector
8431/// splat of a constant integer, return the bitwise-not source value.
8432/// TODO: This could be extended to handle non-splat vector integer constants.
8434 Value *NotV;
8435 if (match(V, m_Not(m_Value(NotV))))
8436 return NotV;
8437
8438 const APInt *C;
8439 if (match(V, m_APInt(C)))
8440 return ConstantInt::get(V->getType(), ~(*C));
8441
8442 return nullptr;
8443}
8444
8445/// Match non-obvious integer minimum and maximum sequences.
8447 Value *CmpLHS, Value *CmpRHS,
8448 Value *TrueVal, Value *FalseVal,
8449 Value *&LHS, Value *&RHS,
8450 unsigned Depth) {
8451 // Assume success. If there's no match, callers should not use these anyway.
8452 LHS = TrueVal;
8453 RHS = FalseVal;
8454
8455 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8457 return SPR;
8458
8459 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8461 return SPR;
8462
8463 // Look through 'not' ops to find disguised min/max.
8464 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8465 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8466 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8467 switch (Pred) {
8468 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8469 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8470 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8471 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8472 default: break;
8473 }
8474 }
8475
8476 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8477 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8478 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8479 switch (Pred) {
8480 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8481 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8482 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8483 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8484 default: break;
8485 }
8486 }
8487
8488 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8489 return {SPF_UNKNOWN, SPNB_NA, false};
8490
8491 const APInt *C1;
8492 if (!match(CmpRHS, m_APInt(C1)))
8493 return {SPF_UNKNOWN, SPNB_NA, false};
8494
8495 // An unsigned min/max can be written with a signed compare.
8496 const APInt *C2;
8497 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8498 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8499 // Is the sign bit set?
8500 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8501 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8502 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8503 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8504
8505 // Is the sign bit clear?
8506 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8507 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8508 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8509 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8510 }
8511
8512 return {SPF_UNKNOWN, SPNB_NA, false};
8513}
8514
8515bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8516 bool AllowPoison) {
8517 assert(X && Y && "Invalid operand");
8518
8519 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8520 if (!match(X, m_Neg(m_Specific(Y))))
8521 return false;
8522
8523 auto *BO = cast<BinaryOperator>(X);
8524 if (NeedNSW && !BO->hasNoSignedWrap())
8525 return false;
8526
8527 auto *Zero = cast<Constant>(BO->getOperand(0));
8528 if (!AllowPoison && !Zero->isNullValue())
8529 return false;
8530
8531 return true;
8532 };
8533
8534 // X = -Y or Y = -X
8535 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8536 return true;
8537
8538 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8539 Value *A, *B;
8540 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8541 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8542 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8544}
8545
8546bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8547 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8548 Value *A, *B, *C;
8549 CmpPredicate Pred1, Pred2;
8550 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8551 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8552 return false;
8553
8554 // They must both have samesign flag or not.
8555 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8556 return false;
8557
8558 if (B == C)
8559 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8560
8561 // Try to infer the relationship from constant ranges.
8562 const APInt *RHSC1, *RHSC2;
8563 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8564 return false;
8565
8566 // Sign bits of two RHSCs should match.
8567 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8568 return false;
8569
8570 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8571 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8572
8573 return CR1.inverse() == CR2;
8574}
8575
8577 SelectPatternNaNBehavior NaNBehavior,
8578 bool Ordered) {
8579 switch (Pred) {
8580 default:
8581 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8582 case ICmpInst::ICMP_UGT:
8583 case ICmpInst::ICMP_UGE:
8584 return {SPF_UMAX, SPNB_NA, false};
8585 case ICmpInst::ICMP_SGT:
8586 case ICmpInst::ICMP_SGE:
8587 return {SPF_SMAX, SPNB_NA, false};
8588 case ICmpInst::ICMP_ULT:
8589 case ICmpInst::ICMP_ULE:
8590 return {SPF_UMIN, SPNB_NA, false};
8591 case ICmpInst::ICMP_SLT:
8592 case ICmpInst::ICMP_SLE:
8593 return {SPF_SMIN, SPNB_NA, false};
8594 case FCmpInst::FCMP_UGT:
8595 case FCmpInst::FCMP_UGE:
8596 case FCmpInst::FCMP_OGT:
8597 case FCmpInst::FCMP_OGE:
8598 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8599 case FCmpInst::FCMP_ULT:
8600 case FCmpInst::FCMP_ULE:
8601 case FCmpInst::FCMP_OLT:
8602 case FCmpInst::FCMP_OLE:
8603 return {SPF_FMINNUM, NaNBehavior, Ordered};
8604 }
8605}
8606
8607std::optional<std::pair<CmpPredicate, Constant *>>
8610 "Only for relational integer predicates.");
8611 if (isa<UndefValue>(C))
8612 return std::nullopt;
8613
8614 Type *Type = C->getType();
8615 bool IsSigned = ICmpInst::isSigned(Pred);
8616
8618 bool WillIncrement =
8619 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8620
8621 // Check if the constant operand can be safely incremented/decremented
8622 // without overflowing/underflowing.
8623 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8624 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8625 };
8626
8627 Constant *SafeReplacementConstant = nullptr;
8628 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8629 // Bail out if the constant can't be safely incremented/decremented.
8630 if (!ConstantIsOk(CI))
8631 return std::nullopt;
8632 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8633 unsigned NumElts = FVTy->getNumElements();
8634 for (unsigned i = 0; i != NumElts; ++i) {
8635 Constant *Elt = C->getAggregateElement(i);
8636 if (!Elt)
8637 return std::nullopt;
8638
8639 if (isa<UndefValue>(Elt))
8640 continue;
8641
8642 // Bail out if we can't determine if this constant is min/max or if we
8643 // know that this constant is min/max.
8644 auto *CI = dyn_cast<ConstantInt>(Elt);
8645 if (!CI || !ConstantIsOk(CI))
8646 return std::nullopt;
8647
8648 if (!SafeReplacementConstant)
8649 SafeReplacementConstant = CI;
8650 }
8651 } else if (isa<VectorType>(C->getType())) {
8652 // Handle scalable splat
8653 Value *SplatC = C->getSplatValue();
8654 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8655 // Bail out if the constant can't be safely incremented/decremented.
8656 if (!CI || !ConstantIsOk(CI))
8657 return std::nullopt;
8658 } else {
8659 // ConstantExpr?
8660 return std::nullopt;
8661 }
8662
8663 // It may not be safe to change a compare predicate in the presence of
8664 // undefined elements, so replace those elements with the first safe constant
8665 // that we found.
8666 // TODO: in case of poison, it is safe; let's replace undefs only.
8667 if (C->containsUndefOrPoisonElement()) {
8668 assert(SafeReplacementConstant && "Replacement constant not set");
8669 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8670 }
8671
8673
8674 // Increment or decrement the constant.
8675 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8676 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8677
8678 return std::make_pair(NewPred, NewC);
8679}
8680
8682 FastMathFlags FMF,
8683 Value *CmpLHS, Value *CmpRHS,
8684 Value *TrueVal, Value *FalseVal,
8685 Value *&LHS, Value *&RHS,
8686 unsigned Depth) {
8687 bool HasMismatchedZeros = false;
8688 if (CmpInst::isFPPredicate(Pred)) {
8689 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8690 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8691 // purpose of identifying min/max. Disregard vector constants with undefined
8692 // elements because those can not be back-propagated for analysis.
8693 Value *OutputZeroVal = nullptr;
8694 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8695 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8696 OutputZeroVal = TrueVal;
8697 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8698 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8699 OutputZeroVal = FalseVal;
8700
8701 if (OutputZeroVal) {
8702 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8703 HasMismatchedZeros = true;
8704 CmpLHS = OutputZeroVal;
8705 }
8706 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8707 HasMismatchedZeros = true;
8708 CmpRHS = OutputZeroVal;
8709 }
8710 }
8711 }
8712
8713 LHS = CmpLHS;
8714 RHS = CmpRHS;
8715
8716 // Signed zero may return inconsistent results between implementations.
8717 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8718 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8719 // Therefore, we behave conservatively and only proceed if at least one of the
8720 // operands is known to not be zero or if we don't care about signed zero.
8721 switch (Pred) {
8722 default: break;
8725 if (!HasMismatchedZeros)
8726 break;
8727 [[fallthrough]];
8730 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8731 !isKnownNonZero(CmpRHS))
8732 return {SPF_UNKNOWN, SPNB_NA, false};
8733 }
8734
8735 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8736 bool Ordered = false;
8737
8738 // When given one NaN and one non-NaN input:
8739 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8740 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8741 // ordered comparison fails), which could be NaN or non-NaN.
8742 // so here we discover exactly what NaN behavior is required/accepted.
8743 if (CmpInst::isFPPredicate(Pred)) {
8744 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8745 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8746
8747 if (LHSSafe && RHSSafe) {
8748 // Both operands are known non-NaN.
8749 NaNBehavior = SPNB_RETURNS_ANY;
8750 Ordered = CmpInst::isOrdered(Pred);
8751 } else if (CmpInst::isOrdered(Pred)) {
8752 // An ordered comparison will return false when given a NaN, so it
8753 // returns the RHS.
8754 Ordered = true;
8755 if (LHSSafe)
8756 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8757 NaNBehavior = SPNB_RETURNS_NAN;
8758 else if (RHSSafe)
8759 NaNBehavior = SPNB_RETURNS_OTHER;
8760 else
8761 // Completely unsafe.
8762 return {SPF_UNKNOWN, SPNB_NA, false};
8763 } else {
8764 Ordered = false;
8765 // An unordered comparison will return true when given a NaN, so it
8766 // returns the LHS.
8767 if (LHSSafe)
8768 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8769 NaNBehavior = SPNB_RETURNS_OTHER;
8770 else if (RHSSafe)
8771 NaNBehavior = SPNB_RETURNS_NAN;
8772 else
8773 // Completely unsafe.
8774 return {SPF_UNKNOWN, SPNB_NA, false};
8775 }
8776 }
8777
8778 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8779 std::swap(CmpLHS, CmpRHS);
8780 Pred = CmpInst::getSwappedPredicate(Pred);
8781 if (NaNBehavior == SPNB_RETURNS_NAN)
8782 NaNBehavior = SPNB_RETURNS_OTHER;
8783 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8784 NaNBehavior = SPNB_RETURNS_NAN;
8785 Ordered = !Ordered;
8786 }
8787
8788 // ([if]cmp X, Y) ? X : Y
8789 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8790 return getSelectPattern(Pred, NaNBehavior, Ordered);
8791
8792 if (isKnownNegation(TrueVal, FalseVal)) {
8793 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8794 // match against either LHS or sext(LHS).
8795 auto MaybeSExtCmpLHS =
8796 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8797 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8798 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8799 if (match(TrueVal, MaybeSExtCmpLHS)) {
8800 // Set the return values. If the compare uses the negated value (-X >s 0),
8801 // swap the return values because the negated value is always 'RHS'.
8802 LHS = TrueVal;
8803 RHS = FalseVal;
8804 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8805 std::swap(LHS, RHS);
8806
8807 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8808 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8809 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8810 return {SPF_ABS, SPNB_NA, false};
8811
8812 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8813 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8814 return {SPF_ABS, SPNB_NA, false};
8815
8816 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8817 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8818 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8819 return {SPF_NABS, SPNB_NA, false};
8820 }
8821 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8822 // Set the return values. If the compare uses the negated value (-X >s 0),
8823 // swap the return values because the negated value is always 'RHS'.
8824 LHS = FalseVal;
8825 RHS = TrueVal;
8826 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8827 std::swap(LHS, RHS);
8828
8829 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8830 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8831 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8832 return {SPF_NABS, SPNB_NA, false};
8833
8834 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8835 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8836 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8837 return {SPF_ABS, SPNB_NA, false};
8838 }
8839 }
8840
8841 if (CmpInst::isIntPredicate(Pred))
8842 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8843
8844 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8845 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8846 // semantics than minNum. Be conservative in such case.
8847 if (NaNBehavior != SPNB_RETURNS_ANY ||
8848 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8849 !isKnownNonZero(CmpRHS)))
8850 return {SPF_UNKNOWN, SPNB_NA, false};
8851
8852 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8853}
8854
8856 Instruction::CastOps *CastOp) {
8857 const DataLayout &DL = CmpI->getDataLayout();
8858
8859 Constant *CastedTo = nullptr;
8860 switch (*CastOp) {
8861 case Instruction::ZExt:
8862 if (CmpI->isUnsigned())
8863 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8864 break;
8865 case Instruction::SExt:
8866 if (CmpI->isSigned())
8867 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8868 break;
8869 case Instruction::Trunc:
8870 Constant *CmpConst;
8871 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8872 CmpConst->getType() == SrcTy) {
8873 // Here we have the following case:
8874 //
8875 // %cond = cmp iN %x, CmpConst
8876 // %tr = trunc iN %x to iK
8877 // %narrowsel = select i1 %cond, iK %t, iK C
8878 //
8879 // We can always move trunc after select operation:
8880 //
8881 // %cond = cmp iN %x, CmpConst
8882 // %widesel = select i1 %cond, iN %x, iN CmpConst
8883 // %tr = trunc iN %widesel to iK
8884 //
8885 // Note that C could be extended in any way because we don't care about
8886 // upper bits after truncation. It can't be abs pattern, because it would
8887 // look like:
8888 //
8889 // select i1 %cond, x, -x.
8890 //
8891 // So only min/max pattern could be matched. Such match requires widened C
8892 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8893 // CmpConst == C is checked below.
8894 CastedTo = CmpConst;
8895 } else {
8896 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8897 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8898 }
8899 break;
8900 case Instruction::FPTrunc:
8901 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8902 break;
8903 case Instruction::FPExt:
8904 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8905 break;
8906 case Instruction::FPToUI:
8907 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8908 break;
8909 case Instruction::FPToSI:
8910 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8911 break;
8912 case Instruction::UIToFP:
8913 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8914 break;
8915 case Instruction::SIToFP:
8916 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8917 break;
8918 default:
8919 break;
8920 }
8921
8922 if (!CastedTo)
8923 return nullptr;
8924
8925 // Make sure the cast doesn't lose any information.
8926 Constant *CastedBack =
8927 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8928 if (CastedBack && CastedBack != C)
8929 return nullptr;
8930
8931 return CastedTo;
8932}
8933
8934/// Helps to match a select pattern in case of a type mismatch.
8935///
8936/// The function processes the case when type of true and false values of a
8937/// select instruction differs from type of the cmp instruction operands because
8938/// of a cast instruction. The function checks if it is legal to move the cast
8939/// operation after "select". If yes, it returns the new second value of
8940/// "select" (with the assumption that cast is moved):
8941/// 1. As operand of cast instruction when both values of "select" are same cast
8942/// instructions.
8943/// 2. As restored constant (by applying reverse cast operation) when the first
8944/// value of the "select" is a cast operation and the second value is a
8945/// constant. It is implemented in lookThroughCastConst().
8946/// 3. As one operand is cast instruction and the other is not. The operands in
8947/// sel(cmp) are in different type integer.
8948/// NOTE: We return only the new second value because the first value could be
8949/// accessed as operand of cast instruction.
8950static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8951 Instruction::CastOps *CastOp) {
8952 auto *Cast1 = dyn_cast<CastInst>(V1);
8953 if (!Cast1)
8954 return nullptr;
8955
8956 *CastOp = Cast1->getOpcode();
8957 Type *SrcTy = Cast1->getSrcTy();
8958 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8959 // If V1 and V2 are both the same cast from the same type, look through V1.
8960 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
8961 return Cast2->getOperand(0);
8962 return nullptr;
8963 }
8964
8965 auto *C = dyn_cast<Constant>(V2);
8966 if (C)
8967 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
8968
8969 Value *CastedTo = nullptr;
8970 if (*CastOp == Instruction::Trunc) {
8971 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
8972 // Here we have the following case:
8973 // %y_ext = sext iK %y to iN
8974 // %cond = cmp iN %x, %y_ext
8975 // %tr = trunc iN %x to iK
8976 // %narrowsel = select i1 %cond, iK %tr, iK %y
8977 //
8978 // We can always move trunc after select operation:
8979 // %y_ext = sext iK %y to iN
8980 // %cond = cmp iN %x, %y_ext
8981 // %widesel = select i1 %cond, iN %x, iN %y_ext
8982 // %tr = trunc iN %widesel to iK
8983 assert(V2->getType() == Cast1->getType() &&
8984 "V2 and Cast1 should be the same type.");
8985 CastedTo = CmpI->getOperand(1);
8986 }
8987 }
8988
8989 return CastedTo;
8990}
8992 Instruction::CastOps *CastOp,
8993 unsigned Depth) {
8995 return {SPF_UNKNOWN, SPNB_NA, false};
8996
8998 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
8999
9000 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9001 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9002
9003 Value *TrueVal = SI->getTrueValue();
9004 Value *FalseVal = SI->getFalseValue();
9005
9007 CmpI, TrueVal, FalseVal, LHS, RHS,
9008 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
9009 CastOp, Depth);
9010}
9011
9013 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9014 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9015 CmpInst::Predicate Pred = CmpI->getPredicate();
9016 Value *CmpLHS = CmpI->getOperand(0);
9017 Value *CmpRHS = CmpI->getOperand(1);
9018 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9019 FMF.setNoNaNs();
9020
9021 // Bail out early.
9022 if (CmpI->isEquality())
9023 return {SPF_UNKNOWN, SPNB_NA, false};
9024
9025 // Deal with type mismatches.
9026 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9027 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9028 // If this is a potential fmin/fmax with a cast to integer, then ignore
9029 // -0.0 because there is no corresponding integer value.
9030 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9031 FMF.setNoSignedZeros();
9032 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9033 cast<CastInst>(TrueVal)->getOperand(0), C,
9034 LHS, RHS, Depth);
9035 }
9036 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9037 // If this is a potential fmin/fmax with a cast to integer, then ignore
9038 // -0.0 because there is no corresponding integer value.
9039 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9040 FMF.setNoSignedZeros();
9041 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9042 C, cast<CastInst>(FalseVal)->getOperand(0),
9043 LHS, RHS, Depth);
9044 }
9045 }
9046 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9047 LHS, RHS, Depth);
9048}
9049
9051 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9052 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9053 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9054 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9055 if (SPF == SPF_FMINNUM)
9056 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9057 if (SPF == SPF_FMAXNUM)
9058 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9059 llvm_unreachable("unhandled!");
9060}
9061
9063 switch (SPF) {
9065 return Intrinsic::umin;
9067 return Intrinsic::umax;
9069 return Intrinsic::smin;
9071 return Intrinsic::smax;
9072 default:
9073 llvm_unreachable("Unexpected SPF");
9074 }
9075}
9076
9078 if (SPF == SPF_SMIN) return SPF_SMAX;
9079 if (SPF == SPF_UMIN) return SPF_UMAX;
9080 if (SPF == SPF_SMAX) return SPF_SMIN;
9081 if (SPF == SPF_UMAX) return SPF_UMIN;
9082 llvm_unreachable("unhandled!");
9083}
9084
9086 switch (MinMaxID) {
9087 case Intrinsic::smax: return Intrinsic::smin;
9088 case Intrinsic::smin: return Intrinsic::smax;
9089 case Intrinsic::umax: return Intrinsic::umin;
9090 case Intrinsic::umin: return Intrinsic::umax;
9091 // Please note that next four intrinsics may produce the same result for
9092 // original and inverted case even if X != Y due to NaN is handled specially.
9093 case Intrinsic::maximum: return Intrinsic::minimum;
9094 case Intrinsic::minimum: return Intrinsic::maximum;
9095 case Intrinsic::maxnum: return Intrinsic::minnum;
9096 case Intrinsic::minnum: return Intrinsic::maxnum;
9097 default: llvm_unreachable("Unexpected intrinsic");
9098 }
9099}
9100
9102 switch (SPF) {
9105 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9106 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9107 default: llvm_unreachable("Unexpected flavor");
9108 }
9109}
9110
9111std::pair<Intrinsic::ID, bool>
9113 // Check if VL contains select instructions that can be folded into a min/max
9114 // vector intrinsic and return the intrinsic if it is possible.
9115 // TODO: Support floating point min/max.
9116 bool AllCmpSingleUse = true;
9117 SelectPatternResult SelectPattern;
9118 SelectPattern.Flavor = SPF_UNKNOWN;
9119 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9120 Value *LHS, *RHS;
9121 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9122 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9123 return false;
9124 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9125 SelectPattern.Flavor != CurrentPattern.Flavor)
9126 return false;
9127 SelectPattern = CurrentPattern;
9128 AllCmpSingleUse &=
9130 return true;
9131 })) {
9132 switch (SelectPattern.Flavor) {
9133 case SPF_SMIN:
9134 return {Intrinsic::smin, AllCmpSingleUse};
9135 case SPF_UMIN:
9136 return {Intrinsic::umin, AllCmpSingleUse};
9137 case SPF_SMAX:
9138 return {Intrinsic::smax, AllCmpSingleUse};
9139 case SPF_UMAX:
9140 return {Intrinsic::umax, AllCmpSingleUse};
9141 case SPF_FMAXNUM:
9142 return {Intrinsic::maxnum, AllCmpSingleUse};
9143 case SPF_FMINNUM:
9144 return {Intrinsic::minnum, AllCmpSingleUse};
9145 default:
9146 llvm_unreachable("unexpected select pattern flavor");
9147 }
9148 }
9149 return {Intrinsic::not_intrinsic, false};
9150}
9151
9152template <typename InstTy>
9153static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9154 Value *&Init, Value *&OtherOp) {
9155 // Handle the case of a simple two-predecessor recurrence PHI.
9156 // There's a lot more that could theoretically be done here, but
9157 // this is sufficient to catch some interesting cases.
9158 // TODO: Expand list -- gep, uadd.sat etc.
9159 if (PN->getNumIncomingValues() != 2)
9160 return false;
9161
9162 for (unsigned I = 0; I != 2; ++I) {
9163 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9164 Operation && Operation->getNumOperands() >= 2) {
9165 Value *LHS = Operation->getOperand(0);
9166 Value *RHS = Operation->getOperand(1);
9167 if (LHS != PN && RHS != PN)
9168 continue;
9169
9170 Inst = Operation;
9171 Init = PN->getIncomingValue(!I);
9172 OtherOp = (LHS == PN) ? RHS : LHS;
9173 return true;
9174 }
9175 }
9176 return false;
9177}
9178
9180 Value *&Start, Value *&Step) {
9181 // We try to match a recurrence of the form:
9182 // %iv = [Start, %entry], [%iv.next, %backedge]
9183 // %iv.next = binop %iv, Step
9184 // Or:
9185 // %iv = [Start, %entry], [%iv.next, %backedge]
9186 // %iv.next = binop Step, %iv
9187 return matchTwoInputRecurrence(P, BO, Start, Step);
9188}
9189
9191 Value *&Start, Value *&Step) {
9192 BinaryOperator *BO = nullptr;
9193 P = dyn_cast<PHINode>(I->getOperand(0));
9194 if (!P)
9195 P = dyn_cast<PHINode>(I->getOperand(1));
9196 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9197}
9198
9200 PHINode *&P, Value *&Init,
9201 Value *&OtherOp) {
9202 // Binary intrinsics only supported for now.
9203 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9204 I->getType() != I->getArgOperand(1)->getType())
9205 return false;
9206
9207 IntrinsicInst *II = nullptr;
9208 P = dyn_cast<PHINode>(I->getArgOperand(0));
9209 if (!P)
9210 P = dyn_cast<PHINode>(I->getArgOperand(1));
9211
9212 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9213}
9214
9215/// Return true if "icmp Pred LHS RHS" is always true.
9217 const Value *RHS) {
9218 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9219 return true;
9220
9221 switch (Pred) {
9222 default:
9223 return false;
9224
9225 case CmpInst::ICMP_SLE: {
9226 const APInt *C;
9227
9228 // LHS s<= LHS +_{nsw} C if C >= 0
9229 // LHS s<= LHS | C if C >= 0
9230 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9232 return !C->isNegative();
9233
9234 // LHS s<= smax(LHS, V) for any V
9236 return true;
9237
9238 // smin(RHS, V) s<= RHS for any V
9240 return true;
9241
9242 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9243 const Value *X;
9244 const APInt *CLHS, *CRHS;
9245 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9247 return CLHS->sle(*CRHS);
9248
9249 return false;
9250 }
9251
9252 case CmpInst::ICMP_ULE: {
9253 // LHS u<= LHS +_{nuw} V for any V
9254 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9256 return true;
9257
9258 // LHS u<= LHS | V for any V
9259 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9260 return true;
9261
9262 // LHS u<= umax(LHS, V) for any V
9264 return true;
9265
9266 // RHS >> V u<= RHS for any V
9267 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9268 return true;
9269
9270 // RHS u/ C_ugt_1 u<= RHS
9271 const APInt *C;
9272 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9273 return true;
9274
9275 // RHS & V u<= RHS for any V
9277 return true;
9278
9279 // umin(RHS, V) u<= RHS for any V
9281 return true;
9282
9283 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9284 const Value *X;
9285 const APInt *CLHS, *CRHS;
9286 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9288 return CLHS->ule(*CRHS);
9289
9290 return false;
9291 }
9292 }
9293}
9294
9295/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9296/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9297static std::optional<bool>
9299 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9300 switch (Pred) {
9301 default:
9302 return std::nullopt;
9303
9304 case CmpInst::ICMP_SLT:
9305 case CmpInst::ICMP_SLE:
9306 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9308 return true;
9309 return std::nullopt;
9310
9311 case CmpInst::ICMP_SGT:
9312 case CmpInst::ICMP_SGE:
9313 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9315 return true;
9316 return std::nullopt;
9317
9318 case CmpInst::ICMP_ULT:
9319 case CmpInst::ICMP_ULE:
9320 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9322 return true;
9323 return std::nullopt;
9324
9325 case CmpInst::ICMP_UGT:
9326 case CmpInst::ICMP_UGE:
9327 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9329 return true;
9330 return std::nullopt;
9331 }
9332}
9333
9334/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9335/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9336/// Otherwise, return std::nullopt if we can't infer anything.
9337static std::optional<bool>
9339 CmpPredicate RPred, const ConstantRange &RCR) {
9340 auto CRImpliesPred = [&](ConstantRange CR,
9341 CmpInst::Predicate Pred) -> std::optional<bool> {
9342 // If all true values for lhs and true for rhs, lhs implies rhs
9343 if (CR.icmp(Pred, RCR))
9344 return true;
9345
9346 // If there is no overlap, lhs implies not rhs
9347 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9348 return false;
9349
9350 return std::nullopt;
9351 };
9352 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9353 RPred))
9354 return Res;
9355 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9357 : LPred.dropSameSign();
9359 : RPred.dropSameSign();
9360 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9361 RPred);
9362 }
9363 return std::nullopt;
9364}
9365
9366/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9367/// is true. Return false if LHS implies RHS is false. Otherwise, return
9368/// std::nullopt if we can't infer anything.
9369static std::optional<bool>
9370isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9371 CmpPredicate RPred, const Value *R0, const Value *R1,
9372 const DataLayout &DL, bool LHSIsTrue) {
9373 // The rest of the logic assumes the LHS condition is true. If that's not the
9374 // case, invert the predicate to make it so.
9375 if (!LHSIsTrue)
9376 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9377
9378 // We can have non-canonical operands, so try to normalize any common operand
9379 // to L0/R0.
9380 if (L0 == R1) {
9381 std::swap(R0, R1);
9382 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9383 }
9384 if (R0 == L1) {
9385 std::swap(L0, L1);
9386 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9387 }
9388 if (L1 == R1) {
9389 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9390 if (L0 != R0 || match(L0, m_ImmConstant())) {
9391 std::swap(L0, L1);
9392 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9393 std::swap(R0, R1);
9394 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9395 }
9396 }
9397
9398 // See if we can infer anything if operand-0 matches and we have at least one
9399 // constant.
9400 const APInt *Unused;
9401 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9402 // Potential TODO: We could also further use the constant range of L0/R0 to
9403 // further constraint the constant ranges. At the moment this leads to
9404 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9405 // C1` (see discussion: D58633).
9407 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9408 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9410 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9411 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9412 // Even if L1/R1 are not both constant, we can still sometimes deduce
9413 // relationship from a single constant. For example X u> Y implies X != 0.
9414 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9415 return R;
9416 // If both L1/R1 were exact constant ranges and we didn't get anything
9417 // here, we won't be able to deduce this.
9418 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9419 return std::nullopt;
9420 }
9421
9422 // Can we infer anything when the two compares have matching operands?
9423 if (L0 == R0 && L1 == R1)
9424 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9425
9426 // It only really makes sense in the context of signed comparison for "X - Y
9427 // must be positive if X >= Y and no overflow".
9428 // Take SGT as an example: L0:x > L1:y and C >= 0
9429 // ==> R0:(x -nsw y) < R1:(-C) is false
9430 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9431 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9432 SignedLPred == ICmpInst::ICMP_SGE) &&
9433 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9434 if (match(R1, m_NonPositive()) &&
9435 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9436 return false;
9437 }
9438
9439 // Take SLT as an example: L0:x < L1:y and C <= 0
9440 // ==> R0:(x -nsw y) < R1:(-C) is true
9441 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9442 SignedLPred == ICmpInst::ICMP_SLE) &&
9443 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9444 if (match(R1, m_NonNegative()) &&
9445 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9446 return true;
9447 }
9448
9449 // a - b == NonZero -> a != b
9450 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9451 const APInt *L1C;
9452 Value *A, *B;
9453 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9454 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9455 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9456 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9457 (match(A, m_PtrToInt(m_Specific(R0))) &&
9458 match(B, m_PtrToInt(m_Specific(R1)))) ||
9459 (match(A, m_PtrToInt(m_Specific(R1))) &&
9460 match(B, m_PtrToInt(m_Specific(R0)))))) {
9461 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9462 }
9463
9464 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9465 if (L0 == R0 &&
9466 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9467 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9468 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9469 return CmpPredicate::getMatching(LPred, RPred).has_value();
9470
9471 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9472 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9473
9474 return std::nullopt;
9475}
9476
9477/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9478/// false. Otherwise, return std::nullopt if we can't infer anything. We
9479/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9480/// instruction.
9481static std::optional<bool>
9483 const Value *RHSOp0, const Value *RHSOp1,
9484 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9485 // The LHS must be an 'or', 'and', or a 'select' instruction.
9486 assert((LHS->getOpcode() == Instruction::And ||
9487 LHS->getOpcode() == Instruction::Or ||
9488 LHS->getOpcode() == Instruction::Select) &&
9489 "Expected LHS to be 'and', 'or', or 'select'.");
9490
9491 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9492
9493 // If the result of an 'or' is false, then we know both legs of the 'or' are
9494 // false. Similarly, if the result of an 'and' is true, then we know both
9495 // legs of the 'and' are true.
9496 const Value *ALHS, *ARHS;
9497 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9498 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9499 // FIXME: Make this non-recursion.
9500 if (std::optional<bool> Implication = isImpliedCondition(
9501 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9502 return Implication;
9503 if (std::optional<bool> Implication = isImpliedCondition(
9504 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9505 return Implication;
9506 return std::nullopt;
9507 }
9508 return std::nullopt;
9509}
9510
9511std::optional<bool>
9513 const Value *RHSOp0, const Value *RHSOp1,
9514 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9515 // Bail out when we hit the limit.
9517 return std::nullopt;
9518
9519 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9520 // example.
9521 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9522 return std::nullopt;
9523
9524 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9525 "Expected integer type only!");
9526
9527 // Match not
9528 if (match(LHS, m_Not(m_Value(LHS))))
9529 LHSIsTrue = !LHSIsTrue;
9530
9531 // Both LHS and RHS are icmps.
9532 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9533 return isImpliedCondICmps(LHSCmp->getCmpPredicate(), LHSCmp->getOperand(0),
9534 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9535 DL, LHSIsTrue);
9536 const Value *V;
9537 if (match(LHS, m_NUWTrunc(m_Value(V))))
9539 ConstantInt::get(V->getType(), 0), RHSPred,
9540 RHSOp0, RHSOp1, DL, LHSIsTrue);
9541
9542 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9543 /// the RHS to be an icmp.
9544 /// FIXME: Add support for and/or/select on the RHS.
9545 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9546 if ((LHSI->getOpcode() == Instruction::And ||
9547 LHSI->getOpcode() == Instruction::Or ||
9548 LHSI->getOpcode() == Instruction::Select))
9549 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9550 Depth);
9551 }
9552 return std::nullopt;
9553}
9554
9555std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9556 const DataLayout &DL,
9557 bool LHSIsTrue, unsigned Depth) {
9558 // LHS ==> RHS by definition
9559 if (LHS == RHS)
9560 return LHSIsTrue;
9561
9562 // Match not
9563 bool InvertRHS = false;
9564 if (match(RHS, m_Not(m_Value(RHS)))) {
9565 if (LHS == RHS)
9566 return !LHSIsTrue;
9567 InvertRHS = true;
9568 }
9569
9570 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9571 if (auto Implied = isImpliedCondition(
9572 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9573 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9574 return InvertRHS ? !*Implied : *Implied;
9575 return std::nullopt;
9576 }
9577
9578 const Value *V;
9579 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9580 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9581 ConstantInt::get(V->getType(), 0), DL,
9582 LHSIsTrue, Depth))
9583 return InvertRHS ? !*Implied : *Implied;
9584 return std::nullopt;
9585 }
9586
9588 return std::nullopt;
9589
9590 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9591 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9592 const Value *RHS1, *RHS2;
9593 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9594 if (std::optional<bool> Imp =
9595 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9596 if (*Imp == true)
9597 return !InvertRHS;
9598 if (std::optional<bool> Imp =
9599 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9600 if (*Imp == true)
9601 return !InvertRHS;
9602 }
9603 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9604 if (std::optional<bool> Imp =
9605 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9606 if (*Imp == false)
9607 return InvertRHS;
9608 if (std::optional<bool> Imp =
9609 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9610 if (*Imp == false)
9611 return InvertRHS;
9612 }
9613
9614 return std::nullopt;
9615}
9616
9617// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9618// condition dominating ContextI or nullptr, if no condition is found.
9619static std::pair<Value *, bool>
9621 if (!ContextI || !ContextI->getParent())
9622 return {nullptr, false};
9623
9624 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9625 // dominator tree (eg, from a SimplifyQuery) instead?
9626 const BasicBlock *ContextBB = ContextI->getParent();
9627 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9628 if (!PredBB)
9629 return {nullptr, false};
9630
9631 // We need a conditional branch in the predecessor.
9632 Value *PredCond;
9633 BasicBlock *TrueBB, *FalseBB;
9634 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9635 return {nullptr, false};
9636
9637 // The branch should get simplified. Don't bother simplifying this condition.
9638 if (TrueBB == FalseBB)
9639 return {nullptr, false};
9640
9641 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9642 "Predecessor block does not point to successor?");
9643
9644 // Is this condition implied by the predecessor condition?
9645 return {PredCond, TrueBB == ContextBB};
9646}
9647
9648std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9649 const Instruction *ContextI,
9650 const DataLayout &DL) {
9651 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9652 auto PredCond = getDomPredecessorCondition(ContextI);
9653 if (PredCond.first)
9654 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9655 return std::nullopt;
9656}
9657
9659 const Value *LHS,
9660 const Value *RHS,
9661 const Instruction *ContextI,
9662 const DataLayout &DL) {
9663 auto PredCond = getDomPredecessorCondition(ContextI);
9664 if (PredCond.first)
9665 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9666 PredCond.second);
9667 return std::nullopt;
9668}
9669
9671 APInt &Upper, const InstrInfoQuery &IIQ,
9672 bool PreferSignedRange) {
9673 unsigned Width = Lower.getBitWidth();
9674 const APInt *C;
9675 switch (BO.getOpcode()) {
9676 case Instruction::Sub:
9677 if (match(BO.getOperand(0), m_APInt(C))) {
9678 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9679 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9680
9681 // If the caller expects a signed compare, then try to use a signed range.
9682 // Otherwise if both no-wraps are set, use the unsigned range because it
9683 // is never larger than the signed range. Example:
9684 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9685 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9686 if (PreferSignedRange && HasNSW && HasNUW)
9687 HasNUW = false;
9688
9689 if (HasNUW) {
9690 // 'sub nuw c, x' produces [0, C].
9691 Upper = *C + 1;
9692 } else if (HasNSW) {
9693 if (C->isNegative()) {
9694 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9696 Upper = *C - APInt::getSignedMaxValue(Width);
9697 } else {
9698 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9699 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9700 Lower = *C - APInt::getSignedMaxValue(Width);
9702 }
9703 }
9704 }
9705 break;
9706 case Instruction::Add:
9707 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9708 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9709 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9710
9711 // If the caller expects a signed compare, then try to use a signed
9712 // range. Otherwise if both no-wraps are set, use the unsigned range
9713 // because it is never larger than the signed range. Example: "add nuw
9714 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9715 if (PreferSignedRange && HasNSW && HasNUW)
9716 HasNUW = false;
9717
9718 if (HasNUW) {
9719 // 'add nuw x, C' produces [C, UINT_MAX].
9720 Lower = *C;
9721 } else if (HasNSW) {
9722 if (C->isNegative()) {
9723 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9725 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9726 } else {
9727 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9728 Lower = APInt::getSignedMinValue(Width) + *C;
9729 Upper = APInt::getSignedMaxValue(Width) + 1;
9730 }
9731 }
9732 }
9733 break;
9734
9735 case Instruction::And:
9736 if (match(BO.getOperand(1), m_APInt(C)))
9737 // 'and x, C' produces [0, C].
9738 Upper = *C + 1;
9739 // X & -X is a power of two or zero. So we can cap the value at max power of
9740 // two.
9741 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9742 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9743 Upper = APInt::getSignedMinValue(Width) + 1;
9744 break;
9745
9746 case Instruction::Or:
9747 if (match(BO.getOperand(1), m_APInt(C)))
9748 // 'or x, C' produces [C, UINT_MAX].
9749 Lower = *C;
9750 break;
9751
9752 case Instruction::AShr:
9753 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9754 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9756 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9757 } else if (match(BO.getOperand(0), m_APInt(C))) {
9758 unsigned ShiftAmount = Width - 1;
9759 if (!C->isZero() && IIQ.isExact(&BO))
9760 ShiftAmount = C->countr_zero();
9761 if (C->isNegative()) {
9762 // 'ashr C, x' produces [C, C >> (Width-1)]
9763 Lower = *C;
9764 Upper = C->ashr(ShiftAmount) + 1;
9765 } else {
9766 // 'ashr C, x' produces [C >> (Width-1), C]
9767 Lower = C->ashr(ShiftAmount);
9768 Upper = *C + 1;
9769 }
9770 }
9771 break;
9772
9773 case Instruction::LShr:
9774 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9775 // 'lshr x, C' produces [0, UINT_MAX >> C].
9776 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9777 } else if (match(BO.getOperand(0), m_APInt(C))) {
9778 // 'lshr C, x' produces [C >> (Width-1), C].
9779 unsigned ShiftAmount = Width - 1;
9780 if (!C->isZero() && IIQ.isExact(&BO))
9781 ShiftAmount = C->countr_zero();
9782 Lower = C->lshr(ShiftAmount);
9783 Upper = *C + 1;
9784 }
9785 break;
9786
9787 case Instruction::Shl:
9788 if (match(BO.getOperand(0), m_APInt(C))) {
9789 if (IIQ.hasNoUnsignedWrap(&BO)) {
9790 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9791 Lower = *C;
9792 Upper = Lower.shl(Lower.countl_zero()) + 1;
9793 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9794 if (C->isNegative()) {
9795 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9796 unsigned ShiftAmount = C->countl_one() - 1;
9797 Lower = C->shl(ShiftAmount);
9798 Upper = *C + 1;
9799 } else {
9800 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9801 unsigned ShiftAmount = C->countl_zero() - 1;
9802 Lower = *C;
9803 Upper = C->shl(ShiftAmount) + 1;
9804 }
9805 } else {
9806 // If lowbit is set, value can never be zero.
9807 if ((*C)[0])
9808 Lower = APInt::getOneBitSet(Width, 0);
9809 // If we are shifting a constant the largest it can be is if the longest
9810 // sequence of consecutive ones is shifted to the highbits (breaking
9811 // ties for which sequence is higher). At the moment we take a liberal
9812 // upper bound on this by just popcounting the constant.
9813 // TODO: There may be a bitwise trick for it longest/highest
9814 // consecutative sequence of ones (naive method is O(Width) loop).
9815 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9816 }
9817 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9818 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9819 }
9820 break;
9821
9822 case Instruction::SDiv:
9823 if (match(BO.getOperand(1), m_APInt(C))) {
9824 APInt IntMin = APInt::getSignedMinValue(Width);
9825 APInt IntMax = APInt::getSignedMaxValue(Width);
9826 if (C->isAllOnes()) {
9827 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9828 // where C != -1 and C != 0 and C != 1
9829 Lower = IntMin + 1;
9830 Upper = IntMax + 1;
9831 } else if (C->countl_zero() < Width - 1) {
9832 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9833 // where C != -1 and C != 0 and C != 1
9834 Lower = IntMin.sdiv(*C);
9835 Upper = IntMax.sdiv(*C);
9836 if (Lower.sgt(Upper))
9838 Upper = Upper + 1;
9839 assert(Upper != Lower && "Upper part of range has wrapped!");
9840 }
9841 } else if (match(BO.getOperand(0), m_APInt(C))) {
9842 if (C->isMinSignedValue()) {
9843 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9844 Lower = *C;
9845 Upper = Lower.lshr(1) + 1;
9846 } else {
9847 // 'sdiv C, x' produces [-|C|, |C|].
9848 Upper = C->abs() + 1;
9849 Lower = (-Upper) + 1;
9850 }
9851 }
9852 break;
9853
9854 case Instruction::UDiv:
9855 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9856 // 'udiv x, C' produces [0, UINT_MAX / C].
9857 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9858 } else if (match(BO.getOperand(0), m_APInt(C))) {
9859 // 'udiv C, x' produces [0, C].
9860 Upper = *C + 1;
9861 }
9862 break;
9863
9864 case Instruction::SRem:
9865 if (match(BO.getOperand(1), m_APInt(C))) {
9866 // 'srem x, C' produces (-|C|, |C|).
9867 Upper = C->abs();
9868 Lower = (-Upper) + 1;
9869 } else if (match(BO.getOperand(0), m_APInt(C))) {
9870 if (C->isNegative()) {
9871 // 'srem -|C|, x' produces [-|C|, 0].
9872 Upper = 1;
9873 Lower = *C;
9874 } else {
9875 // 'srem |C|, x' produces [0, |C|].
9876 Upper = *C + 1;
9877 }
9878 }
9879 break;
9880
9881 case Instruction::URem:
9882 if (match(BO.getOperand(1), m_APInt(C)))
9883 // 'urem x, C' produces [0, C).
9884 Upper = *C;
9885 else if (match(BO.getOperand(0), m_APInt(C)))
9886 // 'urem C, x' produces [0, C].
9887 Upper = *C + 1;
9888 break;
9889
9890 default:
9891 break;
9892 }
9893}
9894
9896 bool UseInstrInfo) {
9897 unsigned Width = II.getType()->getScalarSizeInBits();
9898 const APInt *C;
9899 switch (II.getIntrinsicID()) {
9900 case Intrinsic::ctlz:
9901 case Intrinsic::cttz: {
9902 APInt Upper(Width, Width);
9903 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
9904 Upper += 1;
9905 // Maximum of set/clear bits is the bit width.
9907 }
9908 case Intrinsic::ctpop:
9909 // Maximum of set/clear bits is the bit width.
9911 APInt(Width, Width) + 1);
9912 case Intrinsic::uadd_sat:
9913 // uadd.sat(x, C) produces [C, UINT_MAX].
9914 if (match(II.getOperand(0), m_APInt(C)) ||
9915 match(II.getOperand(1), m_APInt(C)))
9917 break;
9918 case Intrinsic::sadd_sat:
9919 if (match(II.getOperand(0), m_APInt(C)) ||
9920 match(II.getOperand(1), m_APInt(C))) {
9921 if (C->isNegative())
9922 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9924 APInt::getSignedMaxValue(Width) + *C +
9925 1);
9926
9927 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9929 APInt::getSignedMaxValue(Width) + 1);
9930 }
9931 break;
9932 case Intrinsic::usub_sat:
9933 // usub.sat(C, x) produces [0, C].
9934 if (match(II.getOperand(0), m_APInt(C)))
9935 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9936
9937 // usub.sat(x, C) produces [0, UINT_MAX - C].
9938 if (match(II.getOperand(1), m_APInt(C)))
9940 APInt::getMaxValue(Width) - *C + 1);
9941 break;
9942 case Intrinsic::ssub_sat:
9943 if (match(II.getOperand(0), m_APInt(C))) {
9944 if (C->isNegative())
9945 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9947 *C - APInt::getSignedMinValue(Width) +
9948 1);
9949
9950 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9952 APInt::getSignedMaxValue(Width) + 1);
9953 } else if (match(II.getOperand(1), m_APInt(C))) {
9954 if (C->isNegative())
9955 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9957 APInt::getSignedMaxValue(Width) + 1);
9958
9959 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9961 APInt::getSignedMaxValue(Width) - *C +
9962 1);
9963 }
9964 break;
9965 case Intrinsic::umin:
9966 case Intrinsic::umax:
9967 case Intrinsic::smin:
9968 case Intrinsic::smax:
9969 if (!match(II.getOperand(0), m_APInt(C)) &&
9970 !match(II.getOperand(1), m_APInt(C)))
9971 break;
9972
9973 switch (II.getIntrinsicID()) {
9974 case Intrinsic::umin:
9975 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9976 case Intrinsic::umax:
9978 case Intrinsic::smin:
9980 *C + 1);
9981 case Intrinsic::smax:
9983 APInt::getSignedMaxValue(Width) + 1);
9984 default:
9985 llvm_unreachable("Must be min/max intrinsic");
9986 }
9987 break;
9988 case Intrinsic::abs:
9989 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
9990 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9991 if (match(II.getOperand(1), m_One()))
9993 APInt::getSignedMaxValue(Width) + 1);
9994
9996 APInt::getSignedMinValue(Width) + 1);
9997 case Intrinsic::vscale:
9998 if (!II.getParent() || !II.getFunction())
9999 break;
10000 return getVScaleRange(II.getFunction(), Width);
10001 default:
10002 break;
10003 }
10004
10005 return ConstantRange::getFull(Width);
10006}
10007
10009 const InstrInfoQuery &IIQ) {
10010 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10011 const Value *LHS = nullptr, *RHS = nullptr;
10013 if (R.Flavor == SPF_UNKNOWN)
10014 return ConstantRange::getFull(BitWidth);
10015
10016 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10017 // If the negation part of the abs (in RHS) has the NSW flag,
10018 // then the result of abs(X) is [0..SIGNED_MAX],
10019 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10020 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10024
10027 }
10028
10029 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10030 // The result of -abs(X) is <= 0.
10032 APInt(BitWidth, 1));
10033 }
10034
10035 const APInt *C;
10036 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10037 return ConstantRange::getFull(BitWidth);
10038
10039 switch (R.Flavor) {
10040 case SPF_UMIN:
10042 case SPF_UMAX:
10044 case SPF_SMIN:
10046 *C + 1);
10047 case SPF_SMAX:
10050 default:
10051 return ConstantRange::getFull(BitWidth);
10052 }
10053}
10054
10056 // The maximum representable value of a half is 65504. For floats the maximum
10057 // value is 3.4e38 which requires roughly 129 bits.
10058 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10059 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10060 return;
10061 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10062 Lower = APInt(BitWidth, -65504, true);
10063 Upper = APInt(BitWidth, 65505);
10064 }
10065
10066 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10067 // For a fptoui the lower limit is left as 0.
10068 Upper = APInt(BitWidth, 65505);
10069 }
10070}
10071
10073 bool UseInstrInfo, AssumptionCache *AC,
10074 const Instruction *CtxI,
10075 const DominatorTree *DT,
10076 unsigned Depth) {
10077 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10078
10080 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10081
10082 if (auto *C = dyn_cast<Constant>(V))
10083 return C->toConstantRange();
10084
10085 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10086 InstrInfoQuery IIQ(UseInstrInfo);
10087 ConstantRange CR = ConstantRange::getFull(BitWidth);
10088 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10089 APInt Lower = APInt(BitWidth, 0);
10090 APInt Upper = APInt(BitWidth, 0);
10091 // TODO: Return ConstantRange.
10092 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10094 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10095 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10096 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10098 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10100 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10101 CR = CRTrue.unionWith(CRFalse);
10103 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10104 APInt Lower = APInt(BitWidth, 0);
10105 APInt Upper = APInt(BitWidth, 0);
10106 // TODO: Return ConstantRange.
10109 } else if (const auto *A = dyn_cast<Argument>(V))
10110 if (std::optional<ConstantRange> Range = A->getRange())
10111 CR = *Range;
10112
10113 if (auto *I = dyn_cast<Instruction>(V)) {
10114 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10116
10117 if (const auto *CB = dyn_cast<CallBase>(V))
10118 if (std::optional<ConstantRange> Range = CB->getRange())
10119 CR = CR.intersectWith(*Range);
10120 }
10121
10122 if (CtxI && AC) {
10123 // Try to restrict the range based on information from assumptions.
10124 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10125 if (!AssumeVH)
10126 continue;
10127 CallInst *I = cast<CallInst>(AssumeVH);
10128 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10129 "Got assumption for the wrong function!");
10130 assert(I->getIntrinsicID() == Intrinsic::assume &&
10131 "must be an assume intrinsic");
10132
10133 if (!isValidAssumeForContext(I, CtxI, DT))
10134 continue;
10135 Value *Arg = I->getArgOperand(0);
10136 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10137 // Currently we just use information from comparisons.
10138 if (!Cmp || Cmp->getOperand(0) != V)
10139 continue;
10140 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10141 ConstantRange RHS =
10142 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10143 UseInstrInfo, AC, I, DT, Depth + 1);
10144 CR = CR.intersectWith(
10145 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10146 }
10147 }
10148
10149 return CR;
10150}
10151
10152static void
10154 function_ref<void(Value *)> InsertAffected) {
10155 assert(V != nullptr);
10156 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10157 InsertAffected(V);
10158 } else if (auto *I = dyn_cast<Instruction>(V)) {
10159 InsertAffected(V);
10160
10161 // Peek through unary operators to find the source of the condition.
10162 Value *Op;
10165 InsertAffected(Op);
10166 }
10167 }
10168}
10169
10171 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10172 auto AddAffected = [&InsertAffected](Value *V) {
10173 addValueAffectedByCondition(V, InsertAffected);
10174 };
10175
10176 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10177 if (IsAssume) {
10178 AddAffected(LHS);
10179 AddAffected(RHS);
10180 } else if (match(RHS, m_Constant()))
10181 AddAffected(LHS);
10182 };
10183
10184 SmallVector<Value *, 8> Worklist;
10186 Worklist.push_back(Cond);
10187 while (!Worklist.empty()) {
10188 Value *V = Worklist.pop_back_val();
10189 if (!Visited.insert(V).second)
10190 continue;
10191
10192 CmpPredicate Pred;
10193 Value *A, *B, *X;
10194
10195 if (IsAssume) {
10196 AddAffected(V);
10197 if (match(V, m_Not(m_Value(X))))
10198 AddAffected(X);
10199 }
10200
10201 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10202 // assume(A && B) is split to -> assume(A); assume(B);
10203 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10204 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10205 // enough information to be worth handling (intersection of information as
10206 // opposed to union).
10207 if (!IsAssume) {
10208 Worklist.push_back(A);
10209 Worklist.push_back(B);
10210 }
10211 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10212 bool HasRHSC = match(B, m_ConstantInt());
10213 if (ICmpInst::isEquality(Pred)) {
10214 AddAffected(A);
10215 if (IsAssume)
10216 AddAffected(B);
10217 if (HasRHSC) {
10218 Value *Y;
10219 // (X << C) or (X >>_s C) or (X >>_u C).
10220 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10221 AddAffected(X);
10222 // (X & C) or (X | C).
10223 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10224 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10225 AddAffected(X);
10226 AddAffected(Y);
10227 }
10228 // X - Y
10229 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10230 AddAffected(X);
10231 AddAffected(Y);
10232 }
10233 }
10234 } else {
10235 AddCmpOperands(A, B);
10236 if (HasRHSC) {
10237 // Handle (A + C1) u< C2, which is the canonical form of
10238 // A > C3 && A < C4.
10240 AddAffected(X);
10241
10242 if (ICmpInst::isUnsigned(Pred)) {
10243 Value *Y;
10244 // X & Y u> C -> X >u C && Y >u C
10245 // X | Y u< C -> X u< C && Y u< C
10246 // X nuw+ Y u< C -> X u< C && Y u< C
10247 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10248 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10249 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10250 AddAffected(X);
10251 AddAffected(Y);
10252 }
10253 // X nuw- Y u> C -> X u> C
10254 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10255 AddAffected(X);
10256 }
10257 }
10258
10259 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10260 // by computeKnownFPClass().
10262 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10263 InsertAffected(X);
10264 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10265 InsertAffected(X);
10266 }
10267 }
10268
10269 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10270 AddAffected(X);
10271 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10272 AddCmpOperands(A, B);
10273
10274 // fcmp fneg(x), y
10275 // fcmp fabs(x), y
10276 // fcmp fneg(fabs(x)), y
10277 if (match(A, m_FNeg(m_Value(A))))
10278 AddAffected(A);
10279 if (match(A, m_FAbs(m_Value(A))))
10280 AddAffected(A);
10281
10283 m_Value()))) {
10284 // Handle patterns that computeKnownFPClass() support.
10285 AddAffected(A);
10286 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10287 // Assume is checked here as X is already added above for assumes in
10288 // addValueAffectedByCondition
10289 AddAffected(X);
10290 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10291 // Assume is checked here to avoid issues with ephemeral values
10292 Worklist.push_back(X);
10293 }
10294 }
10295}
10296
10298 // (X >> C) or/add (X & mask(C) != 0)
10299 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10300 if (BO->getOpcode() == Instruction::Add ||
10301 BO->getOpcode() == Instruction::Or) {
10302 const Value *X;
10303 const APInt *C1, *C2;
10304 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10308 m_Zero())))) &&
10309 C2->popcount() == C1->getZExtValue())
10310 return X;
10311 }
10312 }
10313 return nullptr;
10314}
10315
10317 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10318}
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 constexpr unsigned MaxInstrsToCheckForFree
Maximum number of instructions to check between assume and context instruction.
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
UndefPoisonKind
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static bool includesPoison(UndefPoisonKind Kind)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool includesUndef(UndefPoisonKind Kind)
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst, Value *&Init, Value *&OtherOp)
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ?
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return the number of times the sign bit of the register is replicated into the other bits.
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ?
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, const SimplifyQuery &SQ, bool Invert, unsigned Depth)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp PredALHS ARHS" is true.
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth)
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Value * RHS
Value * LHS
bool isFinite() const
Definition APFloat.h:1454
bool isNaN() const
Definition APFloat.h:1447
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1138
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1098
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1079
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1406
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:423
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1540
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1391
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1670
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1385
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:206
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1330
unsigned ceilLogBase2() const
Definition APInt.h:1764
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1201
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:371
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:380
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:209
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:216
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:329
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1249
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1396
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1166
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1628
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:219
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1041
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:356
unsigned logBase2() const
Definition APInt.h:1761
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:827
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:471
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:405
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1150
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:873
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1130
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:296
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:200
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1388
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1237
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:239
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1221
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1449
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
iterator end() const
Definition ArrayRef.h:136
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
iterator begin() const
Definition ArrayRef.h:135
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:191
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:223
LLVM_ABI bool isSingleEdge() const
Check if this is the only edge between Start and End.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:472
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:374
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
This class is the base class for the comparison instructions.
Definition InstrTypes.h:666
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:678
@ ICMP_SLT
signed less than
Definition InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:708
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:684
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:693
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:682
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:683
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:702
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:701
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:705
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:692
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:703
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:690
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:685
@ ICMP_NE
not equal
Definition InstrTypes.h:700
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:706
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:704
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:691
bool isSigned() const
Definition InstrTypes.h:932
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:829
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:944
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:772
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:791
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:767
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:895
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:778
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:938
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:702
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:593
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:668
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:776
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
ConstantFP - Floating Point Values [float, double].
Definition Constants.h:277
This is the shared class of boolean and integer constants.
Definition Constants.h:87
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:163
This class represents a range of values.
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static LLVM_ABI ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
LLVM_ABI OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
LLVM_ABI bool isAllNegative() const
Return true if all values in this range are negative.
LLVM_ABI OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
LLVM_ABI KnownBits toKnownBits() const
Return known bits for values in this range.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other?
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
LLVM_ABI bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static LLVM_ABI ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
LLVM_ABI ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static LLVM_ABI ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
LLVM_ABI bool contains(const APInt &Val) const
Return true if the specified value is in the set.
LLVM_ABI OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
LLVM_ABI ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
LLVM_ABI OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
LLVM_ABI ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition Constant.h:43
static LLVM_ABI Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
LLVM_ABI bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition Constants.cpp:76
LLVM_ABI bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition Constants.cpp:90
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:63
bool isLittleEndian() const
Layout endianness...
Definition DataLayout.h:207
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:760
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
bool hasNoSync() const
Determine if the call can synchroize with other threads.
Definition Function.h:637
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:1078
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:573
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition DataLayout.h:712
TypeSize getElementOffset(unsigned Idx) const
Definition DataLayout.h:743
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:201
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:123
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.
ap_match< APInt > m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
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.
ap_match< APFloat > m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
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)
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.
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
MatchFunctor< Val, Pattern > match_fn(const Pattern &P)
A match functor that can be used as a UnaryPredicate in functional algorithms like all_of.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > extract(Y &&MD)
Extract a Value from Metadata.
Definition Metadata.h:667
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)
LLVM_ABI bool willNotFreeBetween(const Instruction *Assume, const Instruction *CtxI)
Returns true, if no instruction between Assume and CtxI may free memory and the function is marked as...
@ Offset
Definition DWP.cpp:477
@ Length
Definition DWP.cpp:477
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
LLVM_ABI KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, const SimplifyQuery &SQ, unsigned Depth=0)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1705
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1657
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition ScopeExit.h:59
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2452
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:644
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:229
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:289
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2116
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:416
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:293
gep_type_iterator gep_type_end(const User *GEP)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1534
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:677
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:348
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:186
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:754
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1712
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:342
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
generic_gep_type_iterator<> gep_type_iterator
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
LLVM_ABI RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
LLVM_ABI bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
LLVM_ABI bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
LLVM_ABI Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
LLVM_ABI const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
LLVM_ABI Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
LLVM_ABI bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
@ Add
Sum of integers.
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
DWARFExpression::Operation Op
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
constexpr unsigned BitWidth
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &SQ, unsigned Depth=0)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point value can never contain a NaN or infinity.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
gep_type_iterator gep_type_begin(const User *GEP)
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1877
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
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:869
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:324
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:374
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
constexpr bool outputsAreZero() const
Return true if output denormals should be flushed to 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ IEEE
IEEE-754 denormal numbers preserved.
constexpr bool inputsAreZero() const
Return true if input denormals must be implicitly treated as 0.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getIEEE()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:301
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static LLVM_ABI std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:186
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:255
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:124
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:251
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:242
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:274
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
Definition KnownBits.h:119
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:161
KnownBits byteSwap() const
Definition KnownBits.h:514
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:289
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:518
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:172
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:321
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:225
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:311
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:180
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:245
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:347
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:196
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:248
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:326
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:353
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:280
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:92
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:219
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:167
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:206
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a zero.
bool isUnknown() const
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
LLVM_ABI void propagateCanonicalizingSrc(const KnownFPClass &Src, DenormalMode Mode)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void signBitMustBeZero()
Assume the sign bit is zero.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a negative zero.
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC