Thanks to visit codestin.com
Credit goes to llvm.org

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::ctpop:
7423 case Intrinsic::bswap:
7424 case Intrinsic::bitreverse:
7425 case Intrinsic::fshl:
7426 case Intrinsic::fshr:
7427 case Intrinsic::smax:
7428 case Intrinsic::smin:
7429 case Intrinsic::scmp:
7430 case Intrinsic::umax:
7431 case Intrinsic::umin:
7432 case Intrinsic::ucmp:
7433 case Intrinsic::ptrmask:
7434 case Intrinsic::fptoui_sat:
7435 case Intrinsic::fptosi_sat:
7436 case Intrinsic::sadd_with_overflow:
7437 case Intrinsic::ssub_with_overflow:
7438 case Intrinsic::smul_with_overflow:
7439 case Intrinsic::uadd_with_overflow:
7440 case Intrinsic::usub_with_overflow:
7441 case Intrinsic::umul_with_overflow:
7442 case Intrinsic::sadd_sat:
7443 case Intrinsic::uadd_sat:
7444 case Intrinsic::ssub_sat:
7445 case Intrinsic::usub_sat:
7446 return false;
7447 case Intrinsic::sshl_sat:
7448 case Intrinsic::ushl_sat:
7449 return includesPoison(Kind) &&
7450 !shiftAmountKnownInRange(II->getArgOperand(1));
7451 case Intrinsic::fma:
7452 case Intrinsic::fmuladd:
7453 case Intrinsic::sqrt:
7454 case Intrinsic::powi:
7455 case Intrinsic::sin:
7456 case Intrinsic::cos:
7457 case Intrinsic::pow:
7458 case Intrinsic::log:
7459 case Intrinsic::log10:
7460 case Intrinsic::log2:
7461 case Intrinsic::exp:
7462 case Intrinsic::exp2:
7463 case Intrinsic::exp10:
7464 case Intrinsic::fabs:
7465 case Intrinsic::copysign:
7466 case Intrinsic::floor:
7467 case Intrinsic::ceil:
7468 case Intrinsic::trunc:
7469 case Intrinsic::rint:
7470 case Intrinsic::nearbyint:
7471 case Intrinsic::round:
7472 case Intrinsic::roundeven:
7473 case Intrinsic::fptrunc_round:
7474 case Intrinsic::canonicalize:
7475 case Intrinsic::arithmetic_fence:
7476 case Intrinsic::minnum:
7477 case Intrinsic::maxnum:
7478 case Intrinsic::minimum:
7479 case Intrinsic::maximum:
7480 case Intrinsic::minimumnum:
7481 case Intrinsic::maximumnum:
7482 case Intrinsic::is_fpclass:
7483 case Intrinsic::ldexp:
7484 case Intrinsic::frexp:
7485 return false;
7486 case Intrinsic::lround:
7487 case Intrinsic::llround:
7488 case Intrinsic::lrint:
7489 case Intrinsic::llrint:
7490 // If the value doesn't fit an unspecified value is returned (but this
7491 // is not poison).
7492 return false;
7493 }
7494 }
7495 [[fallthrough]];
7496 case Instruction::CallBr:
7497 case Instruction::Invoke: {
7498 const auto *CB = cast<CallBase>(Op);
7499 return !CB->hasRetAttr(Attribute::NoUndef);
7500 }
7501 case Instruction::InsertElement:
7502 case Instruction::ExtractElement: {
7503 // If index exceeds the length of the vector, it returns poison
7504 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7505 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7506 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7507 if (includesPoison(Kind))
7508 return !Idx ||
7509 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7510 return false;
7511 }
7512 case Instruction::ShuffleVector: {
7514 ? cast<ConstantExpr>(Op)->getShuffleMask()
7515 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7516 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7517 }
7518 case Instruction::FNeg:
7519 case Instruction::PHI:
7520 case Instruction::Select:
7521 case Instruction::ExtractValue:
7522 case Instruction::InsertValue:
7523 case Instruction::Freeze:
7524 case Instruction::ICmp:
7525 case Instruction::FCmp:
7526 case Instruction::GetElementPtr:
7527 return false;
7528 case Instruction::AddrSpaceCast:
7529 return true;
7530 default: {
7531 const auto *CE = dyn_cast<ConstantExpr>(Op);
7532 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7533 return false;
7534 else if (Instruction::isBinaryOp(Opcode))
7535 return false;
7536 // Be conservative and return true.
7537 return true;
7538 }
7539 }
7540}
7541
7543 bool ConsiderFlagsAndMetadata) {
7544 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7545 ConsiderFlagsAndMetadata);
7546}
7547
7548bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7549 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7550 ConsiderFlagsAndMetadata);
7551}
7552
7553static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7554 unsigned Depth) {
7555 if (ValAssumedPoison == V)
7556 return true;
7557
7558 const unsigned MaxDepth = 2;
7559 if (Depth >= MaxDepth)
7560 return false;
7561
7562 if (const auto *I = dyn_cast<Instruction>(V)) {
7563 if (any_of(I->operands(), [=](const Use &Op) {
7564 return propagatesPoison(Op) &&
7565 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7566 }))
7567 return true;
7568
7569 // V = extractvalue V0, idx
7570 // V2 = extractvalue V0, idx2
7571 // V0's elements are all poison or not. (e.g., add_with_overflow)
7572 const WithOverflowInst *II;
7574 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7575 llvm::is_contained(II->args(), ValAssumedPoison)))
7576 return true;
7577 }
7578 return false;
7579}
7580
7581static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7582 unsigned Depth) {
7583 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7584 return true;
7585
7586 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7587 return true;
7588
7589 const unsigned MaxDepth = 2;
7590 if (Depth >= MaxDepth)
7591 return false;
7592
7593 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7594 if (I && !canCreatePoison(cast<Operator>(I))) {
7595 return all_of(I->operands(), [=](const Value *Op) {
7596 return impliesPoison(Op, V, Depth + 1);
7597 });
7598 }
7599 return false;
7600}
7601
7602bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7603 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7604}
7605
7606static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7607
7609 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7610 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7612 return false;
7613
7614 if (isa<MetadataAsValue>(V))
7615 return false;
7616
7617 if (const auto *A = dyn_cast<Argument>(V)) {
7618 if (A->hasAttribute(Attribute::NoUndef) ||
7619 A->hasAttribute(Attribute::Dereferenceable) ||
7620 A->hasAttribute(Attribute::DereferenceableOrNull))
7621 return true;
7622 }
7623
7624 if (auto *C = dyn_cast<Constant>(V)) {
7625 if (isa<PoisonValue>(C))
7626 return !includesPoison(Kind);
7627
7628 if (isa<UndefValue>(C))
7629 return !includesUndef(Kind);
7630
7633 return true;
7634
7635 if (C->getType()->isVectorTy()) {
7636 if (isa<ConstantExpr>(C)) {
7637 // Scalable vectors can use a ConstantExpr to build a splat.
7638 if (Constant *SplatC = C->getSplatValue())
7639 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7640 return true;
7641 } else {
7642 if (includesUndef(Kind) && C->containsUndefElement())
7643 return false;
7644 if (includesPoison(Kind) && C->containsPoisonElement())
7645 return false;
7646 return !C->containsConstantExpression();
7647 }
7648 }
7649 }
7650
7651 // Strip cast operations from a pointer value.
7652 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7653 // inbounds with zero offset. To guarantee that the result isn't poison, the
7654 // stripped pointer is checked as it has to be pointing into an allocated
7655 // object or be null `null` to ensure `inbounds` getelement pointers with a
7656 // zero offset could not produce poison.
7657 // It can strip off addrspacecast that do not change bit representation as
7658 // well. We believe that such addrspacecast is equivalent to no-op.
7659 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7660 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7661 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7662 return true;
7663
7664 auto OpCheck = [&](const Value *V) {
7665 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7666 };
7667
7668 if (auto *Opr = dyn_cast<Operator>(V)) {
7669 // If the value is a freeze instruction, then it can never
7670 // be undef or poison.
7671 if (isa<FreezeInst>(V))
7672 return true;
7673
7674 if (const auto *CB = dyn_cast<CallBase>(V)) {
7675 if (CB->hasRetAttr(Attribute::NoUndef) ||
7676 CB->hasRetAttr(Attribute::Dereferenceable) ||
7677 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7678 return true;
7679 }
7680
7681 if (!::canCreateUndefOrPoison(Opr, Kind,
7682 /*ConsiderFlagsAndMetadata=*/true)) {
7683 if (const auto *PN = dyn_cast<PHINode>(V)) {
7684 unsigned Num = PN->getNumIncomingValues();
7685 bool IsWellDefined = true;
7686 for (unsigned i = 0; i < Num; ++i) {
7687 if (PN == PN->getIncomingValue(i))
7688 continue;
7689 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7690 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7691 DT, Depth + 1, Kind)) {
7692 IsWellDefined = false;
7693 break;
7694 }
7695 }
7696 if (IsWellDefined)
7697 return true;
7698 } else if (all_of(Opr->operands(), OpCheck))
7699 return true;
7700 }
7701 }
7702
7703 if (auto *I = dyn_cast<LoadInst>(V))
7704 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7705 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7706 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7707 return true;
7708
7710 return true;
7711
7712 // CxtI may be null or a cloned instruction.
7713 if (!CtxI || !CtxI->getParent() || !DT)
7714 return false;
7715
7716 auto *DNode = DT->getNode(CtxI->getParent());
7717 if (!DNode)
7718 // Unreachable block
7719 return false;
7720
7721 // If V is used as a branch condition before reaching CtxI, V cannot be
7722 // undef or poison.
7723 // br V, BB1, BB2
7724 // BB1:
7725 // CtxI ; V cannot be undef or poison here
7726 auto *Dominator = DNode->getIDom();
7727 // This check is purely for compile time reasons: we can skip the IDom walk
7728 // if what we are checking for includes undef and the value is not an integer.
7729 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7730 while (Dominator) {
7731 auto *TI = Dominator->getBlock()->getTerminator();
7732
7733 Value *Cond = nullptr;
7734 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7735 if (BI->isConditional())
7736 Cond = BI->getCondition();
7737 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7738 Cond = SI->getCondition();
7739 }
7740
7741 if (Cond) {
7742 if (Cond == V)
7743 return true;
7744 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7745 // For poison, we can analyze further
7746 auto *Opr = cast<Operator>(Cond);
7747 if (any_of(Opr->operands(), [V](const Use &U) {
7748 return V == U && propagatesPoison(U);
7749 }))
7750 return true;
7751 }
7752 }
7753
7754 Dominator = Dominator->getIDom();
7755 }
7756
7757 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7758 return true;
7759
7760 return false;
7761}
7762
7764 const Instruction *CtxI,
7765 const DominatorTree *DT,
7766 unsigned Depth) {
7767 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7769}
7770
7772 const Instruction *CtxI,
7773 const DominatorTree *DT, unsigned Depth) {
7774 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7776}
7777
7779 const Instruction *CtxI,
7780 const DominatorTree *DT, unsigned Depth) {
7781 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7783}
7784
7785/// Return true if undefined behavior would provably be executed on the path to
7786/// OnPathTo if Root produced a posion result. Note that this doesn't say
7787/// anything about whether OnPathTo is actually executed or whether Root is
7788/// actually poison. This can be used to assess whether a new use of Root can
7789/// be added at a location which is control equivalent with OnPathTo (such as
7790/// immediately before it) without introducing UB which didn't previously
7791/// exist. Note that a false result conveys no information.
7793 Instruction *OnPathTo,
7794 DominatorTree *DT) {
7795 // Basic approach is to assume Root is poison, propagate poison forward
7796 // through all users we can easily track, and then check whether any of those
7797 // users are provable UB and must execute before out exiting block might
7798 // exit.
7799
7800 // The set of all recursive users we've visited (which are assumed to all be
7801 // poison because of said visit)
7804 Worklist.push_back(Root);
7805 while (!Worklist.empty()) {
7806 const Instruction *I = Worklist.pop_back_val();
7807
7808 // If we know this must trigger UB on a path leading our target.
7809 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7810 return true;
7811
7812 // If we can't analyze propagation through this instruction, just skip it
7813 // and transitive users. Safe as false is a conservative result.
7814 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7815 return KnownPoison.contains(U) && propagatesPoison(U);
7816 }))
7817 continue;
7818
7819 if (KnownPoison.insert(I).second)
7820 for (const User *User : I->users())
7821 Worklist.push_back(cast<Instruction>(User));
7822 }
7823
7824 // Might be non-UB, or might have a path we couldn't prove must execute on
7825 // way to exiting bb.
7826 return false;
7827}
7828
7830 const SimplifyQuery &SQ) {
7831 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7832 Add, SQ);
7833}
7834
7837 const WithCache<const Value *> &RHS,
7838 const SimplifyQuery &SQ) {
7839 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7840}
7841
7843 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7844 // of time because it's possible for another thread to interfere with it for an
7845 // arbitrary length of time, but programs aren't allowed to rely on that.
7846
7847 // If there is no successor, then execution can't transfer to it.
7848 if (isa<ReturnInst>(I))
7849 return false;
7851 return false;
7852
7853 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7854 // Instruction::willReturn.
7855 //
7856 // FIXME: Move this check into Instruction::willReturn.
7857 if (isa<CatchPadInst>(I)) {
7858 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7859 default:
7860 // A catchpad may invoke exception object constructors and such, which
7861 // in some languages can be arbitrary code, so be conservative by default.
7862 return false;
7864 // For CoreCLR, it just involves a type test.
7865 return true;
7866 }
7867 }
7868
7869 // An instruction that returns without throwing must transfer control flow
7870 // to a successor.
7871 return !I->mayThrow() && I->willReturn();
7872}
7873
7875 // TODO: This is slightly conservative for invoke instruction since exiting
7876 // via an exception *is* normal control for them.
7877 for (const Instruction &I : *BB)
7879 return false;
7880 return true;
7881}
7882
7889
7892 assert(ScanLimit && "scan limit must be non-zero");
7893 for (const Instruction &I : Range) {
7894 if (--ScanLimit == 0)
7895 return false;
7897 return false;
7898 }
7899 return true;
7900}
7901
7903 const Loop *L) {
7904 // The loop header is guaranteed to be executed for every iteration.
7905 //
7906 // FIXME: Relax this constraint to cover all basic blocks that are
7907 // guaranteed to be executed at every iteration.
7908 if (I->getParent() != L->getHeader()) return false;
7909
7910 for (const Instruction &LI : *L->getHeader()) {
7911 if (&LI == I) return true;
7912 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7913 }
7914 llvm_unreachable("Instruction not contained in its own parent basic block.");
7915}
7916
7918 switch (IID) {
7919 // TODO: Add more intrinsics.
7920 case Intrinsic::sadd_with_overflow:
7921 case Intrinsic::ssub_with_overflow:
7922 case Intrinsic::smul_with_overflow:
7923 case Intrinsic::uadd_with_overflow:
7924 case Intrinsic::usub_with_overflow:
7925 case Intrinsic::umul_with_overflow:
7926 // If an input is a vector containing a poison element, the
7927 // two output vectors (calculated results, overflow bits)'
7928 // corresponding lanes are poison.
7929 return true;
7930 case Intrinsic::ctpop:
7931 case Intrinsic::ctlz:
7932 case Intrinsic::cttz:
7933 case Intrinsic::abs:
7934 case Intrinsic::smax:
7935 case Intrinsic::smin:
7936 case Intrinsic::umax:
7937 case Intrinsic::umin:
7938 case Intrinsic::scmp:
7939 case Intrinsic::is_fpclass:
7940 case Intrinsic::ptrmask:
7941 case Intrinsic::ucmp:
7942 case Intrinsic::bitreverse:
7943 case Intrinsic::bswap:
7944 case Intrinsic::sadd_sat:
7945 case Intrinsic::ssub_sat:
7946 case Intrinsic::sshl_sat:
7947 case Intrinsic::uadd_sat:
7948 case Intrinsic::usub_sat:
7949 case Intrinsic::ushl_sat:
7950 case Intrinsic::smul_fix:
7951 case Intrinsic::smul_fix_sat:
7952 case Intrinsic::umul_fix:
7953 case Intrinsic::umul_fix_sat:
7954 case Intrinsic::pow:
7955 case Intrinsic::powi:
7956 case Intrinsic::sin:
7957 case Intrinsic::sinh:
7958 case Intrinsic::cos:
7959 case Intrinsic::cosh:
7960 case Intrinsic::sincos:
7961 case Intrinsic::sincospi:
7962 case Intrinsic::tan:
7963 case Intrinsic::tanh:
7964 case Intrinsic::asin:
7965 case Intrinsic::acos:
7966 case Intrinsic::atan:
7967 case Intrinsic::atan2:
7968 case Intrinsic::canonicalize:
7969 case Intrinsic::sqrt:
7970 case Intrinsic::exp:
7971 case Intrinsic::exp2:
7972 case Intrinsic::exp10:
7973 case Intrinsic::log:
7974 case Intrinsic::log2:
7975 case Intrinsic::log10:
7976 case Intrinsic::modf:
7977 case Intrinsic::floor:
7978 case Intrinsic::ceil:
7979 case Intrinsic::trunc:
7980 case Intrinsic::rint:
7981 case Intrinsic::nearbyint:
7982 case Intrinsic::round:
7983 case Intrinsic::roundeven:
7984 case Intrinsic::lrint:
7985 case Intrinsic::llrint:
7986 return true;
7987 default:
7988 return false;
7989 }
7990}
7991
7992bool llvm::propagatesPoison(const Use &PoisonOp) {
7993 const Operator *I = cast<Operator>(PoisonOp.getUser());
7994 switch (I->getOpcode()) {
7995 case Instruction::Freeze:
7996 case Instruction::PHI:
7997 case Instruction::Invoke:
7998 return false;
7999 case Instruction::Select:
8000 return PoisonOp.getOperandNo() == 0;
8001 case Instruction::Call:
8002 if (auto *II = dyn_cast<IntrinsicInst>(I))
8003 return intrinsicPropagatesPoison(II->getIntrinsicID());
8004 return false;
8005 case Instruction::ICmp:
8006 case Instruction::FCmp:
8007 case Instruction::GetElementPtr:
8008 return true;
8009 default:
8011 return true;
8012
8013 // Be conservative and return false.
8014 return false;
8015 }
8016}
8017
8018/// Enumerates all operands of \p I that are guaranteed to not be undef or
8019/// poison. If the callback \p Handle returns true, stop processing and return
8020/// true. Otherwise, return false.
8021template <typename CallableT>
8023 const CallableT &Handle) {
8024 switch (I->getOpcode()) {
8025 case Instruction::Store:
8026 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8027 return true;
8028 break;
8029
8030 case Instruction::Load:
8031 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8032 return true;
8033 break;
8034
8035 // Since dereferenceable attribute imply noundef, atomic operations
8036 // also implicitly have noundef pointers too
8037 case Instruction::AtomicCmpXchg:
8039 return true;
8040 break;
8041
8042 case Instruction::AtomicRMW:
8043 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8044 return true;
8045 break;
8046
8047 case Instruction::Call:
8048 case Instruction::Invoke: {
8049 const CallBase *CB = cast<CallBase>(I);
8050 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8051 return true;
8052 for (unsigned i = 0; i < CB->arg_size(); ++i)
8053 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8054 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8055 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8056 Handle(CB->getArgOperand(i)))
8057 return true;
8058 break;
8059 }
8060 case Instruction::Ret:
8061 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8062 Handle(I->getOperand(0)))
8063 return true;
8064 break;
8065 case Instruction::Switch:
8066 if (Handle(cast<SwitchInst>(I)->getCondition()))
8067 return true;
8068 break;
8069 case Instruction::Br: {
8070 auto *BR = cast<BranchInst>(I);
8071 if (BR->isConditional() && Handle(BR->getCondition()))
8072 return true;
8073 break;
8074 }
8075 default:
8076 break;
8077 }
8078
8079 return false;
8080}
8081
8082/// Enumerates all operands of \p I that are guaranteed to not be poison.
8083template <typename CallableT>
8085 const CallableT &Handle) {
8086 if (handleGuaranteedWellDefinedOps(I, Handle))
8087 return true;
8088 switch (I->getOpcode()) {
8089 // Divisors of these operations are allowed to be partially undef.
8090 case Instruction::UDiv:
8091 case Instruction::SDiv:
8092 case Instruction::URem:
8093 case Instruction::SRem:
8094 return Handle(I->getOperand(1));
8095 default:
8096 return false;
8097 }
8098}
8099
8101 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8103 I, [&](const Value *V) { return KnownPoison.count(V); });
8104}
8105
8107 bool PoisonOnly) {
8108 // We currently only look for uses of values within the same basic
8109 // block, as that makes it easier to guarantee that the uses will be
8110 // executed given that Inst is executed.
8111 //
8112 // FIXME: Expand this to consider uses beyond the same basic block. To do
8113 // this, look out for the distinction between post-dominance and strong
8114 // post-dominance.
8115 const BasicBlock *BB = nullptr;
8117 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8118 BB = Inst->getParent();
8119 Begin = Inst->getIterator();
8120 Begin++;
8121 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8122 if (Arg->getParent()->isDeclaration())
8123 return false;
8124 BB = &Arg->getParent()->getEntryBlock();
8125 Begin = BB->begin();
8126 } else {
8127 return false;
8128 }
8129
8130 // Limit number of instructions we look at, to avoid scanning through large
8131 // blocks. The current limit is chosen arbitrarily.
8132 unsigned ScanLimit = 32;
8133 BasicBlock::const_iterator End = BB->end();
8134
8135 if (!PoisonOnly) {
8136 // Since undef does not propagate eagerly, be conservative & just check
8137 // whether a value is directly passed to an instruction that must take
8138 // well-defined operands.
8139
8140 for (const auto &I : make_range(Begin, End)) {
8141 if (--ScanLimit == 0)
8142 break;
8143
8144 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8145 return WellDefinedOp == V;
8146 }))
8147 return true;
8148
8150 break;
8151 }
8152 return false;
8153 }
8154
8155 // Set of instructions that we have proved will yield poison if Inst
8156 // does.
8157 SmallPtrSet<const Value *, 16> YieldsPoison;
8159
8160 YieldsPoison.insert(V);
8161 Visited.insert(BB);
8162
8163 while (true) {
8164 for (const auto &I : make_range(Begin, End)) {
8165 if (--ScanLimit == 0)
8166 return false;
8167 if (mustTriggerUB(&I, YieldsPoison))
8168 return true;
8170 return false;
8171
8172 // If an operand is poison and propagates it, mark I as yielding poison.
8173 for (const Use &Op : I.operands()) {
8174 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8175 YieldsPoison.insert(&I);
8176 break;
8177 }
8178 }
8179
8180 // Special handling for select, which returns poison if its operand 0 is
8181 // poison (handled in the loop above) *or* if both its true/false operands
8182 // are poison (handled here).
8183 if (I.getOpcode() == Instruction::Select &&
8184 YieldsPoison.count(I.getOperand(1)) &&
8185 YieldsPoison.count(I.getOperand(2))) {
8186 YieldsPoison.insert(&I);
8187 }
8188 }
8189
8190 BB = BB->getSingleSuccessor();
8191 if (!BB || !Visited.insert(BB).second)
8192 break;
8193
8194 Begin = BB->getFirstNonPHIIt();
8195 End = BB->end();
8196 }
8197 return false;
8198}
8199
8201 return ::programUndefinedIfUndefOrPoison(Inst, false);
8202}
8203
8205 return ::programUndefinedIfUndefOrPoison(Inst, true);
8206}
8207
8208static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8209 if (FMF.noNaNs())
8210 return true;
8211
8212 if (auto *C = dyn_cast<ConstantFP>(V))
8213 return !C->isNaN();
8214
8215 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8216 if (!C->getElementType()->isFloatingPointTy())
8217 return false;
8218 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8219 if (C->getElementAsAPFloat(I).isNaN())
8220 return false;
8221 }
8222 return true;
8223 }
8224
8226 return true;
8227
8228 return false;
8229}
8230
8231static bool isKnownNonZero(const Value *V) {
8232 if (auto *C = dyn_cast<ConstantFP>(V))
8233 return !C->isZero();
8234
8235 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8236 if (!C->getElementType()->isFloatingPointTy())
8237 return false;
8238 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8239 if (C->getElementAsAPFloat(I).isZero())
8240 return false;
8241 }
8242 return true;
8243 }
8244
8245 return false;
8246}
8247
8248/// Match clamp pattern for float types without care about NaNs or signed zeros.
8249/// Given non-min/max outer cmp/select from the clamp pattern this
8250/// function recognizes if it can be substitued by a "canonical" min/max
8251/// pattern.
8253 Value *CmpLHS, Value *CmpRHS,
8254 Value *TrueVal, Value *FalseVal,
8255 Value *&LHS, Value *&RHS) {
8256 // Try to match
8257 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8258 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8259 // and return description of the outer Max/Min.
8260
8261 // First, check if select has inverse order:
8262 if (CmpRHS == FalseVal) {
8263 std::swap(TrueVal, FalseVal);
8264 Pred = CmpInst::getInversePredicate(Pred);
8265 }
8266
8267 // Assume success now. If there's no match, callers should not use these anyway.
8268 LHS = TrueVal;
8269 RHS = FalseVal;
8270
8271 const APFloat *FC1;
8272 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8273 return {SPF_UNKNOWN, SPNB_NA, false};
8274
8275 const APFloat *FC2;
8276 switch (Pred) {
8277 case CmpInst::FCMP_OLT:
8278 case CmpInst::FCMP_OLE:
8279 case CmpInst::FCMP_ULT:
8280 case CmpInst::FCMP_ULE:
8281 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8282 *FC1 < *FC2)
8283 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8284 break;
8285 case CmpInst::FCMP_OGT:
8286 case CmpInst::FCMP_OGE:
8287 case CmpInst::FCMP_UGT:
8288 case CmpInst::FCMP_UGE:
8289 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8290 *FC1 > *FC2)
8291 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8292 break;
8293 default:
8294 break;
8295 }
8296
8297 return {SPF_UNKNOWN, SPNB_NA, false};
8298}
8299
8300/// Recognize variations of:
8301/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8303 Value *CmpLHS, Value *CmpRHS,
8304 Value *TrueVal, Value *FalseVal) {
8305 // Swap the select operands and predicate to match the patterns below.
8306 if (CmpRHS != TrueVal) {
8307 Pred = ICmpInst::getSwappedPredicate(Pred);
8308 std::swap(TrueVal, FalseVal);
8309 }
8310 const APInt *C1;
8311 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8312 const APInt *C2;
8313 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8314 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8315 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8316 return {SPF_SMAX, SPNB_NA, false};
8317
8318 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8319 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8320 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8321 return {SPF_SMIN, SPNB_NA, false};
8322
8323 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8324 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8325 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8326 return {SPF_UMAX, SPNB_NA, false};
8327
8328 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8329 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8330 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8331 return {SPF_UMIN, SPNB_NA, false};
8332 }
8333 return {SPF_UNKNOWN, SPNB_NA, false};
8334}
8335
8336/// Recognize variations of:
8337/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8339 Value *CmpLHS, Value *CmpRHS,
8340 Value *TVal, Value *FVal,
8341 unsigned Depth) {
8342 // TODO: Allow FP min/max with nnan/nsz.
8343 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8344
8345 Value *A = nullptr, *B = nullptr;
8346 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8347 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8348 return {SPF_UNKNOWN, SPNB_NA, false};
8349
8350 Value *C = nullptr, *D = nullptr;
8351 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8352 if (L.Flavor != R.Flavor)
8353 return {SPF_UNKNOWN, SPNB_NA, false};
8354
8355 // We have something like: x Pred y ? min(a, b) : min(c, d).
8356 // Try to match the compare to the min/max operations of the select operands.
8357 // First, make sure we have the right compare predicate.
8358 switch (L.Flavor) {
8359 case SPF_SMIN:
8360 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8361 Pred = ICmpInst::getSwappedPredicate(Pred);
8362 std::swap(CmpLHS, CmpRHS);
8363 }
8364 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8365 break;
8366 return {SPF_UNKNOWN, SPNB_NA, false};
8367 case SPF_SMAX:
8368 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8369 Pred = ICmpInst::getSwappedPredicate(Pred);
8370 std::swap(CmpLHS, CmpRHS);
8371 }
8372 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8373 break;
8374 return {SPF_UNKNOWN, SPNB_NA, false};
8375 case SPF_UMIN:
8376 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8377 Pred = ICmpInst::getSwappedPredicate(Pred);
8378 std::swap(CmpLHS, CmpRHS);
8379 }
8380 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8381 break;
8382 return {SPF_UNKNOWN, SPNB_NA, false};
8383 case SPF_UMAX:
8384 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8385 Pred = ICmpInst::getSwappedPredicate(Pred);
8386 std::swap(CmpLHS, CmpRHS);
8387 }
8388 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8389 break;
8390 return {SPF_UNKNOWN, SPNB_NA, false};
8391 default:
8392 return {SPF_UNKNOWN, SPNB_NA, false};
8393 }
8394
8395 // If there is a common operand in the already matched min/max and the other
8396 // min/max operands match the compare operands (either directly or inverted),
8397 // then this is min/max of the same flavor.
8398
8399 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8400 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8401 if (D == B) {
8402 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8403 match(A, m_Not(m_Specific(CmpRHS)))))
8404 return {L.Flavor, SPNB_NA, false};
8405 }
8406 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8407 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8408 if (C == B) {
8409 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8410 match(A, m_Not(m_Specific(CmpRHS)))))
8411 return {L.Flavor, SPNB_NA, false};
8412 }
8413 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8414 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8415 if (D == A) {
8416 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8417 match(B, m_Not(m_Specific(CmpRHS)))))
8418 return {L.Flavor, SPNB_NA, false};
8419 }
8420 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8421 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8422 if (C == A) {
8423 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8424 match(B, m_Not(m_Specific(CmpRHS)))))
8425 return {L.Flavor, SPNB_NA, false};
8426 }
8427
8428 return {SPF_UNKNOWN, SPNB_NA, false};
8429}
8430
8431/// If the input value is the result of a 'not' op, constant integer, or vector
8432/// splat of a constant integer, return the bitwise-not source value.
8433/// TODO: This could be extended to handle non-splat vector integer constants.
8435 Value *NotV;
8436 if (match(V, m_Not(m_Value(NotV))))
8437 return NotV;
8438
8439 const APInt *C;
8440 if (match(V, m_APInt(C)))
8441 return ConstantInt::get(V->getType(), ~(*C));
8442
8443 return nullptr;
8444}
8445
8446/// Match non-obvious integer minimum and maximum sequences.
8448 Value *CmpLHS, Value *CmpRHS,
8449 Value *TrueVal, Value *FalseVal,
8450 Value *&LHS, Value *&RHS,
8451 unsigned Depth) {
8452 // Assume success. If there's no match, callers should not use these anyway.
8453 LHS = TrueVal;
8454 RHS = FalseVal;
8455
8456 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8458 return SPR;
8459
8460 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8462 return SPR;
8463
8464 // Look through 'not' ops to find disguised min/max.
8465 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8466 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8467 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8468 switch (Pred) {
8469 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8470 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8471 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8472 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8473 default: break;
8474 }
8475 }
8476
8477 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8478 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8479 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8480 switch (Pred) {
8481 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8482 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8483 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8484 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8485 default: break;
8486 }
8487 }
8488
8489 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8490 return {SPF_UNKNOWN, SPNB_NA, false};
8491
8492 const APInt *C1;
8493 if (!match(CmpRHS, m_APInt(C1)))
8494 return {SPF_UNKNOWN, SPNB_NA, false};
8495
8496 // An unsigned min/max can be written with a signed compare.
8497 const APInt *C2;
8498 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8499 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8500 // Is the sign bit set?
8501 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8502 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8503 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8504 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8505
8506 // Is the sign bit clear?
8507 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8508 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8509 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8510 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8511 }
8512
8513 return {SPF_UNKNOWN, SPNB_NA, false};
8514}
8515
8516bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8517 bool AllowPoison) {
8518 assert(X && Y && "Invalid operand");
8519
8520 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8521 if (!match(X, m_Neg(m_Specific(Y))))
8522 return false;
8523
8524 auto *BO = cast<BinaryOperator>(X);
8525 if (NeedNSW && !BO->hasNoSignedWrap())
8526 return false;
8527
8528 auto *Zero = cast<Constant>(BO->getOperand(0));
8529 if (!AllowPoison && !Zero->isNullValue())
8530 return false;
8531
8532 return true;
8533 };
8534
8535 // X = -Y or Y = -X
8536 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8537 return true;
8538
8539 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8540 Value *A, *B;
8541 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8542 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8543 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8545}
8546
8547bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8548 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8549 Value *A, *B, *C;
8550 CmpPredicate Pred1, Pred2;
8551 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8552 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8553 return false;
8554
8555 // They must both have samesign flag or not.
8556 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8557 return false;
8558
8559 if (B == C)
8560 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8561
8562 // Try to infer the relationship from constant ranges.
8563 const APInt *RHSC1, *RHSC2;
8564 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8565 return false;
8566
8567 // Sign bits of two RHSCs should match.
8568 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8569 return false;
8570
8571 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8572 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8573
8574 return CR1.inverse() == CR2;
8575}
8576
8578 SelectPatternNaNBehavior NaNBehavior,
8579 bool Ordered) {
8580 switch (Pred) {
8581 default:
8582 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8583 case ICmpInst::ICMP_UGT:
8584 case ICmpInst::ICMP_UGE:
8585 return {SPF_UMAX, SPNB_NA, false};
8586 case ICmpInst::ICMP_SGT:
8587 case ICmpInst::ICMP_SGE:
8588 return {SPF_SMAX, SPNB_NA, false};
8589 case ICmpInst::ICMP_ULT:
8590 case ICmpInst::ICMP_ULE:
8591 return {SPF_UMIN, SPNB_NA, false};
8592 case ICmpInst::ICMP_SLT:
8593 case ICmpInst::ICMP_SLE:
8594 return {SPF_SMIN, SPNB_NA, false};
8595 case FCmpInst::FCMP_UGT:
8596 case FCmpInst::FCMP_UGE:
8597 case FCmpInst::FCMP_OGT:
8598 case FCmpInst::FCMP_OGE:
8599 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8600 case FCmpInst::FCMP_ULT:
8601 case FCmpInst::FCMP_ULE:
8602 case FCmpInst::FCMP_OLT:
8603 case FCmpInst::FCMP_OLE:
8604 return {SPF_FMINNUM, NaNBehavior, Ordered};
8605 }
8606}
8607
8608std::optional<std::pair<CmpPredicate, Constant *>>
8611 "Only for relational integer predicates.");
8612 if (isa<UndefValue>(C))
8613 return std::nullopt;
8614
8615 Type *Type = C->getType();
8616 bool IsSigned = ICmpInst::isSigned(Pred);
8617
8619 bool WillIncrement =
8620 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8621
8622 // Check if the constant operand can be safely incremented/decremented
8623 // without overflowing/underflowing.
8624 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8625 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8626 };
8627
8628 Constant *SafeReplacementConstant = nullptr;
8629 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8630 // Bail out if the constant can't be safely incremented/decremented.
8631 if (!ConstantIsOk(CI))
8632 return std::nullopt;
8633 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8634 unsigned NumElts = FVTy->getNumElements();
8635 for (unsigned i = 0; i != NumElts; ++i) {
8636 Constant *Elt = C->getAggregateElement(i);
8637 if (!Elt)
8638 return std::nullopt;
8639
8640 if (isa<UndefValue>(Elt))
8641 continue;
8642
8643 // Bail out if we can't determine if this constant is min/max or if we
8644 // know that this constant is min/max.
8645 auto *CI = dyn_cast<ConstantInt>(Elt);
8646 if (!CI || !ConstantIsOk(CI))
8647 return std::nullopt;
8648
8649 if (!SafeReplacementConstant)
8650 SafeReplacementConstant = CI;
8651 }
8652 } else if (isa<VectorType>(C->getType())) {
8653 // Handle scalable splat
8654 Value *SplatC = C->getSplatValue();
8655 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8656 // Bail out if the constant can't be safely incremented/decremented.
8657 if (!CI || !ConstantIsOk(CI))
8658 return std::nullopt;
8659 } else {
8660 // ConstantExpr?
8661 return std::nullopt;
8662 }
8663
8664 // It may not be safe to change a compare predicate in the presence of
8665 // undefined elements, so replace those elements with the first safe constant
8666 // that we found.
8667 // TODO: in case of poison, it is safe; let's replace undefs only.
8668 if (C->containsUndefOrPoisonElement()) {
8669 assert(SafeReplacementConstant && "Replacement constant not set");
8670 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8671 }
8672
8674
8675 // Increment or decrement the constant.
8676 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8677 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8678
8679 return std::make_pair(NewPred, NewC);
8680}
8681
8683 FastMathFlags FMF,
8684 Value *CmpLHS, Value *CmpRHS,
8685 Value *TrueVal, Value *FalseVal,
8686 Value *&LHS, Value *&RHS,
8687 unsigned Depth) {
8688 bool HasMismatchedZeros = false;
8689 if (CmpInst::isFPPredicate(Pred)) {
8690 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8691 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8692 // purpose of identifying min/max. Disregard vector constants with undefined
8693 // elements because those can not be back-propagated for analysis.
8694 Value *OutputZeroVal = nullptr;
8695 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8696 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8697 OutputZeroVal = TrueVal;
8698 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8699 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8700 OutputZeroVal = FalseVal;
8701
8702 if (OutputZeroVal) {
8703 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8704 HasMismatchedZeros = true;
8705 CmpLHS = OutputZeroVal;
8706 }
8707 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8708 HasMismatchedZeros = true;
8709 CmpRHS = OutputZeroVal;
8710 }
8711 }
8712 }
8713
8714 LHS = CmpLHS;
8715 RHS = CmpRHS;
8716
8717 // Signed zero may return inconsistent results between implementations.
8718 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8719 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8720 // Therefore, we behave conservatively and only proceed if at least one of the
8721 // operands is known to not be zero or if we don't care about signed zero.
8722 switch (Pred) {
8723 default: break;
8726 if (!HasMismatchedZeros)
8727 break;
8728 [[fallthrough]];
8731 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8732 !isKnownNonZero(CmpRHS))
8733 return {SPF_UNKNOWN, SPNB_NA, false};
8734 }
8735
8736 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8737 bool Ordered = false;
8738
8739 // When given one NaN and one non-NaN input:
8740 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8741 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8742 // ordered comparison fails), which could be NaN or non-NaN.
8743 // so here we discover exactly what NaN behavior is required/accepted.
8744 if (CmpInst::isFPPredicate(Pred)) {
8745 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8746 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8747
8748 if (LHSSafe && RHSSafe) {
8749 // Both operands are known non-NaN.
8750 NaNBehavior = SPNB_RETURNS_ANY;
8751 Ordered = CmpInst::isOrdered(Pred);
8752 } else if (CmpInst::isOrdered(Pred)) {
8753 // An ordered comparison will return false when given a NaN, so it
8754 // returns the RHS.
8755 Ordered = true;
8756 if (LHSSafe)
8757 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8758 NaNBehavior = SPNB_RETURNS_NAN;
8759 else if (RHSSafe)
8760 NaNBehavior = SPNB_RETURNS_OTHER;
8761 else
8762 // Completely unsafe.
8763 return {SPF_UNKNOWN, SPNB_NA, false};
8764 } else {
8765 Ordered = false;
8766 // An unordered comparison will return true when given a NaN, so it
8767 // returns the LHS.
8768 if (LHSSafe)
8769 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8770 NaNBehavior = SPNB_RETURNS_OTHER;
8771 else if (RHSSafe)
8772 NaNBehavior = SPNB_RETURNS_NAN;
8773 else
8774 // Completely unsafe.
8775 return {SPF_UNKNOWN, SPNB_NA, false};
8776 }
8777 }
8778
8779 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8780 std::swap(CmpLHS, CmpRHS);
8781 Pred = CmpInst::getSwappedPredicate(Pred);
8782 if (NaNBehavior == SPNB_RETURNS_NAN)
8783 NaNBehavior = SPNB_RETURNS_OTHER;
8784 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8785 NaNBehavior = SPNB_RETURNS_NAN;
8786 Ordered = !Ordered;
8787 }
8788
8789 // ([if]cmp X, Y) ? X : Y
8790 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8791 return getSelectPattern(Pred, NaNBehavior, Ordered);
8792
8793 if (isKnownNegation(TrueVal, FalseVal)) {
8794 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8795 // match against either LHS or sext(LHS).
8796 auto MaybeSExtCmpLHS =
8797 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8798 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8799 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8800 if (match(TrueVal, MaybeSExtCmpLHS)) {
8801 // Set the return values. If the compare uses the negated value (-X >s 0),
8802 // swap the return values because the negated value is always 'RHS'.
8803 LHS = TrueVal;
8804 RHS = FalseVal;
8805 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8806 std::swap(LHS, RHS);
8807
8808 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8809 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8810 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8811 return {SPF_ABS, SPNB_NA, false};
8812
8813 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8814 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8815 return {SPF_ABS, SPNB_NA, false};
8816
8817 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8818 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8819 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8820 return {SPF_NABS, SPNB_NA, false};
8821 }
8822 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8823 // Set the return values. If the compare uses the negated value (-X >s 0),
8824 // swap the return values because the negated value is always 'RHS'.
8825 LHS = FalseVal;
8826 RHS = TrueVal;
8827 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8828 std::swap(LHS, RHS);
8829
8830 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8831 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8832 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8833 return {SPF_NABS, SPNB_NA, false};
8834
8835 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8836 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8837 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8838 return {SPF_ABS, SPNB_NA, false};
8839 }
8840 }
8841
8842 if (CmpInst::isIntPredicate(Pred))
8843 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8844
8845 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8846 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8847 // semantics than minNum. Be conservative in such case.
8848 if (NaNBehavior != SPNB_RETURNS_ANY ||
8849 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8850 !isKnownNonZero(CmpRHS)))
8851 return {SPF_UNKNOWN, SPNB_NA, false};
8852
8853 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8854}
8855
8857 Instruction::CastOps *CastOp) {
8858 const DataLayout &DL = CmpI->getDataLayout();
8859
8860 Constant *CastedTo = nullptr;
8861 switch (*CastOp) {
8862 case Instruction::ZExt:
8863 if (CmpI->isUnsigned())
8864 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8865 break;
8866 case Instruction::SExt:
8867 if (CmpI->isSigned())
8868 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8869 break;
8870 case Instruction::Trunc:
8871 Constant *CmpConst;
8872 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8873 CmpConst->getType() == SrcTy) {
8874 // Here we have the following case:
8875 //
8876 // %cond = cmp iN %x, CmpConst
8877 // %tr = trunc iN %x to iK
8878 // %narrowsel = select i1 %cond, iK %t, iK C
8879 //
8880 // We can always move trunc after select operation:
8881 //
8882 // %cond = cmp iN %x, CmpConst
8883 // %widesel = select i1 %cond, iN %x, iN CmpConst
8884 // %tr = trunc iN %widesel to iK
8885 //
8886 // Note that C could be extended in any way because we don't care about
8887 // upper bits after truncation. It can't be abs pattern, because it would
8888 // look like:
8889 //
8890 // select i1 %cond, x, -x.
8891 //
8892 // So only min/max pattern could be matched. Such match requires widened C
8893 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8894 // CmpConst == C is checked below.
8895 CastedTo = CmpConst;
8896 } else {
8897 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8898 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8899 }
8900 break;
8901 case Instruction::FPTrunc:
8902 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8903 break;
8904 case Instruction::FPExt:
8905 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8906 break;
8907 case Instruction::FPToUI:
8908 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8909 break;
8910 case Instruction::FPToSI:
8911 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8912 break;
8913 case Instruction::UIToFP:
8914 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8915 break;
8916 case Instruction::SIToFP:
8917 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8918 break;
8919 default:
8920 break;
8921 }
8922
8923 if (!CastedTo)
8924 return nullptr;
8925
8926 // Make sure the cast doesn't lose any information.
8927 Constant *CastedBack =
8928 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8929 if (CastedBack && CastedBack != C)
8930 return nullptr;
8931
8932 return CastedTo;
8933}
8934
8935/// Helps to match a select pattern in case of a type mismatch.
8936///
8937/// The function processes the case when type of true and false values of a
8938/// select instruction differs from type of the cmp instruction operands because
8939/// of a cast instruction. The function checks if it is legal to move the cast
8940/// operation after "select". If yes, it returns the new second value of
8941/// "select" (with the assumption that cast is moved):
8942/// 1. As operand of cast instruction when both values of "select" are same cast
8943/// instructions.
8944/// 2. As restored constant (by applying reverse cast operation) when the first
8945/// value of the "select" is a cast operation and the second value is a
8946/// constant. It is implemented in lookThroughCastConst().
8947/// 3. As one operand is cast instruction and the other is not. The operands in
8948/// sel(cmp) are in different type integer.
8949/// NOTE: We return only the new second value because the first value could be
8950/// accessed as operand of cast instruction.
8951static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8952 Instruction::CastOps *CastOp) {
8953 auto *Cast1 = dyn_cast<CastInst>(V1);
8954 if (!Cast1)
8955 return nullptr;
8956
8957 *CastOp = Cast1->getOpcode();
8958 Type *SrcTy = Cast1->getSrcTy();
8959 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8960 // If V1 and V2 are both the same cast from the same type, look through V1.
8961 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
8962 return Cast2->getOperand(0);
8963 return nullptr;
8964 }
8965
8966 auto *C = dyn_cast<Constant>(V2);
8967 if (C)
8968 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
8969
8970 Value *CastedTo = nullptr;
8971 if (*CastOp == Instruction::Trunc) {
8972 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
8973 // Here we have the following case:
8974 // %y_ext = sext iK %y to iN
8975 // %cond = cmp iN %x, %y_ext
8976 // %tr = trunc iN %x to iK
8977 // %narrowsel = select i1 %cond, iK %tr, iK %y
8978 //
8979 // We can always move trunc after select operation:
8980 // %y_ext = sext iK %y to iN
8981 // %cond = cmp iN %x, %y_ext
8982 // %widesel = select i1 %cond, iN %x, iN %y_ext
8983 // %tr = trunc iN %widesel to iK
8984 assert(V2->getType() == Cast1->getType() &&
8985 "V2 and Cast1 should be the same type.");
8986 CastedTo = CmpI->getOperand(1);
8987 }
8988 }
8989
8990 return CastedTo;
8991}
8993 Instruction::CastOps *CastOp,
8994 unsigned Depth) {
8996 return {SPF_UNKNOWN, SPNB_NA, false};
8997
8999 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9000
9001 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9002 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9003
9004 Value *TrueVal = SI->getTrueValue();
9005 Value *FalseVal = SI->getFalseValue();
9006
9008 CmpI, TrueVal, FalseVal, LHS, RHS,
9009 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
9010 CastOp, Depth);
9011}
9012
9014 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9015 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
9016 CmpInst::Predicate Pred = CmpI->getPredicate();
9017 Value *CmpLHS = CmpI->getOperand(0);
9018 Value *CmpRHS = CmpI->getOperand(1);
9019 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
9020 FMF.setNoNaNs();
9021
9022 // Bail out early.
9023 if (CmpI->isEquality())
9024 return {SPF_UNKNOWN, SPNB_NA, false};
9025
9026 // Deal with type mismatches.
9027 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9028 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9029 // If this is a potential fmin/fmax with a cast to integer, then ignore
9030 // -0.0 because there is no corresponding integer value.
9031 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9032 FMF.setNoSignedZeros();
9033 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9034 cast<CastInst>(TrueVal)->getOperand(0), C,
9035 LHS, RHS, Depth);
9036 }
9037 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9038 // If this is a potential fmin/fmax with a cast to integer, then ignore
9039 // -0.0 because there is no corresponding integer value.
9040 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9041 FMF.setNoSignedZeros();
9042 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9043 C, cast<CastInst>(FalseVal)->getOperand(0),
9044 LHS, RHS, Depth);
9045 }
9046 }
9047 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9048 LHS, RHS, Depth);
9049}
9050
9052 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9053 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9054 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9055 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9056 if (SPF == SPF_FMINNUM)
9057 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9058 if (SPF == SPF_FMAXNUM)
9059 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9060 llvm_unreachable("unhandled!");
9061}
9062
9064 switch (SPF) {
9066 return Intrinsic::umin;
9068 return Intrinsic::umax;
9070 return Intrinsic::smin;
9072 return Intrinsic::smax;
9073 default:
9074 llvm_unreachable("Unexpected SPF");
9075 }
9076}
9077
9079 if (SPF == SPF_SMIN) return SPF_SMAX;
9080 if (SPF == SPF_UMIN) return SPF_UMAX;
9081 if (SPF == SPF_SMAX) return SPF_SMIN;
9082 if (SPF == SPF_UMAX) return SPF_UMIN;
9083 llvm_unreachable("unhandled!");
9084}
9085
9087 switch (MinMaxID) {
9088 case Intrinsic::smax: return Intrinsic::smin;
9089 case Intrinsic::smin: return Intrinsic::smax;
9090 case Intrinsic::umax: return Intrinsic::umin;
9091 case Intrinsic::umin: return Intrinsic::umax;
9092 // Please note that next four intrinsics may produce the same result for
9093 // original and inverted case even if X != Y due to NaN is handled specially.
9094 case Intrinsic::maximum: return Intrinsic::minimum;
9095 case Intrinsic::minimum: return Intrinsic::maximum;
9096 case Intrinsic::maxnum: return Intrinsic::minnum;
9097 case Intrinsic::minnum: return Intrinsic::maxnum;
9098 default: llvm_unreachable("Unexpected intrinsic");
9099 }
9100}
9101
9103 switch (SPF) {
9106 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9107 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9108 default: llvm_unreachable("Unexpected flavor");
9109 }
9110}
9111
9112std::pair<Intrinsic::ID, bool>
9114 // Check if VL contains select instructions that can be folded into a min/max
9115 // vector intrinsic and return the intrinsic if it is possible.
9116 // TODO: Support floating point min/max.
9117 bool AllCmpSingleUse = true;
9118 SelectPatternResult SelectPattern;
9119 SelectPattern.Flavor = SPF_UNKNOWN;
9120 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9121 Value *LHS, *RHS;
9122 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9123 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9124 return false;
9125 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9126 SelectPattern.Flavor != CurrentPattern.Flavor)
9127 return false;
9128 SelectPattern = CurrentPattern;
9129 AllCmpSingleUse &=
9131 return true;
9132 })) {
9133 switch (SelectPattern.Flavor) {
9134 case SPF_SMIN:
9135 return {Intrinsic::smin, AllCmpSingleUse};
9136 case SPF_UMIN:
9137 return {Intrinsic::umin, AllCmpSingleUse};
9138 case SPF_SMAX:
9139 return {Intrinsic::smax, AllCmpSingleUse};
9140 case SPF_UMAX:
9141 return {Intrinsic::umax, AllCmpSingleUse};
9142 case SPF_FMAXNUM:
9143 return {Intrinsic::maxnum, AllCmpSingleUse};
9144 case SPF_FMINNUM:
9145 return {Intrinsic::minnum, AllCmpSingleUse};
9146 default:
9147 llvm_unreachable("unexpected select pattern flavor");
9148 }
9149 }
9150 return {Intrinsic::not_intrinsic, false};
9151}
9152
9153template <typename InstTy>
9154static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9155 Value *&Init, Value *&OtherOp) {
9156 // Handle the case of a simple two-predecessor recurrence PHI.
9157 // There's a lot more that could theoretically be done here, but
9158 // this is sufficient to catch some interesting cases.
9159 // TODO: Expand list -- gep, uadd.sat etc.
9160 if (PN->getNumIncomingValues() != 2)
9161 return false;
9162
9163 for (unsigned I = 0; I != 2; ++I) {
9164 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9165 Operation && Operation->getNumOperands() >= 2) {
9166 Value *LHS = Operation->getOperand(0);
9167 Value *RHS = Operation->getOperand(1);
9168 if (LHS != PN && RHS != PN)
9169 continue;
9170
9171 Inst = Operation;
9172 Init = PN->getIncomingValue(!I);
9173 OtherOp = (LHS == PN) ? RHS : LHS;
9174 return true;
9175 }
9176 }
9177 return false;
9178}
9179
9181 Value *&Start, Value *&Step) {
9182 // We try to match a recurrence of the form:
9183 // %iv = [Start, %entry], [%iv.next, %backedge]
9184 // %iv.next = binop %iv, Step
9185 // Or:
9186 // %iv = [Start, %entry], [%iv.next, %backedge]
9187 // %iv.next = binop Step, %iv
9188 return matchTwoInputRecurrence(P, BO, Start, Step);
9189}
9190
9192 Value *&Start, Value *&Step) {
9193 BinaryOperator *BO = nullptr;
9194 P = dyn_cast<PHINode>(I->getOperand(0));
9195 if (!P)
9196 P = dyn_cast<PHINode>(I->getOperand(1));
9197 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9198}
9199
9201 PHINode *&P, Value *&Init,
9202 Value *&OtherOp) {
9203 // Binary intrinsics only supported for now.
9204 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9205 I->getType() != I->getArgOperand(1)->getType())
9206 return false;
9207
9208 IntrinsicInst *II = nullptr;
9209 P = dyn_cast<PHINode>(I->getArgOperand(0));
9210 if (!P)
9211 P = dyn_cast<PHINode>(I->getArgOperand(1));
9212
9213 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9214}
9215
9216/// Return true if "icmp Pred LHS RHS" is always true.
9218 const Value *RHS) {
9219 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9220 return true;
9221
9222 switch (Pred) {
9223 default:
9224 return false;
9225
9226 case CmpInst::ICMP_SLE: {
9227 const APInt *C;
9228
9229 // LHS s<= LHS +_{nsw} C if C >= 0
9230 // LHS s<= LHS | C if C >= 0
9231 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9233 return !C->isNegative();
9234
9235 // LHS s<= smax(LHS, V) for any V
9237 return true;
9238
9239 // smin(RHS, V) s<= RHS for any V
9241 return true;
9242
9243 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9244 const Value *X;
9245 const APInt *CLHS, *CRHS;
9246 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9248 return CLHS->sle(*CRHS);
9249
9250 return false;
9251 }
9252
9253 case CmpInst::ICMP_ULE: {
9254 // LHS u<= LHS +_{nuw} V for any V
9255 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9257 return true;
9258
9259 // LHS u<= LHS | V for any V
9260 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9261 return true;
9262
9263 // LHS u<= umax(LHS, V) for any V
9265 return true;
9266
9267 // RHS >> V u<= RHS for any V
9268 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9269 return true;
9270
9271 // RHS u/ C_ugt_1 u<= RHS
9272 const APInt *C;
9273 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9274 return true;
9275
9276 // RHS & V u<= RHS for any V
9278 return true;
9279
9280 // umin(RHS, V) u<= RHS for any V
9282 return true;
9283
9284 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9285 const Value *X;
9286 const APInt *CLHS, *CRHS;
9287 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9289 return CLHS->ule(*CRHS);
9290
9291 return false;
9292 }
9293 }
9294}
9295
9296/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9297/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9298static std::optional<bool>
9300 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9301 switch (Pred) {
9302 default:
9303 return std::nullopt;
9304
9305 case CmpInst::ICMP_SLT:
9306 case CmpInst::ICMP_SLE:
9307 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9309 return true;
9310 return std::nullopt;
9311
9312 case CmpInst::ICMP_SGT:
9313 case CmpInst::ICMP_SGE:
9314 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9316 return true;
9317 return std::nullopt;
9318
9319 case CmpInst::ICMP_ULT:
9320 case CmpInst::ICMP_ULE:
9321 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9323 return true;
9324 return std::nullopt;
9325
9326 case CmpInst::ICMP_UGT:
9327 case CmpInst::ICMP_UGE:
9328 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9330 return true;
9331 return std::nullopt;
9332 }
9333}
9334
9335/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9336/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9337/// Otherwise, return std::nullopt if we can't infer anything.
9338static std::optional<bool>
9340 CmpPredicate RPred, const ConstantRange &RCR) {
9341 auto CRImpliesPred = [&](ConstantRange CR,
9342 CmpInst::Predicate Pred) -> std::optional<bool> {
9343 // If all true values for lhs and true for rhs, lhs implies rhs
9344 if (CR.icmp(Pred, RCR))
9345 return true;
9346
9347 // If there is no overlap, lhs implies not rhs
9348 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9349 return false;
9350
9351 return std::nullopt;
9352 };
9353 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9354 RPred))
9355 return Res;
9356 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9358 : LPred.dropSameSign();
9360 : RPred.dropSameSign();
9361 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9362 RPred);
9363 }
9364 return std::nullopt;
9365}
9366
9367/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9368/// is true. Return false if LHS implies RHS is false. Otherwise, return
9369/// std::nullopt if we can't infer anything.
9370static std::optional<bool>
9371isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9372 CmpPredicate RPred, const Value *R0, const Value *R1,
9373 const DataLayout &DL, bool LHSIsTrue) {
9374 // The rest of the logic assumes the LHS condition is true. If that's not the
9375 // case, invert the predicate to make it so.
9376 if (!LHSIsTrue)
9377 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9378
9379 // We can have non-canonical operands, so try to normalize any common operand
9380 // to L0/R0.
9381 if (L0 == R1) {
9382 std::swap(R0, R1);
9383 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9384 }
9385 if (R0 == L1) {
9386 std::swap(L0, L1);
9387 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9388 }
9389 if (L1 == R1) {
9390 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9391 if (L0 != R0 || match(L0, m_ImmConstant())) {
9392 std::swap(L0, L1);
9393 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9394 std::swap(R0, R1);
9395 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9396 }
9397 }
9398
9399 // See if we can infer anything if operand-0 matches and we have at least one
9400 // constant.
9401 const APInt *Unused;
9402 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9403 // Potential TODO: We could also further use the constant range of L0/R0 to
9404 // further constraint the constant ranges. At the moment this leads to
9405 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9406 // C1` (see discussion: D58633).
9408 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9409 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9411 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9412 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9413 // Even if L1/R1 are not both constant, we can still sometimes deduce
9414 // relationship from a single constant. For example X u> Y implies X != 0.
9415 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9416 return R;
9417 // If both L1/R1 were exact constant ranges and we didn't get anything
9418 // here, we won't be able to deduce this.
9419 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9420 return std::nullopt;
9421 }
9422
9423 // Can we infer anything when the two compares have matching operands?
9424 if (L0 == R0 && L1 == R1)
9425 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9426
9427 // It only really makes sense in the context of signed comparison for "X - Y
9428 // must be positive if X >= Y and no overflow".
9429 // Take SGT as an example: L0:x > L1:y and C >= 0
9430 // ==> R0:(x -nsw y) < R1:(-C) is false
9431 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9432 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9433 SignedLPred == ICmpInst::ICMP_SGE) &&
9434 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9435 if (match(R1, m_NonPositive()) &&
9436 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9437 return false;
9438 }
9439
9440 // Take SLT as an example: L0:x < L1:y and C <= 0
9441 // ==> R0:(x -nsw y) < R1:(-C) is true
9442 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9443 SignedLPred == ICmpInst::ICMP_SLE) &&
9444 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9445 if (match(R1, m_NonNegative()) &&
9446 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9447 return true;
9448 }
9449
9450 // a - b == NonZero -> a != b
9451 // ptrtoint(a) - ptrtoint(b) == NonZero -> a != b
9452 const APInt *L1C;
9453 Value *A, *B;
9454 if (LPred == ICmpInst::ICMP_EQ && ICmpInst::isEquality(RPred) &&
9455 match(L1, m_APInt(L1C)) && !L1C->isZero() &&
9456 match(L0, m_Sub(m_Value(A), m_Value(B))) &&
9457 ((A == R0 && B == R1) || (A == R1 && B == R0) ||
9458 (match(A, m_PtrToInt(m_Specific(R0))) &&
9459 match(B, m_PtrToInt(m_Specific(R1)))) ||
9460 (match(A, m_PtrToInt(m_Specific(R1))) &&
9461 match(B, m_PtrToInt(m_Specific(R0)))))) {
9462 return RPred.dropSameSign() == ICmpInst::ICMP_NE;
9463 }
9464
9465 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9466 if (L0 == R0 &&
9467 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9468 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9469 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9470 return CmpPredicate::getMatching(LPred, RPred).has_value();
9471
9472 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9473 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9474
9475 return std::nullopt;
9476}
9477
9478/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9479/// is true. Return false if LHS implies RHS is false. Otherwise, return
9480/// std::nullopt if we can't infer anything.
9481static std::optional<bool>
9483 FCmpInst::Predicate RPred, const Value *R0, const Value *R1,
9484 const DataLayout &DL, bool LHSIsTrue) {
9485 // The rest of the logic assumes the LHS condition is true. If that's not the
9486 // case, invert the predicate to make it so.
9487 if (!LHSIsTrue)
9488 LPred = FCmpInst::getInversePredicate(LPred);
9489
9490 // We can have non-canonical operands, so try to normalize any common operand
9491 // to L0/R0.
9492 if (L0 == R1) {
9493 std::swap(R0, R1);
9494 RPred = FCmpInst::getSwappedPredicate(RPred);
9495 }
9496 if (R0 == L1) {
9497 std::swap(L0, L1);
9498 LPred = FCmpInst::getSwappedPredicate(LPred);
9499 }
9500 if (L1 == R1) {
9501 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9502 if (L0 != R0 || match(L0, m_ImmConstant())) {
9503 std::swap(L0, L1);
9504 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9505 std::swap(R0, R1);
9506 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9507 }
9508 }
9509
9510 // Can we infer anything when the two compares have matching operands?
9511 if (L0 == R0 && L1 == R1) {
9512 if ((LPred & RPred) == LPred)
9513 return true;
9514 if ((LPred & ~RPred) == LPred)
9515 return false;
9516 }
9517
9518 // See if we can infer anything if operand-0 matches and we have at least one
9519 // constant.
9520 const APFloat *L1C, *R1C;
9521 if (L0 == R0 && match(L1, m_APFloat(L1C)) && match(R1, m_APFloat(R1C))) {
9522 if (std::optional<ConstantFPRange> DomCR =
9524 if (std::optional<ConstantFPRange> ImpliedCR =
9526 if (ImpliedCR->contains(*DomCR))
9527 return true;
9528 }
9529 if (std::optional<ConstantFPRange> ImpliedCR =
9531 FCmpInst::getInversePredicate(RPred), *R1C)) {
9532 if (ImpliedCR->contains(*DomCR))
9533 return false;
9534 }
9535 }
9536 }
9537
9538 return std::nullopt;
9539}
9540
9541/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9542/// false. Otherwise, return std::nullopt if we can't infer anything. We
9543/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9544/// instruction.
9545static std::optional<bool>
9547 const Value *RHSOp0, const Value *RHSOp1,
9548 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9549 // The LHS must be an 'or', 'and', or a 'select' instruction.
9550 assert((LHS->getOpcode() == Instruction::And ||
9551 LHS->getOpcode() == Instruction::Or ||
9552 LHS->getOpcode() == Instruction::Select) &&
9553 "Expected LHS to be 'and', 'or', or 'select'.");
9554
9555 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9556
9557 // If the result of an 'or' is false, then we know both legs of the 'or' are
9558 // false. Similarly, if the result of an 'and' is true, then we know both
9559 // legs of the 'and' are true.
9560 const Value *ALHS, *ARHS;
9561 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9562 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9563 // FIXME: Make this non-recursion.
9564 if (std::optional<bool> Implication = isImpliedCondition(
9565 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9566 return Implication;
9567 if (std::optional<bool> Implication = isImpliedCondition(
9568 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9569 return Implication;
9570 return std::nullopt;
9571 }
9572 return std::nullopt;
9573}
9574
9575std::optional<bool>
9577 const Value *RHSOp0, const Value *RHSOp1,
9578 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9579 // Bail out when we hit the limit.
9581 return std::nullopt;
9582
9583 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9584 // example.
9585 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9586 return std::nullopt;
9587
9588 assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9589 "Expected integer type only!");
9590
9591 // Match not
9592 if (match(LHS, m_Not(m_Value(LHS))))
9593 LHSIsTrue = !LHSIsTrue;
9594
9595 // Both LHS and RHS are icmps.
9596 if (RHSOp0->getType()->getScalarType()->isIntOrPtrTy()) {
9597 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9598 return isImpliedCondICmps(LHSCmp->getCmpPredicate(),
9599 LHSCmp->getOperand(0), LHSCmp->getOperand(1),
9600 RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
9601 const Value *V;
9602 if (match(LHS, m_NUWTrunc(m_Value(V))))
9604 ConstantInt::get(V->getType(), 0), RHSPred,
9605 RHSOp0, RHSOp1, DL, LHSIsTrue);
9606 } else {
9607 assert(RHSOp0->getType()->isFPOrFPVectorTy() &&
9608 "Expected floating point type only!");
9609 if (const auto *LHSCmp = dyn_cast<FCmpInst>(LHS))
9610 return isImpliedCondFCmps(LHSCmp->getPredicate(), LHSCmp->getOperand(0),
9611 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9612 DL, LHSIsTrue);
9613 }
9614
9615 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9616 /// the RHS to be an icmp.
9617 /// FIXME: Add support for and/or/select on the RHS.
9618 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9619 if ((LHSI->getOpcode() == Instruction::And ||
9620 LHSI->getOpcode() == Instruction::Or ||
9621 LHSI->getOpcode() == Instruction::Select))
9622 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9623 Depth);
9624 }
9625 return std::nullopt;
9626}
9627
9628std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9629 const DataLayout &DL,
9630 bool LHSIsTrue, unsigned Depth) {
9631 // LHS ==> RHS by definition
9632 if (LHS == RHS)
9633 return LHSIsTrue;
9634
9635 // Match not
9636 bool InvertRHS = false;
9637 if (match(RHS, m_Not(m_Value(RHS)))) {
9638 if (LHS == RHS)
9639 return !LHSIsTrue;
9640 InvertRHS = true;
9641 }
9642
9643 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9644 if (auto Implied = isImpliedCondition(
9645 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9646 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9647 return InvertRHS ? !*Implied : *Implied;
9648 return std::nullopt;
9649 }
9650 if (const FCmpInst *RHSCmp = dyn_cast<FCmpInst>(RHS)) {
9651 if (auto Implied = isImpliedCondition(
9652 LHS, RHSCmp->getPredicate(), RHSCmp->getOperand(0),
9653 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9654 return InvertRHS ? !*Implied : *Implied;
9655 return std::nullopt;
9656 }
9657
9658 const Value *V;
9659 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9660 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9661 ConstantInt::get(V->getType(), 0), DL,
9662 LHSIsTrue, Depth))
9663 return InvertRHS ? !*Implied : *Implied;
9664 return std::nullopt;
9665 }
9666
9668 return std::nullopt;
9669
9670 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9671 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9672 const Value *RHS1, *RHS2;
9673 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9674 if (std::optional<bool> Imp =
9675 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9676 if (*Imp == true)
9677 return !InvertRHS;
9678 if (std::optional<bool> Imp =
9679 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9680 if (*Imp == true)
9681 return !InvertRHS;
9682 }
9683 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9684 if (std::optional<bool> Imp =
9685 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9686 if (*Imp == false)
9687 return InvertRHS;
9688 if (std::optional<bool> Imp =
9689 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9690 if (*Imp == false)
9691 return InvertRHS;
9692 }
9693
9694 return std::nullopt;
9695}
9696
9697// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9698// condition dominating ContextI or nullptr, if no condition is found.
9699static std::pair<Value *, bool>
9701 if (!ContextI || !ContextI->getParent())
9702 return {nullptr, false};
9703
9704 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9705 // dominator tree (eg, from a SimplifyQuery) instead?
9706 const BasicBlock *ContextBB = ContextI->getParent();
9707 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9708 if (!PredBB)
9709 return {nullptr, false};
9710
9711 // We need a conditional branch in the predecessor.
9712 Value *PredCond;
9713 BasicBlock *TrueBB, *FalseBB;
9714 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9715 return {nullptr, false};
9716
9717 // The branch should get simplified. Don't bother simplifying this condition.
9718 if (TrueBB == FalseBB)
9719 return {nullptr, false};
9720
9721 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9722 "Predecessor block does not point to successor?");
9723
9724 // Is this condition implied by the predecessor condition?
9725 return {PredCond, TrueBB == ContextBB};
9726}
9727
9728std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9729 const Instruction *ContextI,
9730 const DataLayout &DL) {
9731 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9732 auto PredCond = getDomPredecessorCondition(ContextI);
9733 if (PredCond.first)
9734 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9735 return std::nullopt;
9736}
9737
9739 const Value *LHS,
9740 const Value *RHS,
9741 const Instruction *ContextI,
9742 const DataLayout &DL) {
9743 auto PredCond = getDomPredecessorCondition(ContextI);
9744 if (PredCond.first)
9745 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9746 PredCond.second);
9747 return std::nullopt;
9748}
9749
9751 APInt &Upper, const InstrInfoQuery &IIQ,
9752 bool PreferSignedRange) {
9753 unsigned Width = Lower.getBitWidth();
9754 const APInt *C;
9755 switch (BO.getOpcode()) {
9756 case Instruction::Sub:
9757 if (match(BO.getOperand(0), m_APInt(C))) {
9758 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9759 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9760
9761 // If the caller expects a signed compare, then try to use a signed range.
9762 // Otherwise if both no-wraps are set, use the unsigned range because it
9763 // is never larger than the signed range. Example:
9764 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9765 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9766 if (PreferSignedRange && HasNSW && HasNUW)
9767 HasNUW = false;
9768
9769 if (HasNUW) {
9770 // 'sub nuw c, x' produces [0, C].
9771 Upper = *C + 1;
9772 } else if (HasNSW) {
9773 if (C->isNegative()) {
9774 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9776 Upper = *C - APInt::getSignedMaxValue(Width);
9777 } else {
9778 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9779 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9780 Lower = *C - APInt::getSignedMaxValue(Width);
9782 }
9783 }
9784 }
9785 break;
9786 case Instruction::Add:
9787 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9788 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9789 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9790
9791 // If the caller expects a signed compare, then try to use a signed
9792 // range. Otherwise if both no-wraps are set, use the unsigned range
9793 // because it is never larger than the signed range. Example: "add nuw
9794 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9795 if (PreferSignedRange && HasNSW && HasNUW)
9796 HasNUW = false;
9797
9798 if (HasNUW) {
9799 // 'add nuw x, C' produces [C, UINT_MAX].
9800 Lower = *C;
9801 } else if (HasNSW) {
9802 if (C->isNegative()) {
9803 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9805 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9806 } else {
9807 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9808 Lower = APInt::getSignedMinValue(Width) + *C;
9809 Upper = APInt::getSignedMaxValue(Width) + 1;
9810 }
9811 }
9812 }
9813 break;
9814
9815 case Instruction::And:
9816 if (match(BO.getOperand(1), m_APInt(C)))
9817 // 'and x, C' produces [0, C].
9818 Upper = *C + 1;
9819 // X & -X is a power of two or zero. So we can cap the value at max power of
9820 // two.
9821 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9822 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9823 Upper = APInt::getSignedMinValue(Width) + 1;
9824 break;
9825
9826 case Instruction::Or:
9827 if (match(BO.getOperand(1), m_APInt(C)))
9828 // 'or x, C' produces [C, UINT_MAX].
9829 Lower = *C;
9830 break;
9831
9832 case Instruction::AShr:
9833 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9834 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9836 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9837 } else if (match(BO.getOperand(0), m_APInt(C))) {
9838 unsigned ShiftAmount = Width - 1;
9839 if (!C->isZero() && IIQ.isExact(&BO))
9840 ShiftAmount = C->countr_zero();
9841 if (C->isNegative()) {
9842 // 'ashr C, x' produces [C, C >> (Width-1)]
9843 Lower = *C;
9844 Upper = C->ashr(ShiftAmount) + 1;
9845 } else {
9846 // 'ashr C, x' produces [C >> (Width-1), C]
9847 Lower = C->ashr(ShiftAmount);
9848 Upper = *C + 1;
9849 }
9850 }
9851 break;
9852
9853 case Instruction::LShr:
9854 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9855 // 'lshr x, C' produces [0, UINT_MAX >> C].
9856 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9857 } else if (match(BO.getOperand(0), m_APInt(C))) {
9858 // 'lshr C, x' produces [C >> (Width-1), C].
9859 unsigned ShiftAmount = Width - 1;
9860 if (!C->isZero() && IIQ.isExact(&BO))
9861 ShiftAmount = C->countr_zero();
9862 Lower = C->lshr(ShiftAmount);
9863 Upper = *C + 1;
9864 }
9865 break;
9866
9867 case Instruction::Shl:
9868 if (match(BO.getOperand(0), m_APInt(C))) {
9869 if (IIQ.hasNoUnsignedWrap(&BO)) {
9870 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9871 Lower = *C;
9872 Upper = Lower.shl(Lower.countl_zero()) + 1;
9873 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9874 if (C->isNegative()) {
9875 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9876 unsigned ShiftAmount = C->countl_one() - 1;
9877 Lower = C->shl(ShiftAmount);
9878 Upper = *C + 1;
9879 } else {
9880 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9881 unsigned ShiftAmount = C->countl_zero() - 1;
9882 Lower = *C;
9883 Upper = C->shl(ShiftAmount) + 1;
9884 }
9885 } else {
9886 // If lowbit is set, value can never be zero.
9887 if ((*C)[0])
9888 Lower = APInt::getOneBitSet(Width, 0);
9889 // If we are shifting a constant the largest it can be is if the longest
9890 // sequence of consecutive ones is shifted to the highbits (breaking
9891 // ties for which sequence is higher). At the moment we take a liberal
9892 // upper bound on this by just popcounting the constant.
9893 // TODO: There may be a bitwise trick for it longest/highest
9894 // consecutative sequence of ones (naive method is O(Width) loop).
9895 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9896 }
9897 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9898 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9899 }
9900 break;
9901
9902 case Instruction::SDiv:
9903 if (match(BO.getOperand(1), m_APInt(C))) {
9904 APInt IntMin = APInt::getSignedMinValue(Width);
9905 APInt IntMax = APInt::getSignedMaxValue(Width);
9906 if (C->isAllOnes()) {
9907 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9908 // where C != -1 and C != 0 and C != 1
9909 Lower = IntMin + 1;
9910 Upper = IntMax + 1;
9911 } else if (C->countl_zero() < Width - 1) {
9912 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9913 // where C != -1 and C != 0 and C != 1
9914 Lower = IntMin.sdiv(*C);
9915 Upper = IntMax.sdiv(*C);
9916 if (Lower.sgt(Upper))
9918 Upper = Upper + 1;
9919 assert(Upper != Lower && "Upper part of range has wrapped!");
9920 }
9921 } else if (match(BO.getOperand(0), m_APInt(C))) {
9922 if (C->isMinSignedValue()) {
9923 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9924 Lower = *C;
9925 Upper = Lower.lshr(1) + 1;
9926 } else {
9927 // 'sdiv C, x' produces [-|C|, |C|].
9928 Upper = C->abs() + 1;
9929 Lower = (-Upper) + 1;
9930 }
9931 }
9932 break;
9933
9934 case Instruction::UDiv:
9935 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9936 // 'udiv x, C' produces [0, UINT_MAX / C].
9937 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9938 } else if (match(BO.getOperand(0), m_APInt(C))) {
9939 // 'udiv C, x' produces [0, C].
9940 Upper = *C + 1;
9941 }
9942 break;
9943
9944 case Instruction::SRem:
9945 if (match(BO.getOperand(1), m_APInt(C))) {
9946 // 'srem x, C' produces (-|C|, |C|).
9947 Upper = C->abs();
9948 Lower = (-Upper) + 1;
9949 } else if (match(BO.getOperand(0), m_APInt(C))) {
9950 if (C->isNegative()) {
9951 // 'srem -|C|, x' produces [-|C|, 0].
9952 Upper = 1;
9953 Lower = *C;
9954 } else {
9955 // 'srem |C|, x' produces [0, |C|].
9956 Upper = *C + 1;
9957 }
9958 }
9959 break;
9960
9961 case Instruction::URem:
9962 if (match(BO.getOperand(1), m_APInt(C)))
9963 // 'urem x, C' produces [0, C).
9964 Upper = *C;
9965 else if (match(BO.getOperand(0), m_APInt(C)))
9966 // 'urem C, x' produces [0, C].
9967 Upper = *C + 1;
9968 break;
9969
9970 default:
9971 break;
9972 }
9973}
9974
9976 bool UseInstrInfo) {
9977 unsigned Width = II.getType()->getScalarSizeInBits();
9978 const APInt *C;
9979 switch (II.getIntrinsicID()) {
9980 case Intrinsic::ctlz:
9981 case Intrinsic::cttz: {
9982 APInt Upper(Width, Width);
9983 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
9984 Upper += 1;
9985 // Maximum of set/clear bits is the bit width.
9987 }
9988 case Intrinsic::ctpop:
9989 // Maximum of set/clear bits is the bit width.
9991 APInt(Width, Width) + 1);
9992 case Intrinsic::uadd_sat:
9993 // uadd.sat(x, C) produces [C, UINT_MAX].
9994 if (match(II.getOperand(0), m_APInt(C)) ||
9995 match(II.getOperand(1), m_APInt(C)))
9997 break;
9998 case Intrinsic::sadd_sat:
9999 if (match(II.getOperand(0), m_APInt(C)) ||
10000 match(II.getOperand(1), m_APInt(C))) {
10001 if (C->isNegative())
10002 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
10004 APInt::getSignedMaxValue(Width) + *C +
10005 1);
10006
10007 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
10009 APInt::getSignedMaxValue(Width) + 1);
10010 }
10011 break;
10012 case Intrinsic::usub_sat:
10013 // usub.sat(C, x) produces [0, C].
10014 if (match(II.getOperand(0), m_APInt(C)))
10015 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10016
10017 // usub.sat(x, C) produces [0, UINT_MAX - C].
10018 if (match(II.getOperand(1), m_APInt(C)))
10020 APInt::getMaxValue(Width) - *C + 1);
10021 break;
10022 case Intrinsic::ssub_sat:
10023 if (match(II.getOperand(0), m_APInt(C))) {
10024 if (C->isNegative())
10025 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10027 *C - APInt::getSignedMinValue(Width) +
10028 1);
10029
10030 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10032 APInt::getSignedMaxValue(Width) + 1);
10033 } else if (match(II.getOperand(1), m_APInt(C))) {
10034 if (C->isNegative())
10035 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10037 APInt::getSignedMaxValue(Width) + 1);
10038
10039 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10041 APInt::getSignedMaxValue(Width) - *C +
10042 1);
10043 }
10044 break;
10045 case Intrinsic::umin:
10046 case Intrinsic::umax:
10047 case Intrinsic::smin:
10048 case Intrinsic::smax:
10049 if (!match(II.getOperand(0), m_APInt(C)) &&
10050 !match(II.getOperand(1), m_APInt(C)))
10051 break;
10052
10053 switch (II.getIntrinsicID()) {
10054 case Intrinsic::umin:
10055 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10056 case Intrinsic::umax:
10058 case Intrinsic::smin:
10060 *C + 1);
10061 case Intrinsic::smax:
10063 APInt::getSignedMaxValue(Width) + 1);
10064 default:
10065 llvm_unreachable("Must be min/max intrinsic");
10066 }
10067 break;
10068 case Intrinsic::abs:
10069 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10070 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10071 if (match(II.getOperand(1), m_One()))
10073 APInt::getSignedMaxValue(Width) + 1);
10074
10076 APInt::getSignedMinValue(Width) + 1);
10077 case Intrinsic::vscale:
10078 if (!II.getParent() || !II.getFunction())
10079 break;
10080 return getVScaleRange(II.getFunction(), Width);
10081 default:
10082 break;
10083 }
10084
10085 return ConstantRange::getFull(Width);
10086}
10087
10089 const InstrInfoQuery &IIQ) {
10090 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10091 const Value *LHS = nullptr, *RHS = nullptr;
10093 if (R.Flavor == SPF_UNKNOWN)
10094 return ConstantRange::getFull(BitWidth);
10095
10096 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10097 // If the negation part of the abs (in RHS) has the NSW flag,
10098 // then the result of abs(X) is [0..SIGNED_MAX],
10099 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10100 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10104
10107 }
10108
10109 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10110 // The result of -abs(X) is <= 0.
10112 APInt(BitWidth, 1));
10113 }
10114
10115 const APInt *C;
10116 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10117 return ConstantRange::getFull(BitWidth);
10118
10119 switch (R.Flavor) {
10120 case SPF_UMIN:
10122 case SPF_UMAX:
10124 case SPF_SMIN:
10126 *C + 1);
10127 case SPF_SMAX:
10130 default:
10131 return ConstantRange::getFull(BitWidth);
10132 }
10133}
10134
10136 // The maximum representable value of a half is 65504. For floats the maximum
10137 // value is 3.4e38 which requires roughly 129 bits.
10138 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10139 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10140 return;
10141 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10142 Lower = APInt(BitWidth, -65504, true);
10143 Upper = APInt(BitWidth, 65505);
10144 }
10145
10146 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10147 // For a fptoui the lower limit is left as 0.
10148 Upper = APInt(BitWidth, 65505);
10149 }
10150}
10151
10153 bool UseInstrInfo, AssumptionCache *AC,
10154 const Instruction *CtxI,
10155 const DominatorTree *DT,
10156 unsigned Depth) {
10157 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10158
10160 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10161
10162 if (auto *C = dyn_cast<Constant>(V))
10163 return C->toConstantRange();
10164
10165 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10166 InstrInfoQuery IIQ(UseInstrInfo);
10167 ConstantRange CR = ConstantRange::getFull(BitWidth);
10168 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10169 APInt Lower = APInt(BitWidth, 0);
10170 APInt Upper = APInt(BitWidth, 0);
10171 // TODO: Return ConstantRange.
10172 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10174 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10175 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10176 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10178 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10180 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10181 CR = CRTrue.unionWith(CRFalse);
10183 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10184 APInt Lower = APInt(BitWidth, 0);
10185 APInt Upper = APInt(BitWidth, 0);
10186 // TODO: Return ConstantRange.
10189 } else if (const auto *A = dyn_cast<Argument>(V))
10190 if (std::optional<ConstantRange> Range = A->getRange())
10191 CR = *Range;
10192
10193 if (auto *I = dyn_cast<Instruction>(V)) {
10194 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10196
10197 if (const auto *CB = dyn_cast<CallBase>(V))
10198 if (std::optional<ConstantRange> Range = CB->getRange())
10199 CR = CR.intersectWith(*Range);
10200 }
10201
10202 if (CtxI && AC) {
10203 // Try to restrict the range based on information from assumptions.
10204 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10205 if (!AssumeVH)
10206 continue;
10207 CallInst *I = cast<CallInst>(AssumeVH);
10208 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10209 "Got assumption for the wrong function!");
10210 assert(I->getIntrinsicID() == Intrinsic::assume &&
10211 "must be an assume intrinsic");
10212
10213 if (!isValidAssumeForContext(I, CtxI, DT))
10214 continue;
10215 Value *Arg = I->getArgOperand(0);
10216 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10217 // Currently we just use information from comparisons.
10218 if (!Cmp || Cmp->getOperand(0) != V)
10219 continue;
10220 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10221 ConstantRange RHS =
10222 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10223 UseInstrInfo, AC, I, DT, Depth + 1);
10224 CR = CR.intersectWith(
10225 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10226 }
10227 }
10228
10229 return CR;
10230}
10231
10232static void
10234 function_ref<void(Value *)> InsertAffected) {
10235 assert(V != nullptr);
10236 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10237 InsertAffected(V);
10238 } else if (auto *I = dyn_cast<Instruction>(V)) {
10239 InsertAffected(V);
10240
10241 // Peek through unary operators to find the source of the condition.
10242 Value *Op;
10245 InsertAffected(Op);
10246 }
10247 }
10248}
10249
10251 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10252 auto AddAffected = [&InsertAffected](Value *V) {
10253 addValueAffectedByCondition(V, InsertAffected);
10254 };
10255
10256 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10257 if (IsAssume) {
10258 AddAffected(LHS);
10259 AddAffected(RHS);
10260 } else if (match(RHS, m_Constant()))
10261 AddAffected(LHS);
10262 };
10263
10264 SmallVector<Value *, 8> Worklist;
10266 Worklist.push_back(Cond);
10267 while (!Worklist.empty()) {
10268 Value *V = Worklist.pop_back_val();
10269 if (!Visited.insert(V).second)
10270 continue;
10271
10272 CmpPredicate Pred;
10273 Value *A, *B, *X;
10274
10275 if (IsAssume) {
10276 AddAffected(V);
10277 if (match(V, m_Not(m_Value(X))))
10278 AddAffected(X);
10279 }
10280
10281 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10282 // assume(A && B) is split to -> assume(A); assume(B);
10283 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10284 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10285 // enough information to be worth handling (intersection of information as
10286 // opposed to union).
10287 if (!IsAssume) {
10288 Worklist.push_back(A);
10289 Worklist.push_back(B);
10290 }
10291 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10292 bool HasRHSC = match(B, m_ConstantInt());
10293 if (ICmpInst::isEquality(Pred)) {
10294 AddAffected(A);
10295 if (IsAssume)
10296 AddAffected(B);
10297 if (HasRHSC) {
10298 Value *Y;
10299 // (X << C) or (X >>_s C) or (X >>_u C).
10300 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10301 AddAffected(X);
10302 // (X & C) or (X | C).
10303 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10304 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10305 AddAffected(X);
10306 AddAffected(Y);
10307 }
10308 // X - Y
10309 else if (match(A, m_Sub(m_Value(X), m_Value(Y)))) {
10310 AddAffected(X);
10311 AddAffected(Y);
10312 }
10313 }
10314 } else {
10315 AddCmpOperands(A, B);
10316 if (HasRHSC) {
10317 // Handle (A + C1) u< C2, which is the canonical form of
10318 // A > C3 && A < C4.
10320 AddAffected(X);
10321
10322 if (ICmpInst::isUnsigned(Pred)) {
10323 Value *Y;
10324 // X & Y u> C -> X >u C && Y >u C
10325 // X | Y u< C -> X u< C && Y u< C
10326 // X nuw+ Y u< C -> X u< C && Y u< C
10327 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10328 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10329 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10330 AddAffected(X);
10331 AddAffected(Y);
10332 }
10333 // X nuw- Y u> C -> X u> C
10334 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10335 AddAffected(X);
10336 }
10337 }
10338
10339 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10340 // by computeKnownFPClass().
10342 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10343 InsertAffected(X);
10344 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10345 InsertAffected(X);
10346 }
10347 }
10348
10349 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10350 AddAffected(X);
10351 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10352 AddCmpOperands(A, B);
10353
10354 // fcmp fneg(x), y
10355 // fcmp fabs(x), y
10356 // fcmp fneg(fabs(x)), y
10357 if (match(A, m_FNeg(m_Value(A))))
10358 AddAffected(A);
10359 if (match(A, m_FAbs(m_Value(A))))
10360 AddAffected(A);
10361
10363 m_Value()))) {
10364 // Handle patterns that computeKnownFPClass() support.
10365 AddAffected(A);
10366 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10367 // Assume is checked here as X is already added above for assumes in
10368 // addValueAffectedByCondition
10369 AddAffected(X);
10370 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10371 // Assume is checked here to avoid issues with ephemeral values
10372 Worklist.push_back(X);
10373 }
10374 }
10375}
10376
10378 // (X >> C) or/add (X & mask(C) != 0)
10379 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10380 if (BO->getOpcode() == Instruction::Add ||
10381 BO->getOpcode() == Instruction::Or) {
10382 const Value *X;
10383 const APInt *C1, *C2;
10384 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10388 m_Zero())))) &&
10389 C2->popcount() == C1->getZExtValue())
10390 return X;
10391 }
10392 }
10393 return nullptr;
10394}
10395
10397 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10398}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Utilities for dealing with flags related to floating point properties and mode controls.
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static cl::opt< RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode > Mode("regalloc-enable-advisor", cl::Hidden, cl::init(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default), cl::desc("Enable regalloc advisor mode"), cl::values(clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Default, "default", "Default"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Release, "release", "precompiled"), clEnumValN(RegAllocEvictionAdvisorAnalysisLegacy::AdvisorMode::Development, "development", "for training")))
std::pair< BasicBlock *, BasicBlock * > Edge
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
static TableGen::Emitter::Opt Y("gen-skeleton-entry", EmitSkeleton, "Generate example skeleton entry")
static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static SmallVector< VPValue *, 4 > getOperands(ArrayRef< VPValue * > Values, unsigned OperandIndex)
Definition VPlanSLP.cpp:210
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext, unsigned Depth=0)
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, SimplifyQuery &Q, unsigned Depth)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
static unsigned computeNumSignBitsVectorConstant(const Value *V, const APInt &DemandedElts, unsigned TyBits)
For vector constants, loop over the elements and find the constant with the minimum number of sign bi...
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V1 == (binop V2, X), where X is known non-zero.
static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q, unsigned Depth)
Test whether a GEP's result is known to be non-null.
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static constexpr unsigned MaxInstrsToCheckForFree
Maximum number of instructions to check between assume and context instruction.
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
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
bool isFinite() const
Definition APFloat.h:1454
bool isNaN() const
Definition APFloat.h:1447
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition APFloat.h:1138
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition APFloat.h:1098
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition APFloat.h:1079
Class for arbitrary precision integers.
Definition APInt.h:78
LLVM_ABI APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition APInt.cpp:1573
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition APInt.h:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1406
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition APInt.h:423
uint64_t getZExtValue() const
Get zero extended value.
Definition APInt.h:1540
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition APInt.h:1391
unsigned popcount() const
Count the number of bits set.
Definition APInt.h:1670
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition APInt.h:1385
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition APInt.h:206
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition APInt.h:1330
unsigned ceilLogBase2() const
Definition APInt.h:1764
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition APInt.h:1201
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition APInt.h:371
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:380
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1488
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition APInt.h:209
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition APInt.h:216
bool isNegative() const
Determine sign of this APInt.
Definition APInt.h:329
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition APInt.h:1249
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition APInt.h:1396
LLVM_ABI APInt reverseBits() const
Definition APInt.cpp:768
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition APInt.h:1166
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition APInt.h:1628
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition APInt.h:219
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition APInt.cpp:1041
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition APInt.h:356
unsigned logBase2() const
Definition APInt.h:1761
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition APInt.h:827
bool getBoolValue() const
Convert APInt to a boolean value.
Definition APInt.h:471
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition APInt.h:405
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition APInt.h:1150
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition APInt.h:873
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition APInt.h:1130
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition APInt.h:296
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition APInt.h:200
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition APInt.h:1388
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition APInt.h:1237
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:239
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition APInt.h:1221
void clearSignBit()
Set the sign bit to 0.
Definition APInt.h:1449
an instruction to allocate memory on the stack
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:41
iterator end() const
Definition ArrayRef.h:136
size_t size() const
size - Get the array size.
Definition ArrayRef.h:147
iterator begin() const
Definition ArrayRef.h:135
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:142
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition ArrayRef.h:191
Class to represent array types.
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:69
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition Attributes.h:223
LLVM_ABI bool isSingleEdge() const
Check if this is the only edge between Start and End.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:472
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:459
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
LLVM_ABI Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition InstrTypes.h:374
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
unsigned arg_size() const
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition InstrTypes.h:448
This class is the base class for the comparison instructions.
Definition InstrTypes.h:666
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition InstrTypes.h:678
@ ICMP_SLT
signed less than
Definition InstrTypes.h:707
@ ICMP_SLE
signed less or equal
Definition InstrTypes.h:708
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition InstrTypes.h:684
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition InstrTypes.h:693
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition InstrTypes.h:682
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition InstrTypes.h:683
@ ICMP_UGE
unsigned greater or equal
Definition InstrTypes.h:702
@ ICMP_UGT
unsigned greater than
Definition InstrTypes.h:701
@ ICMP_SGT
signed greater than
Definition InstrTypes.h:705
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition InstrTypes.h:692
@ ICMP_ULT
unsigned less than
Definition InstrTypes.h:703
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition InstrTypes.h:690
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition InstrTypes.h:685
@ ICMP_NE
not equal
Definition InstrTypes.h:700
@ ICMP_SGE
signed greater or equal
Definition InstrTypes.h:706
@ ICMP_ULE
unsigned less or equal
Definition InstrTypes.h:704
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition InstrTypes.h:691
bool isSigned() const
Definition InstrTypes.h:932
static LLVM_ABI bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition InstrTypes.h:829
bool isTrueWhenEqual() const
This is just a convenience.
Definition InstrTypes.h:944
static bool isFPPredicate(Predicate P)
Definition InstrTypes.h:772
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition InstrTypes.h:791
Predicate getPredicate() const
Return the predicate for this instruction.
Definition InstrTypes.h:767
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition InstrTypes.h:895
static bool isIntPredicate(Predicate P)
Definition InstrTypes.h:778
static LLVM_ABI bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition InstrTypes.h:938
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
static LLVM_ABI std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
LLVM_ABI CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
CmpInst::Predicate dropSameSign() const
Drops samesign information.
bool hasSameSign() const
Query samesign information, for optimizations.
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:702
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition Constants.h:593
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition Constants.h:668
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition Constants.h:776
static LLVM_ABI Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
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:1705
MaybeAlign getAlign(const CallInst &I, unsigned Index)
LLVM_ABI bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1657
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition ScopeExit.h:59
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2452
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:644
LLVM_ABI bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition Loads.cpp:229
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition bit.h:289
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2116
LLVM_ABI bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition Loads.cpp:416
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition MathExtras.h:293
gep_type_iterator gep_type_end(const User *GEP)
int ilogb(const APFloat &Arg)
Returns the exponent of the internal representation of the APFloat.
Definition APFloat.h:1534
LLVM_ABI bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
LLVM_ABI CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:677
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:348
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:186
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:754
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1712
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:342
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
LLVM_ABI void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, const SimplifyQuery &Q, unsigned Depth=0)
Adjust Known for the given select Arm to include information from the select Cond.
LLVM_ABI bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
LLVM_ABI OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
LLVM_ABI bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
LLVM_ABI bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
LLVM_ABI void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
LLVM_ABI SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
LLVM_ABI bool programUndefinedIfPoison(const Instruction *Inst)
LLVM_ABI SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
LLVM_ABI bool matchSimpleBinaryIntrinsicRecurrence(const IntrinsicInst *I, PHINode *&P, Value *&Init, Value *&OtherOp)
Attempt to match a simple value-accumulating recurrence of the form: llvm.intrinsic....
LLVM_ABI bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
LLVM_ABI bool cannotBeNegativeZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is never equal to -0.0.
LLVM_ABI bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
generic_gep_type_iterator<> gep_type_iterator
FunctionAddr VTableAddr Count
Definition InstrProf.h:139
LLVM_ABI uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
LLVM_ABI OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
LLVM_ABI Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
LLVM_ABI bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
LLVM_ABI bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:548
LLVM_ABI bool intrinsicPropagatesPoison(Intrinsic::ID IID)
Return whether this intrinsic propagates poison for all operands.
LLVM_ABI bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
LLVM_ABI bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
LLVM_ABI RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, AssumptionCache &AC, const Instruction *CtxI, const DominatorTree *DT=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
LLVM_ABI bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true, bool IgnoreUBImplyingAttrs=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
LLVM_ABI bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
LLVM_ABI Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ Other
Any other memory.
Definition ModRef.h:68
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
Definition ModRef.h:71
LLVM_ABI const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
LLVM_ABI Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
LLVM_ABI SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, FastMathFlags FMF=FastMathFlags(), Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
LLVM_ABI OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
LLVM_ABI bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
@ Add
Sum of integers.
LLVM_ABI ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
LLVM_ABI bool isKnownNonEqual(const Value *V1, const Value *V2, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the given values are known to be non-equal when defined.
DWARFExpression::Operation Op
LLVM_ABI bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
ArrayRef(const T &OneElt) -> ArrayRef< T >
LLVM_ABI unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return the number of times the sign bit of the register is replicated into the other bits.
constexpr unsigned BitWidth
LLVM_ABI KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &SQ, unsigned Depth=0)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
LLVM_ABI OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
LLVM_ABI bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
LLVM_ABI bool isKnownNeverInfOrNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point value can never contain a NaN or infinity.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:560
LLVM_ABI bool isKnownNeverNaN(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not a NaN or if the floating-point vector value has...
gep_type_iterator gep_type_begin(const User *GEP)
LLVM_ABI Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
LLVM_ABI std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
LLVM_ABI unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Get the upper bound on bit size for this Value Op as a signed integer.
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1877
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition Alignment.h:197
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
LLVM_ABI std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
LLVM_ABI std::optional< bool > computeKnownFPSignBit(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return false if we can prove that the specified FP value's sign bit is 0.
LLVM_ABI bool canIgnoreSignBitOfNaN(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is NaN.
LLVM_ABI void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:869
static LLVM_ABI unsigned int semanticsPrecision(const fltSemantics &)
Definition APFloat.cpp:324
static LLVM_ABI bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition APFloat.cpp:374
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Represents offset+length into a ConstantDataArray.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
constexpr bool outputsAreZero() const
Return true if output denormals should be flushed to 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ IEEE
IEEE-754 denormal numbers preserved.
constexpr bool inputsAreZero() const
Return true if input denormals must be implicitly treated as 0.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getIEEE()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
bool isExact(const BinaryOperator *Op) const
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
bool hasNoSignedZeros(const InstT *Op) const
bool hasNoSignedWrap(const InstT *Op) const
bool hasNoUnsignedWrap(const InstT *Op) const
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition KnownBits.h:301
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
static LLVM_ABI std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition KnownBits.h:186
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition KnownBits.h:255
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition KnownBits.h:108
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
void makeNonNegative()
Make this value non-negative.
Definition KnownBits.h:124
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition KnownBits.h:251
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition KnownBits.h:242
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
bool isUnknown() const
Returns true if we don't know any bits.
Definition KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition KnownBits.h:274
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
void makeNegative()
Make this value negative.
Definition KnownBits.h:119
void setAllConflict()
Make all bits known to be both zero and one.
Definition KnownBits.h:99
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition KnownBits.h:161
KnownBits byteSwap() const
Definition KnownBits.h:514
bool hasConflict() const
Returns true if there is conflicting information.
Definition KnownBits.h:51
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition KnownBits.h:289
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition KnownBits.h:86
KnownBits reverseBits() const
Definition KnownBits.h:518
unsigned getBitWidth() const
Get the bit width of this value.
Definition KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition KnownBits.h:172
bool isConstant() const
Returns true if we know the value of all bits.
Definition KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition KnownBits.h:321
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition KnownBits.h:111
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition KnownBits.h:225
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition KnownBits.h:311
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition KnownBits.h:180
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition KnownBits.h:245
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition KnownBits.h:347
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition KnownBits.h:196
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition KnownBits.h:248
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition KnownBits.h:145
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
static LLVM_ABI KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition KnownBits.cpp:60
static LLVM_ABI KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition KnownBits.h:326
bool isNegative() const
Returns true if this value is known to be negative.
Definition KnownBits.h:105
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition KnownBits.h:353
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition KnownBits.h:280
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition KnownBits.h:92
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition KnownBits.h:219
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition KnownBits.h:167
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
static LLVM_ABI std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
static LLVM_ABI std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
static LLVM_ABI KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition KnownBits.h:206
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a zero.
bool isUnknown() const
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
LLVM_ABI void propagateCanonicalizingSrc(const KnownFPClass &Src, DenormalMode Mode)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void signBitMustBeZero()
Assume the sign bit is zero.
LLVM_ABI bool isKnownNeverLogicalPosZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a positive zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
LLVM_ABI bool isKnownNeverLogicalNegZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a negative zero.
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
const DominatorTree * DT
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
const DomConditionCache * DC
const InstrInfoQuery IIQ
const CondContext * CC