LLVM 22.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/STLExtras.h"
20#include "llvm/ADT/ScopeExit.h"
23#include "llvm/ADT/StringRef.h"
33#include "llvm/Analysis/Loads.h"
38#include "llvm/IR/Argument.h"
39#include "llvm/IR/Attributes.h"
40#include "llvm/IR/BasicBlock.h"
41#include "llvm/IR/Constant.h"
43#include "llvm/IR/Constants.h"
46#include "llvm/IR/Dominators.h"
48#include "llvm/IR/Function.h"
50#include "llvm/IR/GlobalAlias.h"
51#include "llvm/IR/GlobalValue.h"
53#include "llvm/IR/InstrTypes.h"
54#include "llvm/IR/Instruction.h"
57#include "llvm/IR/Intrinsics.h"
58#include "llvm/IR/IntrinsicsAArch64.h"
59#include "llvm/IR/IntrinsicsAMDGPU.h"
60#include "llvm/IR/IntrinsicsRISCV.h"
61#include "llvm/IR/IntrinsicsX86.h"
62#include "llvm/IR/LLVMContext.h"
63#include "llvm/IR/Metadata.h"
64#include "llvm/IR/Module.h"
65#include "llvm/IR/Operator.h"
67#include "llvm/IR/Type.h"
68#include "llvm/IR/User.h"
69#include "llvm/IR/Value.h"
78#include <algorithm>
79#include <cassert>
80#include <cstdint>
81#include <optional>
82#include <utility>
83
84using namespace llvm;
85using namespace llvm::PatternMatch;
86
87// Controls the number of uses of the value searched for possible
88// dominating comparisons.
89static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
90 cl::Hidden, cl::init(20));
91
92
93/// Returns the bitwidth of the given scalar or pointer type. For vector types,
94/// returns the element type's bitwidth.
95static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
96 if (unsigned BitWidth = Ty->getScalarSizeInBits())
97 return BitWidth;
98
99 return DL.getPointerTypeSizeInBits(Ty);
100}
101
102// Given the provided Value and, potentially, a context instruction, return
103// the preferred context instruction (if any).
104static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
105 // If we've been provided with a context instruction, then use that (provided
106 // it has been inserted).
107 if (CxtI && CxtI->getParent())
108 return CxtI;
109
110 // If the value is really an already-inserted instruction, then use that.
111 CxtI = dyn_cast<Instruction>(V);
112 if (CxtI && CxtI->getParent())
113 return CxtI;
114
115 return nullptr;
116}
117
119 const APInt &DemandedElts,
120 APInt &DemandedLHS, APInt &DemandedRHS) {
121 if (isa<ScalableVectorType>(Shuf->getType())) {
122 assert(DemandedElts == APInt(1,1));
123 DemandedLHS = DemandedRHS = DemandedElts;
124 return true;
125 }
126
127 int NumElts =
128 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
129 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
130 DemandedElts, DemandedLHS, DemandedRHS);
131}
132
133static void computeKnownBits(const Value *V, const APInt &DemandedElts,
134 KnownBits &Known, const SimplifyQuery &Q,
135 unsigned Depth);
136
138 const SimplifyQuery &Q, unsigned Depth) {
139 // Since the number of lanes in a scalable vector is unknown at compile time,
140 // we track one bit which is implicitly broadcast to all lanes. This means
141 // that all lanes in a scalable vector are considered demanded.
142 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
143 APInt DemandedElts =
144 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
145 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
146}
147
149 const DataLayout &DL, AssumptionCache *AC,
150 const Instruction *CxtI, const DominatorTree *DT,
151 bool UseInstrInfo, unsigned Depth) {
152 computeKnownBits(V, Known,
153 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
154 Depth);
155}
156
158 AssumptionCache *AC, const Instruction *CxtI,
159 const DominatorTree *DT, bool UseInstrInfo,
160 unsigned Depth) {
161 return computeKnownBits(
162 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
163}
164
165KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
166 const DataLayout &DL, AssumptionCache *AC,
167 const Instruction *CxtI,
168 const DominatorTree *DT, bool UseInstrInfo,
169 unsigned Depth) {
170 return computeKnownBits(
171 V, DemandedElts,
172 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
173}
174
175static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS,
176 const SimplifyQuery &SQ) {
177 // Look for an inverted mask: (X & ~M) op (Y & M).
178 {
179 Value *M;
180 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
182 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
183 return true;
184 }
185
186 // X op (Y & ~X)
189 return true;
190
191 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
192 // for constant Y.
193 Value *Y;
194 if (match(RHS,
196 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
197 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
198 return true;
199
200 // Peek through extends to find a 'not' of the other side:
201 // (ext Y) op ext(~Y)
202 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
204 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
205 return true;
206
207 // Look for: (A & B) op ~(A | B)
208 {
209 Value *A, *B;
210 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
212 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
213 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
214 return true;
215 }
216
217 // Look for: (X << V) op (Y >> (BitWidth - V))
218 // or (X >> V) op (Y << (BitWidth - V))
219 {
220 const Value *V;
221 const APInt *R;
222 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
223 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
224 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
225 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
226 R->uge(LHS->getType()->getScalarSizeInBits()))
227 return true;
228 }
229
230 return false;
231}
232
234 const WithCache<const Value *> &RHSCache,
235 const SimplifyQuery &SQ) {
236 const Value *LHS = LHSCache.getValue();
237 const Value *RHS = RHSCache.getValue();
238
239 assert(LHS->getType() == RHS->getType() &&
240 "LHS and RHS should have the same type");
242 "LHS and RHS should be integers");
243
246 return true;
247
249 RHSCache.getKnownBits(SQ));
250}
251
253 return !I->user_empty() && all_of(I->users(), [](const User *U) {
254 return match(U, m_ICmp(m_Value(), m_Zero()));
255 });
256}
257
259 return !I->user_empty() && all_of(I->users(), [](const User *U) {
260 CmpPredicate P;
261 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
262 });
263}
264
266 bool OrZero, AssumptionCache *AC,
267 const Instruction *CxtI,
268 const DominatorTree *DT, bool UseInstrInfo,
269 unsigned Depth) {
270 return ::isKnownToBeAPowerOfTwo(
271 V, OrZero, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo),
272 Depth);
273}
274
275static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
276 const SimplifyQuery &Q, unsigned Depth);
277
279 unsigned Depth) {
280 return computeKnownBits(V, SQ, Depth).isNonNegative();
281}
282
284 unsigned Depth) {
285 if (auto *CI = dyn_cast<ConstantInt>(V))
286 return CI->getValue().isStrictlyPositive();
287
288 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
289 // this updated.
290 KnownBits Known = computeKnownBits(V, SQ, Depth);
291 return Known.isNonNegative() &&
292 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
293}
294
296 unsigned Depth) {
297 return computeKnownBits(V, SQ, Depth).isNegative();
298}
299
300static bool isKnownNonEqual(const Value *V1, const Value *V2,
301 const APInt &DemandedElts, const SimplifyQuery &Q,
302 unsigned Depth);
303
304bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
305 const SimplifyQuery &Q, unsigned Depth) {
306 // We don't support looking through casts.
307 if (V1 == V2 || V1->getType() != V2->getType())
308 return false;
309 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
310 APInt DemandedElts =
311 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
312 return ::isKnownNonEqual(V1, V2, DemandedElts, Q, Depth);
313}
314
315bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
316 const SimplifyQuery &SQ, unsigned Depth) {
317 KnownBits Known(Mask.getBitWidth());
318 computeKnownBits(V, Known, SQ, Depth);
319 return Mask.isSubsetOf(Known.Zero);
320}
321
322static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
323 const SimplifyQuery &Q, unsigned Depth);
324
325static unsigned ComputeNumSignBits(const Value *V, const SimplifyQuery &Q,
326 unsigned Depth = 0) {
327 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
328 APInt DemandedElts =
329 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
330 return ComputeNumSignBits(V, DemandedElts, Q, Depth);
331}
332
333unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
334 AssumptionCache *AC, const Instruction *CxtI,
335 const DominatorTree *DT, bool UseInstrInfo,
336 unsigned Depth) {
337 return ::ComputeNumSignBits(
338 V, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo), Depth);
339}
340
342 AssumptionCache *AC,
343 const Instruction *CxtI,
344 const DominatorTree *DT,
345 unsigned Depth) {
346 unsigned SignBits = ComputeNumSignBits(V, DL, AC, CxtI, DT, Depth);
347 return V->getType()->getScalarSizeInBits() - SignBits + 1;
348}
349
350static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
351 bool NSW, bool NUW,
352 const APInt &DemandedElts,
353 KnownBits &KnownOut, KnownBits &Known2,
354 const SimplifyQuery &Q, unsigned Depth) {
355 computeKnownBits(Op1, DemandedElts, KnownOut, Q, Depth + 1);
356
357 // If one operand is unknown and we have no nowrap information,
358 // the result will be unknown independently of the second operand.
359 if (KnownOut.isUnknown() && !NSW && !NUW)
360 return;
361
362 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
363 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
364
365 if (!Add && NSW && !KnownOut.isNonNegative() &&
366 isImpliedByDomCondition(ICmpInst::ICMP_SLE, Op1, Op0, Q.CxtI, Q.DL)
367 .value_or(false))
368 KnownOut.makeNonNegative();
369}
370
371static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
372 bool NUW, const APInt &DemandedElts,
373 KnownBits &Known, KnownBits &Known2,
374 const SimplifyQuery &Q, unsigned Depth) {
375 computeKnownBits(Op1, DemandedElts, Known, Q, Depth + 1);
376 computeKnownBits(Op0, DemandedElts, Known2, Q, Depth + 1);
377
378 bool isKnownNegative = false;
379 bool isKnownNonNegative = false;
380 // If the multiplication is known not to overflow, compute the sign bit.
381 if (NSW) {
382 if (Op0 == Op1) {
383 // The product of a number with itself is non-negative.
384 isKnownNonNegative = true;
385 } else {
386 bool isKnownNonNegativeOp1 = Known.isNonNegative();
387 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
388 bool isKnownNegativeOp1 = Known.isNegative();
389 bool isKnownNegativeOp0 = Known2.isNegative();
390 // The product of two numbers with the same sign is non-negative.
391 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
392 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
393 if (!isKnownNonNegative && NUW) {
394 // mul nuw nsw with a factor > 1 is non-negative.
396 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
397 KnownBits::sgt(Known2, One).value_or(false);
398 }
399
400 // The product of a negative number and a non-negative number is either
401 // negative or zero.
404 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
405 Known2.isNonZero()) ||
406 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
407 }
408 }
409
410 bool SelfMultiply = Op0 == Op1;
411 if (SelfMultiply)
412 SelfMultiply &=
413 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
414 Known = KnownBits::mul(Known, Known2, SelfMultiply);
415
416 // Only make use of no-wrap flags if we failed to compute the sign bit
417 // directly. This matters if the multiplication always overflows, in
418 // which case we prefer to follow the result of the direct computation,
419 // though as the program is invoking undefined behaviour we can choose
420 // whatever we like here.
421 if (isKnownNonNegative && !Known.isNegative())
422 Known.makeNonNegative();
423 else if (isKnownNegative && !Known.isNonNegative())
424 Known.makeNegative();
425}
426
428 KnownBits &Known) {
429 unsigned BitWidth = Known.getBitWidth();
430 unsigned NumRanges = Ranges.getNumOperands() / 2;
431 assert(NumRanges >= 1);
432
433 Known.Zero.setAllBits();
434 Known.One.setAllBits();
435
436 for (unsigned i = 0; i < NumRanges; ++i) {
438 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
440 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
441 ConstantRange Range(Lower->getValue(), Upper->getValue());
442 // BitWidth must equal the Ranges BitWidth for the correct number of high
443 // bits to be set.
445 "Known bit width must match range bit width!");
446
447 // The first CommonPrefixBits of all values in Range are equal.
448 unsigned CommonPrefixBits =
450 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
452 Known.One &= UnsignedMax & Mask;
453 Known.Zero &= ~UnsignedMax & Mask;
454 }
455}
456
457static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
461
462 // The instruction defining an assumption's condition itself is always
463 // considered ephemeral to that assumption (even if it has other
464 // non-ephemeral users). See r246696's test case for an example.
465 if (is_contained(I->operands(), E))
466 return true;
467
468 while (!WorkSet.empty()) {
469 const Instruction *V = WorkSet.pop_back_val();
470 if (!Visited.insert(V).second)
471 continue;
472
473 // If all uses of this value are ephemeral, then so is this value.
474 if (all_of(V->users(), [&](const User *U) {
475 return EphValues.count(cast<Instruction>(U));
476 })) {
477 if (V == E)
478 return true;
479
480 if (V == I || (!V->mayHaveSideEffects() && !V->isTerminator())) {
481 EphValues.insert(V);
482
483 if (const User *U = dyn_cast<User>(V)) {
484 for (const Use &U : U->operands()) {
485 if (const auto *I = dyn_cast<Instruction>(U.get()))
486 WorkSet.push_back(I);
487 }
488 }
489 }
490 }
491 }
492
493 return false;
494}
495
496// Is this an intrinsic that cannot be speculated but also cannot trap?
498 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
499 return CI->isAssumeLikeIntrinsic();
500
501 return false;
502}
503
505 const Instruction *CxtI,
506 const DominatorTree *DT,
507 bool AllowEphemerals) {
508 // There are two restrictions on the use of an assume:
509 // 1. The assume must dominate the context (or the control flow must
510 // reach the assume whenever it reaches the context).
511 // 2. The context must not be in the assume's set of ephemeral values
512 // (otherwise we will use the assume to prove that the condition
513 // feeding the assume is trivially true, thus causing the removal of
514 // the assume).
515
516 if (Inv->getParent() == CxtI->getParent()) {
517 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
518 // in the BB.
519 if (Inv->comesBefore(CxtI))
520 return true;
521
522 // Don't let an assume affect itself - this would cause the problems
523 // `isEphemeralValueOf` is trying to prevent, and it would also make
524 // the loop below go out of bounds.
525 if (!AllowEphemerals && Inv == CxtI)
526 return false;
527
528 // The context comes first, but they're both in the same block.
529 // Make sure there is nothing in between that might interrupt
530 // the control flow, not even CxtI itself.
531 // We limit the scan distance between the assume and its context instruction
532 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
533 // it can be adjusted if needed (could be turned into a cl::opt).
534 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
536 return false;
537
538 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
539 }
540
541 // Inv and CxtI are in different blocks.
542 if (DT) {
543 if (DT->dominates(Inv, CxtI))
544 return true;
545 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
546 Inv->getParent()->isEntryBlock()) {
547 // We don't have a DT, but this trivially dominates.
548 return true;
549 }
550
551 return false;
552}
553
554// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
555// we still have enough information about `RHS` to conclude non-zero. For
556// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
557// so the extra compile time may not be worth it, but possibly a second API
558// should be created for use outside of loops.
559static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
560 // v u> y implies v != 0.
561 if (Pred == ICmpInst::ICMP_UGT)
562 return true;
563
564 // Special-case v != 0 to also handle v != null.
565 if (Pred == ICmpInst::ICMP_NE)
566 return match(RHS, m_Zero());
567
568 // All other predicates - rely on generic ConstantRange handling.
569 const APInt *C;
571 if (match(RHS, m_APInt(C))) {
573 return !TrueValues.contains(Zero);
574 }
575
576 auto *VC = dyn_cast<ConstantDataVector>(RHS);
577 if (VC == nullptr)
578 return false;
579
580 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
581 ++ElemIdx) {
583 Pred, VC->getElementAsAPInt(ElemIdx));
584 if (TrueValues.contains(Zero))
585 return false;
586 }
587 return true;
588}
589
590static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
591 Value *&ValOut, Instruction *&CtxIOut,
592 const PHINode **PhiOut = nullptr) {
593 ValOut = U->get();
594 if (ValOut == PHI)
595 return;
596 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
597 if (PhiOut)
598 *PhiOut = PHI;
599 Value *V;
600 // If the Use is a select of this phi, compute analysis on other arm to break
601 // recursion.
602 // TODO: Min/Max
603 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
604 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
605 ValOut = V;
606
607 // Same for select, if this phi is 2-operand phi, compute analysis on other
608 // incoming value to break recursion.
609 // TODO: We could handle any number of incoming edges as long as we only have
610 // two unique values.
611 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
612 IncPhi && IncPhi->getNumIncomingValues() == 2) {
613 for (int Idx = 0; Idx < 2; ++Idx) {
614 if (IncPhi->getIncomingValue(Idx) == PHI) {
615 ValOut = IncPhi->getIncomingValue(1 - Idx);
616 if (PhiOut)
617 *PhiOut = IncPhi;
618 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
619 break;
620 }
621 }
622 }
623}
624
625static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
626 // Use of assumptions is context-sensitive. If we don't have a context, we
627 // cannot use them!
628 if (!Q.AC || !Q.CxtI)
629 return false;
630
631 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
632 if (!Elem.Assume)
633 continue;
634
635 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
636 assert(I->getFunction() == Q.CxtI->getFunction() &&
637 "Got assumption for the wrong function!");
638
639 if (Elem.Index != AssumptionCache::ExprResultIdx) {
640 if (!V->getType()->isPointerTy())
641 continue;
643 *I, I->bundle_op_info_begin()[Elem.Index])) {
644 if (RK.WasOn == V &&
645 (RK.AttrKind == Attribute::NonNull ||
646 (RK.AttrKind == Attribute::Dereferenceable &&
648 V->getType()->getPointerAddressSpace()))) &&
650 return true;
651 }
652 continue;
653 }
654
655 // Warning: This loop can end up being somewhat performance sensitive.
656 // We're running this loop for once for each value queried resulting in a
657 // runtime of ~O(#assumes * #values).
658
659 Value *RHS;
660 CmpPredicate Pred;
661 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
662 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
663 continue;
664
665 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
666 return true;
667 }
668
669 return false;
670}
671
673 Value *LHS, Value *RHS, KnownBits &Known,
674 const SimplifyQuery &Q) {
675 if (RHS->getType()->isPointerTy()) {
676 // Handle comparison of pointer to null explicitly, as it will not be
677 // covered by the m_APInt() logic below.
678 if (LHS == V && match(RHS, m_Zero())) {
679 switch (Pred) {
680 case ICmpInst::ICMP_EQ:
681 Known.setAllZero();
682 break;
683 case ICmpInst::ICMP_SGE:
684 case ICmpInst::ICMP_SGT:
685 Known.makeNonNegative();
686 break;
687 case ICmpInst::ICMP_SLT:
688 Known.makeNegative();
689 break;
690 default:
691 break;
692 }
693 }
694 return;
695 }
696
697 unsigned BitWidth = Known.getBitWidth();
698 auto m_V =
700
701 Value *Y;
702 const APInt *Mask, *C;
703 if (!match(RHS, m_APInt(C)))
704 return;
705
706 uint64_t ShAmt;
707 switch (Pred) {
708 case ICmpInst::ICMP_EQ:
709 // assume(V = C)
710 if (match(LHS, m_V)) {
711 Known = Known.unionWith(KnownBits::makeConstant(*C));
712 // assume(V & Mask = C)
713 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
714 // For one bits in Mask, we can propagate bits from C to V.
715 Known.One |= *C;
716 if (match(Y, m_APInt(Mask)))
717 Known.Zero |= ~*C & *Mask;
718 // assume(V | Mask = C)
719 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
720 // For zero bits in Mask, we can propagate bits from C to V.
721 Known.Zero |= ~*C;
722 if (match(Y, m_APInt(Mask)))
723 Known.One |= *C & ~*Mask;
724 // assume(V << ShAmt = C)
725 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
726 ShAmt < BitWidth) {
727 // For those bits in C that are known, we can propagate them to known
728 // bits in V shifted to the right by ShAmt.
730 RHSKnown >>= ShAmt;
731 Known = Known.unionWith(RHSKnown);
732 // assume(V >> ShAmt = C)
733 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
734 ShAmt < BitWidth) {
735 // For those bits in RHS that are known, we can propagate them to known
736 // bits in V shifted to the right by C.
738 RHSKnown <<= ShAmt;
739 Known = Known.unionWith(RHSKnown);
740 }
741 break;
742 case ICmpInst::ICMP_NE: {
743 // assume (V & B != 0) where B is a power of 2
744 const APInt *BPow2;
745 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
746 Known.One |= *BPow2;
747 break;
748 }
749 default: {
750 const APInt *Offset = nullptr;
751 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
753 if (Offset)
754 LHSRange = LHSRange.sub(*Offset);
755 Known = Known.unionWith(LHSRange.toKnownBits());
756 }
757 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
758 // X & Y u> C -> X u> C && Y u> C
759 // X nuw- Y u> C -> X u> C
760 if (match(LHS, m_c_And(m_V, m_Value())) ||
761 match(LHS, m_NUWSub(m_V, m_Value())))
762 Known.One.setHighBits(
763 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
764 }
765 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
766 // X | Y u< C -> X u< C && Y u< C
767 // X nuw+ Y u< C -> X u< C && Y u< C
768 if (match(LHS, m_c_Or(m_V, m_Value())) ||
769 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
770 Known.Zero.setHighBits(
771 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
772 }
773 }
774 } break;
775 }
776}
777
778static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
779 KnownBits &Known,
780 const SimplifyQuery &SQ, bool Invert) {
782 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
783 Value *LHS = Cmp->getOperand(0);
784 Value *RHS = Cmp->getOperand(1);
785
786 // Handle icmp pred (trunc V), C
787 if (match(LHS, m_Trunc(m_Specific(V)))) {
789 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
790 if (cast<TruncInst>(LHS)->hasNoUnsignedWrap())
791 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
792 else
793 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
794 return;
795 }
796
797 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
798}
799
801 KnownBits &Known, const SimplifyQuery &SQ,
802 bool Invert, unsigned Depth) {
803 Value *A, *B;
806 KnownBits Known2(Known.getBitWidth());
807 KnownBits Known3(Known.getBitWidth());
808 computeKnownBitsFromCond(V, A, Known2, SQ, Invert, Depth + 1);
809 computeKnownBitsFromCond(V, B, Known3, SQ, Invert, Depth + 1);
810 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
812 Known2 = Known2.unionWith(Known3);
813 else
814 Known2 = Known2.intersectWith(Known3);
815 Known = Known.unionWith(Known2);
816 return;
817 }
818
819 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
820 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
821 return;
822 }
823
824 if (match(Cond, m_Trunc(m_Specific(V)))) {
825 KnownBits DstKnown(1);
826 if (Invert) {
827 DstKnown.setAllZero();
828 } else {
829 DstKnown.setAllOnes();
830 }
831 if (cast<TruncInst>(Cond)->hasNoUnsignedWrap()) {
832 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
833 return;
834 }
835 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
836 return;
837 }
838
840 computeKnownBitsFromCond(V, A, Known, SQ, !Invert, Depth + 1);
841}
842
844 const SimplifyQuery &Q, unsigned Depth) {
845 // Handle injected condition.
846 if (Q.CC && Q.CC->AffectedValues.contains(V))
847 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Q, Q.CC->Invert, Depth);
848
849 if (!Q.CxtI)
850 return;
851
852 if (Q.DC && Q.DT) {
853 // Handle dominating conditions.
854 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
855 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
856 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
857 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
858 /*Invert*/ false, Depth);
859
860 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
861 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
862 computeKnownBitsFromCond(V, BI->getCondition(), Known, Q,
863 /*Invert*/ true, Depth);
864 }
865
866 if (Known.hasConflict())
867 Known.resetAll();
868 }
869
870 if (!Q.AC)
871 return;
872
873 unsigned BitWidth = Known.getBitWidth();
874
875 // Note that the patterns below need to be kept in sync with the code
876 // in AssumptionCache::updateAffectedValues.
877
878 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
879 if (!Elem.Assume)
880 continue;
881
882 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
883 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
884 "Got assumption for the wrong function!");
885
886 if (Elem.Index != AssumptionCache::ExprResultIdx) {
887 if (!V->getType()->isPointerTy())
888 continue;
890 *I, I->bundle_op_info_begin()[Elem.Index])) {
891 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
892 // be the producer of the pointer in the bundle. At the moment, align
893 // assumptions aren't optimized away.
894 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
895 isPowerOf2_64(RK.ArgValue) &&
896 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
897 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
898 }
899 continue;
900 }
901
902 // Warning: This loop can end up being somewhat performance sensitive.
903 // We're running this loop for once for each value queried resulting in a
904 // runtime of ~O(#assumes * #values).
905
906 Value *Arg = I->getArgOperand(0);
907
908 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
909 assert(BitWidth == 1 && "assume operand is not i1?");
910 (void)BitWidth;
911 Known.setAllOnes();
912 return;
913 }
914 if (match(Arg, m_Not(m_Specific(V))) &&
916 assert(BitWidth == 1 && "assume operand is not i1?");
917 (void)BitWidth;
918 Known.setAllZero();
919 return;
920 }
921 auto *Trunc = dyn_cast<TruncInst>(Arg);
922 if (Trunc && Trunc->getOperand(0) == V &&
924 if (Trunc->hasNoUnsignedWrap()) {
926 return;
927 }
928 Known.One.setBit(0);
929 return;
930 }
931
932 // The remaining tests are all recursive, so bail out if we hit the limit.
934 continue;
935
936 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
937 if (!Cmp)
938 continue;
939
940 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
941 continue;
942
943 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
944 }
945
946 // Conflicting assumption: Undefined behavior will occur on this execution
947 // path.
948 if (Known.hasConflict())
949 Known.resetAll();
950}
951
952/// Compute known bits from a shift operator, including those with a
953/// non-constant shift amount. Known is the output of this function. Known2 is a
954/// pre-allocated temporary with the same bit width as Known and on return
955/// contains the known bit of the shift value source. KF is an
956/// operator-specific function that, given the known-bits and a shift amount,
957/// compute the implied known-bits of the shift operator's result respectively
958/// for that shift amount. The results from calling KF are conservatively
959/// combined for all permitted shift amounts.
961 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
962 KnownBits &Known2, const SimplifyQuery &Q, unsigned Depth,
963 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
964 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
965 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
966 // To limit compile-time impact, only query isKnownNonZero() if we know at
967 // least something about the shift amount.
968 bool ShAmtNonZero =
969 Known.isNonZero() ||
970 (Known.getMaxValue().ult(Known.getBitWidth()) &&
971 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
972 Known = KF(Known2, Known, ShAmtNonZero);
973}
974
975static KnownBits
976getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
977 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
978 const SimplifyQuery &Q, unsigned Depth) {
979 unsigned BitWidth = KnownLHS.getBitWidth();
980 KnownBits KnownOut(BitWidth);
981 bool IsAnd = false;
982 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
983 Value *X = nullptr, *Y = nullptr;
984
985 switch (I->getOpcode()) {
986 case Instruction::And:
987 KnownOut = KnownLHS & KnownRHS;
988 IsAnd = true;
989 // and(x, -x) is common idioms that will clear all but lowest set
990 // bit. If we have a single known bit in x, we can clear all bits
991 // above it.
992 // TODO: instcombine often reassociates independent `and` which can hide
993 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
994 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
995 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
996 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
997 KnownOut = KnownLHS.blsi();
998 else
999 KnownOut = KnownRHS.blsi();
1000 }
1001 break;
1002 case Instruction::Or:
1003 KnownOut = KnownLHS | KnownRHS;
1004 break;
1005 case Instruction::Xor:
1006 KnownOut = KnownLHS ^ KnownRHS;
1007 // xor(x, x-1) is common idioms that will clear all but lowest set
1008 // bit. If we have a single known bit in x, we can clear all bits
1009 // above it.
1010 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
1011 // -1 but for the purpose of demanded bits (xor(x, x-C) &
1012 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
1013 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
1014 if (HasKnownOne &&
1016 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
1017 KnownOut = XBits.blsmsk();
1018 }
1019 break;
1020 default:
1021 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
1022 }
1023
1024 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
1025 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1026 // here we handle the more general case of adding any odd number by
1027 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1028 // TODO: This could be generalized to clearing any bit set in y where the
1029 // following bit is known to be unset in y.
1030 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1034 KnownBits KnownY(BitWidth);
1035 computeKnownBits(Y, DemandedElts, KnownY, Q, Depth + 1);
1036 if (KnownY.countMinTrailingOnes() > 0) {
1037 if (IsAnd)
1038 KnownOut.Zero.setBit(0);
1039 else
1040 KnownOut.One.setBit(0);
1041 }
1042 }
1043 return KnownOut;
1044}
1045
1047 const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q,
1048 unsigned Depth,
1049 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1050 KnownBitsFunc) {
1051 APInt DemandedEltsLHS, DemandedEltsRHS;
1053 DemandedElts, DemandedEltsLHS,
1054 DemandedEltsRHS);
1055
1056 const auto ComputeForSingleOpFunc =
1057 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1058 return KnownBitsFunc(
1059 computeKnownBits(Op, DemandedEltsOp, Q, Depth + 1),
1060 computeKnownBits(Op, DemandedEltsOp << 1, Q, Depth + 1));
1061 };
1062
1063 if (DemandedEltsRHS.isZero())
1064 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1065 if (DemandedEltsLHS.isZero())
1066 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1067
1068 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1069 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1070}
1071
1072// Public so this can be used in `SimplifyDemandedUseBits`.
1074 const KnownBits &KnownLHS,
1075 const KnownBits &KnownRHS,
1076 const SimplifyQuery &SQ,
1077 unsigned Depth) {
1078 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1079 APInt DemandedElts =
1080 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1081
1082 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, SQ,
1083 Depth);
1084}
1085
1087 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1088 // Without vscale_range, we only know that vscale is non-zero.
1089 if (!Attr.isValid())
1091
1092 unsigned AttrMin = Attr.getVScaleRangeMin();
1093 // Minimum is larger than vscale width, result is always poison.
1094 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1095 return ConstantRange::getEmpty(BitWidth);
1096
1097 APInt Min(BitWidth, AttrMin);
1098 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1099 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1101
1102 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1103}
1104
1106 Value *Arm, bool Invert,
1107 const SimplifyQuery &Q, unsigned Depth) {
1108 // If we have a constant arm, we are done.
1109 if (Known.isConstant())
1110 return;
1111
1112 // See what condition implies about the bits of the select arm.
1113 KnownBits CondRes(Known.getBitWidth());
1114 computeKnownBitsFromCond(Arm, Cond, CondRes, Q, Invert, Depth + 1);
1115 // If we don't get any information from the condition, no reason to
1116 // proceed.
1117 if (CondRes.isUnknown())
1118 return;
1119
1120 // We can have conflict if the condition is dead. I.e if we have
1121 // (x | 64) < 32 ? (x | 64) : y
1122 // we will have conflict at bit 6 from the condition/the `or`.
1123 // In that case just return. Its not particularly important
1124 // what we do, as this select is going to be simplified soon.
1125 CondRes = CondRes.unionWith(Known);
1126 if (CondRes.hasConflict())
1127 return;
1128
1129 // Finally make sure the information we found is valid. This is relatively
1130 // expensive so it's left for the very end.
1131 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1132 return;
1133
1134 // Finally, we know we get information from the condition and its valid,
1135 // so return it.
1136 Known = CondRes;
1137}
1138
1139// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1140// Returns the input and lower/upper bounds.
1141static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1142 const APInt *&CLow, const APInt *&CHigh) {
1143 assert(isa<Operator>(Select) &&
1144 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1145 "Input should be a Select!");
1146
1147 const Value *LHS = nullptr, *RHS = nullptr;
1149 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1150 return false;
1151
1152 if (!match(RHS, m_APInt(CLow)))
1153 return false;
1154
1155 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1157 if (getInverseMinMaxFlavor(SPF) != SPF2)
1158 return false;
1159
1160 if (!match(RHS2, m_APInt(CHigh)))
1161 return false;
1162
1163 if (SPF == SPF_SMIN)
1164 std::swap(CLow, CHigh);
1165
1166 In = LHS2;
1167 return CLow->sle(*CHigh);
1168}
1169
1171 const APInt *&CLow,
1172 const APInt *&CHigh) {
1173 assert((II->getIntrinsicID() == Intrinsic::smin ||
1174 II->getIntrinsicID() == Intrinsic::smax) &&
1175 "Must be smin/smax");
1176
1177 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1178 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1179 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1180 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1181 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1182 return false;
1183
1184 if (II->getIntrinsicID() == Intrinsic::smin)
1185 std::swap(CLow, CHigh);
1186 return CLow->sle(*CHigh);
1187}
1188
1190 KnownBits &Known) {
1191 const APInt *CLow, *CHigh;
1192 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1193 Known = Known.unionWith(
1194 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1195}
1196
1198 const APInt &DemandedElts,
1199 KnownBits &Known,
1200 const SimplifyQuery &Q,
1201 unsigned Depth) {
1202 unsigned BitWidth = Known.getBitWidth();
1203
1204 KnownBits Known2(BitWidth);
1205 switch (I->getOpcode()) {
1206 default: break;
1207 case Instruction::Load:
1208 if (MDNode *MD =
1209 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1211 break;
1212 case Instruction::And:
1213 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1214 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1215
1216 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1217 break;
1218 case Instruction::Or:
1219 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1220 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1221
1222 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1223 break;
1224 case Instruction::Xor:
1225 computeKnownBits(I->getOperand(1), DemandedElts, Known, Q, Depth + 1);
1226 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1227
1228 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Q, Depth);
1229 break;
1230 case Instruction::Mul: {
1231 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1232 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1233 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1234 DemandedElts, Known, Known2, Q, Depth);
1235 break;
1236 }
1237 case Instruction::UDiv: {
1238 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1239 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1240 Known =
1241 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1242 break;
1243 }
1244 case Instruction::SDiv: {
1245 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1246 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1247 Known =
1248 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1249 break;
1250 }
1251 case Instruction::Select: {
1252 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1253 KnownBits Res(Known.getBitWidth());
1254 computeKnownBits(Arm, DemandedElts, Res, Q, Depth + 1);
1255 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Q, Depth);
1256 return Res;
1257 };
1258 // Only known if known in both the LHS and RHS.
1259 Known =
1260 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1261 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1262 break;
1263 }
1264 case Instruction::FPTrunc:
1265 case Instruction::FPExt:
1266 case Instruction::FPToUI:
1267 case Instruction::FPToSI:
1268 case Instruction::SIToFP:
1269 case Instruction::UIToFP:
1270 break; // Can't work with floating point.
1271 case Instruction::PtrToInt:
1272 case Instruction::IntToPtr:
1273 // Fall through and handle them the same as zext/trunc.
1274 [[fallthrough]];
1275 case Instruction::ZExt:
1276 case Instruction::Trunc: {
1277 Type *SrcTy = I->getOperand(0)->getType();
1278
1279 unsigned SrcBitWidth;
1280 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1281 // which fall through here.
1282 Type *ScalarTy = SrcTy->getScalarType();
1283 SrcBitWidth = ScalarTy->isPointerTy() ?
1284 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1285 Q.DL.getTypeSizeInBits(ScalarTy);
1286
1287 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1288 Known = Known.anyextOrTrunc(SrcBitWidth);
1289 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1290 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1291 Inst && Inst->hasNonNeg() && !Known.isNegative())
1292 Known.makeNonNegative();
1293 Known = Known.zextOrTrunc(BitWidth);
1294 break;
1295 }
1296 case Instruction::BitCast: {
1297 Type *SrcTy = I->getOperand(0)->getType();
1298 if (SrcTy->isIntOrPtrTy() &&
1299 // TODO: For now, not handling conversions like:
1300 // (bitcast i64 %x to <2 x i32>)
1301 !I->getType()->isVectorTy()) {
1302 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1303 break;
1304 }
1305
1306 const Value *V;
1307 // Handle bitcast from floating point to integer.
1308 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1309 V->getType()->isFPOrFPVectorTy()) {
1310 Type *FPType = V->getType()->getScalarType();
1311 KnownFPClass Result =
1312 computeKnownFPClass(V, DemandedElts, fcAllFlags, Q, Depth + 1);
1313 FPClassTest FPClasses = Result.KnownFPClasses;
1314
1315 // TODO: Treat it as zero/poison if the use of I is unreachable.
1316 if (FPClasses == fcNone)
1317 break;
1318
1319 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1320 Known.Zero.setAllBits();
1321 Known.One.setAllBits();
1322
1323 if (FPClasses & fcInf)
1325 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1326
1327 if (FPClasses & fcZero)
1329 APInt::getZero(FPType->getScalarSizeInBits())));
1330
1331 Known.Zero.clearSignBit();
1332 Known.One.clearSignBit();
1333 }
1334
1335 if (Result.SignBit) {
1336 if (*Result.SignBit)
1337 Known.makeNegative();
1338 else
1339 Known.makeNonNegative();
1340 }
1341
1342 break;
1343 }
1344
1345 // Handle cast from vector integer type to scalar or vector integer.
1346 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1347 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1348 !I->getType()->isIntOrIntVectorTy() ||
1349 isa<ScalableVectorType>(I->getType()))
1350 break;
1351
1352 unsigned NumElts = DemandedElts.getBitWidth();
1353 bool IsLE = Q.DL.isLittleEndian();
1354 // Look through a cast from narrow vector elements to wider type.
1355 // Examples: v4i32 -> v2i64, v3i8 -> v24
1356 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1357 if (BitWidth % SubBitWidth == 0) {
1358 // Known bits are automatically intersected across demanded elements of a
1359 // vector. So for example, if a bit is computed as known zero, it must be
1360 // zero across all demanded elements of the vector.
1361 //
1362 // For this bitcast, each demanded element of the output is sub-divided
1363 // across a set of smaller vector elements in the source vector. To get
1364 // the known bits for an entire element of the output, compute the known
1365 // bits for each sub-element sequentially. This is done by shifting the
1366 // one-set-bit demanded elements parameter across the sub-elements for
1367 // consecutive calls to computeKnownBits. We are using the demanded
1368 // elements parameter as a mask operator.
1369 //
1370 // The known bits of each sub-element are then inserted into place
1371 // (dependent on endian) to form the full result of known bits.
1372 unsigned SubScale = BitWidth / SubBitWidth;
1373 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1374 for (unsigned i = 0; i != NumElts; ++i) {
1375 if (DemandedElts[i])
1376 SubDemandedElts.setBit(i * SubScale);
1377 }
1378
1379 KnownBits KnownSrc(SubBitWidth);
1380 for (unsigned i = 0; i != SubScale; ++i) {
1381 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc, Q,
1382 Depth + 1);
1383 unsigned ShiftElt = IsLE ? i : SubScale - 1 - i;
1384 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1385 }
1386 }
1387 // Look through a cast from wider vector elements to narrow type.
1388 // Examples: v2i64 -> v4i32
1389 if (SubBitWidth % BitWidth == 0) {
1390 unsigned SubScale = SubBitWidth / BitWidth;
1391 KnownBits KnownSrc(SubBitWidth);
1392 APInt SubDemandedElts =
1393 APIntOps::ScaleBitMask(DemandedElts, NumElts / SubScale);
1394 computeKnownBits(I->getOperand(0), SubDemandedElts, KnownSrc, Q,
1395 Depth + 1);
1396
1397 Known.Zero.setAllBits();
1398 Known.One.setAllBits();
1399 for (unsigned i = 0; i != NumElts; ++i) {
1400 if (DemandedElts[i]) {
1401 unsigned Shifts = IsLE ? i : NumElts - 1 - i;
1402 unsigned Offset = (Shifts % SubScale) * BitWidth;
1403 Known = Known.intersectWith(KnownSrc.extractBits(BitWidth, Offset));
1404 if (Known.isUnknown())
1405 break;
1406 }
1407 }
1408 }
1409 break;
1410 }
1411 case Instruction::SExt: {
1412 // Compute the bits in the result that are not present in the input.
1413 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1414
1415 Known = Known.trunc(SrcBitWidth);
1416 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1417 // If the sign bit of the input is known set or clear, then we know the
1418 // top bits of the result.
1419 Known = Known.sext(BitWidth);
1420 break;
1421 }
1422 case Instruction::Shl: {
1423 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1424 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1425 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1426 bool ShAmtNonZero) {
1427 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1428 };
1429 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1430 KF);
1431 // Trailing zeros of a right-shifted constant never decrease.
1432 const APInt *C;
1433 if (match(I->getOperand(0), m_APInt(C)))
1434 Known.Zero.setLowBits(C->countr_zero());
1435 break;
1436 }
1437 case Instruction::LShr: {
1438 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1439 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1440 bool ShAmtNonZero) {
1441 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1442 };
1443 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1444 KF);
1445 // Leading zeros of a left-shifted constant never decrease.
1446 const APInt *C;
1447 if (match(I->getOperand(0), m_APInt(C)))
1448 Known.Zero.setHighBits(C->countl_zero());
1449 break;
1450 }
1451 case Instruction::AShr: {
1452 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1453 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1454 bool ShAmtNonZero) {
1455 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1456 };
1457 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Q, Depth,
1458 KF);
1459 break;
1460 }
1461 case Instruction::Sub: {
1462 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1463 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1464 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1465 DemandedElts, Known, Known2, Q, Depth);
1466 break;
1467 }
1468 case Instruction::Add: {
1469 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1470 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1471 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1472 DemandedElts, Known, Known2, Q, Depth);
1473 break;
1474 }
1475 case Instruction::SRem:
1476 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1477 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1478 Known = KnownBits::srem(Known, Known2);
1479 break;
1480
1481 case Instruction::URem:
1482 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1483 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1484 Known = KnownBits::urem(Known, Known2);
1485 break;
1486 case Instruction::Alloca:
1487 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
1488 break;
1489 case Instruction::GetElementPtr: {
1490 // Analyze all of the subscripts of this getelementptr instruction
1491 // to determine if we can prove known low zero bits.
1492 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1493 // Accumulate the constant indices in a separate variable
1494 // to minimize the number of calls to computeForAddSub.
1495 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1496 APInt AccConstIndices(IndexWidth, 0);
1497
1498 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1499 if (IndexWidth == BitWidth) {
1500 // Note that inbounds does *not* guarantee nsw for the addition, as only
1501 // the offset is signed, while the base address is unsigned.
1502 Known = KnownBits::add(Known, IndexBits);
1503 } else {
1504 // If the index width is smaller than the pointer width, only add the
1505 // value to the low bits.
1506 assert(IndexWidth < BitWidth &&
1507 "Index width can't be larger than pointer width");
1508 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1509 }
1510 };
1511
1513 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1514 // TrailZ can only become smaller, short-circuit if we hit zero.
1515 if (Known.isUnknown())
1516 break;
1517
1518 Value *Index = I->getOperand(i);
1519
1520 // Handle case when index is zero.
1521 Constant *CIndex = dyn_cast<Constant>(Index);
1522 if (CIndex && CIndex->isZeroValue())
1523 continue;
1524
1525 if (StructType *STy = GTI.getStructTypeOrNull()) {
1526 // Handle struct member offset arithmetic.
1527
1528 assert(CIndex &&
1529 "Access to structure field must be known at compile time");
1530
1531 if (CIndex->getType()->isVectorTy())
1532 Index = CIndex->getSplatValue();
1533
1534 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1535 const StructLayout *SL = Q.DL.getStructLayout(STy);
1537 AccConstIndices += Offset;
1538 continue;
1539 }
1540
1541 // Handle array index arithmetic.
1542 Type *IndexedTy = GTI.getIndexedType();
1543 if (!IndexedTy->isSized()) {
1544 Known.resetAll();
1545 break;
1546 }
1547
1548 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1549 uint64_t StrideInBytes = Stride.getKnownMinValue();
1550 if (!Stride.isScalable()) {
1551 // Fast path for constant offset.
1552 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1553 AccConstIndices +=
1554 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1555 continue;
1556 }
1557 }
1558
1559 KnownBits IndexBits =
1560 computeKnownBits(Index, Q, Depth + 1).sextOrTrunc(IndexWidth);
1561 KnownBits ScalingFactor(IndexWidth);
1562 // Multiply by current sizeof type.
1563 // &A[i] == A + i * sizeof(*A[i]).
1564 if (Stride.isScalable()) {
1565 // For scalable types the only thing we know about sizeof is
1566 // that this is a multiple of the minimum size.
1567 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1568 } else {
1569 ScalingFactor =
1570 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1571 }
1572 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1573 }
1574 if (!Known.isUnknown() && !AccConstIndices.isZero())
1575 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1576 break;
1577 }
1578 case Instruction::PHI: {
1579 const PHINode *P = cast<PHINode>(I);
1580 BinaryOperator *BO = nullptr;
1581 Value *R = nullptr, *L = nullptr;
1582 if (matchSimpleRecurrence(P, BO, R, L)) {
1583 // Handle the case of a simple two-predecessor recurrence PHI.
1584 // There's a lot more that could theoretically be done here, but
1585 // this is sufficient to catch some interesting cases.
1586 unsigned Opcode = BO->getOpcode();
1587
1588 switch (Opcode) {
1589 // If this is a shift recurrence, we know the bits being shifted in. We
1590 // can combine that with information about the start value of the
1591 // recurrence to conclude facts about the result. If this is a udiv
1592 // recurrence, we know that the result can never exceed either the
1593 // numerator or the start value, whichever is greater.
1594 case Instruction::LShr:
1595 case Instruction::AShr:
1596 case Instruction::Shl:
1597 case Instruction::UDiv:
1598 if (BO->getOperand(0) != I)
1599 break;
1600 [[fallthrough]];
1601
1602 // For a urem recurrence, the result can never exceed the start value. The
1603 // phi could either be the numerator or the denominator.
1604 case Instruction::URem: {
1605 // We have matched a recurrence of the form:
1606 // %iv = [R, %entry], [%iv.next, %backedge]
1607 // %iv.next = shift_op %iv, L
1608
1609 // Recurse with the phi context to avoid concern about whether facts
1610 // inferred hold at original context instruction. TODO: It may be
1611 // correct to use the original context. IF warranted, explore and
1612 // add sufficient tests to cover.
1614 RecQ.CxtI = P;
1615 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1616 switch (Opcode) {
1617 case Instruction::Shl:
1618 // A shl recurrence will only increase the tailing zeros
1619 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1620 break;
1621 case Instruction::LShr:
1622 case Instruction::UDiv:
1623 case Instruction::URem:
1624 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1625 // the start value.
1626 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1627 break;
1628 case Instruction::AShr:
1629 // An ashr recurrence will extend the initial sign bit
1630 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1631 Known.One.setHighBits(Known2.countMinLeadingOnes());
1632 break;
1633 }
1634 break;
1635 }
1636
1637 // Check for operations that have the property that if
1638 // both their operands have low zero bits, the result
1639 // will have low zero bits.
1640 case Instruction::Add:
1641 case Instruction::Sub:
1642 case Instruction::And:
1643 case Instruction::Or:
1644 case Instruction::Mul: {
1645 // Change the context instruction to the "edge" that flows into the
1646 // phi. This is important because that is where the value is actually
1647 // "evaluated" even though it is used later somewhere else. (see also
1648 // D69571).
1650
1651 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1652 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1653 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1654
1655 // Ok, we have a PHI of the form L op= R. Check for low
1656 // zero bits.
1657 RecQ.CxtI = RInst;
1658 computeKnownBits(R, DemandedElts, Known2, RecQ, Depth + 1);
1659
1660 // We need to take the minimum number of known bits
1661 KnownBits Known3(BitWidth);
1662 RecQ.CxtI = LInst;
1663 computeKnownBits(L, DemandedElts, Known3, RecQ, Depth + 1);
1664
1665 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1666 Known3.countMinTrailingZeros()));
1667
1668 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1669 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1670 break;
1671
1672 switch (Opcode) {
1673 // If initial value of recurrence is nonnegative, and we are adding
1674 // a nonnegative number with nsw, the result can only be nonnegative
1675 // or poison value regardless of the number of times we execute the
1676 // add in phi recurrence. If initial value is negative and we are
1677 // adding a negative number with nsw, the result can only be
1678 // negative or poison value. Similar arguments apply to sub and mul.
1679 //
1680 // (add non-negative, non-negative) --> non-negative
1681 // (add negative, negative) --> negative
1682 case Instruction::Add: {
1683 if (Known2.isNonNegative() && Known3.isNonNegative())
1684 Known.makeNonNegative();
1685 else if (Known2.isNegative() && Known3.isNegative())
1686 Known.makeNegative();
1687 break;
1688 }
1689
1690 // (sub nsw non-negative, negative) --> non-negative
1691 // (sub nsw negative, non-negative) --> negative
1692 case Instruction::Sub: {
1693 if (BO->getOperand(0) != I)
1694 break;
1695 if (Known2.isNonNegative() && Known3.isNegative())
1696 Known.makeNonNegative();
1697 else if (Known2.isNegative() && Known3.isNonNegative())
1698 Known.makeNegative();
1699 break;
1700 }
1701
1702 // (mul nsw non-negative, non-negative) --> non-negative
1703 case Instruction::Mul:
1704 if (Known2.isNonNegative() && Known3.isNonNegative())
1705 Known.makeNonNegative();
1706 break;
1707
1708 default:
1709 break;
1710 }
1711 break;
1712 }
1713
1714 default:
1715 break;
1716 }
1717 }
1718
1719 // Unreachable blocks may have zero-operand PHI nodes.
1720 if (P->getNumIncomingValues() == 0)
1721 break;
1722
1723 // Otherwise take the unions of the known bit sets of the operands,
1724 // taking conservative care to avoid excessive recursion.
1725 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1726 // Skip if every incoming value references to ourself.
1727 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1728 break;
1729
1730 Known.Zero.setAllBits();
1731 Known.One.setAllBits();
1732 for (const Use &U : P->operands()) {
1733 Value *IncValue;
1734 const PHINode *CxtPhi;
1735 Instruction *CxtI;
1736 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1737 // Skip direct self references.
1738 if (IncValue == P)
1739 continue;
1740
1741 // Change the context instruction to the "edge" that flows into the
1742 // phi. This is important because that is where the value is actually
1743 // "evaluated" even though it is used later somewhere else. (see also
1744 // D69571).
1746
1747 Known2 = KnownBits(BitWidth);
1748
1749 // Recurse, but cap the recursion to one level, because we don't
1750 // want to waste time spinning around in loops.
1751 // TODO: See if we can base recursion limiter on number of incoming phi
1752 // edges so we don't overly clamp analysis.
1753 computeKnownBits(IncValue, DemandedElts, Known2, RecQ,
1755
1756 // See if we can further use a conditional branch into the phi
1757 // to help us determine the range of the value.
1758 if (!Known2.isConstant()) {
1759 CmpPredicate Pred;
1760 const APInt *RHSC;
1761 BasicBlock *TrueSucc, *FalseSucc;
1762 // TODO: Use RHS Value and compute range from its known bits.
1763 if (match(RecQ.CxtI,
1764 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1765 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1766 // Check for cases of duplicate successors.
1767 if ((TrueSucc == CxtPhi->getParent()) !=
1768 (FalseSucc == CxtPhi->getParent())) {
1769 // If we're using the false successor, invert the predicate.
1770 if (FalseSucc == CxtPhi->getParent())
1771 Pred = CmpInst::getInversePredicate(Pred);
1772 // Get the knownbits implied by the incoming phi condition.
1773 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1774 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1775 // We can have conflicts here if we are analyzing deadcode (its
1776 // impossible for us reach this BB based the icmp).
1777 if (KnownUnion.hasConflict()) {
1778 // No reason to continue analyzing in a known dead region, so
1779 // just resetAll and break. This will cause us to also exit the
1780 // outer loop.
1781 Known.resetAll();
1782 break;
1783 }
1784 Known2 = KnownUnion;
1785 }
1786 }
1787 }
1788
1789 Known = Known.intersectWith(Known2);
1790 // If all bits have been ruled out, there's no need to check
1791 // more operands.
1792 if (Known.isUnknown())
1793 break;
1794 }
1795 }
1796 break;
1797 }
1798 case Instruction::Call:
1799 case Instruction::Invoke: {
1800 // If range metadata is attached to this call, set known bits from that,
1801 // and then intersect with known bits based on other properties of the
1802 // function.
1803 if (MDNode *MD =
1804 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1806
1807 const auto *CB = cast<CallBase>(I);
1808
1809 if (std::optional<ConstantRange> Range = CB->getRange())
1810 Known = Known.unionWith(Range->toKnownBits());
1811
1812 if (const Value *RV = CB->getReturnedArgOperand()) {
1813 if (RV->getType() == I->getType()) {
1814 computeKnownBits(RV, Known2, Q, Depth + 1);
1815 Known = Known.unionWith(Known2);
1816 // If the function doesn't return properly for all input values
1817 // (e.g. unreachable exits) then there might be conflicts between the
1818 // argument value and the range metadata. Simply discard the known bits
1819 // in case of conflicts.
1820 if (Known.hasConflict())
1821 Known.resetAll();
1822 }
1823 }
1824 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1825 switch (II->getIntrinsicID()) {
1826 default:
1827 break;
1828 case Intrinsic::abs: {
1829 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1830 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
1831 Known = Known.unionWith(Known2.abs(IntMinIsPoison));
1832 break;
1833 }
1834 case Intrinsic::bitreverse:
1835 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1836 Known = Known.unionWith(Known2.reverseBits());
1837 break;
1838 case Intrinsic::bswap:
1839 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1840 Known = Known.unionWith(Known2.byteSwap());
1841 break;
1842 case Intrinsic::ctlz: {
1843 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1844 // If we have a known 1, its position is our upper bound.
1845 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
1846 // If this call is poison for 0 input, the result will be less than 2^n.
1847 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1848 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1849 unsigned LowBits = llvm::bit_width(PossibleLZ);
1850 Known.Zero.setBitsFrom(LowBits);
1851 break;
1852 }
1853 case Intrinsic::cttz: {
1854 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1855 // If we have a known 1, its position is our upper bound.
1856 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
1857 // If this call is poison for 0 input, the result will be less than 2^n.
1858 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1859 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1860 unsigned LowBits = llvm::bit_width(PossibleTZ);
1861 Known.Zero.setBitsFrom(LowBits);
1862 break;
1863 }
1864 case Intrinsic::ctpop: {
1865 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1866 // We can bound the space the count needs. Also, bits known to be zero
1867 // can't contribute to the population.
1868 unsigned BitsPossiblySet = Known2.countMaxPopulation();
1869 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
1870 Known.Zero.setBitsFrom(LowBits);
1871 // TODO: we could bound KnownOne using the lower bound on the number
1872 // of bits which might be set provided by popcnt KnownOne2.
1873 break;
1874 }
1875 case Intrinsic::fshr:
1876 case Intrinsic::fshl: {
1877 const APInt *SA;
1878 if (!match(I->getOperand(2), m_APInt(SA)))
1879 break;
1880
1881 // Normalize to funnel shift left.
1882 uint64_t ShiftAmt = SA->urem(BitWidth);
1883 if (II->getIntrinsicID() == Intrinsic::fshr)
1884 ShiftAmt = BitWidth - ShiftAmt;
1885
1886 KnownBits Known3(BitWidth);
1887 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Q, Depth + 1);
1888 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Q, Depth + 1);
1889
1890 Known2 <<= ShiftAmt;
1891 Known3 >>= BitWidth - ShiftAmt;
1892 Known = Known2.unionWith(Known3);
1893 break;
1894 }
1895 case Intrinsic::uadd_sat:
1896 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1897 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1898 Known = KnownBits::uadd_sat(Known, Known2);
1899 break;
1900 case Intrinsic::usub_sat:
1901 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1902 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1903 Known = KnownBits::usub_sat(Known, Known2);
1904 break;
1905 case Intrinsic::sadd_sat:
1906 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1907 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1908 Known = KnownBits::sadd_sat(Known, Known2);
1909 break;
1910 case Intrinsic::ssub_sat:
1911 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1912 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1913 Known = KnownBits::ssub_sat(Known, Known2);
1914 break;
1915 // Vec reverse preserves bits from input vec.
1916 case Intrinsic::vector_reverse:
1917 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known, Q,
1918 Depth + 1);
1919 break;
1920 // for min/max/and/or reduce, any bit common to each element in the
1921 // input vec is set in the output.
1922 case Intrinsic::vector_reduce_and:
1923 case Intrinsic::vector_reduce_or:
1924 case Intrinsic::vector_reduce_umax:
1925 case Intrinsic::vector_reduce_umin:
1926 case Intrinsic::vector_reduce_smax:
1927 case Intrinsic::vector_reduce_smin:
1928 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1929 break;
1930 case Intrinsic::vector_reduce_xor: {
1931 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
1932 // The zeros common to all vecs are zero in the output.
1933 // If the number of elements is odd, then the common ones remain. If the
1934 // number of elements is even, then the common ones becomes zeros.
1935 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
1936 // Even, so the ones become zeros.
1937 bool EvenCnt = VecTy->getElementCount().isKnownEven();
1938 if (EvenCnt)
1939 Known.Zero |= Known.One;
1940 // Maybe even element count so need to clear ones.
1941 if (VecTy->isScalableTy() || EvenCnt)
1942 Known.One.clearAllBits();
1943 break;
1944 }
1945 case Intrinsic::umin:
1946 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1947 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1948 Known = KnownBits::umin(Known, Known2);
1949 break;
1950 case Intrinsic::umax:
1951 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1952 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1953 Known = KnownBits::umax(Known, Known2);
1954 break;
1955 case Intrinsic::smin:
1956 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1957 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1958 Known = KnownBits::smin(Known, Known2);
1960 break;
1961 case Intrinsic::smax:
1962 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1963 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1964 Known = KnownBits::smax(Known, Known2);
1966 break;
1967 case Intrinsic::ptrmask: {
1968 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1969
1970 const Value *Mask = I->getOperand(1);
1971 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
1972 computeKnownBits(Mask, DemandedElts, Known2, Q, Depth + 1);
1973 // TODO: 1-extend would be more precise.
1974 Known &= Known2.anyextOrTrunc(BitWidth);
1975 break;
1976 }
1977 case Intrinsic::x86_sse2_pmulh_w:
1978 case Intrinsic::x86_avx2_pmulh_w:
1979 case Intrinsic::x86_avx512_pmulh_w_512:
1980 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1981 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1982 Known = KnownBits::mulhs(Known, Known2);
1983 break;
1984 case Intrinsic::x86_sse2_pmulhu_w:
1985 case Intrinsic::x86_avx2_pmulhu_w:
1986 case Intrinsic::x86_avx512_pmulhu_w_512:
1987 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth + 1);
1988 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Q, Depth + 1);
1989 Known = KnownBits::mulhu(Known, Known2);
1990 break;
1991 case Intrinsic::x86_sse42_crc32_64_64:
1992 Known.Zero.setBitsFrom(32);
1993 break;
1994 case Intrinsic::x86_ssse3_phadd_d_128:
1995 case Intrinsic::x86_ssse3_phadd_w_128:
1996 case Intrinsic::x86_avx2_phadd_d:
1997 case Intrinsic::x86_avx2_phadd_w: {
1999 I, DemandedElts, Q, Depth,
2000 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2001 return KnownBits::add(KnownLHS, KnownRHS);
2002 });
2003 break;
2004 }
2005 case Intrinsic::x86_ssse3_phadd_sw_128:
2006 case Intrinsic::x86_avx2_phadd_sw: {
2008 I, DemandedElts, Q, Depth, KnownBits::sadd_sat);
2009 break;
2010 }
2011 case Intrinsic::x86_ssse3_phsub_d_128:
2012 case Intrinsic::x86_ssse3_phsub_w_128:
2013 case Intrinsic::x86_avx2_phsub_d:
2014 case Intrinsic::x86_avx2_phsub_w: {
2016 I, DemandedElts, Q, Depth,
2017 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
2018 return KnownBits::sub(KnownLHS, KnownRHS);
2019 });
2020 break;
2021 }
2022 case Intrinsic::x86_ssse3_phsub_sw_128:
2023 case Intrinsic::x86_avx2_phsub_sw: {
2025 I, DemandedElts, Q, Depth, KnownBits::ssub_sat);
2026 break;
2027 }
2028 case Intrinsic::riscv_vsetvli:
2029 case Intrinsic::riscv_vsetvlimax: {
2030 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
2031 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
2033 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
2034 RISCVVType::VLMUL VLMUL = static_cast<RISCVVType::VLMUL>(
2035 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
2036 uint64_t MaxVLEN =
2038 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
2039
2040 // Result of vsetvli must be not larger than AVL.
2041 if (HasAVL)
2042 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
2043 MaxVL = std::min(MaxVL, CI->getZExtValue());
2044
2045 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
2046 if (BitWidth > KnownZeroFirstBit)
2047 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2048 break;
2049 }
2050 case Intrinsic::vscale: {
2051 if (!II->getParent() || !II->getFunction())
2052 break;
2053
2054 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2055 break;
2056 }
2057 }
2058 }
2059 break;
2060 }
2061 case Instruction::ShuffleVector: {
2062 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2063 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2064 if (!Shuf) {
2065 Known.resetAll();
2066 return;
2067 }
2068 // For undef elements, we don't know anything about the common state of
2069 // the shuffle result.
2070 APInt DemandedLHS, DemandedRHS;
2071 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2072 Known.resetAll();
2073 return;
2074 }
2075 Known.One.setAllBits();
2076 Known.Zero.setAllBits();
2077 if (!!DemandedLHS) {
2078 const Value *LHS = Shuf->getOperand(0);
2079 computeKnownBits(LHS, DemandedLHS, Known, Q, Depth + 1);
2080 // If we don't know any bits, early out.
2081 if (Known.isUnknown())
2082 break;
2083 }
2084 if (!!DemandedRHS) {
2085 const Value *RHS = Shuf->getOperand(1);
2086 computeKnownBits(RHS, DemandedRHS, Known2, Q, Depth + 1);
2087 Known = Known.intersectWith(Known2);
2088 }
2089 break;
2090 }
2091 case Instruction::InsertElement: {
2092 if (isa<ScalableVectorType>(I->getType())) {
2093 Known.resetAll();
2094 return;
2095 }
2096 const Value *Vec = I->getOperand(0);
2097 const Value *Elt = I->getOperand(1);
2098 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2099 unsigned NumElts = DemandedElts.getBitWidth();
2100 APInt DemandedVecElts = DemandedElts;
2101 bool NeedsElt = true;
2102 // If we know the index we are inserting too, clear it from Vec check.
2103 if (CIdx && CIdx->getValue().ult(NumElts)) {
2104 DemandedVecElts.clearBit(CIdx->getZExtValue());
2105 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2106 }
2107
2108 Known.One.setAllBits();
2109 Known.Zero.setAllBits();
2110 if (NeedsElt) {
2111 computeKnownBits(Elt, Known, Q, Depth + 1);
2112 // If we don't know any bits, early out.
2113 if (Known.isUnknown())
2114 break;
2115 }
2116
2117 if (!DemandedVecElts.isZero()) {
2118 computeKnownBits(Vec, DemandedVecElts, Known2, Q, Depth + 1);
2119 Known = Known.intersectWith(Known2);
2120 }
2121 break;
2122 }
2123 case Instruction::ExtractElement: {
2124 // Look through extract element. If the index is non-constant or
2125 // out-of-range demand all elements, otherwise just the extracted element.
2126 const Value *Vec = I->getOperand(0);
2127 const Value *Idx = I->getOperand(1);
2128 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2129 if (isa<ScalableVectorType>(Vec->getType())) {
2130 // FIXME: there's probably *something* we can do with scalable vectors
2131 Known.resetAll();
2132 break;
2133 }
2134 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2135 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2136 if (CIdx && CIdx->getValue().ult(NumElts))
2137 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2138 computeKnownBits(Vec, DemandedVecElts, Known, Q, Depth + 1);
2139 break;
2140 }
2141 case Instruction::ExtractValue:
2142 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2143 const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
2144 if (EVI->getNumIndices() != 1) break;
2145 if (EVI->getIndices()[0] == 0) {
2146 switch (II->getIntrinsicID()) {
2147 default: break;
2148 case Intrinsic::uadd_with_overflow:
2149 case Intrinsic::sadd_with_overflow:
2151 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2152 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2153 break;
2154 case Intrinsic::usub_with_overflow:
2155 case Intrinsic::ssub_with_overflow:
2157 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2158 /* NUW=*/false, DemandedElts, Known, Known2, Q, Depth);
2159 break;
2160 case Intrinsic::umul_with_overflow:
2161 case Intrinsic::smul_with_overflow:
2162 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2163 false, DemandedElts, Known, Known2, Q, Depth);
2164 break;
2165 }
2166 }
2167 }
2168 break;
2169 case Instruction::Freeze:
2170 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2171 Depth + 1))
2172 computeKnownBits(I->getOperand(0), Known, Q, Depth + 1);
2173 break;
2174 }
2175}
2176
2177/// Determine which bits of V are known to be either zero or one and return
2178/// them.
2179KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2180 const SimplifyQuery &Q, unsigned Depth) {
2181 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2182 ::computeKnownBits(V, DemandedElts, Known, Q, Depth);
2183 return Known;
2184}
2185
2186/// Determine which bits of V are known to be either zero or one and return
2187/// them.
2189 unsigned Depth) {
2190 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2191 computeKnownBits(V, Known, Q, Depth);
2192 return Known;
2193}
2194
2195/// Determine which bits of V are known to be either zero or one and return
2196/// them in the Known bit set.
2197///
2198/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2199/// we cannot optimize based on the assumption that it is zero without changing
2200/// it to be an explicit zero. If we don't change it to zero, other code could
2201/// optimized based on the contradictory assumption that it is non-zero.
2202/// Because instcombine aggressively folds operations with undef args anyway,
2203/// this won't lose us code quality.
2204///
2205/// This function is defined on values with integer type, values with pointer
2206/// type, and vectors of integers. In the case
2207/// where V is a vector, known zero, and known one values are the
2208/// same width as the vector element, and the bit is set only if it is true
2209/// for all of the demanded elements in the vector specified by DemandedElts.
2210void computeKnownBits(const Value *V, const APInt &DemandedElts,
2211 KnownBits &Known, const SimplifyQuery &Q,
2212 unsigned Depth) {
2213 if (!DemandedElts) {
2214 // No demanded elts, better to assume we don't know anything.
2215 Known.resetAll();
2216 return;
2217 }
2218
2219 assert(V && "No Value?");
2220 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2221
2222#ifndef NDEBUG
2223 Type *Ty = V->getType();
2224 unsigned BitWidth = Known.getBitWidth();
2225
2227 "Not integer or pointer type!");
2228
2229 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2230 assert(
2231 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2232 "DemandedElt width should equal the fixed vector number of elements");
2233 } else {
2234 assert(DemandedElts == APInt(1, 1) &&
2235 "DemandedElt width should be 1 for scalars or scalable vectors");
2236 }
2237
2238 Type *ScalarTy = Ty->getScalarType();
2239 if (ScalarTy->isPointerTy()) {
2240 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2241 "V and Known should have same BitWidth");
2242 } else {
2243 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2244 "V and Known should have same BitWidth");
2245 }
2246#endif
2247
2248 const APInt *C;
2249 if (match(V, m_APInt(C))) {
2250 // We know all of the bits for a scalar constant or a splat vector constant!
2251 Known = KnownBits::makeConstant(*C);
2252 return;
2253 }
2254 // Null and aggregate-zero are all-zeros.
2255 if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
2256 Known.setAllZero();
2257 return;
2258 }
2259 // Handle a constant vector by taking the intersection of the known bits of
2260 // each element.
2261 if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) {
2262 assert(!isa<ScalableVectorType>(V->getType()));
2263 // We know that CDV must be a vector of integers. Take the intersection of
2264 // each element.
2265 Known.Zero.setAllBits(); Known.One.setAllBits();
2266 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2267 if (!DemandedElts[i])
2268 continue;
2269 APInt Elt = CDV->getElementAsAPInt(i);
2270 Known.Zero &= ~Elt;
2271 Known.One &= Elt;
2272 }
2273 if (Known.hasConflict())
2274 Known.resetAll();
2275 return;
2276 }
2277
2278 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2279 assert(!isa<ScalableVectorType>(V->getType()));
2280 // We know that CV must be a vector of integers. Take the intersection of
2281 // each element.
2282 Known.Zero.setAllBits(); Known.One.setAllBits();
2283 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2284 if (!DemandedElts[i])
2285 continue;
2286 Constant *Element = CV->getAggregateElement(i);
2287 if (isa<PoisonValue>(Element))
2288 continue;
2289 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2290 if (!ElementCI) {
2291 Known.resetAll();
2292 return;
2293 }
2294 const APInt &Elt = ElementCI->getValue();
2295 Known.Zero &= ~Elt;
2296 Known.One &= Elt;
2297 }
2298 if (Known.hasConflict())
2299 Known.resetAll();
2300 return;
2301 }
2302
2303 // Start out not knowing anything.
2304 Known.resetAll();
2305
2306 // We can't imply anything about undefs.
2307 if (isa<UndefValue>(V))
2308 return;
2309
2310 // There's no point in looking through other users of ConstantData for
2311 // assumptions. Confirm that we've handled them all.
2312 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2313
2314 if (const auto *A = dyn_cast<Argument>(V))
2315 if (std::optional<ConstantRange> Range = A->getRange())
2316 Known = Range->toKnownBits();
2317
2318 // All recursive calls that increase depth must come after this.
2320 return;
2321
2322 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2323 // the bits of its aliasee.
2324 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2325 if (!GA->isInterposable())
2326 computeKnownBits(GA->getAliasee(), Known, Q, Depth + 1);
2327 return;
2328 }
2329
2330 if (const Operator *I = dyn_cast<Operator>(V))
2331 computeKnownBitsFromOperator(I, DemandedElts, Known, Q, Depth);
2332 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2333 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2334 Known = CR->toKnownBits();
2335 }
2336
2337 // Aligned pointers have trailing zeros - refine Known.Zero set
2338 if (isa<PointerType>(V->getType())) {
2339 Align Alignment = V->getPointerAlignment(Q.DL);
2340 Known.Zero.setLowBits(Log2(Alignment));
2341 }
2342
2343 // computeKnownBitsFromContext strictly refines Known.
2344 // Therefore, we run them after computeKnownBitsFromOperator.
2345
2346 // Check whether we can determine known bits from context such as assumes.
2347 computeKnownBitsFromContext(V, Known, Q, Depth);
2348}
2349
2350/// Try to detect a recurrence that the value of the induction variable is
2351/// always a power of two (or zero).
2352static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2353 SimplifyQuery &Q, unsigned Depth) {
2354 BinaryOperator *BO = nullptr;
2355 Value *Start = nullptr, *Step = nullptr;
2356 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2357 return false;
2358
2359 // Initial value must be a power of two.
2360 for (const Use &U : PN->operands()) {
2361 if (U.get() == Start) {
2362 // Initial value comes from a different BB, need to adjust context
2363 // instruction for analysis.
2364 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2365 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Q, Depth))
2366 return false;
2367 }
2368 }
2369
2370 // Except for Mul, the induction variable must be on the left side of the
2371 // increment expression, otherwise its value can be arbitrary.
2372 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2373 return false;
2374
2375 Q.CxtI = BO->getParent()->getTerminator();
2376 switch (BO->getOpcode()) {
2377 case Instruction::Mul:
2378 // Power of two is closed under multiplication.
2379 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2380 Q.IIQ.hasNoSignedWrap(BO)) &&
2381 isKnownToBeAPowerOfTwo(Step, OrZero, Q, Depth);
2382 case Instruction::SDiv:
2383 // Start value must not be signmask for signed division, so simply being a
2384 // power of two is not sufficient, and it has to be a constant.
2385 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2386 return false;
2387 [[fallthrough]];
2388 case Instruction::UDiv:
2389 // Divisor must be a power of two.
2390 // If OrZero is false, cannot guarantee induction variable is non-zero after
2391 // division, same for Shr, unless it is exact division.
2392 return (OrZero || Q.IIQ.isExact(BO)) &&
2393 isKnownToBeAPowerOfTwo(Step, false, Q, Depth);
2394 case Instruction::Shl:
2395 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2396 case Instruction::AShr:
2397 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2398 return false;
2399 [[fallthrough]];
2400 case Instruction::LShr:
2401 return OrZero || Q.IIQ.isExact(BO);
2402 default:
2403 return false;
2404 }
2405}
2406
2407/// Return true if we can infer that \p V is known to be a power of 2 from
2408/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2409static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2410 const Value *Cond,
2411 bool CondIsTrue) {
2412 CmpPredicate Pred;
2413 const APInt *RHSC;
2414 if (!match(Cond, m_ICmp(Pred, m_Intrinsic<Intrinsic::ctpop>(m_Specific(V)),
2415 m_APInt(RHSC))))
2416 return false;
2417 if (!CondIsTrue)
2418 Pred = ICmpInst::getInversePredicate(Pred);
2419 // ctpop(V) u< 2
2420 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2421 return true;
2422 // ctpop(V) == 1
2423 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2424}
2425
2426/// Return true if the given value is known to have exactly one
2427/// bit set when defined. For vectors return true if every element is known to
2428/// be a power of two when defined. Supports values with integer or pointer
2429/// types and vectors of integers.
2430bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero,
2431 const SimplifyQuery &Q, unsigned Depth) {
2432 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2433
2434 if (isa<Constant>(V))
2435 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2436
2437 // i1 is by definition a power of 2 or zero.
2438 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2439 return true;
2440
2441 // Try to infer from assumptions.
2442 if (Q.AC && Q.CxtI) {
2443 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2444 if (!AssumeVH)
2445 continue;
2446 CallInst *I = cast<CallInst>(AssumeVH);
2447 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2448 /*CondIsTrue=*/true) &&
2450 return true;
2451 }
2452 }
2453
2454 // Handle dominating conditions.
2455 if (Q.DC && Q.CxtI && Q.DT) {
2456 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2457 Value *Cond = BI->getCondition();
2458
2459 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2461 /*CondIsTrue=*/true) &&
2462 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2463 return true;
2464
2465 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2467 /*CondIsTrue=*/false) &&
2468 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2469 return true;
2470 }
2471 }
2472
2473 auto *I = dyn_cast<Instruction>(V);
2474 if (!I)
2475 return false;
2476
2477 if (Q.CxtI && match(V, m_VScale())) {
2478 const Function *F = Q.CxtI->getFunction();
2479 // The vscale_range indicates vscale is a power-of-two.
2480 return F->hasFnAttribute(Attribute::VScaleRange);
2481 }
2482
2483 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2484 // it is shifted off the end then the result is undefined.
2485 if (match(I, m_Shl(m_One(), m_Value())))
2486 return true;
2487
2488 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2489 // the bottom. If it is shifted off the bottom then the result is undefined.
2490 if (match(I, m_LShr(m_SignMask(), m_Value())))
2491 return true;
2492
2493 // The remaining tests are all recursive, so bail out if we hit the limit.
2495 return false;
2496
2497 switch (I->getOpcode()) {
2498 case Instruction::ZExt:
2499 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2500 case Instruction::Trunc:
2501 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2502 case Instruction::Shl:
2503 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2504 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2505 return false;
2506 case Instruction::LShr:
2507 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2508 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2509 return false;
2510 case Instruction::UDiv:
2511 if (Q.IIQ.isExact(cast<BinaryOperator>(I)))
2512 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth);
2513 return false;
2514 case Instruction::Mul:
2515 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2516 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth) &&
2517 (OrZero || isKnownNonZero(I, Q, Depth));
2518 case Instruction::And:
2519 // A power of two and'd with anything is a power of two or zero.
2520 if (OrZero &&
2521 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Q, Depth) ||
2522 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Q, Depth)))
2523 return true;
2524 // X & (-X) is always a power of two or zero.
2525 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2526 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2527 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2528 return false;
2529 case Instruction::Add: {
2530 // Adding a power-of-two or zero to the same power-of-two or zero yields
2531 // either the original power-of-two, a larger power-of-two or zero.
2532 const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
2533 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2534 Q.IIQ.hasNoSignedWrap(VOBO)) {
2535 if (match(I->getOperand(0),
2536 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2537 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth))
2538 return true;
2539 if (match(I->getOperand(1),
2540 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2541 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Q, Depth))
2542 return true;
2543
2544 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2545 KnownBits LHSBits(BitWidth);
2546 computeKnownBits(I->getOperand(0), LHSBits, Q, Depth);
2547
2548 KnownBits RHSBits(BitWidth);
2549 computeKnownBits(I->getOperand(1), RHSBits, Q, Depth);
2550 // If i8 V is a power of two or zero:
2551 // ZeroBits: 1 1 1 0 1 1 1 1
2552 // ~ZeroBits: 0 0 0 1 0 0 0 0
2553 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2554 // If OrZero isn't set, we cannot give back a zero result.
2555 // Make sure either the LHS or RHS has a bit set.
2556 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2557 return true;
2558 }
2559
2560 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2561 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2562 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2563 return true;
2564 return false;
2565 }
2566 case Instruction::Select:
2567 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Q, Depth) &&
2568 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Q, Depth);
2569 case Instruction::PHI: {
2570 // A PHI node is power of two if all incoming values are power of two, or if
2571 // it is an induction variable where in each step its value is a power of
2572 // two.
2573 auto *PN = cast<PHINode>(I);
2575
2576 // Check if it is an induction variable and always power of two.
2577 if (isPowerOfTwoRecurrence(PN, OrZero, RecQ, Depth))
2578 return true;
2579
2580 // Recursively check all incoming values. Limit recursion to 2 levels, so
2581 // that search complexity is limited to number of operands^2.
2582 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2583 return llvm::all_of(PN->operands(), [&](const Use &U) {
2584 // Value is power of 2 if it is coming from PHI node itself by induction.
2585 if (U.get() == PN)
2586 return true;
2587
2588 // Change the context instruction to the incoming block where it is
2589 // evaluated.
2590 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2591 return isKnownToBeAPowerOfTwo(U.get(), OrZero, RecQ, NewDepth);
2592 });
2593 }
2594 case Instruction::Invoke:
2595 case Instruction::Call: {
2596 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2597 switch (II->getIntrinsicID()) {
2598 case Intrinsic::umax:
2599 case Intrinsic::smax:
2600 case Intrinsic::umin:
2601 case Intrinsic::smin:
2602 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Q, Depth) &&
2603 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2604 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2605 // thus dont change pow2/non-pow2 status.
2606 case Intrinsic::bitreverse:
2607 case Intrinsic::bswap:
2608 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2609 case Intrinsic::fshr:
2610 case Intrinsic::fshl:
2611 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2612 if (II->getArgOperand(0) == II->getArgOperand(1))
2613 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Q, Depth);
2614 break;
2615 default:
2616 break;
2617 }
2618 }
2619 return false;
2620 }
2621 default:
2622 return false;
2623 }
2624}
2625
2626/// Test whether a GEP's result is known to be non-null.
2627///
2628/// Uses properties inherent in a GEP to try to determine whether it is known
2629/// to be non-null.
2630///
2631/// Currently this routine does not support vector GEPs.
2632static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q,
2633 unsigned Depth) {
2634 const Function *F = nullptr;
2635 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2636 F = I->getFunction();
2637
2638 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2639 // may be null iff the base pointer is null and the offset is zero.
2640 if (!GEP->hasNoUnsignedWrap() &&
2641 !(GEP->isInBounds() &&
2642 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2643 return false;
2644
2645 // FIXME: Support vector-GEPs.
2646 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2647
2648 // If the base pointer is non-null, we cannot walk to a null address with an
2649 // inbounds GEP in address space zero.
2650 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2651 return true;
2652
2653 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2654 // If so, then the GEP cannot produce a null pointer, as doing so would
2655 // inherently violate the inbounds contract within address space zero.
2657 GTI != GTE; ++GTI) {
2658 // Struct types are easy -- they must always be indexed by a constant.
2659 if (StructType *STy = GTI.getStructTypeOrNull()) {
2660 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2661 unsigned ElementIdx = OpC->getZExtValue();
2662 const StructLayout *SL = Q.DL.getStructLayout(STy);
2663 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2664 if (ElementOffset > 0)
2665 return true;
2666 continue;
2667 }
2668
2669 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2670 if (GTI.getSequentialElementStride(Q.DL).isZero())
2671 continue;
2672
2673 // Fast path the constant operand case both for efficiency and so we don't
2674 // increment Depth when just zipping down an all-constant GEP.
2675 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2676 if (!OpC->isZero())
2677 return true;
2678 continue;
2679 }
2680
2681 // We post-increment Depth here because while isKnownNonZero increments it
2682 // as well, when we pop back up that increment won't persist. We don't want
2683 // to recurse 10k times just because we have 10k GEP operands. We don't
2684 // bail completely out because we want to handle constant GEPs regardless
2685 // of depth.
2687 continue;
2688
2689 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2690 return true;
2691 }
2692
2693 return false;
2694}
2695
2697 const Instruction *CtxI,
2698 const DominatorTree *DT) {
2699 assert(!isa<Constant>(V) && "Called for constant?");
2700
2701 if (!CtxI || !DT)
2702 return false;
2703
2704 unsigned NumUsesExplored = 0;
2705 for (auto &U : V->uses()) {
2706 // Avoid massive lists
2707 if (NumUsesExplored >= DomConditionsMaxUses)
2708 break;
2709 NumUsesExplored++;
2710
2711 const Instruction *UI = cast<Instruction>(U.getUser());
2712 // If the value is used as an argument to a call or invoke, then argument
2713 // attributes may provide an answer about null-ness.
2714 if (V->getType()->isPointerTy()) {
2715 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2716 if (CB->isArgOperand(&U) &&
2717 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2718 /*AllowUndefOrPoison=*/false) &&
2719 DT->dominates(CB, CtxI))
2720 return true;
2721 }
2722 }
2723
2724 // If the value is used as a load/store, then the pointer must be non null.
2725 if (V == getLoadStorePointerOperand(UI)) {
2727 V->getType()->getPointerAddressSpace()) &&
2728 DT->dominates(UI, CtxI))
2729 return true;
2730 }
2731
2732 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2733 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2734 isValidAssumeForContext(UI, CtxI, DT))
2735 return true;
2736
2737 // Consider only compare instructions uniquely controlling a branch
2738 Value *RHS;
2739 CmpPredicate Pred;
2740 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2741 continue;
2742
2743 bool NonNullIfTrue;
2744 if (cmpExcludesZero(Pred, RHS))
2745 NonNullIfTrue = true;
2747 NonNullIfTrue = false;
2748 else
2749 continue;
2750
2753 for (const auto *CmpU : UI->users()) {
2754 assert(WorkList.empty() && "Should be!");
2755 if (Visited.insert(CmpU).second)
2756 WorkList.push_back(CmpU);
2757
2758 while (!WorkList.empty()) {
2759 auto *Curr = WorkList.pop_back_val();
2760
2761 // If a user is an AND, add all its users to the work list. We only
2762 // propagate "pred != null" condition through AND because it is only
2763 // correct to assume that all conditions of AND are met in true branch.
2764 // TODO: Support similar logic of OR and EQ predicate?
2765 if (NonNullIfTrue)
2766 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2767 for (const auto *CurrU : Curr->users())
2768 if (Visited.insert(CurrU).second)
2769 WorkList.push_back(CurrU);
2770 continue;
2771 }
2772
2773 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2774 assert(BI->isConditional() && "uses a comparison!");
2775
2776 BasicBlock *NonNullSuccessor =
2777 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2778 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2779 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2780 return true;
2781 } else if (NonNullIfTrue && isGuard(Curr) &&
2782 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2783 return true;
2784 }
2785 }
2786 }
2787 }
2788
2789 return false;
2790}
2791
2792/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2793/// ensure that the value it's attached to is never Value? 'RangeType' is
2794/// is the type of the value described by the range.
2795static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2796 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2797 assert(NumRanges >= 1);
2798 for (unsigned i = 0; i < NumRanges; ++i) {
2800 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2802 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2803 ConstantRange Range(Lower->getValue(), Upper->getValue());
2804 if (Range.contains(Value))
2805 return false;
2806 }
2807 return true;
2808}
2809
2810/// Try to detect a recurrence that monotonically increases/decreases from a
2811/// non-zero starting value. These are common as induction variables.
2812static bool isNonZeroRecurrence(const PHINode *PN) {
2813 BinaryOperator *BO = nullptr;
2814 Value *Start = nullptr, *Step = nullptr;
2815 const APInt *StartC, *StepC;
2816 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2817 !match(Start, m_APInt(StartC)) || StartC->isZero())
2818 return false;
2819
2820 switch (BO->getOpcode()) {
2821 case Instruction::Add:
2822 // Starting from non-zero and stepping away from zero can never wrap back
2823 // to zero.
2824 return BO->hasNoUnsignedWrap() ||
2825 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
2826 StartC->isNegative() == StepC->isNegative());
2827 case Instruction::Mul:
2828 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2829 match(Step, m_APInt(StepC)) && !StepC->isZero();
2830 case Instruction::Shl:
2831 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
2832 case Instruction::AShr:
2833 case Instruction::LShr:
2834 return BO->isExact();
2835 default:
2836 return false;
2837 }
2838}
2839
2840static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
2841 return match(Op0, m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,
2842 m_Specific(Op1), m_Zero()))) ||
2843 match(Op1, m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,
2844 m_Specific(Op0), m_Zero())));
2845}
2846
2847static bool isNonZeroAdd(const APInt &DemandedElts, const SimplifyQuery &Q,
2848 unsigned BitWidth, Value *X, Value *Y, bool NSW,
2849 bool NUW, unsigned Depth) {
2850 // (X + (X != 0)) is non zero
2851 if (matchOpWithOpEqZero(X, Y))
2852 return true;
2853
2854 if (NUW)
2855 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2856 isKnownNonZero(X, DemandedElts, Q, Depth);
2857
2858 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
2859 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
2860
2861 // If X and Y are both non-negative (as signed values) then their sum is not
2862 // zero unless both X and Y are zero.
2863 if (XKnown.isNonNegative() && YKnown.isNonNegative())
2864 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2865 isKnownNonZero(X, DemandedElts, Q, Depth))
2866 return true;
2867
2868 // If X and Y are both negative (as signed values) then their sum is not
2869 // zero unless both X and Y equal INT_MIN.
2870 if (XKnown.isNegative() && YKnown.isNegative()) {
2872 // The sign bit of X is set. If some other bit is set then X is not equal
2873 // to INT_MIN.
2874 if (XKnown.One.intersects(Mask))
2875 return true;
2876 // The sign bit of Y is set. If some other bit is set then Y is not equal
2877 // to INT_MIN.
2878 if (YKnown.One.intersects(Mask))
2879 return true;
2880 }
2881
2882 // The sum of a non-negative number and a power of two is not zero.
2883 if (XKnown.isNonNegative() &&
2884 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Q, Depth))
2885 return true;
2886 if (YKnown.isNonNegative() &&
2887 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Q, Depth))
2888 return true;
2889
2890 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
2891}
2892
2893static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q,
2894 unsigned BitWidth, Value *X, Value *Y,
2895 unsigned Depth) {
2896 // (X - (X != 0)) is non zero
2897 // ((X != 0) - X) is non zero
2898 if (matchOpWithOpEqZero(X, Y))
2899 return true;
2900
2901 // TODO: Move this case into isKnownNonEqual().
2902 if (auto *C = dyn_cast<Constant>(X))
2903 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
2904 return true;
2905
2906 return ::isKnownNonEqual(X, Y, DemandedElts, Q, Depth);
2907}
2908
2909static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q,
2910 unsigned BitWidth, Value *X, Value *Y, bool NSW,
2911 bool NUW, unsigned Depth) {
2912 // If X and Y are non-zero then so is X * Y as long as the multiplication
2913 // does not overflow.
2914 if (NSW || NUW)
2915 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
2916 isKnownNonZero(Y, DemandedElts, Q, Depth);
2917
2918 // If either X or Y is odd, then if the other is non-zero the result can't
2919 // be zero.
2920 KnownBits XKnown = computeKnownBits(X, DemandedElts, Q, Depth);
2921 if (XKnown.One[0])
2922 return isKnownNonZero(Y, DemandedElts, Q, Depth);
2923
2924 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Q, Depth);
2925 if (YKnown.One[0])
2926 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
2927
2928 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
2929 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
2930 // the lowest known One of X and Y. If they are non-zero, the result
2931 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
2932 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
2933 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
2934 BitWidth;
2935}
2936
2937static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
2938 const SimplifyQuery &Q, const KnownBits &KnownVal,
2939 unsigned Depth) {
2940 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2941 switch (I->getOpcode()) {
2942 case Instruction::Shl:
2943 return Lhs.shl(Rhs);
2944 case Instruction::LShr:
2945 return Lhs.lshr(Rhs);
2946 case Instruction::AShr:
2947 return Lhs.ashr(Rhs);
2948 default:
2949 llvm_unreachable("Unknown Shift Opcode");
2950 }
2951 };
2952
2953 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2954 switch (I->getOpcode()) {
2955 case Instruction::Shl:
2956 return Lhs.lshr(Rhs);
2957 case Instruction::LShr:
2958 case Instruction::AShr:
2959 return Lhs.shl(Rhs);
2960 default:
2961 llvm_unreachable("Unknown Shift Opcode");
2962 }
2963 };
2964
2965 if (KnownVal.isUnknown())
2966 return false;
2967
2968 KnownBits KnownCnt =
2969 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
2970 APInt MaxShift = KnownCnt.getMaxValue();
2971 unsigned NumBits = KnownVal.getBitWidth();
2972 if (MaxShift.uge(NumBits))
2973 return false;
2974
2975 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
2976 return true;
2977
2978 // If all of the bits shifted out are known to be zero, and Val is known
2979 // non-zero then at least one non-zero bit must remain.
2980 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
2981 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
2982 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
2983 return true;
2984
2985 return false;
2986}
2987
2989 const APInt &DemandedElts,
2990 const SimplifyQuery &Q, unsigned Depth) {
2991 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
2992 switch (I->getOpcode()) {
2993 case Instruction::Alloca:
2994 // Alloca never returns null, malloc might.
2995 return I->getType()->getPointerAddressSpace() == 0;
2996 case Instruction::GetElementPtr:
2997 if (I->getType()->isPointerTy())
2998 return isGEPKnownNonNull(cast<GEPOperator>(I), Q, Depth);
2999 break;
3000 case Instruction::BitCast: {
3001 // We need to be a bit careful here. We can only peek through the bitcast
3002 // if the scalar size of elements in the operand are smaller than and a
3003 // multiple of the size they are casting too. Take three cases:
3004 //
3005 // 1) Unsafe:
3006 // bitcast <2 x i16> %NonZero to <4 x i8>
3007 //
3008 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
3009 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
3010 // guranteed (imagine just sign bit set in the 2 i16 elements).
3011 //
3012 // 2) Unsafe:
3013 // bitcast <4 x i3> %NonZero to <3 x i4>
3014 //
3015 // Even though the scalar size of the src (`i3`) is smaller than the
3016 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
3017 // its possible for the `3 x i4` elements to be zero because there are
3018 // some elements in the destination that don't contain any full src
3019 // element.
3020 //
3021 // 3) Safe:
3022 // bitcast <4 x i8> %NonZero to <2 x i16>
3023 //
3024 // This is always safe as non-zero in the 4 i8 elements implies
3025 // non-zero in the combination of any two adjacent ones. Since i8 is a
3026 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
3027 // This all implies the 2 i16 elements are non-zero.
3028 Type *FromTy = I->getOperand(0)->getType();
3029 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
3030 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
3031 return isKnownNonZero(I->getOperand(0), Q, Depth);
3032 } break;
3033 case Instruction::IntToPtr:
3034 // Note that we have to take special care to avoid looking through
3035 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
3036 // as casts that can alter the value, e.g., AddrSpaceCasts.
3037 if (!isa<ScalableVectorType>(I->getType()) &&
3038 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3039 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3040 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3041 break;
3042 case Instruction::PtrToInt:
3043 // Similar to int2ptr above, we can look through ptr2int here if the cast
3044 // is a no-op or an extend and not a truncate.
3045 if (!isa<ScalableVectorType>(I->getType()) &&
3046 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3047 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3048 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3049 break;
3050 case Instruction::Trunc:
3051 // nuw/nsw trunc preserves zero/non-zero status of input.
3052 if (auto *TI = dyn_cast<TruncInst>(I))
3053 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3054 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3055 break;
3056
3057 // Iff x - y != 0, then x ^ y != 0
3058 // Therefore we can do the same exact checks
3059 case Instruction::Xor:
3060 case Instruction::Sub:
3061 return isNonZeroSub(DemandedElts, Q, BitWidth, I->getOperand(0),
3062 I->getOperand(1), Depth);
3063 case Instruction::Or:
3064 // (X | (X != 0)) is non zero
3065 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3066 return true;
3067 // X | Y != 0 if X != Y.
3068 if (isKnownNonEqual(I->getOperand(0), I->getOperand(1), DemandedElts, Q,
3069 Depth))
3070 return true;
3071 // X | Y != 0 if X != 0 or Y != 0.
3072 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3073 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3074 case Instruction::SExt:
3075 case Instruction::ZExt:
3076 // ext X != 0 if X != 0.
3077 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3078
3079 case Instruction::Shl: {
3080 // shl nsw/nuw can't remove any non-zero bits.
3081 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
3082 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3083 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3084
3085 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3086 // if the lowest bit is shifted off the end.
3087 KnownBits Known(BitWidth);
3088 computeKnownBits(I->getOperand(0), DemandedElts, Known, Q, Depth);
3089 if (Known.One[0])
3090 return true;
3091
3092 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3093 }
3094 case Instruction::LShr:
3095 case Instruction::AShr: {
3096 // shr exact can only shift out zero bits.
3097 const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(I);
3098 if (BO->isExact())
3099 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3100
3101 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3102 // defined if the sign bit is shifted off the end.
3103 KnownBits Known =
3104 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3105 if (Known.isNegative())
3106 return true;
3107
3108 return isNonZeroShift(I, DemandedElts, Q, Known, Depth);
3109 }
3110 case Instruction::UDiv:
3111 case Instruction::SDiv: {
3112 // X / Y
3113 // div exact can only produce a zero if the dividend is zero.
3114 if (cast<PossiblyExactOperator>(I)->isExact())
3115 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3116
3117 KnownBits XKnown =
3118 computeKnownBits(I->getOperand(0), DemandedElts, Q, Depth);
3119 // If X is fully unknown we won't be able to figure anything out so don't
3120 // both computing knownbits for Y.
3121 if (XKnown.isUnknown())
3122 return false;
3123
3124 KnownBits YKnown =
3125 computeKnownBits(I->getOperand(1), DemandedElts, Q, Depth);
3126 if (I->getOpcode() == Instruction::SDiv) {
3127 // For signed division need to compare abs value of the operands.
3128 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3129 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3130 }
3131 // If X u>= Y then div is non zero (0/0 is UB).
3132 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3133 // If X is total unknown or X u< Y we won't be able to prove non-zero
3134 // with compute known bits so just return early.
3135 return XUgeY && *XUgeY;
3136 }
3137 case Instruction::Add: {
3138 // X + Y.
3139
3140 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3141 // non-zero.
3142 auto *BO = cast<OverflowingBinaryOperator>(I);
3143 return isNonZeroAdd(DemandedElts, Q, BitWidth, I->getOperand(0),
3144 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3145 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3146 }
3147 case Instruction::Mul: {
3148 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
3149 return isNonZeroMul(DemandedElts, Q, BitWidth, I->getOperand(0),
3150 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3151 Q.IIQ.hasNoUnsignedWrap(BO), Depth);
3152 }
3153 case Instruction::Select: {
3154 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3155
3156 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3157 // then see if the select condition implies the arm is non-zero. For example
3158 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3159 // dominated by `X != 0`.
3160 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3161 Value *Op;
3162 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3163 // Op is trivially non-zero.
3164 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3165 return true;
3166
3167 // The condition of the select dominates the true/false arm. Check if the
3168 // condition implies that a given arm is non-zero.
3169 Value *X;
3170 CmpPredicate Pred;
3171 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3172 return false;
3173
3174 if (!IsTrueArm)
3175 Pred = ICmpInst::getInversePredicate(Pred);
3176
3177 return cmpExcludesZero(Pred, X);
3178 };
3179
3180 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3181 SelectArmIsNonZero(/* IsTrueArm */ false))
3182 return true;
3183 break;
3184 }
3185 case Instruction::PHI: {
3186 auto *PN = cast<PHINode>(I);
3188 return true;
3189
3190 // Check if all incoming values are non-zero using recursion.
3192 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3193 return llvm::all_of(PN->operands(), [&](const Use &U) {
3194 if (U.get() == PN)
3195 return true;
3196 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3197 // Check if the branch on the phi excludes zero.
3198 CmpPredicate Pred;
3199 Value *X;
3200 BasicBlock *TrueSucc, *FalseSucc;
3201 if (match(RecQ.CxtI,
3202 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3203 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3204 // Check for cases of duplicate successors.
3205 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3206 // If we're using the false successor, invert the predicate.
3207 if (FalseSucc == PN->getParent())
3208 Pred = CmpInst::getInversePredicate(Pred);
3209 if (cmpExcludesZero(Pred, X))
3210 return true;
3211 }
3212 }
3213 // Finally recurse on the edge and check it directly.
3214 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3215 });
3216 }
3217 case Instruction::InsertElement: {
3218 if (isa<ScalableVectorType>(I->getType()))
3219 break;
3220
3221 const Value *Vec = I->getOperand(0);
3222 const Value *Elt = I->getOperand(1);
3223 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3224
3225 unsigned NumElts = DemandedElts.getBitWidth();
3226 APInt DemandedVecElts = DemandedElts;
3227 bool SkipElt = false;
3228 // If we know the index we are inserting too, clear it from Vec check.
3229 if (CIdx && CIdx->getValue().ult(NumElts)) {
3230 DemandedVecElts.clearBit(CIdx->getZExtValue());
3231 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3232 }
3233
3234 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3235 // are non-zero.
3236 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3237 (DemandedVecElts.isZero() ||
3238 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3239 }
3240 case Instruction::ExtractElement:
3241 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3242 const Value *Vec = EEI->getVectorOperand();
3243 const Value *Idx = EEI->getIndexOperand();
3244 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3245 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3246 unsigned NumElts = VecTy->getNumElements();
3247 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3248 if (CIdx && CIdx->getValue().ult(NumElts))
3249 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3250 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3251 }
3252 }
3253 break;
3254 case Instruction::ShuffleVector: {
3255 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3256 if (!Shuf)
3257 break;
3258 APInt DemandedLHS, DemandedRHS;
3259 // For undef elements, we don't know anything about the common state of
3260 // the shuffle result.
3261 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3262 break;
3263 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3264 return (DemandedRHS.isZero() ||
3265 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3266 (DemandedLHS.isZero() ||
3267 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3268 }
3269 case Instruction::Freeze:
3270 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3271 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3272 Depth);
3273 case Instruction::Load: {
3274 auto *LI = cast<LoadInst>(I);
3275 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3276 // is never null.
3277 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3278 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3279 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3280 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3281 return true;
3282 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3284 }
3285
3286 // No need to fall through to computeKnownBits as range metadata is already
3287 // handled in isKnownNonZero.
3288 return false;
3289 }
3290 case Instruction::ExtractValue: {
3291 const WithOverflowInst *WO;
3292 if (match(I, m_ExtractValue<0>(m_WithOverflowInst(WO)))) {
3293 switch (WO->getBinaryOp()) {
3294 default:
3295 break;
3296 case Instruction::Add:
3297 return isNonZeroAdd(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3298 WO->getArgOperand(1),
3299 /*NSW=*/false,
3300 /*NUW=*/false, Depth);
3301 case Instruction::Sub:
3302 return isNonZeroSub(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3303 WO->getArgOperand(1), Depth);
3304 case Instruction::Mul:
3305 return isNonZeroMul(DemandedElts, Q, BitWidth, WO->getArgOperand(0),
3306 WO->getArgOperand(1),
3307 /*NSW=*/false, /*NUW=*/false, Depth);
3308 break;
3309 }
3310 }
3311 break;
3312 }
3313 case Instruction::Call:
3314 case Instruction::Invoke: {
3315 const auto *Call = cast<CallBase>(I);
3316 if (I->getType()->isPointerTy()) {
3317 if (Call->isReturnNonNull())
3318 return true;
3319 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3320 return isKnownNonZero(RP, Q, Depth);
3321 } else {
3322 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3324 if (std::optional<ConstantRange> Range = Call->getRange()) {
3325 const APInt ZeroValue(Range->getBitWidth(), 0);
3326 if (!Range->contains(ZeroValue))
3327 return true;
3328 }
3329 if (const Value *RV = Call->getReturnedArgOperand())
3330 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3331 return true;
3332 }
3333
3334 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3335 switch (II->getIntrinsicID()) {
3336 case Intrinsic::sshl_sat:
3337 case Intrinsic::ushl_sat:
3338 case Intrinsic::abs:
3339 case Intrinsic::bitreverse:
3340 case Intrinsic::bswap:
3341 case Intrinsic::ctpop:
3342 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3343 // NB: We don't do usub_sat here as in any case we can prove its
3344 // non-zero, we will fold it to `sub nuw` in InstCombine.
3345 case Intrinsic::ssub_sat:
3346 return isNonZeroSub(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3347 II->getArgOperand(1), Depth);
3348 case Intrinsic::sadd_sat:
3349 return isNonZeroAdd(DemandedElts, Q, BitWidth, II->getArgOperand(0),
3350 II->getArgOperand(1),
3351 /*NSW=*/true, /* NUW=*/false, Depth);
3352 // Vec reverse preserves zero/non-zero status from input vec.
3353 case Intrinsic::vector_reverse:
3354 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3355 Q, Depth);
3356 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3357 case Intrinsic::vector_reduce_or:
3358 case Intrinsic::vector_reduce_umax:
3359 case Intrinsic::vector_reduce_umin:
3360 case Intrinsic::vector_reduce_smax:
3361 case Intrinsic::vector_reduce_smin:
3362 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3363 case Intrinsic::umax:
3364 case Intrinsic::uadd_sat:
3365 // umax(X, (X != 0)) is non zero
3366 // X +usat (X != 0) is non zero
3367 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3368 return true;
3369
3370 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3371 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3372 case Intrinsic::smax: {
3373 // If either arg is strictly positive the result is non-zero. Otherwise
3374 // the result is non-zero if both ops are non-zero.
3375 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3376 const KnownBits &OpKnown) {
3377 if (!OpNonZero.has_value())
3378 OpNonZero = OpKnown.isNonZero() ||
3379 isKnownNonZero(Op, DemandedElts, Q, Depth);
3380 return *OpNonZero;
3381 };
3382 // Avoid re-computing isKnownNonZero.
3383 std::optional<bool> Op0NonZero, Op1NonZero;
3384 KnownBits Op1Known =
3385 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3386 if (Op1Known.isNonNegative() &&
3387 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3388 return true;
3389 KnownBits Op0Known =
3390 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3391 if (Op0Known.isNonNegative() &&
3392 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3393 return true;
3394 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3395 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3396 }
3397 case Intrinsic::smin: {
3398 // If either arg is negative the result is non-zero. Otherwise
3399 // the result is non-zero if both ops are non-zero.
3400 KnownBits Op1Known =
3401 computeKnownBits(II->getArgOperand(1), DemandedElts, Q, Depth);
3402 if (Op1Known.isNegative())
3403 return true;
3404 KnownBits Op0Known =
3405 computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth);
3406 if (Op0Known.isNegative())
3407 return true;
3408
3409 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3410 return true;
3411 }
3412 [[fallthrough]];
3413 case Intrinsic::umin:
3414 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3415 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3416 case Intrinsic::cttz:
3417 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3418 .Zero[0];
3419 case Intrinsic::ctlz:
3420 return computeKnownBits(II->getArgOperand(0), DemandedElts, Q, Depth)
3421 .isNonNegative();
3422 case Intrinsic::fshr:
3423 case Intrinsic::fshl:
3424 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3425 if (II->getArgOperand(0) == II->getArgOperand(1))
3426 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3427 break;
3428 case Intrinsic::vscale:
3429 return true;
3430 case Intrinsic::experimental_get_vector_length:
3431 return isKnownNonZero(I->getOperand(0), Q, Depth);
3432 default:
3433 break;
3434 }
3435 break;
3436 }
3437
3438 return false;
3439 }
3440 }
3441
3442 KnownBits Known(BitWidth);
3443 computeKnownBits(I, DemandedElts, Known, Q, Depth);
3444 return Known.One != 0;
3445}
3446
3447/// Return true if the given value is known to be non-zero when defined. For
3448/// vectors, return true if every demanded element is known to be non-zero when
3449/// defined. For pointers, if the context instruction and dominator tree are
3450/// specified, perform context-sensitive analysis and return true if the
3451/// pointer couldn't possibly be null at the specified instruction.
3452/// Supports values with integer or pointer type and vectors of integers.
3453bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3454 const SimplifyQuery &Q, unsigned Depth) {
3455 Type *Ty = V->getType();
3456
3457#ifndef NDEBUG
3458 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3459
3460 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3461 assert(
3462 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3463 "DemandedElt width should equal the fixed vector number of elements");
3464 } else {
3465 assert(DemandedElts == APInt(1, 1) &&
3466 "DemandedElt width should be 1 for scalars");
3467 }
3468#endif
3469
3470 if (auto *C = dyn_cast<Constant>(V)) {
3471 if (C->isNullValue())
3472 return false;
3473 if (isa<ConstantInt>(C))
3474 // Must be non-zero due to null test above.
3475 return true;
3476
3477 // For constant vectors, check that all elements are poison or known
3478 // non-zero to determine that the whole vector is known non-zero.
3479 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3480 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3481 if (!DemandedElts[i])
3482 continue;
3483 Constant *Elt = C->getAggregateElement(i);
3484 if (!Elt || Elt->isNullValue())
3485 return false;
3486 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3487 return false;
3488 }
3489 return true;
3490 }
3491
3492 // Constant ptrauth can be null, iff the base pointer can be.
3493 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3494 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3495
3496 // A global variable in address space 0 is non null unless extern weak
3497 // or an absolute symbol reference. Other address spaces may have null as a
3498 // valid address for a global, so we can't assume anything.
3499 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3500 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3501 GV->getType()->getAddressSpace() == 0)
3502 return true;
3503 }
3504
3505 // For constant expressions, fall through to the Operator code below.
3506 if (!isa<ConstantExpr>(V))
3507 return false;
3508 }
3509
3510 if (const auto *A = dyn_cast<Argument>(V))
3511 if (std::optional<ConstantRange> Range = A->getRange()) {
3512 const APInt ZeroValue(Range->getBitWidth(), 0);
3513 if (!Range->contains(ZeroValue))
3514 return true;
3515 }
3516
3517 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3518 return true;
3519
3520 // Some of the tests below are recursive, so bail out if we hit the limit.
3522 return false;
3523
3524 // Check for pointer simplifications.
3525
3526 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3527 // A byval, inalloca may not be null in a non-default addres space. A
3528 // nonnull argument is assumed never 0.
3529 if (const Argument *A = dyn_cast<Argument>(V)) {
3530 if (((A->hasPassPointeeByValueCopyAttr() &&
3531 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3532 A->hasNonNullAttr()))
3533 return true;
3534 }
3535 }
3536
3537 if (const auto *I = dyn_cast<Operator>(V))
3538 if (isKnownNonZeroFromOperator(I, DemandedElts, Q, Depth))
3539 return true;
3540
3541 if (!isa<Constant>(V) &&
3543 return true;
3544
3545 if (const Value *Stripped = stripNullTest(V))
3546 return isKnownNonZero(Stripped, DemandedElts, Q, Depth);
3547
3548 return false;
3549}
3550
3552 unsigned Depth) {
3553 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3554 APInt DemandedElts =
3555 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3556 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3557}
3558
3559/// If the pair of operators are the same invertible function, return the
3560/// the operands of the function corresponding to each input. Otherwise,
3561/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3562/// every input value to exactly one output value. This is equivalent to
3563/// saying that Op1 and Op2 are equal exactly when the specified pair of
3564/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3565static std::optional<std::pair<Value*, Value*>>
3567 const Operator *Op2) {
3568 if (Op1->getOpcode() != Op2->getOpcode())
3569 return std::nullopt;
3570
3571 auto getOperands = [&](unsigned OpNum) -> auto {
3572 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3573 };
3574
3575 switch (Op1->getOpcode()) {
3576 default:
3577 break;
3578 case Instruction::Or:
3579 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3580 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3581 break;
3582 [[fallthrough]];
3583 case Instruction::Xor:
3584 case Instruction::Add: {
3585 Value *Other;
3586 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3587 return std::make_pair(Op1->getOperand(1), Other);
3588 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3589 return std::make_pair(Op1->getOperand(0), Other);
3590 break;
3591 }
3592 case Instruction::Sub:
3593 if (Op1->getOperand(0) == Op2->getOperand(0))
3594 return getOperands(1);
3595 if (Op1->getOperand(1) == Op2->getOperand(1))
3596 return getOperands(0);
3597 break;
3598 case Instruction::Mul: {
3599 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3600 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3601 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3602 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3603 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3604 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3605 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3606 break;
3607
3608 // Assume operand order has been canonicalized
3609 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3610 isa<ConstantInt>(Op1->getOperand(1)) &&
3611 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3612 return getOperands(0);
3613 break;
3614 }
3615 case Instruction::Shl: {
3616 // Same as multiplies, with the difference that we don't need to check
3617 // for a non-zero multiply. Shifts always multiply by non-zero.
3618 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3619 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3620 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3621 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3622 break;
3623
3624 if (Op1->getOperand(1) == Op2->getOperand(1))
3625 return getOperands(0);
3626 break;
3627 }
3628 case Instruction::AShr:
3629 case Instruction::LShr: {
3630 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3631 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3632 if (!PEO1->isExact() || !PEO2->isExact())
3633 break;
3634
3635 if (Op1->getOperand(1) == Op2->getOperand(1))
3636 return getOperands(0);
3637 break;
3638 }
3639 case Instruction::SExt:
3640 case Instruction::ZExt:
3641 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3642 return getOperands(0);
3643 break;
3644 case Instruction::PHI: {
3645 const PHINode *PN1 = cast<PHINode>(Op1);
3646 const PHINode *PN2 = cast<PHINode>(Op2);
3647
3648 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3649 // are a single invertible function of the start values? Note that repeated
3650 // application of an invertible function is also invertible
3651 BinaryOperator *BO1 = nullptr;
3652 Value *Start1 = nullptr, *Step1 = nullptr;
3653 BinaryOperator *BO2 = nullptr;
3654 Value *Start2 = nullptr, *Step2 = nullptr;
3655 if (PN1->getParent() != PN2->getParent() ||
3656 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3657 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3658 break;
3659
3660 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3661 cast<Operator>(BO2));
3662 if (!Values)
3663 break;
3664
3665 // We have to be careful of mutually defined recurrences here. Ex:
3666 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3667 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3668 // The invertibility of these is complicated, and not worth reasoning
3669 // about (yet?).
3670 if (Values->first != PN1 || Values->second != PN2)
3671 break;
3672
3673 return std::make_pair(Start1, Start2);
3674 }
3675 }
3676 return std::nullopt;
3677}
3678
3679/// Return true if V1 == (binop V2, X), where X is known non-zero.
3680/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3681/// implies V2 != V1.
3682static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3683 const APInt &DemandedElts,
3684 const SimplifyQuery &Q, unsigned Depth) {
3685 const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
3686 if (!BO)
3687 return false;
3688 switch (BO->getOpcode()) {
3689 default:
3690 break;
3691 case Instruction::Or:
3692 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3693 break;
3694 [[fallthrough]];
3695 case Instruction::Xor:
3696 case Instruction::Add:
3697 Value *Op = nullptr;
3698 if (V2 == BO->getOperand(0))
3699 Op = BO->getOperand(1);
3700 else if (V2 == BO->getOperand(1))
3701 Op = BO->getOperand(0);
3702 else
3703 return false;
3704 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3705 }
3706 return false;
3707}
3708
3709/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3710/// the multiplication is nuw or nsw.
3711static bool isNonEqualMul(const Value *V1, const Value *V2,
3712 const APInt &DemandedElts, const SimplifyQuery &Q,
3713 unsigned Depth) {
3714 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3715 const APInt *C;
3716 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3717 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3718 !C->isZero() && !C->isOne() &&
3719 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3720 }
3721 return false;
3722}
3723
3724/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3725/// the shift is nuw or nsw.
3726static bool isNonEqualShl(const Value *V1, const Value *V2,
3727 const APInt &DemandedElts, const SimplifyQuery &Q,
3728 unsigned Depth) {
3729 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3730 const APInt *C;
3731 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3732 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3733 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3734 }
3735 return false;
3736}
3737
3738static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3739 const APInt &DemandedElts, const SimplifyQuery &Q,
3740 unsigned Depth) {
3741 // Check two PHIs are in same block.
3742 if (PN1->getParent() != PN2->getParent())
3743 return false;
3744
3746 bool UsedFullRecursion = false;
3747 for (const BasicBlock *IncomBB : PN1->blocks()) {
3748 if (!VisitedBBs.insert(IncomBB).second)
3749 continue; // Don't reprocess blocks that we have dealt with already.
3750 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3751 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3752 const APInt *C1, *C2;
3753 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3754 continue;
3755
3756 // Only one pair of phi operands is allowed for full recursion.
3757 if (UsedFullRecursion)
3758 return false;
3759
3761 RecQ.CxtI = IncomBB->getTerminator();
3762 if (!isKnownNonEqual(IV1, IV2, DemandedElts, RecQ, Depth + 1))
3763 return false;
3764 UsedFullRecursion = true;
3765 }
3766 return true;
3767}
3768
3769static bool isNonEqualSelect(const Value *V1, const Value *V2,
3770 const APInt &DemandedElts, const SimplifyQuery &Q,
3771 unsigned Depth) {
3772 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3773 if (!SI1)
3774 return false;
3775
3776 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3777 const Value *Cond1 = SI1->getCondition();
3778 const Value *Cond2 = SI2->getCondition();
3779 if (Cond1 == Cond2)
3780 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3781 DemandedElts, Q, Depth + 1) &&
3782 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3783 DemandedElts, Q, Depth + 1);
3784 }
3785 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Q, Depth + 1) &&
3786 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Q, Depth + 1);
3787}
3788
3789// Check to see if A is both a GEP and is the incoming value for a PHI in the
3790// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3791// one of them being the recursive GEP A and the other a ptr at same base and at
3792// the same/higher offset than B we are only incrementing the pointer further in
3793// loop if offset of recursive GEP is greater than 0.
3795 const SimplifyQuery &Q) {
3796 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3797 return false;
3798
3799 auto *GEPA = dyn_cast<GEPOperator>(A);
3800 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3801 return false;
3802
3803 // Handle 2 incoming PHI values with one being a recursive GEP.
3804 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3805 if (!PN || PN->getNumIncomingValues() != 2)
3806 return false;
3807
3808 // Search for the recursive GEP as an incoming operand, and record that as
3809 // Step.
3810 Value *Start = nullptr;
3811 Value *Step = const_cast<Value *>(A);
3812 if (PN->getIncomingValue(0) == Step)
3813 Start = PN->getIncomingValue(1);
3814 else if (PN->getIncomingValue(1) == Step)
3815 Start = PN->getIncomingValue(0);
3816 else
3817 return false;
3818
3819 // Other incoming node base should match the B base.
3820 // StartOffset >= OffsetB && StepOffset > 0?
3821 // StartOffset <= OffsetB && StepOffset < 0?
3822 // Is non-equal if above are true.
3823 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3824 // optimisation to inbounds GEPs only.
3825 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3826 APInt StartOffset(IndexWidth, 0);
3827 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3828 APInt StepOffset(IndexWidth, 0);
3829 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3830
3831 // Check if Base Pointer of Step matches the PHI.
3832 if (Step != PN)
3833 return false;
3834 APInt OffsetB(IndexWidth, 0);
3835 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3836 return Start == B &&
3837 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
3838 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
3839}
3840
3841static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2,
3842 const SimplifyQuery &Q, unsigned Depth) {
3843 if (!Q.CxtI)
3844 return false;
3845
3846 // Try to infer NonEqual based on information from dominating conditions.
3847 if (Q.DC && Q.DT) {
3848 auto IsKnownNonEqualFromDominatingCondition = [&](const Value *V) {
3849 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
3850 Value *Cond = BI->getCondition();
3851 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
3852 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
3853 isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL,
3854 /*LHSIsTrue=*/true, Depth)
3855 .value_or(false))
3856 return true;
3857
3858 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
3859 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
3860 isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL,
3861 /*LHSIsTrue=*/false, Depth)
3862 .value_or(false))
3863 return true;
3864 }
3865
3866 return false;
3867 };
3868
3869 if (IsKnownNonEqualFromDominatingCondition(V1) ||
3870 IsKnownNonEqualFromDominatingCondition(V2))
3871 return true;
3872 }
3873
3874 if (!Q.AC)
3875 return false;
3876
3877 // Try to infer NonEqual based on information from assumptions.
3878 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
3879 if (!AssumeVH)
3880 continue;
3881 CallInst *I = cast<CallInst>(AssumeVH);
3882
3883 assert(I->getFunction() == Q.CxtI->getFunction() &&
3884 "Got assumption for the wrong function!");
3885 assert(I->getIntrinsicID() == Intrinsic::assume &&
3886 "must be an assume intrinsic");
3887
3888 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
3889 /*LHSIsTrue=*/true, Depth)
3890 .value_or(false) &&
3892 return true;
3893 }
3894
3895 return false;
3896}
3897
3898/// Return true if it is known that V1 != V2.
3899static bool isKnownNonEqual(const Value *V1, const Value *V2,
3900 const APInt &DemandedElts, const SimplifyQuery &Q,
3901 unsigned Depth) {
3902 if (V1 == V2)
3903 return false;
3904 if (V1->getType() != V2->getType())
3905 // We can't look through casts yet.
3906 return false;
3907
3909 return false;
3910
3911 // See if we can recurse through (exactly one of) our operands. This
3912 // requires our operation be 1-to-1 and map every input value to exactly
3913 // one output value. Such an operation is invertible.
3914 auto *O1 = dyn_cast<Operator>(V1);
3915 auto *O2 = dyn_cast<Operator>(V2);
3916 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3917 if (auto Values = getInvertibleOperands(O1, O2))
3918 return isKnownNonEqual(Values->first, Values->second, DemandedElts, Q,
3919 Depth + 1);
3920
3921 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3922 const PHINode *PN2 = cast<PHINode>(V2);
3923 // FIXME: This is missing a generalization to handle the case where one is
3924 // a PHI and another one isn't.
3925 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Q, Depth))
3926 return true;
3927 };
3928 }
3929
3930 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Q, Depth) ||
3931 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Q, Depth))
3932 return true;
3933
3934 if (isNonEqualMul(V1, V2, DemandedElts, Q, Depth) ||
3935 isNonEqualMul(V2, V1, DemandedElts, Q, Depth))
3936 return true;
3937
3938 if (isNonEqualShl(V1, V2, DemandedElts, Q, Depth) ||
3939 isNonEqualShl(V2, V1, DemandedElts, Q, Depth))
3940 return true;
3941
3942 if (V1->getType()->isIntOrIntVectorTy()) {
3943 // Are any known bits in V1 contradictory to known bits in V2? If V1
3944 // has a known zero where V2 has a known one, they must not be equal.
3945 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Q, Depth);
3946 if (!Known1.isUnknown()) {
3947 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Q, Depth);
3948 if (Known1.Zero.intersects(Known2.One) ||
3949 Known2.Zero.intersects(Known1.One))
3950 return true;
3951 }
3952 }
3953
3954 if (isNonEqualSelect(V1, V2, DemandedElts, Q, Depth) ||
3955 isNonEqualSelect(V2, V1, DemandedElts, Q, Depth))
3956 return true;
3957
3958 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3960 return true;
3961
3962 Value *A, *B;
3963 // PtrToInts are NonEqual if their Ptrs are NonEqual.
3964 // Check PtrToInt type matches the pointer size.
3965 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
3967 return isKnownNonEqual(A, B, DemandedElts, Q, Depth + 1);
3968
3969 if (isKnownNonEqualFromContext(V1, V2, Q, Depth))
3970 return true;
3971
3972 return false;
3973}
3974
3975/// For vector constants, loop over the elements and find the constant with the
3976/// minimum number of sign bits. Return 0 if the value is not a vector constant
3977/// or if any element was not analyzed; otherwise, return the count for the
3978/// element with the minimum number of sign bits.
3980 const APInt &DemandedElts,
3981 unsigned TyBits) {
3982 const auto *CV = dyn_cast<Constant>(V);
3983 if (!CV || !isa<FixedVectorType>(CV->getType()))
3984 return 0;
3985
3986 unsigned MinSignBits = TyBits;
3987 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3988 for (unsigned i = 0; i != NumElts; ++i) {
3989 if (!DemandedElts[i])
3990 continue;
3991 // If we find a non-ConstantInt, bail out.
3992 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3993 if (!Elt)
3994 return 0;
3995
3996 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3997 }
3998
3999 return MinSignBits;
4000}
4001
4002static unsigned ComputeNumSignBitsImpl(const Value *V,
4003 const APInt &DemandedElts,
4004 const SimplifyQuery &Q, unsigned Depth);
4005
4006static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
4007 const SimplifyQuery &Q, unsigned Depth) {
4008 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Q, Depth);
4009 assert(Result > 0 && "At least one sign bit needs to be present!");
4010 return Result;
4011}
4012
4013/// Return the number of times the sign bit of the register is replicated into
4014/// the other bits. We know that at least 1 bit is always equal to the sign bit
4015/// (itself), but other cases can give us information. For example, immediately
4016/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
4017/// other, so we return 3. For vectors, return the number of sign bits for the
4018/// vector element with the minimum number of known sign bits of the demanded
4019/// elements in the vector specified by DemandedElts.
4020static unsigned ComputeNumSignBitsImpl(const Value *V,
4021 const APInt &DemandedElts,
4022 const SimplifyQuery &Q, unsigned Depth) {
4023 Type *Ty = V->getType();
4024#ifndef NDEBUG
4025 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4026
4027 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
4028 assert(
4029 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
4030 "DemandedElt width should equal the fixed vector number of elements");
4031 } else {
4032 assert(DemandedElts == APInt(1, 1) &&
4033 "DemandedElt width should be 1 for scalars");
4034 }
4035#endif
4036
4037 // We return the minimum number of sign bits that are guaranteed to be present
4038 // in V, so for undef we have to conservatively return 1. We don't have the
4039 // same behavior for poison though -- that's a FIXME today.
4040
4041 Type *ScalarTy = Ty->getScalarType();
4042 unsigned TyBits = ScalarTy->isPointerTy() ?
4043 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
4044 Q.DL.getTypeSizeInBits(ScalarTy);
4045
4046 unsigned Tmp, Tmp2;
4047 unsigned FirstAnswer = 1;
4048
4049 // Note that ConstantInt is handled by the general computeKnownBits case
4050 // below.
4051
4053 return 1;
4054
4055 if (auto *U = dyn_cast<Operator>(V)) {
4056 switch (Operator::getOpcode(V)) {
4057 default: break;
4058 case Instruction::BitCast: {
4059 Value *Src = U->getOperand(0);
4060 Type *SrcTy = Src->getType();
4061
4062 // Skip if the source type is not an integer or integer vector type
4063 // This ensures we only process integer-like types
4064 if (!SrcTy->isIntOrIntVectorTy())
4065 break;
4066
4067 unsigned SrcBits = SrcTy->getScalarSizeInBits();
4068
4069 // Bitcast 'large element' scalar/vector to 'small element' vector.
4070 if ((SrcBits % TyBits) != 0)
4071 break;
4072
4073 // Only proceed if the destination type is a fixed-size vector
4074 if (isa<FixedVectorType>(Ty)) {
4075 // Fast case - sign splat can be simply split across the small elements.
4076 // This works for both vector and scalar sources
4077 Tmp = ComputeNumSignBits(Src, Q, Depth + 1);
4078 if (Tmp == SrcBits)
4079 return TyBits;
4080 }
4081 break;
4082 }
4083 case Instruction::SExt:
4084 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
4085 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1) +
4086 Tmp;
4087
4088 case Instruction::SDiv: {
4089 const APInt *Denominator;
4090 // sdiv X, C -> adds log(C) sign bits.
4091 if (match(U->getOperand(1), m_APInt(Denominator))) {
4092
4093 // Ignore non-positive denominator.
4094 if (!Denominator->isStrictlyPositive())
4095 break;
4096
4097 // Calculate the incoming numerator bits.
4098 unsigned NumBits =
4099 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4100
4101 // Add floor(log(C)) bits to the numerator bits.
4102 return std::min(TyBits, NumBits + Denominator->logBase2());
4103 }
4104 break;
4105 }
4106
4107 case Instruction::SRem: {
4108 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4109
4110 const APInt *Denominator;
4111 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4112 // positive constant. This let us put a lower bound on the number of sign
4113 // bits.
4114 if (match(U->getOperand(1), m_APInt(Denominator))) {
4115
4116 // Ignore non-positive denominator.
4117 if (Denominator->isStrictlyPositive()) {
4118 // Calculate the leading sign bit constraints by examining the
4119 // denominator. Given that the denominator is positive, there are two
4120 // cases:
4121 //
4122 // 1. The numerator is positive. The result range is [0,C) and
4123 // [0,C) u< (1 << ceilLogBase2(C)).
4124 //
4125 // 2. The numerator is negative. Then the result range is (-C,0] and
4126 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4127 //
4128 // Thus a lower bound on the number of sign bits is `TyBits -
4129 // ceilLogBase2(C)`.
4130
4131 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4132 Tmp = std::max(Tmp, ResBits);
4133 }
4134 }
4135 return Tmp;
4136 }
4137
4138 case Instruction::AShr: {
4139 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4140 // ashr X, C -> adds C sign bits. Vectors too.
4141 const APInt *ShAmt;
4142 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4143 if (ShAmt->uge(TyBits))
4144 break; // Bad shift.
4145 unsigned ShAmtLimited = ShAmt->getZExtValue();
4146 Tmp += ShAmtLimited;
4147 if (Tmp > TyBits) Tmp = TyBits;
4148 }
4149 return Tmp;
4150 }
4151 case Instruction::Shl: {
4152 const APInt *ShAmt;
4153 Value *X = nullptr;
4154 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4155 // shl destroys sign bits.
4156 if (ShAmt->uge(TyBits))
4157 break; // Bad shift.
4158 // We can look through a zext (more or less treating it as a sext) if
4159 // all extended bits are shifted out.
4160 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4161 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4162 Tmp = ComputeNumSignBits(X, DemandedElts, Q, Depth + 1);
4163 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4164 } else
4165 Tmp =
4166 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4167 if (ShAmt->uge(Tmp))
4168 break; // Shifted all sign bits out.
4169 Tmp2 = ShAmt->getZExtValue();
4170 return Tmp - Tmp2;
4171 }
4172 break;
4173 }
4174 case Instruction::And:
4175 case Instruction::Or:
4176 case Instruction::Xor: // NOT is handled here.
4177 // Logical binary ops preserve the number of sign bits at the worst.
4178 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4179 if (Tmp != 1) {
4180 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4181 FirstAnswer = std::min(Tmp, Tmp2);
4182 // We computed what we know about the sign bits as our first
4183 // answer. Now proceed to the generic code that uses
4184 // computeKnownBits, and pick whichever answer is better.
4185 }
4186 break;
4187
4188 case Instruction::Select: {
4189 // If we have a clamp pattern, we know that the number of sign bits will
4190 // be the minimum of the clamp min/max range.
4191 const Value *X;
4192 const APInt *CLow, *CHigh;
4193 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4194 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4195
4196 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4197 if (Tmp == 1)
4198 break;
4199 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Q, Depth + 1);
4200 return std::min(Tmp, Tmp2);
4201 }
4202
4203 case Instruction::Add:
4204 // Add can have at most one carry bit. Thus we know that the output
4205 // is, at worst, one more bit than the inputs.
4206 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4207 if (Tmp == 1) break;
4208
4209 // Special case decrementing a value (ADD X, -1):
4210 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4211 if (CRHS->isAllOnesValue()) {
4212 KnownBits Known(TyBits);
4213 computeKnownBits(U->getOperand(0), DemandedElts, Known, Q, Depth + 1);
4214
4215 // If the input is known to be 0 or 1, the output is 0/-1, which is
4216 // all sign bits set.
4217 if ((Known.Zero | 1).isAllOnes())
4218 return TyBits;
4219
4220 // If we are subtracting one from a positive number, there is no carry
4221 // out of the result.
4222 if (Known.isNonNegative())
4223 return Tmp;
4224 }
4225
4226 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4227 if (Tmp2 == 1)
4228 break;
4229 return std::min(Tmp, Tmp2) - 1;
4230
4231 case Instruction::Sub:
4232 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4233 if (Tmp2 == 1)
4234 break;
4235
4236 // Handle NEG.
4237 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4238 if (CLHS->isNullValue()) {
4239 KnownBits Known(TyBits);
4240 computeKnownBits(U->getOperand(1), DemandedElts, Known, Q, Depth + 1);
4241 // If the input is known to be 0 or 1, the output is 0/-1, which is
4242 // all sign bits set.
4243 if ((Known.Zero | 1).isAllOnes())
4244 return TyBits;
4245
4246 // If the input is known to be positive (the sign bit is known clear),
4247 // the output of the NEG has the same number of sign bits as the
4248 // input.
4249 if (Known.isNonNegative())
4250 return Tmp2;
4251
4252 // Otherwise, we treat this like a SUB.
4253 }
4254
4255 // Sub can have at most one carry bit. Thus we know that the output
4256 // is, at worst, one more bit than the inputs.
4257 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4258 if (Tmp == 1)
4259 break;
4260 return std::min(Tmp, Tmp2) - 1;
4261
4262 case Instruction::Mul: {
4263 // The output of the Mul can be at most twice the valid bits in the
4264 // inputs.
4265 unsigned SignBitsOp0 =
4266 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4267 if (SignBitsOp0 == 1)
4268 break;
4269 unsigned SignBitsOp1 =
4270 ComputeNumSignBits(U->getOperand(1), DemandedElts, Q, Depth + 1);
4271 if (SignBitsOp1 == 1)
4272 break;
4273 unsigned OutValidBits =
4274 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4275 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4276 }
4277
4278 case Instruction::PHI: {
4279 const PHINode *PN = cast<PHINode>(U);
4280 unsigned NumIncomingValues = PN->getNumIncomingValues();
4281 // Don't analyze large in-degree PHIs.
4282 if (NumIncomingValues > 4) break;
4283 // Unreachable blocks may have zero-operand PHI nodes.
4284 if (NumIncomingValues == 0) break;
4285
4286 // Take the minimum of all incoming values. This can't infinitely loop
4287 // because of our depth threshold.
4289 Tmp = TyBits;
4290 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4291 if (Tmp == 1) return Tmp;
4292 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4293 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4294 DemandedElts, RecQ, Depth + 1));
4295 }
4296 return Tmp;
4297 }
4298
4299 case Instruction::Trunc: {
4300 // If the input contained enough sign bits that some remain after the
4301 // truncation, then we can make use of that. Otherwise we don't know
4302 // anything.
4303 Tmp = ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4304 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4305 if (Tmp > (OperandTyBits - TyBits))
4306 return Tmp - (OperandTyBits - TyBits);
4307
4308 return 1;
4309 }
4310
4311 case Instruction::ExtractElement:
4312 // Look through extract element. At the moment we keep this simple and
4313 // skip tracking the specific element. But at least we might find
4314 // information valid for all elements of the vector (for example if vector
4315 // is sign extended, shifted, etc).
4316 return ComputeNumSignBits(U->getOperand(0), Q, Depth + 1);
4317
4318 case Instruction::ShuffleVector: {
4319 // Collect the minimum number of sign bits that are shared by every vector
4320 // element referenced by the shuffle.
4321 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4322 if (!Shuf) {
4323 // FIXME: Add support for shufflevector constant expressions.
4324 return 1;
4325 }
4326 APInt DemandedLHS, DemandedRHS;
4327 // For undef elements, we don't know anything about the common state of
4328 // the shuffle result.
4329 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4330 return 1;
4331 Tmp = std::numeric_limits<unsigned>::max();
4332 if (!!DemandedLHS) {
4333 const Value *LHS = Shuf->getOperand(0);
4334 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Q, Depth + 1);
4335 }
4336 // If we don't know anything, early out and try computeKnownBits
4337 // fall-back.
4338 if (Tmp == 1)
4339 break;
4340 if (!!DemandedRHS) {
4341 const Value *RHS = Shuf->getOperand(1);
4342 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Q, Depth + 1);
4343 Tmp = std::min(Tmp, Tmp2);
4344 }
4345 // If we don't know anything, early out and try computeKnownBits
4346 // fall-back.
4347 if (Tmp == 1)
4348 break;
4349 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4350 return Tmp;
4351 }
4352 case Instruction::Call: {
4353 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4354 switch (II->getIntrinsicID()) {
4355 default:
4356 break;
4357 case Intrinsic::abs:
4358 Tmp =
4359 ComputeNumSignBits(U->getOperand(0), DemandedElts, Q, Depth + 1);
4360 if (Tmp == 1)
4361 break;
4362
4363 // Absolute value reduces number of sign bits by at most 1.
4364 return Tmp - 1;
4365 case Intrinsic::smin:
4366 case Intrinsic::smax: {
4367 const APInt *CLow, *CHigh;
4368 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4369 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4370 }
4371 }
4372 }
4373 }
4374 }
4375 }
4376
4377 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4378 // use this information.
4379
4380 // If we can examine all elements of a vector constant successfully, we're
4381 // done (we can't do any better than that). If not, keep trying.
4382 if (unsigned VecSignBits =
4383 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4384 return VecSignBits;
4385
4386 KnownBits Known(TyBits);
4387 computeKnownBits(V, DemandedElts, Known, Q, Depth);
4388
4389 // If we know that the sign bit is either zero or one, determine the number of
4390 // identical bits in the top of the input value.
4391 return std::max(FirstAnswer, Known.countMinSignBits());
4392}
4393
4395 const TargetLibraryInfo *TLI) {
4396 const Function *F = CB.getCalledFunction();
4397 if (!F)
4399
4400 if (F->isIntrinsic())
4401 return F->getIntrinsicID();
4402
4403 // We are going to infer semantics of a library function based on mapping it
4404 // to an LLVM intrinsic. Check that the library function is available from
4405 // this callbase and in this environment.
4406 LibFunc Func;
4407 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4408 !CB.onlyReadsMemory())
4410
4411 switch (Func) {
4412 default:
4413 break;
4414 case LibFunc_sin:
4415 case LibFunc_sinf:
4416 case LibFunc_sinl:
4417 return Intrinsic::sin;
4418 case LibFunc_cos:
4419 case LibFunc_cosf:
4420 case LibFunc_cosl:
4421 return Intrinsic::cos;
4422 case LibFunc_tan:
4423 case LibFunc_tanf:
4424 case LibFunc_tanl:
4425 return Intrinsic::tan;
4426 case LibFunc_asin:
4427 case LibFunc_asinf:
4428 case LibFunc_asinl:
4429 return Intrinsic::asin;
4430 case LibFunc_acos:
4431 case LibFunc_acosf:
4432 case LibFunc_acosl:
4433 return Intrinsic::acos;
4434 case LibFunc_atan:
4435 case LibFunc_atanf:
4436 case LibFunc_atanl:
4437 return Intrinsic::atan;
4438 case LibFunc_atan2:
4439 case LibFunc_atan2f:
4440 case LibFunc_atan2l:
4441 return Intrinsic::atan2;
4442 case LibFunc_sinh:
4443 case LibFunc_sinhf:
4444 case LibFunc_sinhl:
4445 return Intrinsic::sinh;
4446 case LibFunc_cosh:
4447 case LibFunc_coshf:
4448 case LibFunc_coshl:
4449 return Intrinsic::cosh;
4450 case LibFunc_tanh:
4451 case LibFunc_tanhf:
4452 case LibFunc_tanhl:
4453 return Intrinsic::tanh;
4454 case LibFunc_exp:
4455 case LibFunc_expf:
4456 case LibFunc_expl:
4457 return Intrinsic::exp;
4458 case LibFunc_exp2:
4459 case LibFunc_exp2f:
4460 case LibFunc_exp2l:
4461 return Intrinsic::exp2;
4462 case LibFunc_exp10:
4463 case LibFunc_exp10f:
4464 case LibFunc_exp10l:
4465 return Intrinsic::exp10;
4466 case LibFunc_log:
4467 case LibFunc_logf:
4468 case LibFunc_logl:
4469 return Intrinsic::log;
4470 case LibFunc_log10:
4471 case LibFunc_log10f:
4472 case LibFunc_log10l:
4473 return Intrinsic::log10;
4474 case LibFunc_log2:
4475 case LibFunc_log2f:
4476 case LibFunc_log2l:
4477 return Intrinsic::log2;
4478 case LibFunc_fabs:
4479 case LibFunc_fabsf:
4480 case LibFunc_fabsl:
4481 return Intrinsic::fabs;
4482 case LibFunc_fmin:
4483 case LibFunc_fminf:
4484 case LibFunc_fminl:
4485 return Intrinsic::minnum;
4486 case LibFunc_fmax:
4487 case LibFunc_fmaxf:
4488 case LibFunc_fmaxl:
4489 return Intrinsic::maxnum;
4490 case LibFunc_copysign:
4491 case LibFunc_copysignf:
4492 case LibFunc_copysignl:
4493 return Intrinsic::copysign;
4494 case LibFunc_floor:
4495 case LibFunc_floorf:
4496 case LibFunc_floorl:
4497 return Intrinsic::floor;
4498 case LibFunc_ceil:
4499 case LibFunc_ceilf:
4500 case LibFunc_ceill:
4501 return Intrinsic::ceil;
4502 case LibFunc_trunc:
4503 case LibFunc_truncf:
4504 case LibFunc_truncl:
4505 return Intrinsic::trunc;
4506 case LibFunc_rint:
4507 case LibFunc_rintf:
4508 case LibFunc_rintl:
4509 return Intrinsic::rint;
4510 case LibFunc_nearbyint:
4511 case LibFunc_nearbyintf:
4512 case LibFunc_nearbyintl:
4513 return Intrinsic::nearbyint;
4514 case LibFunc_round:
4515 case LibFunc_roundf:
4516 case LibFunc_roundl:
4517 return Intrinsic::round;
4518 case LibFunc_roundeven:
4519 case LibFunc_roundevenf:
4520 case LibFunc_roundevenl:
4521 return Intrinsic::roundeven;
4522 case LibFunc_pow:
4523 case LibFunc_powf:
4524 case LibFunc_powl:
4525 return Intrinsic::pow;
4526 case LibFunc_sqrt:
4527 case LibFunc_sqrtf:
4528 case LibFunc_sqrtl:
4529 return Intrinsic::sqrt;
4530 }
4531
4533}
4534
4535static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4536 Ty = Ty->getScalarType();
4537 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4538 return Mode.Output == DenormalMode::IEEE ||
4539 Mode.Output == DenormalMode::PositiveZero;
4540}
4541/// Given an exploded icmp instruction, return true if the comparison only
4542/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4543/// the result of the comparison is true when the input value is signed.
4545 bool &TrueIfSigned) {
4546 switch (Pred) {
4547 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4548 TrueIfSigned = true;
4549 return RHS.isZero();
4550 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4551 TrueIfSigned = true;
4552 return RHS.isAllOnes();
4553 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4554 TrueIfSigned = false;
4555 return RHS.isAllOnes();
4556 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4557 TrueIfSigned = false;
4558 return RHS.isZero();
4559 case ICmpInst::ICMP_UGT:
4560 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4561 TrueIfSigned = true;
4562 return RHS.isMaxSignedValue();
4563 case ICmpInst::ICMP_UGE:
4564 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4565 TrueIfSigned = true;
4566 return RHS.isMinSignedValue();
4567 case ICmpInst::ICMP_ULT:
4568 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4569 TrueIfSigned = false;
4570 return RHS.isMinSignedValue();
4571 case ICmpInst::ICMP_ULE:
4572 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4573 TrueIfSigned = false;
4574 return RHS.isMaxSignedValue();
4575 default:
4576 return false;
4577 }
4578}
4579
4581 bool CondIsTrue,
4582 const Instruction *CxtI,
4583 KnownFPClass &KnownFromContext,
4584 unsigned Depth = 0) {
4585 Value *A, *B;
4587 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4588 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4589 computeKnownFPClassFromCond(V, A, CondIsTrue, CxtI, KnownFromContext,
4590 Depth + 1);
4591 computeKnownFPClassFromCond(V, B, CondIsTrue, CxtI, KnownFromContext,
4592 Depth + 1);
4593 return;
4594 }
4596 computeKnownFPClassFromCond(V, A, !CondIsTrue, CxtI, KnownFromContext,
4597 Depth + 1);
4598 return;
4599 }
4600 CmpPredicate Pred;
4601 Value *LHS;
4602 uint64_t ClassVal = 0;
4603 const APFloat *CRHS;
4604 const APInt *RHS;
4605 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4606 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4607 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4608 if (CmpVal == V)
4609 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4610 } else if (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(
4611 m_Specific(V), m_ConstantInt(ClassVal)))) {
4612 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4613 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4614 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4615 m_APInt(RHS)))) {
4616 bool TrueIfSigned;
4617 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4618 return;
4619 if (TrueIfSigned == CondIsTrue)
4620 KnownFromContext.signBitMustBeOne();
4621 else
4622 KnownFromContext.signBitMustBeZero();
4623 }
4624}
4625
4627 const SimplifyQuery &Q) {
4628 KnownFPClass KnownFromContext;
4629
4630 if (Q.CC && Q.CC->AffectedValues.contains(V))
4632 KnownFromContext);
4633
4634 if (!Q.CxtI)
4635 return KnownFromContext;
4636
4637 if (Q.DC && Q.DT) {
4638 // Handle dominating conditions.
4639 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4640 Value *Cond = BI->getCondition();
4641
4642 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4643 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4644 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/true, Q.CxtI,
4645 KnownFromContext);
4646
4647 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4648 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4649 computeKnownFPClassFromCond(V, Cond, /*CondIsTrue=*/false, Q.CxtI,
4650 KnownFromContext);
4651 }
4652 }
4653
4654 if (!Q.AC)
4655 return KnownFromContext;
4656
4657 // Try to restrict the floating-point classes based on information from
4658 // assumptions.
4659 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
4660 if (!AssumeVH)
4661 continue;
4662 CallInst *I = cast<CallInst>(AssumeVH);
4663
4664 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
4665 "Got assumption for the wrong function!");
4666 assert(I->getIntrinsicID() == Intrinsic::assume &&
4667 "must be an assume intrinsic");
4668
4669 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
4670 continue;
4671
4672 computeKnownFPClassFromCond(V, I->getArgOperand(0),
4673 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
4674 }
4675
4676 return KnownFromContext;
4677}
4678
4679void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4680 FPClassTest InterestedClasses, KnownFPClass &Known,
4681 const SimplifyQuery &Q, unsigned Depth);
4682
4683static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
4684 FPClassTest InterestedClasses,
4685 const SimplifyQuery &Q, unsigned Depth) {
4686 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
4687 APInt DemandedElts =
4688 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
4689 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Q, Depth);
4690}
4691
4693 const APInt &DemandedElts,
4694 FPClassTest InterestedClasses,
4695 KnownFPClass &Known,
4696 const SimplifyQuery &Q,
4697 unsigned Depth) {
4698 if ((InterestedClasses &
4700 return;
4701
4702 KnownFPClass KnownSrc;
4703 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4704 KnownSrc, Q, Depth + 1);
4705
4706 // Sign should be preserved
4707 // TODO: Handle cannot be ordered greater than zero
4708 if (KnownSrc.cannotBeOrderedLessThanZero())
4710
4711 Known.propagateNaN(KnownSrc, true);
4712
4713 // Infinity needs a range check.
4714}
4715
4716void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
4717 FPClassTest InterestedClasses, KnownFPClass &Known,
4718 const SimplifyQuery &Q, unsigned Depth) {
4719 assert(Known.isUnknown() && "should not be called with known information");
4720
4721 if (!DemandedElts) {
4722 // No demanded elts, better to assume we don't know anything.
4723 Known.resetAll();
4724 return;
4725 }
4726
4727 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
4728
4729 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
4730 Known.KnownFPClasses = CFP->getValueAPF().classify();
4731 Known.SignBit = CFP->isNegative();
4732 return;
4733 }
4734
4735 if (isa<ConstantAggregateZero>(V)) {
4736 Known.KnownFPClasses = fcPosZero;
4737 Known.SignBit = false;
4738 return;
4739 }
4740
4741 if (isa<PoisonValue>(V)) {
4742 Known.KnownFPClasses = fcNone;
4743 Known.SignBit = false;
4744 return;
4745 }
4746
4747 // Try to handle fixed width vector constants
4748 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
4749 const Constant *CV = dyn_cast<Constant>(V);
4750 if (VFVTy && CV) {
4751 Known.KnownFPClasses = fcNone;
4752 bool SignBitAllZero = true;
4753 bool SignBitAllOne = true;
4754
4755 // For vectors, verify that each element is not NaN.
4756 unsigned NumElts = VFVTy->getNumElements();
4757 for (unsigned i = 0; i != NumElts; ++i) {
4758 if (!DemandedElts[i])
4759 continue;
4760
4761 Constant *Elt = CV->getAggregateElement(i);
4762 if (!Elt) {
4763 Known = KnownFPClass();
4764 return;
4765 }
4766 if (isa<PoisonValue>(Elt))
4767 continue;
4768 auto *CElt = dyn_cast<ConstantFP>(Elt);
4769 if (!CElt) {
4770 Known = KnownFPClass();
4771 return;
4772 }
4773
4774 const APFloat &C = CElt->getValueAPF();
4775 Known.KnownFPClasses |= C.classify();
4776 if (C.isNegative())
4777 SignBitAllZero = false;
4778 else
4779 SignBitAllOne = false;
4780 }
4781 if (SignBitAllOne != SignBitAllZero)
4782 Known.SignBit = SignBitAllOne;
4783 return;
4784 }
4785
4786 FPClassTest KnownNotFromFlags = fcNone;
4787 if (const auto *CB = dyn_cast<CallBase>(V))
4788 KnownNotFromFlags |= CB->getRetNoFPClass();
4789 else if (const auto *Arg = dyn_cast<Argument>(V))
4790 KnownNotFromFlags |= Arg->getNoFPClass();
4791
4792 const Operator *Op = dyn_cast<Operator>(V);
4793 if (const FPMathOperator *FPOp = dyn_cast_or_null<FPMathOperator>(Op)) {
4794 if (FPOp->hasNoNaNs())
4795 KnownNotFromFlags |= fcNan;
4796 if (FPOp->hasNoInfs())
4797 KnownNotFromFlags |= fcInf;
4798 }
4799
4800 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
4801 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
4802
4803 // We no longer need to find out about these bits from inputs if we can
4804 // assume this from flags/attributes.
4805 InterestedClasses &= ~KnownNotFromFlags;
4806
4807 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
4808 Known.knownNot(KnownNotFromFlags);
4809 if (!Known.SignBit && AssumedClasses.SignBit) {
4810 if (*AssumedClasses.SignBit)
4811 Known.signBitMustBeOne();
4812 else
4813 Known.signBitMustBeZero();
4814 }
4815 });
4816
4817 if (!Op)
4818 return;
4819
4820 // All recursive calls that increase depth must come after this.
4822 return;
4823
4824 const unsigned Opc = Op->getOpcode();
4825 switch (Opc) {
4826 case Instruction::FNeg: {
4827 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
4828 Known, Q, Depth + 1);
4829 Known.fneg();
4830 break;
4831 }
4832 case Instruction::Select: {
4833 Value *Cond = Op->getOperand(0);
4834 Value *LHS = Op->getOperand(1);
4835 Value *RHS = Op->getOperand(2);
4836
4837 FPClassTest FilterLHS = fcAllFlags;
4838 FPClassTest FilterRHS = fcAllFlags;
4839
4840 Value *TestedValue = nullptr;
4841 FPClassTest MaskIfTrue = fcAllFlags;
4842 FPClassTest MaskIfFalse = fcAllFlags;
4843 uint64_t ClassVal = 0;
4844 const Function *F = cast<Instruction>(Op)->getFunction();
4845 CmpPredicate Pred;
4846 Value *CmpLHS, *CmpRHS;
4847 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
4848 // If the select filters out a value based on the class, it no longer
4849 // participates in the class of the result
4850
4851 // TODO: In some degenerate cases we can infer something if we try again
4852 // without looking through sign operations.
4853 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
4854 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
4855 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
4856 } else if (match(Cond,
4857 m_Intrinsic<Intrinsic::is_fpclass>(
4858 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
4859 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
4860 MaskIfTrue = TestedMask;
4861 MaskIfFalse = ~TestedMask;
4862 }
4863
4864 if (TestedValue == LHS) {
4865 // match !isnan(x) ? x : y
4866 FilterLHS = MaskIfTrue;
4867 } else if (TestedValue == RHS) { // && IsExactClass
4868 // match !isnan(x) ? y : x
4869 FilterRHS = MaskIfFalse;
4870 }
4871
4872 KnownFPClass Known2;
4873 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
4874 Q, Depth + 1);
4875 Known.KnownFPClasses &= FilterLHS;
4876
4877 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
4878 Known2, Q, Depth + 1);
4879 Known2.KnownFPClasses &= FilterRHS;
4880
4881 Known |= Known2;
4882 break;
4883 }
4884 case Instruction::Call: {
4885 const CallInst *II = cast<CallInst>(Op);
4886 const Intrinsic::ID IID = II->getIntrinsicID();
4887 switch (IID) {
4888 case Intrinsic::fabs: {
4889 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
4890 // If we only care about the sign bit we don't need to inspect the
4891 // operand.
4892 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
4893 InterestedClasses, Known, Q, Depth + 1);
4894 }
4895
4896 Known.fabs();
4897 break;
4898 }
4899 case Intrinsic::copysign: {
4900 KnownFPClass KnownSign;
4901
4902 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4903 Known, Q, Depth + 1);
4904 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4905 KnownSign, Q, Depth + 1);
4906 Known.copysign(KnownSign);
4907 break;
4908 }
4909 case Intrinsic::fma:
4910 case Intrinsic::fmuladd: {
4911 if ((InterestedClasses & fcNegative) == fcNone)
4912 break;
4913
4914 if (II->getArgOperand(0) != II->getArgOperand(1))
4915 break;
4916
4917 // The multiply cannot be -0 and therefore the add can't be -0
4918 Known.knownNot(fcNegZero);
4919
4920 // x * x + y is non-negative if y is non-negative.
4921 KnownFPClass KnownAddend;
4922 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
4923 KnownAddend, Q, Depth + 1);
4924
4925 if (KnownAddend.cannotBeOrderedLessThanZero())
4926 Known.knownNot(fcNegative);
4927 break;
4928 }
4929 case Intrinsic::sqrt:
4930 case Intrinsic::experimental_constrained_sqrt: {
4931 KnownFPClass KnownSrc;
4932 FPClassTest InterestedSrcs = InterestedClasses;
4933 if (InterestedClasses & fcNan)
4934 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
4935
4936 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
4937 KnownSrc, Q, Depth + 1);
4938
4939 if (KnownSrc.isKnownNeverPosInfinity())
4940 Known.knownNot(fcPosInf);
4941 if (KnownSrc.isKnownNever(fcSNan))
4942 Known.knownNot(fcSNan);
4943
4944 // Any negative value besides -0 returns a nan.
4945 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
4946 Known.knownNot(fcNan);
4947
4948 // The only negative value that can be returned is -0 for -0 inputs.
4950
4951 // If the input denormal mode could be PreserveSign, a negative
4952 // subnormal input could produce a negative zero output.
4953 const Function *F = II->getFunction();
4954 const fltSemantics &FltSem =
4955 II->getType()->getScalarType()->getFltSemantics();
4956
4957 if (Q.IIQ.hasNoSignedZeros(II) ||
4958 (F &&
4959 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem))))
4960 Known.knownNot(fcNegZero);
4961
4962 break;
4963 }
4964 case Intrinsic::sin:
4965 case Intrinsic::cos: {
4966 // Return NaN on infinite inputs.
4967 KnownFPClass KnownSrc;
4968 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4969 KnownSrc, Q, Depth + 1);
4970 Known.knownNot(fcInf);
4971 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
4972 Known.knownNot(fcNan);
4973 break;
4974 }
4975 case Intrinsic::maxnum:
4976 case Intrinsic::minnum:
4977 case Intrinsic::minimum:
4978 case Intrinsic::maximum:
4979 case Intrinsic::minimumnum:
4980 case Intrinsic::maximumnum: {
4981 KnownFPClass KnownLHS, KnownRHS;
4982 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
4983 KnownLHS, Q, Depth + 1);
4984 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
4985 KnownRHS, Q, Depth + 1);
4986
4987 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
4988 Known = KnownLHS | KnownRHS;
4989
4990 // If either operand is not NaN, the result is not NaN.
4991 if (NeverNaN &&
4992 (IID == Intrinsic::minnum || IID == Intrinsic::maxnum ||
4993 IID == Intrinsic::minimumnum || IID == Intrinsic::maximumnum))
4994 Known.knownNot(fcNan);
4995
4996 if (IID == Intrinsic::maxnum || IID == Intrinsic::maximumnum) {
4997 // If at least one operand is known to be positive, the result must be
4998 // positive.
4999 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
5000 KnownLHS.isKnownNeverNaN()) ||
5001 (KnownRHS.cannotBeOrderedLessThanZero() &&
5002 KnownRHS.isKnownNeverNaN()))
5004 } else if (IID == Intrinsic::maximum) {
5005 // If at least one operand is known to be positive, the result must be
5006 // positive.
5007 if (KnownLHS.cannotBeOrderedLessThanZero() ||
5008 KnownRHS.cannotBeOrderedLessThanZero())
5010 } else if (IID == Intrinsic::minnum || IID == Intrinsic::minimumnum) {
5011 // If at least one operand is known to be negative, the result must be
5012 // negative.
5013 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
5014 KnownLHS.isKnownNeverNaN()) ||
5015 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
5016 KnownRHS.isKnownNeverNaN()))
5018 } else if (IID == Intrinsic::minimum) {
5019 // If at least one operand is known to be negative, the result must be
5020 // negative.
5021 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
5024 } else
5025 llvm_unreachable("unhandled intrinsic");
5026
5027 // Fixup zero handling if denormals could be returned as a zero.
5028 //
5029 // As there's no spec for denormal flushing, be conservative with the
5030 // treatment of denormals that could be flushed to zero. For older
5031 // subtargets on AMDGPU the min/max instructions would not flush the
5032 // output and return the original value.
5033 //
5034 if ((Known.KnownFPClasses & fcZero) != fcNone &&
5035 !Known.isKnownNeverSubnormal()) {
5036 const Function *Parent = II->getFunction();
5037 if (!Parent)
5038 break;
5039
5040 DenormalMode Mode = Parent->getDenormalMode(
5041 II->getType()->getScalarType()->getFltSemantics());
5042 if (Mode != DenormalMode::getIEEE())
5043 Known.KnownFPClasses |= fcZero;
5044 }
5045
5046 if (Known.isKnownNeverNaN()) {
5047 if (KnownLHS.SignBit && KnownRHS.SignBit &&
5048 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5049 if (*KnownLHS.SignBit)
5050 Known.signBitMustBeOne();
5051 else
5052 Known.signBitMustBeZero();
5053 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum ||
5054 IID == Intrinsic::maximumnum ||
5055 IID == Intrinsic::minimumnum) ||
5056 // FIXME: Should be using logical zero versions
5057 ((KnownLHS.isKnownNeverNegZero() ||
5058 KnownRHS.isKnownNeverPosZero()) &&
5059 (KnownLHS.isKnownNeverPosZero() ||
5060 KnownRHS.isKnownNeverNegZero()))) {
5061 if ((IID == Intrinsic::maximum || IID == Intrinsic::maximumnum ||
5062 IID == Intrinsic::maxnum) &&
5063 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5064 Known.signBitMustBeZero();
5065 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minimumnum ||
5066 IID == Intrinsic::minnum) &&
5067 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5068 Known.signBitMustBeOne();
5069 }
5070 }
5071 break;
5072 }
5073 case Intrinsic::canonicalize: {
5074 KnownFPClass KnownSrc;
5075 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5076 KnownSrc, Q, Depth + 1);
5077
5078 // This is essentially a stronger form of
5079 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5080 // actually have an IR canonicalization guarantee.
5081
5082 // Canonicalize may flush denormals to zero, so we have to consider the
5083 // denormal mode to preserve known-not-0 knowledge.
5084 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5085
5086 // Stronger version of propagateNaN
5087 // Canonicalize is guaranteed to quiet signaling nans.
5088 if (KnownSrc.isKnownNeverNaN())
5089 Known.knownNot(fcNan);
5090 else
5091 Known.knownNot(fcSNan);
5092
5093 const Function *F = II->getFunction();
5094 if (!F)
5095 break;
5096
5097 // If the parent function flushes denormals, the canonical output cannot
5098 // be a denormal.
5099 const fltSemantics &FPType =
5100 II->getType()->getScalarType()->getFltSemantics();
5101 DenormalMode DenormMode = F->getDenormalMode(FPType);
5102 if (DenormMode == DenormalMode::getIEEE()) {
5103 if (KnownSrc.isKnownNever(fcPosZero))
5104 Known.knownNot(fcPosZero);
5105 if (KnownSrc.isKnownNever(fcNegZero))
5106 Known.knownNot(fcNegZero);
5107 break;
5108 }
5109
5110 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5111 Known.knownNot(fcSubnormal);
5112
5113 if (DenormMode.Input == DenormalMode::PositiveZero ||
5114 (DenormMode.Output == DenormalMode::PositiveZero &&
5115 DenormMode.Input == DenormalMode::IEEE))
5116 Known.knownNot(fcNegZero);
5117
5118 break;
5119 }
5120 case Intrinsic::vector_reduce_fmax:
5121 case Intrinsic::vector_reduce_fmin:
5122 case Intrinsic::vector_reduce_fmaximum:
5123 case Intrinsic::vector_reduce_fminimum: {
5124 // reduce min/max will choose an element from one of the vector elements,
5125 // so we can infer and class information that is common to all elements.
5126 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5127 InterestedClasses, Q, Depth + 1);
5128 // Can only propagate sign if output is never NaN.
5129 if (!Known.isKnownNeverNaN())
5130 Known.SignBit.reset();
5131 break;
5132 }
5133 // reverse preserves all characteristics of the input vec's element.
5134 case Intrinsic::vector_reverse:
5135 Known = computeKnownFPClass(
5136 II->getArgOperand(0), DemandedElts.reverseBits(),
5137 II->getFastMathFlags(), InterestedClasses, Q, Depth + 1);
5138 break;
5139 case Intrinsic::trunc:
5140 case Intrinsic::floor:
5141 case Intrinsic::ceil:
5142 case Intrinsic::rint:
5143 case Intrinsic::nearbyint:
5144 case Intrinsic::round:
5145 case Intrinsic::roundeven: {
5146 KnownFPClass KnownSrc;
5147 FPClassTest InterestedSrcs = InterestedClasses;
5148 if (InterestedSrcs & fcPosFinite)
5149 InterestedSrcs |= fcPosFinite;
5150 if (InterestedSrcs & fcNegFinite)
5151 InterestedSrcs |= fcNegFinite;
5152 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5153 KnownSrc, Q, Depth + 1);
5154
5155 // Integer results cannot be subnormal.
5156 Known.knownNot(fcSubnormal);
5157
5158 Known.propagateNaN(KnownSrc, true);
5159
5160 // Pass through infinities, except PPC_FP128 is a special case for
5161 // intrinsics other than trunc.
5162 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5163 if (KnownSrc.isKnownNeverPosInfinity())
5164 Known.knownNot(fcPosInf);
5165 if (KnownSrc.isKnownNeverNegInfinity())
5166 Known.knownNot(fcNegInf);
5167 }
5168
5169 // Negative round ups to 0 produce -0
5170 if (KnownSrc.isKnownNever(fcPosFinite))
5171 Known.knownNot(fcPosFinite);
5172 if (KnownSrc.isKnownNever(fcNegFinite))
5173 Known.knownNot(fcNegFinite);
5174
5175 break;
5176 }
5177 case Intrinsic::exp:
5178 case Intrinsic::exp2:
5179 case Intrinsic::exp10: {
5180 Known.knownNot(fcNegative);
5181 if ((InterestedClasses & fcNan) == fcNone)
5182 break;
5183
5184 KnownFPClass KnownSrc;
5185 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5186 KnownSrc, Q, Depth + 1);
5187 if (KnownSrc.isKnownNeverNaN()) {
5188 Known.knownNot(fcNan);
5189 Known.signBitMustBeZero();
5190 }
5191
5192 break;
5193 }
5194 case Intrinsic::fptrunc_round: {
5195 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5196 Q, Depth);
5197 break;
5198 }
5199 case Intrinsic::log:
5200 case Intrinsic::log10:
5201 case Intrinsic::log2:
5202 case Intrinsic::experimental_constrained_log:
5203 case Intrinsic::experimental_constrained_log10:
5204 case Intrinsic::experimental_constrained_log2: {
5205 // log(+inf) -> +inf
5206 // log([+-]0.0) -> -inf
5207 // log(-inf) -> nan
5208 // log(-x) -> nan
5209 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5210 break;
5211
5212 FPClassTest InterestedSrcs = InterestedClasses;
5213 if ((InterestedClasses & fcNegInf) != fcNone)
5214 InterestedSrcs |= fcZero | fcSubnormal;
5215 if ((InterestedClasses & fcNan) != fcNone)
5216 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5217
5218 KnownFPClass KnownSrc;
5219 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5220 KnownSrc, Q, Depth + 1);
5221
5222 if (KnownSrc.isKnownNeverPosInfinity())
5223 Known.knownNot(fcPosInf);
5224
5225 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5226 Known.knownNot(fcNan);
5227
5228 const Function *F = II->getFunction();
5229
5230 if (!F)
5231 break;
5232
5233 const fltSemantics &FltSem =
5234 II->getType()->getScalarType()->getFltSemantics();
5235 DenormalMode Mode = F->getDenormalMode(FltSem);
5236
5237 if (KnownSrc.isKnownNeverLogicalZero(Mode))
5238 Known.knownNot(fcNegInf);
5239
5240 break;
5241 }
5242 case Intrinsic::powi: {
5243 if ((InterestedClasses & fcNegative) == fcNone)
5244 break;
5245
5246 const Value *Exp = II->getArgOperand(1);
5247 Type *ExpTy = Exp->getType();
5248 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5249 KnownBits ExponentKnownBits(BitWidth);
5250 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5251 ExponentKnownBits, Q, Depth + 1);
5252
5253 if (ExponentKnownBits.Zero[0]) { // Is even
5254 Known.knownNot(fcNegative);
5255 break;
5256 }
5257
5258 // Given that exp is an integer, here are the
5259 // ways that pow can return a negative value:
5260 //
5261 // pow(-x, exp) --> negative if exp is odd and x is negative.
5262 // pow(-0, exp) --> -inf if exp is negative odd.
5263 // pow(-0, exp) --> -0 if exp is positive odd.
5264 // pow(-inf, exp) --> -0 if exp is negative odd.
5265 // pow(-inf, exp) --> -inf if exp is positive odd.
5266 KnownFPClass KnownSrc;
5267 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5268 KnownSrc, Q, Depth + 1);
5269 if (KnownSrc.isKnownNever(fcNegative))
5270 Known.knownNot(fcNegative);
5271 break;
5272 }
5273 case Intrinsic::ldexp: {
5274 KnownFPClass KnownSrc;
5275 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5276 KnownSrc, Q, Depth + 1);
5277 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5278
5279 // Sign is preserved, but underflows may produce zeroes.
5280 if (KnownSrc.isKnownNever(fcNegative))
5281 Known.knownNot(fcNegative);
5282 else if (KnownSrc.cannotBeOrderedLessThanZero())
5284
5285 if (KnownSrc.isKnownNever(fcPositive))
5286 Known.knownNot(fcPositive);
5287 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5289
5290 // Can refine inf/zero handling based on the exponent operand.
5291 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5292 if ((InterestedClasses & ExpInfoMask) == fcNone)
5293 break;
5294 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5295 break;
5296
5297 const fltSemantics &Flt =
5298 II->getType()->getScalarType()->getFltSemantics();
5299 unsigned Precision = APFloat::semanticsPrecision(Flt);
5300 const Value *ExpArg = II->getArgOperand(1);
5302 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5303
5304 const int MantissaBits = Precision - 1;
5305 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5306 Known.knownNot(fcSubnormal);
5307
5308 const Function *F = II->getFunction();
5309 const APInt *ConstVal = ExpRange.getSingleElement();
5310 const fltSemantics &FltSem =
5311 II->getType()->getScalarType()->getFltSemantics();
5312 if (ConstVal && ConstVal->isZero()) {
5313 // ldexp(x, 0) -> x, so propagate everything.
5314 Known.propagateCanonicalizingSrc(KnownSrc, F->getDenormalMode(FltSem));
5315 } else if (ExpRange.isAllNegative()) {
5316 // If we know the power is <= 0, can't introduce inf
5317 if (KnownSrc.isKnownNeverPosInfinity())
5318 Known.knownNot(fcPosInf);
5319 if (KnownSrc.isKnownNeverNegInfinity())
5320 Known.knownNot(fcNegInf);
5321 } else if (ExpRange.isAllNonNegative()) {
5322 // If we know the power is >= 0, can't introduce subnormal or zero
5323 if (KnownSrc.isKnownNeverPosSubnormal())
5324 Known.knownNot(fcPosSubnormal);
5325 if (KnownSrc.isKnownNeverNegSubnormal())
5326 Known.knownNot(fcNegSubnormal);
5327 if (F &&
5328 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5329 Known.knownNot(fcPosZero);
5330 if (F &&
5331 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5332 Known.knownNot(fcNegZero);
5333 }
5334
5335 break;
5336 }
5337 case Intrinsic::arithmetic_fence: {
5338 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5339 Known, Q, Depth + 1);
5340 break;
5341 }
5342 case Intrinsic::experimental_constrained_sitofp:
5343 case Intrinsic::experimental_constrained_uitofp:
5344 // Cannot produce nan
5345 Known.knownNot(fcNan);
5346
5347 // sitofp and uitofp turn into +0.0 for zero.
5348 Known.knownNot(fcNegZero);
5349
5350 // Integers cannot be subnormal
5351 Known.knownNot(fcSubnormal);
5352
5353 if (IID == Intrinsic::experimental_constrained_uitofp)
5354 Known.signBitMustBeZero();
5355
5356 // TODO: Copy inf handling from instructions
5357 break;
5358 default:
5359 break;
5360 }
5361
5362 break;
5363 }
5364 case Instruction::FAdd:
5365 case Instruction::FSub: {
5366 KnownFPClass KnownLHS, KnownRHS;
5367 bool WantNegative =
5368 Op->getOpcode() == Instruction::FAdd &&
5369 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5370 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5371 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5372
5373 if (!WantNaN && !WantNegative && !WantNegZero)
5374 break;
5375
5376 FPClassTest InterestedSrcs = InterestedClasses;
5377 if (WantNegative)
5378 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5379 if (InterestedClasses & fcNan)
5380 InterestedSrcs |= fcInf;
5381 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5382 KnownRHS, Q, Depth + 1);
5383
5384 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5385 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5386 WantNegZero || Opc == Instruction::FSub) {
5387
5388 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5389 // there's no point.
5390 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5391 KnownLHS, Q, Depth + 1);
5392 // Adding positive and negative infinity produces NaN.
5393 // TODO: Check sign of infinities.
5394 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5395 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5396 Known.knownNot(fcNan);
5397
5398 // FIXME: Context function should always be passed in separately
5399 const Function *F = cast<Instruction>(Op)->getFunction();
5400
5401 if (Op->getOpcode() == Instruction::FAdd) {
5402 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5403 KnownRHS.cannotBeOrderedLessThanZero())
5405 if (!F)
5406 break;
5407
5408 const fltSemantics &FltSem =
5409 Op->getType()->getScalarType()->getFltSemantics();
5410 DenormalMode Mode = F->getDenormalMode(FltSem);
5411
5412 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5413 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5414 KnownRHS.isKnownNeverLogicalNegZero(Mode)) &&
5415 // Make sure output negative denormal can't flush to -0
5416 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5417 Known.knownNot(fcNegZero);
5418 } else {
5419 if (!F)
5420 break;
5421
5422 const fltSemantics &FltSem =
5423 Op->getType()->getScalarType()->getFltSemantics();
5424 DenormalMode Mode = F->getDenormalMode(FltSem);
5425
5426 // Only fsub -0, +0 can return -0
5427 if ((KnownLHS.isKnownNeverLogicalNegZero(Mode) ||
5428 KnownRHS.isKnownNeverLogicalPosZero(Mode)) &&
5429 // Make sure output negative denormal can't flush to -0
5430 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5431 Known.knownNot(fcNegZero);
5432 }
5433 }
5434
5435 break;
5436 }
5437 case Instruction::FMul: {
5438 // X * X is always non-negative or a NaN.
5439 if (Op->getOperand(0) == Op->getOperand(1))
5440 Known.knownNot(fcNegative);
5441
5442 if ((InterestedClasses & fcNan) != fcNan)
5443 break;
5444
5445 // fcSubnormal is only needed in case of DAZ.
5446 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5447
5448 KnownFPClass KnownLHS, KnownRHS;
5449 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5450 Q, Depth + 1);
5451 if (!KnownRHS.isKnownNeverNaN())
5452 break;
5453
5454 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5455 Q, Depth + 1);
5456 if (!KnownLHS.isKnownNeverNaN())
5457 break;
5458
5459 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5460 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5461 Known.signBitMustBeZero();
5462 else
5463 Known.signBitMustBeOne();
5464 }
5465
5466 // If 0 * +/-inf produces NaN.
5467 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5468 Known.knownNot(fcNan);
5469 break;
5470 }
5471
5472 const Function *F = cast<Instruction>(Op)->getFunction();
5473 if (!F)
5474 break;
5475
5476 Type *OpTy = Op->getType()->getScalarType();
5477 const fltSemantics &FltSem = OpTy->getFltSemantics();
5478 DenormalMode Mode = F->getDenormalMode(FltSem);
5479
5480 if ((KnownRHS.isKnownNeverInfinity() ||
5481 KnownLHS.isKnownNeverLogicalZero(Mode)) &&
5482 (KnownLHS.isKnownNeverInfinity() ||
5483 KnownRHS.isKnownNeverLogicalZero(Mode)))
5484 Known.knownNot(fcNan);
5485
5486 break;
5487 }
5488 case Instruction::FDiv:
5489 case Instruction::FRem: {
5490 if (Op->getOperand(0) == Op->getOperand(1)) {
5491 // TODO: Could filter out snan if we inspect the operand
5492 if (Op->getOpcode() == Instruction::FDiv) {
5493 // X / X is always exactly 1.0 or a NaN.
5495 } else {
5496 // X % X is always exactly [+-]0.0 or a NaN.
5497 Known.KnownFPClasses = fcNan | fcZero;
5498 }
5499
5500 break;
5501 }
5502
5503 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5504 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5505 const bool WantPositive =
5506 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5507 if (!WantNan && !WantNegative && !WantPositive)
5508 break;
5509
5510 KnownFPClass KnownLHS, KnownRHS;
5511
5512 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5513 fcNan | fcInf | fcZero | fcNegative, KnownRHS, Q,
5514 Depth + 1);
5515
5516 bool KnowSomethingUseful =
5517 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5518
5519 if (KnowSomethingUseful || WantPositive) {
5520 const FPClassTest InterestedLHS =
5521 WantPositive ? fcAllFlags
5523
5524 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5525 InterestedClasses & InterestedLHS, KnownLHS, Q,
5526 Depth + 1);
5527 }
5528
5529 const Function *F = cast<Instruction>(Op)->getFunction();
5530 const fltSemantics &FltSem =
5531 Op->getType()->getScalarType()->getFltSemantics();
5532
5533 if (Op->getOpcode() == Instruction::FDiv) {
5534 // Only 0/0, Inf/Inf produce NaN.
5535 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5536 (KnownLHS.isKnownNeverInfinity() ||
5537 KnownRHS.isKnownNeverInfinity()) &&
5538 ((F &&
5539 KnownLHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) ||
5540 (F &&
5541 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))))) {
5542 Known.knownNot(fcNan);
5543 }
5544
5545 // X / -0.0 is -Inf (or NaN).
5546 // +X / +X is +X
5547 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5548 Known.knownNot(fcNegative);
5549 } else {
5550 // Inf REM x and x REM 0 produce NaN.
5551 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5552 KnownLHS.isKnownNeverInfinity() && F &&
5553 KnownRHS.isKnownNeverLogicalZero(F->getDenormalMode(FltSem))) {
5554 Known.knownNot(fcNan);
5555 }
5556
5557 // The sign for frem is the same as the first operand.
5558 if (KnownLHS.cannotBeOrderedLessThanZero())
5560 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5562
5563 // See if we can be more aggressive about the sign of 0.
5564 if (KnownLHS.isKnownNever(fcNegative))
5565 Known.knownNot(fcNegative);
5566 if (KnownLHS.isKnownNever(fcPositive))
5567 Known.knownNot(fcPositive);
5568 }
5569
5570 break;
5571 }
5572 case Instruction::FPExt: {
5573 // Infinity, nan and zero propagate from source.
5574 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5575 Known, Q, Depth + 1);
5576
5577 const fltSemantics &DstTy =
5578 Op->getType()->getScalarType()->getFltSemantics();
5579 const fltSemantics &SrcTy =
5580 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5581
5582 // All subnormal inputs should be in the normal range in the result type.
5583 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5584 if (Known.KnownFPClasses & fcPosSubnormal)
5585 Known.KnownFPClasses |= fcPosNormal;
5586 if (Known.KnownFPClasses & fcNegSubnormal)
5587 Known.KnownFPClasses |= fcNegNormal;
5588 Known.knownNot(fcSubnormal);
5589 }
5590
5591 // Sign bit of a nan isn't guaranteed.
5592 if (!Known.isKnownNeverNaN())
5593 Known.SignBit = std::nullopt;
5594 break;
5595 }
5596 case Instruction::FPTrunc: {
5597 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known, Q,
5598 Depth);
5599 break;
5600 }
5601 case Instruction::SIToFP:
5602 case Instruction::UIToFP: {
5603 // Cannot produce nan
5604 Known.knownNot(fcNan);
5605
5606 // Integers cannot be subnormal
5607 Known.knownNot(fcSubnormal);
5608
5609 // sitofp and uitofp turn into +0.0 for zero.
5610 Known.knownNot(fcNegZero);
5611 if (Op->getOpcode() == Instruction::UIToFP)
5612 Known.signBitMustBeZero();
5613
5614 if (InterestedClasses & fcInf) {
5615 // Get width of largest magnitude integer (remove a bit if signed).
5616 // This still works for a signed minimum value because the largest FP
5617 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5618 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5619 if (Op->getOpcode() == Instruction::SIToFP)
5620 --IntSize;
5621
5622 // If the exponent of the largest finite FP value can hold the largest
5623 // integer, the result of the cast must be finite.
5624 Type *FPTy = Op->getType()->getScalarType();
5625 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5626 Known.knownNot(fcInf);
5627 }
5628
5629 break;
5630 }
5631 case Instruction::ExtractElement: {
5632 // Look through extract element. If the index is non-constant or
5633 // out-of-range demand all elements, otherwise just the extracted element.
5634 const Value *Vec = Op->getOperand(0);
5635
5636 APInt DemandedVecElts;
5637 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5638 unsigned NumElts = VecTy->getNumElements();
5639 DemandedVecElts = APInt::getAllOnes(NumElts);
5640 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(1));
5641 if (CIdx && CIdx->getValue().ult(NumElts))
5642 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5643 } else {
5644 DemandedVecElts = APInt(1, 1);
5645 }
5646
5647 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5648 Q, Depth + 1);
5649 }
5650 case Instruction::InsertElement: {
5651 if (isa<ScalableVectorType>(Op->getType()))
5652 return;
5653
5654 const Value *Vec = Op->getOperand(0);
5655 const Value *Elt = Op->getOperand(1);
5656 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5657 unsigned NumElts = DemandedElts.getBitWidth();
5658 APInt DemandedVecElts = DemandedElts;
5659 bool NeedsElt = true;
5660 // If we know the index we are inserting to, clear it from Vec check.
5661 if (CIdx && CIdx->getValue().ult(NumElts)) {
5662 DemandedVecElts.clearBit(CIdx->getZExtValue());
5663 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5664 }
5665
5666 // Do we demand the inserted element?
5667 if (NeedsElt) {
5668 computeKnownFPClass(Elt, Known, InterestedClasses, Q, Depth + 1);
5669 // If we don't know any bits, early out.
5670 if (Known.isUnknown())
5671 break;
5672 } else {
5673 Known.KnownFPClasses = fcNone;
5674 }
5675
5676 // Do we need anymore elements from Vec?
5677 if (!DemandedVecElts.isZero()) {
5678 KnownFPClass Known2;
5679 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2, Q,
5680 Depth + 1);
5681 Known |= Known2;
5682 }
5683
5684 break;
5685 }
5686 case Instruction::ShuffleVector: {
5687 // For undef elements, we don't know anything about the common state of
5688 // the shuffle result.
5689 APInt DemandedLHS, DemandedRHS;
5690 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5691 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5692 return;
5693
5694 if (!!DemandedLHS) {
5695 const Value *LHS = Shuf->getOperand(0);
5696 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known, Q,
5697 Depth + 1);
5698
5699 // If we don't know any bits, early out.
5700 if (Known.isUnknown())
5701 break;
5702 } else {
5703 Known.KnownFPClasses = fcNone;
5704 }
5705
5706 if (!!DemandedRHS) {
5707 KnownFPClass Known2;
5708 const Value *RHS = Shuf->getOperand(1);
5709 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2, Q,
5710 Depth + 1);
5711 Known |= Known2;
5712 }
5713
5714 break;
5715 }
5716 case Instruction::ExtractValue: {
5717 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
5718 ArrayRef<unsigned> Indices = Extract->getIndices();
5719 const Value *Src = Extract->getAggregateOperand();
5720 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
5721 Indices[0] == 0) {
5722 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
5723 switch (II->getIntrinsicID()) {
5724 case Intrinsic::frexp: {
5725 Known.knownNot(fcSubnormal);
5726
5727 KnownFPClass KnownSrc;
5728 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5729 InterestedClasses, KnownSrc, Q, Depth + 1);
5730
5731 const Function *F = cast<Instruction>(Op)->getFunction();
5732 const fltSemantics &FltSem =
5733 Op->getType()->getScalarType()->getFltSemantics();
5734
5735 if (KnownSrc.isKnownNever(fcNegative))
5736 Known.knownNot(fcNegative);
5737 else {
5738 if (F &&
5739 KnownSrc.isKnownNeverLogicalNegZero(F->getDenormalMode(FltSem)))
5740 Known.knownNot(fcNegZero);
5741 if (KnownSrc.isKnownNever(fcNegInf))
5742 Known.knownNot(fcNegInf);
5743 }
5744
5745 if (KnownSrc.isKnownNever(fcPositive))
5746 Known.knownNot(fcPositive);
5747 else {
5748 if (F &&
5749 KnownSrc.isKnownNeverLogicalPosZero(F->getDenormalMode(FltSem)))
5750 Known.knownNot(fcPosZero);
5751 if (KnownSrc.isKnownNever(fcPosInf))
5752 Known.knownNot(fcPosInf);
5753 }
5754
5755 Known.propagateNaN(KnownSrc);
5756 return;
5757 }
5758 default:
5759 break;
5760 }
5761 }
5762 }
5763
5764 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Q,
5765 Depth + 1);
5766 break;
5767 }
5768 case Instruction::PHI: {
5769 const PHINode *P = cast<PHINode>(Op);
5770 // Unreachable blocks may have zero-operand PHI nodes.
5771 if (P->getNumIncomingValues() == 0)
5772 break;
5773
5774 // Otherwise take the unions of the known bit sets of the operands,
5775 // taking conservative care to avoid excessive recursion.
5776 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
5777
5778 if (Depth < PhiRecursionLimit) {
5779 // Skip if every incoming value references to ourself.
5780 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
5781 break;
5782
5783 bool First = true;
5784
5785 for (const Use &U : P->operands()) {
5786 Value *IncValue;
5787 Instruction *CxtI;
5788 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
5789 // Skip direct self references.
5790 if (IncValue == P)
5791 continue;
5792
5793 KnownFPClass KnownSrc;
5794 // Recurse, but cap the recursion to two levels, because we don't want
5795 // to waste time spinning around in loops. We need at least depth 2 to
5796 // detect known sign bits.
5797 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
5799 PhiRecursionLimit);
5800
5801 if (First) {
5802 Known = KnownSrc;
5803 First = false;
5804 } else {
5805 Known |= KnownSrc;
5806 }
5807
5808 if (Known.KnownFPClasses == fcAllFlags)
5809 break;
5810 }
5811 }
5812
5813 break;
5814 }
5815 case Instruction::BitCast: {
5816 const Value *Src;
5817 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
5818 !Src->getType()->isIntOrIntVectorTy())
5819 break;
5820
5821 const Type *Ty = Op->getType()->getScalarType();
5822 KnownBits Bits(Ty->getScalarSizeInBits());
5823 computeKnownBits(Src, DemandedElts, Bits, Q, Depth + 1);
5824
5825 // Transfer information from the sign bit.
5826 if (Bits.isNonNegative())
5827 Known.signBitMustBeZero();
5828 else if (Bits.isNegative())
5829 Known.signBitMustBeOne();
5830
5831 if (Ty->isIEEELikeFPTy()) {
5832 // IEEE floats are NaN when all bits of the exponent plus at least one of
5833 // the fraction bits are 1. This means:
5834 // - If we assume unknown bits are 0 and the value is NaN, it will
5835 // always be NaN
5836 // - If we assume unknown bits are 1 and the value is not NaN, it can
5837 // never be NaN
5838 // Note: They do not hold for x86_fp80 format.
5839 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
5840 Known.KnownFPClasses = fcNan;
5841 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
5842 Known.knownNot(fcNan);
5843
5844 // Build KnownBits representing Inf and check if it must be equal or
5845 // unequal to this value.
5846 auto InfKB = KnownBits::makeConstant(
5848 InfKB.Zero.clearSignBit();
5849 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
5850 assert(!InfResult.value());
5851 Known.knownNot(fcInf);
5852 } else if (Bits == InfKB) {
5853 Known.KnownFPClasses = fcInf;
5854 }
5855
5856 // Build KnownBits representing Zero and check if it must be equal or
5857 // unequal to this value.
5858 auto ZeroKB = KnownBits::makeConstant(
5860 ZeroKB.Zero.clearSignBit();
5861 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
5862 assert(!ZeroResult.value());
5863 Known.knownNot(fcZero);
5864 } else if (Bits == ZeroKB) {
5865 Known.KnownFPClasses = fcZero;
5866 }
5867 }
5868
5869 break;
5870 }
5871 default:
5872 break;
5873 }
5874}
5875
5877 const APInt &DemandedElts,
5878 FPClassTest InterestedClasses,
5879 const SimplifyQuery &SQ,
5880 unsigned Depth) {
5881 KnownFPClass KnownClasses;
5882 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, SQ,
5883 Depth);
5884 return KnownClasses;
5885}
5886
5888 FPClassTest InterestedClasses,
5889 const SimplifyQuery &SQ,
5890 unsigned Depth) {
5891 KnownFPClass Known;
5892 ::computeKnownFPClass(V, Known, InterestedClasses, SQ, Depth);
5893 return Known;
5894}
5895
5897 const Value *V, const DataLayout &DL, FPClassTest InterestedClasses,
5898 const TargetLibraryInfo *TLI, AssumptionCache *AC, const Instruction *CxtI,
5899 const DominatorTree *DT, bool UseInstrInfo, unsigned Depth) {
5900 return computeKnownFPClass(V, InterestedClasses,
5901 SimplifyQuery(DL, TLI, DT, AC, CxtI, UseInstrInfo),
5902 Depth);
5903}
5904
5906llvm::computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5907 FastMathFlags FMF, FPClassTest InterestedClasses,
5908 const SimplifyQuery &SQ, unsigned Depth) {
5909 if (FMF.noNaNs())
5910 InterestedClasses &= ~fcNan;
5911 if (FMF.noInfs())
5912 InterestedClasses &= ~fcInf;
5913
5914 KnownFPClass Result =
5915 computeKnownFPClass(V, DemandedElts, InterestedClasses, SQ, Depth);
5916
5917 if (FMF.noNaNs())
5918 Result.KnownFPClasses &= ~fcNan;
5919 if (FMF.noInfs())
5920 Result.KnownFPClasses &= ~fcInf;
5921 return Result;
5922}
5923
5925 FPClassTest InterestedClasses,
5926 const SimplifyQuery &SQ,
5927 unsigned Depth) {
5928 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5929 APInt DemandedElts =
5930 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5931 return computeKnownFPClass(V, DemandedElts, FMF, InterestedClasses, SQ,
5932 Depth);
5933}
5934
5936 unsigned Depth) {
5938 return Known.isKnownNeverNegZero();
5939}
5940
5942 unsigned Depth) {
5943 KnownFPClass Known =
5945 return Known.cannotBeOrderedLessThanZero();
5946}
5947
5949 unsigned Depth) {
5951 return Known.isKnownNeverInfinity();
5952}
5953
5954/// Return true if the floating-point value can never contain a NaN or infinity.
5956 unsigned Depth) {
5958 return Known.isKnownNeverNaN() && Known.isKnownNeverInfinity();
5959}
5960
5961/// Return true if the floating-point scalar value is not a NaN or if the
5962/// floating-point vector value has no NaN elements. Return false if a value
5963/// could ever be NaN.
5965 unsigned Depth) {
5967 return Known.isKnownNeverNaN();
5968}
5969
5970/// Return false if we can prove that the specified FP value's sign bit is 0.
5971/// Return true if we can prove that the specified FP value's sign bit is 1.
5972/// Otherwise return std::nullopt.
5973std::optional<bool> llvm::computeKnownFPSignBit(const Value *V,
5974 const SimplifyQuery &SQ,
5975 unsigned Depth) {
5977 return Known.SignBit;
5978}
5979
5981 auto *User = cast<Instruction>(U.getUser());
5982 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
5983 if (FPOp->hasNoSignedZeros())
5984 return true;
5985 }
5986
5987 switch (User->getOpcode()) {
5988 case Instruction::FPToSI:
5989 case Instruction::FPToUI:
5990 return true;
5991 case Instruction::FCmp:
5992 // fcmp treats both positive and negative zero as equal.
5993 return true;
5994 case Instruction::Call:
5995 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
5996 switch (II->getIntrinsicID()) {
5997 case Intrinsic::fabs:
5998 return true;
5999 case Intrinsic::copysign:
6000 return U.getOperandNo() == 0;
6001 case Intrinsic::is_fpclass:
6002 case Intrinsic::vp_is_fpclass: {
6003 auto Test =
6004 static_cast<FPClassTest>(
6005 cast<ConstantInt>(II->getArgOperand(1))->getZExtValue()) &
6006 FPClassTest::fcZero;
6007 return Test == FPClassTest::fcZero || Test == FPClassTest::fcNone;
6008 }
6009 default:
6010 return false;
6011 }
6012 }
6013 return false;
6014 default:
6015 return false;
6016 }
6017}
6018
6020 auto *User = cast<Instruction>(U.getUser());
6021 if (auto *FPOp = dyn_cast<FPMathOperator>(User)) {
6022 if (FPOp->hasNoNaNs())
6023 return true;
6024 }
6025
6026 switch (User->getOpcode()) {
6027 case Instruction::FPToSI:
6028 case Instruction::FPToUI:
6029 return true;
6030 // Proper FP math operations ignore the sign bit of NaN.
6031 case Instruction::FAdd:
6032 case Instruction::FSub:
6033 case Instruction::FMul:
6034 case Instruction::FDiv:
6035 case Instruction::FRem:
6036 case Instruction::FPTrunc:
6037 case Instruction::FPExt:
6038 case Instruction::FCmp:
6039 return true;
6040 // Bitwise FP operations should preserve the sign bit of NaN.
6041 case Instruction::FNeg:
6042 case Instruction::Select:
6043 case Instruction::PHI:
6044 return false;
6045 case Instruction::Ret:
6046 return User->getFunction()->getAttributes().getRetNoFPClass() &
6047 FPClassTest::fcNan;
6048 case Instruction::Call:
6049 case Instruction::Invoke: {
6050 if (auto *II = dyn_cast<IntrinsicInst>(User)) {
6051 switch (II->getIntrinsicID()) {
6052 case Intrinsic::fabs:
6053 return true;
6054 case Intrinsic::copysign:
6055 return U.getOperandNo() == 0;
6056 // Other proper FP math intrinsics ignore the sign bit of NaN.
6057 case Intrinsic::maxnum:
6058 case Intrinsic::minnum:
6059 case Intrinsic::maximum:
6060 case Intrinsic::minimum:
6061 case Intrinsic::maximumnum:
6062 case Intrinsic::minimumnum:
6063 case Intrinsic::canonicalize:
6064 case Intrinsic::fma:
6065 case Intrinsic::fmuladd:
6066 case Intrinsic::sqrt:
6067 case Intrinsic::pow:
6068 case Intrinsic::powi:
6069 case Intrinsic::fptoui_sat:
6070 case Intrinsic::fptosi_sat:
6071 case Intrinsic::is_fpclass:
6072 case Intrinsic::vp_is_fpclass:
6073 return true;
6074 default:
6075 return false;
6076 }
6077 }
6078
6079 FPClassTest NoFPClass =
6080 cast<CallBase>(User)->getParamNoFPClass(U.getOperandNo());
6081 return NoFPClass & FPClassTest::fcNan;
6082 }
6083 default:
6084 return false;
6085 }
6086}
6087
6089
6090 // All byte-wide stores are splatable, even of arbitrary variables.
6091 if (V->getType()->isIntegerTy(8))
6092 return V;
6093
6094 LLVMContext &Ctx = V->getContext();
6095
6096 // Undef don't care.
6097 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6098 if (isa<UndefValue>(V))
6099 return UndefInt8;
6100
6101 // Return poison for zero-sized type.
6102 if (DL.getTypeStoreSize(V->getType()).isZero())
6103 return PoisonValue::get(Type::getInt8Ty(Ctx));
6104
6105 Constant *C = dyn_cast<Constant>(V);
6106 if (!C) {
6107 // Conceptually, we could handle things like:
6108 // %a = zext i8 %X to i16
6109 // %b = shl i16 %a, 8
6110 // %c = or i16 %a, %b
6111 // but until there is an example that actually needs this, it doesn't seem
6112 // worth worrying about.
6113 return nullptr;
6114 }
6115
6116 // Handle 'null' ConstantArrayZero etc.
6117 if (C->isNullValue())
6119
6120 // Constant floating-point values can be handled as integer values if the
6121 // corresponding integer value is "byteable". An important case is 0.0.
6122 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6123 Type *Ty = nullptr;
6124 if (CFP->getType()->isHalfTy())
6125 Ty = Type::getInt16Ty(Ctx);
6126 else if (CFP->getType()->isFloatTy())
6127 Ty = Type::getInt32Ty(Ctx);
6128 else if (CFP->getType()->isDoubleTy())
6129 Ty = Type::getInt64Ty(Ctx);
6130 // Don't handle long double formats, which have strange constraints.
6131 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6132 : nullptr;
6133 }
6134
6135 // We can handle constant integers that are multiple of 8 bits.
6136 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6137 if (CI->getBitWidth() % 8 == 0) {
6138 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6139 if (!CI->getValue().isSplat(8))
6140 return nullptr;
6141 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6142 }
6143 }
6144
6145 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6146 if (CE->getOpcode() == Instruction::IntToPtr) {
6147 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6148 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6150 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6151 return isBytewiseValue(Op, DL);
6152 }
6153 }
6154 }
6155
6156 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6157 if (LHS == RHS)
6158 return LHS;
6159 if (!LHS || !RHS)
6160 return nullptr;
6161 if (LHS == UndefInt8)
6162 return RHS;
6163 if (RHS == UndefInt8)
6164 return LHS;
6165 return nullptr;
6166 };
6167
6168 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
6169 Value *Val = UndefInt8;
6170 for (uint64_t I = 0, E = CA->getNumElements(); I != E; ++I)
6171 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6172 return nullptr;
6173 return Val;
6174 }
6175
6176 if (isa<ConstantAggregate>(C)) {
6177 Value *Val = UndefInt8;
6178 for (Value *Op : C->operands())
6179 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6180 return nullptr;
6181 return Val;
6182 }
6183
6184 // Don't try to handle the handful of other constants.
6185 return nullptr;
6186}
6187
6188// This is the recursive version of BuildSubAggregate. It takes a few different
6189// arguments. Idxs is the index within the nested struct From that we are
6190// looking at now (which is of type IndexedType). IdxSkip is the number of
6191// indices from Idxs that should be left out when inserting into the resulting
6192// struct. To is the result struct built so far, new insertvalue instructions
6193// build on that.
6194static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6196 unsigned IdxSkip,
6197 BasicBlock::iterator InsertBefore) {
6198 StructType *STy = dyn_cast<StructType>(IndexedType);
6199 if (STy) {
6200 // Save the original To argument so we can modify it
6201 Value *OrigTo = To;
6202 // General case, the type indexed by Idxs is a struct
6203 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6204 // Process each struct element recursively
6205 Idxs.push_back(i);
6206 Value *PrevTo = To;
6207 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6208 InsertBefore);
6209 Idxs.pop_back();
6210 if (!To) {
6211 // Couldn't find any inserted value for this index? Cleanup
6212 while (PrevTo != OrigTo) {
6213 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
6214 PrevTo = Del->getAggregateOperand();
6215 Del->eraseFromParent();
6216 }
6217 // Stop processing elements
6218 break;
6219 }
6220 }
6221 // If we successfully found a value for each of our subaggregates
6222 if (To)
6223 return To;
6224 }
6225 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6226 // the struct's elements had a value that was inserted directly. In the latter
6227 // case, perhaps we can't determine each of the subelements individually, but
6228 // we might be able to find the complete struct somewhere.
6229
6230 // Find the value that is at that particular spot
6231 Value *V = FindInsertedValue(From, Idxs);
6232
6233 if (!V)
6234 return nullptr;
6235
6236 // Insert the value in the new (sub) aggregate
6237 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6238 InsertBefore);
6239}
6240
6241// This helper takes a nested struct and extracts a part of it (which is again a
6242// struct) into a new value. For example, given the struct:
6243// { a, { b, { c, d }, e } }
6244// and the indices "1, 1" this returns
6245// { c, d }.
6246//
6247// It does this by inserting an insertvalue for each element in the resulting
6248// struct, as opposed to just inserting a single struct. This will only work if
6249// each of the elements of the substruct are known (ie, inserted into From by an
6250// insertvalue instruction somewhere).
6251//
6252// All inserted insertvalue instructions are inserted before InsertBefore
6254 BasicBlock::iterator InsertBefore) {
6255 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6256 idx_range);
6257 Value *To = PoisonValue::get(IndexedType);
6258 SmallVector<unsigned, 10> Idxs(idx_range);
6259 unsigned IdxSkip = Idxs.size();
6260
6261 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6262}
6263
6264/// Given an aggregate and a sequence of indices, see if the scalar value
6265/// indexed is already around as a register, for example if it was inserted
6266/// directly into the aggregate.
6267///
6268/// If InsertBefore is not null, this function will duplicate (modified)
6269/// insertvalues when a part of a nested struct is extracted.
6270Value *
6272 std::optional<BasicBlock::iterator> InsertBefore) {
6273 // Nothing to index? Just return V then (this is useful at the end of our
6274 // recursion).
6275 if (idx_range.empty())
6276 return V;
6277 // We have indices, so V should have an indexable type.
6278 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6279 "Not looking at a struct or array?");
6280 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6281 "Invalid indices for type?");
6282
6283 if (Constant *C = dyn_cast<Constant>(V)) {
6284 C = C->getAggregateElement(idx_range[0]);
6285 if (!C) return nullptr;
6286 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6287 }
6288
6289 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
6290 // Loop the indices for the insertvalue instruction in parallel with the
6291 // requested indices
6292 const unsigned *req_idx = idx_range.begin();
6293 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6294 i != e; ++i, ++req_idx) {
6295 if (req_idx == idx_range.end()) {
6296 // We can't handle this without inserting insertvalues
6297 if (!InsertBefore)
6298 return nullptr;
6299
6300 // The requested index identifies a part of a nested aggregate. Handle
6301 // this specially. For example,
6302 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6303 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6304 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6305 // This can be changed into
6306 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6307 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6308 // which allows the unused 0,0 element from the nested struct to be
6309 // removed.
6310 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6311 *InsertBefore);
6312 }
6313
6314 // This insert value inserts something else than what we are looking for.
6315 // See if the (aggregate) value inserted into has the value we are
6316 // looking for, then.
6317 if (*req_idx != *i)
6318 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6319 InsertBefore);
6320 }
6321 // If we end up here, the indices of the insertvalue match with those
6322 // requested (though possibly only partially). Now we recursively look at
6323 // the inserted value, passing any remaining indices.
6324 return FindInsertedValue(I->getInsertedValueOperand(),
6325 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6326 }
6327
6328 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
6329 // If we're extracting a value from an aggregate that was extracted from
6330 // something else, we can extract from that something else directly instead.
6331 // However, we will need to chain I's indices with the requested indices.
6332
6333 // Calculate the number of indices required
6334 unsigned size = I->getNumIndices() + idx_range.size();
6335 // Allocate some space to put the new indices in
6337 Idxs.reserve(size);
6338 // Add indices from the extract value instruction
6339 Idxs.append(I->idx_begin(), I->idx_end());
6340
6341 // Add requested indices
6342 Idxs.append(idx_range.begin(), idx_range.end());
6343
6344 assert(Idxs.size() == size
6345 && "Number of indices added not correct?");
6346
6347 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6348 }
6349 // Otherwise, we don't know (such as, extracting from a function return value
6350 // or load instruction)
6351 return nullptr;
6352}
6353
6354// If V refers to an initialized global constant, set Slice either to
6355// its initializer if the size of its elements equals ElementSize, or,
6356// for ElementSize == 8, to its representation as an array of unsiged
6357// char. Return true on success.
6358// Offset is in the unit "nr of ElementSize sized elements".
6361 unsigned ElementSize, uint64_t Offset) {
6362 assert(V && "V should not be null.");
6363 assert((ElementSize % 8) == 0 &&
6364 "ElementSize expected to be a multiple of the size of a byte.");
6365 unsigned ElementSizeInBytes = ElementSize / 8;
6366
6367 // Drill down into the pointer expression V, ignoring any intervening
6368 // casts, and determine the identity of the object it references along
6369 // with the cumulative byte offset into it.
6370 const GlobalVariable *GV =
6371 dyn_cast<GlobalVariable>(getUnderlyingObject(V));
6372 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6373 // Fail if V is not based on constant global object.
6374 return false;
6375
6376 const DataLayout &DL = GV->getDataLayout();
6377 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6378
6379 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6380 /*AllowNonInbounds*/ true))
6381 // Fail if a constant offset could not be determined.
6382 return false;
6383
6384 uint64_t StartIdx = Off.getLimitedValue();
6385 if (StartIdx == UINT64_MAX)
6386 // Fail if the constant offset is excessive.
6387 return false;
6388
6389 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6390 // elements. Simply bail out if that isn't possible.
6391 if ((StartIdx % ElementSizeInBytes) != 0)
6392 return false;
6393
6394 Offset += StartIdx / ElementSizeInBytes;
6395 ConstantDataArray *Array = nullptr;
6396 ArrayType *ArrayTy = nullptr;
6397
6398 if (GV->getInitializer()->isNullValue()) {
6399 Type *GVTy = GV->getValueType();
6400 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6401 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6402
6403 Slice.Array = nullptr;
6404 Slice.Offset = 0;
6405 // Return an empty Slice for undersized constants to let callers
6406 // transform even undefined library calls into simpler, well-defined
6407 // expressions. This is preferable to making the calls although it
6408 // prevents sanitizers from detecting such calls.
6409 Slice.Length = Length < Offset ? 0 : Length - Offset;
6410 return true;
6411 }
6412
6413 auto *Init = const_cast<Constant *>(GV->getInitializer());
6414 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6415 Type *InitElTy = ArrayInit->getElementType();
6416 if (InitElTy->isIntegerTy(ElementSize)) {
6417 // If Init is an initializer for an array of the expected type
6418 // and size, use it as is.
6419 Array = ArrayInit;
6420 ArrayTy = ArrayInit->getType();
6421 }
6422 }
6423
6424 if (!Array) {
6425 if (ElementSize != 8)
6426 // TODO: Handle conversions to larger integral types.
6427 return false;
6428
6429 // Otherwise extract the portion of the initializer starting
6430 // at Offset as an array of bytes, and reset Offset.
6432 if (!Init)
6433 return false;
6434
6435 Offset = 0;
6436 Array = dyn_cast<ConstantDataArray>(Init);
6437 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6438 }
6439
6440 uint64_t NumElts = ArrayTy->getArrayNumElements();
6441 if (Offset > NumElts)
6442 return false;
6443
6444 Slice.Array = Array;
6445 Slice.Offset = Offset;
6446 Slice.Length = NumElts - Offset;
6447 return true;
6448}
6449
6450/// Extract bytes from the initializer of the constant array V, which need
6451/// not be a nul-terminated string. On success, store the bytes in Str and
6452/// return true. When TrimAtNul is set, Str will contain only the bytes up
6453/// to but not including the first nul. Return false on failure.
6455 bool TrimAtNul) {
6457 if (!getConstantDataArrayInfo(V, Slice, 8))
6458 return false;
6459
6460 if (Slice.Array == nullptr) {
6461 if (TrimAtNul) {
6462 // Return a nul-terminated string even for an empty Slice. This is
6463 // safe because all existing SimplifyLibcalls callers require string
6464 // arguments and the behavior of the functions they fold is undefined
6465 // otherwise. Folding the calls this way is preferable to making
6466 // the undefined library calls, even though it prevents sanitizers
6467 // from reporting such calls.
6468 Str = StringRef();
6469 return true;
6470 }
6471 if (Slice.Length == 1) {
6472 Str = StringRef("", 1);
6473 return true;
6474 }
6475 // We cannot instantiate a StringRef as we do not have an appropriate string
6476 // of 0s at hand.
6477 return false;
6478 }
6479
6480 // Start out with the entire array in the StringRef.
6481 Str = Slice.Array->getAsString();
6482 // Skip over 'offset' bytes.
6483 Str = Str.substr(Slice.Offset);
6484
6485 if (TrimAtNul) {
6486 // Trim off the \0 and anything after it. If the array is not nul
6487 // terminated, we just return the whole end of string. The client may know
6488 // some other way that the string is length-bound.
6489 Str = Str.substr(0, Str.find('\0'));
6490 }
6491 return true;
6492}
6493
6494// These next two are very similar to the above, but also look through PHI
6495// nodes.
6496// TODO: See if we can integrate these two together.
6497
6498/// If we can compute the length of the string pointed to by
6499/// the specified pointer, return 'len+1'. If we can't, return 0.
6502 unsigned CharSize) {
6503 // Look through noop bitcast instructions.
6504 V = V->stripPointerCasts();
6505
6506 // If this is a PHI node, there are two cases: either we have already seen it
6507 // or we haven't.
6508 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6509 if (!PHIs.insert(PN).second)
6510 return ~0ULL; // already in the set.
6511
6512 // If it was new, see if all the input strings are the same length.
6513 uint64_t LenSoFar = ~0ULL;
6514 for (Value *IncValue : PN->incoming_values()) {
6515 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6516 if (Len == 0) return 0; // Unknown length -> unknown.
6517
6518 if (Len == ~0ULL) continue;
6519
6520 if (Len != LenSoFar && LenSoFar != ~0ULL)
6521 return 0; // Disagree -> unknown.
6522 LenSoFar = Len;
6523 }
6524
6525 // Success, all agree.
6526 return LenSoFar;
6527 }
6528
6529 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6530 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6531 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6532 if (Len1 == 0) return 0;
6533 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6534 if (Len2 == 0) return 0;
6535 if (Len1 == ~0ULL) return Len2;
6536 if (Len2 == ~0ULL) return Len1;
6537 if (Len1 != Len2) return 0;
6538 return Len1;
6539 }
6540
6541 // Otherwise, see if we can read the string.
6543 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6544 return 0;
6545
6546 if (Slice.Array == nullptr)
6547 // Zeroinitializer (including an empty one).
6548 return 1;
6549
6550 // Search for the first nul character. Return a conservative result even
6551 // when there is no nul. This is safe since otherwise the string function
6552 // being folded such as strlen is undefined, and can be preferable to
6553 // making the undefined library call.
6554 unsigned NullIndex = 0;
6555 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6556 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6557 break;
6558 }
6559
6560 return NullIndex + 1;
6561}
6562
6563/// If we can compute the length of the string pointed to by
6564/// the specified pointer, return 'len+1'. If we can't, return 0.
6565uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6566 if (!V->getType()->isPointerTy())
6567 return 0;
6568
6570 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6571 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6572 // an empty string as a length.
6573 return Len == ~0ULL ? 1 : Len;
6574}
6575
6576const Value *
6578 bool MustPreserveNullness) {
6579 assert(Call &&
6580 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6581 if (const Value *RV = Call->getReturnedArgOperand())
6582 return RV;
6583 // This can be used only as a aliasing property.
6585 Call, MustPreserveNullness))
6586 return Call->getArgOperand(0);
6587 return nullptr;
6588}
6589
6591 const CallBase *Call, bool MustPreserveNullness) {
6592 switch (Call->getIntrinsicID()) {
6593 case Intrinsic::launder_invariant_group:
6594 case Intrinsic::strip_invariant_group:
6595 case Intrinsic::aarch64_irg:
6596 case Intrinsic::aarch64_tagp:
6597 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6598 // input pointer (and thus preserve null-ness for the purposes of escape
6599 // analysis, which is where the MustPreserveNullness flag comes in to play).
6600 // However, it will not necessarily map ptr addrspace(N) null to ptr
6601 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6602 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6603 // list, no one should be relying on such a strict interpretation of
6604 // MustPreserveNullness (and, at time of writing, they are not), but we
6605 // document this fact out of an abundance of caution.
6606 case Intrinsic::amdgcn_make_buffer_rsrc:
6607 return true;
6608 case Intrinsic::ptrmask:
6609 return !MustPreserveNullness;
6610 case Intrinsic::threadlocal_address:
6611 // The underlying variable changes with thread ID. The Thread ID may change
6612 // at coroutine suspend points.
6613 return !Call->getParent()->getParent()->isPresplitCoroutine();
6614 default:
6615 return false;
6616 }
6617}
6618
6619/// \p PN defines a loop-variant pointer to an object. Check if the
6620/// previous iteration of the loop was referring to the same object as \p PN.
6622 const LoopInfo *LI) {
6623 // Find the loop-defined value.
6624 Loop *L = LI->getLoopFor(PN->getParent());
6625 if (PN->getNumIncomingValues() != 2)
6626 return true;
6627
6628 // Find the value from previous iteration.
6629 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6630 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6631 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6632 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6633 return true;
6634
6635 // If a new pointer is loaded in the loop, the pointer references a different
6636 // object in every iteration. E.g.:
6637 // for (i)
6638 // int *p = a[i];
6639 // ...
6640 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6641 if (!L->isLoopInvariant(Load->getPointerOperand()))
6642 return false;
6643 return true;
6644}
6645
6646const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6647 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6648 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6649 const Value *PtrOp = GEP->getPointerOperand();
6650 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6651 return V;
6652 V = PtrOp;
6653 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6654 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6655 Value *NewV = cast<Operator>(V)->getOperand(0);
6656 if (!NewV->getType()->isPointerTy())
6657 return V;
6658 V = NewV;
6659 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6660 if (GA->isInterposable())
6661 return V;
6662 V = GA->getAliasee();
6663 } else {
6664 if (auto *PHI = dyn_cast<PHINode>(V)) {
6665 // Look through single-arg phi nodes created by LCSSA.
6666 if (PHI->getNumIncomingValues() == 1) {
6667 V = PHI->getIncomingValue(0);
6668 continue;
6669 }
6670 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6671 // CaptureTracking can know about special capturing properties of some
6672 // intrinsics like launder.invariant.group, that can't be expressed with
6673 // the attributes, but have properties like returning aliasing pointer.
6674 // Because some analysis may assume that nocaptured pointer is not
6675 // returned from some special intrinsic (because function would have to
6676 // be marked with returns attribute), it is crucial to use this function
6677 // because it should be in sync with CaptureTracking. Not using it may
6678 // cause weird miscompilations where 2 aliasing pointers are assumed to
6679 // noalias.
6680 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6681 V = RP;
6682 continue;
6683 }
6684 }
6685
6686 return V;
6687 }
6688 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6689 }
6690 return V;
6691}
6692
6695 const LoopInfo *LI, unsigned MaxLookup) {
6698 Worklist.push_back(V);
6699 do {
6700 const Value *P = Worklist.pop_back_val();
6701 P = getUnderlyingObject(P, MaxLookup);
6702
6703 if (!Visited.insert(P).second)
6704 continue;
6705
6706 if (auto *SI = dyn_cast<SelectInst>(P)) {
6707 Worklist.push_back(SI->getTrueValue());
6708 Worklist.push_back(SI->getFalseValue());
6709 continue;
6710 }
6711
6712 if (auto *PN = dyn_cast<PHINode>(P)) {
6713 // If this PHI changes the underlying object in every iteration of the
6714 // loop, don't look through it. Consider:
6715 // int **A;
6716 // for (i) {
6717 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6718 // Curr = A[i];
6719 // *Prev, *Curr;
6720 //
6721 // Prev is tracking Curr one iteration behind so they refer to different
6722 // underlying objects.
6723 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6725 append_range(Worklist, PN->incoming_values());
6726 else
6727 Objects.push_back(P);
6728 continue;
6729 }
6730
6731 Objects.push_back(P);
6732 } while (!Worklist.empty());
6733}
6734
6736 const unsigned MaxVisited = 8;
6737
6740 Worklist.push_back(V);
6741 const Value *Object = nullptr;
6742 // Used as fallback if we can't find a common underlying object through
6743 // recursion.
6744 bool First = true;
6745 const Value *FirstObject = getUnderlyingObject(V);
6746 do {
6747 const Value *P = Worklist.pop_back_val();
6748 P = First ? FirstObject : getUnderlyingObject(P);
6749 First = false;
6750
6751 if (!Visited.insert(P).second)
6752 continue;
6753
6754 if (Visited.size() == MaxVisited)
6755 return FirstObject;
6756
6757 if (auto *SI = dyn_cast<SelectInst>(P)) {
6758 Worklist.push_back(SI->getTrueValue());
6759 Worklist.push_back(SI->getFalseValue());
6760 continue;
6761 }
6762
6763 if (auto *PN = dyn_cast<PHINode>(P)) {
6764 append_range(Worklist, PN->incoming_values());
6765 continue;
6766 }
6767
6768 if (!Object)
6769 Object = P;
6770 else if (Object != P)
6771 return FirstObject;
6772 } while (!Worklist.empty());
6773
6774 return Object ? Object : FirstObject;
6775}
6776
6777/// This is the function that does the work of looking through basic
6778/// ptrtoint+arithmetic+inttoptr sequences.
6779static const Value *getUnderlyingObjectFromInt(const Value *V) {
6780 do {
6781 if (const Operator *U = dyn_cast<Operator>(V)) {
6782 // If we find a ptrtoint, we can transfer control back to the
6783 // regular getUnderlyingObjectFromInt.
6784 if (U->getOpcode() == Instruction::PtrToInt)
6785 return U->getOperand(0);
6786 // If we find an add of a constant, a multiplied value, or a phi, it's
6787 // likely that the other operand will lead us to the base
6788 // object. We don't have to worry about the case where the
6789 // object address is somehow being computed by the multiply,
6790 // because our callers only care when the result is an
6791 // identifiable object.
6792 if (U->getOpcode() != Instruction::Add ||
6793 (!isa<ConstantInt>(U->getOperand(1)) &&
6794 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6795 !isa<PHINode>(U->getOperand(1))))
6796 return V;
6797 V = U->getOperand(0);
6798 } else {
6799 return V;
6800 }
6801 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6802 } while (true);
6803}
6804
6805/// This is a wrapper around getUnderlyingObjects and adds support for basic
6806/// ptrtoint+arithmetic+inttoptr sequences.
6807/// It returns false if unidentified object is found in getUnderlyingObjects.
6809 SmallVectorImpl<Value *> &Objects) {
6811 SmallVector<const Value *, 4> Working(1, V);
6812 do {
6813 V = Working.pop_back_val();
6814
6816 getUnderlyingObjects(V, Objs);
6817
6818 for (const Value *V : Objs) {
6819 if (!Visited.insert(V).second)
6820 continue;
6821 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6822 const Value *O =
6823 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6824 if (O->getType()->isPointerTy()) {
6825 Working.push_back(O);
6826 continue;
6827 }
6828 }
6829 // If getUnderlyingObjects fails to find an identifiable object,
6830 // getUnderlyingObjectsForCodeGen also fails for safety.
6831 if (!isIdentifiedObject(V)) {
6832 Objects.clear();
6833 return false;
6834 }
6835 Objects.push_back(const_cast<Value *>(V));
6836 }
6837 } while (!Working.empty());
6838 return true;
6839}
6840
6842 AllocaInst *Result = nullptr;
6844 SmallVector<Value *, 4> Worklist;
6845
6846 auto AddWork = [&](Value *V) {
6847 if (Visited.insert(V).second)
6848 Worklist.push_back(V);
6849 };
6850
6851 AddWork(V);
6852 do {
6853 V = Worklist.pop_back_val();
6854 assert(Visited.count(V));
6855
6856 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6857 if (Result && Result != AI)
6858 return nullptr;
6859 Result = AI;
6860 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
6861 AddWork(CI->getOperand(0));
6862 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
6863 for (Value *IncValue : PN->incoming_values())
6864 AddWork(IncValue);
6865 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
6866 AddWork(SI->getTrueValue());
6867 AddWork(SI->getFalseValue());
6868 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
6869 if (OffsetZero && !GEP->hasAllZeroIndices())
6870 return nullptr;
6871 AddWork(GEP->getPointerOperand());
6872 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
6873 Value *Returned = CB->getReturnedArgOperand();
6874 if (Returned)
6875 AddWork(Returned);
6876 else
6877 return nullptr;
6878 } else {
6879 return nullptr;
6880 }
6881 } while (!Worklist.empty());
6882
6883 return Result;
6884}
6885
6887 const Value *V, bool AllowLifetime, bool AllowDroppable) {
6888 for (const User *U : V->users()) {
6889 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
6890 if (!II)
6891 return false;
6892
6893 if (AllowLifetime && II->isLifetimeStartOrEnd())
6894 continue;
6895
6896 if (AllowDroppable && II->isDroppable())
6897 continue;
6898
6899 return false;
6900 }
6901 return true;
6902}
6903
6906 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
6907}
6910 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
6911}
6912
6914 if (auto *II = dyn_cast<IntrinsicInst>(I))
6915 return isTriviallyVectorizable(II->getIntrinsicID());
6916 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
6917 return (!Shuffle || Shuffle->isSelect()) &&
6918 !isa<CallBase, BitCastInst, ExtractElementInst>(I);
6919}
6920
6922 const Instruction *Inst, const Instruction *CtxI, AssumptionCache *AC,
6923 const DominatorTree *DT, const TargetLibraryInfo *TLI, bool UseVariableInfo,
6924 bool IgnoreUBImplyingAttrs) {
6925 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
6926 AC, DT, TLI, UseVariableInfo,
6927 IgnoreUBImplyingAttrs);
6928}
6929
6931 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
6932 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
6933 bool UseVariableInfo, bool IgnoreUBImplyingAttrs) {
6934#ifndef NDEBUG
6935 if (Inst->getOpcode() != Opcode) {
6936 // Check that the operands are actually compatible with the Opcode override.
6937 auto hasEqualReturnAndLeadingOperandTypes =
6938 [](const Instruction *Inst, unsigned NumLeadingOperands) {
6939 if (Inst->getNumOperands() < NumLeadingOperands)
6940 return false;
6941 const Type *ExpectedType = Inst->getType();
6942 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
6943 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
6944 return false;
6945 return true;
6946 };
6948 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
6949 assert(!Instruction::isUnaryOp(Opcode) ||
6950 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
6951 }
6952#endif
6953
6954 switch (Opcode) {
6955 default:
6956 return true;
6957 case Instruction::UDiv:
6958 case Instruction::URem: {
6959 // x / y is undefined if y == 0.
6960 const APInt *V;
6961 if (match(Inst->getOperand(1), m_APInt(V)))
6962 return *V != 0;
6963 return false;
6964 }
6965 case Instruction::SDiv:
6966 case Instruction::SRem: {
6967 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
6968 const APInt *Numerator, *Denominator;
6969 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
6970 return false;
6971 // We cannot hoist this division if the denominator is 0.
6972 if (*Denominator == 0)
6973 return false;
6974 // It's safe to hoist if the denominator is not 0 or -1.
6975 if (!Denominator->isAllOnes())
6976 return true;
6977 // At this point we know that the denominator is -1. It is safe to hoist as
6978 // long we know that the numerator is not INT_MIN.
6979 if (match(Inst->getOperand(0), m_APInt(Numerator)))
6980 return !Numerator->isMinSignedValue();
6981 // The numerator *might* be MinSignedValue.
6982 return false;
6983 }
6984 case Instruction::Load: {
6985 if (!UseVariableInfo)
6986 return false;
6987
6988 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
6989 if (!LI)
6990 return false;
6991 if (mustSuppressSpeculation(*LI))
6992 return false;
6993 const DataLayout &DL = LI->getDataLayout();
6995 LI->getType(), LI->getAlign(), DL,
6996 CtxI, AC, DT, TLI);
6997 }
6998 case Instruction::Call: {
6999 auto *CI = dyn_cast<const CallInst>(Inst);
7000 if (!CI)
7001 return false;
7002 const Function *Callee = CI->getCalledFunction();
7003
7004 // The called function could have undefined behavior or side-effects, even
7005 // if marked readnone nounwind.
7006 if (!Callee || !Callee->isSpeculatable())
7007 return false;
7008 // Since the operands may be changed after hoisting, undefined behavior may
7009 // be triggered by some UB-implying attributes.
7010 return IgnoreUBImplyingAttrs || !CI->hasUBImplyingAttrs();
7011 }
7012 case Instruction::VAArg:
7013 case Instruction::Alloca:
7014 case Instruction::Invoke:
7015 case Instruction::CallBr:
7016 case Instruction::PHI:
7017 case Instruction::Store:
7018 case Instruction::Ret:
7019 case Instruction::Br:
7020 case Instruction::IndirectBr:
7021 case Instruction::Switch:
7022 case Instruction::Unreachable:
7023 case Instruction::Fence:
7024 case Instruction::AtomicRMW:
7025 case Instruction::AtomicCmpXchg:
7026 case Instruction::LandingPad:
7027 case Instruction::Resume:
7028 case Instruction::CatchSwitch:
7029 case Instruction::CatchPad:
7030 case Instruction::CatchRet:
7031 case Instruction::CleanupPad:
7032 case Instruction::CleanupRet:
7033 return false; // Misc instructions which have effects
7034 }
7035}
7036
7038 if (I.mayReadOrWriteMemory())
7039 // Memory dependency possible
7040 return true;
7042 // Can't move above a maythrow call or infinite loop. Or if an
7043 // inalloca alloca, above a stacksave call.
7044 return true;
7046 // 1) Can't reorder two inf-loop calls, even if readonly
7047 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7048 // safe to speculative execute. (Inverse of above)
7049 return true;
7050 return false;
7051}
7052
7053/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7055 switch (OR) {
7056 case ConstantRange::OverflowResult::MayOverflow:
7057 return OverflowResult::MayOverflow;
7058 case ConstantRange::OverflowResult::AlwaysOverflowsLow:
7059 return OverflowResult::AlwaysOverflowsLow;
7060 case ConstantRange::OverflowResult::AlwaysOverflowsHigh:
7061 return OverflowResult::AlwaysOverflowsHigh;
7062 case ConstantRange::OverflowResult::NeverOverflows:
7063 return OverflowResult::NeverOverflows;
7064 }
7065 llvm_unreachable("Unknown OverflowResult");
7066}
7067
7068/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7071 bool ForSigned,
7072 const SimplifyQuery &SQ) {
7073 ConstantRange CR1 =
7074 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7075 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7078 return CR1.intersectWith(CR2, RangeType);
7079}
7080
7082 const Value *RHS,
7083 const SimplifyQuery &SQ,
7084 bool IsNSW) {
7085 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7086 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7087
7088 // mul nsw of two non-negative numbers is also nuw.
7089 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
7090 return OverflowResult::NeverOverflows;
7091
7092 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
7093 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
7094 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7095}
7096
7098 const Value *RHS,
7099 const SimplifyQuery &SQ) {
7100 // Multiplying n * m significant bits yields a result of n + m significant
7101 // bits. If the total number of significant bits does not exceed the
7102 // result bit width (minus 1), there is no overflow.
7103 // This means if we have enough leading sign bits in the operands
7104 // we can guarantee that the result does not overflow.
7105 // Ref: "Hacker's Delight" by Henry Warren
7106 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7107
7108 // Note that underestimating the number of sign bits gives a more
7109 // conservative answer.
7110 unsigned SignBits =
7112
7113 // First handle the easy case: if we have enough sign bits there's
7114 // definitely no overflow.
7115 if (SignBits > BitWidth + 1)
7116 return OverflowResult::NeverOverflows;
7117
7118 // There are two ambiguous cases where there can be no overflow:
7119 // SignBits == BitWidth + 1 and
7120 // SignBits == BitWidth
7121 // The second case is difficult to check, therefore we only handle the
7122 // first case.
7123 if (SignBits == BitWidth + 1) {
7124 // It overflows only when both arguments are negative and the true
7125 // product is exactly the minimum negative number.
7126 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7127 // For simplicity we just check if at least one side is not negative.
7128 KnownBits LHSKnown = computeKnownBits(LHS, SQ);
7129 KnownBits RHSKnown = computeKnownBits(RHS, SQ);
7130 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7131 return OverflowResult::NeverOverflows;
7132 }
7133 return OverflowResult::MayOverflow;
7134}
7135
7138 const WithCache<const Value *> &RHS,
7139 const SimplifyQuery &SQ) {
7140 ConstantRange LHSRange =
7141 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7142 ConstantRange RHSRange =
7143 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7144 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7145}
7146
7147static OverflowResult
7149 const WithCache<const Value *> &RHS,
7150 const AddOperator *Add, const SimplifyQuery &SQ) {
7151 if (Add && Add->hasNoSignedWrap()) {
7152 return OverflowResult::NeverOverflows;
7153 }
7154
7155 // If LHS and RHS each have at least two sign bits, the addition will look
7156 // like
7157 //
7158 // XX..... +
7159 // YY.....
7160 //
7161 // If the carry into the most significant position is 0, X and Y can't both
7162 // be 1 and therefore the carry out of the addition is also 0.
7163 //
7164 // If the carry into the most significant position is 1, X and Y can't both
7165 // be 0 and therefore the carry out of the addition is also 1.
7166 //
7167 // Since the carry into the most significant position is always equal to
7168 // the carry out of the addition, there is no signed overflow.
7169 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7170 return OverflowResult::NeverOverflows;
7171
7172 ConstantRange LHSRange =
7173 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7174 ConstantRange RHSRange =
7175 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7176 OverflowResult OR =
7177 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7178 if (OR != OverflowResult::MayOverflow)
7179 return OR;
7180
7181 // The remaining code needs Add to be available. Early returns if not so.
7182 if (!Add)
7183 return OverflowResult::MayOverflow;
7184
7185 // If the sign of Add is the same as at least one of the operands, this add
7186 // CANNOT overflow. If this can be determined from the known bits of the
7187 // operands the above signedAddMayOverflow() check will have already done so.
7188 // The only other way to improve on the known bits is from an assumption, so
7189 // call computeKnownBitsFromContext() directly.
7190 bool LHSOrRHSKnownNonNegative =
7191 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7192 bool LHSOrRHSKnownNegative =
7193 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7194 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7195 KnownBits AddKnown(LHSRange.getBitWidth());
7196 computeKnownBitsFromContext(Add, AddKnown, SQ);
7197 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7198 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7199 return OverflowResult::NeverOverflows;
7200 }
7201
7202 return OverflowResult::MayOverflow;
7203}
7204
7206 const Value *RHS,
7207 const SimplifyQuery &SQ) {
7208 // X - (X % ?)
7209 // The remainder of a value can't have greater magnitude than itself,
7210 // so the subtraction can't overflow.
7211
7212 // X - (X -nuw ?)
7213 // In the minimal case, this would simplify to "?", so there's no subtract
7214 // at all. But if this analysis is used to peek through casts, for example,
7215 // then determining no-overflow may allow other transforms.
7216
7217 // TODO: There are other patterns like this.
7218 // See simplifyICmpWithBinOpOnLHS() for candidates.
7219 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7221 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7222 return OverflowResult::NeverOverflows;
7223
7225 SQ.DL)) {
7226 if (*C)
7227 return OverflowResult::NeverOverflows;
7228 return OverflowResult::AlwaysOverflowsLow;
7229 }
7230
7231 ConstantRange LHSRange =
7232 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7233 ConstantRange RHSRange =
7234 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7235 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7236}
7237
7239 const Value *RHS,
7240 const SimplifyQuery &SQ) {
7241 // X - (X % ?)
7242 // The remainder of a value can't have greater magnitude than itself,
7243 // so the subtraction can't overflow.
7244
7245 // X - (X -nsw ?)
7246 // In the minimal case, this would simplify to "?", so there's no subtract
7247 // at all. But if this analysis is used to peek through casts, for example,
7248 // then determining no-overflow may allow other transforms.
7249 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7251 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7252 return OverflowResult::NeverOverflows;
7253
7254 // If LHS and RHS each have at least two sign bits, the subtraction
7255 // cannot overflow.
7256 if (::ComputeNumSignBits(LHS, SQ) > 1 && ::ComputeNumSignBits(RHS, SQ) > 1)
7257 return OverflowResult::NeverOverflows;
7258
7259 ConstantRange LHSRange =
7260 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7261 ConstantRange RHSRange =
7262 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7263 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7264}
7265
7267 const DominatorTree &DT) {
7268 SmallVector<const BranchInst *, 2> GuardingBranches;
7270
7271 for (const User *U : WO->users()) {
7272 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7273 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7274
7275 if (EVI->getIndices()[0] == 0)
7276 Results.push_back(EVI);
7277 else {
7278 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7279
7280 for (const auto *U : EVI->users())
7281 if (const auto *B = dyn_cast<BranchInst>(U)) {
7282 assert(B->isConditional() && "How else is it using an i1?");
7283 GuardingBranches.push_back(B);
7284 }
7285 }
7286 } else {
7287 // We are using the aggregate directly in a way we don't want to analyze
7288 // here (storing it to a global, say).
7289 return false;
7290 }
7291 }
7292
7293 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7294 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7295 if (!NoWrapEdge.isSingleEdge())
7296 return false;
7297
7298 // Check if all users of the add are provably no-wrap.
7299 for (const auto *Result : Results) {
7300 // If the extractvalue itself is not executed on overflow, the we don't
7301 // need to check each use separately, since domination is transitive.
7302 if (DT.dominates(NoWrapEdge, Result->getParent()))
7303 continue;
7304
7305 for (const auto &RU : Result->uses())
7306 if (!DT.dominates(NoWrapEdge, RU))
7307 return false;
7308 }
7309
7310 return true;
7311 };
7312
7313 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7314}
7315
7316/// Shifts return poison if shiftwidth is larger than the bitwidth.
7317static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7318 auto *C = dyn_cast<Constant>(ShiftAmount);
7319 if (!C)
7320 return false;
7321
7322 // Shifts return poison if shiftwidth is larger than the bitwidth.
7324 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7325 unsigned NumElts = FVTy->getNumElements();
7326 for (unsigned i = 0; i < NumElts; ++i)
7327 ShiftAmounts.push_back(C->getAggregateElement(i));
7328 } else if (isa<ScalableVectorType>(C->getType()))
7329 return false; // Can't tell, just return false to be safe
7330 else
7331 ShiftAmounts.push_back(C);
7332
7333 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7334 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7335 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7336 });
7337
7338 return Safe;
7339}
7340
7342 PoisonOnly = (1 << 0),
7343 UndefOnly = (1 << 1),
7345};
7346
7348 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7349}
7350
7352 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7353}
7354
7356 bool ConsiderFlagsAndMetadata) {
7357
7358 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7359 Op->hasPoisonGeneratingAnnotations())
7360 return true;
7361
7362 unsigned Opcode = Op->getOpcode();
7363
7364 // Check whether opcode is a poison/undef-generating operation
7365 switch (Opcode) {
7366 case Instruction::Shl:
7367 case Instruction::AShr:
7368 case Instruction::LShr:
7369 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7370 case Instruction::FPToSI:
7371 case Instruction::FPToUI:
7372 // fptosi/ui yields poison if the resulting value does not fit in the
7373 // destination type.
7374 return true;
7375 case Instruction::Call:
7376 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7377 switch (II->getIntrinsicID()) {
7378 // TODO: Add more intrinsics.
7379 case Intrinsic::ctlz:
7380 case Intrinsic::cttz:
7381 case Intrinsic::abs:
7382 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7383 return false;
7384 break;
7385 case Intrinsic::ctpop:
7386 case Intrinsic::bswap:
7387 case Intrinsic::bitreverse:
7388 case Intrinsic::fshl:
7389 case Intrinsic::fshr:
7390 case Intrinsic::smax:
7391 case Intrinsic::smin:
7392 case Intrinsic::scmp:
7393 case Intrinsic::umax:
7394 case Intrinsic::umin:
7395 case Intrinsic::ucmp:
7396 case Intrinsic::ptrmask:
7397 case Intrinsic::fptoui_sat:
7398 case Intrinsic::fptosi_sat:
7399 case Intrinsic::sadd_with_overflow:
7400 case Intrinsic::ssub_with_overflow:
7401 case Intrinsic::smul_with_overflow:
7402 case Intrinsic::uadd_with_overflow:
7403 case Intrinsic::usub_with_overflow:
7404 case Intrinsic::umul_with_overflow:
7405 case Intrinsic::sadd_sat:
7406 case Intrinsic::uadd_sat:
7407 case Intrinsic::ssub_sat:
7408 case Intrinsic::usub_sat:
7409 return false;
7410 case Intrinsic::sshl_sat:
7411 case Intrinsic::ushl_sat:
7412 return includesPoison(Kind) &&
7413 !shiftAmountKnownInRange(II->getArgOperand(1));
7414 case Intrinsic::fma:
7415 case Intrinsic::fmuladd:
7416 case Intrinsic::sqrt:
7417 case Intrinsic::powi:
7418 case Intrinsic::sin:
7419 case Intrinsic::cos:
7420 case Intrinsic::pow:
7421 case Intrinsic::log:
7422 case Intrinsic::log10:
7423 case Intrinsic::log2:
7424 case Intrinsic::exp:
7425 case Intrinsic::exp2:
7426 case Intrinsic::exp10:
7427 case Intrinsic::fabs:
7428 case Intrinsic::copysign:
7429 case Intrinsic::floor:
7430 case Intrinsic::ceil:
7431 case Intrinsic::trunc:
7432 case Intrinsic::rint:
7433 case Intrinsic::nearbyint:
7434 case Intrinsic::round:
7435 case Intrinsic::roundeven:
7436 case Intrinsic::fptrunc_round:
7437 case Intrinsic::canonicalize:
7438 case Intrinsic::arithmetic_fence:
7439 case Intrinsic::minnum:
7440 case Intrinsic::maxnum:
7441 case Intrinsic::minimum:
7442 case Intrinsic::maximum:
7443 case Intrinsic::minimumnum:
7444 case Intrinsic::maximumnum:
7445 case Intrinsic::is_fpclass:
7446 case Intrinsic::ldexp:
7447 case Intrinsic::frexp:
7448 return false;
7449 case Intrinsic::lround:
7450 case Intrinsic::llround:
7451 case Intrinsic::lrint:
7452 case Intrinsic::llrint:
7453 // If the value doesn't fit an unspecified value is returned (but this
7454 // is not poison).
7455 return false;
7456 }
7457 }
7458 [[fallthrough]];
7459 case Instruction::CallBr:
7460 case Instruction::Invoke: {
7461 const auto *CB = cast<CallBase>(Op);
7462 return !CB->hasRetAttr(Attribute::NoUndef);
7463 }
7464 case Instruction::InsertElement:
7465 case Instruction::ExtractElement: {
7466 // If index exceeds the length of the vector, it returns poison
7467 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7468 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7469 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7470 if (includesPoison(Kind))
7471 return !Idx ||
7472 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7473 return false;
7474 }
7475 case Instruction::ShuffleVector: {
7476 ArrayRef<int> Mask = isa<ConstantExpr>(Op)
7477 ? cast<ConstantExpr>(Op)->getShuffleMask()
7478 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7479 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7480 }
7481 case Instruction::FNeg:
7482 case Instruction::PHI:
7483 case Instruction::Select:
7484 case Instruction::ExtractValue:
7485 case Instruction::InsertValue:
7486 case Instruction::Freeze:
7487 case Instruction::ICmp:
7488 case Instruction::FCmp:
7489 case Instruction::GetElementPtr:
7490 return false;
7491 case Instruction::AddrSpaceCast:
7492 return true;
7493 default: {
7494 const auto *CE = dyn_cast<ConstantExpr>(Op);
7495 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7496 return false;
7497 else if (Instruction::isBinaryOp(Opcode))
7498 return false;
7499 // Be conservative and return true.
7500 return true;
7501 }
7502 }
7503}
7504
7506 bool ConsiderFlagsAndMetadata) {
7507 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7508 ConsiderFlagsAndMetadata);
7509}
7510
7511bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7512 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7513 ConsiderFlagsAndMetadata);
7514}
7515
7516static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7517 unsigned Depth) {
7518 if (ValAssumedPoison == V)
7519 return true;
7520
7521 const unsigned MaxDepth = 2;
7522 if (Depth >= MaxDepth)
7523 return false;
7524
7525 if (const auto *I = dyn_cast<Instruction>(V)) {
7526 if (any_of(I->operands(), [=](const Use &Op) {
7527 return propagatesPoison(Op) &&
7528 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7529 }))
7530 return true;
7531
7532 // V = extractvalue V0, idx
7533 // V2 = extractvalue V0, idx2
7534 // V0's elements are all poison or not. (e.g., add_with_overflow)
7535 const WithOverflowInst *II;
7537 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7538 llvm::is_contained(II->args(), ValAssumedPoison)))
7539 return true;
7540 }
7541 return false;
7542}
7543
7544static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7545 unsigned Depth) {
7546 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7547 return true;
7548
7549 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7550 return true;
7551
7552 const unsigned MaxDepth = 2;
7553 if (Depth >= MaxDepth)
7554 return false;
7555
7556 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7557 if (I && !canCreatePoison(cast<Operator>(I))) {
7558 return all_of(I->operands(), [=](const Value *Op) {
7559 return impliesPoison(Op, V, Depth + 1);
7560 });
7561 }
7562 return false;
7563}
7564
7565bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7566 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7567}
7568
7569static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7570
7572 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7573 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7575 return false;
7576
7577 if (isa<MetadataAsValue>(V))
7578 return false;
7579
7580 if (const auto *A = dyn_cast<Argument>(V)) {
7581 if (A->hasAttribute(Attribute::NoUndef) ||
7582 A->hasAttribute(Attribute::Dereferenceable) ||
7583 A->hasAttribute(Attribute::DereferenceableOrNull))
7584 return true;
7585 }
7586
7587 if (auto *C = dyn_cast<Constant>(V)) {
7588 if (isa<PoisonValue>(C))
7589 return !includesPoison(Kind);
7590
7591 if (isa<UndefValue>(C))
7592 return !includesUndef(Kind);
7593
7594 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(C) ||
7595 isa<ConstantPointerNull>(C) || isa<Function>(C))
7596 return true;
7597
7598 if (C->getType()->isVectorTy()) {
7599 if (isa<ConstantExpr>(C)) {
7600 // Scalable vectors can use a ConstantExpr to build a splat.
7601 if (Constant *SplatC = C->getSplatValue())
7602 if (isa<ConstantInt>(SplatC) || isa<ConstantFP>(SplatC))
7603 return true;
7604 } else {
7605 if (includesUndef(Kind) && C->containsUndefElement())
7606 return false;
7607 if (includesPoison(Kind) && C->containsPoisonElement())
7608 return false;
7609 return !C->containsConstantExpression();
7610 }
7611 }
7612 }
7613
7614 // Strip cast operations from a pointer value.
7615 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7616 // inbounds with zero offset. To guarantee that the result isn't poison, the
7617 // stripped pointer is checked as it has to be pointing into an allocated
7618 // object or be null `null` to ensure `inbounds` getelement pointers with a
7619 // zero offset could not produce poison.
7620 // It can strip off addrspacecast that do not change bit representation as
7621 // well. We believe that such addrspacecast is equivalent to no-op.
7622 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7623 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7624 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7625 return true;
7626
7627 auto OpCheck = [&](const Value *V) {
7628 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7629 };
7630
7631 if (auto *Opr = dyn_cast<Operator>(V)) {
7632 // If the value is a freeze instruction, then it can never
7633 // be undef or poison.
7634 if (isa<FreezeInst>(V))
7635 return true;
7636
7637 if (const auto *CB = dyn_cast<CallBase>(V)) {
7638 if (CB->hasRetAttr(Attribute::NoUndef) ||
7639 CB->hasRetAttr(Attribute::Dereferenceable) ||
7640 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7641 return true;
7642 }
7643
7644 if (const auto *PN = dyn_cast<PHINode>(V)) {
7645 unsigned Num = PN->getNumIncomingValues();
7646 bool IsWellDefined = true;
7647 for (unsigned i = 0; i < Num; ++i) {
7648 if (PN == PN->getIncomingValue(i))
7649 continue;
7650 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7651 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7652 DT, Depth + 1, Kind)) {
7653 IsWellDefined = false;
7654 break;
7655 }
7656 }
7657 if (IsWellDefined)
7658 return true;
7659 } else if (!::canCreateUndefOrPoison(Opr, Kind,
7660 /*ConsiderFlagsAndMetadata*/ true) &&
7661 all_of(Opr->operands(), OpCheck))
7662 return true;
7663 }
7664
7665 if (auto *I = dyn_cast<LoadInst>(V))
7666 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7667 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7668 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7669 return true;
7670
7672 return true;
7673
7674 // CxtI may be null or a cloned instruction.
7675 if (!CtxI || !CtxI->getParent() || !DT)
7676 return false;
7677
7678 auto *DNode = DT->getNode(CtxI->getParent());
7679 if (!DNode)
7680 // Unreachable block
7681 return false;
7682
7683 // If V is used as a branch condition before reaching CtxI, V cannot be
7684 // undef or poison.
7685 // br V, BB1, BB2
7686 // BB1:
7687 // CtxI ; V cannot be undef or poison here
7688 auto *Dominator = DNode->getIDom();
7689 // This check is purely for compile time reasons: we can skip the IDom walk
7690 // if what we are checking for includes undef and the value is not an integer.
7691 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7692 while (Dominator) {
7693 auto *TI = Dominator->getBlock()->getTerminator();
7694
7695 Value *Cond = nullptr;
7696 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7697 if (BI->isConditional())
7698 Cond = BI->getCondition();
7699 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7700 Cond = SI->getCondition();
7701 }
7702
7703 if (Cond) {
7704 if (Cond == V)
7705 return true;
7706 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7707 // For poison, we can analyze further
7708 auto *Opr = cast<Operator>(Cond);
7709 if (any_of(Opr->operands(), [V](const Use &U) {
7710 return V == U && propagatesPoison(U);
7711 }))
7712 return true;
7713 }
7714 }
7715
7716 Dominator = Dominator->getIDom();
7717 }
7718
7719 if (AC && getKnowledgeValidInContext(V, {Attribute::NoUndef}, *AC, CtxI, DT))
7720 return true;
7721
7722 return false;
7723}
7724
7726 const Instruction *CtxI,
7727 const DominatorTree *DT,
7728 unsigned Depth) {
7729 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7731}
7732
7734 const Instruction *CtxI,
7735 const DominatorTree *DT, unsigned Depth) {
7736 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7738}
7739
7741 const Instruction *CtxI,
7742 const DominatorTree *DT, unsigned Depth) {
7743 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7745}
7746
7747/// Return true if undefined behavior would provably be executed on the path to
7748/// OnPathTo if Root produced a posion result. Note that this doesn't say
7749/// anything about whether OnPathTo is actually executed or whether Root is
7750/// actually poison. This can be used to assess whether a new use of Root can
7751/// be added at a location which is control equivalent with OnPathTo (such as
7752/// immediately before it) without introducing UB which didn't previously
7753/// exist. Note that a false result conveys no information.
7755 Instruction *OnPathTo,
7756 DominatorTree *DT) {
7757 // Basic approach is to assume Root is poison, propagate poison forward
7758 // through all users we can easily track, and then check whether any of those
7759 // users are provable UB and must execute before out exiting block might
7760 // exit.
7761
7762 // The set of all recursive users we've visited (which are assumed to all be
7763 // poison because of said visit)
7766 Worklist.push_back(Root);
7767 while (!Worklist.empty()) {
7768 const Instruction *I = Worklist.pop_back_val();
7769
7770 // If we know this must trigger UB on a path leading our target.
7771 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7772 return true;
7773
7774 // If we can't analyze propagation through this instruction, just skip it
7775 // and transitive users. Safe as false is a conservative result.
7776 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7777 return KnownPoison.contains(U) && propagatesPoison(U);
7778 }))
7779 continue;
7780
7781 if (KnownPoison.insert(I).second)
7782 for (const User *User : I->users())
7783 Worklist.push_back(cast<Instruction>(User));
7784 }
7785
7786 // Might be non-UB, or might have a path we couldn't prove must execute on
7787 // way to exiting bb.
7788 return false;
7789}
7790
7792 const SimplifyQuery &SQ) {
7793 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7794 Add, SQ);
7795}
7796
7799 const WithCache<const Value *> &RHS,
7800 const SimplifyQuery &SQ) {
7801 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7802}
7803
7805 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7806 // of time because it's possible for another thread to interfere with it for an
7807 // arbitrary length of time, but programs aren't allowed to rely on that.
7808
7809 // If there is no successor, then execution can't transfer to it.
7810 if (isa<ReturnInst>(I))
7811 return false;
7812 if (isa<UnreachableInst>(I))
7813 return false;
7814
7815 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7816 // Instruction::willReturn.
7817 //
7818 // FIXME: Move this check into Instruction::willReturn.
7819 if (isa<CatchPadInst>(I)) {
7820 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7821 default:
7822 // A catchpad may invoke exception object constructors and such, which
7823 // in some languages can be arbitrary code, so be conservative by default.
7824 return false;
7825 case EHPersonality::CoreCLR:
7826 // For CoreCLR, it just involves a type test.
7827 return true;
7828 }
7829 }
7830
7831 // An instruction that returns without throwing must transfer control flow
7832 // to a successor.
7833 return !I->mayThrow() && I->willReturn();
7834}
7835
7837 // TODO: This is slightly conservative for invoke instruction since exiting
7838 // via an exception *is* normal control for them.
7839 for (const Instruction &I : *BB)
7841 return false;
7842 return true;
7843}
7844
7847 unsigned ScanLimit) {
7849 ScanLimit);
7850}
7851
7854 assert(ScanLimit && "scan limit must be non-zero");
7855 for (const Instruction &I : Range) {
7856 if (--ScanLimit == 0)
7857 return false;
7859 return false;
7860 }
7861 return true;
7862}
7863
7865 const Loop *L) {
7866 // The loop header is guaranteed to be executed for every iteration.
7867 //
7868 // FIXME: Relax this constraint to cover all basic blocks that are
7869 // guaranteed to be executed at every iteration.
7870 if (I->getParent() != L->getHeader()) return false;
7871
7872 for (const Instruction &LI : *L->getHeader()) {
7873 if (&LI == I) return true;
7874 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7875 }
7876 llvm_unreachable("Instruction not contained in its own parent basic block.");
7877}
7878
7880 switch (IID) {
7881 // TODO: Add more intrinsics.
7882 case Intrinsic::sadd_with_overflow:
7883 case Intrinsic::ssub_with_overflow:
7884 case Intrinsic::smul_with_overflow:
7885 case Intrinsic::uadd_with_overflow:
7886 case Intrinsic::usub_with_overflow:
7887 case Intrinsic::umul_with_overflow:
7888 // If an input is a vector containing a poison element, the
7889 // two output vectors (calculated results, overflow bits)'
7890 // corresponding lanes are poison.
7891 return true;
7892 case Intrinsic::ctpop:
7893 case Intrinsic::ctlz:
7894 case Intrinsic::cttz:
7895 case Intrinsic::abs:
7896 case Intrinsic::smax:
7897 case Intrinsic::smin:
7898 case Intrinsic::umax:
7899 case Intrinsic::umin:
7900 case Intrinsic::scmp:
7901 case Intrinsic::is_fpclass:
7902 case Intrinsic::ptrmask:
7903 case Intrinsic::ucmp:
7904 case Intrinsic::bitreverse:
7905 case Intrinsic::bswap:
7906 case Intrinsic::sadd_sat:
7907 case Intrinsic::ssub_sat:
7908 case Intrinsic::sshl_sat:
7909 case Intrinsic::uadd_sat:
7910 case Intrinsic::usub_sat:
7911 case Intrinsic::ushl_sat:
7912 case Intrinsic::smul_fix:
7913 case Intrinsic::smul_fix_sat:
7914 case Intrinsic::umul_fix:
7915 case Intrinsic::umul_fix_sat:
7916 case Intrinsic::pow:
7917 case Intrinsic::powi:
7918 case Intrinsic::sin:
7919 case Intrinsic::sinh:
7920 case Intrinsic::cos:
7921 case Intrinsic::cosh:
7922 case Intrinsic::sincos:
7923 case Intrinsic::sincospi:
7924 case Intrinsic::tan:
7925 case Intrinsic::tanh:
7926 case Intrinsic::asin:
7927 case Intrinsic::acos:
7928 case Intrinsic::atan:
7929 case Intrinsic::atan2:
7930 case Intrinsic::canonicalize:
7931 case Intrinsic::sqrt:
7932 case Intrinsic::exp:
7933 case Intrinsic::exp2:
7934 case Intrinsic::exp10:
7935 case Intrinsic::log:
7936 case Intrinsic::log2:
7937 case Intrinsic::log10:
7938 case Intrinsic::modf:
7939 case Intrinsic::floor:
7940 case Intrinsic::ceil:
7941 case Intrinsic::trunc:
7942 case Intrinsic::rint:
7943 case Intrinsic::nearbyint:
7944 case Intrinsic::round:
7945 case Intrinsic::roundeven:
7946 case Intrinsic::lrint:
7947 case Intrinsic::llrint:
7948 return true;
7949 default:
7950 return false;
7951 }
7952}
7953
7954bool llvm::propagatesPoison(const Use &PoisonOp) {
7955 const Operator *I = cast<Operator>(PoisonOp.getUser());
7956 switch (I->getOpcode()) {
7957 case Instruction::Freeze:
7958 case Instruction::PHI:
7959 case Instruction::Invoke:
7960 return false;
7961 case Instruction::Select:
7962 return PoisonOp.getOperandNo() == 0;
7963 case Instruction::Call:
7964 if (auto *II = dyn_cast<IntrinsicInst>(I))
7965 return intrinsicPropagatesPoison(II->getIntrinsicID());
7966 return false;
7967 case Instruction::ICmp:
7968 case Instruction::FCmp:
7969 case Instruction::GetElementPtr:
7970 return true;
7971 default:
7972 if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I))
7973 return true;
7974
7975 // Be conservative and return false.
7976 return false;
7977 }
7978}
7979
7980/// Enumerates all operands of \p I that are guaranteed to not be undef or
7981/// poison. If the callback \p Handle returns true, stop processing and return
7982/// true. Otherwise, return false.
7983template <typename CallableT>
7985 const CallableT &Handle) {
7986 switch (I->getOpcode()) {
7987 case Instruction::Store:
7988 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
7989 return true;
7990 break;
7991
7992 case Instruction::Load:
7993 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
7994 return true;
7995 break;
7996
7997 // Since dereferenceable attribute imply noundef, atomic operations
7998 // also implicitly have noundef pointers too
7999 case Instruction::AtomicCmpXchg:
8000 if (Handle(cast<AtomicCmpXchgInst>(I)->getPointerOperand()))
8001 return true;
8002 break;
8003
8004 case Instruction::AtomicRMW:
8005 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8006 return true;
8007 break;
8008
8009 case Instruction::Call:
8010 case Instruction::Invoke: {
8011 const CallBase *CB = cast<CallBase>(I);
8012 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8013 return true;
8014 for (unsigned i = 0; i < CB->arg_size(); ++i)
8015 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8016 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8017 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8018 Handle(CB->getArgOperand(i)))
8019 return true;
8020 break;
8021 }
8022 case Instruction::Ret:
8023 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8024 Handle(I->getOperand(0)))
8025 return true;
8026 break;
8027 case Instruction::Switch:
8028 if (Handle(cast<SwitchInst>(I)->getCondition()))
8029 return true;
8030 break;
8031 case Instruction::Br: {
8032 auto *BR = cast<BranchInst>(I);
8033 if (BR->isConditional() && Handle(BR->getCondition()))
8034 return true;
8035 break;
8036 }
8037 default:
8038 break;
8039 }
8040
8041 return false;
8042}
8043
8044/// Enumerates all operands of \p I that are guaranteed to not be poison.
8045template <typename CallableT>
8047 const CallableT &Handle) {
8048 if (handleGuaranteedWellDefinedOps(I, Handle))
8049 return true;
8050 switch (I->getOpcode()) {
8051 // Divisors of these operations are allowed to be partially undef.
8052 case Instruction::UDiv:
8053 case Instruction::SDiv:
8054 case Instruction::URem:
8055 case Instruction::SRem:
8056 return Handle(I->getOperand(1));
8057 default:
8058 return false;
8059 }
8060}
8061
8063 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8065 I, [&](const Value *V) { return KnownPoison.count(V); });
8066}
8067
8069 bool PoisonOnly) {
8070 // We currently only look for uses of values within the same basic
8071 // block, as that makes it easier to guarantee that the uses will be
8072 // executed given that Inst is executed.
8073 //
8074 // FIXME: Expand this to consider uses beyond the same basic block. To do
8075 // this, look out for the distinction between post-dominance and strong
8076 // post-dominance.
8077 const BasicBlock *BB = nullptr;
8079 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8080 BB = Inst->getParent();
8081 Begin = Inst->getIterator();
8082 Begin++;
8083 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8084 if (Arg->getParent()->isDeclaration())
8085 return false;
8086 BB = &Arg->getParent()->getEntryBlock();
8087 Begin = BB->begin();
8088 } else {
8089 return false;
8090 }
8091
8092 // Limit number of instructions we look at, to avoid scanning through large
8093 // blocks. The current limit is chosen arbitrarily.
8094 unsigned ScanLimit = 32;
8096
8097 if (!PoisonOnly) {
8098 // Since undef does not propagate eagerly, be conservative & just check
8099 // whether a value is directly passed to an instruction that must take
8100 // well-defined operands.
8101
8102 for (const auto &I : make_range(Begin, End)) {
8103 if (--ScanLimit == 0)
8104 break;
8105
8106 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8107 return WellDefinedOp == V;
8108 }))
8109 return true;
8110
8112 break;
8113 }
8114 return false;
8115 }
8116
8117 // Set of instructions that we have proved will yield poison if Inst
8118 // does.
8119 SmallPtrSet<const Value *, 16> YieldsPoison;
8121
8122 YieldsPoison.insert(V);
8123 Visited.insert(BB);
8124
8125 while (true) {
8126 for (const auto &I : make_range(Begin, End)) {
8127 if (--ScanLimit == 0)
8128 return false;
8129 if (mustTriggerUB(&I, YieldsPoison))
8130 return true;
8132 return false;
8133
8134 // If an operand is poison and propagates it, mark I as yielding poison.
8135 for (const Use &Op : I.operands()) {
8136 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8137 YieldsPoison.insert(&I);
8138 break;
8139 }
8140 }
8141
8142 // Special handling for select, which returns poison if its operand 0 is
8143 // poison (handled in the loop above) *or* if both its true/false operands
8144 // are poison (handled here).
8145 if (I.getOpcode() == Instruction::Select &&
8146 YieldsPoison.count(I.getOperand(1)) &&
8147 YieldsPoison.count(I.getOperand(2))) {
8148 YieldsPoison.insert(&I);
8149 }
8150 }
8151
8152 BB = BB->getSingleSuccessor();
8153 if (!BB || !Visited.insert(BB).second)
8154 break;
8155
8156 Begin = BB->getFirstNonPHIIt();
8157 End = BB->end();
8158 }
8159 return false;
8160}
8161
8163 return ::programUndefinedIfUndefOrPoison(Inst, false);
8164}
8165
8167 return ::programUndefinedIfUndefOrPoison(Inst, true);
8168}
8169
8170static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8171 if (FMF.noNaNs())
8172 return true;
8173
8174 if (auto *C = dyn_cast<ConstantFP>(V))
8175 return !C->isNaN();
8176
8177 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8178 if (!C->getElementType()->isFloatingPointTy())
8179 return false;
8180 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8181 if (C->getElementAsAPFloat(I).isNaN())
8182 return false;
8183 }
8184 return true;
8185 }
8186
8187 if (isa<ConstantAggregateZero>(V))
8188 return true;
8189
8190 return false;
8191}
8192
8193static bool isKnownNonZero(const Value *V) {
8194 if (auto *C = dyn_cast<ConstantFP>(V))
8195 return !C->isZero();
8196
8197 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8198 if (!C->getElementType()->isFloatingPointTy())
8199 return false;
8200 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8201 if (C->getElementAsAPFloat(I).isZero())
8202 return false;
8203 }
8204 return true;
8205 }
8206
8207 return false;
8208}
8209
8210/// Match clamp pattern for float types without care about NaNs or signed zeros.
8211/// Given non-min/max outer cmp/select from the clamp pattern this
8212/// function recognizes if it can be substitued by a "canonical" min/max
8213/// pattern.
8215 Value *CmpLHS, Value *CmpRHS,
8216 Value *TrueVal, Value *FalseVal,
8217 Value *&LHS, Value *&RHS) {
8218 // Try to match
8219 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8220 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8221 // and return description of the outer Max/Min.
8222
8223 // First, check if select has inverse order:
8224 if (CmpRHS == FalseVal) {
8225 std::swap(TrueVal, FalseVal);
8226 Pred = CmpInst::getInversePredicate(Pred);
8227 }
8228
8229 // Assume success now. If there's no match, callers should not use these anyway.
8230 LHS = TrueVal;
8231 RHS = FalseVal;
8232
8233 const APFloat *FC1;
8234 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8235 return {SPF_UNKNOWN, SPNB_NA, false};
8236
8237 const APFloat *FC2;
8238 switch (Pred) {
8239 case CmpInst::FCMP_OLT:
8240 case CmpInst::FCMP_OLE:
8241 case CmpInst::FCMP_ULT:
8242 case CmpInst::FCMP_ULE:
8243 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8244 *FC1 < *FC2)
8245 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8246 break;
8247 case CmpInst::FCMP_OGT:
8248 case CmpInst::FCMP_OGE:
8249 case CmpInst::FCMP_UGT:
8250 case CmpInst::FCMP_UGE:
8251 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8252 *FC1 > *FC2)
8253 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8254 break;
8255 default:
8256 break;
8257 }
8258
8259 return {SPF_UNKNOWN, SPNB_NA, false};
8260}
8261
8262/// Recognize variations of:
8263/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8265 Value *CmpLHS, Value *CmpRHS,
8266 Value *TrueVal, Value *FalseVal) {
8267 // Swap the select operands and predicate to match the patterns below.
8268 if (CmpRHS != TrueVal) {
8269 Pred = ICmpInst::getSwappedPredicate(Pred);
8270 std::swap(TrueVal, FalseVal);
8271 }
8272 const APInt *C1;
8273 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8274 const APInt *C2;
8275 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8276 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8277 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8278 return {SPF_SMAX, SPNB_NA, false};
8279
8280 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8281 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8282 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8283 return {SPF_SMIN, SPNB_NA, false};
8284
8285 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8286 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8287 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8288 return {SPF_UMAX, SPNB_NA, false};
8289
8290 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8291 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8292 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8293 return {SPF_UMIN, SPNB_NA, false};
8294 }
8295 return {SPF_UNKNOWN, SPNB_NA, false};
8296}
8297
8298/// Recognize variations of:
8299/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8301 Value *CmpLHS, Value *CmpRHS,
8302 Value *TVal, Value *FVal,
8303 unsigned Depth) {
8304 // TODO: Allow FP min/max with nnan/nsz.
8305 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8306
8307 Value *A = nullptr, *B = nullptr;
8308 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8309 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8310 return {SPF_UNKNOWN, SPNB_NA, false};
8311
8312 Value *C = nullptr, *D = nullptr;
8313 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8314 if (L.Flavor != R.Flavor)
8315 return {SPF_UNKNOWN, SPNB_NA, false};
8316
8317 // We have something like: x Pred y ? min(a, b) : min(c, d).
8318 // Try to match the compare to the min/max operations of the select operands.
8319 // First, make sure we have the right compare predicate.
8320 switch (L.Flavor) {
8321 case SPF_SMIN:
8322 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8323 Pred = ICmpInst::getSwappedPredicate(Pred);
8324 std::swap(CmpLHS, CmpRHS);
8325 }
8326 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8327 break;
8328 return {SPF_UNKNOWN, SPNB_NA, false};
8329 case SPF_SMAX:
8330 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8331 Pred = ICmpInst::getSwappedPredicate(Pred);
8332 std::swap(CmpLHS, CmpRHS);
8333 }
8334 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8335 break;
8336 return {SPF_UNKNOWN, SPNB_NA, false};
8337 case SPF_UMIN:
8338 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8339 Pred = ICmpInst::getSwappedPredicate(Pred);
8340 std::swap(CmpLHS, CmpRHS);
8341 }
8342 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8343 break;
8344 return {SPF_UNKNOWN, SPNB_NA, false};
8345 case SPF_UMAX:
8346 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8347 Pred = ICmpInst::getSwappedPredicate(Pred);
8348 std::swap(CmpLHS, CmpRHS);
8349 }
8350 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8351 break;
8352 return {SPF_UNKNOWN, SPNB_NA, false};
8353 default:
8354 return {SPF_UNKNOWN, SPNB_NA, false};
8355 }
8356
8357 // If there is a common operand in the already matched min/max and the other
8358 // min/max operands match the compare operands (either directly or inverted),
8359 // then this is min/max of the same flavor.
8360
8361 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8362 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8363 if (D == B) {
8364 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8365 match(A, m_Not(m_Specific(CmpRHS)))))
8366 return {L.Flavor, SPNB_NA, false};
8367 }
8368 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8369 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8370 if (C == B) {
8371 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8372 match(A, m_Not(m_Specific(CmpRHS)))))
8373 return {L.Flavor, SPNB_NA, false};
8374 }
8375 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8376 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8377 if (D == A) {
8378 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8379 match(B, m_Not(m_Specific(CmpRHS)))))
8380 return {L.Flavor, SPNB_NA, false};
8381 }
8382 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8383 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8384 if (C == A) {
8385 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8386 match(B, m_Not(m_Specific(CmpRHS)))))
8387 return {L.Flavor, SPNB_NA, false};
8388 }
8389
8390 return {SPF_UNKNOWN, SPNB_NA, false};
8391}
8392
8393/// If the input value is the result of a 'not' op, constant integer, or vector
8394/// splat of a constant integer, return the bitwise-not source value.
8395/// TODO: This could be extended to handle non-splat vector integer constants.
8397 Value *NotV;
8398 if (match(V, m_Not(m_Value(NotV))))
8399 return NotV;
8400
8401 const APInt *C;
8402 if (match(V, m_APInt(C)))
8403 return ConstantInt::get(V->getType(), ~(*C));
8404
8405 return nullptr;
8406}
8407
8408/// Match non-obvious integer minimum and maximum sequences.
8410 Value *CmpLHS, Value *CmpRHS,
8411 Value *TrueVal, Value *FalseVal,
8412 Value *&LHS, Value *&RHS,
8413 unsigned Depth) {
8414 // Assume success. If there's no match, callers should not use these anyway.
8415 LHS = TrueVal;
8416 RHS = FalseVal;
8417
8418 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8419 if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
8420 return SPR;
8421
8422 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8423 if (SPR.Flavor != SelectPatternFlavor::SPF_UNKNOWN)
8424 return SPR;
8425
8426 // Look through 'not' ops to find disguised min/max.
8427 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8428 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8429 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8430 switch (Pred) {
8431 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8432 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8433 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8434 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8435 default: break;
8436 }
8437 }
8438
8439 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8440 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8441 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8442 switch (Pred) {
8443 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8444 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8445 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8446 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8447 default: break;
8448 }
8449 }
8450
8451 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8452 return {SPF_UNKNOWN, SPNB_NA, false};
8453
8454 const APInt *C1;
8455 if (!match(CmpRHS, m_APInt(C1)))
8456 return {SPF_UNKNOWN, SPNB_NA, false};
8457
8458 // An unsigned min/max can be written with a signed compare.
8459 const APInt *C2;
8460 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8461 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8462 // Is the sign bit set?
8463 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8464 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8465 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8466 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8467
8468 // Is the sign bit clear?
8469 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8470 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8471 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8472 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8473 }
8474
8475 return {SPF_UNKNOWN, SPNB_NA, false};
8476}
8477
8478bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8479 bool AllowPoison) {
8480 assert(X && Y && "Invalid operand");
8481
8482 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8483 if (!match(X, m_Neg(m_Specific(Y))))
8484 return false;
8485
8486 auto *BO = cast<BinaryOperator>(X);
8487 if (NeedNSW && !BO->hasNoSignedWrap())
8488 return false;
8489
8490 auto *Zero = cast<Constant>(BO->getOperand(0));
8491 if (!AllowPoison && !Zero->isNullValue())
8492 return false;
8493
8494 return true;
8495 };
8496
8497 // X = -Y or Y = -X
8498 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8499 return true;
8500
8501 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8502 Value *A, *B;
8503 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8504 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8505 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8507}
8508
8509bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8510 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8511 Value *A, *B, *C;
8512 CmpPredicate Pred1, Pred2;
8513 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8514 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8515 return false;
8516
8517 // They must both have samesign flag or not.
8518 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8519 return false;
8520
8521 if (B == C)
8522 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8523
8524 // Try to infer the relationship from constant ranges.
8525 const APInt *RHSC1, *RHSC2;
8526 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8527 return false;
8528
8529 // Sign bits of two RHSCs should match.
8530 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8531 return false;
8532
8533 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8534 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8535
8536 return CR1.inverse() == CR2;
8537}
8538
8540 SelectPatternNaNBehavior NaNBehavior,
8541 bool Ordered) {
8542 switch (Pred) {
8543 default:
8544 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8545 case ICmpInst::ICMP_UGT:
8546 case ICmpInst::ICMP_UGE:
8547 return {SPF_UMAX, SPNB_NA, false};
8548 case ICmpInst::ICMP_SGT:
8549 case ICmpInst::ICMP_SGE:
8550 return {SPF_SMAX, SPNB_NA, false};
8551 case ICmpInst::ICMP_ULT:
8552 case ICmpInst::ICMP_ULE:
8553 return {SPF_UMIN, SPNB_NA, false};
8554 case ICmpInst::ICMP_SLT:
8555 case ICmpInst::ICMP_SLE:
8556 return {SPF_SMIN, SPNB_NA, false};
8557 case FCmpInst::FCMP_UGT:
8558 case FCmpInst::FCMP_UGE:
8559 case FCmpInst::FCMP_OGT:
8560 case FCmpInst::FCMP_OGE:
8561 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8562 case FCmpInst::FCMP_ULT:
8563 case FCmpInst::FCMP_ULE:
8564 case FCmpInst::FCMP_OLT:
8565 case FCmpInst::FCMP_OLE:
8566 return {SPF_FMINNUM, NaNBehavior, Ordered};
8567 }
8568}
8569
8570std::optional<std::pair<CmpPredicate, Constant *>>
8572 assert(ICmpInst::isRelational(Pred) && ICmpInst::isIntPredicate(Pred) &&
8573 "Only for relational integer predicates.");
8574 if (isa<UndefValue>(C))
8575 return std::nullopt;
8576
8577 Type *Type = C->getType();
8578 bool IsSigned = ICmpInst::isSigned(Pred);
8579
8581 bool WillIncrement =
8582 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8583
8584 // Check if the constant operand can be safely incremented/decremented
8585 // without overflowing/underflowing.
8586 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8587 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8588 };
8589
8590 Constant *SafeReplacementConstant = nullptr;
8591 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8592 // Bail out if the constant can't be safely incremented/decremented.
8593 if (!ConstantIsOk(CI))
8594 return std::nullopt;
8595 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8596 unsigned NumElts = FVTy->getNumElements();
8597 for (unsigned i = 0; i != NumElts; ++i) {
8598 Constant *Elt = C->getAggregateElement(i);
8599 if (!Elt)
8600 return std::nullopt;
8601
8602 if (isa<UndefValue>(Elt))
8603 continue;
8604
8605 // Bail out if we can't determine if this constant is min/max or if we
8606 // know that this constant is min/max.
8607 auto *CI = dyn_cast<ConstantInt>(Elt);
8608 if (!CI || !ConstantIsOk(CI))
8609 return std::nullopt;
8610
8611 if (!SafeReplacementConstant)
8612 SafeReplacementConstant = CI;
8613 }
8614 } else if (isa<VectorType>(C->getType())) {
8615 // Handle scalable splat
8616 Value *SplatC = C->getSplatValue();
8617 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8618 // Bail out if the constant can't be safely incremented/decremented.
8619 if (!CI || !ConstantIsOk(CI))
8620 return std::nullopt;
8621 } else {
8622 // ConstantExpr?
8623 return std::nullopt;
8624 }
8625
8626 // It may not be safe to change a compare predicate in the presence of
8627 // undefined elements, so replace those elements with the first safe constant
8628 // that we found.
8629 // TODO: in case of poison, it is safe; let's replace undefs only.
8630 if (C->containsUndefOrPoisonElement()) {
8631 assert(SafeReplacementConstant && "Replacement constant not set");
8632 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8633 }
8634
8636
8637 // Increment or decrement the constant.
8638 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8639 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8640
8641 return std::make_pair(NewPred, NewC);
8642}
8643
8645 FastMathFlags FMF,
8646 Value *CmpLHS, Value *CmpRHS,
8647 Value *TrueVal, Value *FalseVal,
8648 Value *&LHS, Value *&RHS,
8649 unsigned Depth) {
8650 bool HasMismatchedZeros = false;
8651 if (CmpInst::isFPPredicate(Pred)) {
8652 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8653 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8654 // purpose of identifying min/max. Disregard vector constants with undefined
8655 // elements because those can not be back-propagated for analysis.
8656 Value *OutputZeroVal = nullptr;
8657 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8658 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8659 OutputZeroVal = TrueVal;
8660 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8661 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8662 OutputZeroVal = FalseVal;
8663
8664 if (OutputZeroVal) {
8665 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8666 HasMismatchedZeros = true;
8667 CmpLHS = OutputZeroVal;
8668 }
8669 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8670 HasMismatchedZeros = true;
8671 CmpRHS = OutputZeroVal;
8672 }
8673 }
8674 }
8675
8676 LHS = CmpLHS;
8677 RHS = CmpRHS;
8678
8679 // Signed zero may return inconsistent results between implementations.
8680 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8681 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8682 // Therefore, we behave conservatively and only proceed if at least one of the
8683 // operands is known to not be zero or if we don't care about signed zero.
8684 switch (Pred) {
8685 default: break;
8688 if (!HasMismatchedZeros)
8689 break;
8690 [[fallthrough]];
8693 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8694 !isKnownNonZero(CmpRHS))
8695 return {SPF_UNKNOWN, SPNB_NA, false};
8696 }
8697
8698 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8699 bool Ordered = false;
8700
8701 // When given one NaN and one non-NaN input:
8702 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8703 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8704 // ordered comparison fails), which could be NaN or non-NaN.
8705 // so here we discover exactly what NaN behavior is required/accepted.
8706 if (CmpInst::isFPPredicate(Pred)) {
8707 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8708 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8709
8710 if (LHSSafe && RHSSafe) {
8711 // Both operands are known non-NaN.
8712 NaNBehavior = SPNB_RETURNS_ANY;
8713 Ordered = CmpInst::isOrdered(Pred);
8714 } else if (CmpInst::isOrdered(Pred)) {
8715 // An ordered comparison will return false when given a NaN, so it
8716 // returns the RHS.
8717 Ordered = true;
8718 if (LHSSafe)
8719 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8720 NaNBehavior = SPNB_RETURNS_NAN;
8721 else if (RHSSafe)
8722 NaNBehavior = SPNB_RETURNS_OTHER;
8723 else
8724 // Completely unsafe.
8725 return {SPF_UNKNOWN, SPNB_NA, false};
8726 } else {
8727 Ordered = false;
8728 // An unordered comparison will return true when given a NaN, so it
8729 // returns the LHS.
8730 if (LHSSafe)
8731 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8732 NaNBehavior = SPNB_RETURNS_OTHER;
8733 else if (RHSSafe)
8734 NaNBehavior = SPNB_RETURNS_NAN;
8735 else
8736 // Completely unsafe.
8737 return {SPF_UNKNOWN, SPNB_NA, false};
8738 }
8739 }
8740
8741 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8742 std::swap(CmpLHS, CmpRHS);
8743 Pred = CmpInst::getSwappedPredicate(Pred);
8744 if (NaNBehavior == SPNB_RETURNS_NAN)
8745 NaNBehavior = SPNB_RETURNS_OTHER;
8746 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8747 NaNBehavior = SPNB_RETURNS_NAN;
8748 Ordered = !Ordered;
8749 }
8750
8751 // ([if]cmp X, Y) ? X : Y
8752 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8753 return getSelectPattern(Pred, NaNBehavior, Ordered);
8754
8755 if (isKnownNegation(TrueVal, FalseVal)) {
8756 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8757 // match against either LHS or sext(LHS).
8758 auto MaybeSExtCmpLHS =
8759 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8760 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8761 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8762 if (match(TrueVal, MaybeSExtCmpLHS)) {
8763 // Set the return values. If the compare uses the negated value (-X >s 0),
8764 // swap the return values because the negated value is always 'RHS'.
8765 LHS = TrueVal;
8766 RHS = FalseVal;
8767 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8768 std::swap(LHS, RHS);
8769
8770 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8771 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8772 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8773 return {SPF_ABS, SPNB_NA, false};
8774
8775 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8776 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8777 return {SPF_ABS, SPNB_NA, false};
8778
8779 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8780 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8781 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8782 return {SPF_NABS, SPNB_NA, false};
8783 }
8784 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8785 // Set the return values. If the compare uses the negated value (-X >s 0),
8786 // swap the return values because the negated value is always 'RHS'.
8787 LHS = FalseVal;
8788 RHS = TrueVal;
8789 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8790 std::swap(LHS, RHS);
8791
8792 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8793 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8794 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8795 return {SPF_NABS, SPNB_NA, false};
8796
8797 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8798 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8799 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8800 return {SPF_ABS, SPNB_NA, false};
8801 }
8802 }
8803
8804 if (CmpInst::isIntPredicate(Pred))
8805 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8806
8807 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8808 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8809 // semantics than minNum. Be conservative in such case.
8810 if (NaNBehavior != SPNB_RETURNS_ANY ||
8811 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8812 !isKnownNonZero(CmpRHS)))
8813 return {SPF_UNKNOWN, SPNB_NA, false};
8814
8815 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8816}
8817
8819 Instruction::CastOps *CastOp) {
8820 const DataLayout &DL = CmpI->getDataLayout();
8821
8822 Constant *CastedTo = nullptr;
8823 switch (*CastOp) {
8824 case Instruction::ZExt:
8825 if (CmpI->isUnsigned())
8826 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8827 break;
8828 case Instruction::SExt:
8829 if (CmpI->isSigned())
8830 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8831 break;
8832 case Instruction::Trunc:
8833 Constant *CmpConst;
8834 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8835 CmpConst->getType() == SrcTy) {
8836 // Here we have the following case:
8837 //
8838 // %cond = cmp iN %x, CmpConst
8839 // %tr = trunc iN %x to iK
8840 // %narrowsel = select i1 %cond, iK %t, iK C
8841 //
8842 // We can always move trunc after select operation:
8843 //
8844 // %cond = cmp iN %x, CmpConst
8845 // %widesel = select i1 %cond, iN %x, iN CmpConst
8846 // %tr = trunc iN %widesel to iK
8847 //
8848 // Note that C could be extended in any way because we don't care about
8849 // upper bits after truncation. It can't be abs pattern, because it would
8850 // look like:
8851 //
8852 // select i1 %cond, x, -x.
8853 //
8854 // So only min/max pattern could be matched. Such match requires widened C
8855 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8856 // CmpConst == C is checked below.
8857 CastedTo = CmpConst;
8858 } else {
8859 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8860 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8861 }
8862 break;
8863 case Instruction::FPTrunc:
8864 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8865 break;
8866 case Instruction::FPExt:
8867 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8868 break;
8869 case Instruction::FPToUI:
8870 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8871 break;
8872 case Instruction::FPToSI:
8873 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8874 break;
8875 case Instruction::UIToFP:
8876 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8877 break;
8878 case Instruction::SIToFP:
8879 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8880 break;
8881 default:
8882 break;
8883 }
8884
8885 if (!CastedTo)
8886 return nullptr;
8887
8888 // Make sure the cast doesn't lose any information.
8889 Constant *CastedBack =
8890 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8891 if (CastedBack && CastedBack != C)
8892 return nullptr;
8893
8894 return CastedTo;
8895}
8896
8897/// Helps to match a select pattern in case of a type mismatch.
8898///
8899/// The function processes the case when type of true and false values of a
8900/// select instruction differs from type of the cmp instruction operands because
8901/// of a cast instruction. The function checks if it is legal to move the cast
8902/// operation after "select". If yes, it returns the new second value of
8903/// "select" (with the assumption that cast is moved):
8904/// 1. As operand of cast instruction when both values of "select" are same cast
8905/// instructions.
8906/// 2. As restored constant (by applying reverse cast operation) when the first
8907/// value of the "select" is a cast operation and the second value is a
8908/// constant. It is implemented in lookThroughCastConst().
8909/// 3. As one operand is cast instruction and the other is not. The operands in
8910/// sel(cmp) are in different type integer.
8911/// NOTE: We return only the new second value because the first value could be
8912/// accessed as operand of cast instruction.
8913static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
8914 Instruction::CastOps *CastOp) {
8915 auto *Cast1 = dyn_cast<CastInst>(V1);
8916 if (!Cast1)
8917 return nullptr;
8918
8919 *CastOp = Cast1->getOpcode();
8920 Type *SrcTy = Cast1->getSrcTy();
8921 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
8922 // If V1 and V2 are both the same cast from the same type, look through V1.
8923 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
8924 return Cast2->getOperand(0);
8925 return nullptr;
8926 }
8927
8928 auto *C = dyn_cast<Constant>(V2);
8929 if (C)
8930 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
8931
8932 Value *CastedTo = nullptr;
8933 if (*CastOp == Instruction::Trunc) {
8934 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
8935 // Here we have the following case:
8936 // %y_ext = sext iK %y to iN
8937 // %cond = cmp iN %x, %y_ext
8938 // %tr = trunc iN %x to iK
8939 // %narrowsel = select i1 %cond, iK %tr, iK %y
8940 //
8941 // We can always move trunc after select operation:
8942 // %y_ext = sext iK %y to iN
8943 // %cond = cmp iN %x, %y_ext
8944 // %widesel = select i1 %cond, iN %x, iN %y_ext
8945 // %tr = trunc iN %widesel to iK
8946 assert(V2->getType() == Cast1->getType() &&
8947 "V2 and Cast1 should be the same type.");
8948 CastedTo = CmpI->getOperand(1);
8949 }
8950 }
8951
8952 return CastedTo;
8953}
8955 Instruction::CastOps *CastOp,
8956 unsigned Depth) {
8958 return {SPF_UNKNOWN, SPNB_NA, false};
8959
8960 SelectInst *SI = dyn_cast<SelectInst>(V);
8961 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
8962
8963 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
8964 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
8965
8966 Value *TrueVal = SI->getTrueValue();
8967 Value *FalseVal = SI->getFalseValue();
8968
8970 CmpI, TrueVal, FalseVal, LHS, RHS,
8971 isa<FPMathOperator>(SI) ? SI->getFastMathFlags() : FastMathFlags(),
8972 CastOp, Depth);
8973}
8974
8976 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
8977 FastMathFlags FMF, Instruction::CastOps *CastOp, unsigned Depth) {
8978 CmpInst::Predicate Pred = CmpI->getPredicate();
8979 Value *CmpLHS = CmpI->getOperand(0);
8980 Value *CmpRHS = CmpI->getOperand(1);
8981 if (isa<FPMathOperator>(CmpI) && CmpI->hasNoNaNs())
8982 FMF.setNoNaNs();
8983
8984 // Bail out early.
8985 if (CmpI->isEquality())
8986 return {SPF_UNKNOWN, SPNB_NA, false};
8987
8988 // Deal with type mismatches.
8989 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
8990 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
8991 // If this is a potential fmin/fmax with a cast to integer, then ignore
8992 // -0.0 because there is no corresponding integer value.
8993 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
8994 FMF.setNoSignedZeros();
8995 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
8996 cast<CastInst>(TrueVal)->getOperand(0), C,
8997 LHS, RHS, Depth);
8998 }
8999 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9000 // If this is a potential fmin/fmax with a cast to integer, then ignore
9001 // -0.0 because there is no corresponding integer value.
9002 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9003 FMF.setNoSignedZeros();
9004 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9005 C, cast<CastInst>(FalseVal)->getOperand(0),
9006 LHS, RHS, Depth);
9007 }
9008 }
9009 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9010 LHS, RHS, Depth);
9011}
9012
9014 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9015 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9016 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9017 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9018 if (SPF == SPF_FMINNUM)
9019 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9020 if (SPF == SPF_FMAXNUM)
9021 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9022 llvm_unreachable("unhandled!");
9023}
9024
9026 switch (SPF) {
9027 case SelectPatternFlavor::SPF_UMIN:
9028 return Intrinsic::umin;
9029 case SelectPatternFlavor::SPF_UMAX:
9030 return Intrinsic::umax;
9031 case SelectPatternFlavor::SPF_SMIN:
9032 return Intrinsic::smin;
9033 case SelectPatternFlavor::SPF_SMAX:
9034 return Intrinsic::smax;
9035 default:
9036 llvm_unreachable("Unexpected SPF");
9037 }
9038}
9039
9041 if (SPF == SPF_SMIN) return SPF_SMAX;
9042 if (SPF == SPF_UMIN) return SPF_UMAX;
9043 if (SPF == SPF_SMAX) return SPF_SMIN;
9044 if (SPF == SPF_UMAX) return SPF_UMIN;
9045 llvm_unreachable("unhandled!");
9046}
9047
9049 switch (MinMaxID) {
9050 case Intrinsic::smax: return Intrinsic::smin;
9051 case Intrinsic::smin: return Intrinsic::smax;
9052 case Intrinsic::umax: return Intrinsic::umin;
9053 case Intrinsic::umin: return Intrinsic::umax;
9054 // Please note that next four intrinsics may produce the same result for
9055 // original and inverted case even if X != Y due to NaN is handled specially.
9056 case Intrinsic::maximum: return Intrinsic::minimum;
9057 case Intrinsic::minimum: return Intrinsic::maximum;
9058 case Intrinsic::maxnum: return Intrinsic::minnum;
9059 case Intrinsic::minnum: return Intrinsic::maxnum;
9060 default: llvm_unreachable("Unexpected intrinsic");
9061 }
9062}
9063
9065 switch (SPF) {
9068 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9069 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9070 default: llvm_unreachable("Unexpected flavor");
9071 }
9072}
9073
9074std::pair<Intrinsic::ID, bool>
9076 // Check if VL contains select instructions that can be folded into a min/max
9077 // vector intrinsic and return the intrinsic if it is possible.
9078 // TODO: Support floating point min/max.
9079 bool AllCmpSingleUse = true;
9080 SelectPatternResult SelectPattern;
9081 SelectPattern.Flavor = SPF_UNKNOWN;
9082 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9083 Value *LHS, *RHS;
9084 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9085 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9086 return false;
9087 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9088 SelectPattern.Flavor != CurrentPattern.Flavor)
9089 return false;
9090 SelectPattern = CurrentPattern;
9091 AllCmpSingleUse &=
9093 return true;
9094 })) {
9095 switch (SelectPattern.Flavor) {
9096 case SPF_SMIN:
9097 return {Intrinsic::smin, AllCmpSingleUse};
9098 case SPF_UMIN:
9099 return {Intrinsic::umin, AllCmpSingleUse};
9100 case SPF_SMAX:
9101 return {Intrinsic::smax, AllCmpSingleUse};
9102 case SPF_UMAX:
9103 return {Intrinsic::umax, AllCmpSingleUse};
9104 case SPF_FMAXNUM:
9105 return {Intrinsic::maxnum, AllCmpSingleUse};
9106 case SPF_FMINNUM:
9107 return {Intrinsic::minnum, AllCmpSingleUse};
9108 default:
9109 llvm_unreachable("unexpected select pattern flavor");
9110 }
9111 }
9112 return {Intrinsic::not_intrinsic, false};
9113}
9114
9115template <typename InstTy>
9116static bool matchTwoInputRecurrence(const PHINode *PN, InstTy *&Inst,
9117 Value *&Init, Value *&OtherOp) {
9118 // Handle the case of a simple two-predecessor recurrence PHI.
9119 // There's a lot more that could theoretically be done here, but
9120 // this is sufficient to catch some interesting cases.
9121 // TODO: Expand list -- gep, uadd.sat etc.
9122 if (PN->getNumIncomingValues() != 2)
9123 return false;
9124
9125 for (unsigned I = 0; I != 2; ++I) {
9126 if (auto *Operation = dyn_cast<InstTy>(PN->getIncomingValue(I));
9127 Operation && Operation->getNumOperands() >= 2) {
9128 Value *LHS = Operation->getOperand(0);
9129 Value *RHS = Operation->getOperand(1);
9130 if (LHS != PN && RHS != PN)
9131 continue;
9132
9133 Inst = Operation;
9134 Init = PN->getIncomingValue(!I);
9135 OtherOp = (LHS == PN) ? RHS : LHS;
9136 return true;
9137 }
9138 }
9139 return false;
9140}
9141
9143 Value *&Start, Value *&Step) {
9144 // We try to match a recurrence of the form:
9145 // %iv = [Start, %entry], [%iv.next, %backedge]
9146 // %iv.next = binop %iv, Step
9147 // Or:
9148 // %iv = [Start, %entry], [%iv.next, %backedge]
9149 // %iv.next = binop Step, %iv
9150 return matchTwoInputRecurrence(P, BO, Start, Step);
9151}
9152
9154 Value *&Start, Value *&Step) {
9155 BinaryOperator *BO = nullptr;
9156 P = dyn_cast<PHINode>(I->getOperand(0));
9157 if (!P)
9158 P = dyn_cast<PHINode>(I->getOperand(1));
9159 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9160}
9161
9163 PHINode *&P, Value *&Init,
9164 Value *&OtherOp) {
9165 // Binary intrinsics only supported for now.
9166 if (I->arg_size() != 2 || I->getType() != I->getArgOperand(0)->getType() ||
9167 I->getType() != I->getArgOperand(1)->getType())
9168 return false;
9169
9170 IntrinsicInst *II = nullptr;
9171 P = dyn_cast<PHINode>(I->getArgOperand(0));
9172 if (!P)
9173 P = dyn_cast<PHINode>(I->getArgOperand(1));
9174
9175 return P && matchTwoInputRecurrence(P, II, Init, OtherOp) && II == I;
9176}
9177
9178/// Return true if "icmp Pred LHS RHS" is always true.
9179static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
9180 const Value *RHS) {
9181 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9182 return true;
9183
9184 switch (Pred) {
9185 default:
9186 return false;
9187
9188 case CmpInst::ICMP_SLE: {
9189 const APInt *C;
9190
9191 // LHS s<= LHS +_{nsw} C if C >= 0
9192 // LHS s<= LHS | C if C >= 0
9193 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9195 return !C->isNegative();
9196
9197 // LHS s<= smax(LHS, V) for any V
9199 return true;
9200
9201 // smin(RHS, V) s<= RHS for any V
9203 return true;
9204
9205 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9206 const Value *X;
9207 const APInt *CLHS, *CRHS;
9208 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9210 return CLHS->sle(*CRHS);
9211
9212 return false;
9213 }
9214
9215 case CmpInst::ICMP_ULE: {
9216 // LHS u<= LHS +_{nuw} V for any V
9217 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9218 cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
9219 return true;
9220
9221 // LHS u<= LHS | V for any V
9222 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9223 return true;
9224
9225 // LHS u<= umax(LHS, V) for any V
9227 return true;
9228
9229 // RHS >> V u<= RHS for any V
9230 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9231 return true;
9232
9233 // RHS u/ C_ugt_1 u<= RHS
9234 const APInt *C;
9235 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9236 return true;
9237
9238 // RHS & V u<= RHS for any V
9240 return true;
9241
9242 // umin(RHS, V) u<= RHS for any V
9244 return true;
9245
9246 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9247 const Value *X;
9248 const APInt *CLHS, *CRHS;
9249 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9251 return CLHS->ule(*CRHS);
9252
9253 return false;
9254 }
9255 }
9256}
9257
9258/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9259/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9260static std::optional<bool>
9262 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9263 switch (Pred) {
9264 default:
9265 return std::nullopt;
9266
9267 case CmpInst::ICMP_SLT:
9268 case CmpInst::ICMP_SLE:
9269 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9271 return true;
9272 return std::nullopt;
9273
9274 case CmpInst::ICMP_SGT:
9275 case CmpInst::ICMP_SGE:
9276 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9278 return true;
9279 return std::nullopt;
9280
9281 case CmpInst::ICMP_ULT:
9282 case CmpInst::ICMP_ULE:
9283 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9285 return true;
9286 return std::nullopt;
9287
9288 case CmpInst::ICMP_UGT:
9289 case CmpInst::ICMP_UGE:
9290 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9292 return true;
9293 return std::nullopt;
9294 }
9295}
9296
9297/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9298/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9299/// Otherwise, return std::nullopt if we can't infer anything.
9300static std::optional<bool>
9302 CmpPredicate RPred, const ConstantRange &RCR) {
9303 auto CRImpliesPred = [&](ConstantRange CR,
9304 CmpInst::Predicate Pred) -> std::optional<bool> {
9305 // If all true values for lhs and true for rhs, lhs implies rhs
9306 if (CR.icmp(Pred, RCR))
9307 return true;
9308
9309 // If there is no overlap, lhs implies not rhs
9310 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9311 return false;
9312
9313 return std::nullopt;
9314 };
9315 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9316 RPred))
9317 return Res;
9318 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9320 : LPred.dropSameSign();
9322 : RPred.dropSameSign();
9323 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9324 RPred);
9325 }
9326 return std::nullopt;
9327}
9328
9329/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9330/// is true. Return false if LHS implies RHS is false. Otherwise, return
9331/// std::nullopt if we can't infer anything.
9332static std::optional<bool>
9333isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1,
9334 CmpPredicate RPred, const Value *R0, const Value *R1,
9335 const DataLayout &DL, bool LHSIsTrue) {
9336 // The rest of the logic assumes the LHS condition is true. If that's not the
9337 // case, invert the predicate to make it so.
9338 if (!LHSIsTrue)
9339 LPred = ICmpInst::getInverseCmpPredicate(LPred);
9340
9341 // We can have non-canonical operands, so try to normalize any common operand
9342 // to L0/R0.
9343 if (L0 == R1) {
9344 std::swap(R0, R1);
9345 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9346 }
9347 if (R0 == L1) {
9348 std::swap(L0, L1);
9349 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9350 }
9351 if (L1 == R1) {
9352 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9353 if (L0 != R0 || match(L0, m_ImmConstant())) {
9354 std::swap(L0, L1);
9355 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9356 std::swap(R0, R1);
9357 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9358 }
9359 }
9360
9361 // See if we can infer anything if operand-0 matches and we have at least one
9362 // constant.
9363 const APInt *Unused;
9364 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9365 // Potential TODO: We could also further use the constant range of L0/R0 to
9366 // further constraint the constant ranges. At the moment this leads to
9367 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9368 // C1` (see discussion: D58633).
9370 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9371 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9373 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9374 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9375 // Even if L1/R1 are not both constant, we can still sometimes deduce
9376 // relationship from a single constant. For example X u> Y implies X != 0.
9377 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9378 return R;
9379 // If both L1/R1 were exact constant ranges and we didn't get anything
9380 // here, we won't be able to deduce this.
9381 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9382 return std::nullopt;
9383 }
9384
9385 // Can we infer anything when the two compares have matching operands?
9386 if (L0 == R0 && L1 == R1)
9387 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9388
9389 // It only really makes sense in the context of signed comparison for "X - Y
9390 // must be positive if X >= Y and no overflow".
9391 // Take SGT as an example: L0:x > L1:y and C >= 0
9392 // ==> R0:(x -nsw y) < R1:(-C) is false
9393 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9394 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9395 SignedLPred == ICmpInst::ICMP_SGE) &&
9396 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9397 if (match(R1, m_NonPositive()) &&
9398 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9399 return false;
9400 }
9401
9402 // Take SLT as an example: L0:x < L1:y and C <= 0
9403 // ==> R0:(x -nsw y) < R1:(-C) is true
9404 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9405 SignedLPred == ICmpInst::ICMP_SLE) &&
9406 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9407 if (match(R1, m_NonNegative()) &&
9408 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9409 return true;
9410 }
9411
9412 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9413 if (L0 == R0 &&
9414 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9415 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9416 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9417 return CmpPredicate::getMatching(LPred, RPred).has_value();
9418
9419 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9420 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9421
9422 return std::nullopt;
9423}
9424
9425/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9426/// false. Otherwise, return std::nullopt if we can't infer anything. We
9427/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9428/// instruction.
9429static std::optional<bool>
9431 const Value *RHSOp0, const Value *RHSOp1,
9432 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9433 // The LHS must be an 'or', 'and', or a 'select' instruction.
9434 assert((LHS->getOpcode() == Instruction::And ||
9435 LHS->getOpcode() == Instruction::Or ||
9436 LHS->getOpcode() == Instruction::Select) &&
9437 "Expected LHS to be 'and', 'or', or 'select'.");
9438
9439 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9440
9441 // If the result of an 'or' is false, then we know both legs of the 'or' are
9442 // false. Similarly, if the result of an 'and' is true, then we know both
9443 // legs of the 'and' are true.
9444 const Value *ALHS, *ARHS;
9445 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9446 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9447 // FIXME: Make this non-recursion.
9448 if (std::optional<bool> Implication = isImpliedCondition(
9449 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9450 return Implication;
9451 if (std::optional<bool> Implication = isImpliedCondition(
9452 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9453 return Implication;
9454 return std::nullopt;
9455 }
9456 return std::nullopt;
9457}
9458
9459std::optional<bool>
9461 const Value *RHSOp0, const Value *RHSOp1,
9462 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9463 // Bail out when we hit the limit.
9465 return std::nullopt;
9466
9467 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9468 // example.
9469 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9470 return std::nullopt;
9471
9473 "Expected integer type only!");
9474
9475 // Match not
9476 if (match(LHS, m_Not(m_Value(LHS))))
9477 LHSIsTrue = !LHSIsTrue;
9478
9479 // Both LHS and RHS are icmps.
9480 if (const auto *LHSCmp = dyn_cast<ICmpInst>(LHS))
9481 return isImpliedCondICmps(LHSCmp->getCmpPredicate(), LHSCmp->getOperand(0),
9482 LHSCmp->getOperand(1), RHSPred, RHSOp0, RHSOp1,
9483 DL, LHSIsTrue);
9484 const Value *V;
9485 if (match(LHS, m_NUWTrunc(m_Value(V))))
9487 ConstantInt::get(V->getType(), 0), RHSPred,
9488 RHSOp0, RHSOp1, DL, LHSIsTrue);
9489
9490 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9491 /// the RHS to be an icmp.
9492 /// FIXME: Add support for and/or/select on the RHS.
9493 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9494 if ((LHSI->getOpcode() == Instruction::And ||
9495 LHSI->getOpcode() == Instruction::Or ||
9496 LHSI->getOpcode() == Instruction::Select))
9497 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9498 Depth);
9499 }
9500 return std::nullopt;
9501}
9502
9503std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9504 const DataLayout &DL,
9505 bool LHSIsTrue, unsigned Depth) {
9506 // LHS ==> RHS by definition
9507 if (LHS == RHS)
9508 return LHSIsTrue;
9509
9510 // Match not
9511 bool InvertRHS = false;
9512 if (match(RHS, m_Not(m_Value(RHS)))) {
9513 if (LHS == RHS)
9514 return !LHSIsTrue;
9515 InvertRHS = true;
9516 }
9517
9518 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9519 if (auto Implied = isImpliedCondition(
9520 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9521 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9522 return InvertRHS ? !*Implied : *Implied;
9523 return std::nullopt;
9524 }
9525
9526 const Value *V;
9527 if (match(RHS, m_NUWTrunc(m_Value(V)))) {
9528 if (auto Implied = isImpliedCondition(LHS, CmpInst::ICMP_NE, V,
9529 ConstantInt::get(V->getType(), 0), DL,
9530 LHSIsTrue, Depth))
9531 return InvertRHS ? !*Implied : *Implied;
9532 return std::nullopt;
9533 }
9534
9536 return std::nullopt;
9537
9538 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9539 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9540 const Value *RHS1, *RHS2;
9541 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9542 if (std::optional<bool> Imp =
9543 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9544 if (*Imp == true)
9545 return !InvertRHS;
9546 if (std::optional<bool> Imp =
9547 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9548 if (*Imp == true)
9549 return !InvertRHS;
9550 }
9551 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9552 if (std::optional<bool> Imp =
9553 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9554 if (*Imp == false)
9555 return InvertRHS;
9556 if (std::optional<bool> Imp =
9557 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9558 if (*Imp == false)
9559 return InvertRHS;
9560 }
9561
9562 return std::nullopt;
9563}
9564
9565// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9566// condition dominating ContextI or nullptr, if no condition is found.
9567static std::pair<Value *, bool>
9569 if (!ContextI || !ContextI->getParent())
9570 return {nullptr, false};
9571
9572 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9573 // dominator tree (eg, from a SimplifyQuery) instead?
9574 const BasicBlock *ContextBB = ContextI->getParent();
9575 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9576 if (!PredBB)
9577 return {nullptr, false};
9578
9579 // We need a conditional branch in the predecessor.
9580 Value *PredCond;
9581 BasicBlock *TrueBB, *FalseBB;
9582 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9583 return {nullptr, false};
9584
9585 // The branch should get simplified. Don't bother simplifying this condition.
9586 if (TrueBB == FalseBB)
9587 return {nullptr, false};
9588
9589 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9590 "Predecessor block does not point to successor?");
9591
9592 // Is this condition implied by the predecessor condition?
9593 return {PredCond, TrueBB == ContextBB};
9594}
9595
9596std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9597 const Instruction *ContextI,
9598 const DataLayout &DL) {
9599 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9600 auto PredCond = getDomPredecessorCondition(ContextI);
9601 if (PredCond.first)
9602 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9603 return std::nullopt;
9604}
9605
9607 const Value *LHS,
9608 const Value *RHS,
9609 const Instruction *ContextI,
9610 const DataLayout &DL) {
9611 auto PredCond = getDomPredecessorCondition(ContextI);
9612 if (PredCond.first)
9613 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9614 PredCond.second);
9615 return std::nullopt;
9616}
9617
9619 APInt &Upper, const InstrInfoQuery &IIQ,
9620 bool PreferSignedRange) {
9621 unsigned Width = Lower.getBitWidth();
9622 const APInt *C;
9623 switch (BO.getOpcode()) {
9624 case Instruction::Sub:
9625 if (match(BO.getOperand(0), m_APInt(C))) {
9626 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9627 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9628
9629 // If the caller expects a signed compare, then try to use a signed range.
9630 // Otherwise if both no-wraps are set, use the unsigned range because it
9631 // is never larger than the signed range. Example:
9632 // "sub nuw nsw i8 -2, x" is unsigned [0, 254] vs. signed [-128, 126].
9633 // "sub nuw nsw i8 2, x" is unsigned [0, 2] vs. signed [-125, 127].
9634 if (PreferSignedRange && HasNSW && HasNUW)
9635 HasNUW = false;
9636
9637 if (HasNUW) {
9638 // 'sub nuw c, x' produces [0, C].
9639 Upper = *C + 1;
9640 } else if (HasNSW) {
9641 if (C->isNegative()) {
9642 // 'sub nsw -C, x' produces [SINT_MIN, -C - SINT_MIN].
9644 Upper = *C - APInt::getSignedMaxValue(Width);
9645 } else {
9646 // Note that sub 0, INT_MIN is not NSW. It techically is a signed wrap
9647 // 'sub nsw C, x' produces [C - SINT_MAX, SINT_MAX].
9648 Lower = *C - APInt::getSignedMaxValue(Width);
9650 }
9651 }
9652 }
9653 break;
9654 case Instruction::Add:
9655 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9656 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9657 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9658
9659 // If the caller expects a signed compare, then try to use a signed
9660 // range. Otherwise if both no-wraps are set, use the unsigned range
9661 // because it is never larger than the signed range. Example: "add nuw
9662 // nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9663 if (PreferSignedRange && HasNSW && HasNUW)
9664 HasNUW = false;
9665
9666 if (HasNUW) {
9667 // 'add nuw x, C' produces [C, UINT_MAX].
9668 Lower = *C;
9669 } else if (HasNSW) {
9670 if (C->isNegative()) {
9671 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9673 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9674 } else {
9675 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9676 Lower = APInt::getSignedMinValue(Width) + *C;
9677 Upper = APInt::getSignedMaxValue(Width) + 1;
9678 }
9679 }
9680 }
9681 break;
9682
9683 case Instruction::And:
9684 if (match(BO.getOperand(1), m_APInt(C)))
9685 // 'and x, C' produces [0, C].
9686 Upper = *C + 1;
9687 // X & -X is a power of two or zero. So we can cap the value at max power of
9688 // two.
9689 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9690 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9691 Upper = APInt::getSignedMinValue(Width) + 1;
9692 break;
9693
9694 case Instruction::Or:
9695 if (match(BO.getOperand(1), m_APInt(C)))
9696 // 'or x, C' produces [C, UINT_MAX].
9697 Lower = *C;
9698 break;
9699
9700 case Instruction::AShr:
9701 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9702 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9704 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9705 } else if (match(BO.getOperand(0), m_APInt(C))) {
9706 unsigned ShiftAmount = Width - 1;
9707 if (!C->isZero() && IIQ.isExact(&BO))
9708 ShiftAmount = C->countr_zero();
9709 if (C->isNegative()) {
9710 // 'ashr C, x' produces [C, C >> (Width-1)]
9711 Lower = *C;
9712 Upper = C->ashr(ShiftAmount) + 1;
9713 } else {
9714 // 'ashr C, x' produces [C >> (Width-1), C]
9715 Lower = C->ashr(ShiftAmount);
9716 Upper = *C + 1;
9717 }
9718 }
9719 break;
9720
9721 case Instruction::LShr:
9722 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9723 // 'lshr x, C' produces [0, UINT_MAX >> C].
9724 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9725 } else if (match(BO.getOperand(0), m_APInt(C))) {
9726 // 'lshr C, x' produces [C >> (Width-1), C].
9727 unsigned ShiftAmount = Width - 1;
9728 if (!C->isZero() && IIQ.isExact(&BO))
9729 ShiftAmount = C->countr_zero();
9730 Lower = C->lshr(ShiftAmount);
9731 Upper = *C + 1;
9732 }
9733 break;
9734
9735 case Instruction::Shl:
9736 if (match(BO.getOperand(0), m_APInt(C))) {
9737 if (IIQ.hasNoUnsignedWrap(&BO)) {
9738 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9739 Lower = *C;
9740 Upper = Lower.shl(Lower.countl_zero()) + 1;
9741 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9742 if (C->isNegative()) {
9743 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9744 unsigned ShiftAmount = C->countl_one() - 1;
9745 Lower = C->shl(ShiftAmount);
9746 Upper = *C + 1;
9747 } else {
9748 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9749 unsigned ShiftAmount = C->countl_zero() - 1;
9750 Lower = *C;
9751 Upper = C->shl(ShiftAmount) + 1;
9752 }
9753 } else {
9754 // If lowbit is set, value can never be zero.
9755 if ((*C)[0])
9756 Lower = APInt::getOneBitSet(Width, 0);
9757 // If we are shifting a constant the largest it can be is if the longest
9758 // sequence of consecutive ones is shifted to the highbits (breaking
9759 // ties for which sequence is higher). At the moment we take a liberal
9760 // upper bound on this by just popcounting the constant.
9761 // TODO: There may be a bitwise trick for it longest/highest
9762 // consecutative sequence of ones (naive method is O(Width) loop).
9763 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9764 }
9765 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9766 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9767 }
9768 break;
9769
9770 case Instruction::SDiv:
9771 if (match(BO.getOperand(1), m_APInt(C))) {
9772 APInt IntMin = APInt::getSignedMinValue(Width);
9773 APInt IntMax = APInt::getSignedMaxValue(Width);
9774 if (C->isAllOnes()) {
9775 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9776 // where C != -1 and C != 0 and C != 1
9777 Lower = IntMin + 1;
9778 Upper = IntMax + 1;
9779 } else if (C->countl_zero() < Width - 1) {
9780 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9781 // where C != -1 and C != 0 and C != 1
9782 Lower = IntMin.sdiv(*C);
9783 Upper = IntMax.sdiv(*C);
9784 if (Lower.sgt(Upper))
9786 Upper = Upper + 1;
9787 assert(Upper != Lower && "Upper part of range has wrapped!");
9788 }
9789 } else if (match(BO.getOperand(0), m_APInt(C))) {
9790 if (C->isMinSignedValue()) {
9791 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9792 Lower = *C;
9793 Upper = Lower.lshr(1) + 1;
9794 } else {
9795 // 'sdiv C, x' produces [-|C|, |C|].
9796 Upper = C->abs() + 1;
9797 Lower = (-Upper) + 1;
9798 }
9799 }
9800 break;
9801
9802 case Instruction::UDiv:
9803 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9804 // 'udiv x, C' produces [0, UINT_MAX / C].
9805 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9806 } else if (match(BO.getOperand(0), m_APInt(C))) {
9807 // 'udiv C, x' produces [0, C].
9808 Upper = *C + 1;
9809 }
9810 break;
9811
9812 case Instruction::SRem:
9813 if (match(BO.getOperand(1), m_APInt(C))) {
9814 // 'srem x, C' produces (-|C|, |C|).
9815 Upper = C->abs();
9816 Lower = (-Upper) + 1;
9817 } else if (match(BO.getOperand(0), m_APInt(C))) {
9818 if (C->isNegative()) {
9819 // 'srem -|C|, x' produces [-|C|, 0].
9820 Upper = 1;
9821 Lower = *C;
9822 } else {
9823 // 'srem |C|, x' produces [0, |C|].
9824 Upper = *C + 1;
9825 }
9826 }
9827 break;
9828
9829 case Instruction::URem:
9830 if (match(BO.getOperand(1), m_APInt(C)))
9831 // 'urem x, C' produces [0, C).
9832 Upper = *C;
9833 else if (match(BO.getOperand(0), m_APInt(C)))
9834 // 'urem C, x' produces [0, C].
9835 Upper = *C + 1;
9836 break;
9837
9838 default:
9839 break;
9840 }
9841}
9842
9844 bool UseInstrInfo) {
9845 unsigned Width = II.getType()->getScalarSizeInBits();
9846 const APInt *C;
9847 switch (II.getIntrinsicID()) {
9848 case Intrinsic::ctlz:
9849 case Intrinsic::cttz: {
9850 APInt Upper(Width, Width);
9851 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
9852 Upper += 1;
9853 // Maximum of set/clear bits is the bit width.
9855 }
9856 case Intrinsic::ctpop:
9857 // Maximum of set/clear bits is the bit width.
9859 APInt(Width, Width) + 1);
9860 case Intrinsic::uadd_sat:
9861 // uadd.sat(x, C) produces [C, UINT_MAX].
9862 if (match(II.getOperand(0), m_APInt(C)) ||
9863 match(II.getOperand(1), m_APInt(C)))
9865 break;
9866 case Intrinsic::sadd_sat:
9867 if (match(II.getOperand(0), m_APInt(C)) ||
9868 match(II.getOperand(1), m_APInt(C))) {
9869 if (C->isNegative())
9870 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9872 APInt::getSignedMaxValue(Width) + *C +
9873 1);
9874
9875 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9877 APInt::getSignedMaxValue(Width) + 1);
9878 }
9879 break;
9880 case Intrinsic::usub_sat:
9881 // usub.sat(C, x) produces [0, C].
9882 if (match(II.getOperand(0), m_APInt(C)))
9883 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9884
9885 // usub.sat(x, C) produces [0, UINT_MAX - C].
9886 if (match(II.getOperand(1), m_APInt(C)))
9888 APInt::getMaxValue(Width) - *C + 1);
9889 break;
9890 case Intrinsic::ssub_sat:
9891 if (match(II.getOperand(0), m_APInt(C))) {
9892 if (C->isNegative())
9893 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9895 *C - APInt::getSignedMinValue(Width) +
9896 1);
9897
9898 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9900 APInt::getSignedMaxValue(Width) + 1);
9901 } else if (match(II.getOperand(1), m_APInt(C))) {
9902 if (C->isNegative())
9903 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9905 APInt::getSignedMaxValue(Width) + 1);
9906
9907 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9909 APInt::getSignedMaxValue(Width) - *C +
9910 1);
9911 }
9912 break;
9913 case Intrinsic::umin:
9914 case Intrinsic::umax:
9915 case Intrinsic::smin:
9916 case Intrinsic::smax:
9917 if (!match(II.getOperand(0), m_APInt(C)) &&
9918 !match(II.getOperand(1), m_APInt(C)))
9919 break;
9920
9921 switch (II.getIntrinsicID()) {
9922 case Intrinsic::umin:
9923 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9924 case Intrinsic::umax:
9926 case Intrinsic::smin:
9928 *C + 1);
9929 case Intrinsic::smax:
9931 APInt::getSignedMaxValue(Width) + 1);
9932 default:
9933 llvm_unreachable("Must be min/max intrinsic");
9934 }
9935 break;
9936 case Intrinsic::abs:
9937 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
9938 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9939 if (match(II.getOperand(1), m_One()))
9941 APInt::getSignedMaxValue(Width) + 1);
9942
9944 APInt::getSignedMinValue(Width) + 1);
9945 case Intrinsic::vscale:
9946 if (!II.getParent() || !II.getFunction())
9947 break;
9948 return getVScaleRange(II.getFunction(), Width);
9949 default:
9950 break;
9951 }
9952
9953 return ConstantRange::getFull(Width);
9954}
9955
9957 const InstrInfoQuery &IIQ) {
9958 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
9959 const Value *LHS = nullptr, *RHS = nullptr;
9961 if (R.Flavor == SPF_UNKNOWN)
9962 return ConstantRange::getFull(BitWidth);
9963
9964 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
9965 // If the negation part of the abs (in RHS) has the NSW flag,
9966 // then the result of abs(X) is [0..SIGNED_MAX],
9967 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9968 if (match(RHS, m_Neg(m_Specific(LHS))) &&
9969 IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
9972
9975 }
9976
9977 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
9978 // The result of -abs(X) is <= 0.
9980 APInt(BitWidth, 1));
9981 }
9982
9983 const APInt *C;
9984 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
9985 return ConstantRange::getFull(BitWidth);
9986
9987 switch (R.Flavor) {
9988 case SPF_UMIN:
9990 case SPF_UMAX:
9992 case SPF_SMIN:
9994 *C + 1);
9995 case SPF_SMAX:
9998 default:
9999 return ConstantRange::getFull(BitWidth);
10000 }
10001}
10002
10004 // The maximum representable value of a half is 65504. For floats the maximum
10005 // value is 3.4e38 which requires roughly 129 bits.
10006 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10007 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10008 return;
10009 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10010 Lower = APInt(BitWidth, -65504, true);
10011 Upper = APInt(BitWidth, 65505);
10012 }
10013
10014 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10015 // For a fptoui the lower limit is left as 0.
10016 Upper = APInt(BitWidth, 65505);
10017 }
10018}
10019
10021 bool UseInstrInfo, AssumptionCache *AC,
10022 const Instruction *CtxI,
10023 const DominatorTree *DT,
10024 unsigned Depth) {
10025 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10026
10028 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10029
10030 if (auto *C = dyn_cast<Constant>(V))
10031 return C->toConstantRange();
10032
10033 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10034 InstrInfoQuery IIQ(UseInstrInfo);
10035 ConstantRange CR = ConstantRange::getFull(BitWidth);
10036 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10037 APInt Lower = APInt(BitWidth, 0);
10038 APInt Upper = APInt(BitWidth, 0);
10039 // TODO: Return ConstantRange.
10040 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10042 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10043 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10044 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10046 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10048 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10049 CR = CRTrue.unionWith(CRFalse);
10050 CR = CR.intersectWith(getRangeForSelectPattern(*SI, IIQ));
10051 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10052 APInt Lower = APInt(BitWidth, 0);
10053 APInt Upper = APInt(BitWidth, 0);
10054 // TODO: Return ConstantRange.
10055 setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
10057 } else if (const auto *A = dyn_cast<Argument>(V))
10058 if (std::optional<ConstantRange> Range = A->getRange())
10059 CR = *Range;
10060
10061 if (auto *I = dyn_cast<Instruction>(V)) {
10062 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10064
10065 if (const auto *CB = dyn_cast<CallBase>(V))
10066 if (std::optional<ConstantRange> Range = CB->getRange())
10067 CR = CR.intersectWith(*Range);
10068 }
10069
10070 if (CtxI && AC) {
10071 // Try to restrict the range based on information from assumptions.
10072 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10073 if (!AssumeVH)
10074 continue;
10075 CallInst *I = cast<CallInst>(AssumeVH);
10076 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10077 "Got assumption for the wrong function!");
10078 assert(I->getIntrinsicID() == Intrinsic::assume &&
10079 "must be an assume intrinsic");
10080
10081 if (!isValidAssumeForContext(I, CtxI, DT))
10082 continue;
10083 Value *Arg = I->getArgOperand(0);
10084 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10085 // Currently we just use information from comparisons.
10086 if (!Cmp || Cmp->getOperand(0) != V)
10087 continue;
10088 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10090 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10091 UseInstrInfo, AC, I, DT, Depth + 1);
10092 CR = CR.intersectWith(
10093 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10094 }
10095 }
10096
10097 return CR;
10098}
10099
10100static void
10102 function_ref<void(Value *)> InsertAffected) {
10103 assert(V != nullptr);
10104 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10105 InsertAffected(V);
10106 } else if (auto *I = dyn_cast<Instruction>(V)) {
10107 InsertAffected(V);
10108
10109 // Peek through unary operators to find the source of the condition.
10110 Value *Op;
10112 if (isa<Instruction>(Op) || isa<Argument>(Op))
10113 InsertAffected(Op);
10114 }
10115 }
10116}
10117
10119 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10120 auto AddAffected = [&InsertAffected](Value *V) {
10121 addValueAffectedByCondition(V, InsertAffected);
10122 };
10123
10124 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10125 if (IsAssume) {
10126 AddAffected(LHS);
10127 AddAffected(RHS);
10128 } else if (match(RHS, m_Constant()))
10129 AddAffected(LHS);
10130 };
10131
10132 SmallVector<Value *, 8> Worklist;
10134 Worklist.push_back(Cond);
10135 while (!Worklist.empty()) {
10136 Value *V = Worklist.pop_back_val();
10137 if (!Visited.insert(V).second)
10138 continue;
10139
10140 CmpPredicate Pred;
10141 Value *A, *B, *X;
10142
10143 if (IsAssume) {
10144 AddAffected(V);
10145 if (match(V, m_Not(m_Value(X))))
10146 AddAffected(X);
10147 }
10148
10149 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10150 // assume(A && B) is split to -> assume(A); assume(B);
10151 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10152 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10153 // enough information to be worth handling (intersection of information as
10154 // opposed to union).
10155 if (!IsAssume) {
10156 Worklist.push_back(A);
10157 Worklist.push_back(B);
10158 }
10159 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10160 bool HasRHSC = match(B, m_ConstantInt());
10161 if (ICmpInst::isEquality(Pred)) {
10162 AddAffected(A);
10163 if (IsAssume)
10164 AddAffected(B);
10165 if (HasRHSC) {
10166 Value *Y;
10167 // (X & C) or (X | C).
10168 // (X << C) or (X >>_s C) or (X >>_u C).
10169 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10170 AddAffected(X);
10171 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10172 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10173 AddAffected(X);
10174 AddAffected(Y);
10175 }
10176 }
10177 } else {
10178 AddCmpOperands(A, B);
10179 if (HasRHSC) {
10180 // Handle (A + C1) u< C2, which is the canonical form of
10181 // A > C3 && A < C4.
10183 AddAffected(X);
10184
10185 if (ICmpInst::isUnsigned(Pred)) {
10186 Value *Y;
10187 // X & Y u> C -> X >u C && Y >u C
10188 // X | Y u< C -> X u< C && Y u< C
10189 // X nuw+ Y u< C -> X u< C && Y u< C
10190 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10191 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10192 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10193 AddAffected(X);
10194 AddAffected(Y);
10195 }
10196 // X nuw- Y u> C -> X u> C
10197 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10198 AddAffected(X);
10199 }
10200 }
10201
10202 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10203 // by computeKnownFPClass().
10205 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10206 InsertAffected(X);
10207 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10208 InsertAffected(X);
10209 }
10210 }
10211
10212 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10213 AddAffected(X);
10214 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10215 AddCmpOperands(A, B);
10216
10217 // fcmp fneg(x), y
10218 // fcmp fabs(x), y
10219 // fcmp fneg(fabs(x)), y
10220 if (match(A, m_FNeg(m_Value(A))))
10221 AddAffected(A);
10222 if (match(A, m_FAbs(m_Value(A))))
10223 AddAffected(A);
10224
10225 } else if (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
10226 m_Value()))) {
10227 // Handle patterns that computeKnownFPClass() support.
10228 AddAffected(A);
10229 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10230 // Assume is checked here as X is already added above for assumes in
10231 // addValueAffectedByCondition
10232 AddAffected(X);
10233 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10234 // Assume is checked here to avoid issues with ephemeral values
10235 Worklist.push_back(X);
10236 }
10237 }
10238}
10239
10241 // (X >> C) or/add (X & mask(C) != 0)
10242 if (const auto *BO = dyn_cast<BinaryOperator>(V)) {
10243 if (BO->getOpcode() == Instruction::Add ||
10244 BO->getOpcode() == Instruction::Or) {
10245 const Value *X;
10246 const APInt *C1, *C2;
10247 if (match(BO, m_c_BinOp(m_LShr(m_Value(X), m_APInt(C1)),
10249 ICmpInst::ICMP_NE,
10251 m_Zero())))) &&
10252 C2->popcount() == C1->getZExtValue())
10253 return X;
10254 }
10255 }
10256 return nullptr;
10257}
10258
10260 return const_cast<Value *>(stripNullTest(const_cast<const Value *>(V)));
10261}
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...
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1328
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Utilities for dealing with flags related to floating point properties and mode controls.
Hexagon Common GEP
static MaybeAlign getAlign(Value *Ptr)
Definition: IRBuilder.cpp:442
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
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
PowerPC Reduce CR logical Operation
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
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 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 bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext, unsigned Depth=0)
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, SimplifyQuery &Q, unsigned Depth)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
static unsigned computeNumSignBitsVectorConstant(const Value *V, const APInt &DemandedElts, unsigned TyBits)
For vector constants, loop over the elements and find the constant with the minimum number of sign bi...
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V1 == (binop V2, X), where X is known non-zero.
static bool isGEPKnownNonNull(const GEPOperator *GEP, const SimplifyQuery &Q, unsigned Depth)
Test whether a GEP's result is known to be non-null.
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroMul(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW, unsigned Depth)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, const SimplifyQuery &Q, unsigned Depth)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool isNonZeroSub(const APInt &DemandedElts, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, unsigned Depth)
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static bool isKnownNonZero(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if the given value is known to be non-zero when defined.
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, const SimplifyQuery &Q, const KnownBits &KnownVal, unsigned Depth)
UndefPoisonKind
static bool isKnownNonEqualFromContext(const Value *V1, const Value *V2, const SimplifyQuery &Q, unsigned Depth)
static bool includesPoison(UndefPoisonKind Kind)
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static std::optional< bool > isImpliedCondICmps(CmpPredicate LPred, const Value *L0, const Value *L1, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool includesUndef(UndefPoisonKind Kind)
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, FastMathFlags FMF, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
static bool isKnownNonEqual(const Value *V1, const Value *V2, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if it is known that V1 != V2.
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 ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,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 bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC, const Instruction *CtxI, const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind)
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 bool canCreateUndefOrPoison(const Operator *Op, UndefPoisonKind Kind, bool ConsiderFlagsAndMetadata)
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
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 bool impliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
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 void computeKnownBits(const Value *V, const APInt &DemandedElts, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth)
Determine which bits of V are known to be either zero or one and return them in the Known bit set.
static OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const AddOperator *Add, const SimplifyQuery &SQ)
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, const SimplifyQuery &Q, unsigned Depth)
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 Pred ALHS ARHS" is true.
static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly)
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 unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, 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
APInt bitcastToAPInt() const
Definition: APFloat.h:1353
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
LLVM_ABI APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1033
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:206
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1330
unsigned ceilLogBase2() const
Definition: APInt.h:1764
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1201
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:371
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
LLVM_ABI APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1666
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1488
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:209
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:216
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:329
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition: APInt.h:1249
LLVM_ABI APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1644
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1396
LLVM_ABI APInt reverseBits() const
Definition: APInt.cpp:768
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1166
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1628
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
LLVM_ABI APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1041
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:356
unsigned logBase2() const
Definition: APInt.h:1761
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:827
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1319
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:471
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:405
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1150
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:873
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1130
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:296
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1388
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1237
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:239
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
void clearSignBit()
Set the sign bit to 0.
Definition: APInt.h:1449
an instruction to allocate memory on the stack
Definition: Instructions.h:64
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.
Definition: DerivedTypes.h:398
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.
LLVM_ABI std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
Definition: Attributes.cpp:474
LLVM_ABI unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
Definition: Attributes.cpp:468
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.
Definition: Dominators.cpp:52
LLVM Basic Block Representation.
Definition: BasicBlock.h:62
iterator end()
Definition: BasicBlock.h:472
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:459
LLVM_ABI InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:337
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.
Definition: BasicBlock.cpp:437
LLVM_ABI const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:467
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:213
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...
Definition: InstrTypes.h:1116
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1348
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
Definition: InstrTypes.h:1751
Value * getCalledOperand() const
Definition: InstrTypes.h:1340
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1292
unsigned arg_size() const
Definition: InstrTypes.h:1290
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 isFPPredicate() const
Definition: InstrTypes.h:784
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
bool isIntPredicate() const
Definition: InstrTypes.h:785
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...
Definition: CmpPredicate.h:23
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.
Definition: CmpPredicate.h:47
bool hasSameSign() const
Query samesign information, for optimizations.
Definition: CmpPredicate.h:43
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
LLVM_ABI uint64_t getElementAsInteger(uint64_t i) const
If this is a sequential container of integers (of any size), return the specified element in the low ...
Definition: Constants.cpp:3112
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)
Definition: Constants.cpp:2647
static LLVM_ABI Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2328
static LLVM_ABI Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2272
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)
Definition: Constants.cpp:868
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.
Definition: ConstantRange.h:47
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 APInt getUnsignedMin() const
Return the smallest unsigned value contained in the ConstantRange.
LLVM_ABI bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other? NOTE: false does not mean that inverse pr...
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 APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
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.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
Definition: ConstantRange.h:84
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.
Definition: Constants.cpp:784
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1713
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
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
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:198
LLVM_ABI const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:708
LLVM_ABI unsigned getIndexTypeSizeInBits(Type *Ty) const
The size in bits of the index used in GEP calculation for this type.
Definition: DataLayout.cpp:753
LLVM_ABI unsigned getPointerTypeSizeInBits(Type *) const
The pointer representation size in bits for this type.
Definition: DataLayout.cpp:742
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:674
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
NodeT * getBlock() 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.
Definition: Dominators.cpp:135
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static LLVM_ABI Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:200
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:22
bool noSignedZeros() const
Definition: FMF.h:67
bool noInfs() const
Definition: FMF.h:66
void setNoSignedZeros(bool B=true)
Definition: FMF.h:84
void setNoNaNs(bool B=true)
Definition: FMF.h:78
bool noNaNs() const
Definition: FMF.h:65
const BasicBlock & getEntryBlock() const
Definition: Function.h:807
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition: Function.cpp:803
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:949
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition: Globals.cpp:132
Type * getValueType() const
Definition: GlobalValue.h:298
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.
bool isEquality() const
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.
Value * getAggregateOperand()
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
Definition: Instruction.h:317
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.
Definition: Instruction.cpp:82
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.
Definition: Instruction.h:312
bool isUnaryOp() const
Definition: Instruction.h:316
LLVM_ABI const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Definition: Instruction.cpp:86
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:49
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
An instruction for reading from memory.
Definition: Instructions.h:180
Value * getPointerOperand()
Definition: Instructions.h:259
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:215
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:40
Metadata node.
Definition: Metadata.h:1077
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:33
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:43
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:78
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1885
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...
Definition: SmallPtrSet.h:380
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:470
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:401
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:541
bool empty() const
Definition: SmallVector.h:82
size_t size() const
Definition: SmallVector.h:79
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:574
void reserve(size_type N)
Definition: SmallVector.h:664
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:684
void push_back(const T &Elt)
Definition: SmallVector.h:414
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1197
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:626
TypeSize getElementOffset(unsigned Idx) const
Definition: DataLayout.h:657
Class to represent struct types.
Definition: DerivedTypes.h:218
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:368
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:369
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
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:273
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:246
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:267
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
static LLVM_ABI IntegerType * getInt64Ty(LLVMContext &C)
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:311
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
static LLVM_ABI IntegerType * getInt16Ty(LLVMContext &C)
LLVM_ABI const fltSemantics & getFltSemantics() const
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:240
bool isIEEELikeFPTy() const
Return true if this is a well-behaved IEEE-like type, which has a IEEE compatible layout,...
Definition: Type.h:170
LLVM_ABI unsigned getIntegerBitWidth() const
LLVM_ABI uint64_t getArrayNumElements() const
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:352
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1866
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
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:203
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:172
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:169
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition: ilist_node.h:34
self_iterator getIterator()
Definition: ilist_node.h:134
A range adaptor for a pair of iterators.
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.
Definition: PatternMatch.h:524
cst_pred_ty< is_lowbit_mask > m_LowBitMask()
Match an integer or vector with only the low bit(s) set.
Definition: PatternMatch.h:673
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.
Definition: PatternMatch.h:664
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.
Definition: PatternMatch.h:619
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.
Definition: PatternMatch.h:165
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.
Definition: PatternMatch.h:652
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)
Definition: PatternMatch.h:49
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.
Definition: PatternMatch.h:766
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:962
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
Definition: PatternMatch.h:560
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
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.
Definition: PatternMatch.h:876
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()...
Definition: PatternMatch.h:980
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.
Definition: PatternMatch.h:931
NoWrapTrunc_match< OpTy, TruncInst::NoUnsignedWrap > m_NUWTrunc(const OpTy &Op)
Matches trunc nuw.
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
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.
Definition: PatternMatch.h:92
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:316
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.
Definition: PatternMatch.h:189
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.
Definition: PatternMatch.h:582
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.
Definition: PatternMatch.h:612
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
static unsigned decodeVSEW(unsigned VSEW)
LLVM_ABI unsigned getSEWLMULRatio(unsigned SEW, VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:444
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
LLVM_ABI bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
LLVM_ABI bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
LLVM_ABI Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
@ Offset
Definition: DWP.cpp:477
@ Length
Definition: DWP.cpp:477
OverflowResult
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:1744
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:1702
LLVM_ABI bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
LLVM_ABI bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
LLVM_ABI bool isKnownNeverInfinity(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if the floating-point scalar value is not an infinity or if the floating-point vector val...
LLVM_ABI void computeKnownBitsFromContext(const Value *V, KnownBits &Known, const SimplifyQuery &Q, unsigned Depth=0)
Merge bits known from context-dependent facts into Known.
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
LLVM_ABI bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
LLVM_ABI bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
LLVM_ABI const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
LLVM_ABI bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
LLVM_ABI AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
LLVM_ABI APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
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:232
LLVM_ABI bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
LLVM_ABI Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
LLVM_ABI Value * stripNullTest(Value *V)
Returns the inner value X if the expression has the form f(X) where f(X) == 0 if and only if X == 0,...
LLVM_ABI bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
LLVM_ABI std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition: bit.h:270
LLVM_ABI bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2155
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.
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:342
LLVM_ABI bool canIgnoreSignBitOfZero(const Use &U)
Return true if the sign bit of the FP value can be ignored by the user when the value is zero.
LLVM_ABI bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
LLVM_ABI ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, FPClassTest RHSClass, bool LookThroughSrc=true)
LLVM_ABI ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
LLVM_ABI bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: bit.h:157
LLVM_ABI bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
LLVM_ABI RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
LLVM_ABI bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
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:1751
LLVM_ABI OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
LLVM_ABI bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:336
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
Definition: GuardUtils.cpp:18
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition: bit.h:203
LLVM_ABI SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
Definition: ValueTracking.h:47
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_SMIN
@ 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 ...
Definition: Function.cpp:1172
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...
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:
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.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
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.
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
Definition: BitmaskEnum.h:223
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.
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:1916
LLVM_ABI OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
LLVM_ABI bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true, unsigned Depth=0)
Return true if the given value is known to have exactly one bit set when defined.
LLVM_ABI std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
LLVM_ABI bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
LLVM_ABI void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
LLVM_ABI Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
LLVM_ABI bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
LLVM_ABI bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
LLVM_ABI Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
LLVM_ABI bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
LLVM_ABI bool cannotBeOrderedLessThanZero(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if we can prove that the specified FP value is either NaN or never less than -0....
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
LLVM_ABI bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
Definition: VectorUtils.cpp:46
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:858
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Definition: SimplifyQuery.h:66
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
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...
Definition: SimplifyQuery.h:26
bool isExact(const BinaryOperator *Op) const
Definition: SimplifyQuery.h:49
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
Definition: SimplifyQuery.h:31
bool hasNoSignedZeros(const InstT *Op) const
Definition: SimplifyQuery.h:55
bool hasNoSignedWrap(const InstT *Op) const
Definition: SimplifyQuery.h:43
bool hasNoUnsignedWrap(const InstT *Op) const
Definition: SimplifyQuery.h:37
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:294
static LLVM_ABI KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
Definition: KnownBits.cpp:764
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.
Definition: KnownBits.cpp:487
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition: KnownBits.h:179
static LLVM_ABI KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:916
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:248
static LLVM_ABI KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:211
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:101
LLVM_ABI KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
Definition: KnownBits.cpp:1127
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:117
static LLVM_ABI KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:773
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition: KnownBits.h:244
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:235
static LLVM_ABI KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:427
static LLVM_ABI KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
Definition: KnownBits.cpp:767
static LLVM_ABI KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1056
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:66
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:267
LLVM_ABI KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
Definition: KnownBits.cpp:1138
void makeNegative()
Make this value negative.
Definition: KnownBits.h:112
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:154
KnownBits byteSwap() const
Definition: KnownBits.h:507
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:51
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:282
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:86
KnownBits reverseBits() const
Definition: KnownBits.h:511
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:44
static LLVM_ABI KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:187
KnownBits zext(unsigned BitWidth) const
Return known bits for a zero extension of the value we're tracking.
Definition: KnownBits.h:165
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:54
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:74
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition: KnownBits.h:314
static LLVM_ABI KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:370
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:104
KnownBits extractBits(unsigned NumBits, unsigned BitPosition) const
Return a subset of the known bits from [bitPosition,bitPosition+numBits).
Definition: KnownBits.h:218
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:304
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:173
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition: KnownBits.h:238
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition: KnownBits.h:340
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:189
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:241
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:138
static LLVM_ABI KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:215
static LLVM_ABI KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:908
static LLVM_ABI KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1073
static LLVM_ABI KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1016
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).
Definition: KnownBits.cpp:960
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:319
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:98
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition: KnownBits.h:346
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:273
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition: KnownBits.h:92
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:212
static LLVM_ABI KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
Definition: KnownBits.cpp:770
static LLVM_ABI KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:803
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition: KnownBits.h:160
LLVM_ABI KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:549
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.
Definition: KnownBits.cpp:525
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.
Definition: KnownBits.cpp:511
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).
Definition: KnownBits.cpp:285
static LLVM_ABI KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:205
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
Definition: KnownFPClass.h:25
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
Definition: KnownFPClass.h:51
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
Definition: KnownFPClass.h:114
static constexpr FPClassTest OrderedGreaterThanZeroMask
Definition: KnownFPClass.h:92
static constexpr FPClassTest OrderedLessThanZeroMask
Definition: KnownFPClass.h:90
void knownNot(FPClassTest RuleOut)
Definition: KnownFPClass.h:126
void copysign(const KnownFPClass &Sign)
Definition: KnownFPClass.h:173
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
Definition: KnownFPClass.h:60
LLVM_ABI bool isKnownNeverLogicalZero(DenormalMode Mode) const
Return true if it's know this can never be interpreted as a zero.
bool isUnknown() const
Definition: KnownFPClass.h:42
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
Definition: KnownFPClass.h:57
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
Definition: KnownFPClass.h:66
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
Definition: KnownFPClass.h:73
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 ...
Definition: KnownFPClass.h:29
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
Definition: KnownFPClass.h:45
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
Definition: KnownFPClass.h:36
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
Definition: KnownFPClass.h:77
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
Definition: KnownFPClass.h:198
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
Definition: KnownFPClass.h:103
void signBitMustBeOne()
Assume the sign bit is one.
Definition: KnownFPClass.h:168
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.
Definition: KnownFPClass.h:162
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.
Definition: KnownFPClass.h:54
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.
Definition: KnownFPClass.h:63
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
Definition: SimplifyQuery.h:72
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
Definition: SimplifyQuery.h:76
const DominatorTree * DT
Definition: SimplifyQuery.h:74
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
Definition: SimplifyQuery.h:75
const DomConditionCache * DC
Definition: SimplifyQuery.h:77
const InstrInfoQuery IIQ
Definition: SimplifyQuery.h:83
const CondContext * CC
Definition: SimplifyQuery.h:78