LLVM 21.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"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/ScopeExit.h"
21#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/StringRef.h"
32#include "llvm/Analysis/Loads.h"
37#include "llvm/IR/Argument.h"
38#include "llvm/IR/Attributes.h"
39#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/Constant.h"
42#include "llvm/IR/Constants.h"
45#include "llvm/IR/Dominators.h"
47#include "llvm/IR/Function.h"
49#include "llvm/IR/GlobalAlias.h"
50#include "llvm/IR/GlobalValue.h"
52#include "llvm/IR/InstrTypes.h"
53#include "llvm/IR/Instruction.h"
56#include "llvm/IR/Intrinsics.h"
57#include "llvm/IR/IntrinsicsAArch64.h"
58#include "llvm/IR/IntrinsicsAMDGPU.h"
59#include "llvm/IR/IntrinsicsRISCV.h"
60#include "llvm/IR/IntrinsicsX86.h"
61#include "llvm/IR/LLVMContext.h"
62#include "llvm/IR/Metadata.h"
63#include "llvm/IR/Module.h"
64#include "llvm/IR/Operator.h"
66#include "llvm/IR/Type.h"
67#include "llvm/IR/User.h"
68#include "llvm/IR/Value.h"
76#include <algorithm>
77#include <cassert>
78#include <cstdint>
79#include <optional>
80#include <utility>
81
82using namespace llvm;
83using namespace llvm::PatternMatch;
84
85// Controls the number of uses of the value searched for possible
86// dominating comparisons.
87static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
88 cl::Hidden, cl::init(20));
89
90
91/// Returns the bitwidth of the given scalar or pointer type. For vector types,
92/// returns the element type's bitwidth.
93static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
94 if (unsigned BitWidth = Ty->getScalarSizeInBits())
95 return BitWidth;
96
97 return DL.getPointerTypeSizeInBits(Ty);
98}
99
100// Given the provided Value and, potentially, a context instruction, return
101// the preferred context instruction (if any).
102static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
103 // If we've been provided with a context instruction, then use that (provided
104 // it has been inserted).
105 if (CxtI && CxtI->getParent())
106 return CxtI;
107
108 // If the value is really an already-inserted instruction, then use that.
109 CxtI = dyn_cast<Instruction>(V);
110 if (CxtI && CxtI->getParent())
111 return CxtI;
112
113 return nullptr;
114}
115
117 const APInt &DemandedElts,
118 APInt &DemandedLHS, APInt &DemandedRHS) {
119 if (isa<ScalableVectorType>(Shuf->getType())) {
120 assert(DemandedElts == APInt(1,1));
121 DemandedLHS = DemandedRHS = DemandedElts;
122 return true;
123 }
124
125 int NumElts =
126 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
127 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
128 DemandedElts, DemandedLHS, DemandedRHS);
129}
130
131static void computeKnownBits(const Value *V, const APInt &DemandedElts,
132 KnownBits &Known, unsigned Depth,
133 const SimplifyQuery &Q);
134
135void llvm::computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
136 const SimplifyQuery &Q) {
137 // Since the number of lanes in a scalable vector is unknown at compile time,
138 // we track one bit which is implicitly broadcast to all lanes. This means
139 // that all lanes in a scalable vector are considered demanded.
140 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
141 APInt DemandedElts =
142 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
143 ::computeKnownBits(V, DemandedElts, Known, Depth, Q);
144}
145
147 const DataLayout &DL, unsigned Depth,
148 AssumptionCache *AC, const Instruction *CxtI,
149 const DominatorTree *DT, bool UseInstrInfo) {
151 V, Known, Depth,
152 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
153}
154
156 unsigned Depth, AssumptionCache *AC,
157 const Instruction *CxtI,
158 const DominatorTree *DT, bool UseInstrInfo) {
159 return computeKnownBits(
160 V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
161}
162
163KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
164 const DataLayout &DL, unsigned Depth,
165 AssumptionCache *AC, const Instruction *CxtI,
166 const DominatorTree *DT, bool UseInstrInfo) {
167 return computeKnownBits(
168 V, DemandedElts, Depth,
169 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
170}
171
172static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS,
173 const SimplifyQuery &SQ) {
174 // Look for an inverted mask: (X & ~M) op (Y & M).
175 {
176 Value *M;
177 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
179 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
180 return true;
181 }
182
183 // X op (Y & ~X)
186 return true;
187
188 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
189 // for constant Y.
190 Value *Y;
191 if (match(RHS,
193 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
194 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
195 return true;
196
197 // Peek through extends to find a 'not' of the other side:
198 // (ext Y) op ext(~Y)
199 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
201 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
202 return true;
203
204 // Look for: (A & B) op ~(A | B)
205 {
206 Value *A, *B;
207 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
209 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
210 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
211 return true;
212 }
213
214 // Look for: (X << V) op (Y >> (BitWidth - V))
215 // or (X >> V) op (Y << (BitWidth - V))
216 {
217 const Value *V;
218 const APInt *R;
219 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
220 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
221 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
222 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
223 R->uge(LHS->getType()->getScalarSizeInBits()))
224 return true;
225 }
226
227 return false;
228}
229
231 const WithCache<const Value *> &RHSCache,
232 const SimplifyQuery &SQ) {
233 const Value *LHS = LHSCache.getValue();
234 const Value *RHS = RHSCache.getValue();
235
236 assert(LHS->getType() == RHS->getType() &&
237 "LHS and RHS should have the same type");
239 "LHS and RHS should be integers");
240
243 return true;
244
246 RHSCache.getKnownBits(SQ));
247}
248
250 return !I->user_empty() && all_of(I->users(), [](const User *U) {
251 return match(U, m_ICmp(m_Value(), m_Zero()));
252 });
253}
254
256 return !I->user_empty() && all_of(I->users(), [](const User *U) {
257 CmpPredicate P;
258 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
259 });
260}
261
263 bool OrZero, unsigned Depth,
264 AssumptionCache *AC, const Instruction *CxtI,
265 const DominatorTree *DT, bool UseInstrInfo) {
266 return ::isKnownToBeAPowerOfTwo(
267 V, OrZero, Depth,
268 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
269}
270
271static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
272 const SimplifyQuery &Q, unsigned Depth);
273
275 unsigned Depth) {
276 return computeKnownBits(V, Depth, SQ).isNonNegative();
277}
278
280 unsigned Depth) {
281 if (auto *CI = dyn_cast<ConstantInt>(V))
282 return CI->getValue().isStrictlyPositive();
283
284 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
285 // this updated.
286 KnownBits Known = computeKnownBits(V, Depth, SQ);
287 return Known.isNonNegative() &&
288 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
289}
290
292 unsigned Depth) {
293 return computeKnownBits(V, Depth, SQ).isNegative();
294}
295
296static bool isKnownNonEqual(const Value *V1, const Value *V2,
297 const APInt &DemandedElts, unsigned Depth,
298 const SimplifyQuery &Q);
299
300bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
301 const SimplifyQuery &Q, unsigned Depth) {
302 // We don't support looking through casts.
303 if (V1 == V2 || V1->getType() != V2->getType())
304 return false;
305 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
306 APInt DemandedElts =
307 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
308 return ::isKnownNonEqual(V1, V2, DemandedElts, Depth, Q);
309}
310
311bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
312 const SimplifyQuery &SQ, unsigned Depth) {
313 KnownBits Known(Mask.getBitWidth());
314 computeKnownBits(V, Known, Depth, SQ);
315 return Mask.isSubsetOf(Known.Zero);
316}
317
318static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
319 unsigned Depth, const SimplifyQuery &Q);
320
321static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
322 const SimplifyQuery &Q) {
323 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
324 APInt DemandedElts =
325 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
326 return ComputeNumSignBits(V, DemandedElts, Depth, Q);
327}
328
329unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
330 unsigned Depth, AssumptionCache *AC,
331 const Instruction *CxtI,
332 const DominatorTree *DT, bool UseInstrInfo) {
333 return ::ComputeNumSignBits(
334 V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
335}
336
338 unsigned Depth, AssumptionCache *AC,
339 const Instruction *CxtI,
340 const DominatorTree *DT) {
341 unsigned SignBits = ComputeNumSignBits(V, DL, Depth, AC, CxtI, DT);
342 return V->getType()->getScalarSizeInBits() - SignBits + 1;
343}
344
345static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
346 bool NSW, bool NUW,
347 const APInt &DemandedElts,
348 KnownBits &KnownOut, KnownBits &Known2,
349 unsigned Depth, const SimplifyQuery &Q) {
350 computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q);
351
352 // If one operand is unknown and we have no nowrap information,
353 // the result will be unknown independently of the second operand.
354 if (KnownOut.isUnknown() && !NSW && !NUW)
355 return;
356
357 computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
358 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
359}
360
361static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
362 bool NUW, const APInt &DemandedElts,
363 KnownBits &Known, KnownBits &Known2,
364 unsigned Depth, const SimplifyQuery &Q) {
365 computeKnownBits(Op1, DemandedElts, Known, Depth + 1, Q);
366 computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
367
368 bool isKnownNegative = false;
369 bool isKnownNonNegative = false;
370 // If the multiplication is known not to overflow, compute the sign bit.
371 if (NSW) {
372 if (Op0 == Op1) {
373 // The product of a number with itself is non-negative.
374 isKnownNonNegative = true;
375 } else {
376 bool isKnownNonNegativeOp1 = Known.isNonNegative();
377 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
378 bool isKnownNegativeOp1 = Known.isNegative();
379 bool isKnownNegativeOp0 = Known2.isNegative();
380 // The product of two numbers with the same sign is non-negative.
381 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
382 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
383 if (!isKnownNonNegative && NUW) {
384 // mul nuw nsw with a factor > 1 is non-negative.
386 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
387 KnownBits::sgt(Known2, One).value_or(false);
388 }
389
390 // The product of a negative number and a non-negative number is either
391 // negative or zero.
394 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
395 Known2.isNonZero()) ||
396 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
397 }
398 }
399
400 bool SelfMultiply = Op0 == Op1;
401 if (SelfMultiply)
402 SelfMultiply &=
403 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
404 Known = KnownBits::mul(Known, Known2, SelfMultiply);
405
406 // Only make use of no-wrap flags if we failed to compute the sign bit
407 // directly. This matters if the multiplication always overflows, in
408 // which case we prefer to follow the result of the direct computation,
409 // though as the program is invoking undefined behaviour we can choose
410 // whatever we like here.
411 if (isKnownNonNegative && !Known.isNegative())
412 Known.makeNonNegative();
413 else if (isKnownNegative && !Known.isNonNegative())
414 Known.makeNegative();
415}
416
418 KnownBits &Known) {
419 unsigned BitWidth = Known.getBitWidth();
420 unsigned NumRanges = Ranges.getNumOperands() / 2;
421 assert(NumRanges >= 1);
422
423 Known.Zero.setAllBits();
424 Known.One.setAllBits();
425
426 for (unsigned i = 0; i < NumRanges; ++i) {
428 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
430 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
431 ConstantRange Range(Lower->getValue(), Upper->getValue());
432
433 // The first CommonPrefixBits of all values in Range are equal.
434 unsigned CommonPrefixBits =
436 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
438 Known.One &= UnsignedMax & Mask;
439 Known.Zero &= ~UnsignedMax & Mask;
440 }
441}
442
443static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
447
448 // The instruction defining an assumption's condition itself is always
449 // considered ephemeral to that assumption (even if it has other
450 // non-ephemeral users). See r246696's test case for an example.
451 if (is_contained(I->operands(), E))
452 return true;
453
454 while (!WorkSet.empty()) {
455 const Value *V = WorkSet.pop_back_val();
456 if (!Visited.insert(V).second)
457 continue;
458
459 // If all uses of this value are ephemeral, then so is this value.
460 if (llvm::all_of(V->users(), [&](const User *U) {
461 return EphValues.count(U);
462 })) {
463 if (V == E)
464 return true;
465
466 if (V == I || (isa<Instruction>(V) &&
467 !cast<Instruction>(V)->mayHaveSideEffects() &&
468 !cast<Instruction>(V)->isTerminator())) {
469 EphValues.insert(V);
470 if (const User *U = dyn_cast<User>(V))
471 append_range(WorkSet, U->operands());
472 }
473 }
474 }
475
476 return false;
477}
478
479// Is this an intrinsic that cannot be speculated but also cannot trap?
481 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
482 return CI->isAssumeLikeIntrinsic();
483
484 return false;
485}
486
488 const Instruction *CxtI,
489 const DominatorTree *DT,
490 bool AllowEphemerals) {
491 // There are two restrictions on the use of an assume:
492 // 1. The assume must dominate the context (or the control flow must
493 // reach the assume whenever it reaches the context).
494 // 2. The context must not be in the assume's set of ephemeral values
495 // (otherwise we will use the assume to prove that the condition
496 // feeding the assume is trivially true, thus causing the removal of
497 // the assume).
498
499 if (Inv->getParent() == CxtI->getParent()) {
500 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
501 // in the BB.
502 if (Inv->comesBefore(CxtI))
503 return true;
504
505 // Don't let an assume affect itself - this would cause the problems
506 // `isEphemeralValueOf` is trying to prevent, and it would also make
507 // the loop below go out of bounds.
508 if (!AllowEphemerals && Inv == CxtI)
509 return false;
510
511 // The context comes first, but they're both in the same block.
512 // Make sure there is nothing in between that might interrupt
513 // the control flow, not even CxtI itself.
514 // We limit the scan distance between the assume and its context instruction
515 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
516 // it can be adjusted if needed (could be turned into a cl::opt).
517 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
519 return false;
520
521 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
522 }
523
524 // Inv and CxtI are in different blocks.
525 if (DT) {
526 if (DT->dominates(Inv, CxtI))
527 return true;
528 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
529 Inv->getParent()->isEntryBlock()) {
530 // We don't have a DT, but this trivially dominates.
531 return true;
532 }
533
534 return false;
535}
536
537// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
538// we still have enough information about `RHS` to conclude non-zero. For
539// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
540// so the extra compile time may not be worth it, but possibly a second API
541// should be created for use outside of loops.
542static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
543 // v u> y implies v != 0.
544 if (Pred == ICmpInst::ICMP_UGT)
545 return true;
546
547 // Special-case v != 0 to also handle v != null.
548 if (Pred == ICmpInst::ICMP_NE)
549 return match(RHS, m_Zero());
550
551 // All other predicates - rely on generic ConstantRange handling.
552 const APInt *C;
554 if (match(RHS, m_APInt(C))) {
556 return !TrueValues.contains(Zero);
557 }
558
559 auto *VC = dyn_cast<ConstantDataVector>(RHS);
560 if (VC == nullptr)
561 return false;
562
563 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
564 ++ElemIdx) {
566 Pred, VC->getElementAsAPInt(ElemIdx));
567 if (TrueValues.contains(Zero))
568 return false;
569 }
570 return true;
571}
572
573static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
574 Value *&ValOut, Instruction *&CtxIOut,
575 const PHINode **PhiOut = nullptr) {
576 ValOut = U->get();
577 if (ValOut == PHI)
578 return;
579 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
580 if (PhiOut)
581 *PhiOut = PHI;
582 Value *V;
583 // If the Use is a select of this phi, compute analysis on other arm to break
584 // recursion.
585 // TODO: Min/Max
586 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
587 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
588 ValOut = V;
589
590 // Same for select, if this phi is 2-operand phi, compute analysis on other
591 // incoming value to break recursion.
592 // TODO: We could handle any number of incoming edges as long as we only have
593 // two unique values.
594 if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
595 IncPhi && IncPhi->getNumIncomingValues() == 2) {
596 for (int Idx = 0; Idx < 2; ++Idx) {
597 if (IncPhi->getIncomingValue(Idx) == PHI) {
598 ValOut = IncPhi->getIncomingValue(1 - Idx);
599 if (PhiOut)
600 *PhiOut = IncPhi;
601 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
602 break;
603 }
604 }
605 }
606}
607
608static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
609 // Use of assumptions is context-sensitive. If we don't have a context, we
610 // cannot use them!
611 if (!Q.AC || !Q.CxtI)
612 return false;
613
614 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
615 if (!Elem.Assume)
616 continue;
617
618 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
619 assert(I->getFunction() == Q.CxtI->getFunction() &&
620 "Got assumption for the wrong function!");
621
622 if (Elem.Index != AssumptionCache::ExprResultIdx) {
623 if (!V->getType()->isPointerTy())
624 continue;
626 *I, I->bundle_op_info_begin()[Elem.Index])) {
627 if (RK.WasOn == V &&
628 (RK.AttrKind == Attribute::NonNull ||
629 (RK.AttrKind == Attribute::Dereferenceable &&
631 V->getType()->getPointerAddressSpace()))) &&
633 return true;
634 }
635 continue;
636 }
637
638 // Warning: This loop can end up being somewhat performance sensitive.
639 // We're running this loop for once for each value queried resulting in a
640 // runtime of ~O(#assumes * #values).
641
642 Value *RHS;
643 CmpPredicate Pred;
644 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
645 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
646 continue;
647
648 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
649 return true;
650 }
651
652 return false;
653}
654
656 Value *LHS, Value *RHS, KnownBits &Known,
657 const SimplifyQuery &Q) {
658 if (RHS->getType()->isPointerTy()) {
659 // Handle comparison of pointer to null explicitly, as it will not be
660 // covered by the m_APInt() logic below.
661 if (LHS == V && match(RHS, m_Zero())) {
662 switch (Pred) {
663 case ICmpInst::ICMP_EQ:
664 Known.setAllZero();
665 break;
666 case ICmpInst::ICMP_SGE:
667 case ICmpInst::ICMP_SGT:
668 Known.makeNonNegative();
669 break;
670 case ICmpInst::ICMP_SLT:
671 Known.makeNegative();
672 break;
673 default:
674 break;
675 }
676 }
677 return;
678 }
679
680 unsigned BitWidth = Known.getBitWidth();
681 auto m_V =
683
684 Value *Y;
685 const APInt *Mask, *C;
686 if (!match(RHS, m_APInt(C)))
687 return;
688
689 uint64_t ShAmt;
690 switch (Pred) {
691 case ICmpInst::ICMP_EQ:
692 // assume(V = C)
693 if (match(LHS, m_V)) {
694 Known = Known.unionWith(KnownBits::makeConstant(*C));
695 // assume(V & Mask = C)
696 } else if (match(LHS, m_c_And(m_V, m_Value(Y)))) {
697 // For one bits in Mask, we can propagate bits from C to V.
698 Known.One |= *C;
699 if (match(Y, m_APInt(Mask)))
700 Known.Zero |= ~*C & *Mask;
701 // assume(V | Mask = C)
702 } else if (match(LHS, m_c_Or(m_V, m_Value(Y)))) {
703 // For zero bits in Mask, we can propagate bits from C to V.
704 Known.Zero |= ~*C;
705 if (match(Y, m_APInt(Mask)))
706 Known.One |= *C & ~*Mask;
707 // assume(V << ShAmt = C)
708 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
709 ShAmt < BitWidth) {
710 // For those bits in C that are known, we can propagate them to known
711 // bits in V shifted to the right by ShAmt.
713 RHSKnown.Zero.lshrInPlace(ShAmt);
714 RHSKnown.One.lshrInPlace(ShAmt);
715 Known = Known.unionWith(RHSKnown);
716 // assume(V >> ShAmt = C)
717 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
718 ShAmt < BitWidth) {
720 // For those bits in RHS that are known, we can propagate them to known
721 // bits in V shifted to the right by C.
722 Known.Zero |= RHSKnown.Zero << ShAmt;
723 Known.One |= RHSKnown.One << ShAmt;
724 }
725 break;
726 case ICmpInst::ICMP_NE: {
727 // assume (V & B != 0) where B is a power of 2
728 const APInt *BPow2;
729 if (C->isZero() && match(LHS, m_And(m_V, m_Power2(BPow2))))
730 Known.One |= *BPow2;
731 break;
732 }
733 default: {
734 const APInt *Offset = nullptr;
735 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
737 if (Offset)
738 LHSRange = LHSRange.sub(*Offset);
739 Known = Known.unionWith(LHSRange.toKnownBits());
740 }
741 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
742 // X & Y u> C -> X u> C && Y u> C
743 // X nuw- Y u> C -> X u> C
744 if (match(LHS, m_c_And(m_V, m_Value())) ||
745 match(LHS, m_NUWSub(m_V, m_Value())))
746 Known.One.setHighBits(
747 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
748 }
749 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
750 // X | Y u< C -> X u< C && Y u< C
751 // X nuw+ Y u< C -> X u< C && Y u< C
752 if (match(LHS, m_c_Or(m_V, m_Value())) ||
753 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
754 Known.Zero.setHighBits(
755 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
756 }
757 }
758 } break;
759 }
760}
761
762static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
763 KnownBits &Known,
764 const SimplifyQuery &SQ, bool Invert) {
766 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
767 Value *LHS = Cmp->getOperand(0);
768 Value *RHS = Cmp->getOperand(1);
769
770 // Handle icmp pred (trunc V), C
771 if (match(LHS, m_Trunc(m_Specific(V)))) {
773 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
774 if (cast<TruncInst>(LHS)->hasNoUnsignedWrap())
775 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
776 else
777 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
778 return;
779 }
780
781 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
782}
783
785 KnownBits &Known, unsigned Depth,
786 const SimplifyQuery &SQ, bool Invert) {
787 Value *A, *B;
790 KnownBits Known2(Known.getBitWidth());
791 KnownBits Known3(Known.getBitWidth());
792 computeKnownBitsFromCond(V, A, Known2, Depth + 1, SQ, Invert);
793 computeKnownBitsFromCond(V, B, Known3, Depth + 1, SQ, Invert);
794 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
796 Known2 = Known2.unionWith(Known3);
797 else
798 Known2 = Known2.intersectWith(Known3);
799 Known = Known.unionWith(Known2);
800 return;
801 }
802
803 if (auto *Cmp = dyn_cast<ICmpInst>(Cond)) {
804 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
805 return;
806 }
807
808 if (match(Cond, m_Trunc(m_Specific(V)))) {
809 KnownBits DstKnown(1);
810 if (Invert) {
811 DstKnown.setAllZero();
812 } else {
813 DstKnown.setAllOnes();
814 }
815 if (cast<TruncInst>(Cond)->hasNoUnsignedWrap()) {
816 Known = Known.unionWith(DstKnown.zext(Known.getBitWidth()));
817 return;
818 }
819 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
820 return;
821 }
822
824 computeKnownBitsFromCond(V, A, Known, Depth + 1, SQ, !Invert);
825}
826
828 unsigned Depth, const SimplifyQuery &Q) {
829 // Handle injected condition.
830 if (Q.CC && Q.CC->AffectedValues.contains(V))
831 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Depth, Q, Q.CC->Invert);
832
833 if (!Q.CxtI)
834 return;
835
836 if (Q.DC && Q.DT) {
837 // Handle dominating conditions.
838 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
839 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
840 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
841 computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
842 /*Invert*/ false);
843
844 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
845 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
846 computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
847 /*Invert*/ true);
848 }
849
850 if (Known.hasConflict())
851 Known.resetAll();
852 }
853
854 if (!Q.AC)
855 return;
856
857 unsigned BitWidth = Known.getBitWidth();
858
859 // Note that the patterns below need to be kept in sync with the code
860 // in AssumptionCache::updateAffectedValues.
861
862 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
863 if (!Elem.Assume)
864 continue;
865
866 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
867 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
868 "Got assumption for the wrong function!");
869
870 if (Elem.Index != AssumptionCache::ExprResultIdx) {
871 if (!V->getType()->isPointerTy())
872 continue;
874 *I, I->bundle_op_info_begin()[Elem.Index])) {
875 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
876 // be the producer of the pointer in the bundle. At the moment, align
877 // assumptions aren't optimized away.
878 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
879 isPowerOf2_64(RK.ArgValue) &&
880 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
881 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
882 }
883 continue;
884 }
885
886 // Warning: This loop can end up being somewhat performance sensitive.
887 // We're running this loop for once for each value queried resulting in a
888 // runtime of ~O(#assumes * #values).
889
890 Value *Arg = I->getArgOperand(0);
891
892 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
893 assert(BitWidth == 1 && "assume operand is not i1?");
894 (void)BitWidth;
895 Known.setAllOnes();
896 return;
897 }
898 if (match(Arg, m_Not(m_Specific(V))) &&
900 assert(BitWidth == 1 && "assume operand is not i1?");
901 (void)BitWidth;
902 Known.setAllZero();
903 return;
904 }
905
906 // The remaining tests are all recursive, so bail out if we hit the limit.
908 continue;
909
910 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
911 if (!Cmp)
912 continue;
913
914 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
915 continue;
916
917 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
918 }
919
920 // Conflicting assumption: Undefined behavior will occur on this execution
921 // path.
922 if (Known.hasConflict())
923 Known.resetAll();
924}
925
926/// Compute known bits from a shift operator, including those with a
927/// non-constant shift amount. Known is the output of this function. Known2 is a
928/// pre-allocated temporary with the same bit width as Known and on return
929/// contains the known bit of the shift value source. KF is an
930/// operator-specific function that, given the known-bits and a shift amount,
931/// compute the implied known-bits of the shift operator's result respectively
932/// for that shift amount. The results from calling KF are conservatively
933/// combined for all permitted shift amounts.
935 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
936 KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q,
937 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
938 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
939 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
940 // To limit compile-time impact, only query isKnownNonZero() if we know at
941 // least something about the shift amount.
942 bool ShAmtNonZero =
943 Known.isNonZero() ||
944 (Known.getMaxValue().ult(Known.getBitWidth()) &&
945 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
946 Known = KF(Known2, Known, ShAmtNonZero);
947}
948
949static KnownBits
950getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
951 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
952 unsigned Depth, const SimplifyQuery &Q) {
953 unsigned BitWidth = KnownLHS.getBitWidth();
954 KnownBits KnownOut(BitWidth);
955 bool IsAnd = false;
956 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
957 Value *X = nullptr, *Y = nullptr;
958
959 switch (I->getOpcode()) {
960 case Instruction::And:
961 KnownOut = KnownLHS & KnownRHS;
962 IsAnd = true;
963 // and(x, -x) is common idioms that will clear all but lowest set
964 // bit. If we have a single known bit in x, we can clear all bits
965 // above it.
966 // TODO: instcombine often reassociates independent `and` which can hide
967 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
968 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
969 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
970 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
971 KnownOut = KnownLHS.blsi();
972 else
973 KnownOut = KnownRHS.blsi();
974 }
975 break;
976 case Instruction::Or:
977 KnownOut = KnownLHS | KnownRHS;
978 break;
979 case Instruction::Xor:
980 KnownOut = KnownLHS ^ KnownRHS;
981 // xor(x, x-1) is common idioms that will clear all but lowest set
982 // bit. If we have a single known bit in x, we can clear all bits
983 // above it.
984 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
985 // -1 but for the purpose of demanded bits (xor(x, x-C) &
986 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
987 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
988 if (HasKnownOne &&
990 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
991 KnownOut = XBits.blsmsk();
992 }
993 break;
994 default:
995 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
996 }
997
998 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
999 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
1000 // here we handle the more general case of adding any odd number by
1001 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1002 // TODO: This could be generalized to clearing any bit set in y where the
1003 // following bit is known to be unset in y.
1004 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1008 KnownBits KnownY(BitWidth);
1009 computeKnownBits(Y, DemandedElts, KnownY, Depth + 1, Q);
1010 if (KnownY.countMinTrailingOnes() > 0) {
1011 if (IsAnd)
1012 KnownOut.Zero.setBit(0);
1013 else
1014 KnownOut.One.setBit(0);
1015 }
1016 }
1017 return KnownOut;
1018}
1019
1021 const Operator *I, const APInt &DemandedElts, unsigned Depth,
1022 const SimplifyQuery &Q,
1023 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1024 KnownBitsFunc) {
1025 APInt DemandedEltsLHS, DemandedEltsRHS;
1027 DemandedElts, DemandedEltsLHS,
1028 DemandedEltsRHS);
1029
1030 const auto ComputeForSingleOpFunc =
1031 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1032 return KnownBitsFunc(
1033 computeKnownBits(Op, DemandedEltsOp, Depth + 1, Q),
1034 computeKnownBits(Op, DemandedEltsOp << 1, Depth + 1, Q));
1035 };
1036
1037 if (DemandedEltsRHS.isZero())
1038 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1039 if (DemandedEltsLHS.isZero())
1040 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1041
1042 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1043 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1044}
1045
1046// Public so this can be used in `SimplifyDemandedUseBits`.
1048 const KnownBits &KnownLHS,
1049 const KnownBits &KnownRHS,
1050 unsigned Depth,
1051 const SimplifyQuery &SQ) {
1052 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1053 APInt DemandedElts =
1054 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1055
1056 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, Depth,
1057 SQ);
1058}
1059
1061 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1062 // Without vscale_range, we only know that vscale is non-zero.
1063 if (!Attr.isValid())
1065
1066 unsigned AttrMin = Attr.getVScaleRangeMin();
1067 // Minimum is larger than vscale width, result is always poison.
1068 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1069 return ConstantRange::getEmpty(BitWidth);
1070
1071 APInt Min(BitWidth, AttrMin);
1072 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1073 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1075
1076 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1077}
1078
1080 Value *Arm, bool Invert, unsigned Depth,
1081 const SimplifyQuery &Q) {
1082 // If we have a constant arm, we are done.
1083 if (Known.isConstant())
1084 return;
1085
1086 // See what condition implies about the bits of the select arm.
1087 KnownBits CondRes(Known.getBitWidth());
1088 computeKnownBitsFromCond(Arm, Cond, CondRes, Depth + 1, Q, Invert);
1089 // If we don't get any information from the condition, no reason to
1090 // proceed.
1091 if (CondRes.isUnknown())
1092 return;
1093
1094 // We can have conflict if the condition is dead. I.e if we have
1095 // (x | 64) < 32 ? (x | 64) : y
1096 // we will have conflict at bit 6 from the condition/the `or`.
1097 // In that case just return. Its not particularly important
1098 // what we do, as this select is going to be simplified soon.
1099 CondRes = CondRes.unionWith(Known);
1100 if (CondRes.hasConflict())
1101 return;
1102
1103 // Finally make sure the information we found is valid. This is relatively
1104 // expensive so it's left for the very end.
1105 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1106 return;
1107
1108 // Finally, we know we get information from the condition and its valid,
1109 // so return it.
1110 Known = CondRes;
1111}
1112
1113// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1114// Returns the input and lower/upper bounds.
1115static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1116 const APInt *&CLow, const APInt *&CHigh) {
1117 assert(isa<Operator>(Select) &&
1118 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1119 "Input should be a Select!");
1120
1121 const Value *LHS = nullptr, *RHS = nullptr;
1123 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1124 return false;
1125
1126 if (!match(RHS, m_APInt(CLow)))
1127 return false;
1128
1129 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1131 if (getInverseMinMaxFlavor(SPF) != SPF2)
1132 return false;
1133
1134 if (!match(RHS2, m_APInt(CHigh)))
1135 return false;
1136
1137 if (SPF == SPF_SMIN)
1138 std::swap(CLow, CHigh);
1139
1140 In = LHS2;
1141 return CLow->sle(*CHigh);
1142}
1143
1145 const APInt *&CLow,
1146 const APInt *&CHigh) {
1147 assert((II->getIntrinsicID() == Intrinsic::smin ||
1148 II->getIntrinsicID() == Intrinsic::smax) &&
1149 "Must be smin/smax");
1150
1151 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1152 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1153 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1154 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1155 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1156 return false;
1157
1158 if (II->getIntrinsicID() == Intrinsic::smin)
1159 std::swap(CLow, CHigh);
1160 return CLow->sle(*CHigh);
1161}
1162
1164 KnownBits &Known) {
1165 const APInt *CLow, *CHigh;
1166 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1167 Known = Known.unionWith(
1168 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1169}
1170
1172 const APInt &DemandedElts,
1173 KnownBits &Known, unsigned Depth,
1174 const SimplifyQuery &Q) {
1175 unsigned BitWidth = Known.getBitWidth();
1176
1177 KnownBits Known2(BitWidth);
1178 switch (I->getOpcode()) {
1179 default: break;
1180 case Instruction::Load:
1181 if (MDNode *MD =
1182 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1184 break;
1185 case Instruction::And:
1186 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1187 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1188
1189 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1190 break;
1191 case Instruction::Or:
1192 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1193 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1194
1195 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1196 break;
1197 case Instruction::Xor:
1198 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1199 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1200
1201 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1202 break;
1203 case Instruction::Mul: {
1204 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1205 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1206 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1207 DemandedElts, Known, Known2, Depth, Q);
1208 break;
1209 }
1210 case Instruction::UDiv: {
1211 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1212 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1213 Known =
1214 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1215 break;
1216 }
1217 case Instruction::SDiv: {
1218 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1219 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1220 Known =
1221 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1222 break;
1223 }
1224 case Instruction::Select: {
1225 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1226 KnownBits Res(Known.getBitWidth());
1227 computeKnownBits(Arm, DemandedElts, Res, Depth + 1, Q);
1228 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Depth, Q);
1229 return Res;
1230 };
1231 // Only known if known in both the LHS and RHS.
1232 Known =
1233 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1234 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1235 break;
1236 }
1237 case Instruction::FPTrunc:
1238 case Instruction::FPExt:
1239 case Instruction::FPToUI:
1240 case Instruction::FPToSI:
1241 case Instruction::SIToFP:
1242 case Instruction::UIToFP:
1243 break; // Can't work with floating point.
1244 case Instruction::PtrToInt:
1245 case Instruction::IntToPtr:
1246 // Fall through and handle them the same as zext/trunc.
1247 [[fallthrough]];
1248 case Instruction::ZExt:
1249 case Instruction::Trunc: {
1250 Type *SrcTy = I->getOperand(0)->getType();
1251
1252 unsigned SrcBitWidth;
1253 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1254 // which fall through here.
1255 Type *ScalarTy = SrcTy->getScalarType();
1256 SrcBitWidth = ScalarTy->isPointerTy() ?
1257 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1258 Q.DL.getTypeSizeInBits(ScalarTy);
1259
1260 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1261 Known = Known.anyextOrTrunc(SrcBitWidth);
1262 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1263 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1264 Inst && Inst->hasNonNeg() && !Known.isNegative())
1265 Known.makeNonNegative();
1266 Known = Known.zextOrTrunc(BitWidth);
1267 break;
1268 }
1269 case Instruction::BitCast: {
1270 Type *SrcTy = I->getOperand(0)->getType();
1271 if (SrcTy->isIntOrPtrTy() &&
1272 // TODO: For now, not handling conversions like:
1273 // (bitcast i64 %x to <2 x i32>)
1274 !I->getType()->isVectorTy()) {
1275 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1276 break;
1277 }
1278
1279 const Value *V;
1280 // Handle bitcast from floating point to integer.
1281 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1282 V->getType()->isFPOrFPVectorTy()) {
1283 Type *FPType = V->getType()->getScalarType();
1284 KnownFPClass Result =
1285 computeKnownFPClass(V, DemandedElts, fcAllFlags, Depth + 1, Q);
1286 FPClassTest FPClasses = Result.KnownFPClasses;
1287
1288 // TODO: Treat it as zero/poison if the use of I is unreachable.
1289 if (FPClasses == fcNone)
1290 break;
1291
1292 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1293 Known.Zero.setAllBits();
1294 Known.One.setAllBits();
1295
1296 if (FPClasses & fcInf)
1298 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1299
1300 if (FPClasses & fcZero)
1302 APInt::getZero(FPType->getScalarSizeInBits())));
1303
1304 Known.Zero.clearSignBit();
1305 Known.One.clearSignBit();
1306 }
1307
1308 if (Result.SignBit) {
1309 if (*Result.SignBit)
1310 Known.makeNegative();
1311 else
1312 Known.makeNonNegative();
1313 }
1314
1315 break;
1316 }
1317
1318 // Handle cast from vector integer type to scalar or vector integer.
1319 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1320 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1321 !I->getType()->isIntOrIntVectorTy() ||
1322 isa<ScalableVectorType>(I->getType()))
1323 break;
1324
1325 // Look through a cast from narrow vector elements to wider type.
1326 // Examples: v4i32 -> v2i64, v3i8 -> v24
1327 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1328 if (BitWidth % SubBitWidth == 0) {
1329 // Known bits are automatically intersected across demanded elements of a
1330 // vector. So for example, if a bit is computed as known zero, it must be
1331 // zero across all demanded elements of the vector.
1332 //
1333 // For this bitcast, each demanded element of the output is sub-divided
1334 // across a set of smaller vector elements in the source vector. To get
1335 // the known bits for an entire element of the output, compute the known
1336 // bits for each sub-element sequentially. This is done by shifting the
1337 // one-set-bit demanded elements parameter across the sub-elements for
1338 // consecutive calls to computeKnownBits. We are using the demanded
1339 // elements parameter as a mask operator.
1340 //
1341 // The known bits of each sub-element are then inserted into place
1342 // (dependent on endian) to form the full result of known bits.
1343 unsigned NumElts = DemandedElts.getBitWidth();
1344 unsigned SubScale = BitWidth / SubBitWidth;
1345 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1346 for (unsigned i = 0; i != NumElts; ++i) {
1347 if (DemandedElts[i])
1348 SubDemandedElts.setBit(i * SubScale);
1349 }
1350
1351 KnownBits KnownSrc(SubBitWidth);
1352 for (unsigned i = 0; i != SubScale; ++i) {
1353 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc,
1354 Depth + 1, Q);
1355 unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i;
1356 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1357 }
1358 }
1359 break;
1360 }
1361 case Instruction::SExt: {
1362 // Compute the bits in the result that are not present in the input.
1363 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1364
1365 Known = Known.trunc(SrcBitWidth);
1366 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1367 // If the sign bit of the input is known set or clear, then we know the
1368 // top bits of the result.
1369 Known = Known.sext(BitWidth);
1370 break;
1371 }
1372 case Instruction::Shl: {
1373 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1374 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1375 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1376 bool ShAmtNonZero) {
1377 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1378 };
1379 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1380 KF);
1381 // Trailing zeros of a right-shifted constant never decrease.
1382 const APInt *C;
1383 if (match(I->getOperand(0), m_APInt(C)))
1384 Known.Zero.setLowBits(C->countr_zero());
1385 break;
1386 }
1387 case Instruction::LShr: {
1388 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1389 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1390 bool ShAmtNonZero) {
1391 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1392 };
1393 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1394 KF);
1395 // Leading zeros of a left-shifted constant never decrease.
1396 const APInt *C;
1397 if (match(I->getOperand(0), m_APInt(C)))
1398 Known.Zero.setHighBits(C->countl_zero());
1399 break;
1400 }
1401 case Instruction::AShr: {
1402 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1403 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1404 bool ShAmtNonZero) {
1405 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1406 };
1407 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1408 KF);
1409 break;
1410 }
1411 case Instruction::Sub: {
1412 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1413 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1414 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1415 DemandedElts, Known, Known2, Depth, Q);
1416 break;
1417 }
1418 case Instruction::Add: {
1419 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1420 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1421 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1422 DemandedElts, Known, Known2, Depth, Q);
1423 break;
1424 }
1425 case Instruction::SRem:
1426 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1427 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1428 Known = KnownBits::srem(Known, Known2);
1429 break;
1430
1431 case Instruction::URem:
1432 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1433 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1434 Known = KnownBits::urem(Known, Known2);
1435 break;
1436 case Instruction::Alloca:
1437 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
1438 break;
1439 case Instruction::GetElementPtr: {
1440 // Analyze all of the subscripts of this getelementptr instruction
1441 // to determine if we can prove known low zero bits.
1442 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1443 // Accumulate the constant indices in a separate variable
1444 // to minimize the number of calls to computeForAddSub.
1445 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(I->getType());
1446 APInt AccConstIndices(IndexWidth, 0);
1447
1448 auto AddIndexToKnown = [&](KnownBits IndexBits) {
1449 if (IndexWidth == BitWidth) {
1450 // Note that inbounds does *not* guarantee nsw for the addition, as only
1451 // the offset is signed, while the base address is unsigned.
1452 Known = KnownBits::add(Known, IndexBits);
1453 } else {
1454 // If the index width is smaller than the pointer width, only add the
1455 // value to the low bits.
1456 assert(IndexWidth < BitWidth &&
1457 "Index width can't be larger than pointer width");
1458 Known.insertBits(KnownBits::add(Known.trunc(IndexWidth), IndexBits), 0);
1459 }
1460 };
1461
1463 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1464 // TrailZ can only become smaller, short-circuit if we hit zero.
1465 if (Known.isUnknown())
1466 break;
1467
1468 Value *Index = I->getOperand(i);
1469
1470 // Handle case when index is zero.
1471 Constant *CIndex = dyn_cast<Constant>(Index);
1472 if (CIndex && CIndex->isZeroValue())
1473 continue;
1474
1475 if (StructType *STy = GTI.getStructTypeOrNull()) {
1476 // Handle struct member offset arithmetic.
1477
1478 assert(CIndex &&
1479 "Access to structure field must be known at compile time");
1480
1481 if (CIndex->getType()->isVectorTy())
1482 Index = CIndex->getSplatValue();
1483
1484 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1485 const StructLayout *SL = Q.DL.getStructLayout(STy);
1487 AccConstIndices += Offset;
1488 continue;
1489 }
1490
1491 // Handle array index arithmetic.
1492 Type *IndexedTy = GTI.getIndexedType();
1493 if (!IndexedTy->isSized()) {
1494 Known.resetAll();
1495 break;
1496 }
1497
1498 TypeSize Stride = GTI.getSequentialElementStride(Q.DL);
1499 uint64_t StrideInBytes = Stride.getKnownMinValue();
1500 if (!Stride.isScalable()) {
1501 // Fast path for constant offset.
1502 if (auto *CI = dyn_cast<ConstantInt>(Index)) {
1503 AccConstIndices +=
1504 CI->getValue().sextOrTrunc(IndexWidth) * StrideInBytes;
1505 continue;
1506 }
1507 }
1508
1509 KnownBits IndexBits =
1510 computeKnownBits(Index, Depth + 1, Q).sextOrTrunc(IndexWidth);
1511 KnownBits ScalingFactor(IndexWidth);
1512 // Multiply by current sizeof type.
1513 // &A[i] == A + i * sizeof(*A[i]).
1514 if (Stride.isScalable()) {
1515 // For scalable types the only thing we know about sizeof is
1516 // that this is a multiple of the minimum size.
1517 ScalingFactor.Zero.setLowBits(llvm::countr_zero(StrideInBytes));
1518 } else {
1519 ScalingFactor =
1520 KnownBits::makeConstant(APInt(IndexWidth, StrideInBytes));
1521 }
1522 AddIndexToKnown(KnownBits::mul(IndexBits, ScalingFactor));
1523 }
1524 if (!Known.isUnknown() && !AccConstIndices.isZero())
1525 AddIndexToKnown(KnownBits::makeConstant(AccConstIndices));
1526 break;
1527 }
1528 case Instruction::PHI: {
1529 const PHINode *P = cast<PHINode>(I);
1530 BinaryOperator *BO = nullptr;
1531 Value *R = nullptr, *L = nullptr;
1532 if (matchSimpleRecurrence(P, BO, R, L)) {
1533 // Handle the case of a simple two-predecessor recurrence PHI.
1534 // There's a lot more that could theoretically be done here, but
1535 // this is sufficient to catch some interesting cases.
1536 unsigned Opcode = BO->getOpcode();
1537
1538 switch (Opcode) {
1539 // If this is a shift recurrence, we know the bits being shifted in. We
1540 // can combine that with information about the start value of the
1541 // recurrence to conclude facts about the result. If this is a udiv
1542 // recurrence, we know that the result can never exceed either the
1543 // numerator or the start value, whichever is greater.
1544 case Instruction::LShr:
1545 case Instruction::AShr:
1546 case Instruction::Shl:
1547 case Instruction::UDiv:
1548 if (BO->getOperand(0) != I)
1549 break;
1550 [[fallthrough]];
1551
1552 // For a urem recurrence, the result can never exceed the start value. The
1553 // phi could either be the numerator or the denominator.
1554 case Instruction::URem: {
1555 // We have matched a recurrence of the form:
1556 // %iv = [R, %entry], [%iv.next, %backedge]
1557 // %iv.next = shift_op %iv, L
1558
1559 // Recurse with the phi context to avoid concern about whether facts
1560 // inferred hold at original context instruction. TODO: It may be
1561 // correct to use the original context. IF warranted, explore and
1562 // add sufficient tests to cover.
1564 RecQ.CxtI = P;
1565 computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1566 switch (Opcode) {
1567 case Instruction::Shl:
1568 // A shl recurrence will only increase the tailing zeros
1569 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1570 break;
1571 case Instruction::LShr:
1572 case Instruction::UDiv:
1573 case Instruction::URem:
1574 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1575 // the start value.
1576 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1577 break;
1578 case Instruction::AShr:
1579 // An ashr recurrence will extend the initial sign bit
1580 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1581 Known.One.setHighBits(Known2.countMinLeadingOnes());
1582 break;
1583 }
1584 break;
1585 }
1586
1587 // Check for operations that have the property that if
1588 // both their operands have low zero bits, the result
1589 // will have low zero bits.
1590 case Instruction::Add:
1591 case Instruction::Sub:
1592 case Instruction::And:
1593 case Instruction::Or:
1594 case Instruction::Mul: {
1595 // Change the context instruction to the "edge" that flows into the
1596 // phi. This is important because that is where the value is actually
1597 // "evaluated" even though it is used later somewhere else. (see also
1598 // D69571).
1600
1601 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1602 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1603 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1604
1605 // Ok, we have a PHI of the form L op= R. Check for low
1606 // zero bits.
1607 RecQ.CxtI = RInst;
1608 computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1609
1610 // We need to take the minimum number of known bits
1611 KnownBits Known3(BitWidth);
1612 RecQ.CxtI = LInst;
1613 computeKnownBits(L, DemandedElts, Known3, Depth + 1, RecQ);
1614
1615 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1616 Known3.countMinTrailingZeros()));
1617
1618 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1619 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1620 break;
1621
1622 switch (Opcode) {
1623 // If initial value of recurrence is nonnegative, and we are adding
1624 // a nonnegative number with nsw, the result can only be nonnegative
1625 // or poison value regardless of the number of times we execute the
1626 // add in phi recurrence. If initial value is negative and we are
1627 // adding a negative number with nsw, the result can only be
1628 // negative or poison value. Similar arguments apply to sub and mul.
1629 //
1630 // (add non-negative, non-negative) --> non-negative
1631 // (add negative, negative) --> negative
1632 case Instruction::Add: {
1633 if (Known2.isNonNegative() && Known3.isNonNegative())
1634 Known.makeNonNegative();
1635 else if (Known2.isNegative() && Known3.isNegative())
1636 Known.makeNegative();
1637 break;
1638 }
1639
1640 // (sub nsw non-negative, negative) --> non-negative
1641 // (sub nsw negative, non-negative) --> negative
1642 case Instruction::Sub: {
1643 if (BO->getOperand(0) != I)
1644 break;
1645 if (Known2.isNonNegative() && Known3.isNegative())
1646 Known.makeNonNegative();
1647 else if (Known2.isNegative() && Known3.isNonNegative())
1648 Known.makeNegative();
1649 break;
1650 }
1651
1652 // (mul nsw non-negative, non-negative) --> non-negative
1653 case Instruction::Mul:
1654 if (Known2.isNonNegative() && Known3.isNonNegative())
1655 Known.makeNonNegative();
1656 break;
1657
1658 default:
1659 break;
1660 }
1661 break;
1662 }
1663
1664 default:
1665 break;
1666 }
1667 }
1668
1669 // Unreachable blocks may have zero-operand PHI nodes.
1670 if (P->getNumIncomingValues() == 0)
1671 break;
1672
1673 // Otherwise take the unions of the known bit sets of the operands,
1674 // taking conservative care to avoid excessive recursion.
1675 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1676 // Skip if every incoming value references to ourself.
1677 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1678 break;
1679
1680 Known.Zero.setAllBits();
1681 Known.One.setAllBits();
1682 for (const Use &U : P->operands()) {
1683 Value *IncValue;
1684 const PHINode *CxtPhi;
1685 Instruction *CxtI;
1686 breakSelfRecursivePHI(&U, P, IncValue, CxtI, &CxtPhi);
1687 // Skip direct self references.
1688 if (IncValue == P)
1689 continue;
1690
1691 // Change the context instruction to the "edge" that flows into the
1692 // phi. This is important because that is where the value is actually
1693 // "evaluated" even though it is used later somewhere else. (see also
1694 // D69571).
1696
1697 Known2 = KnownBits(BitWidth);
1698
1699 // Recurse, but cap the recursion to one level, because we don't
1700 // want to waste time spinning around in loops.
1701 // TODO: See if we can base recursion limiter on number of incoming phi
1702 // edges so we don't overly clamp analysis.
1703 computeKnownBits(IncValue, DemandedElts, Known2,
1704 MaxAnalysisRecursionDepth - 1, RecQ);
1705
1706 // See if we can further use a conditional branch into the phi
1707 // to help us determine the range of the value.
1708 if (!Known2.isConstant()) {
1709 CmpPredicate Pred;
1710 const APInt *RHSC;
1711 BasicBlock *TrueSucc, *FalseSucc;
1712 // TODO: Use RHS Value and compute range from its known bits.
1713 if (match(RecQ.CxtI,
1714 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1715 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1716 // Check for cases of duplicate successors.
1717 if ((TrueSucc == CxtPhi->getParent()) !=
1718 (FalseSucc == CxtPhi->getParent())) {
1719 // If we're using the false successor, invert the predicate.
1720 if (FalseSucc == CxtPhi->getParent())
1721 Pred = CmpInst::getInversePredicate(Pred);
1722 // Get the knownbits implied by the incoming phi condition.
1723 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1724 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1725 // We can have conflicts here if we are analyzing deadcode (its
1726 // impossible for us reach this BB based the icmp).
1727 if (KnownUnion.hasConflict()) {
1728 // No reason to continue analyzing in a known dead region, so
1729 // just resetAll and break. This will cause us to also exit the
1730 // outer loop.
1731 Known.resetAll();
1732 break;
1733 }
1734 Known2 = KnownUnion;
1735 }
1736 }
1737 }
1738
1739 Known = Known.intersectWith(Known2);
1740 // If all bits have been ruled out, there's no need to check
1741 // more operands.
1742 if (Known.isUnknown())
1743 break;
1744 }
1745 }
1746 break;
1747 }
1748 case Instruction::Call:
1749 case Instruction::Invoke: {
1750 // If range metadata is attached to this call, set known bits from that,
1751 // and then intersect with known bits based on other properties of the
1752 // function.
1753 if (MDNode *MD =
1754 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1756
1757 const auto *CB = cast<CallBase>(I);
1758
1759 if (std::optional<ConstantRange> Range = CB->getRange())
1760 Known = Known.unionWith(Range->toKnownBits());
1761
1762 if (const Value *RV = CB->getReturnedArgOperand()) {
1763 if (RV->getType() == I->getType()) {
1764 computeKnownBits(RV, Known2, Depth + 1, Q);
1765 Known = Known.unionWith(Known2);
1766 // If the function doesn't return properly for all input values
1767 // (e.g. unreachable exits) then there might be conflicts between the
1768 // argument value and the range metadata. Simply discard the known bits
1769 // in case of conflicts.
1770 if (Known.hasConflict())
1771 Known.resetAll();
1772 }
1773 }
1774 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1775 switch (II->getIntrinsicID()) {
1776 default:
1777 break;
1778 case Intrinsic::abs: {
1779 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1780 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
1781 Known = Known2.abs(IntMinIsPoison);
1782 break;
1783 }
1784 case Intrinsic::bitreverse:
1785 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1786 Known.Zero |= Known2.Zero.reverseBits();
1787 Known.One |= Known2.One.reverseBits();
1788 break;
1789 case Intrinsic::bswap:
1790 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1791 Known.Zero |= Known2.Zero.byteSwap();
1792 Known.One |= Known2.One.byteSwap();
1793 break;
1794 case Intrinsic::ctlz: {
1795 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1796 // If we have a known 1, its position is our upper bound.
1797 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
1798 // If this call is poison for 0 input, the result will be less than 2^n.
1799 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1800 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1801 unsigned LowBits = llvm::bit_width(PossibleLZ);
1802 Known.Zero.setBitsFrom(LowBits);
1803 break;
1804 }
1805 case Intrinsic::cttz: {
1806 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1807 // If we have a known 1, its position is our upper bound.
1808 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
1809 // If this call is poison for 0 input, the result will be less than 2^n.
1810 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1811 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1812 unsigned LowBits = llvm::bit_width(PossibleTZ);
1813 Known.Zero.setBitsFrom(LowBits);
1814 break;
1815 }
1816 case Intrinsic::ctpop: {
1817 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1818 // We can bound the space the count needs. Also, bits known to be zero
1819 // can't contribute to the population.
1820 unsigned BitsPossiblySet = Known2.countMaxPopulation();
1821 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
1822 Known.Zero.setBitsFrom(LowBits);
1823 // TODO: we could bound KnownOne using the lower bound on the number
1824 // of bits which might be set provided by popcnt KnownOne2.
1825 break;
1826 }
1827 case Intrinsic::fshr:
1828 case Intrinsic::fshl: {
1829 const APInt *SA;
1830 if (!match(I->getOperand(2), m_APInt(SA)))
1831 break;
1832
1833 // Normalize to funnel shift left.
1834 uint64_t ShiftAmt = SA->urem(BitWidth);
1835 if (II->getIntrinsicID() == Intrinsic::fshr)
1836 ShiftAmt = BitWidth - ShiftAmt;
1837
1838 KnownBits Known3(BitWidth);
1839 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1840 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Depth + 1, Q);
1841
1842 Known.Zero =
1843 Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
1844 Known.One =
1845 Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
1846 break;
1847 }
1848 case Intrinsic::uadd_sat:
1849 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1850 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1851 Known = KnownBits::uadd_sat(Known, Known2);
1852 break;
1853 case Intrinsic::usub_sat:
1854 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1855 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1856 Known = KnownBits::usub_sat(Known, Known2);
1857 break;
1858 case Intrinsic::sadd_sat:
1859 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1860 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1861 Known = KnownBits::sadd_sat(Known, Known2);
1862 break;
1863 case Intrinsic::ssub_sat:
1864 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1865 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1866 Known = KnownBits::ssub_sat(Known, Known2);
1867 break;
1868 // Vec reverse preserves bits from input vec.
1869 case Intrinsic::vector_reverse:
1870 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known,
1871 Depth + 1, Q);
1872 break;
1873 // for min/max/and/or reduce, any bit common to each element in the
1874 // input vec is set in the output.
1875 case Intrinsic::vector_reduce_and:
1876 case Intrinsic::vector_reduce_or:
1877 case Intrinsic::vector_reduce_umax:
1878 case Intrinsic::vector_reduce_umin:
1879 case Intrinsic::vector_reduce_smax:
1880 case Intrinsic::vector_reduce_smin:
1881 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1882 break;
1883 case Intrinsic::vector_reduce_xor: {
1884 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1885 // The zeros common to all vecs are zero in the output.
1886 // If the number of elements is odd, then the common ones remain. If the
1887 // number of elements is even, then the common ones becomes zeros.
1888 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
1889 // Even, so the ones become zeros.
1890 bool EvenCnt = VecTy->getElementCount().isKnownEven();
1891 if (EvenCnt)
1892 Known.Zero |= Known.One;
1893 // Maybe even element count so need to clear ones.
1894 if (VecTy->isScalableTy() || EvenCnt)
1895 Known.One.clearAllBits();
1896 break;
1897 }
1898 case Intrinsic::umin:
1899 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1900 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1901 Known = KnownBits::umin(Known, Known2);
1902 break;
1903 case Intrinsic::umax:
1904 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1905 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1906 Known = KnownBits::umax(Known, Known2);
1907 break;
1908 case Intrinsic::smin:
1909 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1910 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1911 Known = KnownBits::smin(Known, Known2);
1913 break;
1914 case Intrinsic::smax:
1915 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1916 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1917 Known = KnownBits::smax(Known, Known2);
1919 break;
1920 case Intrinsic::ptrmask: {
1921 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1922
1923 const Value *Mask = I->getOperand(1);
1924 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
1925 computeKnownBits(Mask, DemandedElts, Known2, Depth + 1, Q);
1926 // TODO: 1-extend would be more precise.
1927 Known &= Known2.anyextOrTrunc(BitWidth);
1928 break;
1929 }
1930 case Intrinsic::x86_sse2_pmulh_w:
1931 case Intrinsic::x86_avx2_pmulh_w:
1932 case Intrinsic::x86_avx512_pmulh_w_512:
1933 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1934 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1935 Known = KnownBits::mulhs(Known, Known2);
1936 break;
1937 case Intrinsic::x86_sse2_pmulhu_w:
1938 case Intrinsic::x86_avx2_pmulhu_w:
1939 case Intrinsic::x86_avx512_pmulhu_w_512:
1940 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1941 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1942 Known = KnownBits::mulhu(Known, Known2);
1943 break;
1944 case Intrinsic::x86_sse42_crc32_64_64:
1945 Known.Zero.setBitsFrom(32);
1946 break;
1947 case Intrinsic::x86_ssse3_phadd_d_128:
1948 case Intrinsic::x86_ssse3_phadd_w_128:
1949 case Intrinsic::x86_avx2_phadd_d:
1950 case Intrinsic::x86_avx2_phadd_w: {
1952 I, DemandedElts, Depth, Q,
1953 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
1954 return KnownBits::add(KnownLHS, KnownRHS);
1955 });
1956 break;
1957 }
1958 case Intrinsic::x86_ssse3_phadd_sw_128:
1959 case Intrinsic::x86_avx2_phadd_sw: {
1960 Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
1962 break;
1963 }
1964 case Intrinsic::x86_ssse3_phsub_d_128:
1965 case Intrinsic::x86_ssse3_phsub_w_128:
1966 case Intrinsic::x86_avx2_phsub_d:
1967 case Intrinsic::x86_avx2_phsub_w: {
1969 I, DemandedElts, Depth, Q,
1970 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
1971 return KnownBits::sub(KnownLHS, KnownRHS);
1972 });
1973 break;
1974 }
1975 case Intrinsic::x86_ssse3_phsub_sw_128:
1976 case Intrinsic::x86_avx2_phsub_sw: {
1977 Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
1979 break;
1980 }
1981 case Intrinsic::riscv_vsetvli:
1982 case Intrinsic::riscv_vsetvlimax: {
1983 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
1984 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
1986 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
1987 RISCVII::VLMUL VLMUL = static_cast<RISCVII::VLMUL>(
1988 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
1989 uint64_t MaxVLEN =
1991 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
1992
1993 // Result of vsetvli must be not larger than AVL.
1994 if (HasAVL)
1995 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
1996 MaxVL = std::min(MaxVL, CI->getZExtValue());
1997
1998 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
1999 if (BitWidth > KnownZeroFirstBit)
2000 Known.Zero.setBitsFrom(KnownZeroFirstBit);
2001 break;
2002 }
2003 case Intrinsic::vscale: {
2004 if (!II->getParent() || !II->getFunction())
2005 break;
2006
2007 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
2008 break;
2009 }
2010 }
2011 }
2012 break;
2013 }
2014 case Instruction::ShuffleVector: {
2015 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2016 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2017 if (!Shuf) {
2018 Known.resetAll();
2019 return;
2020 }
2021 // For undef elements, we don't know anything about the common state of
2022 // the shuffle result.
2023 APInt DemandedLHS, DemandedRHS;
2024 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2025 Known.resetAll();
2026 return;
2027 }
2028 Known.One.setAllBits();
2029 Known.Zero.setAllBits();
2030 if (!!DemandedLHS) {
2031 const Value *LHS = Shuf->getOperand(0);
2032 computeKnownBits(LHS, DemandedLHS, Known, Depth + 1, Q);
2033 // If we don't know any bits, early out.
2034 if (Known.isUnknown())
2035 break;
2036 }
2037 if (!!DemandedRHS) {
2038 const Value *RHS = Shuf->getOperand(1);
2039 computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q);
2040 Known = Known.intersectWith(Known2);
2041 }
2042 break;
2043 }
2044 case Instruction::InsertElement: {
2045 if (isa<ScalableVectorType>(I->getType())) {
2046 Known.resetAll();
2047 return;
2048 }
2049 const Value *Vec = I->getOperand(0);
2050 const Value *Elt = I->getOperand(1);
2051 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2052 unsigned NumElts = DemandedElts.getBitWidth();
2053 APInt DemandedVecElts = DemandedElts;
2054 bool NeedsElt = true;
2055 // If we know the index we are inserting too, clear it from Vec check.
2056 if (CIdx && CIdx->getValue().ult(NumElts)) {
2057 DemandedVecElts.clearBit(CIdx->getZExtValue());
2058 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2059 }
2060
2061 Known.One.setAllBits();
2062 Known.Zero.setAllBits();
2063 if (NeedsElt) {
2064 computeKnownBits(Elt, Known, Depth + 1, Q);
2065 // If we don't know any bits, early out.
2066 if (Known.isUnknown())
2067 break;
2068 }
2069
2070 if (!DemandedVecElts.isZero()) {
2071 computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q);
2072 Known = Known.intersectWith(Known2);
2073 }
2074 break;
2075 }
2076 case Instruction::ExtractElement: {
2077 // Look through extract element. If the index is non-constant or
2078 // out-of-range demand all elements, otherwise just the extracted element.
2079 const Value *Vec = I->getOperand(0);
2080 const Value *Idx = I->getOperand(1);
2081 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2082 if (isa<ScalableVectorType>(Vec->getType())) {
2083 // FIXME: there's probably *something* we can do with scalable vectors
2084 Known.resetAll();
2085 break;
2086 }
2087 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2088 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2089 if (CIdx && CIdx->getValue().ult(NumElts))
2090 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2091 computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q);
2092 break;
2093 }
2094 case Instruction::ExtractValue:
2095 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2096 const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
2097 if (EVI->getNumIndices() != 1) break;
2098 if (EVI->getIndices()[0] == 0) {
2099 switch (II->getIntrinsicID()) {
2100 default: break;
2101 case Intrinsic::uadd_with_overflow:
2102 case Intrinsic::sadd_with_overflow:
2104 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2105 /* NUW=*/false, DemandedElts, Known, Known2, Depth, Q);
2106 break;
2107 case Intrinsic::usub_with_overflow:
2108 case Intrinsic::ssub_with_overflow:
2110 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2111 /* NUW=*/false, DemandedElts, Known, Known2, Depth, Q);
2112 break;
2113 case Intrinsic::umul_with_overflow:
2114 case Intrinsic::smul_with_overflow:
2115 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2116 false, DemandedElts, Known, Known2, Depth, Q);
2117 break;
2118 }
2119 }
2120 }
2121 break;
2122 case Instruction::Freeze:
2123 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2124 Depth + 1))
2125 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
2126 break;
2127 }
2128}
2129
2130/// Determine which bits of V are known to be either zero or one and return
2131/// them.
2132KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2133 unsigned Depth, const SimplifyQuery &Q) {
2134 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2135 ::computeKnownBits(V, DemandedElts, Known, Depth, Q);
2136 return Known;
2137}
2138
2139/// Determine which bits of V are known to be either zero or one and return
2140/// them.
2142 const SimplifyQuery &Q) {
2143 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2144 computeKnownBits(V, Known, Depth, Q);
2145 return Known;
2146}
2147
2148/// Determine which bits of V are known to be either zero or one and return
2149/// them in the Known bit set.
2150///
2151/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2152/// we cannot optimize based on the assumption that it is zero without changing
2153/// it to be an explicit zero. If we don't change it to zero, other code could
2154/// optimized based on the contradictory assumption that it is non-zero.
2155/// Because instcombine aggressively folds operations with undef args anyway,
2156/// this won't lose us code quality.
2157///
2158/// This function is defined on values with integer type, values with pointer
2159/// type, and vectors of integers. In the case
2160/// where V is a vector, known zero, and known one values are the
2161/// same width as the vector element, and the bit is set only if it is true
2162/// for all of the demanded elements in the vector specified by DemandedElts.
2163void computeKnownBits(const Value *V, const APInt &DemandedElts,
2164 KnownBits &Known, unsigned Depth,
2165 const SimplifyQuery &Q) {
2166 if (!DemandedElts) {
2167 // No demanded elts, better to assume we don't know anything.
2168 Known.resetAll();
2169 return;
2170 }
2171
2172 assert(V && "No Value?");
2173 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2174
2175#ifndef NDEBUG
2176 Type *Ty = V->getType();
2177 unsigned BitWidth = Known.getBitWidth();
2178
2180 "Not integer or pointer type!");
2181
2182 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2183 assert(
2184 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2185 "DemandedElt width should equal the fixed vector number of elements");
2186 } else {
2187 assert(DemandedElts == APInt(1, 1) &&
2188 "DemandedElt width should be 1 for scalars or scalable vectors");
2189 }
2190
2191 Type *ScalarTy = Ty->getScalarType();
2192 if (ScalarTy->isPointerTy()) {
2193 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2194 "V and Known should have same BitWidth");
2195 } else {
2196 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2197 "V and Known should have same BitWidth");
2198 }
2199#endif
2200
2201 const APInt *C;
2202 if (match(V, m_APInt(C))) {
2203 // We know all of the bits for a scalar constant or a splat vector constant!
2204 Known = KnownBits::makeConstant(*C);
2205 return;
2206 }
2207 // Null and aggregate-zero are all-zeros.
2208 if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
2209 Known.setAllZero();
2210 return;
2211 }
2212 // Handle a constant vector by taking the intersection of the known bits of
2213 // each element.
2214 if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) {
2215 assert(!isa<ScalableVectorType>(V->getType()));
2216 // We know that CDV must be a vector of integers. Take the intersection of
2217 // each element.
2218 Known.Zero.setAllBits(); Known.One.setAllBits();
2219 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2220 if (!DemandedElts[i])
2221 continue;
2222 APInt Elt = CDV->getElementAsAPInt(i);
2223 Known.Zero &= ~Elt;
2224 Known.One &= Elt;
2225 }
2226 if (Known.hasConflict())
2227 Known.resetAll();
2228 return;
2229 }
2230
2231 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2232 assert(!isa<ScalableVectorType>(V->getType()));
2233 // We know that CV must be a vector of integers. Take the intersection of
2234 // each element.
2235 Known.Zero.setAllBits(); Known.One.setAllBits();
2236 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2237 if (!DemandedElts[i])
2238 continue;
2239 Constant *Element = CV->getAggregateElement(i);
2240 if (isa<PoisonValue>(Element))
2241 continue;
2242 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2243 if (!ElementCI) {
2244 Known.resetAll();
2245 return;
2246 }
2247 const APInt &Elt = ElementCI->getValue();
2248 Known.Zero &= ~Elt;
2249 Known.One &= Elt;
2250 }
2251 if (Known.hasConflict())
2252 Known.resetAll();
2253 return;
2254 }
2255
2256 // Start out not knowing anything.
2257 Known.resetAll();
2258
2259 // We can't imply anything about undefs.
2260 if (isa<UndefValue>(V))
2261 return;
2262
2263 // There's no point in looking through other users of ConstantData for
2264 // assumptions. Confirm that we've handled them all.
2265 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2266
2267 if (const auto *A = dyn_cast<Argument>(V))
2268 if (std::optional<ConstantRange> Range = A->getRange())
2269 Known = Range->toKnownBits();
2270
2271 // All recursive calls that increase depth must come after this.
2273 return;
2274
2275 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2276 // the bits of its aliasee.
2277 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2278 if (!GA->isInterposable())
2279 computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q);
2280 return;
2281 }
2282
2283 if (const Operator *I = dyn_cast<Operator>(V))
2284 computeKnownBitsFromOperator(I, DemandedElts, Known, Depth, Q);
2285 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2286 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2287 Known = CR->toKnownBits();
2288 }
2289
2290 // Aligned pointers have trailing zeros - refine Known.Zero set
2291 if (isa<PointerType>(V->getType())) {
2292 Align Alignment = V->getPointerAlignment(Q.DL);
2293 Known.Zero.setLowBits(Log2(Alignment));
2294 }
2295
2296 // computeKnownBitsFromContext strictly refines Known.
2297 // Therefore, we run them after computeKnownBitsFromOperator.
2298
2299 // Check whether we can determine known bits from context such as assumes.
2300 computeKnownBitsFromContext(V, Known, Depth, Q);
2301}
2302
2303/// Try to detect a recurrence that the value of the induction variable is
2304/// always a power of two (or zero).
2305static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2306 unsigned Depth, SimplifyQuery &Q) {
2307 BinaryOperator *BO = nullptr;
2308 Value *Start = nullptr, *Step = nullptr;
2309 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2310 return false;
2311
2312 // Initial value must be a power of two.
2313 for (const Use &U : PN->operands()) {
2314 if (U.get() == Start) {
2315 // Initial value comes from a different BB, need to adjust context
2316 // instruction for analysis.
2317 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2318 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Depth, Q))
2319 return false;
2320 }
2321 }
2322
2323 // Except for Mul, the induction variable must be on the left side of the
2324 // increment expression, otherwise its value can be arbitrary.
2325 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2326 return false;
2327
2328 Q.CxtI = BO->getParent()->getTerminator();
2329 switch (BO->getOpcode()) {
2330 case Instruction::Mul:
2331 // Power of two is closed under multiplication.
2332 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2333 Q.IIQ.hasNoSignedWrap(BO)) &&
2334 isKnownToBeAPowerOfTwo(Step, OrZero, Depth, Q);
2335 case Instruction::SDiv:
2336 // Start value must not be signmask for signed division, so simply being a
2337 // power of two is not sufficient, and it has to be a constant.
2338 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2339 return false;
2340 [[fallthrough]];
2341 case Instruction::UDiv:
2342 // Divisor must be a power of two.
2343 // If OrZero is false, cannot guarantee induction variable is non-zero after
2344 // division, same for Shr, unless it is exact division.
2345 return (OrZero || Q.IIQ.isExact(BO)) &&
2346 isKnownToBeAPowerOfTwo(Step, false, Depth, Q);
2347 case Instruction::Shl:
2348 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2349 case Instruction::AShr:
2350 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2351 return false;
2352 [[fallthrough]];
2353 case Instruction::LShr:
2354 return OrZero || Q.IIQ.isExact(BO);
2355 default:
2356 return false;
2357 }
2358}
2359
2360/// Return true if we can infer that \p V is known to be a power of 2 from
2361/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2362static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2363 const Value *Cond,
2364 bool CondIsTrue) {
2365 CmpPredicate Pred;
2366 const APInt *RHSC;
2367 if (!match(Cond, m_ICmp(Pred, m_Intrinsic<Intrinsic::ctpop>(m_Specific(V)),
2368 m_APInt(RHSC))))
2369 return false;
2370 if (!CondIsTrue)
2371 Pred = ICmpInst::getInversePredicate(Pred);
2372 // ctpop(V) u< 2
2373 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2374 return true;
2375 // ctpop(V) == 1
2376 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2377}
2378
2379/// Return true if the given value is known to have exactly one
2380/// bit set when defined. For vectors return true if every element is known to
2381/// be a power of two when defined. Supports values with integer or pointer
2382/// types and vectors of integers.
2383bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
2384 const SimplifyQuery &Q) {
2385 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2386
2387 if (isa<Constant>(V))
2388 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2389
2390 // i1 is by definition a power of 2 or zero.
2391 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2392 return true;
2393
2394 // Try to infer from assumptions.
2395 if (Q.AC && Q.CxtI) {
2396 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2397 if (!AssumeVH)
2398 continue;
2399 CallInst *I = cast<CallInst>(AssumeVH);
2400 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2401 /*CondIsTrue=*/true) &&
2403 return true;
2404 }
2405 }
2406
2407 // Handle dominating conditions.
2408 if (Q.DC && Q.CxtI && Q.DT) {
2409 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2410 Value *Cond = BI->getCondition();
2411
2412 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2414 /*CondIsTrue=*/true) &&
2415 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2416 return true;
2417
2418 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2420 /*CondIsTrue=*/false) &&
2421 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2422 return true;
2423 }
2424 }
2425
2426 auto *I = dyn_cast<Instruction>(V);
2427 if (!I)
2428 return false;
2429
2430 if (Q.CxtI && match(V, m_VScale())) {
2431 const Function *F = Q.CxtI->getFunction();
2432 // The vscale_range indicates vscale is a power-of-two.
2433 return F->hasFnAttribute(Attribute::VScaleRange);
2434 }
2435
2436 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2437 // it is shifted off the end then the result is undefined.
2438 if (match(I, m_Shl(m_One(), m_Value())))
2439 return true;
2440
2441 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2442 // the bottom. If it is shifted off the bottom then the result is undefined.
2443 if (match(I, m_LShr(m_SignMask(), m_Value())))
2444 return true;
2445
2446 // The remaining tests are all recursive, so bail out if we hit the limit.
2448 return false;
2449
2450 switch (I->getOpcode()) {
2451 case Instruction::ZExt:
2452 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2453 case Instruction::Trunc:
2454 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2455 case Instruction::Shl:
2456 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2457 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2458 return false;
2459 case Instruction::LShr:
2460 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2461 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2462 return false;
2463 case Instruction::UDiv:
2464 if (Q.IIQ.isExact(cast<BinaryOperator>(I)))
2465 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2466 return false;
2467 case Instruction::Mul:
2468 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2469 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q) &&
2470 (OrZero || isKnownNonZero(I, Q, Depth));
2471 case Instruction::And:
2472 // A power of two and'd with anything is a power of two or zero.
2473 if (OrZero &&
2474 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Depth, Q) ||
2475 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Depth, Q)))
2476 return true;
2477 // X & (-X) is always a power of two or zero.
2478 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2479 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2480 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2481 return false;
2482 case Instruction::Add: {
2483 // Adding a power-of-two or zero to the same power-of-two or zero yields
2484 // either the original power-of-two, a larger power-of-two or zero.
2485 const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
2486 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2487 Q.IIQ.hasNoSignedWrap(VOBO)) {
2488 if (match(I->getOperand(0),
2489 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2490 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q))
2491 return true;
2492 if (match(I->getOperand(1),
2493 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2494 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q))
2495 return true;
2496
2497 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2498 KnownBits LHSBits(BitWidth);
2499 computeKnownBits(I->getOperand(0), LHSBits, Depth, Q);
2500
2501 KnownBits RHSBits(BitWidth);
2502 computeKnownBits(I->getOperand(1), RHSBits, Depth, Q);
2503 // If i8 V is a power of two or zero:
2504 // ZeroBits: 1 1 1 0 1 1 1 1
2505 // ~ZeroBits: 0 0 0 1 0 0 0 0
2506 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2507 // If OrZero isn't set, we cannot give back a zero result.
2508 // Make sure either the LHS or RHS has a bit set.
2509 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2510 return true;
2511 }
2512
2513 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2514 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2515 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2516 return true;
2517 return false;
2518 }
2519 case Instruction::Select:
2520 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2521 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Depth, Q);
2522 case Instruction::PHI: {
2523 // A PHI node is power of two if all incoming values are power of two, or if
2524 // it is an induction variable where in each step its value is a power of
2525 // two.
2526 auto *PN = cast<PHINode>(I);
2528
2529 // Check if it is an induction variable and always power of two.
2530 if (isPowerOfTwoRecurrence(PN, OrZero, Depth, RecQ))
2531 return true;
2532
2533 // Recursively check all incoming values. Limit recursion to 2 levels, so
2534 // that search complexity is limited to number of operands^2.
2535 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2536 return llvm::all_of(PN->operands(), [&](const Use &U) {
2537 // Value is power of 2 if it is coming from PHI node itself by induction.
2538 if (U.get() == PN)
2539 return true;
2540
2541 // Change the context instruction to the incoming block where it is
2542 // evaluated.
2543 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2544 return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ);
2545 });
2546 }
2547 case Instruction::Invoke:
2548 case Instruction::Call: {
2549 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2550 switch (II->getIntrinsicID()) {
2551 case Intrinsic::umax:
2552 case Intrinsic::smax:
2553 case Intrinsic::umin:
2554 case Intrinsic::smin:
2555 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Depth, Q) &&
2556 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2557 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2558 // thus dont change pow2/non-pow2 status.
2559 case Intrinsic::bitreverse:
2560 case Intrinsic::bswap:
2561 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2562 case Intrinsic::fshr:
2563 case Intrinsic::fshl:
2564 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2565 if (II->getArgOperand(0) == II->getArgOperand(1))
2566 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2567 break;
2568 default:
2569 break;
2570 }
2571 }
2572 return false;
2573 }
2574 default:
2575 return false;
2576 }
2577}
2578
2579/// Test whether a GEP's result is known to be non-null.
2580///
2581/// Uses properties inherent in a GEP to try to determine whether it is known
2582/// to be non-null.
2583///
2584/// Currently this routine does not support vector GEPs.
2585static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
2586 const SimplifyQuery &Q) {
2587 const Function *F = nullptr;
2588 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2589 F = I->getFunction();
2590
2591 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2592 // may be null iff the base pointer is null and the offset is zero.
2593 if (!GEP->hasNoUnsignedWrap() &&
2594 !(GEP->isInBounds() &&
2595 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2596 return false;
2597
2598 // FIXME: Support vector-GEPs.
2599 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2600
2601 // If the base pointer is non-null, we cannot walk to a null address with an
2602 // inbounds GEP in address space zero.
2603 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2604 return true;
2605
2606 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2607 // If so, then the GEP cannot produce a null pointer, as doing so would
2608 // inherently violate the inbounds contract within address space zero.
2610 GTI != GTE; ++GTI) {
2611 // Struct types are easy -- they must always be indexed by a constant.
2612 if (StructType *STy = GTI.getStructTypeOrNull()) {
2613 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2614 unsigned ElementIdx = OpC->getZExtValue();
2615 const StructLayout *SL = Q.DL.getStructLayout(STy);
2616 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2617 if (ElementOffset > 0)
2618 return true;
2619 continue;
2620 }
2621
2622 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2623 if (GTI.getSequentialElementStride(Q.DL).isZero())
2624 continue;
2625
2626 // Fast path the constant operand case both for efficiency and so we don't
2627 // increment Depth when just zipping down an all-constant GEP.
2628 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2629 if (!OpC->isZero())
2630 return true;
2631 continue;
2632 }
2633
2634 // We post-increment Depth here because while isKnownNonZero increments it
2635 // as well, when we pop back up that increment won't persist. We don't want
2636 // to recurse 10k times just because we have 10k GEP operands. We don't
2637 // bail completely out because we want to handle constant GEPs regardless
2638 // of depth.
2640 continue;
2641
2642 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2643 return true;
2644 }
2645
2646 return false;
2647}
2648
2650 const Instruction *CtxI,
2651 const DominatorTree *DT) {
2652 assert(!isa<Constant>(V) && "Called for constant?");
2653
2654 if (!CtxI || !DT)
2655 return false;
2656
2657 unsigned NumUsesExplored = 0;
2658 for (auto &U : V->uses()) {
2659 // Avoid massive lists
2660 if (NumUsesExplored >= DomConditionsMaxUses)
2661 break;
2662 NumUsesExplored++;
2663
2664 const Instruction *UI = cast<Instruction>(U.getUser());
2665 // If the value is used as an argument to a call or invoke, then argument
2666 // attributes may provide an answer about null-ness.
2667 if (V->getType()->isPointerTy()) {
2668 if (const auto *CB = dyn_cast<CallBase>(UI)) {
2669 if (CB->isArgOperand(&U) &&
2670 CB->paramHasNonNullAttr(CB->getArgOperandNo(&U),
2671 /*AllowUndefOrPoison=*/false) &&
2672 DT->dominates(CB, CtxI))
2673 return true;
2674 }
2675 }
2676
2677 // If the value is used as a load/store, then the pointer must be non null.
2678 if (V == getLoadStorePointerOperand(UI)) {
2680 V->getType()->getPointerAddressSpace()) &&
2681 DT->dominates(UI, CtxI))
2682 return true;
2683 }
2684
2685 if ((match(UI, m_IDiv(m_Value(), m_Specific(V))) ||
2686 match(UI, m_IRem(m_Value(), m_Specific(V)))) &&
2687 isValidAssumeForContext(UI, CtxI, DT))
2688 return true;
2689
2690 // Consider only compare instructions uniquely controlling a branch
2691 Value *RHS;
2692 CmpPredicate Pred;
2693 if (!match(UI, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2694 continue;
2695
2696 bool NonNullIfTrue;
2697 if (cmpExcludesZero(Pred, RHS))
2698 NonNullIfTrue = true;
2700 NonNullIfTrue = false;
2701 else
2702 continue;
2703
2706 for (const auto *CmpU : UI->users()) {
2707 assert(WorkList.empty() && "Should be!");
2708 if (Visited.insert(CmpU).second)
2709 WorkList.push_back(CmpU);
2710
2711 while (!WorkList.empty()) {
2712 auto *Curr = WorkList.pop_back_val();
2713
2714 // If a user is an AND, add all its users to the work list. We only
2715 // propagate "pred != null" condition through AND because it is only
2716 // correct to assume that all conditions of AND are met in true branch.
2717 // TODO: Support similar logic of OR and EQ predicate?
2718 if (NonNullIfTrue)
2719 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2720 for (const auto *CurrU : Curr->users())
2721 if (Visited.insert(CurrU).second)
2722 WorkList.push_back(CurrU);
2723 continue;
2724 }
2725
2726 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2727 assert(BI->isConditional() && "uses a comparison!");
2728
2729 BasicBlock *NonNullSuccessor =
2730 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2731 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2732 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2733 return true;
2734 } else if (NonNullIfTrue && isGuard(Curr) &&
2735 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2736 return true;
2737 }
2738 }
2739 }
2740 }
2741
2742 return false;
2743}
2744
2745/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2746/// ensure that the value it's attached to is never Value? 'RangeType' is
2747/// is the type of the value described by the range.
2748static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2749 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2750 assert(NumRanges >= 1);
2751 for (unsigned i = 0; i < NumRanges; ++i) {
2753 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2755 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2756 ConstantRange Range(Lower->getValue(), Upper->getValue());
2757 if (Range.contains(Value))
2758 return false;
2759 }
2760 return true;
2761}
2762
2763/// Try to detect a recurrence that monotonically increases/decreases from a
2764/// non-zero starting value. These are common as induction variables.
2765static bool isNonZeroRecurrence(const PHINode *PN) {
2766 BinaryOperator *BO = nullptr;
2767 Value *Start = nullptr, *Step = nullptr;
2768 const APInt *StartC, *StepC;
2769 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2770 !match(Start, m_APInt(StartC)) || StartC->isZero())
2771 return false;
2772
2773 switch (BO->getOpcode()) {
2774 case Instruction::Add:
2775 // Starting from non-zero and stepping away from zero can never wrap back
2776 // to zero.
2777 return BO->hasNoUnsignedWrap() ||
2778 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
2779 StartC->isNegative() == StepC->isNegative());
2780 case Instruction::Mul:
2781 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2782 match(Step, m_APInt(StepC)) && !StepC->isZero();
2783 case Instruction::Shl:
2784 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
2785 case Instruction::AShr:
2786 case Instruction::LShr:
2787 return BO->isExact();
2788 default:
2789 return false;
2790 }
2791}
2792
2793static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
2794 return match(Op0, m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,
2795 m_Specific(Op1), m_Zero()))) ||
2796 match(Op1, m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,
2797 m_Specific(Op0), m_Zero())));
2798}
2799
2800static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth,
2801 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2802 Value *Y, bool NSW, bool NUW) {
2803 // (X + (X != 0)) is non zero
2804 if (matchOpWithOpEqZero(X, Y))
2805 return true;
2806
2807 if (NUW)
2808 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2809 isKnownNonZero(X, DemandedElts, Q, Depth);
2810
2811 KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2812 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2813
2814 // If X and Y are both non-negative (as signed values) then their sum is not
2815 // zero unless both X and Y are zero.
2816 if (XKnown.isNonNegative() && YKnown.isNonNegative())
2817 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2818 isKnownNonZero(X, DemandedElts, Q, Depth))
2819 return true;
2820
2821 // If X and Y are both negative (as signed values) then their sum is not
2822 // zero unless both X and Y equal INT_MIN.
2823 if (XKnown.isNegative() && YKnown.isNegative()) {
2825 // The sign bit of X is set. If some other bit is set then X is not equal
2826 // to INT_MIN.
2827 if (XKnown.One.intersects(Mask))
2828 return true;
2829 // The sign bit of Y is set. If some other bit is set then Y is not equal
2830 // to INT_MIN.
2831 if (YKnown.One.intersects(Mask))
2832 return true;
2833 }
2834
2835 // The sum of a non-negative number and a power of two is not zero.
2836 if (XKnown.isNonNegative() &&
2837 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q))
2838 return true;
2839 if (YKnown.isNonNegative() &&
2840 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
2841 return true;
2842
2843 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
2844}
2845
2846static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth,
2847 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2848 Value *Y) {
2849 // (X - (X != 0)) is non zero
2850 // ((X != 0) - X) is non zero
2851 if (matchOpWithOpEqZero(X, Y))
2852 return true;
2853
2854 // TODO: Move this case into isKnownNonEqual().
2855 if (auto *C = dyn_cast<Constant>(X))
2856 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
2857 return true;
2858
2859 return ::isKnownNonEqual(X, Y, DemandedElts, Depth, Q);
2860}
2861
2862static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth,
2863 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2864 Value *Y, bool NSW, bool NUW) {
2865 // If X and Y are non-zero then so is X * Y as long as the multiplication
2866 // does not overflow.
2867 if (NSW || NUW)
2868 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
2869 isKnownNonZero(Y, DemandedElts, Q, Depth);
2870
2871 // If either X or Y is odd, then if the other is non-zero the result can't
2872 // be zero.
2873 KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2874 if (XKnown.One[0])
2875 return isKnownNonZero(Y, DemandedElts, Q, Depth);
2876
2877 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2878 if (YKnown.One[0])
2879 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
2880
2881 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
2882 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
2883 // the lowest known One of X and Y. If they are non-zero, the result
2884 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
2885 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
2886 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
2887 BitWidth;
2888}
2889
2890static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
2891 unsigned Depth, const SimplifyQuery &Q,
2892 const KnownBits &KnownVal) {
2893 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2894 switch (I->getOpcode()) {
2895 case Instruction::Shl:
2896 return Lhs.shl(Rhs);
2897 case Instruction::LShr:
2898 return Lhs.lshr(Rhs);
2899 case Instruction::AShr:
2900 return Lhs.ashr(Rhs);
2901 default:
2902 llvm_unreachable("Unknown Shift Opcode");
2903 }
2904 };
2905
2906 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2907 switch (I->getOpcode()) {
2908 case Instruction::Shl:
2909 return Lhs.lshr(Rhs);
2910 case Instruction::LShr:
2911 case Instruction::AShr:
2912 return Lhs.shl(Rhs);
2913 default:
2914 llvm_unreachable("Unknown Shift Opcode");
2915 }
2916 };
2917
2918 if (KnownVal.isUnknown())
2919 return false;
2920
2921 KnownBits KnownCnt =
2922 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
2923 APInt MaxShift = KnownCnt.getMaxValue();
2924 unsigned NumBits = KnownVal.getBitWidth();
2925 if (MaxShift.uge(NumBits))
2926 return false;
2927
2928 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
2929 return true;
2930
2931 // If all of the bits shifted out are known to be zero, and Val is known
2932 // non-zero then at least one non-zero bit must remain.
2933 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
2934 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
2935 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
2936 return true;
2937
2938 return false;
2939}
2940
2942 const APInt &DemandedElts,
2943 unsigned Depth, const SimplifyQuery &Q) {
2944 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
2945 switch (I->getOpcode()) {
2946 case Instruction::Alloca:
2947 // Alloca never returns null, malloc might.
2948 return I->getType()->getPointerAddressSpace() == 0;
2949 case Instruction::GetElementPtr:
2950 if (I->getType()->isPointerTy())
2951 return isGEPKnownNonNull(cast<GEPOperator>(I), Depth, Q);
2952 break;
2953 case Instruction::BitCast: {
2954 // We need to be a bit careful here. We can only peek through the bitcast
2955 // if the scalar size of elements in the operand are smaller than and a
2956 // multiple of the size they are casting too. Take three cases:
2957 //
2958 // 1) Unsafe:
2959 // bitcast <2 x i16> %NonZero to <4 x i8>
2960 //
2961 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
2962 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
2963 // guranteed (imagine just sign bit set in the 2 i16 elements).
2964 //
2965 // 2) Unsafe:
2966 // bitcast <4 x i3> %NonZero to <3 x i4>
2967 //
2968 // Even though the scalar size of the src (`i3`) is smaller than the
2969 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
2970 // its possible for the `3 x i4` elements to be zero because there are
2971 // some elements in the destination that don't contain any full src
2972 // element.
2973 //
2974 // 3) Safe:
2975 // bitcast <4 x i8> %NonZero to <2 x i16>
2976 //
2977 // This is always safe as non-zero in the 4 i8 elements implies
2978 // non-zero in the combination of any two adjacent ones. Since i8 is a
2979 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
2980 // This all implies the 2 i16 elements are non-zero.
2981 Type *FromTy = I->getOperand(0)->getType();
2982 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
2983 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
2984 return isKnownNonZero(I->getOperand(0), Q, Depth);
2985 } break;
2986 case Instruction::IntToPtr:
2987 // Note that we have to take special care to avoid looking through
2988 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
2989 // as casts that can alter the value, e.g., AddrSpaceCasts.
2990 if (!isa<ScalableVectorType>(I->getType()) &&
2991 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2992 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2993 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2994 break;
2995 case Instruction::PtrToInt:
2996 // Similar to int2ptr above, we can look through ptr2int here if the cast
2997 // is a no-op or an extend and not a truncate.
2998 if (!isa<ScalableVectorType>(I->getType()) &&
2999 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
3000 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
3001 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3002 break;
3003 case Instruction::Trunc:
3004 // nuw/nsw trunc preserves zero/non-zero status of input.
3005 if (auto *TI = dyn_cast<TruncInst>(I))
3006 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3007 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
3008 break;
3009
3010 case Instruction::Sub:
3011 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
3012 I->getOperand(1));
3013 case Instruction::Xor:
3014 // (X ^ (X != 0)) is non zero
3015 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3016 return true;
3017 break;
3018 case Instruction::Or:
3019 // (X | (X != 0)) is non zero
3020 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3021 return true;
3022 // X | Y != 0 if X != 0 or Y != 0.
3023 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3024 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3025 case Instruction::SExt:
3026 case Instruction::ZExt:
3027 // ext X != 0 if X != 0.
3028 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3029
3030 case Instruction::Shl: {
3031 // shl nsw/nuw can't remove any non-zero bits.
3032 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
3033 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3034 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3035
3036 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3037 // if the lowest bit is shifted off the end.
3038 KnownBits Known(BitWidth);
3039 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth, Q);
3040 if (Known.One[0])
3041 return true;
3042
3043 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
3044 }
3045 case Instruction::LShr:
3046 case Instruction::AShr: {
3047 // shr exact can only shift out zero bits.
3048 const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(I);
3049 if (BO->isExact())
3050 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3051
3052 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3053 // defined if the sign bit is shifted off the end.
3054 KnownBits Known =
3055 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
3056 if (Known.isNegative())
3057 return true;
3058
3059 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
3060 }
3061 case Instruction::UDiv:
3062 case Instruction::SDiv: {
3063 // X / Y
3064 // div exact can only produce a zero if the dividend is zero.
3065 if (cast<PossiblyExactOperator>(I)->isExact())
3066 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3067
3068 KnownBits XKnown =
3069 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
3070 // If X is fully unknown we won't be able to figure anything out so don't
3071 // both computing knownbits for Y.
3072 if (XKnown.isUnknown())
3073 return false;
3074
3075 KnownBits YKnown =
3076 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
3077 if (I->getOpcode() == Instruction::SDiv) {
3078 // For signed division need to compare abs value of the operands.
3079 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3080 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3081 }
3082 // If X u>= Y then div is non zero (0/0 is UB).
3083 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3084 // If X is total unknown or X u< Y we won't be able to prove non-zero
3085 // with compute known bits so just return early.
3086 return XUgeY && *XUgeY;
3087 }
3088 case Instruction::Add: {
3089 // X + Y.
3090
3091 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3092 // non-zero.
3093 auto *BO = cast<OverflowingBinaryOperator>(I);
3094 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
3095 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3096 Q.IIQ.hasNoUnsignedWrap(BO));
3097 }
3098 case Instruction::Mul: {
3099 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
3100 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
3101 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3102 Q.IIQ.hasNoUnsignedWrap(BO));
3103 }
3104 case Instruction::Select: {
3105 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3106
3107 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3108 // then see if the select condition implies the arm is non-zero. For example
3109 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3110 // dominated by `X != 0`.
3111 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3112 Value *Op;
3113 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3114 // Op is trivially non-zero.
3115 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3116 return true;
3117
3118 // The condition of the select dominates the true/false arm. Check if the
3119 // condition implies that a given arm is non-zero.
3120 Value *X;
3121 CmpPredicate Pred;
3122 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3123 return false;
3124
3125 if (!IsTrueArm)
3126 Pred = ICmpInst::getInversePredicate(Pred);
3127
3128 return cmpExcludesZero(Pred, X);
3129 };
3130
3131 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3132 SelectArmIsNonZero(/* IsTrueArm */ false))
3133 return true;
3134 break;
3135 }
3136 case Instruction::PHI: {
3137 auto *PN = cast<PHINode>(I);
3139 return true;
3140
3141 // Check if all incoming values are non-zero using recursion.
3143 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3144 return llvm::all_of(PN->operands(), [&](const Use &U) {
3145 if (U.get() == PN)
3146 return true;
3147 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3148 // Check if the branch on the phi excludes zero.
3149 CmpPredicate Pred;
3150 Value *X;
3151 BasicBlock *TrueSucc, *FalseSucc;
3152 if (match(RecQ.CxtI,
3153 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3154 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3155 // Check for cases of duplicate successors.
3156 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3157 // If we're using the false successor, invert the predicate.
3158 if (FalseSucc == PN->getParent())
3159 Pred = CmpInst::getInversePredicate(Pred);
3160 if (cmpExcludesZero(Pred, X))
3161 return true;
3162 }
3163 }
3164 // Finally recurse on the edge and check it directly.
3165 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3166 });
3167 }
3168 case Instruction::InsertElement: {
3169 if (isa<ScalableVectorType>(I->getType()))
3170 break;
3171
3172 const Value *Vec = I->getOperand(0);
3173 const Value *Elt = I->getOperand(1);
3174 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3175
3176 unsigned NumElts = DemandedElts.getBitWidth();
3177 APInt DemandedVecElts = DemandedElts;
3178 bool SkipElt = false;
3179 // If we know the index we are inserting too, clear it from Vec check.
3180 if (CIdx && CIdx->getValue().ult(NumElts)) {
3181 DemandedVecElts.clearBit(CIdx->getZExtValue());
3182 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3183 }
3184
3185 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3186 // are non-zero.
3187 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3188 (DemandedVecElts.isZero() ||
3189 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3190 }
3191 case Instruction::ExtractElement:
3192 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3193 const Value *Vec = EEI->getVectorOperand();
3194 const Value *Idx = EEI->getIndexOperand();
3195 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3196 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3197 unsigned NumElts = VecTy->getNumElements();
3198 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3199 if (CIdx && CIdx->getValue().ult(NumElts))
3200 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3201 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3202 }
3203 }
3204 break;
3205 case Instruction::ShuffleVector: {
3206 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3207 if (!Shuf)
3208 break;
3209 APInt DemandedLHS, DemandedRHS;
3210 // For undef elements, we don't know anything about the common state of
3211 // the shuffle result.
3212 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3213 break;
3214 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3215 return (DemandedRHS.isZero() ||
3216 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3217 (DemandedLHS.isZero() ||
3218 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3219 }
3220 case Instruction::Freeze:
3221 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3222 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3223 Depth);
3224 case Instruction::Load: {
3225 auto *LI = cast<LoadInst>(I);
3226 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3227 // is never null.
3228 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3229 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3230 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3231 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3232 return true;
3233 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3235 }
3236
3237 // No need to fall through to computeKnownBits as range metadata is already
3238 // handled in isKnownNonZero.
3239 return false;
3240 }
3241 case Instruction::ExtractValue: {
3242 const WithOverflowInst *WO;
3243 if (match(I, m_ExtractValue<0>(m_WithOverflowInst(WO)))) {
3244 switch (WO->getBinaryOp()) {
3245 default:
3246 break;
3247 case Instruction::Add:
3248 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
3249 WO->getArgOperand(0), WO->getArgOperand(1),
3250 /*NSW=*/false,
3251 /*NUW=*/false);
3252 case Instruction::Sub:
3253 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
3254 WO->getArgOperand(0), WO->getArgOperand(1));
3255 case Instruction::Mul:
3256 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth,
3257 WO->getArgOperand(0), WO->getArgOperand(1),
3258 /*NSW=*/false, /*NUW=*/false);
3259 break;
3260 }
3261 }
3262 break;
3263 }
3264 case Instruction::Call:
3265 case Instruction::Invoke: {
3266 const auto *Call = cast<CallBase>(I);
3267 if (I->getType()->isPointerTy()) {
3268 if (Call->isReturnNonNull())
3269 return true;
3270 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3271 return isKnownNonZero(RP, Q, Depth);
3272 } else {
3273 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3275 if (std::optional<ConstantRange> Range = Call->getRange()) {
3276 const APInt ZeroValue(Range->getBitWidth(), 0);
3277 if (!Range->contains(ZeroValue))
3278 return true;
3279 }
3280 if (const Value *RV = Call->getReturnedArgOperand())
3281 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3282 return true;
3283 }
3284
3285 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3286 switch (II->getIntrinsicID()) {
3287 case Intrinsic::sshl_sat:
3288 case Intrinsic::ushl_sat:
3289 case Intrinsic::abs:
3290 case Intrinsic::bitreverse:
3291 case Intrinsic::bswap:
3292 case Intrinsic::ctpop:
3293 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3294 // NB: We don't do usub_sat here as in any case we can prove its
3295 // non-zero, we will fold it to `sub nuw` in InstCombine.
3296 case Intrinsic::ssub_sat:
3297 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
3298 II->getArgOperand(0), II->getArgOperand(1));
3299 case Intrinsic::sadd_sat:
3300 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
3301 II->getArgOperand(0), II->getArgOperand(1),
3302 /*NSW=*/true, /* NUW=*/false);
3303 // Vec reverse preserves zero/non-zero status from input vec.
3304 case Intrinsic::vector_reverse:
3305 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3306 Q, Depth);
3307 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3308 case Intrinsic::vector_reduce_or:
3309 case Intrinsic::vector_reduce_umax:
3310 case Intrinsic::vector_reduce_umin:
3311 case Intrinsic::vector_reduce_smax:
3312 case Intrinsic::vector_reduce_smin:
3313 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3314 case Intrinsic::umax:
3315 case Intrinsic::uadd_sat:
3316 // umax(X, (X != 0)) is non zero
3317 // X +usat (X != 0) is non zero
3318 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3319 return true;
3320
3321 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3322 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3323 case Intrinsic::smax: {
3324 // If either arg is strictly positive the result is non-zero. Otherwise
3325 // the result is non-zero if both ops are non-zero.
3326 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3327 const KnownBits &OpKnown) {
3328 if (!OpNonZero.has_value())
3329 OpNonZero = OpKnown.isNonZero() ||
3330 isKnownNonZero(Op, DemandedElts, Q, Depth);
3331 return *OpNonZero;
3332 };
3333 // Avoid re-computing isKnownNonZero.
3334 std::optional<bool> Op0NonZero, Op1NonZero;
3335 KnownBits Op1Known =
3336 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
3337 if (Op1Known.isNonNegative() &&
3338 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3339 return true;
3340 KnownBits Op0Known =
3341 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
3342 if (Op0Known.isNonNegative() &&
3343 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3344 return true;
3345 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3346 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3347 }
3348 case Intrinsic::smin: {
3349 // If either arg is negative the result is non-zero. Otherwise
3350 // the result is non-zero if both ops are non-zero.
3351 KnownBits Op1Known =
3352 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
3353 if (Op1Known.isNegative())
3354 return true;
3355 KnownBits Op0Known =
3356 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
3357 if (Op0Known.isNegative())
3358 return true;
3359
3360 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3361 return true;
3362 }
3363 [[fallthrough]];
3364 case Intrinsic::umin:
3365 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3366 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3367 case Intrinsic::cttz:
3368 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
3369 .Zero[0];
3370 case Intrinsic::ctlz:
3371 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
3372 .isNonNegative();
3373 case Intrinsic::fshr:
3374 case Intrinsic::fshl:
3375 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3376 if (II->getArgOperand(0) == II->getArgOperand(1))
3377 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3378 break;
3379 case Intrinsic::vscale:
3380 return true;
3381 case Intrinsic::experimental_get_vector_length:
3382 return isKnownNonZero(I->getOperand(0), Q, Depth);
3383 default:
3384 break;
3385 }
3386 break;
3387 }
3388
3389 return false;
3390 }
3391 }
3392
3393 KnownBits Known(BitWidth);
3394 computeKnownBits(I, DemandedElts, Known, Depth, Q);
3395 return Known.One != 0;
3396}
3397
3398/// Return true if the given value is known to be non-zero when defined. For
3399/// vectors, return true if every demanded element is known to be non-zero when
3400/// defined. For pointers, if the context instruction and dominator tree are
3401/// specified, perform context-sensitive analysis and return true if the
3402/// pointer couldn't possibly be null at the specified instruction.
3403/// Supports values with integer or pointer type and vectors of integers.
3404bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3405 const SimplifyQuery &Q, unsigned Depth) {
3406 Type *Ty = V->getType();
3407
3408#ifndef NDEBUG
3409 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3410
3411 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3412 assert(
3413 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3414 "DemandedElt width should equal the fixed vector number of elements");
3415 } else {
3416 assert(DemandedElts == APInt(1, 1) &&
3417 "DemandedElt width should be 1 for scalars");
3418 }
3419#endif
3420
3421 if (auto *C = dyn_cast<Constant>(V)) {
3422 if (C->isNullValue())
3423 return false;
3424 if (isa<ConstantInt>(C))
3425 // Must be non-zero due to null test above.
3426 return true;
3427
3428 // For constant vectors, check that all elements are poison or known
3429 // non-zero to determine that the whole vector is known non-zero.
3430 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3431 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3432 if (!DemandedElts[i])
3433 continue;
3434 Constant *Elt = C->getAggregateElement(i);
3435 if (!Elt || Elt->isNullValue())
3436 return false;
3437 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3438 return false;
3439 }
3440 return true;
3441 }
3442
3443 // Constant ptrauth can be null, iff the base pointer can be.
3444 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3445 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3446
3447 // A global variable in address space 0 is non null unless extern weak
3448 // or an absolute symbol reference. Other address spaces may have null as a
3449 // valid address for a global, so we can't assume anything.
3450 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3451 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3452 GV->getType()->getAddressSpace() == 0)
3453 return true;
3454 }
3455
3456 // For constant expressions, fall through to the Operator code below.
3457 if (!isa<ConstantExpr>(V))
3458 return false;
3459 }
3460
3461 if (const auto *A = dyn_cast<Argument>(V))
3462 if (std::optional<ConstantRange> Range = A->getRange()) {
3463 const APInt ZeroValue(Range->getBitWidth(), 0);
3464 if (!Range->contains(ZeroValue))
3465 return true;
3466 }
3467
3468 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3469 return true;
3470
3471 // Some of the tests below are recursive, so bail out if we hit the limit.
3473 return false;
3474
3475 // Check for pointer simplifications.
3476
3477 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3478 // A byval, inalloca may not be null in a non-default addres space. A
3479 // nonnull argument is assumed never 0.
3480 if (const Argument *A = dyn_cast<Argument>(V)) {
3481 if (((A->hasPassPointeeByValueCopyAttr() &&
3482 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3483 A->hasNonNullAttr()))
3484 return true;
3485 }
3486 }
3487
3488 if (const auto *I = dyn_cast<Operator>(V))
3489 if (isKnownNonZeroFromOperator(I, DemandedElts, Depth, Q))
3490 return true;
3491
3492 if (!isa<Constant>(V) &&
3494 return true;
3495
3496 return false;
3497}
3498
3500 unsigned Depth) {
3501 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3502 APInt DemandedElts =
3503 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3504 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3505}
3506
3507/// If the pair of operators are the same invertible function, return the
3508/// the operands of the function corresponding to each input. Otherwise,
3509/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3510/// every input value to exactly one output value. This is equivalent to
3511/// saying that Op1 and Op2 are equal exactly when the specified pair of
3512/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3513static std::optional<std::pair<Value*, Value*>>
3515 const Operator *Op2) {
3516 if (Op1->getOpcode() != Op2->getOpcode())
3517 return std::nullopt;
3518
3519 auto getOperands = [&](unsigned OpNum) -> auto {
3520 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3521 };
3522
3523 switch (Op1->getOpcode()) {
3524 default:
3525 break;
3526 case Instruction::Or:
3527 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3528 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3529 break;
3530 [[fallthrough]];
3531 case Instruction::Xor:
3532 case Instruction::Add: {
3533 Value *Other;
3534 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3535 return std::make_pair(Op1->getOperand(1), Other);
3536 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3537 return std::make_pair(Op1->getOperand(0), Other);
3538 break;
3539 }
3540 case Instruction::Sub:
3541 if (Op1->getOperand(0) == Op2->getOperand(0))
3542 return getOperands(1);
3543 if (Op1->getOperand(1) == Op2->getOperand(1))
3544 return getOperands(0);
3545 break;
3546 case Instruction::Mul: {
3547 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3548 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3549 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3550 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3551 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3552 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3553 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3554 break;
3555
3556 // Assume operand order has been canonicalized
3557 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3558 isa<ConstantInt>(Op1->getOperand(1)) &&
3559 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3560 return getOperands(0);
3561 break;
3562 }
3563 case Instruction::Shl: {
3564 // Same as multiplies, with the difference that we don't need to check
3565 // for a non-zero multiply. Shifts always multiply by non-zero.
3566 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3567 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3568 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3569 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3570 break;
3571
3572 if (Op1->getOperand(1) == Op2->getOperand(1))
3573 return getOperands(0);
3574 break;
3575 }
3576 case Instruction::AShr:
3577 case Instruction::LShr: {
3578 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3579 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3580 if (!PEO1->isExact() || !PEO2->isExact())
3581 break;
3582
3583 if (Op1->getOperand(1) == Op2->getOperand(1))
3584 return getOperands(0);
3585 break;
3586 }
3587 case Instruction::SExt:
3588 case Instruction::ZExt:
3589 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3590 return getOperands(0);
3591 break;
3592 case Instruction::PHI: {
3593 const PHINode *PN1 = cast<PHINode>(Op1);
3594 const PHINode *PN2 = cast<PHINode>(Op2);
3595
3596 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3597 // are a single invertible function of the start values? Note that repeated
3598 // application of an invertible function is also invertible
3599 BinaryOperator *BO1 = nullptr;
3600 Value *Start1 = nullptr, *Step1 = nullptr;
3601 BinaryOperator *BO2 = nullptr;
3602 Value *Start2 = nullptr, *Step2 = nullptr;
3603 if (PN1->getParent() != PN2->getParent() ||
3604 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3605 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3606 break;
3607
3608 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3609 cast<Operator>(BO2));
3610 if (!Values)
3611 break;
3612
3613 // We have to be careful of mutually defined recurrences here. Ex:
3614 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3615 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3616 // The invertibility of these is complicated, and not worth reasoning
3617 // about (yet?).
3618 if (Values->first != PN1 || Values->second != PN2)
3619 break;
3620
3621 return std::make_pair(Start1, Start2);
3622 }
3623 }
3624 return std::nullopt;
3625}
3626
3627/// Return true if V1 == (binop V2, X), where X is known non-zero.
3628/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3629/// implies V2 != V1.
3630static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3631 const APInt &DemandedElts, unsigned Depth,
3632 const SimplifyQuery &Q) {
3633 const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
3634 if (!BO)
3635 return false;
3636 switch (BO->getOpcode()) {
3637 default:
3638 break;
3639 case Instruction::Or:
3640 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3641 break;
3642 [[fallthrough]];
3643 case Instruction::Xor:
3644 case Instruction::Add:
3645 Value *Op = nullptr;
3646 if (V2 == BO->getOperand(0))
3647 Op = BO->getOperand(1);
3648 else if (V2 == BO->getOperand(1))
3649 Op = BO->getOperand(0);
3650 else
3651 return false;
3652 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3653 }
3654 return false;
3655}
3656
3657/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3658/// the multiplication is nuw or nsw.
3659static bool isNonEqualMul(const Value *V1, const Value *V2,
3660 const APInt &DemandedElts, unsigned Depth,
3661 const SimplifyQuery &Q) {
3662 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3663 const APInt *C;
3664 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3665 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3666 !C->isZero() && !C->isOne() &&
3667 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3668 }
3669 return false;
3670}
3671
3672/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3673/// the shift is nuw or nsw.
3674static bool isNonEqualShl(const Value *V1, const Value *V2,
3675 const APInt &DemandedElts, unsigned Depth,
3676 const SimplifyQuery &Q) {
3677 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3678 const APInt *C;
3679 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3680 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3681 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3682 }
3683 return false;
3684}
3685
3686static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3687 const APInt &DemandedElts, unsigned Depth,
3688 const SimplifyQuery &Q) {
3689 // Check two PHIs are in same block.
3690 if (PN1->getParent() != PN2->getParent())
3691 return false;
3692
3694 bool UsedFullRecursion = false;
3695 for (const BasicBlock *IncomBB : PN1->blocks()) {
3696 if (!VisitedBBs.insert(IncomBB).second)
3697 continue; // Don't reprocess blocks that we have dealt with already.
3698 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3699 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3700 const APInt *C1, *C2;
3701 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3702 continue;
3703
3704 // Only one pair of phi operands is allowed for full recursion.
3705 if (UsedFullRecursion)
3706 return false;
3707
3709 RecQ.CxtI = IncomBB->getTerminator();
3710 if (!isKnownNonEqual(IV1, IV2, DemandedElts, Depth + 1, RecQ))
3711 return false;
3712 UsedFullRecursion = true;
3713 }
3714 return true;
3715}
3716
3717static bool isNonEqualSelect(const Value *V1, const Value *V2,
3718 const APInt &DemandedElts, unsigned Depth,
3719 const SimplifyQuery &Q) {
3720 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3721 if (!SI1)
3722 return false;
3723
3724 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3725 const Value *Cond1 = SI1->getCondition();
3726 const Value *Cond2 = SI2->getCondition();
3727 if (Cond1 == Cond2)
3728 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3729 DemandedElts, Depth + 1, Q) &&
3730 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3731 DemandedElts, Depth + 1, Q);
3732 }
3733 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Depth + 1, Q) &&
3734 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Depth + 1, Q);
3735}
3736
3737// Check to see if A is both a GEP and is the incoming value for a PHI in the
3738// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3739// one of them being the recursive GEP A and the other a ptr at same base and at
3740// the same/higher offset than B we are only incrementing the pointer further in
3741// loop if offset of recursive GEP is greater than 0.
3743 const SimplifyQuery &Q) {
3744 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3745 return false;
3746
3747 auto *GEPA = dyn_cast<GEPOperator>(A);
3748 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3749 return false;
3750
3751 // Handle 2 incoming PHI values with one being a recursive GEP.
3752 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3753 if (!PN || PN->getNumIncomingValues() != 2)
3754 return false;
3755
3756 // Search for the recursive GEP as an incoming operand, and record that as
3757 // Step.
3758 Value *Start = nullptr;
3759 Value *Step = const_cast<Value *>(A);
3760 if (PN->getIncomingValue(0) == Step)
3761 Start = PN->getIncomingValue(1);
3762 else if (PN->getIncomingValue(1) == Step)
3763 Start = PN->getIncomingValue(0);
3764 else
3765 return false;
3766
3767 // Other incoming node base should match the B base.
3768 // StartOffset >= OffsetB && StepOffset > 0?
3769 // StartOffset <= OffsetB && StepOffset < 0?
3770 // Is non-equal if above are true.
3771 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3772 // optimisation to inbounds GEPs only.
3773 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3774 APInt StartOffset(IndexWidth, 0);
3775 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3776 APInt StepOffset(IndexWidth, 0);
3777 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3778
3779 // Check if Base Pointer of Step matches the PHI.
3780 if (Step != PN)
3781 return false;
3782 APInt OffsetB(IndexWidth, 0);
3783 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3784 return Start == B &&
3785 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
3786 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
3787}
3788
3789/// Return true if it is known that V1 != V2.
3790static bool isKnownNonEqual(const Value *V1, const Value *V2,
3791 const APInt &DemandedElts, unsigned Depth,
3792 const SimplifyQuery &Q) {
3793 if (V1 == V2)
3794 return false;
3795 if (V1->getType() != V2->getType())
3796 // We can't look through casts yet.
3797 return false;
3798
3800 return false;
3801
3802 // See if we can recurse through (exactly one of) our operands. This
3803 // requires our operation be 1-to-1 and map every input value to exactly
3804 // one output value. Such an operation is invertible.
3805 auto *O1 = dyn_cast<Operator>(V1);
3806 auto *O2 = dyn_cast<Operator>(V2);
3807 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3808 if (auto Values = getInvertibleOperands(O1, O2))
3809 return isKnownNonEqual(Values->first, Values->second, DemandedElts,
3810 Depth + 1, Q);
3811
3812 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3813 const PHINode *PN2 = cast<PHINode>(V2);
3814 // FIXME: This is missing a generalization to handle the case where one is
3815 // a PHI and another one isn't.
3816 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Depth, Q))
3817 return true;
3818 };
3819 }
3820
3821 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Depth, Q) ||
3822 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Depth, Q))
3823 return true;
3824
3825 if (isNonEqualMul(V1, V2, DemandedElts, Depth, Q) ||
3826 isNonEqualMul(V2, V1, DemandedElts, Depth, Q))
3827 return true;
3828
3829 if (isNonEqualShl(V1, V2, DemandedElts, Depth, Q) ||
3830 isNonEqualShl(V2, V1, DemandedElts, Depth, Q))
3831 return true;
3832
3833 if (V1->getType()->isIntOrIntVectorTy()) {
3834 // Are any known bits in V1 contradictory to known bits in V2? If V1
3835 // has a known zero where V2 has a known one, they must not be equal.
3836 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Depth, Q);
3837 if (!Known1.isUnknown()) {
3838 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Depth, Q);
3839 if (Known1.Zero.intersects(Known2.One) ||
3840 Known2.Zero.intersects(Known1.One))
3841 return true;
3842 }
3843 }
3844
3845 if (isNonEqualSelect(V1, V2, DemandedElts, Depth, Q) ||
3846 isNonEqualSelect(V2, V1, DemandedElts, Depth, Q))
3847 return true;
3848
3849 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3851 return true;
3852
3853 Value *A, *B;
3854 // PtrToInts are NonEqual if their Ptrs are NonEqual.
3855 // Check PtrToInt type matches the pointer size.
3856 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
3858 return isKnownNonEqual(A, B, DemandedElts, Depth + 1, Q);
3859
3860 if (!Q.CxtI)
3861 return false;
3862
3863 // Try to infer NonEqual based on information from dominating conditions.
3864 if (Q.DC && Q.DT) {
3865 for (BranchInst *BI : Q.DC->conditionsFor(V1)) {
3866 Value *Cond = BI->getCondition();
3867 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
3868 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()) &&
3869 isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL,
3870 /*LHSIsTrue=*/true, Depth)
3871 .value_or(false))
3872 return true;
3873
3874 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
3875 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()) &&
3876 isImpliedCondition(Cond, ICmpInst::ICMP_NE, V1, V2, Q.DL,
3877 /*LHSIsTrue=*/false, Depth)
3878 .value_or(false))
3879 return true;
3880 }
3881 }
3882
3883 if (!Q.AC)
3884 return false;
3885
3886 // Try to infer NonEqual based on information from assumptions.
3887 for (auto &AssumeVH : Q.AC->assumptionsFor(V1)) {
3888 if (!AssumeVH)
3889 continue;
3890 CallInst *I = cast<CallInst>(AssumeVH);
3891
3892 assert(I->getFunction() == Q.CxtI->getFunction() &&
3893 "Got assumption for the wrong function!");
3894 assert(I->getIntrinsicID() == Intrinsic::assume &&
3895 "must be an assume intrinsic");
3896
3897 if (isImpliedCondition(I->getArgOperand(0), ICmpInst::ICMP_NE, V1, V2, Q.DL,
3898 /*LHSIsTrue=*/true, Depth)
3899 .value_or(false) &&
3901 return true;
3902 }
3903
3904 return false;
3905}
3906
3907/// For vector constants, loop over the elements and find the constant with the
3908/// minimum number of sign bits. Return 0 if the value is not a vector constant
3909/// or if any element was not analyzed; otherwise, return the count for the
3910/// element with the minimum number of sign bits.
3912 const APInt &DemandedElts,
3913 unsigned TyBits) {
3914 const auto *CV = dyn_cast<Constant>(V);
3915 if (!CV || !isa<FixedVectorType>(CV->getType()))
3916 return 0;
3917
3918 unsigned MinSignBits = TyBits;
3919 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3920 for (unsigned i = 0; i != NumElts; ++i) {
3921 if (!DemandedElts[i])
3922 continue;
3923 // If we find a non-ConstantInt, bail out.
3924 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3925 if (!Elt)
3926 return 0;
3927
3928 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3929 }
3930
3931 return MinSignBits;
3932}
3933
3934static unsigned ComputeNumSignBitsImpl(const Value *V,
3935 const APInt &DemandedElts,
3936 unsigned Depth, const SimplifyQuery &Q);
3937
3938static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
3939 unsigned Depth, const SimplifyQuery &Q) {
3940 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q);
3941 assert(Result > 0 && "At least one sign bit needs to be present!");
3942 return Result;
3943}
3944
3945/// Return the number of times the sign bit of the register is replicated into
3946/// the other bits. We know that at least 1 bit is always equal to the sign bit
3947/// (itself), but other cases can give us information. For example, immediately
3948/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
3949/// other, so we return 3. For vectors, return the number of sign bits for the
3950/// vector element with the minimum number of known sign bits of the demanded
3951/// elements in the vector specified by DemandedElts.
3952static unsigned ComputeNumSignBitsImpl(const Value *V,
3953 const APInt &DemandedElts,
3954 unsigned Depth, const SimplifyQuery &Q) {
3955 Type *Ty = V->getType();
3956#ifndef NDEBUG
3957 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3958
3959 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3960 assert(
3961 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3962 "DemandedElt width should equal the fixed vector number of elements");
3963 } else {
3964 assert(DemandedElts == APInt(1, 1) &&
3965 "DemandedElt width should be 1 for scalars");
3966 }
3967#endif
3968
3969 // We return the minimum number of sign bits that are guaranteed to be present
3970 // in V, so for undef we have to conservatively return 1. We don't have the
3971 // same behavior for poison though -- that's a FIXME today.
3972
3973 Type *ScalarTy = Ty->getScalarType();
3974 unsigned TyBits = ScalarTy->isPointerTy() ?
3975 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
3976 Q.DL.getTypeSizeInBits(ScalarTy);
3977
3978 unsigned Tmp, Tmp2;
3979 unsigned FirstAnswer = 1;
3980
3981 // Note that ConstantInt is handled by the general computeKnownBits case
3982 // below.
3983
3985 return 1;
3986
3987 if (auto *U = dyn_cast<Operator>(V)) {
3988 switch (Operator::getOpcode(V)) {
3989 default: break;
3990 case Instruction::SExt:
3991 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
3992 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q) +
3993 Tmp;
3994
3995 case Instruction::SDiv: {
3996 const APInt *Denominator;
3997 // sdiv X, C -> adds log(C) sign bits.
3998 if (match(U->getOperand(1), m_APInt(Denominator))) {
3999
4000 // Ignore non-positive denominator.
4001 if (!Denominator->isStrictlyPositive())
4002 break;
4003
4004 // Calculate the incoming numerator bits.
4005 unsigned NumBits =
4006 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4007
4008 // Add floor(log(C)) bits to the numerator bits.
4009 return std::min(TyBits, NumBits + Denominator->logBase2());
4010 }
4011 break;
4012 }
4013
4014 case Instruction::SRem: {
4015 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4016
4017 const APInt *Denominator;
4018 // srem X, C -> we know that the result is within [-C+1,C) when C is a
4019 // positive constant. This let us put a lower bound on the number of sign
4020 // bits.
4021 if (match(U->getOperand(1), m_APInt(Denominator))) {
4022
4023 // Ignore non-positive denominator.
4024 if (Denominator->isStrictlyPositive()) {
4025 // Calculate the leading sign bit constraints by examining the
4026 // denominator. Given that the denominator is positive, there are two
4027 // cases:
4028 //
4029 // 1. The numerator is positive. The result range is [0,C) and
4030 // [0,C) u< (1 << ceilLogBase2(C)).
4031 //
4032 // 2. The numerator is negative. Then the result range is (-C,0] and
4033 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
4034 //
4035 // Thus a lower bound on the number of sign bits is `TyBits -
4036 // ceilLogBase2(C)`.
4037
4038 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
4039 Tmp = std::max(Tmp, ResBits);
4040 }
4041 }
4042 return Tmp;
4043 }
4044
4045 case Instruction::AShr: {
4046 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4047 // ashr X, C -> adds C sign bits. Vectors too.
4048 const APInt *ShAmt;
4049 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4050 if (ShAmt->uge(TyBits))
4051 break; // Bad shift.
4052 unsigned ShAmtLimited = ShAmt->getZExtValue();
4053 Tmp += ShAmtLimited;
4054 if (Tmp > TyBits) Tmp = TyBits;
4055 }
4056 return Tmp;
4057 }
4058 case Instruction::Shl: {
4059 const APInt *ShAmt;
4060 Value *X = nullptr;
4061 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4062 // shl destroys sign bits.
4063 if (ShAmt->uge(TyBits))
4064 break; // Bad shift.
4065 // We can look through a zext (more or less treating it as a sext) if
4066 // all extended bits are shifted out.
4067 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4068 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4069 Tmp = ComputeNumSignBits(X, DemandedElts, Depth + 1, Q);
4070 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4071 } else
4072 Tmp =
4073 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4074 if (ShAmt->uge(Tmp))
4075 break; // Shifted all sign bits out.
4076 Tmp2 = ShAmt->getZExtValue();
4077 return Tmp - Tmp2;
4078 }
4079 break;
4080 }
4081 case Instruction::And:
4082 case Instruction::Or:
4083 case Instruction::Xor: // NOT is handled here.
4084 // Logical binary ops preserve the number of sign bits at the worst.
4085 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4086 if (Tmp != 1) {
4087 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4088 FirstAnswer = std::min(Tmp, Tmp2);
4089 // We computed what we know about the sign bits as our first
4090 // answer. Now proceed to the generic code that uses
4091 // computeKnownBits, and pick whichever answer is better.
4092 }
4093 break;
4094
4095 case Instruction::Select: {
4096 // If we have a clamp pattern, we know that the number of sign bits will
4097 // be the minimum of the clamp min/max range.
4098 const Value *X;
4099 const APInt *CLow, *CHigh;
4100 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4101 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4102
4103 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4104 if (Tmp == 1)
4105 break;
4106 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Depth + 1, Q);
4107 return std::min(Tmp, Tmp2);
4108 }
4109
4110 case Instruction::Add:
4111 // Add can have at most one carry bit. Thus we know that the output
4112 // is, at worst, one more bit than the inputs.
4113 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
4114 if (Tmp == 1) break;
4115
4116 // Special case decrementing a value (ADD X, -1):
4117 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4118 if (CRHS->isAllOnesValue()) {
4119 KnownBits Known(TyBits);
4120 computeKnownBits(U->getOperand(0), DemandedElts, Known, Depth + 1, Q);
4121
4122 // If the input is known to be 0 or 1, the output is 0/-1, which is
4123 // all sign bits set.
4124 if ((Known.Zero | 1).isAllOnes())
4125 return TyBits;
4126
4127 // If we are subtracting one from a positive number, there is no carry
4128 // out of the result.
4129 if (Known.isNonNegative())
4130 return Tmp;
4131 }
4132
4133 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4134 if (Tmp2 == 1)
4135 break;
4136 return std::min(Tmp, Tmp2) - 1;
4137
4138 case Instruction::Sub:
4139 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4140 if (Tmp2 == 1)
4141 break;
4142
4143 // Handle NEG.
4144 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4145 if (CLHS->isNullValue()) {
4146 KnownBits Known(TyBits);
4147 computeKnownBits(U->getOperand(1), DemandedElts, Known, Depth + 1, Q);
4148 // If the input is known to be 0 or 1, the output is 0/-1, which is
4149 // all sign bits set.
4150 if ((Known.Zero | 1).isAllOnes())
4151 return TyBits;
4152
4153 // If the input is known to be positive (the sign bit is known clear),
4154 // the output of the NEG has the same number of sign bits as the
4155 // input.
4156 if (Known.isNonNegative())
4157 return Tmp2;
4158
4159 // Otherwise, we treat this like a SUB.
4160 }
4161
4162 // Sub can have at most one carry bit. Thus we know that the output
4163 // is, at worst, one more bit than the inputs.
4164 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4165 if (Tmp == 1)
4166 break;
4167 return std::min(Tmp, Tmp2) - 1;
4168
4169 case Instruction::Mul: {
4170 // The output of the Mul can be at most twice the valid bits in the
4171 // inputs.
4172 unsigned SignBitsOp0 =
4173 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4174 if (SignBitsOp0 == 1)
4175 break;
4176 unsigned SignBitsOp1 =
4177 ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4178 if (SignBitsOp1 == 1)
4179 break;
4180 unsigned OutValidBits =
4181 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4182 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4183 }
4184
4185 case Instruction::PHI: {
4186 const PHINode *PN = cast<PHINode>(U);
4187 unsigned NumIncomingValues = PN->getNumIncomingValues();
4188 // Don't analyze large in-degree PHIs.
4189 if (NumIncomingValues > 4) break;
4190 // Unreachable blocks may have zero-operand PHI nodes.
4191 if (NumIncomingValues == 0) break;
4192
4193 // Take the minimum of all incoming values. This can't infinitely loop
4194 // because of our depth threshold.
4196 Tmp = TyBits;
4197 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4198 if (Tmp == 1) return Tmp;
4199 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4200 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4201 DemandedElts, Depth + 1, RecQ));
4202 }
4203 return Tmp;
4204 }
4205
4206 case Instruction::Trunc: {
4207 // If the input contained enough sign bits that some remain after the
4208 // truncation, then we can make use of that. Otherwise we don't know
4209 // anything.
4210 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
4211 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4212 if (Tmp > (OperandTyBits - TyBits))
4213 return Tmp - (OperandTyBits - TyBits);
4214
4215 return 1;
4216 }
4217
4218 case Instruction::ExtractElement:
4219 // Look through extract element. At the moment we keep this simple and
4220 // skip tracking the specific element. But at least we might find
4221 // information valid for all elements of the vector (for example if vector
4222 // is sign extended, shifted, etc).
4223 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
4224
4225 case Instruction::ShuffleVector: {
4226 // Collect the minimum number of sign bits that are shared by every vector
4227 // element referenced by the shuffle.
4228 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4229 if (!Shuf) {
4230 // FIXME: Add support for shufflevector constant expressions.
4231 return 1;
4232 }
4233 APInt DemandedLHS, DemandedRHS;
4234 // For undef elements, we don't know anything about the common state of
4235 // the shuffle result.
4236 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4237 return 1;
4238 Tmp = std::numeric_limits<unsigned>::max();
4239 if (!!DemandedLHS) {
4240 const Value *LHS = Shuf->getOperand(0);
4241 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q);
4242 }
4243 // If we don't know anything, early out and try computeKnownBits
4244 // fall-back.
4245 if (Tmp == 1)
4246 break;
4247 if (!!DemandedRHS) {
4248 const Value *RHS = Shuf->getOperand(1);
4249 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q);
4250 Tmp = std::min(Tmp, Tmp2);
4251 }
4252 // If we don't know anything, early out and try computeKnownBits
4253 // fall-back.
4254 if (Tmp == 1)
4255 break;
4256 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4257 return Tmp;
4258 }
4259 case Instruction::Call: {
4260 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4261 switch (II->getIntrinsicID()) {
4262 default:
4263 break;
4264 case Intrinsic::abs:
4265 Tmp =
4266 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4267 if (Tmp == 1)
4268 break;
4269
4270 // Absolute value reduces number of sign bits by at most 1.
4271 return Tmp - 1;
4272 case Intrinsic::smin:
4273 case Intrinsic::smax: {
4274 const APInt *CLow, *CHigh;
4275 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4276 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4277 }
4278 }
4279 }
4280 }
4281 }
4282 }
4283
4284 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4285 // use this information.
4286
4287 // If we can examine all elements of a vector constant successfully, we're
4288 // done (we can't do any better than that). If not, keep trying.
4289 if (unsigned VecSignBits =
4290 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4291 return VecSignBits;
4292
4293 KnownBits Known(TyBits);
4294 computeKnownBits(V, DemandedElts, Known, Depth, Q);
4295
4296 // If we know that the sign bit is either zero or one, determine the number of
4297 // identical bits in the top of the input value.
4298 return std::max(FirstAnswer, Known.countMinSignBits());
4299}
4300
4302 const TargetLibraryInfo *TLI) {
4303 const Function *F = CB.getCalledFunction();
4304 if (!F)
4306
4307 if (F->isIntrinsic())
4308 return F->getIntrinsicID();
4309
4310 // We are going to infer semantics of a library function based on mapping it
4311 // to an LLVM intrinsic. Check that the library function is available from
4312 // this callbase and in this environment.
4313 LibFunc Func;
4314 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4315 !CB.onlyReadsMemory())
4317
4318 switch (Func) {
4319 default:
4320 break;
4321 case LibFunc_sin:
4322 case LibFunc_sinf:
4323 case LibFunc_sinl:
4324 return Intrinsic::sin;
4325 case LibFunc_cos:
4326 case LibFunc_cosf:
4327 case LibFunc_cosl:
4328 return Intrinsic::cos;
4329 case LibFunc_tan:
4330 case LibFunc_tanf:
4331 case LibFunc_tanl:
4332 return Intrinsic::tan;
4333 case LibFunc_asin:
4334 case LibFunc_asinf:
4335 case LibFunc_asinl:
4336 return Intrinsic::asin;
4337 case LibFunc_acos:
4338 case LibFunc_acosf:
4339 case LibFunc_acosl:
4340 return Intrinsic::acos;
4341 case LibFunc_atan:
4342 case LibFunc_atanf:
4343 case LibFunc_atanl:
4344 return Intrinsic::atan;
4345 case LibFunc_atan2:
4346 case LibFunc_atan2f:
4347 case LibFunc_atan2l:
4348 return Intrinsic::atan2;
4349 case LibFunc_sinh:
4350 case LibFunc_sinhf:
4351 case LibFunc_sinhl:
4352 return Intrinsic::sinh;
4353 case LibFunc_cosh:
4354 case LibFunc_coshf:
4355 case LibFunc_coshl:
4356 return Intrinsic::cosh;
4357 case LibFunc_tanh:
4358 case LibFunc_tanhf:
4359 case LibFunc_tanhl:
4360 return Intrinsic::tanh;
4361 case LibFunc_exp:
4362 case LibFunc_expf:
4363 case LibFunc_expl:
4364 return Intrinsic::exp;
4365 case LibFunc_exp2:
4366 case LibFunc_exp2f:
4367 case LibFunc_exp2l:
4368 return Intrinsic::exp2;
4369 case LibFunc_exp10:
4370 case LibFunc_exp10f:
4371 case LibFunc_exp10l:
4372 return Intrinsic::exp10;
4373 case LibFunc_log:
4374 case LibFunc_logf:
4375 case LibFunc_logl:
4376 return Intrinsic::log;
4377 case LibFunc_log10:
4378 case LibFunc_log10f:
4379 case LibFunc_log10l:
4380 return Intrinsic::log10;
4381 case LibFunc_log2:
4382 case LibFunc_log2f:
4383 case LibFunc_log2l:
4384 return Intrinsic::log2;
4385 case LibFunc_fabs:
4386 case LibFunc_fabsf:
4387 case LibFunc_fabsl:
4388 return Intrinsic::fabs;
4389 case LibFunc_fmin:
4390 case LibFunc_fminf:
4391 case LibFunc_fminl:
4392 return Intrinsic::minnum;
4393 case LibFunc_fmax:
4394 case LibFunc_fmaxf:
4395 case LibFunc_fmaxl:
4396 return Intrinsic::maxnum;
4397 case LibFunc_copysign:
4398 case LibFunc_copysignf:
4399 case LibFunc_copysignl:
4400 return Intrinsic::copysign;
4401 case LibFunc_floor:
4402 case LibFunc_floorf:
4403 case LibFunc_floorl:
4404 return Intrinsic::floor;
4405 case LibFunc_ceil:
4406 case LibFunc_ceilf:
4407 case LibFunc_ceill:
4408 return Intrinsic::ceil;
4409 case LibFunc_trunc:
4410 case LibFunc_truncf:
4411 case LibFunc_truncl:
4412 return Intrinsic::trunc;
4413 case LibFunc_rint:
4414 case LibFunc_rintf:
4415 case LibFunc_rintl:
4416 return Intrinsic::rint;
4417 case LibFunc_nearbyint:
4418 case LibFunc_nearbyintf:
4419 case LibFunc_nearbyintl:
4420 return Intrinsic::nearbyint;
4421 case LibFunc_round:
4422 case LibFunc_roundf:
4423 case LibFunc_roundl:
4424 return Intrinsic::round;
4425 case LibFunc_roundeven:
4426 case LibFunc_roundevenf:
4427 case LibFunc_roundevenl:
4428 return Intrinsic::roundeven;
4429 case LibFunc_pow:
4430 case LibFunc_powf:
4431 case LibFunc_powl:
4432 return Intrinsic::pow;
4433 case LibFunc_sqrt:
4434 case LibFunc_sqrtf:
4435 case LibFunc_sqrtl:
4436 return Intrinsic::sqrt;
4437 }
4438
4440}
4441
4442/// Return true if it's possible to assume IEEE treatment of input denormals in
4443/// \p F for \p Val.
4444static bool inputDenormalIsIEEE(const Function &F, const Type *Ty) {
4445 Ty = Ty->getScalarType();
4446 return F.getDenormalMode(Ty->getFltSemantics()).Input == DenormalMode::IEEE;
4447}
4448
4449static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4450 Ty = Ty->getScalarType();
4451 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4452 return Mode.Input == DenormalMode::IEEE ||
4453 Mode.Input == DenormalMode::PositiveZero;
4454}
4455
4456static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4457 Ty = Ty->getScalarType();
4458 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4459 return Mode.Output == DenormalMode::IEEE ||
4460 Mode.Output == DenormalMode::PositiveZero;
4461}
4462
4464 return isKnownNeverZero() &&
4466}
4467
4469 Type *Ty) const {
4470 return isKnownNeverNegZero() &&
4472}
4473
4475 Type *Ty) const {
4476 if (!isKnownNeverPosZero())
4477 return false;
4478
4479 // If we know there are no denormals, nothing can be flushed to zero.
4481 return true;
4482
4483 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4484 switch (Mode.Input) {
4485 case DenormalMode::IEEE:
4486 return true;
4488 // Negative subnormal won't flush to +0
4489 return isKnownNeverPosSubnormal();
4491 default:
4492 // Both positive and negative subnormal could flush to +0
4493 return false;
4494 }
4495
4496 llvm_unreachable("covered switch over denormal mode");
4497}
4498
4500 Type *Ty) {
4501 KnownFPClasses = Src.KnownFPClasses;
4502 // If we aren't assuming the source can't be a zero, we don't have to check if
4503 // a denormal input could be flushed.
4504 if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero())
4505 return;
4506
4507 // If we know the input can't be a denormal, it can't be flushed to 0.
4508 if (Src.isKnownNeverSubnormal())
4509 return;
4510
4511 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4512
4513 if (!Src.isKnownNeverPosSubnormal() && Mode != DenormalMode::getIEEE())
4515
4516 if (!Src.isKnownNeverNegSubnormal() && Mode != DenormalMode::getIEEE()) {
4517 if (Mode != DenormalMode::getPositiveZero())
4519
4520 if (Mode.Input == DenormalMode::PositiveZero ||
4521 Mode.Output == DenormalMode::PositiveZero ||
4522 Mode.Input == DenormalMode::Dynamic ||
4523 Mode.Output == DenormalMode::Dynamic)
4525 }
4526}
4527
4529 const Function &F, Type *Ty) {
4530 propagateDenormal(Src, F, Ty);
4531 propagateNaN(Src, /*PreserveSign=*/true);
4532}
4533
4534/// Given an exploded icmp instruction, return true if the comparison only
4535/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4536/// the result of the comparison is true when the input value is signed.
4538 bool &TrueIfSigned) {
4539 switch (Pred) {
4540 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4541 TrueIfSigned = true;
4542 return RHS.isZero();
4543 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4544 TrueIfSigned = true;
4545 return RHS.isAllOnes();
4546 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4547 TrueIfSigned = false;
4548 return RHS.isAllOnes();
4549 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4550 TrueIfSigned = false;
4551 return RHS.isZero();
4552 case ICmpInst::ICMP_UGT:
4553 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4554 TrueIfSigned = true;
4555 return RHS.isMaxSignedValue();
4556 case ICmpInst::ICMP_UGE:
4557 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4558 TrueIfSigned = true;
4559 return RHS.isMinSignedValue();
4560 case ICmpInst::ICMP_ULT:
4561 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4562 TrueIfSigned = false;
4563 return RHS.isMinSignedValue();
4564 case ICmpInst::ICMP_ULE:
4565 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4566 TrueIfSigned = false;
4567 return RHS.isMaxSignedValue();
4568 default:
4569 return false;
4570 }
4571}
4572
4573/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
4574/// same result as an fcmp with the given operands.
4575std::pair<Value *, FPClassTest> llvm::fcmpToClassTest(FCmpInst::Predicate Pred,
4576 const Function &F,
4577 Value *LHS, Value *RHS,
4578 bool LookThroughSrc) {
4579 const APFloat *ConstRHS;
4580 if (!match(RHS, m_APFloatAllowPoison(ConstRHS)))
4581 return {nullptr, fcAllFlags};
4582
4583 return fcmpToClassTest(Pred, F, LHS, ConstRHS, LookThroughSrc);
4584}
4585
4586std::pair<Value *, FPClassTest>
4588 const APFloat *ConstRHS, bool LookThroughSrc) {
4589
4590 auto [Src, ClassIfTrue, ClassIfFalse] =
4591 fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4592 if (Src && ClassIfTrue == ~ClassIfFalse)
4593 return {Src, ClassIfTrue};
4594 return {nullptr, fcAllFlags};
4595}
4596
4597/// Return the return value for fcmpImpliesClass for a compare that produces an
4598/// exact class test.
4599static std::tuple<Value *, FPClassTest, FPClassTest> exactClass(Value *V,
4600 FPClassTest M) {
4601 return {V, M, ~M};
4602}
4603
4604std::tuple<Value *, FPClassTest, FPClassTest>
4606 FPClassTest RHSClass, bool LookThroughSrc) {
4607 assert(RHSClass != fcNone);
4608 Value *Src = LHS;
4609
4610 if (Pred == FCmpInst::FCMP_TRUE)
4611 return exactClass(Src, fcAllFlags);
4612
4613 if (Pred == FCmpInst::FCMP_FALSE)
4614 return exactClass(Src, fcNone);
4615
4616 const FPClassTest OrigClass = RHSClass;
4617
4618 const bool IsNegativeRHS = (RHSClass & fcNegative) == RHSClass;
4619 const bool IsPositiveRHS = (RHSClass & fcPositive) == RHSClass;
4620 const bool IsNaN = (RHSClass & ~fcNan) == fcNone;
4621
4622 if (IsNaN) {
4623 // fcmp o__ x, nan -> false
4624 // fcmp u__ x, nan -> true
4625 return exactClass(Src, CmpInst::isOrdered(Pred) ? fcNone : fcAllFlags);
4626 }
4627
4628 // fcmp ord x, zero|normal|subnormal|inf -> ~fcNan
4629 if (Pred == FCmpInst::FCMP_ORD)
4630 return exactClass(Src, ~fcNan);
4631
4632 // fcmp uno x, zero|normal|subnormal|inf -> fcNan
4633 if (Pred == FCmpInst::FCMP_UNO)
4634 return exactClass(Src, fcNan);
4635
4636 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4637 if (IsFabs)
4638 RHSClass = llvm::inverse_fabs(RHSClass);
4639
4640 const bool IsZero = (OrigClass & fcZero) == OrigClass;
4641 if (IsZero) {
4642 assert(Pred != FCmpInst::FCMP_ORD && Pred != FCmpInst::FCMP_UNO);
4643 // Compares with fcNone are only exactly equal to fcZero if input denormals
4644 // are not flushed.
4645 // TODO: Handle DAZ by expanding masks to cover subnormal cases.
4646 if (!inputDenormalIsIEEE(F, LHS->getType()))
4647 return {nullptr, fcAllFlags, fcAllFlags};
4648
4649 switch (Pred) {
4650 case FCmpInst::FCMP_OEQ: // Match x == 0.0
4651 return exactClass(Src, fcZero);
4652 case FCmpInst::FCMP_UEQ: // Match isnan(x) || (x == 0.0)
4653 return exactClass(Src, fcZero | fcNan);
4654 case FCmpInst::FCMP_UNE: // Match (x != 0.0)
4655 return exactClass(Src, ~fcZero);
4656 case FCmpInst::FCMP_ONE: // Match !isnan(x) && x != 0.0
4657 return exactClass(Src, ~fcNan & ~fcZero);
4658 case FCmpInst::FCMP_ORD:
4659 // Canonical form of ord/uno is with a zero. We could also handle
4660 // non-canonical other non-NaN constants or LHS == RHS.
4661 return exactClass(Src, ~fcNan);
4662 case FCmpInst::FCMP_UNO:
4663 return exactClass(Src, fcNan);
4664 case FCmpInst::FCMP_OGT: // x > 0
4666 case FCmpInst::FCMP_UGT: // isnan(x) || x > 0
4668 case FCmpInst::FCMP_OGE: // x >= 0
4669 return exactClass(Src, fcPositive | fcNegZero);
4670 case FCmpInst::FCMP_UGE: // isnan(x) || x >= 0
4671 return exactClass(Src, fcPositive | fcNegZero | fcNan);
4672 case FCmpInst::FCMP_OLT: // x < 0
4674 case FCmpInst::FCMP_ULT: // isnan(x) || x < 0
4676 case FCmpInst::FCMP_OLE: // x <= 0
4677 return exactClass(Src, fcNegative | fcPosZero);
4678 case FCmpInst::FCMP_ULE: // isnan(x) || x <= 0
4679 return exactClass(Src, fcNegative | fcPosZero | fcNan);
4680 default:
4681 llvm_unreachable("all compare types are handled");
4682 }
4683
4684 return {nullptr, fcAllFlags, fcAllFlags};
4685 }
4686
4687 const bool IsDenormalRHS = (OrigClass & fcSubnormal) == OrigClass;
4688
4689 const bool IsInf = (OrigClass & fcInf) == OrigClass;
4690 if (IsInf) {
4691 FPClassTest Mask = fcAllFlags;
4692
4693 switch (Pred) {
4694 case FCmpInst::FCMP_OEQ:
4695 case FCmpInst::FCMP_UNE: {
4696 // Match __builtin_isinf patterns
4697 //
4698 // fcmp oeq x, +inf -> is_fpclass x, fcPosInf
4699 // fcmp oeq fabs(x), +inf -> is_fpclass x, fcInf
4700 // fcmp oeq x, -inf -> is_fpclass x, fcNegInf
4701 // fcmp oeq fabs(x), -inf -> is_fpclass x, 0 -> false
4702 //
4703 // fcmp une x, +inf -> is_fpclass x, ~fcPosInf
4704 // fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf
4705 // fcmp une x, -inf -> is_fpclass x, ~fcNegInf
4706 // fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true
4707 if (IsNegativeRHS) {
4708 Mask = fcNegInf;
4709 if (IsFabs)
4710 Mask = fcNone;
4711 } else {
4712 Mask = fcPosInf;
4713 if (IsFabs)
4714 Mask |= fcNegInf;
4715 }
4716 break;
4717 }
4718 case FCmpInst::FCMP_ONE:
4719 case FCmpInst::FCMP_UEQ: {
4720 // Match __builtin_isinf patterns
4721 // fcmp one x, -inf -> is_fpclass x, fcNegInf
4722 // fcmp one fabs(x), -inf -> is_fpclass x, ~fcNegInf & ~fcNan
4723 // fcmp one x, +inf -> is_fpclass x, ~fcNegInf & ~fcNan
4724 // fcmp one fabs(x), +inf -> is_fpclass x, ~fcInf & fcNan
4725 //
4726 // fcmp ueq x, +inf -> is_fpclass x, fcPosInf|fcNan
4727 // fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan
4728 // fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan
4729 // fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan
4730 if (IsNegativeRHS) {
4731 Mask = ~fcNegInf & ~fcNan;
4732 if (IsFabs)
4733 Mask = ~fcNan;
4734 } else {
4735 Mask = ~fcPosInf & ~fcNan;
4736 if (IsFabs)
4737 Mask &= ~fcNegInf;
4738 }
4739
4740 break;
4741 }
4742 case FCmpInst::FCMP_OLT:
4743 case FCmpInst::FCMP_UGE: {
4744 if (IsNegativeRHS) {
4745 // No value is ordered and less than negative infinity.
4746 // All values are unordered with or at least negative infinity.
4747 // fcmp olt x, -inf -> false
4748 // fcmp uge x, -inf -> true
4749 Mask = fcNone;
4750 break;
4751 }
4752
4753 // fcmp olt fabs(x), +inf -> fcFinite
4754 // fcmp uge fabs(x), +inf -> ~fcFinite
4755 // fcmp olt x, +inf -> fcFinite|fcNegInf
4756 // fcmp uge x, +inf -> ~(fcFinite|fcNegInf)
4757 Mask = fcFinite;
4758 if (!IsFabs)
4759 Mask |= fcNegInf;
4760 break;
4761 }
4762 case FCmpInst::FCMP_OGE:
4763 case FCmpInst::FCMP_ULT: {
4764 if (IsNegativeRHS) {
4765 // fcmp oge x, -inf -> ~fcNan
4766 // fcmp oge fabs(x), -inf -> ~fcNan
4767 // fcmp ult x, -inf -> fcNan
4768 // fcmp ult fabs(x), -inf -> fcNan
4769 Mask = ~fcNan;
4770 break;
4771 }
4772
4773 // fcmp oge fabs(x), +inf -> fcInf
4774 // fcmp oge x, +inf -> fcPosInf
4775 // fcmp ult fabs(x), +inf -> ~fcInf
4776 // fcmp ult x, +inf -> ~fcPosInf
4777 Mask = fcPosInf;
4778 if (IsFabs)
4779 Mask |= fcNegInf;
4780 break;
4781 }
4782 case FCmpInst::FCMP_OGT:
4783 case FCmpInst::FCMP_ULE: {
4784 if (IsNegativeRHS) {
4785 // fcmp ogt x, -inf -> fcmp one x, -inf
4786 // fcmp ogt fabs(x), -inf -> fcmp ord x, x
4787 // fcmp ule x, -inf -> fcmp ueq x, -inf
4788 // fcmp ule fabs(x), -inf -> fcmp uno x, x
4789 Mask = IsFabs ? ~fcNan : ~(fcNegInf | fcNan);
4790 break;
4791 }
4792
4793 // No value is ordered and greater than infinity.
4794 Mask = fcNone;
4795 break;
4796 }
4797 case FCmpInst::FCMP_OLE:
4798 case FCmpInst::FCMP_UGT: {
4799 if (IsNegativeRHS) {
4800 Mask = IsFabs ? fcNone : fcNegInf;
4801 break;
4802 }
4803
4804 // fcmp ole x, +inf -> fcmp ord x, x
4805 // fcmp ole fabs(x), +inf -> fcmp ord x, x
4806 // fcmp ole x, -inf -> fcmp oeq x, -inf
4807 // fcmp ole fabs(x), -inf -> false
4808 Mask = ~fcNan;
4809 break;
4810 }
4811 default:
4812 llvm_unreachable("all compare types are handled");
4813 }
4814
4815 // Invert the comparison for the unordered cases.
4816 if (FCmpInst::isUnordered(Pred))
4817 Mask = ~Mask;
4818
4819 return exactClass(Src, Mask);
4820 }
4821
4822 if (Pred == FCmpInst::FCMP_OEQ)
4823 return {Src, RHSClass, fcAllFlags};
4824
4825 if (Pred == FCmpInst::FCMP_UEQ) {
4826 FPClassTest Class = RHSClass | fcNan;
4827 return {Src, Class, ~fcNan};
4828 }
4829
4830 if (Pred == FCmpInst::FCMP_ONE)
4831 return {Src, ~fcNan, RHSClass | fcNan};
4832
4833 if (Pred == FCmpInst::FCMP_UNE)
4834 return {Src, fcAllFlags, RHSClass};
4835
4836 assert((RHSClass == fcNone || RHSClass == fcPosNormal ||
4837 RHSClass == fcNegNormal || RHSClass == fcNormal ||
4838 RHSClass == fcPosSubnormal || RHSClass == fcNegSubnormal ||
4839 RHSClass == fcSubnormal) &&
4840 "should have been recognized as an exact class test");
4841
4842 if (IsNegativeRHS) {
4843 // TODO: Handle fneg(fabs)
4844 if (IsFabs) {
4845 // fabs(x) o> -k -> fcmp ord x, x
4846 // fabs(x) u> -k -> true
4847 // fabs(x) o< -k -> false
4848 // fabs(x) u< -k -> fcmp uno x, x
4849 switch (Pred) {
4850 case FCmpInst::FCMP_OGT:
4851 case FCmpInst::FCMP_OGE:
4852 return {Src, ~fcNan, fcNan};
4853 case FCmpInst::FCMP_UGT:
4854 case FCmpInst::FCMP_UGE:
4855 return {Src, fcAllFlags, fcNone};
4856 case FCmpInst::FCMP_OLT:
4857 case FCmpInst::FCMP_OLE:
4858 return {Src, fcNone, fcAllFlags};
4859 case FCmpInst::FCMP_ULT:
4860 case FCmpInst::FCMP_ULE:
4861 return {Src, fcNan, ~fcNan};
4862 default:
4863 break;
4864 }
4865
4866 return {nullptr, fcAllFlags, fcAllFlags};
4867 }
4868
4869 FPClassTest ClassesLE = fcNegInf | fcNegNormal;
4871
4872 if (IsDenormalRHS)
4873 ClassesLE |= fcNegSubnormal;
4874 else
4875 ClassesGE |= fcNegNormal;
4876
4877 switch (Pred) {
4878 case FCmpInst::FCMP_OGT:
4879 case FCmpInst::FCMP_OGE:
4880 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4881 case FCmpInst::FCMP_UGT:
4882 case FCmpInst::FCMP_UGE:
4883 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4884 case FCmpInst::FCMP_OLT:
4885 case FCmpInst::FCMP_OLE:
4886 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4887 case FCmpInst::FCMP_ULT:
4888 case FCmpInst::FCMP_ULE:
4889 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4890 default:
4891 break;
4892 }
4893 } else if (IsPositiveRHS) {
4894 FPClassTest ClassesGE = fcPosNormal | fcPosInf;
4896 if (IsDenormalRHS)
4897 ClassesGE |= fcPosSubnormal;
4898 else
4899 ClassesLE |= fcPosNormal;
4900
4901 if (IsFabs) {
4902 ClassesGE = llvm::inverse_fabs(ClassesGE);
4903 ClassesLE = llvm::inverse_fabs(ClassesLE);
4904 }
4905
4906 switch (Pred) {
4907 case FCmpInst::FCMP_OGT:
4908 case FCmpInst::FCMP_OGE:
4909 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4910 case FCmpInst::FCMP_UGT:
4911 case FCmpInst::FCMP_UGE:
4912 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4913 case FCmpInst::FCMP_OLT:
4914 case FCmpInst::FCMP_OLE:
4915 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4916 case FCmpInst::FCMP_ULT:
4917 case FCmpInst::FCMP_ULE:
4918 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4919 default:
4920 break;
4921 }
4922 }
4923
4924 return {nullptr, fcAllFlags, fcAllFlags};
4925}
4926
4927std::tuple<Value *, FPClassTest, FPClassTest>
4929 const APFloat &ConstRHS, bool LookThroughSrc) {
4930 // We can refine checks against smallest normal / largest denormal to an
4931 // exact class test.
4932 if (!ConstRHS.isNegative() && ConstRHS.isSmallestNormalized()) {
4933 Value *Src = LHS;
4934 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4935
4936 FPClassTest Mask;
4937 // Match pattern that's used in __builtin_isnormal.
4938 switch (Pred) {
4939 case FCmpInst::FCMP_OLT:
4940 case FCmpInst::FCMP_UGE: {
4941 // fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero
4942 // fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero
4943 // fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf
4944 // fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero)
4945 Mask = fcZero | fcSubnormal;
4946 if (!IsFabs)
4947 Mask |= fcNegNormal | fcNegInf;
4948
4949 break;
4950 }
4951 case FCmpInst::FCMP_OGE:
4952 case FCmpInst::FCMP_ULT: {
4953 // fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf
4954 // fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal
4955 // fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf)
4956 // fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal)
4957 Mask = fcPosInf | fcPosNormal;
4958 if (IsFabs)
4959 Mask |= fcNegInf | fcNegNormal;
4960 break;
4961 }
4962 default:
4963 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(),
4964 LookThroughSrc);
4965 }
4966
4967 // Invert the comparison for the unordered cases.
4968 if (FCmpInst::isUnordered(Pred))
4969 Mask = ~Mask;
4970
4971 return exactClass(Src, Mask);
4972 }
4973
4974 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(), LookThroughSrc);
4975}
4976
4977std::tuple<Value *, FPClassTest, FPClassTest>
4979 Value *RHS, bool LookThroughSrc) {
4980 const APFloat *ConstRHS;
4981 if (!match(RHS, m_APFloatAllowPoison(ConstRHS)))
4982 return {nullptr, fcAllFlags, fcAllFlags};
4983
4984 // TODO: Just call computeKnownFPClass for RHS to handle non-constants.
4985 return fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4986}
4987
4989 unsigned Depth, bool CondIsTrue,
4990 const Instruction *CxtI,
4991 KnownFPClass &KnownFromContext) {
4992 Value *A, *B;
4994 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4995 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4996 computeKnownFPClassFromCond(V, A, Depth + 1, CondIsTrue, CxtI,
4997 KnownFromContext);
4998 computeKnownFPClassFromCond(V, B, Depth + 1, CondIsTrue, CxtI,
4999 KnownFromContext);
5000 return;
5001 }
5003 computeKnownFPClassFromCond(V, A, Depth + 1, !CondIsTrue, CxtI,
5004 KnownFromContext);
5005 return;
5006 }
5007 CmpPredicate Pred;
5008 Value *LHS;
5009 uint64_t ClassVal = 0;
5010 const APFloat *CRHS;
5011 const APInt *RHS;
5012 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
5013 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
5014 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
5015 if (CmpVal == V)
5016 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
5017 } else if (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(
5018 m_Specific(V), m_ConstantInt(ClassVal)))) {
5019 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
5020 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
5021 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
5022 m_APInt(RHS)))) {
5023 bool TrueIfSigned;
5024 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
5025 return;
5026 if (TrueIfSigned == CondIsTrue)
5027 KnownFromContext.signBitMustBeOne();
5028 else
5029 KnownFromContext.signBitMustBeZero();
5030 }
5031}
5032
5034 const SimplifyQuery &Q) {
5035 KnownFPClass KnownFromContext;
5036
5037 if (!Q.CxtI)
5038 return KnownFromContext;
5039
5040 if (Q.DC && Q.DT) {
5041 // Handle dominating conditions.
5042 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
5043 Value *Cond = BI->getCondition();
5044
5045 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
5046 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
5047 computeKnownFPClassFromCond(V, Cond, /*Depth=*/0, /*CondIsTrue=*/true,
5048 Q.CxtI, KnownFromContext);
5049
5050 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
5051 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
5052 computeKnownFPClassFromCond(V, Cond, /*Depth=*/0, /*CondIsTrue=*/false,
5053 Q.CxtI, KnownFromContext);
5054 }
5055 }
5056
5057 if (!Q.AC)
5058 return KnownFromContext;
5059
5060 // Try to restrict the floating-point classes based on information from
5061 // assumptions.
5062 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
5063 if (!AssumeVH)
5064 continue;
5065 CallInst *I = cast<CallInst>(AssumeVH);
5066
5067 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
5068 "Got assumption for the wrong function!");
5069 assert(I->getIntrinsicID() == Intrinsic::assume &&
5070 "must be an assume intrinsic");
5071
5072 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
5073 continue;
5074
5075 computeKnownFPClassFromCond(V, I->getArgOperand(0), /*Depth=*/0,
5076 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
5077 }
5078
5079 return KnownFromContext;
5080}
5081
5082void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5083 FPClassTest InterestedClasses, KnownFPClass &Known,
5084 unsigned Depth, const SimplifyQuery &Q);
5085
5086static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
5087 FPClassTest InterestedClasses, unsigned Depth,
5088 const SimplifyQuery &Q) {
5089 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5090 APInt DemandedElts =
5091 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5092 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Depth, Q);
5093}
5094
5096 const APInt &DemandedElts,
5097 FPClassTest InterestedClasses,
5098 KnownFPClass &Known, unsigned Depth,
5099 const SimplifyQuery &Q) {
5100 if ((InterestedClasses &
5102 return;
5103
5104 KnownFPClass KnownSrc;
5105 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5106 KnownSrc, Depth + 1, Q);
5107
5108 // Sign should be preserved
5109 // TODO: Handle cannot be ordered greater than zero
5110 if (KnownSrc.cannotBeOrderedLessThanZero())
5112
5113 Known.propagateNaN(KnownSrc, true);
5114
5115 // Infinity needs a range check.
5116}
5117
5118void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5119 FPClassTest InterestedClasses, KnownFPClass &Known,
5120 unsigned Depth, const SimplifyQuery &Q) {
5121 assert(Known.isUnknown() && "should not be called with known information");
5122
5123 if (!DemandedElts) {
5124 // No demanded elts, better to assume we don't know anything.
5125 Known.resetAll();
5126 return;
5127 }
5128
5129 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
5130
5131 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5132 Known.KnownFPClasses = CFP->getValueAPF().classify();
5133 Known.SignBit = CFP->isNegative();
5134 return;
5135 }
5136
5137 if (isa<ConstantAggregateZero>(V)) {
5138 Known.KnownFPClasses = fcPosZero;
5139 Known.SignBit = false;
5140 return;
5141 }
5142
5143 if (isa<PoisonValue>(V)) {
5144 Known.KnownFPClasses = fcNone;
5145 Known.SignBit = false;
5146 return;
5147 }
5148
5149 // Try to handle fixed width vector constants
5150 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5151 const Constant *CV = dyn_cast<Constant>(V);
5152 if (VFVTy && CV) {
5153 Known.KnownFPClasses = fcNone;
5154 bool SignBitAllZero = true;
5155 bool SignBitAllOne = true;
5156
5157 // For vectors, verify that each element is not NaN.
5158 unsigned NumElts = VFVTy->getNumElements();
5159 for (unsigned i = 0; i != NumElts; ++i) {
5160 if (!DemandedElts[i])
5161 continue;
5162
5163 Constant *Elt = CV->getAggregateElement(i);
5164 if (!Elt) {
5165 Known = KnownFPClass();
5166 return;
5167 }
5168 if (isa<PoisonValue>(Elt))
5169 continue;
5170 auto *CElt = dyn_cast<ConstantFP>(Elt);
5171 if (!CElt) {
5172 Known = KnownFPClass();
5173 return;
5174 }
5175
5176 const APFloat &C = CElt->getValueAPF();
5177 Known.KnownFPClasses |= C.classify();
5178 if (C.isNegative())
5179 SignBitAllZero = false;
5180 else
5181 SignBitAllOne = false;
5182 }
5183 if (SignBitAllOne != SignBitAllZero)
5184 Known.SignBit = SignBitAllOne;
5185 return;
5186 }
5187
5188 FPClassTest KnownNotFromFlags = fcNone;
5189 if (const auto *CB = dyn_cast<CallBase>(V))
5190 KnownNotFromFlags |= CB->getRetNoFPClass();
5191 else if (const auto *Arg = dyn_cast<Argument>(V))
5192 KnownNotFromFlags |= Arg->getNoFPClass();
5193
5194 const Operator *Op = dyn_cast<Operator>(V);
5195 if (const FPMathOperator *FPOp = dyn_cast_or_null<FPMathOperator>(Op)) {
5196 if (FPOp->hasNoNaNs())
5197 KnownNotFromFlags |= fcNan;
5198 if (FPOp->hasNoInfs())
5199 KnownNotFromFlags |= fcInf;
5200 }
5201
5202 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5203 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5204
5205 // We no longer need to find out about these bits from inputs if we can
5206 // assume this from flags/attributes.
5207 InterestedClasses &= ~KnownNotFromFlags;
5208
5209 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
5210 Known.knownNot(KnownNotFromFlags);
5211 if (!Known.SignBit && AssumedClasses.SignBit) {
5212 if (*AssumedClasses.SignBit)
5213 Known.signBitMustBeOne();
5214 else
5215 Known.signBitMustBeZero();
5216 }
5217 });
5218
5219 if (!Op)
5220 return;
5221
5222 // All recursive calls that increase depth must come after this.
5224 return;
5225
5226 const unsigned Opc = Op->getOpcode();
5227 switch (Opc) {
5228 case Instruction::FNeg: {
5229 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5230 Known, Depth + 1, Q);
5231 Known.fneg();
5232 break;
5233 }
5234 case Instruction::Select: {
5235 Value *Cond = Op->getOperand(0);
5236 Value *LHS = Op->getOperand(1);
5237 Value *RHS = Op->getOperand(2);
5238
5239 FPClassTest FilterLHS = fcAllFlags;
5240 FPClassTest FilterRHS = fcAllFlags;
5241
5242 Value *TestedValue = nullptr;
5243 FPClassTest MaskIfTrue = fcAllFlags;
5244 FPClassTest MaskIfFalse = fcAllFlags;
5245 uint64_t ClassVal = 0;
5246 const Function *F = cast<Instruction>(Op)->getFunction();
5247 CmpPredicate Pred;
5248 Value *CmpLHS, *CmpRHS;
5249 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
5250 // If the select filters out a value based on the class, it no longer
5251 // participates in the class of the result
5252
5253 // TODO: In some degenerate cases we can infer something if we try again
5254 // without looking through sign operations.
5255 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
5256 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
5257 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
5258 } else if (match(Cond,
5259 m_Intrinsic<Intrinsic::is_fpclass>(
5260 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
5261 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
5262 MaskIfTrue = TestedMask;
5263 MaskIfFalse = ~TestedMask;
5264 }
5265
5266 if (TestedValue == LHS) {
5267 // match !isnan(x) ? x : y
5268 FilterLHS = MaskIfTrue;
5269 } else if (TestedValue == RHS) { // && IsExactClass
5270 // match !isnan(x) ? y : x
5271 FilterRHS = MaskIfFalse;
5272 }
5273
5274 KnownFPClass Known2;
5275 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
5276 Depth + 1, Q);
5277 Known.KnownFPClasses &= FilterLHS;
5278
5279 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
5280 Known2, Depth + 1, Q);
5281 Known2.KnownFPClasses &= FilterRHS;
5282
5283 Known |= Known2;
5284 break;
5285 }
5286 case Instruction::Call: {
5287 const CallInst *II = cast<CallInst>(Op);
5288 const Intrinsic::ID IID = II->getIntrinsicID();
5289 switch (IID) {
5290 case Intrinsic::fabs: {
5291 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5292 // If we only care about the sign bit we don't need to inspect the
5293 // operand.
5294 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5295 InterestedClasses, Known, Depth + 1, Q);
5296 }
5297
5298 Known.fabs();
5299 break;
5300 }
5301 case Intrinsic::copysign: {
5302 KnownFPClass KnownSign;
5303
5304 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5305 Known, Depth + 1, Q);
5306 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5307 KnownSign, Depth + 1, Q);
5308 Known.copysign(KnownSign);
5309 break;
5310 }
5311 case Intrinsic::fma:
5312 case Intrinsic::fmuladd: {
5313 if ((InterestedClasses & fcNegative) == fcNone)
5314 break;
5315
5316 if (II->getArgOperand(0) != II->getArgOperand(1))
5317 break;
5318
5319 // The multiply cannot be -0 and therefore the add can't be -0
5320 Known.knownNot(fcNegZero);
5321
5322 // x * x + y is non-negative if y is non-negative.
5323 KnownFPClass KnownAddend;
5324 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
5325 KnownAddend, Depth + 1, Q);
5326
5327 if (KnownAddend.cannotBeOrderedLessThanZero())
5328 Known.knownNot(fcNegative);
5329 break;
5330 }
5331 case Intrinsic::sqrt:
5332 case Intrinsic::experimental_constrained_sqrt: {
5333 KnownFPClass KnownSrc;
5334 FPClassTest InterestedSrcs = InterestedClasses;
5335 if (InterestedClasses & fcNan)
5336 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5337
5338 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5339 KnownSrc, Depth + 1, Q);
5340
5341 if (KnownSrc.isKnownNeverPosInfinity())
5342 Known.knownNot(fcPosInf);
5343 if (KnownSrc.isKnownNever(fcSNan))
5344 Known.knownNot(fcSNan);
5345
5346 // Any negative value besides -0 returns a nan.
5347 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5348 Known.knownNot(fcNan);
5349
5350 // The only negative value that can be returned is -0 for -0 inputs.
5352
5353 // If the input denormal mode could be PreserveSign, a negative
5354 // subnormal input could produce a negative zero output.
5355 const Function *F = II->getFunction();
5356 if (Q.IIQ.hasNoSignedZeros(II) ||
5357 (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType())))
5358 Known.knownNot(fcNegZero);
5359
5360 break;
5361 }
5362 case Intrinsic::sin:
5363 case Intrinsic::cos: {
5364 // Return NaN on infinite inputs.
5365 KnownFPClass KnownSrc;
5366 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5367 KnownSrc, Depth + 1, Q);
5368 Known.knownNot(fcInf);
5369 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
5370 Known.knownNot(fcNan);
5371 break;
5372 }
5373 case Intrinsic::maxnum:
5374 case Intrinsic::minnum:
5375 case Intrinsic::minimum:
5376 case Intrinsic::maximum: {
5377 KnownFPClass KnownLHS, KnownRHS;
5378 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5379 KnownLHS, Depth + 1, Q);
5380 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5381 KnownRHS, Depth + 1, Q);
5382
5383 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
5384 Known = KnownLHS | KnownRHS;
5385
5386 // If either operand is not NaN, the result is not NaN.
5387 if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum))
5388 Known.knownNot(fcNan);
5389
5390 if (IID == Intrinsic::maxnum) {
5391 // If at least one operand is known to be positive, the result must be
5392 // positive.
5393 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
5394 KnownLHS.isKnownNeverNaN()) ||
5395 (KnownRHS.cannotBeOrderedLessThanZero() &&
5396 KnownRHS.isKnownNeverNaN()))
5398 } else if (IID == Intrinsic::maximum) {
5399 // If at least one operand is known to be positive, the result must be
5400 // positive.
5401 if (KnownLHS.cannotBeOrderedLessThanZero() ||
5402 KnownRHS.cannotBeOrderedLessThanZero())
5404 } else if (IID == Intrinsic::minnum) {
5405 // If at least one operand is known to be negative, the result must be
5406 // negative.
5407 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
5408 KnownLHS.isKnownNeverNaN()) ||
5409 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
5410 KnownRHS.isKnownNeverNaN()))
5412 } else {
5413 // If at least one operand is known to be negative, the result must be
5414 // negative.
5415 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
5418 }
5419
5420 // Fixup zero handling if denormals could be returned as a zero.
5421 //
5422 // As there's no spec for denormal flushing, be conservative with the
5423 // treatment of denormals that could be flushed to zero. For older
5424 // subtargets on AMDGPU the min/max instructions would not flush the
5425 // output and return the original value.
5426 //
5427 if ((Known.KnownFPClasses & fcZero) != fcNone &&
5428 !Known.isKnownNeverSubnormal()) {
5429 const Function *Parent = II->getFunction();
5430 if (!Parent)
5431 break;
5432
5433 DenormalMode Mode = Parent->getDenormalMode(
5434 II->getType()->getScalarType()->getFltSemantics());
5435 if (Mode != DenormalMode::getIEEE())
5436 Known.KnownFPClasses |= fcZero;
5437 }
5438
5439 if (Known.isKnownNeverNaN()) {
5440 if (KnownLHS.SignBit && KnownRHS.SignBit &&
5441 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5442 if (*KnownLHS.SignBit)
5443 Known.signBitMustBeOne();
5444 else
5445 Known.signBitMustBeZero();
5446 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum) ||
5447 ((KnownLHS.isKnownNeverNegZero() ||
5448 KnownRHS.isKnownNeverPosZero()) &&
5449 (KnownLHS.isKnownNeverPosZero() ||
5450 KnownRHS.isKnownNeverNegZero()))) {
5451 if ((IID == Intrinsic::maximum || IID == Intrinsic::maxnum) &&
5452 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5453 Known.signBitMustBeZero();
5454 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minnum) &&
5455 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5456 Known.signBitMustBeOne();
5457 }
5458 }
5459 break;
5460 }
5461 case Intrinsic::canonicalize: {
5462 KnownFPClass KnownSrc;
5463 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5464 KnownSrc, Depth + 1, Q);
5465
5466 // This is essentially a stronger form of
5467 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5468 // actually have an IR canonicalization guarantee.
5469
5470 // Canonicalize may flush denormals to zero, so we have to consider the
5471 // denormal mode to preserve known-not-0 knowledge.
5472 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5473
5474 // Stronger version of propagateNaN
5475 // Canonicalize is guaranteed to quiet signaling nans.
5476 if (KnownSrc.isKnownNeverNaN())
5477 Known.knownNot(fcNan);
5478 else
5479 Known.knownNot(fcSNan);
5480
5481 const Function *F = II->getFunction();
5482 if (!F)
5483 break;
5484
5485 // If the parent function flushes denormals, the canonical output cannot
5486 // be a denormal.
5487 const fltSemantics &FPType =
5488 II->getType()->getScalarType()->getFltSemantics();
5489 DenormalMode DenormMode = F->getDenormalMode(FPType);
5490 if (DenormMode == DenormalMode::getIEEE()) {
5491 if (KnownSrc.isKnownNever(fcPosZero))
5492 Known.knownNot(fcPosZero);
5493 if (KnownSrc.isKnownNever(fcNegZero))
5494 Known.knownNot(fcNegZero);
5495 break;
5496 }
5497
5498 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5499 Known.knownNot(fcSubnormal);
5500
5501 if (DenormMode.Input == DenormalMode::PositiveZero ||
5502 (DenormMode.Output == DenormalMode::PositiveZero &&
5503 DenormMode.Input == DenormalMode::IEEE))
5504 Known.knownNot(fcNegZero);
5505
5506 break;
5507 }
5508 case Intrinsic::vector_reduce_fmax:
5509 case Intrinsic::vector_reduce_fmin:
5510 case Intrinsic::vector_reduce_fmaximum:
5511 case Intrinsic::vector_reduce_fminimum: {
5512 // reduce min/max will choose an element from one of the vector elements,
5513 // so we can infer and class information that is common to all elements.
5514 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5515 InterestedClasses, Depth + 1, Q);
5516 // Can only propagate sign if output is never NaN.
5517 if (!Known.isKnownNeverNaN())
5518 Known.SignBit.reset();
5519 break;
5520 }
5521 // reverse preserves all characteristics of the input vec's element.
5522 case Intrinsic::vector_reverse:
5523 Known = computeKnownFPClass(
5524 II->getArgOperand(0), DemandedElts.reverseBits(),
5525 II->getFastMathFlags(), InterestedClasses, Depth + 1, Q);
5526 break;
5527 case Intrinsic::trunc:
5528 case Intrinsic::floor:
5529 case Intrinsic::ceil:
5530 case Intrinsic::rint:
5531 case Intrinsic::nearbyint:
5532 case Intrinsic::round:
5533 case Intrinsic::roundeven: {
5534 KnownFPClass KnownSrc;
5535 FPClassTest InterestedSrcs = InterestedClasses;
5536 if (InterestedSrcs & fcPosFinite)
5537 InterestedSrcs |= fcPosFinite;
5538 if (InterestedSrcs & fcNegFinite)
5539 InterestedSrcs |= fcNegFinite;
5540 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5541 KnownSrc, Depth + 1, Q);
5542
5543 // Integer results cannot be subnormal.
5544 Known.knownNot(fcSubnormal);
5545
5546 Known.propagateNaN(KnownSrc, true);
5547
5548 // Pass through infinities, except PPC_FP128 is a special case for
5549 // intrinsics other than trunc.
5550 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5551 if (KnownSrc.isKnownNeverPosInfinity())
5552 Known.knownNot(fcPosInf);
5553 if (KnownSrc.isKnownNeverNegInfinity())
5554 Known.knownNot(fcNegInf);
5555 }
5556
5557 // Negative round ups to 0 produce -0
5558 if (KnownSrc.isKnownNever(fcPosFinite))
5559 Known.knownNot(fcPosFinite);
5560 if (KnownSrc.isKnownNever(fcNegFinite))
5561 Known.knownNot(fcNegFinite);
5562
5563 break;
5564 }
5565 case Intrinsic::exp:
5566 case Intrinsic::exp2:
5567 case Intrinsic::exp10: {
5568 Known.knownNot(fcNegative);
5569 if ((InterestedClasses & fcNan) == fcNone)
5570 break;
5571
5572 KnownFPClass KnownSrc;
5573 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5574 KnownSrc, Depth + 1, Q);
5575 if (KnownSrc.isKnownNeverNaN()) {
5576 Known.knownNot(fcNan);
5577 Known.signBitMustBeZero();
5578 }
5579
5580 break;
5581 }
5582 case Intrinsic::fptrunc_round: {
5583 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5584 Depth, Q);
5585 break;
5586 }
5587 case Intrinsic::log:
5588 case Intrinsic::log10:
5589 case Intrinsic::log2:
5590 case Intrinsic::experimental_constrained_log:
5591 case Intrinsic::experimental_constrained_log10:
5592 case Intrinsic::experimental_constrained_log2: {
5593 // log(+inf) -> +inf
5594 // log([+-]0.0) -> -inf
5595 // log(-inf) -> nan
5596 // log(-x) -> nan
5597 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5598 break;
5599
5600 FPClassTest InterestedSrcs = InterestedClasses;
5601 if ((InterestedClasses & fcNegInf) != fcNone)
5602 InterestedSrcs |= fcZero | fcSubnormal;
5603 if ((InterestedClasses & fcNan) != fcNone)
5604 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5605
5606 KnownFPClass KnownSrc;
5607 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5608 KnownSrc, Depth + 1, Q);
5609
5610 if (KnownSrc.isKnownNeverPosInfinity())
5611 Known.knownNot(fcPosInf);
5612
5613 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5614 Known.knownNot(fcNan);
5615
5616 const Function *F = II->getFunction();
5617 if (F && KnownSrc.isKnownNeverLogicalZero(*F, II->getType()))
5618 Known.knownNot(fcNegInf);
5619
5620 break;
5621 }
5622 case Intrinsic::powi: {
5623 if ((InterestedClasses & fcNegative) == fcNone)
5624 break;
5625
5626 const Value *Exp = II->getArgOperand(1);
5627 Type *ExpTy = Exp->getType();
5628 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5629 KnownBits ExponentKnownBits(BitWidth);
5630 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5631 ExponentKnownBits, Depth + 1, Q);
5632
5633 if (ExponentKnownBits.Zero[0]) { // Is even
5634 Known.knownNot(fcNegative);
5635 break;
5636 }
5637
5638 // Given that exp is an integer, here are the
5639 // ways that pow can return a negative value:
5640 //
5641 // pow(-x, exp) --> negative if exp is odd and x is negative.
5642 // pow(-0, exp) --> -inf if exp is negative odd.
5643 // pow(-0, exp) --> -0 if exp is positive odd.
5644 // pow(-inf, exp) --> -0 if exp is negative odd.
5645 // pow(-inf, exp) --> -inf if exp is positive odd.
5646 KnownFPClass KnownSrc;
5647 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5648 KnownSrc, Depth + 1, Q);
5649 if (KnownSrc.isKnownNever(fcNegative))
5650 Known.knownNot(fcNegative);
5651 break;
5652 }
5653 case Intrinsic::ldexp: {
5654 KnownFPClass KnownSrc;
5655 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5656 KnownSrc, Depth + 1, Q);
5657 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5658
5659 // Sign is preserved, but underflows may produce zeroes.
5660 if (KnownSrc.isKnownNever(fcNegative))
5661 Known.knownNot(fcNegative);
5662 else if (KnownSrc.cannotBeOrderedLessThanZero())
5664
5665 if (KnownSrc.isKnownNever(fcPositive))
5666 Known.knownNot(fcPositive);
5667 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5669
5670 // Can refine inf/zero handling based on the exponent operand.
5671 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5672 if ((InterestedClasses & ExpInfoMask) == fcNone)
5673 break;
5674 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5675 break;
5676
5677 const fltSemantics &Flt =
5678 II->getType()->getScalarType()->getFltSemantics();
5679 unsigned Precision = APFloat::semanticsPrecision(Flt);
5680 const Value *ExpArg = II->getArgOperand(1);
5682 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5683
5684 const int MantissaBits = Precision - 1;
5685 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5686 Known.knownNot(fcSubnormal);
5687
5688 const Function *F = II->getFunction();
5689 const APInt *ConstVal = ExpRange.getSingleElement();
5690 if (ConstVal && ConstVal->isZero()) {
5691 // ldexp(x, 0) -> x, so propagate everything.
5692 Known.propagateCanonicalizingSrc(KnownSrc, *F, II->getType());
5693 } else if (ExpRange.isAllNegative()) {
5694 // If we know the power is <= 0, can't introduce inf
5695 if (KnownSrc.isKnownNeverPosInfinity())
5696 Known.knownNot(fcPosInf);
5697 if (KnownSrc.isKnownNeverNegInfinity())
5698 Known.knownNot(fcNegInf);
5699 } else if (ExpRange.isAllNonNegative()) {
5700 // If we know the power is >= 0, can't introduce subnormal or zero
5701 if (KnownSrc.isKnownNeverPosSubnormal())
5702 Known.knownNot(fcPosSubnormal);
5703 if (KnownSrc.isKnownNeverNegSubnormal())
5704 Known.knownNot(fcNegSubnormal);
5705 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, II->getType()))
5706 Known.knownNot(fcPosZero);
5707 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))
5708 Known.knownNot(fcNegZero);
5709 }
5710
5711 break;
5712 }
5713 case Intrinsic::arithmetic_fence: {
5714 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5715 Known, Depth + 1, Q);
5716 break;
5717 }
5718 case Intrinsic::experimental_constrained_sitofp:
5719 case Intrinsic::experimental_constrained_uitofp:
5720 // Cannot produce nan
5721 Known.knownNot(fcNan);
5722
5723 // sitofp and uitofp turn into +0.0 for zero.
5724 Known.knownNot(fcNegZero);
5725
5726 // Integers cannot be subnormal
5727 Known.knownNot(fcSubnormal);
5728
5729 if (IID == Intrinsic::experimental_constrained_uitofp)
5730 Known.signBitMustBeZero();
5731
5732 // TODO: Copy inf handling from instructions
5733 break;
5734 default:
5735 break;
5736 }
5737
5738 break;
5739 }
5740 case Instruction::FAdd:
5741 case Instruction::FSub: {
5742 KnownFPClass KnownLHS, KnownRHS;
5743 bool WantNegative =
5744 Op->getOpcode() == Instruction::FAdd &&
5745 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5746 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5747 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5748
5749 if (!WantNaN && !WantNegative && !WantNegZero)
5750 break;
5751
5752 FPClassTest InterestedSrcs = InterestedClasses;
5753 if (WantNegative)
5754 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5755 if (InterestedClasses & fcNan)
5756 InterestedSrcs |= fcInf;
5757 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5758 KnownRHS, Depth + 1, Q);
5759
5760 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5761 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5762 WantNegZero || Opc == Instruction::FSub) {
5763
5764 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5765 // there's no point.
5766 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5767 KnownLHS, Depth + 1, Q);
5768 // Adding positive and negative infinity produces NaN.
5769 // TODO: Check sign of infinities.
5770 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5771 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5772 Known.knownNot(fcNan);
5773
5774 // FIXME: Context function should always be passed in separately
5775 const Function *F = cast<Instruction>(Op)->getFunction();
5776
5777 if (Op->getOpcode() == Instruction::FAdd) {
5778 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5779 KnownRHS.cannotBeOrderedLessThanZero())
5781 if (!F)
5782 break;
5783
5784 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5785 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5786 KnownRHS.isKnownNeverLogicalNegZero(*F, Op->getType())) &&
5787 // Make sure output negative denormal can't flush to -0
5788 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5789 Known.knownNot(fcNegZero);
5790 } else {
5791 if (!F)
5792 break;
5793
5794 // Only fsub -0, +0 can return -0
5795 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5796 KnownRHS.isKnownNeverLogicalPosZero(*F, Op->getType())) &&
5797 // Make sure output negative denormal can't flush to -0
5798 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5799 Known.knownNot(fcNegZero);
5800 }
5801 }
5802
5803 break;
5804 }
5805 case Instruction::FMul: {
5806 // X * X is always non-negative or a NaN.
5807 if (Op->getOperand(0) == Op->getOperand(1))
5808 Known.knownNot(fcNegative);
5809
5810 if ((InterestedClasses & fcNan) != fcNan)
5811 break;
5812
5813 // fcSubnormal is only needed in case of DAZ.
5814 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5815
5816 KnownFPClass KnownLHS, KnownRHS;
5817 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5818 Depth + 1, Q);
5819 if (!KnownRHS.isKnownNeverNaN())
5820 break;
5821
5822 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5823 Depth + 1, Q);
5824 if (!KnownLHS.isKnownNeverNaN())
5825 break;
5826
5827 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5828 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5829 Known.signBitMustBeZero();
5830 else
5831 Known.signBitMustBeOne();
5832 }
5833
5834 // If 0 * +/-inf produces NaN.
5835 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5836 Known.knownNot(fcNan);
5837 break;
5838 }
5839
5840 const Function *F = cast<Instruction>(Op)->getFunction();
5841 if (!F)
5842 break;
5843
5844 if ((KnownRHS.isKnownNeverInfinity() ||
5845 KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) &&
5846 (KnownLHS.isKnownNeverInfinity() ||
5847 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))
5848 Known.knownNot(fcNan);
5849
5850 break;
5851 }
5852 case Instruction::FDiv:
5853 case Instruction::FRem: {
5854 if (Op->getOperand(0) == Op->getOperand(1)) {
5855 // TODO: Could filter out snan if we inspect the operand
5856 if (Op->getOpcode() == Instruction::FDiv) {
5857 // X / X is always exactly 1.0 or a NaN.
5859 } else {
5860 // X % X is always exactly [+-]0.0 or a NaN.
5861 Known.KnownFPClasses = fcNan | fcZero;
5862 }
5863
5864 break;
5865 }
5866
5867 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5868 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5869 const bool WantPositive =
5870 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5871 if (!WantNan && !WantNegative && !WantPositive)
5872 break;
5873
5874 KnownFPClass KnownLHS, KnownRHS;
5875
5876 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5877 fcNan | fcInf | fcZero | fcNegative, KnownRHS,
5878 Depth + 1, Q);
5879
5880 bool KnowSomethingUseful =
5881 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5882
5883 if (KnowSomethingUseful || WantPositive) {
5884 const FPClassTest InterestedLHS =
5885 WantPositive ? fcAllFlags
5887
5888 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5889 InterestedClasses & InterestedLHS, KnownLHS,
5890 Depth + 1, Q);
5891 }
5892
5893 const Function *F = cast<Instruction>(Op)->getFunction();
5894
5895 if (Op->getOpcode() == Instruction::FDiv) {
5896 // Only 0/0, Inf/Inf produce NaN.
5897 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5898 (KnownLHS.isKnownNeverInfinity() ||
5899 KnownRHS.isKnownNeverInfinity()) &&
5900 ((F && KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) ||
5901 (F && KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))) {
5902 Known.knownNot(fcNan);
5903 }
5904
5905 // X / -0.0 is -Inf (or NaN).
5906 // +X / +X is +X
5907 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5908 Known.knownNot(fcNegative);
5909 } else {
5910 // Inf REM x and x REM 0 produce NaN.
5911 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5912 KnownLHS.isKnownNeverInfinity() && F &&
5913 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())) {
5914 Known.knownNot(fcNan);
5915 }
5916
5917 // The sign for frem is the same as the first operand.
5918 if (KnownLHS.cannotBeOrderedLessThanZero())
5920 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5922
5923 // See if we can be more aggressive about the sign of 0.
5924 if (KnownLHS.isKnownNever(fcNegative))
5925 Known.knownNot(fcNegative);
5926 if (KnownLHS.isKnownNever(fcPositive))
5927 Known.knownNot(fcPositive);
5928 }
5929
5930 break;
5931 }
5932 case Instruction::FPExt: {
5933 // Infinity, nan and zero propagate from source.
5934 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5935 Known, Depth + 1, Q);
5936
5937 const fltSemantics &DstTy =
5938 Op->getType()->getScalarType()->getFltSemantics();
5939 const fltSemantics &SrcTy =
5940 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5941
5942 // All subnormal inputs should be in the normal range in the result type.
5943 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5944 if (Known.KnownFPClasses & fcPosSubnormal)
5945 Known.KnownFPClasses |= fcPosNormal;
5946 if (Known.KnownFPClasses & fcNegSubnormal)
5947 Known.KnownFPClasses |= fcNegNormal;
5948 Known.knownNot(fcSubnormal);
5949 }
5950
5951 // Sign bit of a nan isn't guaranteed.
5952 if (!Known.isKnownNeverNaN())
5953 Known.SignBit = std::nullopt;
5954 break;
5955 }
5956 case Instruction::FPTrunc: {
5957 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5958 Depth, Q);
5959 break;
5960 }
5961 case Instruction::SIToFP:
5962 case Instruction::UIToFP: {
5963 // Cannot produce nan
5964 Known.knownNot(fcNan);
5965
5966 // Integers cannot be subnormal
5967 Known.knownNot(fcSubnormal);
5968
5969 // sitofp and uitofp turn into +0.0 for zero.
5970 Known.knownNot(fcNegZero);
5971 if (Op->getOpcode() == Instruction::UIToFP)
5972 Known.signBitMustBeZero();
5973
5974 if (InterestedClasses & fcInf) {
5975 // Get width of largest magnitude integer (remove a bit if signed).
5976 // This still works for a signed minimum value because the largest FP
5977 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5978 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5979 if (Op->getOpcode() == Instruction::SIToFP)
5980 --IntSize;
5981
5982 // If the exponent of the largest finite FP value can hold the largest
5983 // integer, the result of the cast must be finite.
5984 Type *FPTy = Op->getType()->getScalarType();
5985 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5986 Known.knownNot(fcInf);
5987 }
5988
5989 break;
5990 }
5991 case Instruction::ExtractElement: {
5992 // Look through extract element. If the index is non-constant or
5993 // out-of-range demand all elements, otherwise just the extracted element.
5994 const Value *Vec = Op->getOperand(0);
5995 const Value *Idx = Op->getOperand(1);
5996 auto *CIdx = dyn_cast<ConstantInt>(Idx);
5997
5998 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5999 unsigned NumElts = VecTy->getNumElements();
6000 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
6001 if (CIdx && CIdx->getValue().ult(NumElts))
6002 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
6003 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
6004 Depth + 1, Q);
6005 }
6006
6007 break;
6008 }
6009 case Instruction::InsertElement: {
6010 if (isa<ScalableVectorType>(Op->getType()))
6011 return;
6012
6013 const Value *Vec = Op->getOperand(0);
6014 const Value *Elt = Op->getOperand(1);
6015 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
6016 unsigned NumElts = DemandedElts.getBitWidth();
6017 APInt DemandedVecElts = DemandedElts;
6018 bool NeedsElt = true;
6019 // If we know the index we are inserting to, clear it from Vec check.
6020 if (CIdx && CIdx->getValue().ult(NumElts)) {
6021 DemandedVecElts.clearBit(CIdx->getZExtValue());
6022 NeedsElt = DemandedElts[CIdx->getZExtValue()];
6023 }
6024
6025 // Do we demand the inserted element?
6026 if (NeedsElt) {
6027 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1, Q);
6028 // If we don't know any bits, early out.
6029 if (Known.isUnknown())
6030 break;
6031 } else {
6032 Known.KnownFPClasses = fcNone;
6033 }
6034
6035 // Do we need anymore elements from Vec?
6036 if (!DemandedVecElts.isZero()) {
6037 KnownFPClass Known2;
6038 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
6039 Depth + 1, Q);
6040 Known |= Known2;
6041 }
6042
6043 break;
6044 }
6045 case Instruction::ShuffleVector: {
6046 // For undef elements, we don't know anything about the common state of
6047 // the shuffle result.
6048 APInt DemandedLHS, DemandedRHS;
6049 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
6050 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
6051 return;
6052
6053 if (!!DemandedLHS) {
6054 const Value *LHS = Shuf->getOperand(0);
6055 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
6056 Depth + 1, Q);
6057
6058 // If we don't know any bits, early out.
6059 if (Known.isUnknown())
6060 break;
6061 } else {
6062 Known.KnownFPClasses = fcNone;
6063 }
6064
6065 if (!!DemandedRHS) {
6066 KnownFPClass Known2;
6067 const Value *RHS = Shuf->getOperand(1);
6068 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
6069 Depth + 1, Q);
6070 Known |= Known2;
6071 }
6072
6073 break;
6074 }
6075 case Instruction::ExtractValue: {
6076 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
6077 ArrayRef<unsigned> Indices = Extract->getIndices();
6078 const Value *Src = Extract->getAggregateOperand();
6079 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
6080 Indices[0] == 0) {
6081 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
6082 switch (II->getIntrinsicID()) {
6083 case Intrinsic::frexp: {
6084 Known.knownNot(fcSubnormal);
6085
6086 KnownFPClass KnownSrc;
6087 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6088 InterestedClasses, KnownSrc, Depth + 1, Q);
6089
6090 const Function *F = cast<Instruction>(Op)->getFunction();
6091
6092 if (KnownSrc.isKnownNever(fcNegative))
6093 Known.knownNot(fcNegative);
6094 else {
6095 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, Op->getType()))
6096 Known.knownNot(fcNegZero);
6097 if (KnownSrc.isKnownNever(fcNegInf))
6098 Known.knownNot(fcNegInf);
6099 }
6100
6101 if (KnownSrc.isKnownNever(fcPositive))
6102 Known.knownNot(fcPositive);
6103 else {
6104 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, Op->getType()))
6105 Known.knownNot(fcPosZero);
6106 if (KnownSrc.isKnownNever(fcPosInf))
6107 Known.knownNot(fcPosInf);
6108 }
6109
6110 Known.propagateNaN(KnownSrc);
6111 return;
6112 }
6113 default:
6114 break;
6115 }
6116 }
6117 }
6118
6119 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1,
6120 Q);
6121 break;
6122 }
6123 case Instruction::PHI: {
6124 const PHINode *P = cast<PHINode>(Op);
6125 // Unreachable blocks may have zero-operand PHI nodes.
6126 if (P->getNumIncomingValues() == 0)
6127 break;
6128
6129 // Otherwise take the unions of the known bit sets of the operands,
6130 // taking conservative care to avoid excessive recursion.
6131 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6132
6133 if (Depth < PhiRecursionLimit) {
6134 // Skip if every incoming value references to ourself.
6135 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6136 break;
6137
6138 bool First = true;
6139
6140 for (const Use &U : P->operands()) {
6141 Value *IncValue;
6142 Instruction *CxtI;
6143 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
6144 // Skip direct self references.
6145 if (IncValue == P)
6146 continue;
6147
6148 KnownFPClass KnownSrc;
6149 // Recurse, but cap the recursion to two levels, because we don't want
6150 // to waste time spinning around in loops. We need at least depth 2 to
6151 // detect known sign bits.
6152 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6153 PhiRecursionLimit,
6155
6156 if (First) {
6157 Known = KnownSrc;
6158 First = false;
6159 } else {
6160 Known |= KnownSrc;
6161 }
6162
6163 if (Known.KnownFPClasses == fcAllFlags)
6164 break;
6165 }
6166 }
6167
6168 break;
6169 }
6170 case Instruction::BitCast: {
6171 const Value *Src;
6172 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6173 !Src->getType()->isIntOrIntVectorTy())
6174 break;
6175
6176 const Type *Ty = Op->getType()->getScalarType();
6177 KnownBits Bits(Ty->getScalarSizeInBits());
6178 computeKnownBits(Src, DemandedElts, Bits, Depth + 1, Q);
6179
6180 // Transfer information from the sign bit.
6181 if (Bits.isNonNegative())
6182 Known.signBitMustBeZero();
6183 else if (Bits.isNegative())
6184 Known.signBitMustBeOne();
6185
6186 if (Ty->isIEEE()) {
6187 // IEEE floats are NaN when all bits of the exponent plus at least one of
6188 // the fraction bits are 1. This means:
6189 // - If we assume unknown bits are 0 and the value is NaN, it will
6190 // always be NaN
6191 // - If we assume unknown bits are 1 and the value is not NaN, it can
6192 // never be NaN
6193 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
6194 Known.KnownFPClasses = fcNan;
6195 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
6196 Known.knownNot(fcNan);
6197
6198 // Build KnownBits representing Inf and check if it must be equal or
6199 // unequal to this value.
6200 auto InfKB = KnownBits::makeConstant(
6202 InfKB.Zero.clearSignBit();
6203 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
6204 assert(!InfResult.value());
6205 Known.knownNot(fcInf);
6206 } else if (Bits == InfKB) {
6207 Known.KnownFPClasses = fcInf;
6208 }
6209
6210 // Build KnownBits representing Zero and check if it must be equal or
6211 // unequal to this value.
6212 auto ZeroKB = KnownBits::makeConstant(
6214 ZeroKB.Zero.clearSignBit();
6215 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
6216 assert(!ZeroResult.value());
6217 Known.knownNot(fcZero);
6218 } else if (Bits == ZeroKB) {
6219 Known.KnownFPClasses = fcZero;
6220 }
6221 }
6222
6223 break;
6224 }
6225 default:
6226 break;
6227 }
6228}
6229
6231 const APInt &DemandedElts,
6232 FPClassTest InterestedClasses,
6233 unsigned Depth,
6234 const SimplifyQuery &SQ) {
6235 KnownFPClass KnownClasses;
6236 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, Depth,
6237 SQ);
6238 return KnownClasses;
6239}
6240
6242 FPClassTest InterestedClasses,
6243 unsigned Depth,
6244 const SimplifyQuery &SQ) {
6245 KnownFPClass Known;
6246 ::computeKnownFPClass(V, Known, InterestedClasses, Depth, SQ);
6247 return Known;
6248}
6249
6251
6252 // All byte-wide stores are splatable, even of arbitrary variables.
6253 if (V->getType()->isIntegerTy(8))
6254 return V;
6255
6256 LLVMContext &Ctx = V->getContext();
6257
6258 // Undef don't care.
6259 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6260 if (isa<UndefValue>(V))
6261 return UndefInt8;
6262
6263 // Return poison for zero-sized type.
6264 if (DL.getTypeStoreSize(V->getType()).isZero())
6265 return PoisonValue::get(Type::getInt8Ty(Ctx));
6266
6267 Constant *C = dyn_cast<Constant>(V);
6268 if (!C) {
6269 // Conceptually, we could handle things like:
6270 // %a = zext i8 %X to i16
6271 // %b = shl i16 %a, 8
6272 // %c = or i16 %a, %b
6273 // but until there is an example that actually needs this, it doesn't seem
6274 // worth worrying about.
6275 return nullptr;
6276 }
6277
6278 // Handle 'null' ConstantArrayZero etc.
6279 if (C->isNullValue())
6281
6282 // Constant floating-point values can be handled as integer values if the
6283 // corresponding integer value is "byteable". An important case is 0.0.
6284 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6285 Type *Ty = nullptr;
6286 if (CFP->getType()->isHalfTy())
6287 Ty = Type::getInt16Ty(Ctx);
6288 else if (CFP->getType()->isFloatTy())
6289 Ty = Type::getInt32Ty(Ctx);
6290 else if (CFP->getType()->isDoubleTy())
6291 Ty = Type::getInt64Ty(Ctx);
6292 // Don't handle long double formats, which have strange constraints.
6293 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6294 : nullptr;
6295 }
6296
6297 // We can handle constant integers that are multiple of 8 bits.
6298 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6299 if (CI->getBitWidth() % 8 == 0) {
6300 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6301 if (!CI->getValue().isSplat(8))
6302 return nullptr;
6303 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6304 }
6305 }
6306
6307 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6308 if (CE->getOpcode() == Instruction::IntToPtr) {
6309 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6310 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6312 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6313 return isBytewiseValue(Op, DL);
6314 }
6315 }
6316 }
6317
6318 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6319 if (LHS == RHS)
6320 return LHS;
6321 if (!LHS || !RHS)
6322 return nullptr;
6323 if (LHS == UndefInt8)
6324 return RHS;
6325 if (RHS == UndefInt8)
6326 return LHS;
6327 return nullptr;
6328 };
6329
6330 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
6331 Value *Val = UndefInt8;
6332 for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
6333 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6334 return nullptr;
6335 return Val;
6336 }
6337
6338 if (isa<ConstantAggregate>(C)) {
6339 Value *Val = UndefInt8;
6340 for (Value *Op : C->operands())
6341 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6342 return nullptr;
6343 return Val;
6344 }
6345
6346 // Don't try to handle the handful of other constants.
6347 return nullptr;
6348}
6349
6350// This is the recursive version of BuildSubAggregate. It takes a few different
6351// arguments. Idxs is the index within the nested struct From that we are
6352// looking at now (which is of type IndexedType). IdxSkip is the number of
6353// indices from Idxs that should be left out when inserting into the resulting
6354// struct. To is the result struct built so far, new insertvalue instructions
6355// build on that.
6356static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6358 unsigned IdxSkip,
6359 BasicBlock::iterator InsertBefore) {
6360 StructType *STy = dyn_cast<StructType>(IndexedType);
6361 if (STy) {
6362 // Save the original To argument so we can modify it
6363 Value *OrigTo = To;
6364 // General case, the type indexed by Idxs is a struct
6365 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6366 // Process each struct element recursively
6367 Idxs.push_back(i);
6368 Value *PrevTo = To;
6369 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6370 InsertBefore);
6371 Idxs.pop_back();
6372 if (!To) {
6373 // Couldn't find any inserted value for this index? Cleanup
6374 while (PrevTo != OrigTo) {
6375 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
6376 PrevTo = Del->getAggregateOperand();
6377 Del->eraseFromParent();
6378 }
6379 // Stop processing elements
6380 break;
6381 }
6382 }
6383 // If we successfully found a value for each of our subaggregates
6384 if (To)
6385 return To;
6386 }
6387 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6388 // the struct's elements had a value that was inserted directly. In the latter
6389 // case, perhaps we can't determine each of the subelements individually, but
6390 // we might be able to find the complete struct somewhere.
6391
6392 // Find the value that is at that particular spot
6393 Value *V = FindInsertedValue(From, Idxs);
6394
6395 if (!V)
6396 return nullptr;
6397
6398 // Insert the value in the new (sub) aggregate
6399 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6400 InsertBefore);
6401}
6402
6403// This helper takes a nested struct and extracts a part of it (which is again a
6404// struct) into a new value. For example, given the struct:
6405// { a, { b, { c, d }, e } }
6406// and the indices "1, 1" this returns
6407// { c, d }.
6408//
6409// It does this by inserting an insertvalue for each element in the resulting
6410// struct, as opposed to just inserting a single struct. This will only work if
6411// each of the elements of the substruct are known (ie, inserted into From by an
6412// insertvalue instruction somewhere).
6413//
6414// All inserted insertvalue instructions are inserted before InsertBefore
6416 BasicBlock::iterator InsertBefore) {
6417 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6418 idx_range);
6419 Value *To = PoisonValue::get(IndexedType);
6420 SmallVector<unsigned, 10> Idxs(idx_range);
6421 unsigned IdxSkip = Idxs.size();
6422
6423 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6424}
6425
6426/// Given an aggregate and a sequence of indices, see if the scalar value
6427/// indexed is already around as a register, for example if it was inserted
6428/// directly into the aggregate.
6429///
6430/// If InsertBefore is not null, this function will duplicate (modified)
6431/// insertvalues when a part of a nested struct is extracted.
6432Value *
6434 std::optional<BasicBlock::iterator> InsertBefore) {
6435 // Nothing to index? Just return V then (this is useful at the end of our
6436 // recursion).
6437 if (idx_range.empty())
6438 return V;
6439 // We have indices, so V should have an indexable type.
6440 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6441 "Not looking at a struct or array?");
6442 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6443 "Invalid indices for type?");
6444
6445 if (Constant *C = dyn_cast<Constant>(V)) {
6446 C = C->getAggregateElement(idx_range[0]);
6447 if (!C) return nullptr;
6448 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6449 }
6450
6451 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
6452 // Loop the indices for the insertvalue instruction in parallel with the
6453 // requested indices
6454 const unsigned *req_idx = idx_range.begin();
6455 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6456 i != e; ++i, ++req_idx) {
6457 if (req_idx == idx_range.end()) {
6458 // We can't handle this without inserting insertvalues
6459 if (!InsertBefore)
6460 return nullptr;
6461
6462 // The requested index identifies a part of a nested aggregate. Handle
6463 // this specially. For example,
6464 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6465 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6466 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6467 // This can be changed into
6468 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6469 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6470 // which allows the unused 0,0 element from the nested struct to be
6471 // removed.
6472 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6473 *InsertBefore);
6474 }
6475
6476 // This insert value inserts something else than what we are looking for.
6477 // See if the (aggregate) value inserted into has the value we are
6478 // looking for, then.
6479 if (*req_idx != *i)
6480 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6481 InsertBefore);
6482 }
6483 // If we end up here, the indices of the insertvalue match with those
6484 // requested (though possibly only partially). Now we recursively look at
6485 // the inserted value, passing any remaining indices.
6486 return FindInsertedValue(I->getInsertedValueOperand(),
6487 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6488 }
6489
6490 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
6491 // If we're extracting a value from an aggregate that was extracted from
6492 // something else, we can extract from that something else directly instead.
6493 // However, we will need to chain I's indices with the requested indices.
6494
6495 // Calculate the number of indices required
6496 unsigned size = I->getNumIndices() + idx_range.size();
6497 // Allocate some space to put the new indices in
6499 Idxs.reserve(size);
6500 // Add indices from the extract value instruction
6501 Idxs.append(I->idx_begin(), I->idx_end());
6502
6503 // Add requested indices
6504 Idxs.append(idx_range.begin(), idx_range.end());
6505
6506 assert(Idxs.size() == size
6507 && "Number of indices added not correct?");
6508
6509 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6510 }
6511 // Otherwise, we don't know (such as, extracting from a function return value
6512 // or load instruction)
6513 return nullptr;
6514}
6515
6517 unsigned CharSize) {
6518 // Make sure the GEP has exactly three arguments.
6519 if (GEP->getNumOperands() != 3)
6520 return false;
6521
6522 // Make sure the index-ee is a pointer to array of \p CharSize integers.
6523 // CharSize.
6524 ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
6525 if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
6526 return false;
6527
6528 // Check to make sure that the first operand of the GEP is an integer and
6529 // has value 0 so that we are sure we're indexing into the initializer.
6530 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
6531 if (!FirstIdx || !FirstIdx->isZero())
6532 return false;
6533
6534 return true;
6535}
6536
6537// If V refers to an initialized global constant, set Slice either to
6538// its initializer if the size of its elements equals ElementSize, or,
6539// for ElementSize == 8, to its representation as an array of unsiged
6540// char. Return true on success.
6541// Offset is in the unit "nr of ElementSize sized elements".
6544 unsigned ElementSize, uint64_t Offset) {
6545 assert(V && "V should not be null.");
6546 assert((ElementSize % 8) == 0 &&
6547 "ElementSize expected to be a multiple of the size of a byte.");
6548 unsigned ElementSizeInBytes = ElementSize / 8;
6549
6550 // Drill down into the pointer expression V, ignoring any intervening
6551 // casts, and determine the identity of the object it references along
6552 // with the cumulative byte offset into it.
6553 const GlobalVariable *GV =
6554 dyn_cast<GlobalVariable>(getUnderlyingObject(V));
6555 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6556 // Fail if V is not based on constant global object.
6557 return false;
6558
6559 const DataLayout &DL = GV->getDataLayout();
6560 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6561
6562 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6563 /*AllowNonInbounds*/ true))
6564 // Fail if a constant offset could not be determined.
6565 return false;
6566
6567 uint64_t StartIdx = Off.getLimitedValue();
6568 if (StartIdx == UINT64_MAX)
6569 // Fail if the constant offset is excessive.
6570 return false;
6571
6572 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6573 // elements. Simply bail out if that isn't possible.
6574 if ((StartIdx % ElementSizeInBytes) != 0)
6575 return false;
6576
6577 Offset += StartIdx / ElementSizeInBytes;
6578 ConstantDataArray *Array = nullptr;
6579 ArrayType *ArrayTy = nullptr;
6580
6581 if (GV->getInitializer()->isNullValue()) {
6582 Type *GVTy = GV->getValueType();
6583 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6584 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6585
6586 Slice.Array = nullptr;
6587 Slice.Offset = 0;
6588 // Return an empty Slice for undersized constants to let callers
6589 // transform even undefined library calls into simpler, well-defined
6590 // expressions. This is preferable to making the calls although it
6591 // prevents sanitizers from detecting such calls.
6592 Slice.Length = Length < Offset ? 0 : Length - Offset;
6593 return true;
6594 }
6595
6596 auto *Init = const_cast<Constant *>(GV->getInitializer());
6597 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6598 Type *InitElTy = ArrayInit->getElementType();
6599 if (InitElTy->isIntegerTy(ElementSize)) {
6600 // If Init is an initializer for an array of the expected type
6601 // and size, use it as is.
6602 Array = ArrayInit;
6603 ArrayTy = ArrayInit->getType();
6604 }
6605 }
6606
6607 if (!Array) {
6608 if (ElementSize != 8)
6609 // TODO: Handle conversions to larger integral types.
6610 return false;
6611
6612 // Otherwise extract the portion of the initializer starting
6613 // at Offset as an array of bytes, and reset Offset.
6615 if (!Init)
6616 return false;
6617
6618 Offset = 0;
6619 Array = dyn_cast<ConstantDataArray>(Init);
6620 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6621 }
6622
6623 uint64_t NumElts = ArrayTy->getArrayNumElements();
6624 if (Offset > NumElts)
6625 return false;
6626
6627 Slice.Array = Array;
6628 Slice.Offset = Offset;
6629 Slice.Length = NumElts - Offset;
6630 return true;
6631}
6632
6633/// Extract bytes from the initializer of the constant array V, which need
6634/// not be a nul-terminated string. On success, store the bytes in Str and
6635/// return true. When TrimAtNul is set, Str will contain only the bytes up
6636/// to but not including the first nul. Return false on failure.
6638 bool TrimAtNul) {
6640 if (!getConstantDataArrayInfo(V, Slice, 8))
6641 return false;
6642
6643 if (Slice.Array == nullptr) {
6644 if (TrimAtNul) {
6645 // Return a nul-terminated string even for an empty Slice. This is
6646 // safe because all existing SimplifyLibcalls callers require string
6647 // arguments and the behavior of the functions they fold is undefined
6648 // otherwise. Folding the calls this way is preferable to making
6649 // the undefined library calls, even though it prevents sanitizers
6650 // from reporting such calls.
6651 Str = StringRef();
6652 return true;
6653 }
6654 if (Slice.Length == 1) {
6655 Str = StringRef("", 1);
6656 return true;
6657 }
6658 // We cannot instantiate a StringRef as we do not have an appropriate string
6659 // of 0s at hand.
6660 return false;
6661 }
6662
6663 // Start out with the entire array in the StringRef.
6664 Str = Slice.Array->getAsString();
6665 // Skip over 'offset' bytes.
6666 Str = Str.substr(Slice.Offset);
6667
6668 if (TrimAtNul) {
6669 // Trim off the \0 and anything after it. If the array is not nul
6670 // terminated, we just return the whole end of string. The client may know
6671 // some other way that the string is length-bound.
6672 Str = Str.substr(0, Str.find('\0'));
6673 }
6674 return true;
6675}
6676
6677// These next two are very similar to the above, but also look through PHI
6678// nodes.
6679// TODO: See if we can integrate these two together.
6680
6681/// If we can compute the length of the string pointed to by
6682/// the specified pointer, return 'len+1'. If we can't, return 0.
6685 unsigned CharSize) {
6686 // Look through noop bitcast instructions.
6687 V = V->stripPointerCasts();
6688
6689 // If this is a PHI node, there are two cases: either we have already seen it
6690 // or we haven't.
6691 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6692 if (!PHIs.insert(PN).second)
6693 return ~0ULL; // already in the set.
6694
6695 // If it was new, see if all the input strings are the same length.
6696 uint64_t LenSoFar = ~0ULL;
6697 for (Value *IncValue : PN->incoming_values()) {
6698 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6699 if (Len == 0) return 0; // Unknown length -> unknown.
6700
6701 if (Len == ~0ULL) continue;
6702
6703 if (Len != LenSoFar && LenSoFar != ~0ULL)
6704 return 0; // Disagree -> unknown.
6705 LenSoFar = Len;
6706 }
6707
6708 // Success, all agree.
6709 return LenSoFar;
6710 }
6711
6712 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6713 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6714 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6715 if (Len1 == 0) return 0;
6716 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6717 if (Len2 == 0) return 0;
6718 if (Len1 == ~0ULL) return Len2;
6719 if (Len2 == ~0ULL) return Len1;
6720 if (Len1 != Len2) return 0;
6721 return Len1;
6722 }
6723
6724 // Otherwise, see if we can read the string.
6726 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6727 return 0;
6728
6729 if (Slice.Array == nullptr)
6730 // Zeroinitializer (including an empty one).
6731 return 1;
6732
6733 // Search for the first nul character. Return a conservative result even
6734 // when there is no nul. This is safe since otherwise the string function
6735 // being folded such as strlen is undefined, and can be preferable to
6736 // making the undefined library call.
6737 unsigned NullIndex = 0;
6738 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6739 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6740 break;
6741 }
6742
6743 return NullIndex + 1;
6744}
6745
6746/// If we can compute the length of the string pointed to by
6747/// the specified pointer, return 'len+1'. If we can't, return 0.
6748uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6749 if (!V->getType()->isPointerTy())
6750 return 0;
6751
6753 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6754 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6755 // an empty string as a length.
6756 return Len == ~0ULL ? 1 : Len;
6757}
6758
6759const Value *
6761 bool MustPreserveNullness) {
6762 assert(Call &&
6763 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6764 if (const Value *RV = Call->getReturnedArgOperand())
6765 return RV;
6766 // This can be used only as a aliasing property.
6768 Call, MustPreserveNullness))
6769 return Call->getArgOperand(0);
6770 return nullptr;
6771}
6772
6774 const CallBase *Call, bool MustPreserveNullness) {
6775 switch (Call->getIntrinsicID()) {
6776 case Intrinsic::launder_invariant_group:
6777 case Intrinsic::strip_invariant_group:
6778 case Intrinsic::aarch64_irg:
6779 case Intrinsic::aarch64_tagp:
6780 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6781 // input pointer (and thus preserve null-ness for the purposes of escape
6782 // analysis, which is where the MustPreserveNullness flag comes in to play).
6783 // However, it will not necessarily map ptr addrspace(N) null to ptr
6784 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6785 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6786 // list, no one should be relying on such a strict interpretation of
6787 // MustPreserveNullness (and, at time of writing, they are not), but we
6788 // document this fact out of an abundance of caution.
6789 case Intrinsic::amdgcn_make_buffer_rsrc:
6790 return true;
6791 case Intrinsic::ptrmask:
6792 return !MustPreserveNullness;
6793 case Intrinsic::threadlocal_address:
6794 // The underlying variable changes with thread ID. The Thread ID may change
6795 // at coroutine suspend points.
6796 return !Call->getParent()->getParent()->isPresplitCoroutine();
6797 default:
6798 return false;
6799 }
6800}
6801
6802/// \p PN defines a loop-variant pointer to an object. Check if the
6803/// previous iteration of the loop was referring to the same object as \p PN.
6805 const LoopInfo *LI) {
6806 // Find the loop-defined value.
6807 Loop *L = LI->getLoopFor(PN->getParent());
6808 if (PN->getNumIncomingValues() != 2)
6809 return true;
6810
6811 // Find the value from previous iteration.
6812 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6813 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6814 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6815 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6816 return true;
6817
6818 // If a new pointer is loaded in the loop, the pointer references a different
6819 // object in every iteration. E.g.:
6820 // for (i)
6821 // int *p = a[i];
6822 // ...
6823 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6824 if (!L->isLoopInvariant(Load->getPointerOperand()))
6825 return false;
6826 return true;
6827}
6828
6829const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6830 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6831 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6832 const Value *PtrOp = GEP->getPointerOperand();
6833 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6834 return V;
6835 V = PtrOp;
6836 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6837 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6838 Value *NewV = cast<Operator>(V)->getOperand(0);
6839 if (!NewV->getType()->isPointerTy())
6840 return V;
6841 V = NewV;
6842 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6843 if (GA->isInterposable())
6844 return V;
6845 V = GA->getAliasee();
6846 } else {
6847 if (auto *PHI = dyn_cast<PHINode>(V)) {
6848 // Look through single-arg phi nodes created by LCSSA.
6849 if (PHI->getNumIncomingValues() == 1) {
6850 V = PHI->getIncomingValue(0);
6851 continue;
6852 }
6853 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6854 // CaptureTracking can know about special capturing properties of some
6855 // intrinsics like launder.invariant.group, that can't be expressed with
6856 // the attributes, but have properties like returning aliasing pointer.
6857 // Because some analysis may assume that nocaptured pointer is not
6858 // returned from some special intrinsic (because function would have to
6859 // be marked with returns attribute), it is crucial to use this function
6860 // because it should be in sync with CaptureTracking. Not using it may
6861 // cause weird miscompilations where 2 aliasing pointers are assumed to
6862 // noalias.
6863 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6864 V = RP;
6865 continue;
6866 }
6867 }
6868
6869 return V;
6870 }
6871 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6872 }
6873 return V;
6874}
6875
6878 const LoopInfo *LI, unsigned MaxLookup) {
6881 Worklist.push_back(V);
6882 do {
6883 const Value *P = Worklist.pop_back_val();
6884 P = getUnderlyingObject(P, MaxLookup);
6885
6886 if (!Visited.insert(P).second)
6887 continue;
6888
6889 if (auto *SI = dyn_cast<SelectInst>(P)) {
6890 Worklist.push_back(SI->getTrueValue());
6891 Worklist.push_back(SI->getFalseValue());
6892 continue;
6893 }
6894
6895 if (auto *PN = dyn_cast<PHINode>(P)) {
6896 // If this PHI changes the underlying object in every iteration of the
6897 // loop, don't look through it. Consider:
6898 // int **A;
6899 // for (i) {
6900 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6901 // Curr = A[i];
6902 // *Prev, *Curr;
6903 //
6904 // Prev is tracking Curr one iteration behind so they refer to different
6905 // underlying objects.
6906 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6908 append_range(Worklist, PN->incoming_values());
6909 else
6910 Objects.push_back(P);
6911 continue;
6912 }
6913
6914 Objects.push_back(P);
6915 } while (!Worklist.empty());
6916}
6917
6919 const unsigned MaxVisited = 8;
6920
6923 Worklist.push_back(V);
6924 const Value *Object = nullptr;
6925 // Used as fallback if we can't find a common underlying object through
6926 // recursion.
6927 bool First = true;
6928 const Value *FirstObject = getUnderlyingObject(V);
6929 do {
6930 const Value *P = Worklist.pop_back_val();
6931 P = First ? FirstObject : getUnderlyingObject(P);
6932 First = false;
6933
6934 if (!Visited.insert(P).second)
6935 continue;
6936
6937 if (Visited.size() == MaxVisited)
6938 return FirstObject;
6939
6940 if (auto *SI = dyn_cast<SelectInst>(P)) {
6941 Worklist.push_back(SI->getTrueValue());
6942 Worklist.push_back(SI->getFalseValue());
6943 continue;
6944 }
6945
6946 if (auto *PN = dyn_cast<PHINode>(P)) {
6947 append_range(Worklist, PN->incoming_values());
6948 continue;
6949 }
6950
6951 if (!Object)
6952 Object = P;
6953 else if (Object != P)
6954 return FirstObject;
6955 } while (!Worklist.empty());
6956
6957 return Object ? Object : FirstObject;
6958}
6959
6960/// This is the function that does the work of looking through basic
6961/// ptrtoint+arithmetic+inttoptr sequences.
6962static const Value *getUnderlyingObjectFromInt(const Value *V) {
6963 do {
6964 if (const Operator *U = dyn_cast<Operator>(V)) {
6965 // If we find a ptrtoint, we can transfer control back to the
6966 // regular getUnderlyingObjectFromInt.
6967 if (U->getOpcode() == Instruction::PtrToInt)
6968 return U->getOperand(0);
6969 // If we find an add of a constant, a multiplied value, or a phi, it's
6970 // likely that the other operand will lead us to the base
6971 // object. We don't have to worry about the case where the
6972 // object address is somehow being computed by the multiply,
6973 // because our callers only care when the result is an
6974 // identifiable object.
6975 if (U->getOpcode() != Instruction::Add ||
6976 (!isa<ConstantInt>(U->getOperand(1)) &&
6977 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6978 !isa<PHINode>(U->getOperand(1))))
6979 return V;
6980 V = U->getOperand(0);
6981 } else {
6982 return V;
6983 }
6984 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6985 } while (true);
6986}
6987
6988/// This is a wrapper around getUnderlyingObjects and adds support for basic
6989/// ptrtoint+arithmetic+inttoptr sequences.
6990/// It returns false if unidentified object is found in getUnderlyingObjects.
6992 SmallVectorImpl<Value *> &Objects) {
6994 SmallVector<const Value *, 4> Working(1, V);
6995 do {
6996 V = Working.pop_back_val();
6997
6999 getUnderlyingObjects(V, Objs);
7000
7001 for (const Value *V : Objs) {
7002 if (!Visited.insert(V).second)
7003 continue;
7004 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
7005 const Value *O =
7006 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
7007 if (O->getType()->isPointerTy()) {
7008 Working.push_back(O);
7009 continue;
7010 }
7011 }
7012 // If getUnderlyingObjects fails to find an identifiable object,
7013 // getUnderlyingObjectsForCodeGen also fails for safety.
7014 if (!isIdentifiedObject(V)) {
7015 Objects.clear();
7016 return false;
7017 }
7018 Objects.push_back(const_cast<Value *>(V));
7019 }
7020 } while (!Working.empty());
7021 return true;
7022}
7023
7025 AllocaInst *Result = nullptr;
7027 SmallVector<Value *, 4> Worklist;
7028
7029 auto AddWork = [&](Value *V) {
7030 if (Visited.insert(V).second)
7031 Worklist.push_back(V);
7032 };
7033
7034 AddWork(V);
7035 do {
7036 V = Worklist.pop_back_val();
7037 assert(Visited.count(V));
7038
7039 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
7040 if (Result && Result != AI)
7041 return nullptr;
7042 Result = AI;
7043 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
7044 AddWork(CI->getOperand(0));
7045 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
7046 for (Value *IncValue : PN->incoming_values())
7047 AddWork(IncValue);
7048 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
7049 AddWork(SI->getTrueValue());
7050 AddWork(SI->getFalseValue());
7051 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
7052 if (OffsetZero && !GEP->hasAllZeroIndices())
7053 return nullptr;
7054 AddWork(GEP->getPointerOperand());
7055 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
7056 Value *Returned = CB->getReturnedArgOperand();
7057 if (Returned)
7058 AddWork(Returned);
7059 else
7060 return nullptr;
7061 } else {
7062 return nullptr;
7063 }
7064 } while (!Worklist.empty());
7065
7066 return Result;
7067}
7068
7070 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7071 for (const User *U : V->users()) {
7072 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
7073 if (!II)
7074 return false;
7075
7076 if (AllowLifetime && II->isLifetimeStartOrEnd())
7077 continue;
7078
7079 if (AllowDroppable && II->isDroppable())
7080 continue;
7081
7082 return false;
7083 }
7084 return true;
7085}
7086
7089 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7090}
7093 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7094}
7095
7097 if (auto *II = dyn_cast<IntrinsicInst>(I))
7098 return isTriviallyVectorizable(II->getIntrinsicID());
7099 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7100 return (!Shuffle || Shuffle->isSelect()) &&
7101 !isa<CallBase, BitCastInst, ExtractElementInst>(I);
7102}
7103
7105 const Instruction *CtxI,
7106 AssumptionCache *AC,
7107 const DominatorTree *DT,
7108 const TargetLibraryInfo *TLI,
7109 bool UseVariableInfo) {
7110 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7111 AC, DT, TLI, UseVariableInfo);
7112}
7113
7115 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7116 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7117 bool UseVariableInfo) {
7118#ifndef NDEBUG
7119 if (Inst->getOpcode() != Opcode) {
7120 // Check that the operands are actually compatible with the Opcode override.
7121 auto hasEqualReturnAndLeadingOperandTypes =
7122 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7123 if (Inst->getNumOperands() < NumLeadingOperands)
7124 return false;
7125 const Type *ExpectedType = Inst->getType();
7126 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7127 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7128 return false;
7129 return true;
7130 };
7132 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7133 assert(!Instruction::isUnaryOp(Opcode) ||
7134 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7135 }
7136#endif
7137
7138 switch (Opcode) {
7139 default:
7140 return true;
7141 case Instruction::UDiv:
7142 case Instruction::URem: {
7143 // x / y is undefined if y == 0.
7144 const APInt *V;
7145 if (match(Inst->getOperand(1), m_APInt(V)))
7146 return *V != 0;
7147 return false;
7148 }
7149 case Instruction::SDiv:
7150 case Instruction::SRem: {
7151 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7152 const APInt *Numerator, *Denominator;
7153 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7154 return false;
7155 // We cannot hoist this division if the denominator is 0.
7156 if (*Denominator == 0)
7157 return false;
7158 // It's safe to hoist if the denominator is not 0 or -1.
7159 if (!Denominator->isAllOnes())
7160 return true;
7161 // At this point we know that the denominator is -1. It is safe to hoist as
7162 // long we know that the numerator is not INT_MIN.
7163 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7164 return !Numerator->isMinSignedValue();
7165 // The numerator *might* be MinSignedValue.
7166 return false;
7167 }
7168 case Instruction::Load: {
7169 if (!UseVariableInfo)
7170 return false;
7171
7172 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7173 if (!LI)
7174 return false;
7175 if (mustSuppressSpeculation(*LI))
7176 return false;
7177 const DataLayout &DL = LI->getDataLayout();
7179 LI->getType(), LI->getAlign(), DL,
7180 CtxI, AC, DT, TLI);
7181 }
7182 case Instruction::Call: {
7183 auto *CI = dyn_cast<const CallInst>(Inst);
7184 if (!CI)
7185 return false;
7186 const Function *Callee = CI->getCalledFunction();
7187
7188 // The called function could have undefined behavior or side-effects, even
7189 // if marked readnone nounwind.
7190 return Callee && Callee->isSpeculatable();
7191 }
7192 case Instruction::VAArg:
7193 case Instruction::Alloca:
7194 case Instruction::Invoke:
7195 case Instruction::CallBr:
7196 case Instruction::PHI:
7197 case Instruction::Store:
7198 case Instruction::Ret:
7199 case Instruction::Br:
7200 case Instruction::IndirectBr:
7201 case Instruction::Switch:
7202 case Instruction::Unreachable:
7203 case Instruction::Fence:
7204 case Instruction::AtomicRMW:
7205 case Instruction::AtomicCmpXchg:
7206 case Instruction::LandingPad:
7207 case Instruction::Resume:
7208 case Instruction::CatchSwitch:
7209 case Instruction::CatchPad:
7210 case Instruction::CatchRet:
7211 case Instruction::CleanupPad:
7212 case Instruction::CleanupRet:
7213 return false; // Misc instructions which have effects
7214 }
7215}
7216
7218 if (I.mayReadOrWriteMemory())
7219 // Memory dependency possible
7220 return true;
7222 // Can't move above a maythrow call or infinite loop. Or if an
7223 // inalloca alloca, above a stacksave call.
7224 return true;
7226 // 1) Can't reorder two inf-loop calls, even if readonly
7227 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7228 // safe to speculative execute. (Inverse of above)
7229 return true;
7230 return false;
7231}
7232
7233/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7235 switch (OR) {
7244 }
7245 llvm_unreachable("Unknown OverflowResult");
7246}
7247
7248/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7251 bool ForSigned,
7252 const SimplifyQuery &SQ) {
7253 ConstantRange CR1 =
7254 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7255 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7258 return CR1.intersectWith(CR2, RangeType);
7259}
7260
7262 const Value *RHS,
7263 const SimplifyQuery &SQ,
7264 bool IsNSW) {
7265 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
7266 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
7267
7268 // mul nsw of two non-negative numbers is also nuw.
7269 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
7271
7272 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
7273 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
7274 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7275}
7276
7278 const Value *RHS,
7279 const SimplifyQuery &SQ) {
7280 // Multiplying n * m significant bits yields a result of n + m significant
7281 // bits. If the total number of significant bits does not exceed the
7282 // result bit width (minus 1), there is no overflow.
7283 // This means if we have enough leading sign bits in the operands
7284 // we can guarantee that the result does not overflow.
7285 // Ref: "Hacker's Delight" by Henry Warren
7286 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7287
7288 // Note that underestimating the number of sign bits gives a more
7289 // conservative answer.
7290 unsigned SignBits =
7292
7293 // First handle the easy case: if we have enough sign bits there's
7294 // definitely no overflow.
7295 if (SignBits > BitWidth + 1)
7297
7298 // There are two ambiguous cases where there can be no overflow:
7299 // SignBits == BitWidth + 1 and
7300 // SignBits == BitWidth
7301 // The second case is difficult to check, therefore we only handle the
7302 // first case.
7303 if (SignBits == BitWidth + 1) {
7304 // It overflows only when both arguments are negative and the true
7305 // product is exactly the minimum negative number.
7306 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7307 // For simplicity we just check if at least one side is not negative.
7308 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
7309 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
7310 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7312 }
7314}
7315
7318 const WithCache<const Value *> &RHS,
7319 const SimplifyQuery &SQ) {
7320 ConstantRange LHSRange =
7321 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7322 ConstantRange RHSRange =
7323 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7324 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7325}
7326
7327static OverflowResult
7329 const WithCache<const Value *> &RHS,
7330 const AddOperator *Add, const SimplifyQuery &SQ) {
7331 if (Add && Add->hasNoSignedWrap()) {
7333 }
7334
7335 // If LHS and RHS each have at least two sign bits, the addition will look
7336 // like
7337 //
7338 // XX..... +
7339 // YY.....
7340 //
7341 // If the carry into the most significant position is 0, X and Y can't both
7342 // be 1 and therefore the carry out of the addition is also 0.
7343 //
7344 // If the carry into the most significant position is 1, X and Y can't both
7345 // be 0 and therefore the carry out of the addition is also 1.
7346 //
7347 // Since the carry into the most significant position is always equal to
7348 // the carry out of the addition, there is no signed overflow.
7349 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
7350 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
7352
7353 ConstantRange LHSRange =
7354 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7355 ConstantRange RHSRange =
7356 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7357 OverflowResult OR =
7358 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7360 return OR;
7361
7362 // The remaining code needs Add to be available. Early returns if not so.
7363 if (!Add)
7365
7366 // If the sign of Add is the same as at least one of the operands, this add
7367 // CANNOT overflow. If this can be determined from the known bits of the
7368 // operands the above signedAddMayOverflow() check will have already done so.
7369 // The only other way to improve on the known bits is from an assumption, so
7370 // call computeKnownBitsFromContext() directly.
7371 bool LHSOrRHSKnownNonNegative =
7372 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7373 bool LHSOrRHSKnownNegative =
7374 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7375 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7376 KnownBits AddKnown(LHSRange.getBitWidth());
7377 computeKnownBitsFromContext(Add, AddKnown, /*Depth=*/0, SQ);
7378 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7379 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7381 }
7382
7384}
7385
7387 const Value *RHS,
7388 const SimplifyQuery &SQ) {
7389 // X - (X % ?)
7390 // The remainder of a value can't have greater magnitude than itself,
7391 // so the subtraction can't overflow.
7392
7393 // X - (X -nuw ?)
7394 // In the minimal case, this would simplify to "?", so there's no subtract
7395 // at all. But if this analysis is used to peek through casts, for example,
7396 // then determining no-overflow may allow other transforms.
7397
7398 // TODO: There are other patterns like this.
7399 // See simplifyICmpWithBinOpOnLHS() for candidates.
7400 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7402 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7404
7406 SQ.DL)) {
7407 if (*C)
7410 }
7411
7412 ConstantRange LHSRange =
7413 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7414 ConstantRange RHSRange =
7415 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7416 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7417}
7418
7420 const Value *RHS,
7421 const SimplifyQuery &SQ) {
7422 // X - (X % ?)
7423 // The remainder of a value can't have greater magnitude than itself,
7424 // so the subtraction can't overflow.
7425
7426 // X - (X -nsw ?)
7427 // In the minimal case, this would simplify to "?", so there's no subtract
7428 // at all. But if this analysis is used to peek through casts, for example,
7429 // then determining no-overflow may allow other transforms.
7430 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7432 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7434
7435 // If LHS and RHS each have at least two sign bits, the subtraction
7436 // cannot overflow.
7437 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
7438 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
7440
7441 ConstantRange LHSRange =
7442 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7443 ConstantRange RHSRange =
7444 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7445 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7446}
7447
7449 const DominatorTree &DT) {
7450 SmallVector<const BranchInst *, 2> GuardingBranches;
7452
7453 for (const User *U : WO->users()) {
7454 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7455 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7456
7457 if (EVI->getIndices()[0] == 0)
7458 Results.push_back(EVI);
7459 else {
7460 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7461
7462 for (const auto *U : EVI->users())
7463 if (const auto *B = dyn_cast<BranchInst>(U)) {
7464 assert(B->isConditional() && "How else is it using an i1?");
7465 GuardingBranches.push_back(B);
7466 }
7467 }
7468 } else {
7469 // We are using the aggregate directly in a way we don't want to analyze
7470 // here (storing it to a global, say).
7471 return false;
7472 }
7473 }
7474
7475 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7476 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7477 if (!NoWrapEdge.isSingleEdge())
7478 return false;
7479
7480 // Check if all users of the add are provably no-wrap.
7481 for (const auto *Result : Results) {
7482 // If the extractvalue itself is not executed on overflow, the we don't
7483 // need to check each use separately, since domination is transitive.
7484 if (DT.dominates(NoWrapEdge, Result->getParent()))
7485 continue;
7486
7487 for (const auto &RU : Result->uses())
7488 if (!DT.dominates(NoWrapEdge, RU))
7489 return false;
7490 }
7491
7492 return true;
7493 };
7494
7495 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7496}
7497
7498/// Shifts return poison if shiftwidth is larger than the bitwidth.
7499static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7500 auto *C = dyn_cast<Constant>(ShiftAmount);
7501 if (!C)
7502 return false;
7503
7504 // Shifts return poison if shiftwidth is larger than the bitwidth.
7506 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7507 unsigned NumElts = FVTy->getNumElements();
7508 for (unsigned i = 0; i < NumElts; ++i)
7509 ShiftAmounts.push_back(C->getAggregateElement(i));
7510 } else if (isa<ScalableVectorType>(C->getType()))
7511 return false; // Can't tell, just return false to be safe
7512 else
7513 ShiftAmounts.push_back(C);
7514
7515 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7516 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7517 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7518 });
7519
7520 return Safe;
7521}
7522
7524 PoisonOnly = (1 << 0),
7525 UndefOnly = (1 << 1),
7527};
7528
7530 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7531}
7532
7534 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7535}
7536
7538 bool ConsiderFlagsAndMetadata) {
7539
7540 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7541 Op->hasPoisonGeneratingAnnotations())
7542 return true;
7543
7544 unsigned Opcode = Op->getOpcode();
7545
7546 // Check whether opcode is a poison/undef-generating operation
7547 switch (Opcode) {
7548 case Instruction::Shl:
7549 case Instruction::AShr:
7550 case Instruction::LShr:
7551 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7552 case Instruction::FPToSI:
7553 case Instruction::FPToUI:
7554 // fptosi/ui yields poison if the resulting value does not fit in the
7555 // destination type.
7556 return true;
7557 case Instruction::Call:
7558 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7559 switch (II->getIntrinsicID()) {
7560 // TODO: Add more intrinsics.
7561 case Intrinsic::ctlz:
7562 case Intrinsic::cttz:
7563 case Intrinsic::abs:
7564 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7565 return false;
7566 break;
7567 case Intrinsic::ctpop:
7568 case Intrinsic::bswap:
7569 case Intrinsic::bitreverse:
7570 case Intrinsic::fshl:
7571 case Intrinsic::fshr:
7572 case Intrinsic::smax:
7573 case Intrinsic::smin:
7574 case Intrinsic::umax:
7575 case Intrinsic::umin:
7576 case Intrinsic::ptrmask:
7577 case Intrinsic::fptoui_sat:
7578 case Intrinsic::fptosi_sat:
7579 case Intrinsic::sadd_with_overflow:
7580 case Intrinsic::ssub_with_overflow:
7581 case Intrinsic::smul_with_overflow:
7582 case Intrinsic::uadd_with_overflow:
7583 case Intrinsic::usub_with_overflow:
7584 case Intrinsic::umul_with_overflow:
7585 case Intrinsic::sadd_sat:
7586 case Intrinsic::uadd_sat:
7587 case Intrinsic::ssub_sat:
7588 case Intrinsic::usub_sat:
7589 return false;
7590 case Intrinsic::sshl_sat:
7591 case Intrinsic::ushl_sat:
7592 return includesPoison(Kind) &&
7593 !shiftAmountKnownInRange(II->getArgOperand(1));
7594 case Intrinsic::fma:
7595 case Intrinsic::fmuladd:
7596 case Intrinsic::sqrt:
7597 case Intrinsic::powi:
7598 case Intrinsic::sin:
7599 case Intrinsic::cos:
7600 case Intrinsic::pow:
7601 case Intrinsic::log:
7602 case Intrinsic::log10:
7603 case Intrinsic::log2:
7604 case Intrinsic::exp:
7605 case Intrinsic::exp2:
7606 case Intrinsic::exp10:
7607 case Intrinsic::fabs:
7608 case Intrinsic::copysign:
7609 case Intrinsic::floor:
7610 case Intrinsic::ceil:
7611 case Intrinsic::trunc:
7612 case Intrinsic::rint:
7613 case Intrinsic::nearbyint:
7614 case Intrinsic::round:
7615 case Intrinsic::roundeven:
7616 case Intrinsic::fptrunc_round:
7617 case Intrinsic::canonicalize:
7618 case Intrinsic::arithmetic_fence:
7619 case Intrinsic::minnum:
7620 case Intrinsic::maxnum:
7621 case Intrinsic::minimum:
7622 case Intrinsic::maximum:
7623 case Intrinsic::is_fpclass:
7624 case Intrinsic::ldexp:
7625 case Intrinsic::frexp:
7626 return false;
7627 case Intrinsic::lround:
7628 case Intrinsic::llround:
7629 case Intrinsic::lrint:
7630 case Intrinsic::llrint:
7631 // If the value doesn't fit an unspecified value is returned (but this
7632 // is not poison).
7633 return false;
7634 }
7635 }
7636 [[fallthrough]];
7637 case Instruction::CallBr:
7638 case Instruction::Invoke: {
7639 const auto *CB = cast<CallBase>(Op);
7640 return !CB->hasRetAttr(Attribute::NoUndef);
7641 }
7642 case Instruction::InsertElement:
7643 case Instruction::ExtractElement: {
7644 // If index exceeds the length of the vector, it returns poison
7645 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7646 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7647 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7648 if (includesPoison(Kind))
7649 return !Idx ||
7650 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7651 return false;
7652 }
7653 case Instruction::ShuffleVector: {
7654 ArrayRef<int> Mask = isa<ConstantExpr>(Op)
7655 ? cast<ConstantExpr>(Op)->getShuffleMask()
7656 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7657 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7658 }
7659 case Instruction::FNeg:
7660 case Instruction::PHI:
7661 case Instruction::Select:
7662 case Instruction::URem:
7663 case Instruction::SRem:
7664 case Instruction::ExtractValue:
7665 case Instruction::InsertValue:
7666 case Instruction::Freeze:
7667 case Instruction::ICmp:
7668 case Instruction::FCmp:
7669 case Instruction::FAdd:
7670 case Instruction::FSub:
7671 case Instruction::FMul:
7672 case Instruction::FDiv:
7673 case Instruction::FRem:
7674 return false;
7675 case Instruction::GetElementPtr:
7676 // inbounds is handled above
7677 // TODO: what about inrange on constexpr?
7678 return false;
7679 default: {
7680 const auto *CE = dyn_cast<ConstantExpr>(Op);
7681 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7682 return false;
7683 else if (Instruction::isBinaryOp(Opcode))
7684 return false;
7685 // Be conservative and return true.
7686 return true;
7687 }
7688 }
7689}
7690
7692 bool ConsiderFlagsAndMetadata) {
7693 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7694 ConsiderFlagsAndMetadata);
7695}
7696
7697bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7698 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7699 ConsiderFlagsAndMetadata);
7700}
7701
7702static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7703 unsigned Depth) {
7704 if (ValAssumedPoison == V)
7705 return true;
7706
7707 const unsigned MaxDepth = 2;
7708 if (Depth >= MaxDepth)
7709 return false;
7710
7711 if (const auto *I = dyn_cast<Instruction>(V)) {
7712 if (any_of(I->operands(), [=](const Use &Op) {
7713 return propagatesPoison(Op) &&
7714 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7715 }))
7716 return true;
7717
7718 // V = extractvalue V0, idx
7719 // V2 = extractvalue V0, idx2
7720 // V0's elements are all poison or not. (e.g., add_with_overflow)
7721 const WithOverflowInst *II;
7723 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7724 llvm::is_contained(II->args(), ValAssumedPoison)))
7725 return true;
7726 }
7727 return false;
7728}
7729
7730static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7731 unsigned Depth) {
7732 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7733 return true;
7734
7735 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7736 return true;
7737
7738 const unsigned MaxDepth = 2;
7739 if (Depth >= MaxDepth)
7740 return false;
7741
7742 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7743 if (I && !canCreatePoison(cast<Operator>(I))) {
7744 return all_of(I->operands(), [=](const Value *Op) {
7745 return impliesPoison(Op, V, Depth + 1);
7746 });
7747 }
7748 return false;
7749}
7750
7751bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7752 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7753}
7754
7755static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7756
7758 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7759 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7761 return false;
7762
7763 if (isa<MetadataAsValue>(V))
7764 return false;
7765
7766 if (const auto *A = dyn_cast<Argument>(V)) {
7767 if (A->hasAttribute(Attribute::NoUndef) ||
7768 A->hasAttribute(Attribute::Dereferenceable) ||
7769 A->hasAttribute(Attribute::DereferenceableOrNull))
7770 return true;
7771 }
7772
7773 if (auto *C = dyn_cast<Constant>(V)) {
7774 if (isa<PoisonValue>(C))
7775 return !includesPoison(Kind);
7776
7777 if (isa<UndefValue>(C))
7778 return !includesUndef(Kind);
7779
7780 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(V) ||
7781 isa<ConstantPointerNull>(C) || isa<Function>(C))
7782 return true;
7783
7784 if (C->getType()->isVectorTy() && !isa<ConstantExpr>(C)) {
7785 if (includesUndef(Kind) && C->containsUndefElement())
7786 return false;
7787 if (includesPoison(Kind) && C->containsPoisonElement())
7788 return false;
7789 return !C->containsConstantExpression();
7790 }
7791 }
7792
7793 // Strip cast operations from a pointer value.
7794 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7795 // inbounds with zero offset. To guarantee that the result isn't poison, the
7796 // stripped pointer is checked as it has to be pointing into an allocated
7797 // object or be null `null` to ensure `inbounds` getelement pointers with a
7798 // zero offset could not produce poison.
7799 // It can strip off addrspacecast that do not change bit representation as
7800 // well. We believe that such addrspacecast is equivalent to no-op.
7801 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7802 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7803 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7804 return true;
7805
7806 auto OpCheck = [&](const Value *V) {
7807 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7808 };
7809
7810 if (auto *Opr = dyn_cast<Operator>(V)) {
7811 // If the value is a freeze instruction, then it can never
7812 // be undef or poison.
7813 if (isa<FreezeInst>(V))
7814 return true;
7815
7816 if (const auto *CB = dyn_cast<CallBase>(V)) {
7817 if (CB->hasRetAttr(Attribute::NoUndef) ||
7818 CB->hasRetAttr(Attribute::Dereferenceable) ||
7819 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7820 return true;
7821 }
7822
7823 if (const auto *PN = dyn_cast<PHINode>(V)) {
7824 unsigned Num = PN->getNumIncomingValues();
7825 bool IsWellDefined = true;
7826 for (unsigned i = 0; i < Num; ++i) {
7827 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7828 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7829 DT, Depth + 1, Kind)) {
7830 IsWellDefined = false;
7831 break;
7832 }
7833 }
7834 if (IsWellDefined)
7835 return true;
7836 } else if (!::canCreateUndefOrPoison(Opr, Kind,
7837 /*ConsiderFlagsAndMetadata*/ true) &&
7838 all_of(Opr->operands(), OpCheck))
7839 return true;
7840 }
7841
7842 if (auto *I = dyn_cast<LoadInst>(V))
7843 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7844 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7845 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7846 return true;
7847
7849 return true;
7850
7851 // CxtI may be null or a cloned instruction.
7852 if (!CtxI || !CtxI->getParent() || !DT)
7853 return false;
7854
7855 auto *DNode = DT->getNode(CtxI->getParent());
7856 if (!DNode)
7857 // Unreachable block
7858 return false;
7859
7860 // If V is used as a branch condition before reaching CtxI, V cannot be
7861 // undef or poison.
7862 // br V, BB1, BB2
7863 // BB1:
7864 // CtxI ; V cannot be undef or poison here
7865 auto *Dominator = DNode->getIDom();
7866 // This check is purely for compile time reasons: we can skip the IDom walk
7867 // if what we are checking for includes undef and the value is not an integer.
7868 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7869 while (Dominator) {
7870 auto *TI = Dominator->getBlock()->getTerminator();
7871
7872 Value *Cond = nullptr;
7873 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7874 if (BI->isConditional())
7875 Cond = BI->getCondition();
7876 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7877 Cond = SI->getCondition();
7878 }
7879
7880 if (Cond) {
7881 if (Cond == V)
7882 return true;
7883 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7884 // For poison, we can analyze further
7885 auto *Opr = cast<Operator>(Cond);
7886 if (any_of(Opr->operands(), [V](const Use &U) {
7887 return V == U && propagatesPoison(U);
7888 }))
7889 return true;
7890 }
7891 }
7892
7893 Dominator = Dominator->getIDom();
7894 }
7895
7896 if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
7897 return true;
7898
7899 return false;
7900}
7901
7903 const Instruction *CtxI,
7904 const DominatorTree *DT,
7905 unsigned Depth) {
7906 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7907 UndefPoisonKind::UndefOrPoison);
7908}
7909
7911 const Instruction *CtxI,
7912 const DominatorTree *DT, unsigned Depth) {
7913 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7914 UndefPoisonKind::PoisonOnly);
7915}
7916
7918 const Instruction *CtxI,
7919 const DominatorTree *DT, unsigned Depth) {
7920 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7921 UndefPoisonKind::UndefOnly);
7922}
7923
7924/// Return true if undefined behavior would provably be executed on the path to
7925/// OnPathTo if Root produced a posion result. Note that this doesn't say
7926/// anything about whether OnPathTo is actually executed or whether Root is
7927/// actually poison. This can be used to assess whether a new use of Root can
7928/// be added at a location which is control equivalent with OnPathTo (such as
7929/// immediately before it) without introducing UB which didn't previously
7930/// exist. Note that a false result conveys no information.
7932 Instruction *OnPathTo,
7933 DominatorTree *DT) {
7934 // Basic approach is to assume Root is poison, propagate poison forward
7935 // through all users we can easily track, and then check whether any of those
7936 // users are provable UB and must execute before out exiting block might
7937 // exit.
7938
7939 // The set of all recursive users we've visited (which are assumed to all be
7940 // poison because of said visit)
7941 SmallSet<const Value *, 16> KnownPoison;
7943 Worklist.push_back(Root);
7944 while (!Worklist.empty()) {
7945 const Instruction *I = Worklist.pop_back_val();
7946
7947 // If we know this must trigger UB on a path leading our target.
7948 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7949 return true;
7950
7951 // If we can't analyze propagation through this instruction, just skip it
7952 // and transitive users. Safe as false is a conservative result.
7953 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7954 return KnownPoison.contains(U) && propagatesPoison(U);
7955 }))
7956 continue;
7957
7958 if (KnownPoison.insert(I).second)
7959 for (const User *User : I->users())
7960 Worklist.push_back(cast<Instruction>(User));
7961 }
7962
7963 // Might be non-UB, or might have a path we couldn't prove must execute on
7964 // way to exiting bb.
7965 return false;
7966}
7967
7969 const SimplifyQuery &SQ) {
7970 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7971 Add, SQ);
7972}
7973
7976 const WithCache<const Value *> &RHS,
7977 const SimplifyQuery &SQ) {
7978 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7979}
7980
7982 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7983 // of time because it's possible for another thread to interfere with it for an
7984 // arbitrary length of time, but programs aren't allowed to rely on that.
7985
7986 // If there is no successor, then execution can't transfer to it.
7987 if (isa<ReturnInst>(I))
7988 return false;
7989 if (isa<UnreachableInst>(I))
7990 return false;
7991
7992 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7993 // Instruction::willReturn.
7994 //
7995 // FIXME: Move this check into Instruction::willReturn.
7996 if (isa<CatchPadInst>(I)) {
7997 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7998 default:
7999 // A catchpad may invoke exception object constructors and such, which
8000 // in some languages can be arbitrary code, so be conservative by default.
8001 return false;
8003 // For CoreCLR, it just involves a type test.
8004 return true;
8005 }
8006 }
8007
8008 // An instruction that returns without throwing must transfer control flow
8009 // to a successor.
8010 return !I->mayThrow() && I->willReturn();
8011}
8012
8014 // TODO: This is slightly conservative for invoke instruction since exiting
8015 // via an exception *is* normal control for them.
8016 for (const Instruction &I : *BB)
8018 return false;
8019 return true;
8020}
8021
8024 unsigned ScanLimit) {
8026 ScanLimit);
8027}
8028
8031 assert(ScanLimit && "scan limit must be non-zero");
8032 for (const Instruction &I : Range) {
8033 if (isa<DbgInfoIntrinsic>(I))
8034 continue;
8035 if (--ScanLimit == 0)
8036 return false;
8038 return false;
8039 }
8040 return true;
8041}
8042
8044 const Loop *L) {
8045 // The loop header is guaranteed to be executed for every iteration.
8046 //
8047 // FIXME: Relax this constraint to cover all basic blocks that are
8048 // guaranteed to be executed at every iteration.
8049 if (I->getParent() != L->getHeader()) return false;
8050
8051 for (const Instruction &LI : *L->getHeader()) {
8052 if (&LI == I) return true;
8053 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
8054 }
8055 llvm_unreachable("Instruction not contained in its own parent basic block.");
8056}
8057
8058bool llvm::propagatesPoison(const Use &PoisonOp) {
8059 const Operator *I = cast<Operator>(PoisonOp.getUser());
8060 switch (I->getOpcode()) {
8061 case Instruction::Freeze:
8062 case Instruction::PHI:
8063 case Instruction::Invoke:
8064 return false;
8065 case Instruction::Select:
8066 return PoisonOp.getOperandNo() == 0;
8067 case Instruction::Call:
8068 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
8069 switch (II->getIntrinsicID()) {
8070 // TODO: Add more intrinsics.
8071 case Intrinsic::sadd_with_overflow:
8072 case Intrinsic::ssub_with_overflow:
8073 case Intrinsic::smul_with_overflow:
8074 case Intrinsic::uadd_with_overflow:
8075 case Intrinsic::usub_with_overflow:
8076 case Intrinsic::umul_with_overflow:
8077 // If an input is a vector containing a poison element, the
8078 // two output vectors (calculated results, overflow bits)'
8079 // corresponding lanes are poison.
8080 return true;
8081 case Intrinsic::ctpop:
8082 case Intrinsic::ctlz:
8083 case Intrinsic::cttz:
8084 case Intrinsic::abs:
8085 case Intrinsic::smax:
8086 case Intrinsic::smin:
8087 case Intrinsic::umax:
8088 case Intrinsic::umin:
8089 case Intrinsic::bitreverse:
8090 case Intrinsic::bswap:
8091 case Intrinsic::sadd_sat:
8092 case Intrinsic::ssub_sat:
8093 case Intrinsic::sshl_sat:
8094 case Intrinsic::uadd_sat:
8095 case Intrinsic::usub_sat:
8096 case Intrinsic::ushl_sat:
8097 return true;
8098 }
8099 }
8100 return false;
8101 case Instruction::ICmp:
8102 case Instruction::FCmp:
8103 case Instruction::GetElementPtr:
8104 return true;
8105 default:
8106 if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I))
8107 return true;
8108
8109 // Be conservative and return false.
8110 return false;
8111 }
8112}
8113
8114/// Enumerates all operands of \p I that are guaranteed to not be undef or
8115/// poison. If the callback \p Handle returns true, stop processing and return
8116/// true. Otherwise, return false.
8117template <typename CallableT>
8119 const CallableT &Handle) {
8120 switch (I->getOpcode()) {
8121 case Instruction::Store:
8122 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8123 return true;
8124 break;
8125
8126 case Instruction::Load:
8127 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8128 return true;
8129 break;
8130
8131 // Since dereferenceable attribute imply noundef, atomic operations
8132 // also implicitly have noundef pointers too
8133 case Instruction::AtomicCmpXchg:
8134 if (Handle(cast<AtomicCmpXchgInst>(I)->getPointerOperand()))
8135 return true;
8136 break;
8137
8138 case Instruction::AtomicRMW:
8139 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8140 return true;
8141 break;
8142
8143 case Instruction::Call:
8144 case Instruction::Invoke: {
8145 const CallBase *CB = cast<CallBase>(I);
8146 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8147 return true;
8148 for (unsigned i = 0; i < CB->arg_size(); ++i)
8149 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8150 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8151 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8152 Handle(CB->getArgOperand(i)))
8153 return true;
8154 break;
8155 }
8156 case Instruction::Ret:
8157 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8158 Handle(I->getOperand(0)))
8159 return true;
8160 break;
8161 case Instruction::Switch:
8162 if (Handle(cast<SwitchInst>(I)->getCondition()))
8163 return true;
8164 break;
8165 case Instruction::Br: {
8166 auto *BR = cast<BranchInst>(I);
8167 if (BR->isConditional() && Handle(BR->getCondition()))
8168 return true;
8169 break;
8170 }
8171 default:
8172 break;
8173 }
8174
8175 return false;
8176}
8177
8180 handleGuaranteedWellDefinedOps(I, [&](const Value *V) {
8181 Operands.push_back(V);
8182 return false;
8183 });
8184}
8185
8186/// Enumerates all operands of \p I that are guaranteed to not be poison.
8187template <typename CallableT>
8189 const CallableT &Handle) {
8190 if (handleGuaranteedWellDefinedOps(I, Handle))
8191 return true;
8192 switch (I->getOpcode()) {
8193 // Divisors of these operations are allowed to be partially undef.
8194 case Instruction::UDiv:
8195 case Instruction::SDiv:
8196 case Instruction::URem:
8197 case Instruction::SRem:
8198 return Handle(I->getOperand(1));
8199 default:
8200 return false;
8201 }
8202}
8203
8206 handleGuaranteedNonPoisonOps(I, [&](const Value *V) {
8207 Operands.push_back(V);
8208 return false;
8209 });
8210}
8211
8213 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8215 I, [&](const Value *V) { return KnownPoison.count(V); });
8216}
8217
8219 bool PoisonOnly) {
8220 // We currently only look for uses of values within the same basic
8221 // block, as that makes it easier to guarantee that the uses will be
8222 // executed given that Inst is executed.
8223 //
8224 // FIXME: Expand this to consider uses beyond the same basic block. To do
8225 // this, look out for the distinction between post-dominance and strong
8226 // post-dominance.
8227 const BasicBlock *BB = nullptr;
8229 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8230 BB = Inst->getParent();
8231 Begin = Inst->getIterator();
8232 Begin++;
8233 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8234 if (Arg->getParent()->isDeclaration())
8235 return false;
8236 BB = &Arg->getParent()->getEntryBlock();
8237 Begin = BB->begin();
8238 } else {
8239 return false;
8240 }
8241
8242 // Limit number of instructions we look at, to avoid scanning through large
8243 // blocks. The current limit is chosen arbitrarily.
8244 unsigned ScanLimit = 32;
8246
8247 if (!PoisonOnly) {
8248 // Since undef does not propagate eagerly, be conservative & just check
8249 // whether a value is directly passed to an instruction that must take
8250 // well-defined operands.
8251
8252 for (const auto &I : make_range(Begin, End)) {
8253 if (isa<DbgInfoIntrinsic>(I))
8254 continue;
8255 if (--ScanLimit == 0)
8256 break;
8257
8258 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8259 return WellDefinedOp == V;
8260 }))
8261 return true;
8262
8264 break;
8265 }
8266 return false;
8267 }
8268
8269 // Set of instructions that we have proved will yield poison if Inst
8270 // does.
8271 SmallSet<const Value *, 16> YieldsPoison;
8273
8274 YieldsPoison.insert(V);
8275 Visited.insert(BB);
8276
8277 while (true) {
8278 for (const auto &I : make_range(Begin, End)) {
8279 if (isa<DbgInfoIntrinsic>(I))
8280 continue;
8281 if (--ScanLimit == 0)
8282 return false;
8283 if (mustTriggerUB(&I, YieldsPoison))
8284 return true;
8286 return false;
8287
8288 // If an operand is poison and propagates it, mark I as yielding poison.
8289 for (const Use &Op : I.operands()) {
8290 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8291 YieldsPoison.insert(&I);
8292 break;
8293 }
8294 }
8295
8296 // Special handling for select, which returns poison if its operand 0 is
8297 // poison (handled in the loop above) *or* if both its true/false operands
8298 // are poison (handled here).
8299 if (I.getOpcode() == Instruction::Select &&
8300 YieldsPoison.count(I.getOperand(1)) &&
8301 YieldsPoison.count(I.getOperand(2))) {
8302 YieldsPoison.insert(&I);
8303 }
8304 }
8305
8306 BB = BB->getSingleSuccessor();
8307 if (!BB || !Visited.insert(BB).second)
8308 break;
8309
8310 Begin = BB->getFirstNonPHIIt();
8311 End = BB->end();
8312 }
8313 return false;
8314}
8315
8317 return ::programUndefinedIfUndefOrPoison(Inst, false);
8318}
8319
8321 return ::programUndefinedIfUndefOrPoison(Inst, true);
8322}
8323
8324static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8325 if (FMF.noNaNs())
8326 return true;
8327
8328 if (auto *C = dyn_cast<ConstantFP>(V))
8329 return !C->isNaN();
8330
8331 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8332 if (!C->getElementType()->isFloatingPointTy())
8333 return false;
8334 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8335 if (C->getElementAsAPFloat(I).isNaN())
8336 return false;
8337 }
8338 return true;
8339 }
8340
8341 if (isa<ConstantAggregateZero>(V))
8342 return true;
8343
8344 return false;
8345}
8346
8347static bool isKnownNonZero(const Value *V) {
8348 if (auto *C = dyn_cast<ConstantFP>(V))
8349 return !C->isZero();
8350
8351 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8352 if (!C->getElementType()->isFloatingPointTy())
8353 return false;
8354 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8355 if (C->getElementAsAPFloat(I).isZero())
8356 return false;
8357 }
8358 return true;
8359 }
8360
8361 return false;
8362}
8363
8364/// Match clamp pattern for float types without care about NaNs or signed zeros.
8365/// Given non-min/max outer cmp/select from the clamp pattern this
8366/// function recognizes if it can be substitued by a "canonical" min/max
8367/// pattern.
8369 Value *CmpLHS, Value *CmpRHS,
8370 Value *TrueVal, Value *FalseVal,
8371 Value *&LHS, Value *&RHS) {
8372 // Try to match
8373 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8374 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8375 // and return description of the outer Max/Min.
8376
8377 // First, check if select has inverse order:
8378 if (CmpRHS == FalseVal) {
8379 std::swap(TrueVal, FalseVal);
8380 Pred = CmpInst::getInversePredicate(Pred);
8381 }
8382
8383 // Assume success now. If there's no match, callers should not use these anyway.
8384 LHS = TrueVal;
8385 RHS = FalseVal;
8386
8387 const APFloat *FC1;
8388 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8389 return {SPF_UNKNOWN, SPNB_NA, false};
8390
8391 const APFloat *FC2;
8392 switch (Pred) {
8393 case CmpInst::FCMP_OLT:
8394 case CmpInst::FCMP_OLE:
8395 case CmpInst::FCMP_ULT:
8396 case CmpInst::FCMP_ULE:
8397 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8398 *FC1 < *FC2)
8399 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8400 break;
8401 case CmpInst::FCMP_OGT:
8402 case CmpInst::FCMP_OGE:
8403 case CmpInst::FCMP_UGT:
8404 case CmpInst::FCMP_UGE:
8405 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8406 *FC1 > *FC2)
8407 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8408 break;
8409 default:
8410 break;
8411 }
8412
8413 return {SPF_UNKNOWN, SPNB_NA, false};
8414}
8415
8416/// Recognize variations of:
8417/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8419 Value *CmpLHS, Value *CmpRHS,
8420 Value *TrueVal, Value *FalseVal) {
8421 // Swap the select operands and predicate to match the patterns below.
8422 if (CmpRHS != TrueVal) {
8423 Pred = ICmpInst::getSwappedPredicate(Pred);
8424 std::swap(TrueVal, FalseVal);
8425 }
8426 const APInt *C1;
8427 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8428 const APInt *C2;
8429 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8430 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8431 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8432 return {SPF_SMAX, SPNB_NA, false};
8433
8434 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8435 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8436 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8437 return {SPF_SMIN, SPNB_NA, false};
8438
8439 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8440 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8441 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8442 return {SPF_UMAX, SPNB_NA, false};
8443
8444 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8445 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8446 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8447 return {SPF_UMIN, SPNB_NA, false};
8448 }
8449 return {SPF_UNKNOWN, SPNB_NA, false};
8450}
8451
8452/// Recognize variations of:
8453/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8455 Value *CmpLHS, Value *CmpRHS,
8456 Value *TVal, Value *FVal,
8457 unsigned Depth) {
8458 // TODO: Allow FP min/max with nnan/nsz.
8459 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8460
8461 Value *A = nullptr, *B = nullptr;
8462 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8463 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8464 return {SPF_UNKNOWN, SPNB_NA, false};
8465
8466 Value *C = nullptr, *D = nullptr;
8467 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8468 if (L.Flavor != R.Flavor)
8469 return {SPF_UNKNOWN, SPNB_NA, false};
8470
8471 // We have something like: x Pred y ? min(a, b) : min(c, d).
8472 // Try to match the compare to the min/max operations of the select operands.
8473 // First, make sure we have the right compare predicate.
8474 switch (L.Flavor) {
8475 case SPF_SMIN:
8476 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8477 Pred = ICmpInst::getSwappedPredicate(Pred);
8478 std::swap(CmpLHS, CmpRHS);
8479 }
8480 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8481 break;
8482 return {SPF_UNKNOWN, SPNB_NA, false};
8483 case SPF_SMAX:
8484 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8485 Pred = ICmpInst::getSwappedPredicate(Pred);
8486 std::swap(CmpLHS, CmpRHS);
8487 }
8488 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8489 break;
8490 return {SPF_UNKNOWN, SPNB_NA, false};
8491 case SPF_UMIN:
8492 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8493 Pred = ICmpInst::getSwappedPredicate(Pred);
8494 std::swap(CmpLHS, CmpRHS);
8495 }
8496 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8497 break;
8498 return {SPF_UNKNOWN, SPNB_NA, false};
8499 case SPF_UMAX:
8500 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8501 Pred = ICmpInst::getSwappedPredicate(Pred);
8502 std::swap(CmpLHS, CmpRHS);
8503 }
8504 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8505 break;
8506 return {SPF_UNKNOWN, SPNB_NA, false};
8507 default:
8508 return {SPF_UNKNOWN, SPNB_NA, false};
8509 }
8510
8511 // If there is a common operand in the already matched min/max and the other
8512 // min/max operands match the compare operands (either directly or inverted),
8513 // then this is min/max of the same flavor.
8514
8515 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8516 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8517 if (D == B) {
8518 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8519 match(A, m_Not(m_Specific(CmpRHS)))))
8520 return {L.Flavor, SPNB_NA, false};
8521 }
8522 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8523 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8524 if (C == B) {
8525 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8526 match(A, m_Not(m_Specific(CmpRHS)))))
8527 return {L.Flavor, SPNB_NA, false};
8528 }
8529 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8530 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8531 if (D == A) {
8532 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8533 match(B, m_Not(m_Specific(CmpRHS)))))
8534 return {L.Flavor, SPNB_NA, false};
8535 }
8536 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8537 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8538 if (C == A) {
8539 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8540 match(B, m_Not(m_Specific(CmpRHS)))))
8541 return {L.Flavor, SPNB_NA, false};
8542 }
8543
8544 return {SPF_UNKNOWN, SPNB_NA, false};
8545}
8546
8547/// If the input value is the result of a 'not' op, constant integer, or vector
8548/// splat of a constant integer, return the bitwise-not source value.
8549/// TODO: This could be extended to handle non-splat vector integer constants.
8551 Value *NotV;
8552 if (match(V, m_Not(m_Value(NotV))))
8553 return NotV;
8554
8555 const APInt *C;
8556 if (match(V, m_APInt(C)))
8557 return ConstantInt::get(V->getType(), ~(*C));
8558
8559 return nullptr;
8560}
8561
8562/// Match non-obvious integer minimum and maximum sequences.
8564 Value *CmpLHS, Value *CmpRHS,
8565 Value *TrueVal, Value *FalseVal,
8566 Value *&LHS, Value *&RHS,
8567 unsigned Depth) {
8568 // Assume success. If there's no match, callers should not use these anyway.
8569 LHS = TrueVal;
8570 RHS = FalseVal;
8571
8572 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8574 return SPR;
8575
8576 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8578 return SPR;
8579
8580 // Look through 'not' ops to find disguised min/max.
8581 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8582 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8583 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8584 switch (Pred) {
8585 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8586 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8587 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8588 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8589 default: break;
8590 }
8591 }
8592
8593 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8594 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8595 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8596 switch (Pred) {
8597 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8598 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8599 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8600 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8601 default: break;
8602 }
8603 }
8604
8605 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8606 return {SPF_UNKNOWN, SPNB_NA, false};
8607
8608 const APInt *C1;
8609 if (!match(CmpRHS, m_APInt(C1)))
8610 return {SPF_UNKNOWN, SPNB_NA, false};
8611
8612 // An unsigned min/max can be written with a signed compare.
8613 const APInt *C2;
8614 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8615 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8616 // Is the sign bit set?
8617 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8618 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8619 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8620 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8621
8622 // Is the sign bit clear?
8623 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8624 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8625 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8626 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8627 }
8628
8629 return {SPF_UNKNOWN, SPNB_NA, false};
8630}
8631
8632bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8633 bool AllowPoison) {
8634 assert(X && Y && "Invalid operand");
8635
8636 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8637 if (!match(X, m_Neg(m_Specific(Y))))
8638 return false;
8639
8640 auto *BO = cast<BinaryOperator>(X);
8641 if (NeedNSW && !BO->hasNoSignedWrap())
8642 return false;
8643
8644 auto *Zero = cast<Constant>(BO->getOperand(0));
8645 if (!AllowPoison && !Zero->isNullValue())
8646 return false;
8647
8648 return true;
8649 };
8650
8651 // X = -Y or Y = -X
8652 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8653 return true;
8654
8655 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8656 Value *A, *B;
8657 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8658 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8659 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8661}
8662
8663bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8664 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8665 Value *A, *B, *C;
8666 CmpPredicate Pred1, Pred2;
8667 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8668 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8669 return false;
8670
8671 // They must both have samesign flag or not.
8672 if (Pred1.hasSameSign() != Pred2.hasSameSign())
8673 return false;
8674
8675 if (B == C)
8676 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8677
8678 // Try to infer the relationship from constant ranges.
8679 const APInt *RHSC1, *RHSC2;
8680 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8681 return false;
8682
8683 // Sign bits of two RHSCs should match.
8684 if (Pred1.hasSameSign() && RHSC1->isNonNegative() != RHSC2->isNonNegative())
8685 return false;
8686
8687 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8688 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8689
8690 return CR1.inverse() == CR2;
8691}
8692
8694 SelectPatternNaNBehavior NaNBehavior,
8695 bool Ordered) {
8696 switch (Pred) {
8697 default:
8698 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8699 case ICmpInst::ICMP_UGT:
8700 case ICmpInst::ICMP_UGE:
8701 return {SPF_UMAX, SPNB_NA, false};
8702 case ICmpInst::ICMP_SGT:
8703 case ICmpInst::ICMP_SGE:
8704 return {SPF_SMAX, SPNB_NA, false};
8705 case ICmpInst::ICMP_ULT:
8706 case ICmpInst::ICMP_ULE:
8707 return {SPF_UMIN, SPNB_NA, false};
8708 case ICmpInst::ICMP_SLT:
8709 case ICmpInst::ICMP_SLE:
8710 return {SPF_SMIN, SPNB_NA, false};
8711 case FCmpInst::FCMP_UGT:
8712 case FCmpInst::FCMP_UGE:
8713 case FCmpInst::FCMP_OGT:
8714 case FCmpInst::FCMP_OGE:
8715 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8716 case FCmpInst::FCMP_ULT:
8717 case FCmpInst::FCMP_ULE:
8718 case FCmpInst::FCMP_OLT:
8719 case FCmpInst::FCMP_OLE:
8720 return {SPF_FMINNUM, NaNBehavior, Ordered};
8721 }
8722}
8723
8724std::optional<std::pair<CmpPredicate, Constant *>>
8727 "Only for relational integer predicates.");
8728 if (isa<UndefValue>(C))
8729 return std::nullopt;
8730
8731 Type *Type = C->getType();
8732 bool IsSigned = ICmpInst::isSigned(Pred);
8733
8735 bool WillIncrement =
8736 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8737
8738 // Check if the constant operand can be safely incremented/decremented
8739 // without overflowing/underflowing.
8740 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8741 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8742 };
8743
8744 Constant *SafeReplacementConstant = nullptr;
8745 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8746 // Bail out if the constant can't be safely incremented/decremented.
8747 if (!ConstantIsOk(CI))
8748 return std::nullopt;
8749 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8750 unsigned NumElts = FVTy->getNumElements();
8751 for (unsigned i = 0; i != NumElts; ++i) {
8752 Constant *Elt = C->getAggregateElement(i);
8753 if (!Elt)
8754 return std::nullopt;
8755
8756 if (isa<UndefValue>(Elt))
8757 continue;
8758
8759 // Bail out if we can't determine if this constant is min/max or if we
8760 // know that this constant is min/max.
8761 auto *CI = dyn_cast<ConstantInt>(Elt);
8762 if (!CI || !ConstantIsOk(CI))
8763 return std::nullopt;
8764
8765 if (!SafeReplacementConstant)
8766 SafeReplacementConstant = CI;
8767 }
8768 } else if (isa<VectorType>(C->getType())) {
8769 // Handle scalable splat
8770 Value *SplatC = C->getSplatValue();
8771 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8772 // Bail out if the constant can't be safely incremented/decremented.
8773 if (!CI || !ConstantIsOk(CI))
8774 return std::nullopt;
8775 } else {
8776 // ConstantExpr?
8777 return std::nullopt;
8778 }
8779
8780 // It may not be safe to change a compare predicate in the presence of
8781 // undefined elements, so replace those elements with the first safe constant
8782 // that we found.
8783 // TODO: in case of poison, it is safe; let's replace undefs only.
8784 if (C->containsUndefOrPoisonElement()) {
8785 assert(SafeReplacementConstant && "Replacement constant not set");
8786 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8787 }
8788
8790
8791 // Increment or decrement the constant.
8792 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8793 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8794
8795 return std::make_pair(NewPred, NewC);
8796}
8797
8799 FastMathFlags FMF,
8800 Value *CmpLHS, Value *CmpRHS,
8801 Value *TrueVal, Value *FalseVal,
8802 Value *&LHS, Value *&RHS,
8803 unsigned Depth) {
8804 bool HasMismatchedZeros = false;
8805 if (CmpInst::isFPPredicate(Pred)) {
8806 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8807 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8808 // purpose of identifying min/max. Disregard vector constants with undefined
8809 // elements because those can not be back-propagated for analysis.
8810 Value *OutputZeroVal = nullptr;
8811 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8812 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8813 OutputZeroVal = TrueVal;
8814 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8815 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8816 OutputZeroVal = FalseVal;
8817
8818 if (OutputZeroVal) {
8819 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8820 HasMismatchedZeros = true;
8821 CmpLHS = OutputZeroVal;
8822 }
8823 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8824 HasMismatchedZeros = true;
8825 CmpRHS = OutputZeroVal;
8826 }
8827 }
8828 }
8829
8830 LHS = CmpLHS;
8831 RHS = CmpRHS;
8832
8833 // Signed zero may return inconsistent results between implementations.
8834 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8835 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8836 // Therefore, we behave conservatively and only proceed if at least one of the
8837 // operands is known to not be zero or if we don't care about signed zero.
8838 switch (Pred) {
8839 default: break;
8842 if (!HasMismatchedZeros)
8843 break;
8844 [[fallthrough]];
8847 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8848 !isKnownNonZero(CmpRHS))
8849 return {SPF_UNKNOWN, SPNB_NA, false};
8850 }
8851
8852 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8853 bool Ordered = false;
8854
8855 // When given one NaN and one non-NaN input:
8856 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8857 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8858 // ordered comparison fails), which could be NaN or non-NaN.
8859 // so here we discover exactly what NaN behavior is required/accepted.
8860 if (CmpInst::isFPPredicate(Pred)) {
8861 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8862 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8863
8864 if (LHSSafe && RHSSafe) {
8865 // Both operands are known non-NaN.
8866 NaNBehavior = SPNB_RETURNS_ANY;
8867 } else if (CmpInst::isOrdered(Pred)) {
8868 // An ordered comparison will return false when given a NaN, so it
8869 // returns the RHS.
8870 Ordered = true;
8871 if (LHSSafe)
8872 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8873 NaNBehavior = SPNB_RETURNS_NAN;
8874 else if (RHSSafe)
8875 NaNBehavior = SPNB_RETURNS_OTHER;
8876 else
8877 // Completely unsafe.
8878 return {SPF_UNKNOWN, SPNB_NA, false};
8879 } else {
8880 Ordered = false;
8881 // An unordered comparison will return true when given a NaN, so it
8882 // returns the LHS.
8883 if (LHSSafe)
8884 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8885 NaNBehavior = SPNB_RETURNS_OTHER;
8886 else if (RHSSafe)
8887 NaNBehavior = SPNB_RETURNS_NAN;
8888 else
8889 // Completely unsafe.
8890 return {SPF_UNKNOWN, SPNB_NA, false};
8891 }
8892 }
8893
8894 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8895 std::swap(CmpLHS, CmpRHS);
8896 Pred = CmpInst::getSwappedPredicate(Pred);
8897 if (NaNBehavior == SPNB_RETURNS_NAN)
8898 NaNBehavior = SPNB_RETURNS_OTHER;
8899 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8900 NaNBehavior = SPNB_RETURNS_NAN;
8901 Ordered = !Ordered;
8902 }
8903
8904 // ([if]cmp X, Y) ? X : Y
8905 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8906 return getSelectPattern(Pred, NaNBehavior, Ordered);
8907
8908 if (isKnownNegation(TrueVal, FalseVal)) {
8909 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8910 // match against either LHS or sext(LHS).
8911 auto MaybeSExtCmpLHS =
8912 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8913 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8914 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8915 if (match(TrueVal, MaybeSExtCmpLHS)) {
8916 // Set the return values. If the compare uses the negated value (-X >s 0),
8917 // swap the return values because the negated value is always 'RHS'.
8918 LHS = TrueVal;
8919 RHS = FalseVal;
8920 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8921 std::swap(LHS, RHS);
8922
8923 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8924 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8925 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8926 return {SPF_ABS, SPNB_NA, false};
8927
8928 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8929 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8930 return {SPF_ABS, SPNB_NA, false};
8931
8932 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8933 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8934 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8935 return {SPF_NABS, SPNB_NA, false};
8936 }
8937 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8938 // Set the return values. If the compare uses the negated value (-X >s 0),
8939 // swap the return values because the negated value is always 'RHS'.
8940 LHS = FalseVal;
8941 RHS = TrueVal;
8942 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8943 std::swap(LHS, RHS);
8944
8945 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8946 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8947 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8948 return {SPF_NABS, SPNB_NA, false};
8949
8950 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8951 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8952 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8953 return {SPF_ABS, SPNB_NA, false};
8954 }
8955 }
8956
8957 if (CmpInst::isIntPredicate(Pred))
8958 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8959
8960 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8961 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8962 // semantics than minNum. Be conservative in such case.
8963 if (NaNBehavior != SPNB_RETURNS_ANY ||
8964 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8965 !isKnownNonZero(CmpRHS)))
8966 return {SPF_UNKNOWN, SPNB_NA, false};
8967
8968 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8969}
8970
8972 Instruction::CastOps *CastOp) {
8973 const DataLayout &DL = CmpI->getDataLayout();
8974
8975 Constant *CastedTo = nullptr;
8976 switch (*CastOp) {
8977 case Instruction::ZExt:
8978 if (CmpI->isUnsigned())
8979 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8980 break;
8981 case Instruction::SExt:
8982 if (CmpI->isSigned())
8983 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8984 break;
8985 case Instruction::Trunc:
8986 Constant *CmpConst;
8987 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8988 CmpConst->getType() == SrcTy) {
8989 // Here we have the following case:
8990 //
8991 // %cond = cmp iN %x, CmpConst
8992 // %tr = trunc iN %x to iK
8993 // %narrowsel = select i1 %cond, iK %t, iK C
8994 //
8995 // We can always move trunc after select operation:
8996 //
8997 // %cond = cmp iN %x, CmpConst
8998 // %widesel = select i1 %cond, iN %x, iN CmpConst
8999 // %tr = trunc iN %widesel to iK
9000 //
9001 // Note that C could be extended in any way because we don't care about
9002 // upper bits after truncation. It can't be abs pattern, because it would
9003 // look like:
9004 //
9005 // select i1 %cond, x, -x.
9006 //
9007 // So only min/max pattern could be matched. Such match requires widened C
9008 // == CmpConst. That is why set widened C = CmpConst, condition trunc
9009 // CmpConst == C is checked below.
9010 CastedTo = CmpConst;
9011 } else {
9012 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
9013 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
9014 }
9015 break;
9016 case Instruction::FPTrunc:
9017 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
9018 break;
9019 case Instruction::FPExt:
9020 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
9021 break;
9022 case Instruction::FPToUI:
9023 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
9024 break;
9025 case Instruction::FPToSI:
9026 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
9027 break;
9028 case Instruction::UIToFP:
9029 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
9030 break;
9031 case Instruction::SIToFP:
9032 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
9033 break;
9034 default:
9035 break;
9036 }
9037
9038 if (!CastedTo)
9039 return nullptr;
9040
9041 // Make sure the cast doesn't lose any information.
9042 Constant *CastedBack =
9043 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
9044 if (CastedBack && CastedBack != C)
9045 return nullptr;
9046
9047 return CastedTo;
9048}
9049
9050/// Helps to match a select pattern in case of a type mismatch.
9051///
9052/// The function processes the case when type of true and false values of a
9053/// select instruction differs from type of the cmp instruction operands because
9054/// of a cast instruction. The function checks if it is legal to move the cast
9055/// operation after "select". If yes, it returns the new second value of
9056/// "select" (with the assumption that cast is moved):
9057/// 1. As operand of cast instruction when both values of "select" are same cast
9058/// instructions.
9059/// 2. As restored constant (by applying reverse cast operation) when the first
9060/// value of the "select" is a cast operation and the second value is a
9061/// constant. It is implemented in lookThroughCastConst().
9062/// 3. As one operand is cast instruction and the other is not. The operands in
9063/// sel(cmp) are in different type integer.
9064/// NOTE: We return only the new second value because the first value could be
9065/// accessed as operand of cast instruction.
9066static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9067 Instruction::CastOps *CastOp) {
9068 auto *Cast1 = dyn_cast<CastInst>(V1);
9069 if (!Cast1)
9070 return nullptr;
9071
9072 *CastOp = Cast1->getOpcode();
9073 Type *SrcTy = Cast1->getSrcTy();
9074 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9075 // If V1 and V2 are both the same cast from the same type, look through V1.
9076 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9077 return Cast2->getOperand(0);
9078 return nullptr;
9079 }
9080
9081 auto *C = dyn_cast<Constant>(V2);
9082 if (C)
9083 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9084
9085 Value *CastedTo = nullptr;
9086 if (*CastOp == Instruction::Trunc) {
9087 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9088 // Here we have the following case:
9089 // %y_ext = sext iK %y to iN
9090 // %cond = cmp iN %x, %y_ext
9091 // %tr = trunc iN %x to iK
9092 // %narrowsel = select i1 %cond, iK %tr, iK %y
9093 //
9094 // We can always move trunc after select operation:
9095 // %y_ext = sext iK %y to iN
9096 // %cond = cmp iN %x, %y_ext
9097 // %widesel = select i1 %cond, iN %x, iN %y_ext
9098 // %tr = trunc iN %widesel to iK
9099 assert(V2->getType() == Cast1->getType() &&
9100 "V2 and Cast1 should be the same type.");
9101 CastedTo = CmpI->getOperand(1);
9102 }
9103 }
9104
9105 return CastedTo;
9106}
9108 Instruction::CastOps *CastOp,
9109 unsigned Depth) {
9111 return {SPF_UNKNOWN, SPNB_NA, false};
9112
9113 SelectInst *SI = dyn_cast<SelectInst>(V);
9114 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9115
9116 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9117 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9118
9119 Value *TrueVal = SI->getTrueValue();
9120 Value *FalseVal = SI->getFalseValue();
9121
9122 return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
9123 CastOp, Depth);
9124}
9125
9127 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9128 Instruction::CastOps *CastOp, unsigned Depth) {
9129 CmpInst::Predicate Pred = CmpI->getPredicate();
9130 Value *CmpLHS = CmpI->getOperand(0);
9131 Value *CmpRHS = CmpI->getOperand(1);
9132 FastMathFlags FMF;
9133 if (isa<FPMathOperator>(CmpI))
9134 FMF = CmpI->getFastMathFlags();
9135
9136 // Bail out early.
9137 if (CmpI->isEquality())
9138 return {SPF_UNKNOWN, SPNB_NA, false};
9139
9140 // Deal with type mismatches.
9141 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9142 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9143 // If this is a potential fmin/fmax with a cast to integer, then ignore
9144 // -0.0 because there is no corresponding integer value.
9145 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9146 FMF.setNoSignedZeros();
9147 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9148 cast<CastInst>(TrueVal)->getOperand(0), C,
9149 LHS, RHS, Depth);
9150 }
9151 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9152 // If this is a potential fmin/fmax with a cast to integer, then ignore
9153 // -0.0 because there is no corresponding integer value.
9154 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9155 FMF.setNoSignedZeros();
9156 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9157 C, cast<CastInst>(FalseVal)->getOperand(0),
9158 LHS, RHS, Depth);
9159 }
9160 }
9161 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9162 LHS, RHS, Depth);
9163}
9164
9166 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9167 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9168 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9169 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9170 if (SPF == SPF_FMINNUM)
9171 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9172 if (SPF == SPF_FMAXNUM)
9173 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9174 llvm_unreachable("unhandled!");
9175}
9176
9178 switch (SPF) {
9180 return Intrinsic::umin;
9182 return Intrinsic::umax;
9184 return Intrinsic::smin;
9186 return Intrinsic::smax;
9187 default:
9188 llvm_unreachable("Unexpected SPF");
9189 }
9190}
9191
9193 if (SPF == SPF_SMIN) return SPF_SMAX;
9194 if (SPF == SPF_UMIN) return SPF_UMAX;
9195 if (SPF == SPF_SMAX) return SPF_SMIN;
9196 if (SPF == SPF_UMAX) return SPF_UMIN;
9197 llvm_unreachable("unhandled!");
9198}
9199
9201 switch (MinMaxID) {
9202 case Intrinsic::smax: return Intrinsic::smin;
9203 case Intrinsic::smin: return Intrinsic::smax;
9204 case Intrinsic::umax: return Intrinsic::umin;
9205 case Intrinsic::umin: return Intrinsic::umax;
9206 // Please note that next four intrinsics may produce the same result for
9207 // original and inverted case even if X != Y due to NaN is handled specially.
9208 case Intrinsic::maximum: return Intrinsic::minimum;
9209 case Intrinsic::minimum: return Intrinsic::maximum;
9210 case Intrinsic::maxnum: return Intrinsic::minnum;
9211 case Intrinsic::minnum: return Intrinsic::maxnum;
9212 default: llvm_unreachable("Unexpected intrinsic");
9213 }
9214}
9215
9217 switch (SPF) {
9220 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9221 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9222 default: llvm_unreachable("Unexpected flavor");
9223 }
9224}
9225
9226std::pair<Intrinsic::ID, bool>
9228 // Check if VL contains select instructions that can be folded into a min/max
9229 // vector intrinsic and return the intrinsic if it is possible.
9230 // TODO: Support floating point min/max.
9231 bool AllCmpSingleUse = true;
9232 SelectPatternResult SelectPattern;
9233 SelectPattern.Flavor = SPF_UNKNOWN;
9234 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9235 Value *LHS, *RHS;
9236 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9237 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9238 return false;
9239 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9240 SelectPattern.Flavor != CurrentPattern.Flavor)
9241 return false;
9242 SelectPattern = CurrentPattern;
9243 AllCmpSingleUse &=
9245 return true;
9246 })) {
9247 switch (SelectPattern.Flavor) {
9248 case SPF_SMIN:
9249 return {Intrinsic::smin, AllCmpSingleUse};
9250 case SPF_UMIN:
9251 return {Intrinsic::umin, AllCmpSingleUse};
9252 case SPF_SMAX:
9253 return {Intrinsic::smax, AllCmpSingleUse};
9254 case SPF_UMAX:
9255 return {Intrinsic::umax, AllCmpSingleUse};
9256 case SPF_FMAXNUM:
9257 return {Intrinsic::maxnum, AllCmpSingleUse};
9258 case SPF_FMINNUM:
9259 return {Intrinsic::minnum, AllCmpSingleUse};
9260 default:
9261 llvm_unreachable("unexpected select pattern flavor");
9262 }
9263 }
9264 return {Intrinsic::not_intrinsic, false};
9265}
9266
9268 Value *&Start, Value *&Step) {
9269 // Handle the case of a simple two-predecessor recurrence PHI.
9270 // There's a lot more that could theoretically be done here, but
9271 // this is sufficient to catch some interesting cases.
9272 if (P->getNumIncomingValues() != 2)
9273 return false;
9274
9275 for (unsigned i = 0; i != 2; ++i) {
9276 Value *L = P->getIncomingValue(i);
9277 Value *R = P->getIncomingValue(!i);
9278 auto *LU = dyn_cast<BinaryOperator>(L);
9279 if (!LU)
9280 continue;
9281 unsigned Opcode = LU->getOpcode();
9282
9283 switch (Opcode) {
9284 default:
9285 continue;
9286 // TODO: Expand list -- xor, gep, uadd.sat etc.
9287 case Instruction::LShr:
9288 case Instruction::AShr:
9289 case Instruction::Shl:
9290 case Instruction::Add:
9291 case Instruction::Sub:
9292 case Instruction::UDiv:
9293 case Instruction::URem:
9294 case Instruction::And:
9295 case Instruction::Or:
9296 case Instruction::Mul:
9297 case Instruction::FMul: {
9298 Value *LL = LU->getOperand(0);
9299 Value *LR = LU->getOperand(1);
9300 // Find a recurrence.
9301 if (LL == P)
9302 L = LR;
9303 else if (LR == P)
9304 L = LL;
9305 else
9306 continue; // Check for recurrence with L and R flipped.
9307
9308 break; // Match!
9309 }
9310 };
9311
9312 // We have matched a recurrence of the form:
9313 // %iv = [R, %entry], [%iv.next, %backedge]
9314 // %iv.next = binop %iv, L
9315 // OR
9316 // %iv = [R, %entry], [%iv.next, %backedge]
9317 // %iv.next = binop L, %iv
9318 BO = LU;
9319 Start = R;
9320 Step = L;
9321 return true;
9322 }
9323 return false;
9324}
9325
9327 Value *&Start, Value *&Step) {
9328 BinaryOperator *BO = nullptr;
9329 P = dyn_cast<PHINode>(I->getOperand(0));
9330 if (!P)
9331 P = dyn_cast<PHINode>(I->getOperand(1));
9332 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9333}
9334
9335/// Return true if "icmp Pred LHS RHS" is always true.
9336static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
9337 const Value *RHS) {
9338 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9339 return true;
9340
9341 switch (Pred) {
9342 default:
9343 return false;
9344
9345 case CmpInst::ICMP_SLE: {
9346 const APInt *C;
9347
9348 // LHS s<= LHS +_{nsw} C if C >= 0
9349 // LHS s<= LHS | C if C >= 0
9350 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9352 return !C->isNegative();
9353
9354 // LHS s<= smax(LHS, V) for any V
9356 return true;
9357
9358 // smin(RHS, V) s<= RHS for any V
9360 return true;
9361
9362 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9363 const Value *X;
9364 const APInt *CLHS, *CRHS;
9365 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9367 return CLHS->sle(*CRHS);
9368
9369 return false;
9370 }
9371
9372 case CmpInst::ICMP_ULE: {
9373 // LHS u<= LHS +_{nuw} V for any V
9374 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9375 cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
9376 return true;
9377
9378 // LHS u<= LHS | V for any V
9379 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9380 return true;
9381
9382 // LHS u<= umax(LHS, V) for any V
9384 return true;
9385
9386 // RHS >> V u<= RHS for any V
9387 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9388 return true;
9389
9390 // RHS u/ C_ugt_1 u<= RHS
9391 const APInt *C;
9392 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9393 return true;
9394
9395 // RHS & V u<= RHS for any V
9397 return true;
9398
9399 // umin(RHS, V) u<= RHS for any V
9401 return true;
9402
9403 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9404 const Value *X;
9405 const APInt *CLHS, *CRHS;
9406 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9408 return CLHS->ule(*CRHS);
9409
9410 return false;
9411 }
9412 }
9413}
9414
9415/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9416/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9417static std::optional<bool>
9419 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9420 switch (Pred) {
9421 default:
9422 return std::nullopt;
9423
9424 case CmpInst::ICMP_SLT:
9425 case CmpInst::ICMP_SLE:
9426 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9428 return true;
9429 return std::nullopt;
9430
9431 case CmpInst::ICMP_SGT:
9432 case CmpInst::ICMP_SGE:
9433 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9435 return true;
9436 return std::nullopt;
9437
9438 case CmpInst::ICMP_ULT:
9439 case CmpInst::ICMP_ULE:
9440 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9442 return true;
9443 return std::nullopt;
9444
9445 case CmpInst::ICMP_UGT:
9446 case CmpInst::ICMP_UGE:
9447 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9449 return true;
9450 return std::nullopt;
9451 }
9452}
9453
9454/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9455/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9456/// Otherwise, return std::nullopt if we can't infer anything.
9457static std::optional<bool>
9459 CmpPredicate RPred, const ConstantRange &RCR) {
9460 auto CRImpliesPred = [&](ConstantRange CR,
9461 CmpInst::Predicate Pred) -> std::optional<bool> {
9462 // If all true values for lhs and true for rhs, lhs implies rhs
9463 if (CR.icmp(Pred, RCR))
9464 return true;
9465
9466 // If there is no overlap, lhs implies not rhs
9467 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9468 return false;
9469
9470 return std::nullopt;
9471 };
9472 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9473 RPred))
9474 return Res;
9475 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9477 : static_cast<CmpInst::Predicate>(LPred);
9479 : static_cast<CmpInst::Predicate>(RPred);
9480 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9481 RPred);
9482 }
9483 return std::nullopt;
9484}
9485
9486/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9487/// is true. Return false if LHS implies RHS is false. Otherwise, return
9488/// std::nullopt if we can't infer anything.
9489static std::optional<bool>
9490isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
9491 const Value *R1, const DataLayout &DL, bool LHSIsTrue) {
9492 Value *L0 = LHS->getOperand(0);
9493 Value *L1 = LHS->getOperand(1);
9494
9495 // The rest of the logic assumes the LHS condition is true. If that's not the
9496 // case, invert the predicate to make it so.
9497 CmpPredicate LPred =
9498 LHSIsTrue ? LHS->getCmpPredicate() : LHS->getInverseCmpPredicate();
9499
9500 // We can have non-canonical operands, so try to normalize any common operand
9501 // to L0/R0.
9502 if (L0 == R1) {
9503 std::swap(R0, R1);
9504 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9505 }
9506 if (R0 == L1) {
9507 std::swap(L0, L1);
9508 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9509 }
9510 if (L1 == R1) {
9511 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9512 if (L0 != R0 || match(L0, m_ImmConstant())) {
9513 std::swap(L0, L1);
9514 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9515 std::swap(R0, R1);
9516 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9517 }
9518 }
9519
9520 // See if we can infer anything if operand-0 matches and we have at least one
9521 // constant.
9522 const APInt *Unused;
9523 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9524 // Potential TODO: We could also further use the constant range of L0/R0 to
9525 // further constraint the constant ranges. At the moment this leads to
9526 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9527 // C1` (see discussion: D58633).
9529 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9530 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9532 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9533 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9534 // Even if L1/R1 are not both constant, we can still sometimes deduce
9535 // relationship from a single constant. For example X u> Y implies X != 0.
9536 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9537 return R;
9538 // If both L1/R1 were exact constant ranges and we didn't get anything
9539 // here, we won't be able to deduce this.
9540 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9541 return std::nullopt;
9542 }
9543
9544 // Can we infer anything when the two compares have matching operands?
9545 if (L0 == R0 && L1 == R1)
9546 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9547
9548 // It only really makes sense in the context of signed comparison for "X - Y
9549 // must be positive if X >= Y and no overflow".
9550 // Take SGT as an example: L0:x > L1:y and C >= 0
9551 // ==> R0:(x -nsw y) < R1:(-C) is false
9552 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9553 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9554 SignedLPred == ICmpInst::ICMP_SGE) &&
9555 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9556 if (match(R1, m_NonPositive()) &&
9557 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == false)
9558 return false;
9559 }
9560
9561 // Take SLT as an example: L0:x < L1:y and C <= 0
9562 // ==> R0:(x -nsw y) < R1:(-C) is true
9563 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9564 SignedLPred == ICmpInst::ICMP_SLE) &&
9565 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9566 if (match(R1, m_NonNegative()) &&
9567 ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) == true)
9568 return true;
9569 }
9570
9571 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9572 if (L0 == R0 &&
9573 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9574 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9575 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9576 return CmpPredicate::getMatching(LPred, RPred).has_value();
9577
9578 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9579 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9580
9581 return std::nullopt;
9582}
9583
9584/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9585/// false. Otherwise, return std::nullopt if we can't infer anything. We
9586/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9587/// instruction.
9588static std::optional<bool>
9590 const Value *RHSOp0, const Value *RHSOp1,
9591 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9592 // The LHS must be an 'or', 'and', or a 'select' instruction.
9593 assert((LHS->getOpcode() == Instruction::And ||
9594 LHS->getOpcode() == Instruction::Or ||
9595 LHS->getOpcode() == Instruction::Select) &&
9596 "Expected LHS to be 'and', 'or', or 'select'.");
9597
9598 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9599
9600 // If the result of an 'or' is false, then we know both legs of the 'or' are
9601 // false. Similarly, if the result of an 'and' is true, then we know both
9602 // legs of the 'and' are true.
9603 const Value *ALHS, *ARHS;
9604 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9605 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9606 // FIXME: Make this non-recursion.
9607 if (std::optional<bool> Implication = isImpliedCondition(
9608 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9609 return Implication;
9610 if (std::optional<bool> Implication = isImpliedCondition(
9611 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9612 return Implication;
9613 return std::nullopt;
9614 }
9615 return std::nullopt;
9616}
9617
9618std::optional<bool>
9620 const Value *RHSOp0, const Value *RHSOp1,
9621 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9622 // Bail out when we hit the limit.
9624 return std::nullopt;
9625
9626 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9627 // example.
9628 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9629 return std::nullopt;
9630
9632 "Expected integer type only!");
9633
9634 // Match not
9635 if (match(LHS, m_Not(m_Value(LHS))))
9636 LHSIsTrue = !LHSIsTrue;
9637
9638 // Both LHS and RHS are icmps.
9639 const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
9640 if (LHSCmp)
9641 return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
9642
9643 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9644 /// the RHS to be an icmp.
9645 /// FIXME: Add support for and/or/select on the RHS.
9646 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9647 if ((LHSI->getOpcode() == Instruction::And ||
9648 LHSI->getOpcode() == Instruction::Or ||
9649 LHSI->getOpcode() == Instruction::Select))
9650 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9651 Depth);
9652 }
9653 return std::nullopt;
9654}
9655
9656std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9657 const DataLayout &DL,
9658 bool LHSIsTrue, unsigned Depth) {
9659 // LHS ==> RHS by definition
9660 if (LHS == RHS)
9661 return LHSIsTrue;
9662
9663 // Match not
9664 bool InvertRHS = false;
9665 if (match(RHS, m_Not(m_Value(RHS)))) {
9666 if (LHS == RHS)
9667 return !LHSIsTrue;
9668 InvertRHS = true;
9669 }
9670
9671 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9672 if (auto Implied = isImpliedCondition(
9673 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9674 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9675 return InvertRHS ? !*Implied : *Implied;
9676 return std::nullopt;
9677 }
9678
9680 return std::nullopt;
9681
9682 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9683 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9684 const Value *RHS1, *RHS2;
9685 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9686 if (std::optional<bool> Imp =
9687 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9688 if (*Imp == true)
9689 return !InvertRHS;
9690 if (std::optional<bool> Imp =
9691 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9692 if (*Imp == true)
9693 return !InvertRHS;
9694 }
9695 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9696 if (std::optional<bool> Imp =
9697 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9698 if (*Imp == false)
9699 return InvertRHS;
9700 if (std::optional<bool> Imp =
9701 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9702 if (*Imp == false)
9703 return InvertRHS;
9704 }
9705
9706 return std::nullopt;
9707}
9708
9709// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9710// condition dominating ContextI or nullptr, if no condition is found.
9711static std::pair<Value *, bool>
9713 if (!ContextI || !ContextI->getParent())
9714 return {nullptr, false};
9715
9716 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9717 // dominator tree (eg, from a SimplifyQuery) instead?
9718 const BasicBlock *ContextBB = ContextI->getParent();
9719 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9720 if (!PredBB)
9721 return {nullptr, false};
9722
9723 // We need a conditional branch in the predecessor.
9724 Value *PredCond;
9725 BasicBlock *TrueBB, *FalseBB;
9726 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9727 return {nullptr, false};
9728
9729 // The branch should get simplified. Don't bother simplifying this condition.
9730 if (TrueBB == FalseBB)
9731 return {nullptr, false};
9732
9733 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9734 "Predecessor block does not point to successor?");
9735
9736 // Is this condition implied by the predecessor condition?
9737 return {PredCond, TrueBB == ContextBB};
9738}
9739
9740std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9741 const Instruction *ContextI,
9742 const DataLayout &DL) {
9743 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9744 auto PredCond = getDomPredecessorCondition(ContextI);
9745 if (PredCond.first)
9746 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9747 return std::nullopt;
9748}
9749
9751 const Value *LHS,
9752 const Value *RHS,
9753 const Instruction *ContextI,
9754 const DataLayout &DL) {
9755 auto PredCond = getDomPredecessorCondition(ContextI);
9756 if (PredCond.first)
9757 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9758 PredCond.second);
9759 return std::nullopt;
9760}
9761
9763 APInt &Upper, const InstrInfoQuery &IIQ,
9764 bool PreferSignedRange) {
9765 unsigned Width = Lower.getBitWidth();
9766 const APInt *C;
9767 switch (BO.getOpcode()) {
9768 case Instruction::Add:
9769 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9770 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9771 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9772
9773 // If the caller expects a signed compare, then try to use a signed range.
9774 // Otherwise if both no-wraps are set, use the unsigned range because it
9775 // is never larger than the signed range. Example:
9776 // "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9777 if (PreferSignedRange && HasNSW && HasNUW)
9778 HasNUW = false;
9779
9780 if (HasNUW) {
9781 // 'add nuw x, C' produces [C, UINT_MAX].
9782 Lower = *C;
9783 } else if (HasNSW) {
9784 if (C->isNegative()) {
9785 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9787 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9788 } else {
9789 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9790 Lower = APInt::getSignedMinValue(Width) + *C;
9791 Upper = APInt::getSignedMaxValue(Width) + 1;
9792 }
9793 }
9794 }
9795 break;
9796
9797 case Instruction::And:
9798 if (match(BO.getOperand(1), m_APInt(C)))
9799 // 'and x, C' produces [0, C].
9800 Upper = *C + 1;
9801 // X & -X is a power of two or zero. So we can cap the value at max power of
9802 // two.
9803 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9804 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9805 Upper = APInt::getSignedMinValue(Width) + 1;
9806 break;
9807
9808 case Instruction::Or:
9809 if (match(BO.getOperand(1), m_APInt(C)))
9810 // 'or x, C' produces [C, UINT_MAX].
9811 Lower = *C;
9812 break;
9813
9814 case Instruction::AShr:
9815 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9816 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9818 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9819 } else if (match(BO.getOperand(0), m_APInt(C))) {
9820 unsigned ShiftAmount = Width - 1;
9821 if (!C->isZero() && IIQ.isExact(&BO))
9822 ShiftAmount = C->countr_zero();
9823 if (C->isNegative()) {
9824 // 'ashr C, x' produces [C, C >> (Width-1)]
9825 Lower = *C;
9826 Upper = C->ashr(ShiftAmount) + 1;
9827 } else {
9828 // 'ashr C, x' produces [C >> (Width-1), C]
9829 Lower = C->ashr(ShiftAmount);
9830 Upper = *C + 1;
9831 }
9832 }
9833 break;
9834
9835 case Instruction::LShr:
9836 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9837 // 'lshr x, C' produces [0, UINT_MAX >> C].
9838 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9839 } else if (match(BO.getOperand(0), m_APInt(C))) {
9840 // 'lshr C, x' produces [C >> (Width-1), C].
9841 unsigned ShiftAmount = Width - 1;
9842 if (!C->isZero() && IIQ.isExact(&BO))
9843 ShiftAmount = C->countr_zero();
9844 Lower = C->lshr(ShiftAmount);
9845 Upper = *C + 1;
9846 }
9847 break;
9848
9849 case Instruction::Shl:
9850 if (match(BO.getOperand(0), m_APInt(C))) {
9851 if (IIQ.hasNoUnsignedWrap(&BO)) {
9852 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9853 Lower = *C;
9854 Upper = Lower.shl(Lower.countl_zero()) + 1;
9855 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9856 if (C->isNegative()) {
9857 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9858 unsigned ShiftAmount = C->countl_one() - 1;
9859 Lower = C->shl(ShiftAmount);
9860 Upper = *C + 1;
9861 } else {
9862 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9863 unsigned ShiftAmount = C->countl_zero() - 1;
9864 Lower = *C;
9865 Upper = C->shl(ShiftAmount) + 1;
9866 }
9867 } else {
9868 // If lowbit is set, value can never be zero.
9869 if ((*C)[0])
9870 Lower = APInt::getOneBitSet(Width, 0);
9871 // If we are shifting a constant the largest it can be is if the longest
9872 // sequence of consecutive ones is shifted to the highbits (breaking
9873 // ties for which sequence is higher). At the moment we take a liberal
9874 // upper bound on this by just popcounting the constant.
9875 // TODO: There may be a bitwise trick for it longest/highest
9876 // consecutative sequence of ones (naive method is O(Width) loop).
9877 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9878 }
9879 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9880 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9881 }
9882 break;
9883
9884 case Instruction::SDiv:
9885 if (match(BO.getOperand(1), m_APInt(C))) {
9886 APInt IntMin = APInt::getSignedMinValue(Width);
9887 APInt IntMax = APInt::getSignedMaxValue(Width);
9888 if (C->isAllOnes()) {
9889 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9890 // where C != -1 and C != 0 and C != 1
9891 Lower = IntMin + 1;
9892 Upper = IntMax + 1;
9893 } else if (C->countl_zero() < Width - 1) {
9894 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9895 // where C != -1 and C != 0 and C != 1
9896 Lower = IntMin.sdiv(*C);
9897 Upper = IntMax.sdiv(*C);
9898 if (Lower.sgt(Upper))
9900 Upper = Upper + 1;
9901 assert(Upper != Lower && "Upper part of range has wrapped!");
9902 }
9903 } else if (match(BO.getOperand(0), m_APInt(C))) {
9904 if (C->isMinSignedValue()) {
9905 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9906 Lower = *C;
9907 Upper = Lower.lshr(1) + 1;
9908 } else {
9909 // 'sdiv C, x' produces [-|C|, |C|].
9910 Upper = C->abs() + 1;
9911 Lower = (-Upper) + 1;
9912 }
9913 }
9914 break;
9915
9916 case Instruction::UDiv:
9917 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9918 // 'udiv x, C' produces [0, UINT_MAX / C].
9919 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9920 } else if (match(BO.getOperand(0), m_APInt(C))) {
9921 // 'udiv C, x' produces [0, C].
9922 Upper = *C + 1;
9923 }
9924 break;
9925
9926 case Instruction::SRem:
9927 if (match(BO.getOperand(1), m_APInt(C))) {
9928 // 'srem x, C' produces (-|C|, |C|).
9929 Upper = C->abs();
9930 Lower = (-Upper) + 1;
9931 } else if (match(BO.getOperand(0), m_APInt(C))) {
9932 if (C->isNegative()) {
9933 // 'srem -|C|, x' produces [-|C|, 0].
9934 Upper = 1;
9935 Lower = *C;
9936 } else {
9937 // 'srem |C|, x' produces [0, |C|].
9938 Upper = *C + 1;
9939 }
9940 }
9941 break;
9942
9943 case Instruction::URem:
9944 if (match(BO.getOperand(1), m_APInt(C)))
9945 // 'urem x, C' produces [0, C).
9946 Upper = *C;
9947 else if (match(BO.getOperand(0), m_APInt(C)))
9948 // 'urem C, x' produces [0, C].
9949 Upper = *C + 1;
9950 break;
9951
9952 default:
9953 break;
9954 }
9955}
9956
9958 bool UseInstrInfo) {
9959 unsigned Width = II.getType()->getScalarSizeInBits();
9960 const APInt *C;
9961 switch (II.getIntrinsicID()) {
9962 case Intrinsic::ctlz:
9963 case Intrinsic::cttz: {
9964 APInt Upper(Width, Width);
9965 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
9966 Upper += 1;
9967 // Maximum of set/clear bits is the bit width.
9969 }
9970 case Intrinsic::ctpop:
9971 // Maximum of set/clear bits is the bit width.
9973 APInt(Width, Width) + 1);
9974 case Intrinsic::uadd_sat:
9975 // uadd.sat(x, C) produces [C, UINT_MAX].
9976 if (match(II.getOperand(0), m_APInt(C)) ||
9977 match(II.getOperand(1), m_APInt(C)))
9979 break;
9980 case Intrinsic::sadd_sat:
9981 if (match(II.getOperand(0), m_APInt(C)) ||
9982 match(II.getOperand(1), m_APInt(C))) {
9983 if (C->isNegative())
9984 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9986 APInt::getSignedMaxValue(Width) + *C +
9987 1);
9988
9989 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9991 APInt::getSignedMaxValue(Width) + 1);
9992 }
9993 break;
9994 case Intrinsic::usub_sat:
9995 // usub.sat(C, x) produces [0, C].
9996 if (match(II.getOperand(0), m_APInt(C)))
9997 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9998
9999 // usub.sat(x, C) produces [0, UINT_MAX - C].
10000 if (match(II.getOperand(1), m_APInt(C)))
10002 APInt::getMaxValue(Width) - *C + 1);
10003 break;
10004 case Intrinsic::ssub_sat:
10005 if (match(II.getOperand(0), m_APInt(C))) {
10006 if (C->isNegative())
10007 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
10009 *C - APInt::getSignedMinValue(Width) +
10010 1);
10011
10012 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
10014 APInt::getSignedMaxValue(Width) + 1);
10015 } else if (match(II.getOperand(1), m_APInt(C))) {
10016 if (C->isNegative())
10017 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
10019 APInt::getSignedMaxValue(Width) + 1);
10020
10021 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
10023 APInt::getSignedMaxValue(Width) - *C +
10024 1);
10025 }
10026 break;
10027 case Intrinsic::umin:
10028 case Intrinsic::umax:
10029 case Intrinsic::smin:
10030 case Intrinsic::smax:
10031 if (!match(II.getOperand(0), m_APInt(C)) &&
10032 !match(II.getOperand(1), m_APInt(C)))
10033 break;
10034
10035 switch (II.getIntrinsicID()) {
10036 case Intrinsic::umin:
10037 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
10038 case Intrinsic::umax:
10040 case Intrinsic::smin:
10042 *C + 1);
10043 case Intrinsic::smax:
10045 APInt::getSignedMaxValue(Width) + 1);
10046 default:
10047 llvm_unreachable("Must be min/max intrinsic");
10048 }
10049 break;
10050 case Intrinsic::abs:
10051 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
10052 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10053 if (match(II.getOperand(1), m_One()))
10055 APInt::getSignedMaxValue(Width) + 1);
10056
10058 APInt::getSignedMinValue(Width) + 1);
10059 case Intrinsic::vscale:
10060 if (!II.getParent() || !II.getFunction())
10061 break;
10062 return getVScaleRange(II.getFunction(), Width);
10063 case Intrinsic::scmp:
10064 case Intrinsic::ucmp:
10066 APInt(Width, 2));
10067 default:
10068 break;
10069 }
10070
10071 return ConstantRange::getFull(Width);
10072}
10073
10075 const InstrInfoQuery &IIQ) {
10076 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10077 const Value *LHS = nullptr, *RHS = nullptr;
10079 if (R.Flavor == SPF_UNKNOWN)
10080 return ConstantRange::getFull(BitWidth);
10081
10082 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10083 // If the negation part of the abs (in RHS) has the NSW flag,
10084 // then the result of abs(X) is [0..SIGNED_MAX],
10085 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10086 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10087 IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
10090
10093 }
10094
10095 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10096 // The result of -abs(X) is <= 0.
10098 APInt(BitWidth, 1));
10099 }
10100
10101 const APInt *C;
10102 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10103 return ConstantRange::getFull(BitWidth);
10104
10105 switch (R.Flavor) {
10106 case SPF_UMIN:
10108 case SPF_UMAX:
10110 case SPF_SMIN:
10112 *C + 1);
10113 case SPF_SMAX:
10116 default:
10117 return ConstantRange::getFull(BitWidth);
10118 }
10119}
10120
10122 // The maximum representable value of a half is 65504. For floats the maximum
10123 // value is 3.4e38 which requires roughly 129 bits.
10124 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10125 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10126 return;
10127 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10128 Lower = APInt(BitWidth, -65504, true);
10129 Upper = APInt(BitWidth, 65505);
10130 }
10131
10132 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10133 // For a fptoui the lower limit is left as 0.
10134 Upper = APInt(BitWidth, 65505);
10135 }
10136}
10137
10139 bool UseInstrInfo, AssumptionCache *AC,
10140 const Instruction *CtxI,
10141 const DominatorTree *DT,
10142 unsigned Depth) {
10143 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10144
10146 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10147
10148 if (auto *C = dyn_cast<Constant>(V))
10149 return C->toConstantRange();
10150
10151 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10152 InstrInfoQuery IIQ(UseInstrInfo);
10153 ConstantRange CR = ConstantRange::getFull(BitWidth);
10154 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10155 APInt Lower = APInt(BitWidth, 0);
10156 APInt Upper = APInt(BitWidth, 0);
10157 // TODO: Return ConstantRange.
10158 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10160 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10161 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10162 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10164 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10166 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10167 CR = CRTrue.unionWith(CRFalse);
10168 CR = CR.intersectWith(getRangeForSelectPattern(*SI, IIQ));
10169 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10170 APInt Lower = APInt(BitWidth, 0);
10171 APInt Upper = APInt(BitWidth, 0);
10172 // TODO: Return ConstantRange.
10173 setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
10175 } else if (const auto *A = dyn_cast<Argument>(V))
10176 if (std::optional<ConstantRange> Range = A->getRange())
10177 CR = *Range;
10178
10179 if (auto *I = dyn_cast<Instruction>(V)) {
10180 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10182
10183 if (const auto *CB = dyn_cast<CallBase>(V))
10184 if (std::optional<ConstantRange> Range = CB->getRange())
10185 CR = CR.intersectWith(*Range);
10186 }
10187
10188 if (CtxI && AC) {
10189 // Try to restrict the range based on information from assumptions.
10190 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10191 if (!AssumeVH)
10192 continue;
10193 CallInst *I = cast<CallInst>(AssumeVH);
10194 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10195 "Got assumption for the wrong function!");
10196 assert(I->getIntrinsicID() == Intrinsic::assume &&
10197 "must be an assume intrinsic");
10198
10199 if (!isValidAssumeForContext(I, CtxI, DT))
10200 continue;
10201 Value *Arg = I->getArgOperand(0);
10202 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10203 // Currently we just use information from comparisons.
10204 if (!Cmp || Cmp->getOperand(0) != V)
10205 continue;
10206 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10208 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10209 UseInstrInfo, AC, I, DT, Depth + 1);
10210 CR = CR.intersectWith(
10211 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10212 }
10213 }
10214
10215 return CR;
10216}
10217
10218static void
10220 function_ref<void(Value *)> InsertAffected) {
10221 assert(V != nullptr);
10222 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10223 InsertAffected(V);
10224 } else if (auto *I = dyn_cast<Instruction>(V)) {
10225 InsertAffected(V);
10226
10227 // Peek through unary operators to find the source of the condition.
10228 Value *Op;
10230 if (isa<Instruction>(Op) || isa<Argument>(Op))
10231 InsertAffected(Op);
10232 }
10233 }
10234}
10235
10237 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10238 auto AddAffected = [&InsertAffected](Value *V) {
10239 addValueAffectedByCondition(V, InsertAffected);
10240 };
10241
10242 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10243 if (IsAssume) {
10244 AddAffected(LHS);
10245 AddAffected(RHS);
10246 } else if (match(RHS, m_Constant()))
10247 AddAffected(LHS);
10248 };
10249
10250 SmallVector<Value *, 8> Worklist;
10252 Worklist.push_back(Cond);
10253 while (!Worklist.empty()) {
10254 Value *V = Worklist.pop_back_val();
10255 if (!Visited.insert(V).second)
10256 continue;
10257
10258 CmpPredicate Pred;
10259 Value *A, *B, *X;
10260
10261 if (IsAssume) {
10262 AddAffected(V);
10263 if (match(V, m_Not(m_Value(X))))
10264 AddAffected(X);
10265 }
10266
10267 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10268 // assume(A && B) is split to -> assume(A); assume(B);
10269 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10270 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10271 // enough information to be worth handling (intersection of information as
10272 // opposed to union).
10273 if (!IsAssume) {
10274 Worklist.push_back(A);
10275 Worklist.push_back(B);
10276 }
10277 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10278 bool HasRHSC = match(B, m_ConstantInt());
10279 if (ICmpInst::isEquality(Pred)) {
10280 AddAffected(A);
10281 AddAffected(B);
10282 if (HasRHSC) {
10283 Value *Y;
10284 // (X & C) or (X | C).
10285 // (X << C) or (X >>_s C) or (X >>_u C).
10286 if (match(A, m_Shift(m_Value(X), m_ConstantInt())))
10287 AddAffected(X);
10288 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10289 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10290 AddAffected(X);
10291 AddAffected(Y);
10292 }
10293 }
10294 } else {
10295 AddCmpOperands(A, B);
10296 if (HasRHSC) {
10297 // Handle (A + C1) u< C2, which is the canonical form of
10298 // A > C3 && A < C4.
10300 AddAffected(X);
10301
10302 if (ICmpInst::isUnsigned(Pred)) {
10303 Value *Y;
10304 // X & Y u> C -> X >u C && Y >u C
10305 // X | Y u< C -> X u< C && Y u< C
10306 // X nuw+ Y u< C -> X u< C && Y u< C
10307 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10308 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10309 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10310 AddAffected(X);
10311 AddAffected(Y);
10312 }
10313 // X nuw- Y u> C -> X u> C
10314 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10315 AddAffected(X);
10316 }
10317 }
10318
10319 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10320 // by computeKnownFPClass().
10322 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10323 InsertAffected(X);
10324 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10325 InsertAffected(X);
10326 }
10327 }
10328
10329 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10330 AddAffected(X);
10331 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10332 AddCmpOperands(A, B);
10333
10334 // fcmp fneg(x), y
10335 // fcmp fabs(x), y
10336 // fcmp fneg(fabs(x)), y
10337 if (match(A, m_FNeg(m_Value(A))))
10338 AddAffected(A);
10339 if (match(A, m_FAbs(m_Value(A))))
10340 AddAffected(A);
10341
10342 } else if (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
10343 m_Value()))) {
10344 // Handle patterns that computeKnownFPClass() support.
10345 AddAffected(A);
10346 } else if (!IsAssume && match(V, m_Trunc(m_Value(X)))) {
10347 // Assume is checked here as X is already added above for assumes in
10348 // addValueAffectedByCondition
10349 AddAffected(X);
10350 } else if (!IsAssume && match(V, m_Not(m_Value(X)))) {
10351 // Assume is checked here to avoid issues with ephemeral values
10352 Worklist.push_back(X);
10353 }
10354 }
10355}
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:1315
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Hexagon Common GEP
static MaybeAlign getAlign(Value *Ptr)
Definition: IRBuilder.cpp:500
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
mir Rename Register Operands
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)
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static bool mayHaveSideEffects(MachineInstr &MI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
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 SmallSet 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:245
static SmallVector< VPValue *, 4 > getOperands(ArrayRef< VPValue * > Values, unsigned OperandIndex)
Definition: VPlanSLP.cpp:208
static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
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 isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW)
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 isNonZeroShift(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, const KnownBits &KnownVal)
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 void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
static bool inputDenormalIsIEEE(const Function &F, const Type *Ty)
Return true if it's possible to assume IEEE treatment of input denormals in F for Val.
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
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 std::tuple< Value *, FPClassTest, FPClassTest > exactClass(Value *V, FPClassTest M)
Return the return value for fcmpImpliesClass for a compare that produces an exact class test.
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 isKnownNonEqual(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if it is known that V1 != V2.
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 isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
UndefPoisonKind
static bool includesPoison(UndefPoisonKind Kind)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
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 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 bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, unsigned Depth, SimplifyQuery &Q)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if V1 == (binop V2, X), where X is known non-zero.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, FastMathFlags FMF, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static 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 void computeKnownFPClassFromCond(const Value *V, Value *Cond, unsigned Depth, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext)
static std::optional< bool > isImpliedCondICmps(const ICmpInst *LHS, 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 isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, 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 void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth, const SimplifyQuery &Q)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static void computeKnownBits(const Value *V, const APInt &DemandedElts, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
Determine which bits of V are known to be either zero or one and return them in the Known bit set.
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
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 void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, unsigned Depth, const SimplifyQuery &SQ, bool Invert)
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
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 void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q)
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 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 bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, const SimplifyQuery &Q)
Test whether a GEP's result is known to be non-null.
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y)
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 void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q, 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 cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, unsigned Depth, const SimplifyQuery &Q)
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 unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return the number of times the sign bit of the register is replicated into the other bits.
static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW)
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
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 bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth, const SimplifyQuery &Q)
Value * RHS
Value * LHS
bool isNegative() const
Definition: APFloat.h:1445
bool isFinite() const
Definition: APFloat.h:1450
bool isNaN() const
Definition: APFloat.h:1443
APInt bitcastToAPInt() const
Definition: APFloat.h:1351
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1140
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1100
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5450
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:1081
bool isSmallestNormalized() const
Definition: APFloat.h:1465
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1547
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:1407
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:1520
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1392
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1386
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1007
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:1742
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
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1640
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1468
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
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1618
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1397
APInt reverseBits() const
Definition: APInt.cpp:741
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:1607
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1015
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:356
unsigned logBase2() const
Definition: APInt.h:1739
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
APInt byteSwap() const
Definition: APInt.cpp:719
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:1389
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
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:858
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:1431
an instruction to allocate memory on the stack
Definition: Instructions.h:63
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
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:157
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
iterator begin() const
Definition: ArrayRef.h:156
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
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:198
Class to represent array types.
Definition: DerivedTypes.h:395
Type * getElementType() const
Definition: DerivedTypes.h:408
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.
std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
Definition: Attributes.cpp:470
unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
Definition: Attributes.cpp:464
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:209
bool isSingleEdge() const
Check if this is the only edge between Start and End.
Definition: Dominators.cpp:51
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator end()
Definition: BasicBlock.h:474
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:461
InstListType::const_iterator getFirstNonPHIIt() const
Returns an iterator to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:381
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:178
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:481
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:511
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:220
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
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:240
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition: InstrTypes.h:370
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1112
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1341
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Definition: InstrTypes.h:1738
Value * getCalledOperand() const
Definition: InstrTypes.h:1334
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1286
unsigned arg_size() const
Definition: InstrTypes.h:1284
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:444
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:661
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:673
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:676
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:690
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:702
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:703
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:679
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:688
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:677
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:678
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:697
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:696
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:700
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:687
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:681
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:684
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:698
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:685
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:680
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:682
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:701
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:689
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:699
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:686
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:675
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:683
bool isSigned() const
Definition: InstrTypes.h:928
static 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:825
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:940
bool isFPPredicate() const
Definition: InstrTypes.h:780
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:787
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:763
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition: InstrTypes.h:891
bool isIntPredicate() const
Definition: InstrTypes.h:781
static bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition: InstrTypes.h:934
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition: CmpPredicate.h:22
static std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
bool hasSameSign() const
Query samesign information, for optimizations.
Definition: CmpPredicate.h:42
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:696
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition: Constants.h:587
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition: Constants.h:662
uint64_t getElementAsInteger(unsigned i) const
If this is a sequential container of integers (of any size), return the specified element in the low ...
Definition: Constants.cpp:3115
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:770
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2638
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2321
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2279
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:271
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:866
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:208
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:157
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 ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
bool isAllNegative() const
Return true if all values in this range are negative.
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
KnownBits toKnownBits() const
Return known bits for values in this range.
APInt getUnsignedMin() const
Return the smallest unsigned value contained in the ConstantRange.
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...
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static 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...
ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static 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...
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
Definition: ConstantRange.h:84
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
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:42
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Definition: Constants.cpp:784
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1708
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition: Constants.cpp:76
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:197
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:709
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
Definition: DataLayout.cpp:754
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:743
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:617
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:162
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:122
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static 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:205
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
bool noSignedZeros() const
Definition: FMF.h:68
void setNoSignedZeros(bool B=true)
Definition: FMF.h:85
bool noNaNs() const
Definition: FMF.h:66
const BasicBlock & getEntryBlock() const
Definition: Function.h:815
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition: Function.cpp:807
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:933
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition: Globals.cpp:131
Type * getValueType() const
Definition: GlobalValue.h:297
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
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 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)
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
Definition: Instruction.h:315
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:94
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:72
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:310
bool isUnaryOp() const
Definition: Instruction.h:314
const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Definition: Instruction.cpp:76
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
An instruction for reading from memory.
Definition: Instructions.h:176
Value * getPointerOperand()
Definition: Instructions.h:255
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:211
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:39
Metadata node.
Definition: Metadata.h:1073
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:32
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:42
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:77
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 PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1878
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed.
Definition: Operator.h:155
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition: Operator.h:174
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 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:94
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
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:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:175
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:181
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void reserve(size_type N)
Definition: SmallVector.h:663
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:683
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:567
TypeSize getElementOffset(unsigned Idx) const
Definition: DataLayout.h:596
Class to represent struct types.
Definition: DerivedTypes.h:218
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:365
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:366
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
unsigned getIntegerBitWidth() const
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:270
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:243
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:264
uint64_t getArrayNumElements() const
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:310
static IntegerType * getInt16Ty(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
bool isIEEE() const
Return whether the type is IEEE compatible, as defined by the eponymous method in APFloat.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:267
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition: Type.h:252
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:237
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1859
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:64
unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition: Use.cpp:31
op_range operands()
Definition: User.h:288
Value * getOperand(unsigned i) const
Definition: User.h:228
unsigned getNumOperands() const
Definition: User.h:250
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
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:746
iterator_range< user_iterator > users()
Definition: Value.h:421
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition: WithCache.h:58
PointerType getValue() const
Definition: WithCache.h:56
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:168
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition: ilist_node.h:32
self_iterator getIterator()
Definition: ilist_node.h:132
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.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
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:764
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:885
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.
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:832
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:903
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:599
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
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.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:864
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)
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.
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
Definition: PatternMatch.h:322
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.
VScaleVal_match m_VScale()
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< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
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)
unsigned getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
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.
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...
Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
@ Offset
Definition: DWP.cpp:480
@ Length
Definition: DWP.cpp:480
OverflowResult
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
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:1739
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:1697
bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
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...
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
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.
const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
void getGuaranteedNonPoisonOps(const Instruction *I, SmallVectorImpl< const Value * > &Ops)
Insert operands of I into Ops such that I will trigger undefined behavior if I is executed and that o...
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.
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.
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:217
bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
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.
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:317
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:2115
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined.
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:378
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:297
gep_type_iterator gep_type_end(const User *GEP)
CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
void computeKnownBitsFromContext(const Value *V, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
Merge bits known from context-dependent facts into Known.
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:347
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.
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
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.
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:215
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...
RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, const Instruction *CtxI, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
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 isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
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:1746
KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, unsigned Depth, const SimplifyQuery &SQ)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
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:341
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:281
SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
Definition: ValueTracking.h:44
bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
void getGuaranteedWellDefinedOps(const Instruction *I, SmallVectorImpl< const Value * > &Ops)
Insert operands of I into Ops such that I will trigger undefined behavior if I is executed and that o...
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, Value *RHS, bool LookThroughSrc=true)
Compute the possible floating-point classes that LHS could be based on fcmp \Pred LHS,...
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, unsigned Depth, const SimplifyQuery &Q)
Adjust Known for the given select Arm to include information from the select Cond.
bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
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.
bool programUndefinedIfPoison(const Instruction *Inst)
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...
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:1171
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...
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
FPClassTest inverse_fabs(FPClassTest Mask)
Return the test mask which returns true after fabs is applied to the value.
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'.
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
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...
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
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
bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
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.
const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
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.
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.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
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
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.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, 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...
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
gep_type_iterator gep_type_begin(const User *GEP)
std::pair< Value *, FPClassTest > fcmpToClassTest(CmpInst::Predicate Pred, const Function &F, Value *LHS, Value *RHS, bool LookThroughSrc=true)
Returns a pair of values, which if passed to llvm.is.fpclass, returns the same result as an fcmp with...
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...
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...
void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=6)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1903
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits.
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
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...
bool isGEPBasedOnPointerToString(const GEPOperator *GEP, unsigned CharSize=8)
Returns true if the GEP is based on a pointer to a string (array of.
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.
KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, unsigned Depth, const SimplifyQuery &SQ)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
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...
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
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...
bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Get the upper bound on bit size for this Value Op as a signed integer.
bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
Definition: VectorUtils.cpp:46
bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
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.
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:860
static unsigned int semanticsPrecision(const fltSemantics &)
Definition: APFloat.cpp:315
static bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition: APFloat.cpp:356
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:65
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.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ Dynamic
Denormals have unknown treatment.
@ IEEE
IEEE-754 denormal numbers preserved.
static constexpr DenormalMode getPositiveZero()
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:25
bool isExact(const BinaryOperator *Op) const
Definition: SimplifyQuery.h:48
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
Definition: SimplifyQuery.h:30
bool hasNoSignedZeros(const InstT *Op) const
Definition: SimplifyQuery.h:54
bool hasNoSignedWrap(const InstT *Op) const
Definition: SimplifyQuery.h:42
bool hasNoUnsignedWrap(const InstT *Op) const
Definition: SimplifyQuery.h:36
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:293
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
Definition: KnownBits.cpp:765
static 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:488
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition: KnownBits.h:178
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:909
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:247
static 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:100
KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
Definition: KnownBits.cpp:1120
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:116
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:774
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition: KnownBits.h:243
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:234
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:428
static KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
Definition: KnownBits.cpp:768
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1049
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:65
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:266
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:1131
void makeNegative()
Make this value negative.
Definition: KnownBits.h:111
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:153
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:50
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:281
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:85
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:43
static 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:164
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:53
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:73
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:313
static 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:103
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:303
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:172
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition: KnownBits.h:237
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:336
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:188
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:240
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:137
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:215
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:901
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1066
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1009
static 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 KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:953
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:318
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:97
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:342
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:272
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition: KnownBits.h:91
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:211
static KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
Definition: KnownBits.cpp:771
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:804
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:159
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:550
static 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:526
static 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:512
static 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 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.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
bool isKnownNeverZero() const
Return true if it's known this can never be a zero.
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
bool isKnownNeverLogicalNegZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a negative zero.
bool isKnownNeverLogicalPosZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a positive zero.
void propagateCanonicalizingSrc(const KnownFPClass &Src, const Function &F, Type *Ty)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void propagateDenormal(const KnownFPClass &Src, const Function &F, Type *Ty)
Propagate knowledge from a source value that could be a denormal or zero.
bool isUnknown() const
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
bool isKnownNeverLogicalZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a zero.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
void signBitMustBeZero()
Assume the sign bit is zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
Definition: SimplifyQuery.h:71
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
Definition: SimplifyQuery.h:75
const DominatorTree * DT
Definition: SimplifyQuery.h:73
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
Definition: SimplifyQuery.h:74
const DomConditionCache * DC
Definition: SimplifyQuery.h:76
const InstrInfoQuery IIQ
Definition: SimplifyQuery.h:82
const CondContext * CC
Definition: SimplifyQuery.h:77