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