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