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