LLVM 20.0.0git
ValueTracking.cpp
Go to the documentation of this file.
1//===- ValueTracking.cpp - Walk computations to compute properties --------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains routines that help analyze properties that chains of
10// computations have.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/ADT/APFloat.h"
16#include "llvm/ADT/APInt.h"
17#include "llvm/ADT/ArrayRef.h"
18#include "llvm/ADT/STLExtras.h"
19#include "llvm/ADT/ScopeExit.h"
21#include "llvm/ADT/SmallSet.h"
23#include "llvm/ADT/StringRef.h"
32#include "llvm/Analysis/Loads.h"
37#include "llvm/IR/Argument.h"
38#include "llvm/IR/Attributes.h"
39#include "llvm/IR/BasicBlock.h"
40#include "llvm/IR/Constant.h"
42#include "llvm/IR/Constants.h"
45#include "llvm/IR/Dominators.h"
47#include "llvm/IR/Function.h"
49#include "llvm/IR/GlobalAlias.h"
50#include "llvm/IR/GlobalValue.h"
52#include "llvm/IR/InstrTypes.h"
53#include "llvm/IR/Instruction.h"
56#include "llvm/IR/Intrinsics.h"
57#include "llvm/IR/IntrinsicsAArch64.h"
58#include "llvm/IR/IntrinsicsAMDGPU.h"
59#include "llvm/IR/IntrinsicsRISCV.h"
60#include "llvm/IR/IntrinsicsX86.h"
61#include "llvm/IR/LLVMContext.h"
62#include "llvm/IR/Metadata.h"
63#include "llvm/IR/Module.h"
64#include "llvm/IR/Operator.h"
66#include "llvm/IR/Type.h"
67#include "llvm/IR/User.h"
68#include "llvm/IR/Value.h"
76#include <algorithm>
77#include <cassert>
78#include <cstdint>
79#include <optional>
80#include <utility>
81
82using namespace llvm;
83using namespace llvm::PatternMatch;
84
85// Controls the number of uses of the value searched for possible
86// dominating comparisons.
87static cl::opt<unsigned> DomConditionsMaxUses("dom-conditions-max-uses",
88 cl::Hidden, cl::init(20));
89
90
91/// Returns the bitwidth of the given scalar or pointer type. For vector types,
92/// returns the element type's bitwidth.
93static unsigned getBitWidth(Type *Ty, const DataLayout &DL) {
94 if (unsigned BitWidth = Ty->getScalarSizeInBits())
95 return BitWidth;
96
97 return DL.getPointerTypeSizeInBits(Ty);
98}
99
100// Given the provided Value and, potentially, a context instruction, return
101// the preferred context instruction (if any).
102static const Instruction *safeCxtI(const Value *V, const Instruction *CxtI) {
103 // If we've been provided with a context instruction, then use that (provided
104 // it has been inserted).
105 if (CxtI && CxtI->getParent())
106 return CxtI;
107
108 // If the value is really an already-inserted instruction, then use that.
109 CxtI = dyn_cast<Instruction>(V);
110 if (CxtI && CxtI->getParent())
111 return CxtI;
112
113 return nullptr;
114}
115
116static const Instruction *safeCxtI(const Value *V1, const Value *V2, const Instruction *CxtI) {
117 // If we've been provided with a context instruction, then use that (provided
118 // it has been inserted).
119 if (CxtI && CxtI->getParent())
120 return CxtI;
121
122 // If the value is really an already-inserted instruction, then use that.
123 CxtI = dyn_cast<Instruction>(V1);
124 if (CxtI && CxtI->getParent())
125 return CxtI;
126
127 CxtI = dyn_cast<Instruction>(V2);
128 if (CxtI && CxtI->getParent())
129 return CxtI;
130
131 return nullptr;
132}
133
135 const APInt &DemandedElts,
136 APInt &DemandedLHS, APInt &DemandedRHS) {
137 if (isa<ScalableVectorType>(Shuf->getType())) {
138 assert(DemandedElts == APInt(1,1));
139 DemandedLHS = DemandedRHS = DemandedElts;
140 return true;
141 }
142
143 int NumElts =
144 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
145 return llvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
146 DemandedElts, DemandedLHS, DemandedRHS);
147}
148
149static void computeKnownBits(const Value *V, const APInt &DemandedElts,
150 KnownBits &Known, unsigned Depth,
151 const SimplifyQuery &Q);
152
153void llvm::computeKnownBits(const Value *V, KnownBits &Known, unsigned Depth,
154 const SimplifyQuery &Q) {
155 // Since the number of lanes in a scalable vector is unknown at compile time,
156 // we track one bit which is implicitly broadcast to all lanes. This means
157 // that all lanes in a scalable vector are considered demanded.
158 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
159 APInt DemandedElts =
160 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
161 ::computeKnownBits(V, DemandedElts, Known, Depth, Q);
162}
163
165 const DataLayout &DL, unsigned Depth,
166 AssumptionCache *AC, const Instruction *CxtI,
167 const DominatorTree *DT, bool UseInstrInfo) {
169 V, Known, Depth,
170 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
171}
172
174 unsigned Depth, AssumptionCache *AC,
175 const Instruction *CxtI,
176 const DominatorTree *DT, bool UseInstrInfo) {
177 return computeKnownBits(
178 V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
179}
180
181KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
182 const DataLayout &DL, unsigned Depth,
183 AssumptionCache *AC, const Instruction *CxtI,
184 const DominatorTree *DT, bool UseInstrInfo) {
185 return computeKnownBits(
186 V, DemandedElts, Depth,
187 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
188}
189
190static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS,
191 const SimplifyQuery &SQ) {
192 // Look for an inverted mask: (X & ~M) op (Y & M).
193 {
194 Value *M;
195 if (match(LHS, m_c_And(m_Not(m_Value(M)), m_Value())) &&
197 isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
198 return true;
199 }
200
201 // X op (Y & ~X)
204 return true;
205
206 // X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
207 // for constant Y.
208 Value *Y;
209 if (match(RHS,
211 isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
212 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
213 return true;
214
215 // Peek through extends to find a 'not' of the other side:
216 // (ext Y) op ext(~Y)
217 if (match(LHS, m_ZExtOrSExt(m_Value(Y))) &&
219 isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
220 return true;
221
222 // Look for: (A & B) op ~(A | B)
223 {
224 Value *A, *B;
225 if (match(LHS, m_And(m_Value(A), m_Value(B))) &&
227 isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
228 isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
229 return true;
230 }
231
232 // Look for: (X << V) op (Y >> (BitWidth - V))
233 // or (X >> V) op (Y << (BitWidth - V))
234 {
235 const Value *V;
236 const APInt *R;
237 if (((match(RHS, m_Shl(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
238 match(LHS, m_LShr(m_Value(), m_Specific(V)))) ||
239 (match(RHS, m_LShr(m_Value(), m_Sub(m_APInt(R), m_Value(V)))) &&
240 match(LHS, m_Shl(m_Value(), m_Specific(V))))) &&
241 R->uge(LHS->getType()->getScalarSizeInBits()))
242 return true;
243 }
244
245 return false;
246}
247
249 const WithCache<const Value *> &RHSCache,
250 const SimplifyQuery &SQ) {
251 const Value *LHS = LHSCache.getValue();
252 const Value *RHS = RHSCache.getValue();
253
254 assert(LHS->getType() == RHS->getType() &&
255 "LHS and RHS should have the same type");
257 "LHS and RHS should be integers");
258
261 return true;
262
264 RHSCache.getKnownBits(SQ));
265}
266
268 return !I->user_empty() && all_of(I->users(), [](const User *U) {
269 return match(U, m_ICmp(m_Value(), m_Zero()));
270 });
271}
272
274 return !I->user_empty() && all_of(I->users(), [](const User *U) {
275 CmpPredicate P;
276 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
277 });
278}
279
281 bool OrZero, unsigned Depth,
282 AssumptionCache *AC, const Instruction *CxtI,
283 const DominatorTree *DT, bool UseInstrInfo) {
284 return ::isKnownToBeAPowerOfTwo(
285 V, OrZero, Depth,
286 SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
287}
288
289static bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
290 const SimplifyQuery &Q, unsigned Depth);
291
293 unsigned Depth) {
294 return computeKnownBits(V, Depth, SQ).isNonNegative();
295}
296
298 unsigned Depth) {
299 if (auto *CI = dyn_cast<ConstantInt>(V))
300 return CI->getValue().isStrictlyPositive();
301
302 // If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
303 // this updated.
304 KnownBits Known = computeKnownBits(V, Depth, SQ);
305 return Known.isNonNegative() &&
306 (Known.isNonZero() || isKnownNonZero(V, SQ, Depth));
307}
308
310 unsigned Depth) {
311 return computeKnownBits(V, Depth, SQ).isNegative();
312}
313
314static bool isKnownNonEqual(const Value *V1, const Value *V2,
315 const APInt &DemandedElts, unsigned Depth,
316 const SimplifyQuery &Q);
317
318bool llvm::isKnownNonEqual(const Value *V1, const Value *V2,
319 const DataLayout &DL, AssumptionCache *AC,
320 const Instruction *CxtI, const DominatorTree *DT,
321 bool UseInstrInfo) {
322 // We don't support looking through casts.
323 if (V1 == V2 || V1->getType() != V2->getType())
324 return false;
325 auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
326 APInt DemandedElts =
327 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
328 return ::isKnownNonEqual(
329 V1, V2, DemandedElts, 0,
330 SimplifyQuery(DL, DT, AC, safeCxtI(V2, V1, CxtI), UseInstrInfo));
331}
332
333bool llvm::MaskedValueIsZero(const Value *V, const APInt &Mask,
334 const SimplifyQuery &SQ, unsigned Depth) {
335 KnownBits Known(Mask.getBitWidth());
336 computeKnownBits(V, Known, Depth, SQ);
337 return Mask.isSubsetOf(Known.Zero);
338}
339
340static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
341 unsigned Depth, const SimplifyQuery &Q);
342
343static unsigned ComputeNumSignBits(const Value *V, unsigned Depth,
344 const SimplifyQuery &Q) {
345 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
346 APInt DemandedElts =
347 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
348 return ComputeNumSignBits(V, DemandedElts, Depth, Q);
349}
350
351unsigned llvm::ComputeNumSignBits(const Value *V, const DataLayout &DL,
352 unsigned Depth, AssumptionCache *AC,
353 const Instruction *CxtI,
354 const DominatorTree *DT, bool UseInstrInfo) {
355 return ::ComputeNumSignBits(
356 V, Depth, SimplifyQuery(DL, DT, AC, safeCxtI(V, CxtI), UseInstrInfo));
357}
358
360 unsigned Depth, AssumptionCache *AC,
361 const Instruction *CxtI,
362 const DominatorTree *DT) {
363 unsigned SignBits = ComputeNumSignBits(V, DL, Depth, AC, CxtI, DT);
364 return V->getType()->getScalarSizeInBits() - SignBits + 1;
365}
366
367static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1,
368 bool NSW, bool NUW,
369 const APInt &DemandedElts,
370 KnownBits &KnownOut, KnownBits &Known2,
371 unsigned Depth, const SimplifyQuery &Q) {
372 computeKnownBits(Op1, DemandedElts, KnownOut, Depth + 1, Q);
373
374 // If one operand is unknown and we have no nowrap information,
375 // the result will be unknown independently of the second operand.
376 if (KnownOut.isUnknown() && !NSW && !NUW)
377 return;
378
379 computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
380 KnownOut = KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
381}
382
383static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
384 bool NUW, const APInt &DemandedElts,
385 KnownBits &Known, KnownBits &Known2,
386 unsigned Depth, const SimplifyQuery &Q) {
387 computeKnownBits(Op1, DemandedElts, Known, Depth + 1, Q);
388 computeKnownBits(Op0, DemandedElts, Known2, Depth + 1, Q);
389
390 bool isKnownNegative = false;
391 bool isKnownNonNegative = false;
392 // If the multiplication is known not to overflow, compute the sign bit.
393 if (NSW) {
394 if (Op0 == Op1) {
395 // The product of a number with itself is non-negative.
396 isKnownNonNegative = true;
397 } else {
398 bool isKnownNonNegativeOp1 = Known.isNonNegative();
399 bool isKnownNonNegativeOp0 = Known2.isNonNegative();
400 bool isKnownNegativeOp1 = Known.isNegative();
401 bool isKnownNegativeOp0 = Known2.isNegative();
402 // The product of two numbers with the same sign is non-negative.
403 isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
404 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
405 if (!isKnownNonNegative && NUW) {
406 // mul nuw nsw with a factor > 1 is non-negative.
408 isKnownNonNegative = KnownBits::sgt(Known, One).value_or(false) ||
409 KnownBits::sgt(Known2, One).value_or(false);
410 }
411
412 // The product of a negative number and a non-negative number is either
413 // negative or zero.
416 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
417 Known2.isNonZero()) ||
418 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
419 }
420 }
421
422 bool SelfMultiply = Op0 == Op1;
423 if (SelfMultiply)
424 SelfMultiply &=
425 isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
426 Known = KnownBits::mul(Known, Known2, SelfMultiply);
427
428 // Only make use of no-wrap flags if we failed to compute the sign bit
429 // directly. This matters if the multiplication always overflows, in
430 // which case we prefer to follow the result of the direct computation,
431 // though as the program is invoking undefined behaviour we can choose
432 // whatever we like here.
433 if (isKnownNonNegative && !Known.isNegative())
434 Known.makeNonNegative();
435 else if (isKnownNegative && !Known.isNonNegative())
436 Known.makeNegative();
437}
438
440 KnownBits &Known) {
441 unsigned BitWidth = Known.getBitWidth();
442 unsigned NumRanges = Ranges.getNumOperands() / 2;
443 assert(NumRanges >= 1);
444
445 Known.Zero.setAllBits();
446 Known.One.setAllBits();
447
448 for (unsigned i = 0; i < NumRanges; ++i) {
450 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
452 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
453 ConstantRange Range(Lower->getValue(), Upper->getValue());
454
455 // The first CommonPrefixBits of all values in Range are equal.
456 unsigned CommonPrefixBits =
458 APInt Mask = APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
460 Known.One &= UnsignedMax & Mask;
461 Known.Zero &= ~UnsignedMax & Mask;
462 }
463}
464
465static bool isEphemeralValueOf(const Instruction *I, const Value *E) {
469
470 // The instruction defining an assumption's condition itself is always
471 // considered ephemeral to that assumption (even if it has other
472 // non-ephemeral users). See r246696's test case for an example.
473 if (is_contained(I->operands(), E))
474 return true;
475
476 while (!WorkSet.empty()) {
477 const Value *V = WorkSet.pop_back_val();
478 if (!Visited.insert(V).second)
479 continue;
480
481 // If all uses of this value are ephemeral, then so is this value.
482 if (llvm::all_of(V->users(), [&](const User *U) {
483 return EphValues.count(U);
484 })) {
485 if (V == E)
486 return true;
487
488 if (V == I || (isa<Instruction>(V) &&
489 !cast<Instruction>(V)->mayHaveSideEffects() &&
490 !cast<Instruction>(V)->isTerminator())) {
491 EphValues.insert(V);
492 if (const User *U = dyn_cast<User>(V))
493 append_range(WorkSet, U->operands());
494 }
495 }
496 }
497
498 return false;
499}
500
501// Is this an intrinsic that cannot be speculated but also cannot trap?
503 if (const IntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
504 return CI->isAssumeLikeIntrinsic();
505
506 return false;
507}
508
510 const Instruction *CxtI,
511 const DominatorTree *DT,
512 bool AllowEphemerals) {
513 // There are two restrictions on the use of an assume:
514 // 1. The assume must dominate the context (or the control flow must
515 // reach the assume whenever it reaches the context).
516 // 2. The context must not be in the assume's set of ephemeral values
517 // (otherwise we will use the assume to prove that the condition
518 // feeding the assume is trivially true, thus causing the removal of
519 // the assume).
520
521 if (Inv->getParent() == CxtI->getParent()) {
522 // If Inv and CtxI are in the same block, check if the assume (Inv) is first
523 // in the BB.
524 if (Inv->comesBefore(CxtI))
525 return true;
526
527 // Don't let an assume affect itself - this would cause the problems
528 // `isEphemeralValueOf` is trying to prevent, and it would also make
529 // the loop below go out of bounds.
530 if (!AllowEphemerals && Inv == CxtI)
531 return false;
532
533 // The context comes first, but they're both in the same block.
534 // Make sure there is nothing in between that might interrupt
535 // the control flow, not even CxtI itself.
536 // We limit the scan distance between the assume and its context instruction
537 // to avoid a compile-time explosion. This limit is chosen arbitrarily, so
538 // it can be adjusted if needed (could be turned into a cl::opt).
539 auto Range = make_range(CxtI->getIterator(), Inv->getIterator());
541 return false;
542
543 return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
544 }
545
546 // Inv and CxtI are in different blocks.
547 if (DT) {
548 if (DT->dominates(Inv, CxtI))
549 return true;
550 } else if (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
551 Inv->getParent()->isEntryBlock()) {
552 // We don't have a DT, but this trivially dominates.
553 return true;
554 }
555
556 return false;
557}
558
559// TODO: cmpExcludesZero misses many cases where `RHS` is non-constant but
560// we still have enough information about `RHS` to conclude non-zero. For
561// example Pred=EQ, RHS=isKnownNonZero. cmpExcludesZero is called in loops
562// so the extra compile time may not be worth it, but possibly a second API
563// should be created for use outside of loops.
564static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS) {
565 // v u> y implies v != 0.
566 if (Pred == ICmpInst::ICMP_UGT)
567 return true;
568
569 // Special-case v != 0 to also handle v != null.
570 if (Pred == ICmpInst::ICMP_NE)
571 return match(RHS, m_Zero());
572
573 // All other predicates - rely on generic ConstantRange handling.
574 const APInt *C;
576 if (match(RHS, m_APInt(C))) {
578 return !TrueValues.contains(Zero);
579 }
580
581 auto *VC = dyn_cast<ConstantDataVector>(RHS);
582 if (VC == nullptr)
583 return false;
584
585 for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
586 ++ElemIdx) {
588 Pred, VC->getElementAsAPInt(ElemIdx));
589 if (TrueValues.contains(Zero))
590 return false;
591 }
592 return true;
593}
594
595static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI,
596 Value *&ValOut, Instruction *&CtxIOut) {
597 ValOut = U->get();
598 if (ValOut == PHI)
599 return;
600 CtxIOut = PHI->getIncomingBlock(*U)->getTerminator();
601 Value *V;
602 // If the Use is a select of this phi, compute analysis on other arm to break
603 // recursion.
604 // TODO: Min/Max
605 if (match(ValOut, m_Select(m_Value(), m_Specific(PHI), m_Value(V))) ||
606 match(ValOut, m_Select(m_Value(), m_Value(V), m_Specific(PHI))))
607 ValOut = V;
608
609 // Same for select, if this phi is 2-operand phi, compute analysis on other
610 // incoming value to break recursion.
611 // TODO: We could handle any number of incoming edges as long as we only have
612 // two unique values.
613 else if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
614 IncPhi && IncPhi->getNumIncomingValues() == 2) {
615 for (int Idx = 0; Idx < 2; ++Idx) {
616 if (IncPhi->getIncomingValue(Idx) == PHI) {
617 ValOut = IncPhi->getIncomingValue(1 - Idx);
618 CtxIOut = IncPhi->getIncomingBlock(1 - Idx)->getTerminator();
619 break;
620 }
621 }
622 }
623}
624
625static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q) {
626 // Use of assumptions is context-sensitive. If we don't have a context, we
627 // cannot use them!
628 if (!Q.AC || !Q.CxtI)
629 return false;
630
631 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
632 if (!Elem.Assume)
633 continue;
634
635 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
636 assert(I->getFunction() == Q.CxtI->getFunction() &&
637 "Got assumption for the wrong function!");
638
639 if (Elem.Index != AssumptionCache::ExprResultIdx) {
640 if (!V->getType()->isPointerTy())
641 continue;
643 *I, I->bundle_op_info_begin()[Elem.Index])) {
644 if (RK.WasOn == V &&
645 (RK.AttrKind == Attribute::NonNull ||
646 (RK.AttrKind == Attribute::Dereferenceable &&
648 V->getType()->getPointerAddressSpace()))) &&
650 return true;
651 }
652 continue;
653 }
654
655 // Warning: This loop can end up being somewhat performance sensitive.
656 // We're running this loop for once for each value queried resulting in a
657 // runtime of ~O(#assumes * #values).
658
659 Value *RHS;
660 CmpPredicate Pred;
661 auto m_V = m_CombineOr(m_Specific(V), m_PtrToInt(m_Specific(V)));
662 if (!match(I->getArgOperand(0), m_c_ICmp(Pred, m_V, m_Value(RHS))))
663 continue;
664
665 if (cmpExcludesZero(Pred, RHS) && isValidAssumeForContext(I, Q.CxtI, Q.DT))
666 return true;
667 }
668
669 return false;
670}
671
673 Value *LHS, Value *RHS, KnownBits &Known,
674 const SimplifyQuery &Q) {
675 if (RHS->getType()->isPointerTy()) {
676 // Handle comparison of pointer to null explicitly, as it will not be
677 // covered by the m_APInt() logic below.
678 if (LHS == V && match(RHS, m_Zero())) {
679 switch (Pred) {
680 case ICmpInst::ICMP_EQ:
681 Known.setAllZero();
682 break;
683 case ICmpInst::ICMP_SGE:
684 case ICmpInst::ICMP_SGT:
685 Known.makeNonNegative();
686 break;
687 case ICmpInst::ICMP_SLT:
688 Known.makeNegative();
689 break;
690 default:
691 break;
692 }
693 }
694 return;
695 }
696
697 unsigned BitWidth = Known.getBitWidth();
698 auto m_V =
700
701 Value *Y;
702 const APInt *Mask, *C;
703 uint64_t ShAmt;
704 switch (Pred) {
705 case ICmpInst::ICMP_EQ:
706 // assume(V = C)
707 if (match(LHS, m_V) && match(RHS, m_APInt(C))) {
708 Known = Known.unionWith(KnownBits::makeConstant(*C));
709 // assume(V & Mask = C)
710 } else if (match(LHS, m_c_And(m_V, m_Value(Y))) &&
711 match(RHS, m_APInt(C))) {
712 // For one bits in Mask, we can propagate bits from C to V.
713 Known.One |= *C;
714 if (match(Y, m_APInt(Mask)))
715 Known.Zero |= ~*C & *Mask;
716 // assume(V | Mask = C)
717 } else if (match(LHS, m_c_Or(m_V, m_Value(Y))) && match(RHS, m_APInt(C))) {
718 // For zero bits in Mask, we can propagate bits from C to V.
719 Known.Zero |= ~*C;
720 if (match(Y, m_APInt(Mask)))
721 Known.One |= *C & ~*Mask;
722 // assume(V ^ Mask = C)
723 } else if (match(LHS, m_Xor(m_V, m_APInt(Mask))) &&
724 match(RHS, m_APInt(C))) {
725 // Equivalent to assume(V == Mask ^ C)
726 Known = Known.unionWith(KnownBits::makeConstant(*C ^ *Mask));
727 // assume(V << ShAmt = C)
728 } else if (match(LHS, m_Shl(m_V, m_ConstantInt(ShAmt))) &&
729 match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
730 // For those bits in C that are known, we can propagate them to known
731 // bits in V shifted to the right by ShAmt.
733 RHSKnown.Zero.lshrInPlace(ShAmt);
734 RHSKnown.One.lshrInPlace(ShAmt);
735 Known = Known.unionWith(RHSKnown);
736 // assume(V >> ShAmt = C)
737 } else if (match(LHS, m_Shr(m_V, m_ConstantInt(ShAmt))) &&
738 match(RHS, m_APInt(C)) && ShAmt < BitWidth) {
740 // For those bits in RHS that are known, we can propagate them to known
741 // bits in V shifted to the right by C.
742 Known.Zero |= RHSKnown.Zero << ShAmt;
743 Known.One |= RHSKnown.One << ShAmt;
744 }
745 break;
746 case ICmpInst::ICMP_NE: {
747 // assume (V & B != 0) where B is a power of 2
748 const APInt *BPow2;
749 if (match(LHS, m_And(m_V, m_Power2(BPow2))) && match(RHS, m_Zero()))
750 Known.One |= *BPow2;
751 break;
752 }
753 default:
754 if (match(RHS, m_APInt(C))) {
755 const APInt *Offset = nullptr;
756 if (match(LHS, m_CombineOr(m_V, m_AddLike(m_V, m_APInt(Offset))))) {
758 if (Offset)
759 LHSRange = LHSRange.sub(*Offset);
760 Known = Known.unionWith(LHSRange.toKnownBits());
761 }
762 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
763 // X & Y u> C -> X u> C && Y u> C
764 // X nuw- Y u> C -> X u> C
765 if (match(LHS, m_c_And(m_V, m_Value())) ||
766 match(LHS, m_NUWSub(m_V, m_Value())))
767 Known.One.setHighBits(
768 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
769 }
770 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
771 // X | Y u< C -> X u< C && Y u< C
772 // X nuw+ Y u< C -> X u< C && Y u< C
773 if (match(LHS, m_c_Or(m_V, m_Value())) ||
774 match(LHS, m_c_NUWAdd(m_V, m_Value()))) {
775 Known.Zero.setHighBits(
776 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
777 }
778 }
779 }
780 break;
781 }
782}
783
784static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
785 KnownBits &Known,
786 const SimplifyQuery &SQ, bool Invert) {
788 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
789 Value *LHS = Cmp->getOperand(0);
790 Value *RHS = Cmp->getOperand(1);
791
792 // Handle icmp pred (trunc V), C
793 if (match(LHS, m_Trunc(m_Specific(V)))) {
795 computeKnownBitsFromCmp(LHS, Pred, LHS, RHS, DstKnown, SQ);
796 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
797 return;
798 }
799
800 computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
801}
802
804 KnownBits &Known, unsigned Depth,
805 const SimplifyQuery &SQ, bool Invert) {
806 Value *A, *B;
809 KnownBits Known2(Known.getBitWidth());
810 KnownBits Known3(Known.getBitWidth());
811 computeKnownBitsFromCond(V, A, Known2, Depth + 1, SQ, Invert);
812 computeKnownBitsFromCond(V, B, Known3, Depth + 1, SQ, Invert);
813 if (Invert ? match(Cond, m_LogicalOr(m_Value(), m_Value()))
815 Known2 = Known2.unionWith(Known3);
816 else
817 Known2 = Known2.intersectWith(Known3);
818 Known = Known.unionWith(Known2);
819 }
820
821 if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
822 computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
823}
824
826 unsigned Depth, const SimplifyQuery &Q) {
827 // Handle injected condition.
828 if (Q.CC && Q.CC->AffectedValues.contains(V))
829 computeKnownBitsFromCond(V, Q.CC->Cond, Known, Depth, Q, Q.CC->Invert);
830
831 if (!Q.CxtI)
832 return;
833
834 if (Q.DC && Q.DT) {
835 // Handle dominating conditions.
836 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
837 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
838 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
839 computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
840 /*Invert*/ false);
841
842 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
843 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
844 computeKnownBitsFromCond(V, BI->getCondition(), Known, Depth, Q,
845 /*Invert*/ true);
846 }
847
848 if (Known.hasConflict())
849 Known.resetAll();
850 }
851
852 if (!Q.AC)
853 return;
854
855 unsigned BitWidth = Known.getBitWidth();
856
857 // Note that the patterns below need to be kept in sync with the code
858 // in AssumptionCache::updateAffectedValues.
859
860 for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
861 if (!Elem.Assume)
862 continue;
863
864 AssumeInst *I = cast<AssumeInst>(Elem.Assume);
865 assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
866 "Got assumption for the wrong function!");
867
868 if (Elem.Index != AssumptionCache::ExprResultIdx) {
869 if (!V->getType()->isPointerTy())
870 continue;
872 *I, I->bundle_op_info_begin()[Elem.Index])) {
873 // Allow AllowEphemerals in isValidAssumeForContext, as the CxtI might
874 // be the producer of the pointer in the bundle. At the moment, align
875 // assumptions aren't optimized away.
876 if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
877 isPowerOf2_64(RK.ArgValue) &&
878 isValidAssumeForContext(I, Q.CxtI, Q.DT, /*AllowEphemerals*/ true))
879 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
880 }
881 continue;
882 }
883
884 // Warning: This loop can end up being somewhat performance sensitive.
885 // We're running this loop for once for each value queried resulting in a
886 // runtime of ~O(#assumes * #values).
887
888 Value *Arg = I->getArgOperand(0);
889
890 if (Arg == V && isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
891 assert(BitWidth == 1 && "assume operand is not i1?");
892 (void)BitWidth;
893 Known.setAllOnes();
894 return;
895 }
896 if (match(Arg, m_Not(m_Specific(V))) &&
898 assert(BitWidth == 1 && "assume operand is not i1?");
899 (void)BitWidth;
900 Known.setAllZero();
901 return;
902 }
903
904 // The remaining tests are all recursive, so bail out if we hit the limit.
906 continue;
907
908 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
909 if (!Cmp)
910 continue;
911
912 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
913 continue;
914
915 computeKnownBitsFromICmpCond(V, Cmp, Known, Q, /*Invert=*/false);
916 }
917
918 // Conflicting assumption: Undefined behavior will occur on this execution
919 // path.
920 if (Known.hasConflict())
921 Known.resetAll();
922}
923
924/// Compute known bits from a shift operator, including those with a
925/// non-constant shift amount. Known is the output of this function. Known2 is a
926/// pre-allocated temporary with the same bit width as Known and on return
927/// contains the known bit of the shift value source. KF is an
928/// operator-specific function that, given the known-bits and a shift amount,
929/// compute the implied known-bits of the shift operator's result respectively
930/// for that shift amount. The results from calling KF are conservatively
931/// combined for all permitted shift amounts.
933 const Operator *I, const APInt &DemandedElts, KnownBits &Known,
934 KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q,
935 function_ref<KnownBits(const KnownBits &, const KnownBits &, bool)> KF) {
936 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
937 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
938 // To limit compile-time impact, only query isKnownNonZero() if we know at
939 // least something about the shift amount.
940 bool ShAmtNonZero =
941 Known.isNonZero() ||
942 (Known.getMaxValue().ult(Known.getBitWidth()) &&
943 isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth + 1));
944 Known = KF(Known2, Known, ShAmtNonZero);
945}
946
947static KnownBits
948getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts,
949 const KnownBits &KnownLHS, const KnownBits &KnownRHS,
950 unsigned Depth, const SimplifyQuery &Q) {
951 unsigned BitWidth = KnownLHS.getBitWidth();
952 KnownBits KnownOut(BitWidth);
953 bool IsAnd = false;
954 bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
955 Value *X = nullptr, *Y = nullptr;
956
957 switch (I->getOpcode()) {
958 case Instruction::And:
959 KnownOut = KnownLHS & KnownRHS;
960 IsAnd = true;
961 // and(x, -x) is common idioms that will clear all but lowest set
962 // bit. If we have a single known bit in x, we can clear all bits
963 // above it.
964 // TODO: instcombine often reassociates independent `and` which can hide
965 // this pattern. Try to match and(x, and(-x, y)) / and(and(x, y), -x).
966 if (HasKnownOne && match(I, m_c_And(m_Value(X), m_Neg(m_Deferred(X))))) {
967 // -(-x) == x so using whichever (LHS/RHS) gets us a better result.
968 if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
969 KnownOut = KnownLHS.blsi();
970 else
971 KnownOut = KnownRHS.blsi();
972 }
973 break;
974 case Instruction::Or:
975 KnownOut = KnownLHS | KnownRHS;
976 break;
977 case Instruction::Xor:
978 KnownOut = KnownLHS ^ KnownRHS;
979 // xor(x, x-1) is common idioms that will clear all but lowest set
980 // bit. If we have a single known bit in x, we can clear all bits
981 // above it.
982 // TODO: xor(x, x-1) is often rewritting as xor(x, x-C) where C !=
983 // -1 but for the purpose of demanded bits (xor(x, x-C) &
984 // Demanded) == (xor(x, x-1) & Demanded). Extend the xor pattern
985 // to use arbitrary C if xor(x, x-C) as the same as xor(x, x-1).
986 if (HasKnownOne &&
988 const KnownBits &XBits = I->getOperand(0) == X ? KnownLHS : KnownRHS;
989 KnownOut = XBits.blsmsk();
990 }
991 break;
992 default:
993 llvm_unreachable("Invalid Op used in 'analyzeKnownBitsFromAndXorOr'");
994 }
995
996 // and(x, add (x, -1)) is a common idiom that always clears the low bit;
997 // xor/or(x, add (x, -1)) is an idiom that will always set the low bit.
998 // here we handle the more general case of adding any odd number by
999 // matching the form and/xor/or(x, add(x, y)) where y is odd.
1000 // TODO: This could be generalized to clearing any bit set in y where the
1001 // following bit is known to be unset in y.
1002 if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1006 KnownBits KnownY(BitWidth);
1007 computeKnownBits(Y, DemandedElts, KnownY, Depth + 1, Q);
1008 if (KnownY.countMinTrailingOnes() > 0) {
1009 if (IsAnd)
1010 KnownOut.Zero.setBit(0);
1011 else
1012 KnownOut.One.setBit(0);
1013 }
1014 }
1015 return KnownOut;
1016}
1017
1019 const Operator *I, const APInt &DemandedElts, unsigned Depth,
1020 const SimplifyQuery &Q,
1021 const function_ref<KnownBits(const KnownBits &, const KnownBits &)>
1022 KnownBitsFunc) {
1023 APInt DemandedEltsLHS, DemandedEltsRHS;
1025 DemandedElts, DemandedEltsLHS,
1026 DemandedEltsRHS);
1027
1028 const auto ComputeForSingleOpFunc =
1029 [Depth, &Q, KnownBitsFunc](const Value *Op, APInt &DemandedEltsOp) {
1030 return KnownBitsFunc(
1031 computeKnownBits(Op, DemandedEltsOp, Depth + 1, Q),
1032 computeKnownBits(Op, DemandedEltsOp << 1, Depth + 1, Q));
1033 };
1034
1035 if (DemandedEltsRHS.isZero())
1036 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1037 if (DemandedEltsLHS.isZero())
1038 return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1039
1040 return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1041 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1042}
1043
1044// Public so this can be used in `SimplifyDemandedUseBits`.
1046 const KnownBits &KnownLHS,
1047 const KnownBits &KnownRHS,
1048 unsigned Depth,
1049 const SimplifyQuery &SQ) {
1050 auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1051 APInt DemandedElts =
1052 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
1053
1054 return getKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS, Depth,
1055 SQ);
1056}
1057
1059 Attribute Attr = F->getFnAttribute(Attribute::VScaleRange);
1060 // Without vscale_range, we only know that vscale is non-zero.
1061 if (!Attr.isValid())
1063
1064 unsigned AttrMin = Attr.getVScaleRangeMin();
1065 // Minimum is larger than vscale width, result is always poison.
1066 if ((unsigned)llvm::bit_width(AttrMin) > BitWidth)
1067 return ConstantRange::getEmpty(BitWidth);
1068
1069 APInt Min(BitWidth, AttrMin);
1070 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1071 if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) > BitWidth)
1073
1074 return ConstantRange(Min, APInt(BitWidth, *AttrMax) + 1);
1075}
1076
1078 Value *Arm, bool Invert, unsigned Depth,
1079 const SimplifyQuery &Q) {
1080 // If we have a constant arm, we are done.
1081 if (Known.isConstant())
1082 return;
1083
1084 // See what condition implies about the bits of the select arm.
1085 KnownBits CondRes(Known.getBitWidth());
1086 computeKnownBitsFromCond(Arm, Cond, CondRes, Depth + 1, Q, Invert);
1087 // If we don't get any information from the condition, no reason to
1088 // proceed.
1089 if (CondRes.isUnknown())
1090 return;
1091
1092 // We can have conflict if the condition is dead. I.e if we have
1093 // (x | 64) < 32 ? (x | 64) : y
1094 // we will have conflict at bit 6 from the condition/the `or`.
1095 // In that case just return. Its not particularly important
1096 // what we do, as this select is going to be simplified soon.
1097 CondRes = CondRes.unionWith(Known);
1098 if (CondRes.hasConflict())
1099 return;
1100
1101 // Finally make sure the information we found is valid. This is relatively
1102 // expensive so it's left for the very end.
1103 if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT, Depth + 1))
1104 return;
1105
1106 // Finally, we know we get information from the condition and its valid,
1107 // so return it.
1108 Known = CondRes;
1109}
1110
1111// Match a signed min+max clamp pattern like smax(smin(In, CHigh), CLow).
1112// Returns the input and lower/upper bounds.
1113static bool isSignedMinMaxClamp(const Value *Select, const Value *&In,
1114 const APInt *&CLow, const APInt *&CHigh) {
1115 assert(isa<Operator>(Select) &&
1116 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1117 "Input should be a Select!");
1118
1119 const Value *LHS = nullptr, *RHS = nullptr;
1121 if (SPF != SPF_SMAX && SPF != SPF_SMIN)
1122 return false;
1123
1124 if (!match(RHS, m_APInt(CLow)))
1125 return false;
1126
1127 const Value *LHS2 = nullptr, *RHS2 = nullptr;
1129 if (getInverseMinMaxFlavor(SPF) != SPF2)
1130 return false;
1131
1132 if (!match(RHS2, m_APInt(CHigh)))
1133 return false;
1134
1135 if (SPF == SPF_SMIN)
1136 std::swap(CLow, CHigh);
1137
1138 In = LHS2;
1139 return CLow->sle(*CHigh);
1140}
1141
1143 const APInt *&CLow,
1144 const APInt *&CHigh) {
1145 assert((II->getIntrinsicID() == Intrinsic::smin ||
1146 II->getIntrinsicID() == Intrinsic::smax) &&
1147 "Must be smin/smax");
1148
1149 Intrinsic::ID InverseID = getInverseMinMaxIntrinsic(II->getIntrinsicID());
1150 auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1151 if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1152 !match(II->getArgOperand(1), m_APInt(CLow)) ||
1153 !match(InnerII->getArgOperand(1), m_APInt(CHigh)))
1154 return false;
1155
1156 if (II->getIntrinsicID() == Intrinsic::smin)
1157 std::swap(CLow, CHigh);
1158 return CLow->sle(*CHigh);
1159}
1160
1162 KnownBits &Known) {
1163 const APInt *CLow, *CHigh;
1164 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1165 Known = Known.unionWith(
1166 ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1167}
1168
1170 const APInt &DemandedElts,
1171 KnownBits &Known, unsigned Depth,
1172 const SimplifyQuery &Q) {
1173 unsigned BitWidth = Known.getBitWidth();
1174
1175 KnownBits Known2(BitWidth);
1176 switch (I->getOpcode()) {
1177 default: break;
1178 case Instruction::Load:
1179 if (MDNode *MD =
1180 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1182 break;
1183 case Instruction::And:
1184 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1185 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1186
1187 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1188 break;
1189 case Instruction::Or:
1190 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1191 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1192
1193 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1194 break;
1195 case Instruction::Xor:
1196 computeKnownBits(I->getOperand(1), DemandedElts, Known, Depth + 1, Q);
1197 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1198
1199 Known = getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known, Depth, Q);
1200 break;
1201 case Instruction::Mul: {
1202 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1203 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1204 computeKnownBitsMul(I->getOperand(0), I->getOperand(1), NSW, NUW,
1205 DemandedElts, Known, Known2, Depth, Q);
1206 break;
1207 }
1208 case Instruction::UDiv: {
1209 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1210 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1211 Known =
1212 KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1213 break;
1214 }
1215 case Instruction::SDiv: {
1216 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1217 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1218 Known =
1219 KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1220 break;
1221 }
1222 case Instruction::Select: {
1223 auto ComputeForArm = [&](Value *Arm, bool Invert) {
1224 KnownBits Res(Known.getBitWidth());
1225 computeKnownBits(Arm, DemandedElts, Res, Depth + 1, Q);
1226 adjustKnownBitsForSelectArm(Res, I->getOperand(0), Arm, Invert, Depth, Q);
1227 return Res;
1228 };
1229 // Only known if known in both the LHS and RHS.
1230 Known =
1231 ComputeForArm(I->getOperand(1), /*Invert=*/false)
1232 .intersectWith(ComputeForArm(I->getOperand(2), /*Invert=*/true));
1233 break;
1234 }
1235 case Instruction::FPTrunc:
1236 case Instruction::FPExt:
1237 case Instruction::FPToUI:
1238 case Instruction::FPToSI:
1239 case Instruction::SIToFP:
1240 case Instruction::UIToFP:
1241 break; // Can't work with floating point.
1242 case Instruction::PtrToInt:
1243 case Instruction::IntToPtr:
1244 // Fall through and handle them the same as zext/trunc.
1245 [[fallthrough]];
1246 case Instruction::ZExt:
1247 case Instruction::Trunc: {
1248 Type *SrcTy = I->getOperand(0)->getType();
1249
1250 unsigned SrcBitWidth;
1251 // Note that we handle pointer operands here because of inttoptr/ptrtoint
1252 // which fall through here.
1253 Type *ScalarTy = SrcTy->getScalarType();
1254 SrcBitWidth = ScalarTy->isPointerTy() ?
1255 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1256 Q.DL.getTypeSizeInBits(ScalarTy);
1257
1258 assert(SrcBitWidth && "SrcBitWidth can't be zero");
1259 Known = Known.anyextOrTrunc(SrcBitWidth);
1260 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1261 if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1262 Inst && Inst->hasNonNeg() && !Known.isNegative())
1263 Known.makeNonNegative();
1264 Known = Known.zextOrTrunc(BitWidth);
1265 break;
1266 }
1267 case Instruction::BitCast: {
1268 Type *SrcTy = I->getOperand(0)->getType();
1269 if (SrcTy->isIntOrPtrTy() &&
1270 // TODO: For now, not handling conversions like:
1271 // (bitcast i64 %x to <2 x i32>)
1272 !I->getType()->isVectorTy()) {
1273 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1274 break;
1275 }
1276
1277 const Value *V;
1278 // Handle bitcast from floating point to integer.
1279 if (match(I, m_ElementWiseBitCast(m_Value(V))) &&
1280 V->getType()->isFPOrFPVectorTy()) {
1281 Type *FPType = V->getType()->getScalarType();
1282 KnownFPClass Result =
1283 computeKnownFPClass(V, DemandedElts, fcAllFlags, Depth + 1, Q);
1284 FPClassTest FPClasses = Result.KnownFPClasses;
1285
1286 // TODO: Treat it as zero/poison if the use of I is unreachable.
1287 if (FPClasses == fcNone)
1288 break;
1289
1290 if (Result.isKnownNever(fcNormal | fcSubnormal | fcNan)) {
1291 Known.Zero.setAllBits();
1292 Known.One.setAllBits();
1293
1294 if (FPClasses & fcInf)
1296 APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1297
1298 if (FPClasses & fcZero)
1300 APInt::getZero(FPType->getScalarSizeInBits())));
1301
1302 Known.Zero.clearSignBit();
1303 Known.One.clearSignBit();
1304 }
1305
1306 if (Result.SignBit) {
1307 if (*Result.SignBit)
1308 Known.makeNegative();
1309 else
1310 Known.makeNonNegative();
1311 }
1312
1313 break;
1314 }
1315
1316 // Handle cast from vector integer type to scalar or vector integer.
1317 auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1318 if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1319 !I->getType()->isIntOrIntVectorTy() ||
1320 isa<ScalableVectorType>(I->getType()))
1321 break;
1322
1323 // Look through a cast from narrow vector elements to wider type.
1324 // Examples: v4i32 -> v2i64, v3i8 -> v24
1325 unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1326 if (BitWidth % SubBitWidth == 0) {
1327 // Known bits are automatically intersected across demanded elements of a
1328 // vector. So for example, if a bit is computed as known zero, it must be
1329 // zero across all demanded elements of the vector.
1330 //
1331 // For this bitcast, each demanded element of the output is sub-divided
1332 // across a set of smaller vector elements in the source vector. To get
1333 // the known bits for an entire element of the output, compute the known
1334 // bits for each sub-element sequentially. This is done by shifting the
1335 // one-set-bit demanded elements parameter across the sub-elements for
1336 // consecutive calls to computeKnownBits. We are using the demanded
1337 // elements parameter as a mask operator.
1338 //
1339 // The known bits of each sub-element are then inserted into place
1340 // (dependent on endian) to form the full result of known bits.
1341 unsigned NumElts = DemandedElts.getBitWidth();
1342 unsigned SubScale = BitWidth / SubBitWidth;
1343 APInt SubDemandedElts = APInt::getZero(NumElts * SubScale);
1344 for (unsigned i = 0; i != NumElts; ++i) {
1345 if (DemandedElts[i])
1346 SubDemandedElts.setBit(i * SubScale);
1347 }
1348
1349 KnownBits KnownSrc(SubBitWidth);
1350 for (unsigned i = 0; i != SubScale; ++i) {
1351 computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc,
1352 Depth + 1, Q);
1353 unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i;
1354 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1355 }
1356 }
1357 break;
1358 }
1359 case Instruction::SExt: {
1360 // Compute the bits in the result that are not present in the input.
1361 unsigned SrcBitWidth = I->getOperand(0)->getType()->getScalarSizeInBits();
1362
1363 Known = Known.trunc(SrcBitWidth);
1364 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1365 // If the sign bit of the input is known set or clear, then we know the
1366 // top bits of the result.
1367 Known = Known.sext(BitWidth);
1368 break;
1369 }
1370 case Instruction::Shl: {
1371 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1372 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1373 auto KF = [NUW, NSW](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1374 bool ShAmtNonZero) {
1375 return KnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1376 };
1377 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1378 KF);
1379 // Trailing zeros of a right-shifted constant never decrease.
1380 const APInt *C;
1381 if (match(I->getOperand(0), m_APInt(C)))
1382 Known.Zero.setLowBits(C->countr_zero());
1383 break;
1384 }
1385 case Instruction::LShr: {
1386 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1387 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1388 bool ShAmtNonZero) {
1389 return KnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1390 };
1391 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1392 KF);
1393 // Leading zeros of a left-shifted constant never decrease.
1394 const APInt *C;
1395 if (match(I->getOperand(0), m_APInt(C)))
1396 Known.Zero.setHighBits(C->countl_zero());
1397 break;
1398 }
1399 case Instruction::AShr: {
1400 bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1401 auto KF = [Exact](const KnownBits &KnownVal, const KnownBits &KnownAmt,
1402 bool ShAmtNonZero) {
1403 return KnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1404 };
1405 computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2, Depth, Q,
1406 KF);
1407 break;
1408 }
1409 case Instruction::Sub: {
1410 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1411 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1412 computeKnownBitsAddSub(false, I->getOperand(0), I->getOperand(1), NSW, NUW,
1413 DemandedElts, Known, Known2, Depth, Q);
1414 break;
1415 }
1416 case Instruction::Add: {
1417 bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1418 bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1419 computeKnownBitsAddSub(true, I->getOperand(0), I->getOperand(1), NSW, NUW,
1420 DemandedElts, Known, Known2, Depth, Q);
1421 break;
1422 }
1423 case Instruction::SRem:
1424 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1425 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1426 Known = KnownBits::srem(Known, Known2);
1427 break;
1428
1429 case Instruction::URem:
1430 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1431 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1432 Known = KnownBits::urem(Known, Known2);
1433 break;
1434 case Instruction::Alloca:
1435 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
1436 break;
1437 case Instruction::GetElementPtr: {
1438 // Analyze all of the subscripts of this getelementptr instruction
1439 // to determine if we can prove known low zero bits.
1440 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1441 // Accumulate the constant indices in a separate variable
1442 // to minimize the number of calls to computeForAddSub.
1443 APInt AccConstIndices(BitWidth, 0, /*IsSigned*/ true);
1444
1446 for (unsigned i = 1, e = I->getNumOperands(); i != e; ++i, ++GTI) {
1447 // TrailZ can only become smaller, short-circuit if we hit zero.
1448 if (Known.isUnknown())
1449 break;
1450
1451 Value *Index = I->getOperand(i);
1452
1453 // Handle case when index is zero.
1454 Constant *CIndex = dyn_cast<Constant>(Index);
1455 if (CIndex && CIndex->isZeroValue())
1456 continue;
1457
1458 if (StructType *STy = GTI.getStructTypeOrNull()) {
1459 // Handle struct member offset arithmetic.
1460
1461 assert(CIndex &&
1462 "Access to structure field must be known at compile time");
1463
1464 if (CIndex->getType()->isVectorTy())
1465 Index = CIndex->getSplatValue();
1466
1467 unsigned Idx = cast<ConstantInt>(Index)->getZExtValue();
1468 const StructLayout *SL = Q.DL.getStructLayout(STy);
1470 AccConstIndices += Offset;
1471 continue;
1472 }
1473
1474 // Handle array index arithmetic.
1475 Type *IndexedTy = GTI.getIndexedType();
1476 if (!IndexedTy->isSized()) {
1477 Known.resetAll();
1478 break;
1479 }
1480
1481 unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits();
1482 KnownBits IndexBits(IndexBitWidth);
1483 computeKnownBits(Index, IndexBits, Depth + 1, Q);
1484 TypeSize IndexTypeSize = GTI.getSequentialElementStride(Q.DL);
1485 uint64_t TypeSizeInBytes = IndexTypeSize.getKnownMinValue();
1486 KnownBits ScalingFactor(IndexBitWidth);
1487 // Multiply by current sizeof type.
1488 // &A[i] == A + i * sizeof(*A[i]).
1489 if (IndexTypeSize.isScalable()) {
1490 // For scalable types the only thing we know about sizeof is
1491 // that this is a multiple of the minimum size.
1492 ScalingFactor.Zero.setLowBits(llvm::countr_zero(TypeSizeInBytes));
1493 } else if (IndexBits.isConstant()) {
1494 APInt IndexConst = IndexBits.getConstant();
1495 APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes);
1496 IndexConst *= ScalingFactor;
1497 AccConstIndices += IndexConst.sextOrTrunc(BitWidth);
1498 continue;
1499 } else {
1500 ScalingFactor =
1501 KnownBits::makeConstant(APInt(IndexBitWidth, TypeSizeInBytes));
1502 }
1503 IndexBits = KnownBits::mul(IndexBits, ScalingFactor);
1504
1505 // If the offsets have a different width from the pointer, according
1506 // to the language reference we need to sign-extend or truncate them
1507 // to the width of the pointer.
1508 IndexBits = IndexBits.sextOrTrunc(BitWidth);
1509
1510 // Note that inbounds does *not* guarantee nsw for the addition, as only
1511 // the offset is signed, while the base address is unsigned.
1512 Known = KnownBits::add(Known, IndexBits);
1513 }
1514 if (!Known.isUnknown() && !AccConstIndices.isZero()) {
1515 KnownBits Index = KnownBits::makeConstant(AccConstIndices);
1516 Known = KnownBits::add(Known, Index);
1517 }
1518 break;
1519 }
1520 case Instruction::PHI: {
1521 const PHINode *P = cast<PHINode>(I);
1522 BinaryOperator *BO = nullptr;
1523 Value *R = nullptr, *L = nullptr;
1524 if (matchSimpleRecurrence(P, BO, R, L)) {
1525 // Handle the case of a simple two-predecessor recurrence PHI.
1526 // There's a lot more that could theoretically be done here, but
1527 // this is sufficient to catch some interesting cases.
1528 unsigned Opcode = BO->getOpcode();
1529
1530 switch (Opcode) {
1531 // If this is a shift recurrence, we know the bits being shifted in. We
1532 // can combine that with information about the start value of the
1533 // recurrence to conclude facts about the result. If this is a udiv
1534 // recurrence, we know that the result can never exceed either the
1535 // numerator or the start value, whichever is greater.
1536 case Instruction::LShr:
1537 case Instruction::AShr:
1538 case Instruction::Shl:
1539 case Instruction::UDiv:
1540 if (BO->getOperand(0) != I)
1541 break;
1542 [[fallthrough]];
1543
1544 // For a urem recurrence, the result can never exceed the start value. The
1545 // phi could either be the numerator or the denominator.
1546 case Instruction::URem: {
1547 // We have matched a recurrence of the form:
1548 // %iv = [R, %entry], [%iv.next, %backedge]
1549 // %iv.next = shift_op %iv, L
1550
1551 // Recurse with the phi context to avoid concern about whether facts
1552 // inferred hold at original context instruction. TODO: It may be
1553 // correct to use the original context. IF warranted, explore and
1554 // add sufficient tests to cover.
1556 RecQ.CxtI = P;
1557 computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1558 switch (Opcode) {
1559 case Instruction::Shl:
1560 // A shl recurrence will only increase the tailing zeros
1561 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1562 break;
1563 case Instruction::LShr:
1564 case Instruction::UDiv:
1565 case Instruction::URem:
1566 // lshr, udiv, and urem recurrences will preserve the leading zeros of
1567 // the start value.
1568 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1569 break;
1570 case Instruction::AShr:
1571 // An ashr recurrence will extend the initial sign bit
1572 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1573 Known.One.setHighBits(Known2.countMinLeadingOnes());
1574 break;
1575 }
1576 break;
1577 }
1578
1579 // Check for operations that have the property that if
1580 // both their operands have low zero bits, the result
1581 // will have low zero bits.
1582 case Instruction::Add:
1583 case Instruction::Sub:
1584 case Instruction::And:
1585 case Instruction::Or:
1586 case Instruction::Mul: {
1587 // Change the context instruction to the "edge" that flows into the
1588 // phi. This is important because that is where the value is actually
1589 // "evaluated" even though it is used later somewhere else. (see also
1590 // D69571).
1592
1593 unsigned OpNum = P->getOperand(0) == R ? 0 : 1;
1594 Instruction *RInst = P->getIncomingBlock(OpNum)->getTerminator();
1595 Instruction *LInst = P->getIncomingBlock(1 - OpNum)->getTerminator();
1596
1597 // Ok, we have a PHI of the form L op= R. Check for low
1598 // zero bits.
1599 RecQ.CxtI = RInst;
1600 computeKnownBits(R, DemandedElts, Known2, Depth + 1, RecQ);
1601
1602 // We need to take the minimum number of known bits
1603 KnownBits Known3(BitWidth);
1604 RecQ.CxtI = LInst;
1605 computeKnownBits(L, DemandedElts, Known3, Depth + 1, RecQ);
1606
1607 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1608 Known3.countMinTrailingZeros()));
1609
1610 auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1611 if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1612 break;
1613
1614 switch (Opcode) {
1615 // If initial value of recurrence is nonnegative, and we are adding
1616 // a nonnegative number with nsw, the result can only be nonnegative
1617 // or poison value regardless of the number of times we execute the
1618 // add in phi recurrence. If initial value is negative and we are
1619 // adding a negative number with nsw, the result can only be
1620 // negative or poison value. Similar arguments apply to sub and mul.
1621 //
1622 // (add non-negative, non-negative) --> non-negative
1623 // (add negative, negative) --> negative
1624 case Instruction::Add: {
1625 if (Known2.isNonNegative() && Known3.isNonNegative())
1626 Known.makeNonNegative();
1627 else if (Known2.isNegative() && Known3.isNegative())
1628 Known.makeNegative();
1629 break;
1630 }
1631
1632 // (sub nsw non-negative, negative) --> non-negative
1633 // (sub nsw negative, non-negative) --> negative
1634 case Instruction::Sub: {
1635 if (BO->getOperand(0) != I)
1636 break;
1637 if (Known2.isNonNegative() && Known3.isNegative())
1638 Known.makeNonNegative();
1639 else if (Known2.isNegative() && Known3.isNonNegative())
1640 Known.makeNegative();
1641 break;
1642 }
1643
1644 // (mul nsw non-negative, non-negative) --> non-negative
1645 case Instruction::Mul:
1646 if (Known2.isNonNegative() && Known3.isNonNegative())
1647 Known.makeNonNegative();
1648 break;
1649
1650 default:
1651 break;
1652 }
1653 break;
1654 }
1655
1656 default:
1657 break;
1658 }
1659 }
1660
1661 // Unreachable blocks may have zero-operand PHI nodes.
1662 if (P->getNumIncomingValues() == 0)
1663 break;
1664
1665 // Otherwise take the unions of the known bit sets of the operands,
1666 // taking conservative care to avoid excessive recursion.
1667 if (Depth < MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1668 // Skip if every incoming value references to ourself.
1669 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1670 break;
1671
1672 Known.Zero.setAllBits();
1673 Known.One.setAllBits();
1674 for (const Use &U : P->operands()) {
1675 Value *IncValue;
1676 Instruction *CxtI;
1677 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
1678 // Skip direct self references.
1679 if (IncValue == P)
1680 continue;
1681
1682 // Change the context instruction to the "edge" that flows into the
1683 // phi. This is important because that is where the value is actually
1684 // "evaluated" even though it is used later somewhere else. (see also
1685 // D69571).
1687
1688 Known2 = KnownBits(BitWidth);
1689
1690 // Recurse, but cap the recursion to one level, because we don't
1691 // want to waste time spinning around in loops.
1692 // TODO: See if we can base recursion limiter on number of incoming phi
1693 // edges so we don't overly clamp analysis.
1694 computeKnownBits(IncValue, DemandedElts, Known2,
1695 MaxAnalysisRecursionDepth - 1, RecQ);
1696
1697 // See if we can further use a conditional branch into the phi
1698 // to help us determine the range of the value.
1699 if (!Known2.isConstant()) {
1700 CmpPredicate Pred;
1701 const APInt *RHSC;
1702 BasicBlock *TrueSucc, *FalseSucc;
1703 // TODO: Use RHS Value and compute range from its known bits.
1704 if (match(RecQ.CxtI,
1705 m_Br(m_c_ICmp(Pred, m_Specific(IncValue), m_APInt(RHSC)),
1706 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
1707 // Check for cases of duplicate successors.
1708 if ((TrueSucc == P->getParent()) != (FalseSucc == P->getParent())) {
1709 // If we're using the false successor, invert the predicate.
1710 if (FalseSucc == P->getParent())
1711 Pred = CmpInst::getInversePredicate(Pred);
1712 // Get the knownbits implied by the incoming phi condition.
1713 auto CR = ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1714 KnownBits KnownUnion = Known2.unionWith(CR.toKnownBits());
1715 // We can have conflicts here if we are analyzing deadcode (its
1716 // impossible for us reach this BB based the icmp).
1717 if (KnownUnion.hasConflict()) {
1718 // No reason to continue analyzing in a known dead region, so
1719 // just resetAll and break. This will cause us to also exit the
1720 // outer loop.
1721 Known.resetAll();
1722 break;
1723 }
1724 Known2 = KnownUnion;
1725 }
1726 }
1727 }
1728
1729 Known = Known.intersectWith(Known2);
1730 // If all bits have been ruled out, there's no need to check
1731 // more operands.
1732 if (Known.isUnknown())
1733 break;
1734 }
1735 }
1736 break;
1737 }
1738 case Instruction::Call:
1739 case Instruction::Invoke: {
1740 // If range metadata is attached to this call, set known bits from that,
1741 // and then intersect with known bits based on other properties of the
1742 // function.
1743 if (MDNode *MD =
1744 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1746
1747 const auto *CB = cast<CallBase>(I);
1748
1749 if (std::optional<ConstantRange> Range = CB->getRange())
1750 Known = Known.unionWith(Range->toKnownBits());
1751
1752 if (const Value *RV = CB->getReturnedArgOperand()) {
1753 if (RV->getType() == I->getType()) {
1754 computeKnownBits(RV, Known2, Depth + 1, Q);
1755 Known = Known.unionWith(Known2);
1756 // If the function doesn't return properly for all input values
1757 // (e.g. unreachable exits) then there might be conflicts between the
1758 // argument value and the range metadata. Simply discard the known bits
1759 // in case of conflicts.
1760 if (Known.hasConflict())
1761 Known.resetAll();
1762 }
1763 }
1764 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1765 switch (II->getIntrinsicID()) {
1766 default:
1767 break;
1768 case Intrinsic::abs: {
1769 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1770 bool IntMinIsPoison = match(II->getArgOperand(1), m_One());
1771 Known = Known2.abs(IntMinIsPoison);
1772 break;
1773 }
1774 case Intrinsic::bitreverse:
1775 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1776 Known.Zero |= Known2.Zero.reverseBits();
1777 Known.One |= Known2.One.reverseBits();
1778 break;
1779 case Intrinsic::bswap:
1780 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1781 Known.Zero |= Known2.Zero.byteSwap();
1782 Known.One |= Known2.One.byteSwap();
1783 break;
1784 case Intrinsic::ctlz: {
1785 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1786 // If we have a known 1, its position is our upper bound.
1787 unsigned PossibleLZ = Known2.countMaxLeadingZeros();
1788 // If this call is poison for 0 input, the result will be less than 2^n.
1789 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1790 PossibleLZ = std::min(PossibleLZ, BitWidth - 1);
1791 unsigned LowBits = llvm::bit_width(PossibleLZ);
1792 Known.Zero.setBitsFrom(LowBits);
1793 break;
1794 }
1795 case Intrinsic::cttz: {
1796 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1797 // If we have a known 1, its position is our upper bound.
1798 unsigned PossibleTZ = Known2.countMaxTrailingZeros();
1799 // If this call is poison for 0 input, the result will be less than 2^n.
1800 if (II->getArgOperand(1) == ConstantInt::getTrue(II->getContext()))
1801 PossibleTZ = std::min(PossibleTZ, BitWidth - 1);
1802 unsigned LowBits = llvm::bit_width(PossibleTZ);
1803 Known.Zero.setBitsFrom(LowBits);
1804 break;
1805 }
1806 case Intrinsic::ctpop: {
1807 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1808 // We can bound the space the count needs. Also, bits known to be zero
1809 // can't contribute to the population.
1810 unsigned BitsPossiblySet = Known2.countMaxPopulation();
1811 unsigned LowBits = llvm::bit_width(BitsPossiblySet);
1812 Known.Zero.setBitsFrom(LowBits);
1813 // TODO: we could bound KnownOne using the lower bound on the number
1814 // of bits which might be set provided by popcnt KnownOne2.
1815 break;
1816 }
1817 case Intrinsic::fshr:
1818 case Intrinsic::fshl: {
1819 const APInt *SA;
1820 if (!match(I->getOperand(2), m_APInt(SA)))
1821 break;
1822
1823 // Normalize to funnel shift left.
1824 uint64_t ShiftAmt = SA->urem(BitWidth);
1825 if (II->getIntrinsicID() == Intrinsic::fshr)
1826 ShiftAmt = BitWidth - ShiftAmt;
1827
1828 KnownBits Known3(BitWidth);
1829 computeKnownBits(I->getOperand(0), DemandedElts, Known2, Depth + 1, Q);
1830 computeKnownBits(I->getOperand(1), DemandedElts, Known3, Depth + 1, Q);
1831
1832 Known.Zero =
1833 Known2.Zero.shl(ShiftAmt) | Known3.Zero.lshr(BitWidth - ShiftAmt);
1834 Known.One =
1835 Known2.One.shl(ShiftAmt) | Known3.One.lshr(BitWidth - ShiftAmt);
1836 break;
1837 }
1838 case Intrinsic::uadd_sat:
1839 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1840 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1841 Known = KnownBits::uadd_sat(Known, Known2);
1842 break;
1843 case Intrinsic::usub_sat:
1844 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1845 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1846 Known = KnownBits::usub_sat(Known, Known2);
1847 break;
1848 case Intrinsic::sadd_sat:
1849 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1850 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1851 Known = KnownBits::sadd_sat(Known, Known2);
1852 break;
1853 case Intrinsic::ssub_sat:
1854 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1855 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1856 Known = KnownBits::ssub_sat(Known, Known2);
1857 break;
1858 // Vec reverse preserves bits from input vec.
1859 case Intrinsic::vector_reverse:
1860 computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known,
1861 Depth + 1, Q);
1862 break;
1863 // for min/max/and/or reduce, any bit common to each element in the
1864 // input vec is set in the output.
1865 case Intrinsic::vector_reduce_and:
1866 case Intrinsic::vector_reduce_or:
1867 case Intrinsic::vector_reduce_umax:
1868 case Intrinsic::vector_reduce_umin:
1869 case Intrinsic::vector_reduce_smax:
1870 case Intrinsic::vector_reduce_smin:
1871 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1872 break;
1873 case Intrinsic::vector_reduce_xor: {
1874 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
1875 // The zeros common to all vecs are zero in the output.
1876 // If the number of elements is odd, then the common ones remain. If the
1877 // number of elements is even, then the common ones becomes zeros.
1878 auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
1879 // Even, so the ones become zeros.
1880 bool EvenCnt = VecTy->getElementCount().isKnownEven();
1881 if (EvenCnt)
1882 Known.Zero |= Known.One;
1883 // Maybe even element count so need to clear ones.
1884 if (VecTy->isScalableTy() || EvenCnt)
1885 Known.One.clearAllBits();
1886 break;
1887 }
1888 case Intrinsic::umin:
1889 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1890 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1891 Known = KnownBits::umin(Known, Known2);
1892 break;
1893 case Intrinsic::umax:
1894 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1895 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1896 Known = KnownBits::umax(Known, Known2);
1897 break;
1898 case Intrinsic::smin:
1899 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1900 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1901 Known = KnownBits::smin(Known, Known2);
1903 break;
1904 case Intrinsic::smax:
1905 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1906 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1907 Known = KnownBits::smax(Known, Known2);
1909 break;
1910 case Intrinsic::ptrmask: {
1911 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1912
1913 const Value *Mask = I->getOperand(1);
1914 Known2 = KnownBits(Mask->getType()->getScalarSizeInBits());
1915 computeKnownBits(Mask, DemandedElts, Known2, Depth + 1, Q);
1916 // TODO: 1-extend would be more precise.
1917 Known &= Known2.anyextOrTrunc(BitWidth);
1918 break;
1919 }
1920 case Intrinsic::x86_sse2_pmulh_w:
1921 case Intrinsic::x86_avx2_pmulh_w:
1922 case Intrinsic::x86_avx512_pmulh_w_512:
1923 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1924 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1925 Known = KnownBits::mulhs(Known, Known2);
1926 break;
1927 case Intrinsic::x86_sse2_pmulhu_w:
1928 case Intrinsic::x86_avx2_pmulhu_w:
1929 case Intrinsic::x86_avx512_pmulhu_w_512:
1930 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth + 1, Q);
1931 computeKnownBits(I->getOperand(1), DemandedElts, Known2, Depth + 1, Q);
1932 Known = KnownBits::mulhu(Known, Known2);
1933 break;
1934 case Intrinsic::x86_sse42_crc32_64_64:
1935 Known.Zero.setBitsFrom(32);
1936 break;
1937 case Intrinsic::x86_ssse3_phadd_d_128:
1938 case Intrinsic::x86_ssse3_phadd_w_128:
1939 case Intrinsic::x86_avx2_phadd_d:
1940 case Intrinsic::x86_avx2_phadd_w: {
1942 I, DemandedElts, Depth, Q,
1943 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
1944 return KnownBits::add(KnownLHS, KnownRHS);
1945 });
1946 break;
1947 }
1948 case Intrinsic::x86_ssse3_phadd_sw_128:
1949 case Intrinsic::x86_avx2_phadd_sw: {
1950 Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
1952 break;
1953 }
1954 case Intrinsic::x86_ssse3_phsub_d_128:
1955 case Intrinsic::x86_ssse3_phsub_w_128:
1956 case Intrinsic::x86_avx2_phsub_d:
1957 case Intrinsic::x86_avx2_phsub_w: {
1959 I, DemandedElts, Depth, Q,
1960 [](const KnownBits &KnownLHS, const KnownBits &KnownRHS) {
1961 return KnownBits::sub(KnownLHS, KnownRHS);
1962 });
1963 break;
1964 }
1965 case Intrinsic::x86_ssse3_phsub_sw_128:
1966 case Intrinsic::x86_avx2_phsub_sw: {
1967 Known = computeKnownBitsForHorizontalOperation(I, DemandedElts, Depth,
1969 break;
1970 }
1971 case Intrinsic::riscv_vsetvli:
1972 case Intrinsic::riscv_vsetvlimax: {
1973 bool HasAVL = II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
1974 const ConstantRange Range = getVScaleRange(II->getFunction(), BitWidth);
1976 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
1977 RISCVII::VLMUL VLMUL = static_cast<RISCVII::VLMUL>(
1978 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
1979 uint64_t MaxVLEN =
1981 uint64_t MaxVL = MaxVLEN / RISCVVType::getSEWLMULRatio(SEW, VLMUL);
1982
1983 // Result of vsetvli must be not larger than AVL.
1984 if (HasAVL)
1985 if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
1986 MaxVL = std::min(MaxVL, CI->getZExtValue());
1987
1988 unsigned KnownZeroFirstBit = Log2_32(MaxVL) + 1;
1989 if (BitWidth > KnownZeroFirstBit)
1990 Known.Zero.setBitsFrom(KnownZeroFirstBit);
1991 break;
1992 }
1993 case Intrinsic::vscale: {
1994 if (!II->getParent() || !II->getFunction())
1995 break;
1996
1997 Known = getVScaleRange(II->getFunction(), BitWidth).toKnownBits();
1998 break;
1999 }
2000 }
2001 }
2002 break;
2003 }
2004 case Instruction::ShuffleVector: {
2005 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2006 // FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2007 if (!Shuf) {
2008 Known.resetAll();
2009 return;
2010 }
2011 // For undef elements, we don't know anything about the common state of
2012 // the shuffle result.
2013 APInt DemandedLHS, DemandedRHS;
2014 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2015 Known.resetAll();
2016 return;
2017 }
2018 Known.One.setAllBits();
2019 Known.Zero.setAllBits();
2020 if (!!DemandedLHS) {
2021 const Value *LHS = Shuf->getOperand(0);
2022 computeKnownBits(LHS, DemandedLHS, Known, Depth + 1, Q);
2023 // If we don't know any bits, early out.
2024 if (Known.isUnknown())
2025 break;
2026 }
2027 if (!!DemandedRHS) {
2028 const Value *RHS = Shuf->getOperand(1);
2029 computeKnownBits(RHS, DemandedRHS, Known2, Depth + 1, Q);
2030 Known = Known.intersectWith(Known2);
2031 }
2032 break;
2033 }
2034 case Instruction::InsertElement: {
2035 if (isa<ScalableVectorType>(I->getType())) {
2036 Known.resetAll();
2037 return;
2038 }
2039 const Value *Vec = I->getOperand(0);
2040 const Value *Elt = I->getOperand(1);
2041 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2042 unsigned NumElts = DemandedElts.getBitWidth();
2043 APInt DemandedVecElts = DemandedElts;
2044 bool NeedsElt = true;
2045 // If we know the index we are inserting too, clear it from Vec check.
2046 if (CIdx && CIdx->getValue().ult(NumElts)) {
2047 DemandedVecElts.clearBit(CIdx->getZExtValue());
2048 NeedsElt = DemandedElts[CIdx->getZExtValue()];
2049 }
2050
2051 Known.One.setAllBits();
2052 Known.Zero.setAllBits();
2053 if (NeedsElt) {
2054 computeKnownBits(Elt, Known, Depth + 1, Q);
2055 // If we don't know any bits, early out.
2056 if (Known.isUnknown())
2057 break;
2058 }
2059
2060 if (!DemandedVecElts.isZero()) {
2061 computeKnownBits(Vec, DemandedVecElts, Known2, Depth + 1, Q);
2062 Known = Known.intersectWith(Known2);
2063 }
2064 break;
2065 }
2066 case Instruction::ExtractElement: {
2067 // Look through extract element. If the index is non-constant or
2068 // out-of-range demand all elements, otherwise just the extracted element.
2069 const Value *Vec = I->getOperand(0);
2070 const Value *Idx = I->getOperand(1);
2071 auto *CIdx = dyn_cast<ConstantInt>(Idx);
2072 if (isa<ScalableVectorType>(Vec->getType())) {
2073 // FIXME: there's probably *something* we can do with scalable vectors
2074 Known.resetAll();
2075 break;
2076 }
2077 unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2078 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
2079 if (CIdx && CIdx->getValue().ult(NumElts))
2080 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2081 computeKnownBits(Vec, DemandedVecElts, Known, Depth + 1, Q);
2082 break;
2083 }
2084 case Instruction::ExtractValue:
2085 if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2086 const ExtractValueInst *EVI = cast<ExtractValueInst>(I);
2087 if (EVI->getNumIndices() != 1) break;
2088 if (EVI->getIndices()[0] == 0) {
2089 switch (II->getIntrinsicID()) {
2090 default: break;
2091 case Intrinsic::uadd_with_overflow:
2092 case Intrinsic::sadd_with_overflow:
2094 true, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2095 /* NUW=*/false, DemandedElts, Known, Known2, Depth, Q);
2096 break;
2097 case Intrinsic::usub_with_overflow:
2098 case Intrinsic::ssub_with_overflow:
2100 false, II->getArgOperand(0), II->getArgOperand(1), /*NSW=*/false,
2101 /* NUW=*/false, DemandedElts, Known, Known2, Depth, Q);
2102 break;
2103 case Intrinsic::umul_with_overflow:
2104 case Intrinsic::smul_with_overflow:
2105 computeKnownBitsMul(II->getArgOperand(0), II->getArgOperand(1), false,
2106 false, DemandedElts, Known, Known2, Depth, Q);
2107 break;
2108 }
2109 }
2110 }
2111 break;
2112 case Instruction::Freeze:
2113 if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2114 Depth + 1))
2115 computeKnownBits(I->getOperand(0), Known, Depth + 1, Q);
2116 break;
2117 }
2118}
2119
2120/// Determine which bits of V are known to be either zero or one and return
2121/// them.
2122KnownBits llvm::computeKnownBits(const Value *V, const APInt &DemandedElts,
2123 unsigned Depth, const SimplifyQuery &Q) {
2124 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2125 ::computeKnownBits(V, DemandedElts, Known, Depth, Q);
2126 return Known;
2127}
2128
2129/// Determine which bits of V are known to be either zero or one and return
2130/// them.
2132 const SimplifyQuery &Q) {
2133 KnownBits Known(getBitWidth(V->getType(), Q.DL));
2134 computeKnownBits(V, Known, Depth, Q);
2135 return Known;
2136}
2137
2138/// Determine which bits of V are known to be either zero or one and return
2139/// them in the Known bit set.
2140///
2141/// NOTE: we cannot consider 'undef' to be "IsZero" here. The problem is that
2142/// we cannot optimize based on the assumption that it is zero without changing
2143/// it to be an explicit zero. If we don't change it to zero, other code could
2144/// optimized based on the contradictory assumption that it is non-zero.
2145/// Because instcombine aggressively folds operations with undef args anyway,
2146/// this won't lose us code quality.
2147///
2148/// This function is defined on values with integer type, values with pointer
2149/// type, and vectors of integers. In the case
2150/// where V is a vector, known zero, and known one values are the
2151/// same width as the vector element, and the bit is set only if it is true
2152/// for all of the demanded elements in the vector specified by DemandedElts.
2153void computeKnownBits(const Value *V, const APInt &DemandedElts,
2154 KnownBits &Known, unsigned Depth,
2155 const SimplifyQuery &Q) {
2156 if (!DemandedElts) {
2157 // No demanded elts, better to assume we don't know anything.
2158 Known.resetAll();
2159 return;
2160 }
2161
2162 assert(V && "No Value?");
2163 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2164
2165#ifndef NDEBUG
2166 Type *Ty = V->getType();
2167 unsigned BitWidth = Known.getBitWidth();
2168
2170 "Not integer or pointer type!");
2171
2172 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2173 assert(
2174 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2175 "DemandedElt width should equal the fixed vector number of elements");
2176 } else {
2177 assert(DemandedElts == APInt(1, 1) &&
2178 "DemandedElt width should be 1 for scalars or scalable vectors");
2179 }
2180
2181 Type *ScalarTy = Ty->getScalarType();
2182 if (ScalarTy->isPointerTy()) {
2183 assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2184 "V and Known should have same BitWidth");
2185 } else {
2186 assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2187 "V and Known should have same BitWidth");
2188 }
2189#endif
2190
2191 const APInt *C;
2192 if (match(V, m_APInt(C))) {
2193 // We know all of the bits for a scalar constant or a splat vector constant!
2194 Known = KnownBits::makeConstant(*C);
2195 return;
2196 }
2197 // Null and aggregate-zero are all-zeros.
2198 if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
2199 Known.setAllZero();
2200 return;
2201 }
2202 // Handle a constant vector by taking the intersection of the known bits of
2203 // each element.
2204 if (const ConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) {
2205 assert(!isa<ScalableVectorType>(V->getType()));
2206 // We know that CDV must be a vector of integers. Take the intersection of
2207 // each element.
2208 Known.Zero.setAllBits(); Known.One.setAllBits();
2209 for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2210 if (!DemandedElts[i])
2211 continue;
2212 APInt Elt = CDV->getElementAsAPInt(i);
2213 Known.Zero &= ~Elt;
2214 Known.One &= Elt;
2215 }
2216 if (Known.hasConflict())
2217 Known.resetAll();
2218 return;
2219 }
2220
2221 if (const auto *CV = dyn_cast<ConstantVector>(V)) {
2222 assert(!isa<ScalableVectorType>(V->getType()));
2223 // We know that CV must be a vector of integers. Take the intersection of
2224 // each element.
2225 Known.Zero.setAllBits(); Known.One.setAllBits();
2226 for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2227 if (!DemandedElts[i])
2228 continue;
2229 Constant *Element = CV->getAggregateElement(i);
2230 if (isa<PoisonValue>(Element))
2231 continue;
2232 auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2233 if (!ElementCI) {
2234 Known.resetAll();
2235 return;
2236 }
2237 const APInt &Elt = ElementCI->getValue();
2238 Known.Zero &= ~Elt;
2239 Known.One &= Elt;
2240 }
2241 if (Known.hasConflict())
2242 Known.resetAll();
2243 return;
2244 }
2245
2246 // Start out not knowing anything.
2247 Known.resetAll();
2248
2249 // We can't imply anything about undefs.
2250 if (isa<UndefValue>(V))
2251 return;
2252
2253 // There's no point in looking through other users of ConstantData for
2254 // assumptions. Confirm that we've handled them all.
2255 assert(!isa<ConstantData>(V) && "Unhandled constant data!");
2256
2257 if (const auto *A = dyn_cast<Argument>(V))
2258 if (std::optional<ConstantRange> Range = A->getRange())
2259 Known = Range->toKnownBits();
2260
2261 // All recursive calls that increase depth must come after this.
2263 return;
2264
2265 // A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2266 // the bits of its aliasee.
2267 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2268 if (!GA->isInterposable())
2269 computeKnownBits(GA->getAliasee(), Known, Depth + 1, Q);
2270 return;
2271 }
2272
2273 if (const Operator *I = dyn_cast<Operator>(V))
2274 computeKnownBitsFromOperator(I, DemandedElts, Known, Depth, Q);
2275 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2276 if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2277 Known = CR->toKnownBits();
2278 }
2279
2280 // Aligned pointers have trailing zeros - refine Known.Zero set
2281 if (isa<PointerType>(V->getType())) {
2282 Align Alignment = V->getPointerAlignment(Q.DL);
2283 Known.Zero.setLowBits(Log2(Alignment));
2284 }
2285
2286 // computeKnownBitsFromContext strictly refines Known.
2287 // Therefore, we run them after computeKnownBitsFromOperator.
2288
2289 // Check whether we can determine known bits from context such as assumes.
2290 computeKnownBitsFromContext(V, Known, Depth, Q);
2291}
2292
2293/// Try to detect a recurrence that the value of the induction variable is
2294/// always a power of two (or zero).
2295static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero,
2296 unsigned Depth, SimplifyQuery &Q) {
2297 BinaryOperator *BO = nullptr;
2298 Value *Start = nullptr, *Step = nullptr;
2299 if (!matchSimpleRecurrence(PN, BO, Start, Step))
2300 return false;
2301
2302 // Initial value must be a power of two.
2303 for (const Use &U : PN->operands()) {
2304 if (U.get() == Start) {
2305 // Initial value comes from a different BB, need to adjust context
2306 // instruction for analysis.
2307 Q.CxtI = PN->getIncomingBlock(U)->getTerminator();
2308 if (!isKnownToBeAPowerOfTwo(Start, OrZero, Depth, Q))
2309 return false;
2310 }
2311 }
2312
2313 // Except for Mul, the induction variable must be on the left side of the
2314 // increment expression, otherwise its value can be arbitrary.
2315 if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2316 return false;
2317
2318 Q.CxtI = BO->getParent()->getTerminator();
2319 switch (BO->getOpcode()) {
2320 case Instruction::Mul:
2321 // Power of two is closed under multiplication.
2322 return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2323 Q.IIQ.hasNoSignedWrap(BO)) &&
2324 isKnownToBeAPowerOfTwo(Step, OrZero, Depth, Q);
2325 case Instruction::SDiv:
2326 // Start value must not be signmask for signed division, so simply being a
2327 // power of two is not sufficient, and it has to be a constant.
2328 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2329 return false;
2330 [[fallthrough]];
2331 case Instruction::UDiv:
2332 // Divisor must be a power of two.
2333 // If OrZero is false, cannot guarantee induction variable is non-zero after
2334 // division, same for Shr, unless it is exact division.
2335 return (OrZero || Q.IIQ.isExact(BO)) &&
2336 isKnownToBeAPowerOfTwo(Step, false, Depth, Q);
2337 case Instruction::Shl:
2338 return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2339 case Instruction::AShr:
2340 if (!match(Start, m_Power2()) || match(Start, m_SignMask()))
2341 return false;
2342 [[fallthrough]];
2343 case Instruction::LShr:
2344 return OrZero || Q.IIQ.isExact(BO);
2345 default:
2346 return false;
2347 }
2348}
2349
2350/// Return true if we can infer that \p V is known to be a power of 2 from
2351/// dominating condition \p Cond (e.g., ctpop(V) == 1).
2352static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero,
2353 const Value *Cond,
2354 bool CondIsTrue) {
2355 CmpPredicate Pred;
2356 const APInt *RHSC;
2357 if (!match(Cond, m_ICmp(Pred, m_Intrinsic<Intrinsic::ctpop>(m_Specific(V)),
2358 m_APInt(RHSC))))
2359 return false;
2360 if (!CondIsTrue)
2361 Pred = ICmpInst::getInversePredicate(Pred);
2362 // ctpop(V) u< 2
2363 if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2364 return true;
2365 // ctpop(V) == 1
2366 return Pred == ICmpInst::ICMP_EQ && *RHSC == 1;
2367}
2368
2369/// Return true if the given value is known to have exactly one
2370/// bit set when defined. For vectors return true if every element is known to
2371/// be a power of two when defined. Supports values with integer or pointer
2372/// types and vectors of integers.
2373bool llvm::isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
2374 const SimplifyQuery &Q) {
2375 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
2376
2377 if (isa<Constant>(V))
2378 return OrZero ? match(V, m_Power2OrZero()) : match(V, m_Power2());
2379
2380 // i1 is by definition a power of 2 or zero.
2381 if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2382 return true;
2383
2384 // Try to infer from assumptions.
2385 if (Q.AC && Q.CxtI) {
2386 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2387 if (!AssumeVH)
2388 continue;
2389 CallInst *I = cast<CallInst>(AssumeVH);
2390 if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero, I->getArgOperand(0),
2391 /*CondIsTrue=*/true) &&
2393 return true;
2394 }
2395 }
2396
2397 // Handle dominating conditions.
2398 if (Q.DC && Q.CxtI && Q.DT) {
2399 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2400 Value *Cond = BI->getCondition();
2401
2402 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2404 /*CondIsTrue=*/true) &&
2405 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2406 return true;
2407
2408 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2410 /*CondIsTrue=*/false) &&
2411 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2412 return true;
2413 }
2414 }
2415
2416 auto *I = dyn_cast<Instruction>(V);
2417 if (!I)
2418 return false;
2419
2420 if (Q.CxtI && match(V, m_VScale())) {
2421 const Function *F = Q.CxtI->getFunction();
2422 // The vscale_range indicates vscale is a power-of-two.
2423 return F->hasFnAttribute(Attribute::VScaleRange);
2424 }
2425
2426 // 1 << X is clearly a power of two if the one is not shifted off the end. If
2427 // it is shifted off the end then the result is undefined.
2428 if (match(I, m_Shl(m_One(), m_Value())))
2429 return true;
2430
2431 // (signmask) >>l X is clearly a power of two if the one is not shifted off
2432 // the bottom. If it is shifted off the bottom then the result is undefined.
2433 if (match(I, m_LShr(m_SignMask(), m_Value())))
2434 return true;
2435
2436 // The remaining tests are all recursive, so bail out if we hit the limit.
2438 return false;
2439
2440 switch (I->getOpcode()) {
2441 case Instruction::ZExt:
2442 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2443 case Instruction::Trunc:
2444 return OrZero && isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2445 case Instruction::Shl:
2446 if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2447 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2448 return false;
2449 case Instruction::LShr:
2450 if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2451 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2452 return false;
2453 case Instruction::UDiv:
2454 if (Q.IIQ.isExact(cast<BinaryOperator>(I)))
2455 return isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q);
2456 return false;
2457 case Instruction::Mul:
2458 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2459 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q) &&
2460 (OrZero || isKnownNonZero(I, Q, Depth));
2461 case Instruction::And:
2462 // A power of two and'd with anything is a power of two or zero.
2463 if (OrZero &&
2464 (isKnownToBeAPowerOfTwo(I->getOperand(1), /*OrZero*/ true, Depth, Q) ||
2465 isKnownToBeAPowerOfTwo(I->getOperand(0), /*OrZero*/ true, Depth, Q)))
2466 return true;
2467 // X & (-X) is always a power of two or zero.
2468 if (match(I->getOperand(0), m_Neg(m_Specific(I->getOperand(1)))) ||
2469 match(I->getOperand(1), m_Neg(m_Specific(I->getOperand(0)))))
2470 return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
2471 return false;
2472 case Instruction::Add: {
2473 // Adding a power-of-two or zero to the same power-of-two or zero yields
2474 // either the original power-of-two, a larger power-of-two or zero.
2475 const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
2476 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2477 Q.IIQ.hasNoSignedWrap(VOBO)) {
2478 if (match(I->getOperand(0),
2479 m_c_And(m_Specific(I->getOperand(1)), m_Value())) &&
2480 isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q))
2481 return true;
2482 if (match(I->getOperand(1),
2483 m_c_And(m_Specific(I->getOperand(0)), m_Value())) &&
2484 isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q))
2485 return true;
2486
2487 unsigned BitWidth = V->getType()->getScalarSizeInBits();
2488 KnownBits LHSBits(BitWidth);
2489 computeKnownBits(I->getOperand(0), LHSBits, Depth, Q);
2490
2491 KnownBits RHSBits(BitWidth);
2492 computeKnownBits(I->getOperand(1), RHSBits, Depth, Q);
2493 // If i8 V is a power of two or zero:
2494 // ZeroBits: 1 1 1 0 1 1 1 1
2495 // ~ZeroBits: 0 0 0 1 0 0 0 0
2496 if ((~(LHSBits.Zero & RHSBits.Zero)).isPowerOf2())
2497 // If OrZero isn't set, we cannot give back a zero result.
2498 // Make sure either the LHS or RHS has a bit set.
2499 if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2500 return true;
2501 }
2502
2503 // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2504 if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2505 if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
2506 return true;
2507 return false;
2508 }
2509 case Instruction::Select:
2510 return isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero, Depth, Q) &&
2511 isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero, Depth, Q);
2512 case Instruction::PHI: {
2513 // A PHI node is power of two if all incoming values are power of two, or if
2514 // it is an induction variable where in each step its value is a power of
2515 // two.
2516 auto *PN = cast<PHINode>(I);
2518
2519 // Check if it is an induction variable and always power of two.
2520 if (isPowerOfTwoRecurrence(PN, OrZero, Depth, RecQ))
2521 return true;
2522
2523 // Recursively check all incoming values. Limit recursion to 2 levels, so
2524 // that search complexity is limited to number of operands^2.
2525 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
2526 return llvm::all_of(PN->operands(), [&](const Use &U) {
2527 // Value is power of 2 if it is coming from PHI node itself by induction.
2528 if (U.get() == PN)
2529 return true;
2530
2531 // Change the context instruction to the incoming block where it is
2532 // evaluated.
2533 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
2534 return isKnownToBeAPowerOfTwo(U.get(), OrZero, NewDepth, RecQ);
2535 });
2536 }
2537 case Instruction::Invoke:
2538 case Instruction::Call: {
2539 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2540 switch (II->getIntrinsicID()) {
2541 case Intrinsic::umax:
2542 case Intrinsic::smax:
2543 case Intrinsic::umin:
2544 case Intrinsic::smin:
2545 return isKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero, Depth, Q) &&
2546 isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2547 // bswap/bitreverse just move around bits, but don't change any 1s/0s
2548 // thus dont change pow2/non-pow2 status.
2549 case Intrinsic::bitreverse:
2550 case Intrinsic::bswap:
2551 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2552 case Intrinsic::fshr:
2553 case Intrinsic::fshl:
2554 // If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2555 if (II->getArgOperand(0) == II->getArgOperand(1))
2556 return isKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero, Depth, Q);
2557 break;
2558 default:
2559 break;
2560 }
2561 }
2562 return false;
2563 }
2564 default:
2565 return false;
2566 }
2567}
2568
2569/// Test whether a GEP's result is known to be non-null.
2570///
2571/// Uses properties inherent in a GEP to try to determine whether it is known
2572/// to be non-null.
2573///
2574/// Currently this routine does not support vector GEPs.
2575static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth,
2576 const SimplifyQuery &Q) {
2577 const Function *F = nullptr;
2578 if (const Instruction *I = dyn_cast<Instruction>(GEP))
2579 F = I->getFunction();
2580
2581 // If the gep is nuw or inbounds with invalid null pointer, then the GEP
2582 // may be null iff the base pointer is null and the offset is zero.
2583 if (!GEP->hasNoUnsignedWrap() &&
2584 !(GEP->isInBounds() &&
2585 !NullPointerIsDefined(F, GEP->getPointerAddressSpace())))
2586 return false;
2587
2588 // FIXME: Support vector-GEPs.
2589 assert(GEP->getType()->isPointerTy() && "We only support plain pointer GEP");
2590
2591 // If the base pointer is non-null, we cannot walk to a null address with an
2592 // inbounds GEP in address space zero.
2593 if (isKnownNonZero(GEP->getPointerOperand(), Q, Depth))
2594 return true;
2595
2596 // Walk the GEP operands and see if any operand introduces a non-zero offset.
2597 // If so, then the GEP cannot produce a null pointer, as doing so would
2598 // inherently violate the inbounds contract within address space zero.
2600 GTI != GTE; ++GTI) {
2601 // Struct types are easy -- they must always be indexed by a constant.
2602 if (StructType *STy = GTI.getStructTypeOrNull()) {
2603 ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2604 unsigned ElementIdx = OpC->getZExtValue();
2605 const StructLayout *SL = Q.DL.getStructLayout(STy);
2606 uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2607 if (ElementOffset > 0)
2608 return true;
2609 continue;
2610 }
2611
2612 // If we have a zero-sized type, the index doesn't matter. Keep looping.
2613 if (GTI.getSequentialElementStride(Q.DL).isZero())
2614 continue;
2615
2616 // Fast path the constant operand case both for efficiency and so we don't
2617 // increment Depth when just zipping down an all-constant GEP.
2618 if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2619 if (!OpC->isZero())
2620 return true;
2621 continue;
2622 }
2623
2624 // We post-increment Depth here because while isKnownNonZero increments it
2625 // as well, when we pop back up that increment won't persist. We don't want
2626 // to recurse 10k times just because we have 10k GEP operands. We don't
2627 // bail completely out because we want to handle constant GEPs regardless
2628 // of depth.
2630 continue;
2631
2632 if (isKnownNonZero(GTI.getOperand(), Q, Depth))
2633 return true;
2634 }
2635
2636 return false;
2637}
2638
2640 const Instruction *CtxI,
2641 const DominatorTree *DT) {
2642 assert(!isa<Constant>(V) && "Called for constant?");
2643
2644 if (!CtxI || !DT)
2645 return false;
2646
2647 unsigned NumUsesExplored = 0;
2648 for (const auto *U : V->users()) {
2649 // Avoid massive lists
2650 if (NumUsesExplored >= DomConditionsMaxUses)
2651 break;
2652 NumUsesExplored++;
2653
2654 // If the value is used as an argument to a call or invoke, then argument
2655 // attributes may provide an answer about null-ness.
2656 if (const auto *CB = dyn_cast<CallBase>(U))
2657 if (auto *CalledFunc = CB->getCalledFunction())
2658 for (const Argument &Arg : CalledFunc->args())
2659 if (CB->getArgOperand(Arg.getArgNo()) == V &&
2660 Arg.hasNonNullAttr(/* AllowUndefOrPoison */ false) &&
2661 DT->dominates(CB, CtxI))
2662 return true;
2663
2664 // If the value is used as a load/store, then the pointer must be non null.
2665 if (V == getLoadStorePointerOperand(U)) {
2666 const Instruction *I = cast<Instruction>(U);
2667 if (!NullPointerIsDefined(I->getFunction(),
2668 V->getType()->getPointerAddressSpace()) &&
2669 DT->dominates(I, CtxI))
2670 return true;
2671 }
2672
2673 if ((match(U, m_IDiv(m_Value(), m_Specific(V))) ||
2674 match(U, m_IRem(m_Value(), m_Specific(V)))) &&
2675 isValidAssumeForContext(cast<Instruction>(U), CtxI, DT))
2676 return true;
2677
2678 // Consider only compare instructions uniquely controlling a branch
2679 Value *RHS;
2680 CmpPredicate Pred;
2681 if (!match(U, m_c_ICmp(Pred, m_Specific(V), m_Value(RHS))))
2682 continue;
2683
2684 bool NonNullIfTrue;
2685 if (cmpExcludesZero(Pred, RHS))
2686 NonNullIfTrue = true;
2688 NonNullIfTrue = false;
2689 else
2690 continue;
2691
2694 for (const auto *CmpU : U->users()) {
2695 assert(WorkList.empty() && "Should be!");
2696 if (Visited.insert(CmpU).second)
2697 WorkList.push_back(CmpU);
2698
2699 while (!WorkList.empty()) {
2700 auto *Curr = WorkList.pop_back_val();
2701
2702 // If a user is an AND, add all its users to the work list. We only
2703 // propagate "pred != null" condition through AND because it is only
2704 // correct to assume that all conditions of AND are met in true branch.
2705 // TODO: Support similar logic of OR and EQ predicate?
2706 if (NonNullIfTrue)
2707 if (match(Curr, m_LogicalAnd(m_Value(), m_Value()))) {
2708 for (const auto *CurrU : Curr->users())
2709 if (Visited.insert(CurrU).second)
2710 WorkList.push_back(CurrU);
2711 continue;
2712 }
2713
2714 if (const BranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2715 assert(BI->isConditional() && "uses a comparison!");
2716
2717 BasicBlock *NonNullSuccessor =
2718 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2719 BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2720 if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2721 return true;
2722 } else if (NonNullIfTrue && isGuard(Curr) &&
2723 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2724 return true;
2725 }
2726 }
2727 }
2728 }
2729
2730 return false;
2731}
2732
2733/// Does the 'Range' metadata (which must be a valid MD_range operand list)
2734/// ensure that the value it's attached to is never Value? 'RangeType' is
2735/// is the type of the value described by the range.
2736static bool rangeMetadataExcludesValue(const MDNode* Ranges, const APInt& Value) {
2737 const unsigned NumRanges = Ranges->getNumOperands() / 2;
2738 assert(NumRanges >= 1);
2739 for (unsigned i = 0; i < NumRanges; ++i) {
2741 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2743 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2744 ConstantRange Range(Lower->getValue(), Upper->getValue());
2745 if (Range.contains(Value))
2746 return false;
2747 }
2748 return true;
2749}
2750
2751/// Try to detect a recurrence that monotonically increases/decreases from a
2752/// non-zero starting value. These are common as induction variables.
2753static bool isNonZeroRecurrence(const PHINode *PN) {
2754 BinaryOperator *BO = nullptr;
2755 Value *Start = nullptr, *Step = nullptr;
2756 const APInt *StartC, *StepC;
2757 if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2758 !match(Start, m_APInt(StartC)) || StartC->isZero())
2759 return false;
2760
2761 switch (BO->getOpcode()) {
2762 case Instruction::Add:
2763 // Starting from non-zero and stepping away from zero can never wrap back
2764 // to zero.
2765 return BO->hasNoUnsignedWrap() ||
2766 (BO->hasNoSignedWrap() && match(Step, m_APInt(StepC)) &&
2767 StartC->isNegative() == StepC->isNegative());
2768 case Instruction::Mul:
2769 return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2770 match(Step, m_APInt(StepC)) && !StepC->isZero();
2771 case Instruction::Shl:
2772 return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
2773 case Instruction::AShr:
2774 case Instruction::LShr:
2775 return BO->isExact();
2776 default:
2777 return false;
2778 }
2779}
2780
2781static bool matchOpWithOpEqZero(Value *Op0, Value *Op1) {
2782 return match(Op0, m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,
2783 m_Specific(Op1), m_Zero()))) ||
2784 match(Op1, m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,
2785 m_Specific(Op0), m_Zero())));
2786}
2787
2788static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth,
2789 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2790 Value *Y, bool NSW, bool NUW) {
2791 // (X + (X != 0)) is non zero
2792 if (matchOpWithOpEqZero(X, Y))
2793 return true;
2794
2795 if (NUW)
2796 return isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2797 isKnownNonZero(X, DemandedElts, Q, Depth);
2798
2799 KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2800 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2801
2802 // If X and Y are both non-negative (as signed values) then their sum is not
2803 // zero unless both X and Y are zero.
2804 if (XKnown.isNonNegative() && YKnown.isNonNegative())
2805 if (isKnownNonZero(Y, DemandedElts, Q, Depth) ||
2806 isKnownNonZero(X, DemandedElts, Q, Depth))
2807 return true;
2808
2809 // If X and Y are both negative (as signed values) then their sum is not
2810 // zero unless both X and Y equal INT_MIN.
2811 if (XKnown.isNegative() && YKnown.isNegative()) {
2813 // The sign bit of X is set. If some other bit is set then X is not equal
2814 // to INT_MIN.
2815 if (XKnown.One.intersects(Mask))
2816 return true;
2817 // The sign bit of Y is set. If some other bit is set then Y is not equal
2818 // to INT_MIN.
2819 if (YKnown.One.intersects(Mask))
2820 return true;
2821 }
2822
2823 // The sum of a non-negative number and a power of two is not zero.
2824 if (XKnown.isNonNegative() &&
2825 isKnownToBeAPowerOfTwo(Y, /*OrZero*/ false, Depth, Q))
2826 return true;
2827 if (YKnown.isNonNegative() &&
2828 isKnownToBeAPowerOfTwo(X, /*OrZero*/ false, Depth, Q))
2829 return true;
2830
2831 return KnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
2832}
2833
2834static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth,
2835 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2836 Value *Y) {
2837 // (X - (X != 0)) is non zero
2838 // ((X != 0) - X) is non zero
2839 if (matchOpWithOpEqZero(X, Y))
2840 return true;
2841
2842 // TODO: Move this case into isKnownNonEqual().
2843 if (auto *C = dyn_cast<Constant>(X))
2844 if (C->isNullValue() && isKnownNonZero(Y, DemandedElts, Q, Depth))
2845 return true;
2846
2847 return ::isKnownNonEqual(X, Y, DemandedElts, Depth, Q);
2848}
2849
2850static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth,
2851 const SimplifyQuery &Q, unsigned BitWidth, Value *X,
2852 Value *Y, bool NSW, bool NUW) {
2853 // If X and Y are non-zero then so is X * Y as long as the multiplication
2854 // does not overflow.
2855 if (NSW || NUW)
2856 return isKnownNonZero(X, DemandedElts, Q, Depth) &&
2857 isKnownNonZero(Y, DemandedElts, Q, Depth);
2858
2859 // If either X or Y is odd, then if the other is non-zero the result can't
2860 // be zero.
2861 KnownBits XKnown = computeKnownBits(X, DemandedElts, Depth, Q);
2862 if (XKnown.One[0])
2863 return isKnownNonZero(Y, DemandedElts, Q, Depth);
2864
2865 KnownBits YKnown = computeKnownBits(Y, DemandedElts, Depth, Q);
2866 if (YKnown.One[0])
2867 return XKnown.isNonZero() || isKnownNonZero(X, DemandedElts, Q, Depth);
2868
2869 // If there exists any subset of X (sX) and subset of Y (sY) s.t sX * sY is
2870 // non-zero, then X * Y is non-zero. We can find sX and sY by just taking
2871 // the lowest known One of X and Y. If they are non-zero, the result
2872 // must be non-zero. We can check if LSB(X) * LSB(Y) != 0 by doing
2873 // X.CountLeadingZeros + Y.CountLeadingZeros < BitWidth.
2874 return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
2875 BitWidth;
2876}
2877
2878static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts,
2879 unsigned Depth, const SimplifyQuery &Q,
2880 const KnownBits &KnownVal) {
2881 auto ShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2882 switch (I->getOpcode()) {
2883 case Instruction::Shl:
2884 return Lhs.shl(Rhs);
2885 case Instruction::LShr:
2886 return Lhs.lshr(Rhs);
2887 case Instruction::AShr:
2888 return Lhs.ashr(Rhs);
2889 default:
2890 llvm_unreachable("Unknown Shift Opcode");
2891 }
2892 };
2893
2894 auto InvShiftOp = [&](const APInt &Lhs, const APInt &Rhs) {
2895 switch (I->getOpcode()) {
2896 case Instruction::Shl:
2897 return Lhs.lshr(Rhs);
2898 case Instruction::LShr:
2899 case Instruction::AShr:
2900 return Lhs.shl(Rhs);
2901 default:
2902 llvm_unreachable("Unknown Shift Opcode");
2903 }
2904 };
2905
2906 if (KnownVal.isUnknown())
2907 return false;
2908
2909 KnownBits KnownCnt =
2910 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
2911 APInt MaxShift = KnownCnt.getMaxValue();
2912 unsigned NumBits = KnownVal.getBitWidth();
2913 if (MaxShift.uge(NumBits))
2914 return false;
2915
2916 if (!ShiftOp(KnownVal.One, MaxShift).isZero())
2917 return true;
2918
2919 // If all of the bits shifted out are known to be zero, and Val is known
2920 // non-zero then at least one non-zero bit must remain.
2921 if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
2922 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
2923 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth))
2924 return true;
2925
2926 return false;
2927}
2928
2930 const APInt &DemandedElts,
2931 unsigned Depth, const SimplifyQuery &Q) {
2932 unsigned BitWidth = getBitWidth(I->getType()->getScalarType(), Q.DL);
2933 switch (I->getOpcode()) {
2934 case Instruction::Alloca:
2935 // Alloca never returns null, malloc might.
2936 return I->getType()->getPointerAddressSpace() == 0;
2937 case Instruction::GetElementPtr:
2938 if (I->getType()->isPointerTy())
2939 return isGEPKnownNonNull(cast<GEPOperator>(I), Depth, Q);
2940 break;
2941 case Instruction::BitCast: {
2942 // We need to be a bit careful here. We can only peek through the bitcast
2943 // if the scalar size of elements in the operand are smaller than and a
2944 // multiple of the size they are casting too. Take three cases:
2945 //
2946 // 1) Unsafe:
2947 // bitcast <2 x i16> %NonZero to <4 x i8>
2948 //
2949 // %NonZero can have 2 non-zero i16 elements, but isKnownNonZero on a
2950 // <4 x i8> requires that all 4 i8 elements be non-zero which isn't
2951 // guranteed (imagine just sign bit set in the 2 i16 elements).
2952 //
2953 // 2) Unsafe:
2954 // bitcast <4 x i3> %NonZero to <3 x i4>
2955 //
2956 // Even though the scalar size of the src (`i3`) is smaller than the
2957 // scalar size of the dst `i4`, because `i3` is not a multiple of `i4`
2958 // its possible for the `3 x i4` elements to be zero because there are
2959 // some elements in the destination that don't contain any full src
2960 // element.
2961 //
2962 // 3) Safe:
2963 // bitcast <4 x i8> %NonZero to <2 x i16>
2964 //
2965 // This is always safe as non-zero in the 4 i8 elements implies
2966 // non-zero in the combination of any two adjacent ones. Since i8 is a
2967 // multiple of i16, each i16 is guranteed to have 2 full i8 elements.
2968 // This all implies the 2 i16 elements are non-zero.
2969 Type *FromTy = I->getOperand(0)->getType();
2970 if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
2971 (BitWidth % getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
2972 return isKnownNonZero(I->getOperand(0), Q, Depth);
2973 } break;
2974 case Instruction::IntToPtr:
2975 // Note that we have to take special care to avoid looking through
2976 // truncating casts, e.g., int2ptr/ptr2int with appropriate sizes, as well
2977 // as casts that can alter the value, e.g., AddrSpaceCasts.
2978 if (!isa<ScalableVectorType>(I->getType()) &&
2979 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2980 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2981 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2982 break;
2983 case Instruction::PtrToInt:
2984 // Similar to int2ptr above, we can look through ptr2int here if the cast
2985 // is a no-op or an extend and not a truncate.
2986 if (!isa<ScalableVectorType>(I->getType()) &&
2987 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2988 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2989 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
2990 break;
2991 case Instruction::Trunc:
2992 // nuw/nsw trunc preserves zero/non-zero status of input.
2993 if (auto *TI = dyn_cast<TruncInst>(I))
2994 if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
2995 return isKnownNonZero(TI->getOperand(0), DemandedElts, Q, Depth);
2996 break;
2997
2998 case Instruction::Sub:
2999 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
3000 I->getOperand(1));
3001 case Instruction::Xor:
3002 // (X ^ (X != 0)) is non zero
3003 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3004 return true;
3005 break;
3006 case Instruction::Or:
3007 // (X | (X != 0)) is non zero
3008 if (matchOpWithOpEqZero(I->getOperand(0), I->getOperand(1)))
3009 return true;
3010 // X | Y != 0 if X != 0 or Y != 0.
3011 return isKnownNonZero(I->getOperand(1), DemandedElts, Q, Depth) ||
3012 isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3013 case Instruction::SExt:
3014 case Instruction::ZExt:
3015 // ext X != 0 if X != 0.
3016 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3017
3018 case Instruction::Shl: {
3019 // shl nsw/nuw can't remove any non-zero bits.
3020 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
3021 if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3022 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3023
3024 // shl X, Y != 0 if X is odd. Note that the value of the shift is undefined
3025 // if the lowest bit is shifted off the end.
3026 KnownBits Known(BitWidth);
3027 computeKnownBits(I->getOperand(0), DemandedElts, Known, Depth, Q);
3028 if (Known.One[0])
3029 return true;
3030
3031 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
3032 }
3033 case Instruction::LShr:
3034 case Instruction::AShr: {
3035 // shr exact can only shift out zero bits.
3036 const PossiblyExactOperator *BO = cast<PossiblyExactOperator>(I);
3037 if (BO->isExact())
3038 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3039
3040 // shr X, Y != 0 if X is negative. Note that the value of the shift is not
3041 // defined if the sign bit is shifted off the end.
3042 KnownBits Known =
3043 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
3044 if (Known.isNegative())
3045 return true;
3046
3047 return isNonZeroShift(I, DemandedElts, Depth, Q, Known);
3048 }
3049 case Instruction::UDiv:
3050 case Instruction::SDiv: {
3051 // X / Y
3052 // div exact can only produce a zero if the dividend is zero.
3053 if (cast<PossiblyExactOperator>(I)->isExact())
3054 return isKnownNonZero(I->getOperand(0), DemandedElts, Q, Depth);
3055
3056 KnownBits XKnown =
3057 computeKnownBits(I->getOperand(0), DemandedElts, Depth, Q);
3058 // If X is fully unknown we won't be able to figure anything out so don't
3059 // both computing knownbits for Y.
3060 if (XKnown.isUnknown())
3061 return false;
3062
3063 KnownBits YKnown =
3064 computeKnownBits(I->getOperand(1), DemandedElts, Depth, Q);
3065 if (I->getOpcode() == Instruction::SDiv) {
3066 // For signed division need to compare abs value of the operands.
3067 XKnown = XKnown.abs(/*IntMinIsPoison*/ false);
3068 YKnown = YKnown.abs(/*IntMinIsPoison*/ false);
3069 }
3070 // If X u>= Y then div is non zero (0/0 is UB).
3071 std::optional<bool> XUgeY = KnownBits::uge(XKnown, YKnown);
3072 // If X is total unknown or X u< Y we won't be able to prove non-zero
3073 // with compute known bits so just return early.
3074 return XUgeY && *XUgeY;
3075 }
3076 case Instruction::Add: {
3077 // X + Y.
3078
3079 // If Add has nuw wrap flag, then if either X or Y is non-zero the result is
3080 // non-zero.
3081 auto *BO = cast<OverflowingBinaryOperator>(I);
3082 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
3083 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3084 Q.IIQ.hasNoUnsignedWrap(BO));
3085 }
3086 case Instruction::Mul: {
3087 const OverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
3088 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth, I->getOperand(0),
3089 I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3090 Q.IIQ.hasNoUnsignedWrap(BO));
3091 }
3092 case Instruction::Select: {
3093 // (C ? X : Y) != 0 if X != 0 and Y != 0.
3094
3095 // First check if the arm is non-zero using `isKnownNonZero`. If that fails,
3096 // then see if the select condition implies the arm is non-zero. For example
3097 // (X != 0 ? X : Y), we know the true arm is non-zero as the `X` "return" is
3098 // dominated by `X != 0`.
3099 auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3100 Value *Op;
3101 Op = IsTrueArm ? I->getOperand(1) : I->getOperand(2);
3102 // Op is trivially non-zero.
3103 if (isKnownNonZero(Op, DemandedElts, Q, Depth))
3104 return true;
3105
3106 // The condition of the select dominates the true/false arm. Check if the
3107 // condition implies that a given arm is non-zero.
3108 Value *X;
3109 CmpPredicate Pred;
3110 if (!match(I->getOperand(0), m_c_ICmp(Pred, m_Specific(Op), m_Value(X))))
3111 return false;
3112
3113 if (!IsTrueArm)
3114 Pred = ICmpInst::getInversePredicate(Pred);
3115
3116 return cmpExcludesZero(Pred, X);
3117 };
3118
3119 if (SelectArmIsNonZero(/* IsTrueArm */ true) &&
3120 SelectArmIsNonZero(/* IsTrueArm */ false))
3121 return true;
3122 break;
3123 }
3124 case Instruction::PHI: {
3125 auto *PN = cast<PHINode>(I);
3127 return true;
3128
3129 // Check if all incoming values are non-zero using recursion.
3131 unsigned NewDepth = std::max(Depth, MaxAnalysisRecursionDepth - 1);
3132 return llvm::all_of(PN->operands(), [&](const Use &U) {
3133 if (U.get() == PN)
3134 return true;
3135 RecQ.CxtI = PN->getIncomingBlock(U)->getTerminator();
3136 // Check if the branch on the phi excludes zero.
3137 CmpPredicate Pred;
3138 Value *X;
3139 BasicBlock *TrueSucc, *FalseSucc;
3140 if (match(RecQ.CxtI,
3141 m_Br(m_c_ICmp(Pred, m_Specific(U.get()), m_Value(X)),
3142 m_BasicBlock(TrueSucc), m_BasicBlock(FalseSucc)))) {
3143 // Check for cases of duplicate successors.
3144 if ((TrueSucc == PN->getParent()) != (FalseSucc == PN->getParent())) {
3145 // If we're using the false successor, invert the predicate.
3146 if (FalseSucc == PN->getParent())
3147 Pred = CmpInst::getInversePredicate(Pred);
3148 if (cmpExcludesZero(Pred, X))
3149 return true;
3150 }
3151 }
3152 // Finally recurse on the edge and check it directly.
3153 return isKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3154 });
3155 }
3156 case Instruction::InsertElement: {
3157 if (isa<ScalableVectorType>(I->getType()))
3158 break;
3159
3160 const Value *Vec = I->getOperand(0);
3161 const Value *Elt = I->getOperand(1);
3162 auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3163
3164 unsigned NumElts = DemandedElts.getBitWidth();
3165 APInt DemandedVecElts = DemandedElts;
3166 bool SkipElt = false;
3167 // If we know the index we are inserting too, clear it from Vec check.
3168 if (CIdx && CIdx->getValue().ult(NumElts)) {
3169 DemandedVecElts.clearBit(CIdx->getZExtValue());
3170 SkipElt = !DemandedElts[CIdx->getZExtValue()];
3171 }
3172
3173 // Result is zero if Elt is non-zero and rest of the demanded elts in Vec
3174 // are non-zero.
3175 return (SkipElt || isKnownNonZero(Elt, Q, Depth)) &&
3176 (DemandedVecElts.isZero() ||
3177 isKnownNonZero(Vec, DemandedVecElts, Q, Depth));
3178 }
3179 case Instruction::ExtractElement:
3180 if (const auto *EEI = dyn_cast<ExtractElementInst>(I)) {
3181 const Value *Vec = EEI->getVectorOperand();
3182 const Value *Idx = EEI->getIndexOperand();
3183 auto *CIdx = dyn_cast<ConstantInt>(Idx);
3184 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3185 unsigned NumElts = VecTy->getNumElements();
3186 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
3187 if (CIdx && CIdx->getValue().ult(NumElts))
3188 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3189 return isKnownNonZero(Vec, DemandedVecElts, Q, Depth);
3190 }
3191 }
3192 break;
3193 case Instruction::ShuffleVector: {
3194 auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3195 if (!Shuf)
3196 break;
3197 APInt DemandedLHS, DemandedRHS;
3198 // For undef elements, we don't know anything about the common state of
3199 // the shuffle result.
3200 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3201 break;
3202 // If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3203 return (DemandedRHS.isZero() ||
3204 isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q, Depth)) &&
3205 (DemandedLHS.isZero() ||
3206 isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q, Depth));
3207 }
3208 case Instruction::Freeze:
3209 return isKnownNonZero(I->getOperand(0), Q, Depth) &&
3210 isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3211 Depth);
3212 case Instruction::Load: {
3213 auto *LI = cast<LoadInst>(I);
3214 // A Load tagged with nonnull or dereferenceable with null pointer undefined
3215 // is never null.
3216 if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3217 if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3218 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3219 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3220 return true;
3221 } else if (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3223 }
3224
3225 // No need to fall through to computeKnownBits as range metadata is already
3226 // handled in isKnownNonZero.
3227 return false;
3228 }
3229 case Instruction::ExtractValue: {
3230 const WithOverflowInst *WO;
3231 if (match(I, m_ExtractValue<0>(m_WithOverflowInst(WO)))) {
3232 switch (WO->getBinaryOp()) {
3233 default:
3234 break;
3235 case Instruction::Add:
3236 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
3237 WO->getArgOperand(0), WO->getArgOperand(1),
3238 /*NSW=*/false,
3239 /*NUW=*/false);
3240 case Instruction::Sub:
3241 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
3242 WO->getArgOperand(0), WO->getArgOperand(1));
3243 case Instruction::Mul:
3244 return isNonZeroMul(DemandedElts, Depth, Q, BitWidth,
3245 WO->getArgOperand(0), WO->getArgOperand(1),
3246 /*NSW=*/false, /*NUW=*/false);
3247 break;
3248 }
3249 }
3250 break;
3251 }
3252 case Instruction::Call:
3253 case Instruction::Invoke: {
3254 const auto *Call = cast<CallBase>(I);
3255 if (I->getType()->isPointerTy()) {
3256 if (Call->isReturnNonNull())
3257 return true;
3258 if (const auto *RP = getArgumentAliasingToReturnedPointer(Call, true))
3259 return isKnownNonZero(RP, Q, Depth);
3260 } else {
3261 if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3263 if (std::optional<ConstantRange> Range = Call->getRange()) {
3264 const APInt ZeroValue(Range->getBitWidth(), 0);
3265 if (!Range->contains(ZeroValue))
3266 return true;
3267 }
3268 if (const Value *RV = Call->getReturnedArgOperand())
3269 if (RV->getType() == I->getType() && isKnownNonZero(RV, Q, Depth))
3270 return true;
3271 }
3272
3273 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3274 switch (II->getIntrinsicID()) {
3275 case Intrinsic::sshl_sat:
3276 case Intrinsic::ushl_sat:
3277 case Intrinsic::abs:
3278 case Intrinsic::bitreverse:
3279 case Intrinsic::bswap:
3280 case Intrinsic::ctpop:
3281 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3282 // NB: We don't do usub_sat here as in any case we can prove its
3283 // non-zero, we will fold it to `sub nuw` in InstCombine.
3284 case Intrinsic::ssub_sat:
3285 return isNonZeroSub(DemandedElts, Depth, Q, BitWidth,
3286 II->getArgOperand(0), II->getArgOperand(1));
3287 case Intrinsic::sadd_sat:
3288 return isNonZeroAdd(DemandedElts, Depth, Q, BitWidth,
3289 II->getArgOperand(0), II->getArgOperand(1),
3290 /*NSW=*/true, /* NUW=*/false);
3291 // Vec reverse preserves zero/non-zero status from input vec.
3292 case Intrinsic::vector_reverse:
3293 return isKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3294 Q, Depth);
3295 // umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3296 case Intrinsic::vector_reduce_or:
3297 case Intrinsic::vector_reduce_umax:
3298 case Intrinsic::vector_reduce_umin:
3299 case Intrinsic::vector_reduce_smax:
3300 case Intrinsic::vector_reduce_smin:
3301 return isKnownNonZero(II->getArgOperand(0), Q, Depth);
3302 case Intrinsic::umax:
3303 case Intrinsic::uadd_sat:
3304 // umax(X, (X != 0)) is non zero
3305 // X +usat (X != 0) is non zero
3306 if (matchOpWithOpEqZero(II->getArgOperand(0), II->getArgOperand(1)))
3307 return true;
3308
3309 return isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth) ||
3310 isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3311 case Intrinsic::smax: {
3312 // If either arg is strictly positive the result is non-zero. Otherwise
3313 // the result is non-zero if both ops are non-zero.
3314 auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3315 const KnownBits &OpKnown) {
3316 if (!OpNonZero.has_value())
3317 OpNonZero = OpKnown.isNonZero() ||
3318 isKnownNonZero(Op, DemandedElts, Q, Depth);
3319 return *OpNonZero;
3320 };
3321 // Avoid re-computing isKnownNonZero.
3322 std::optional<bool> Op0NonZero, Op1NonZero;
3323 KnownBits Op1Known =
3324 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
3325 if (Op1Known.isNonNegative() &&
3326 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3327 return true;
3328 KnownBits Op0Known =
3329 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
3330 if (Op0Known.isNonNegative() &&
3331 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3332 return true;
3333 return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3334 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3335 }
3336 case Intrinsic::smin: {
3337 // If either arg is negative the result is non-zero. Otherwise
3338 // the result is non-zero if both ops are non-zero.
3339 KnownBits Op1Known =
3340 computeKnownBits(II->getArgOperand(1), DemandedElts, Depth, Q);
3341 if (Op1Known.isNegative())
3342 return true;
3343 KnownBits Op0Known =
3344 computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q);
3345 if (Op0Known.isNegative())
3346 return true;
3347
3348 if (Op1Known.isNonZero() && Op0Known.isNonZero())
3349 return true;
3350 }
3351 [[fallthrough]];
3352 case Intrinsic::umin:
3353 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth) &&
3354 isKnownNonZero(II->getArgOperand(1), DemandedElts, Q, Depth);
3355 case Intrinsic::cttz:
3356 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
3357 .Zero[0];
3358 case Intrinsic::ctlz:
3359 return computeKnownBits(II->getArgOperand(0), DemandedElts, Depth, Q)
3360 .isNonNegative();
3361 case Intrinsic::fshr:
3362 case Intrinsic::fshl:
3363 // If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3364 if (II->getArgOperand(0) == II->getArgOperand(1))
3365 return isKnownNonZero(II->getArgOperand(0), DemandedElts, Q, Depth);
3366 break;
3367 case Intrinsic::vscale:
3368 return true;
3369 case Intrinsic::experimental_get_vector_length:
3370 return isKnownNonZero(I->getOperand(0), Q, Depth);
3371 default:
3372 break;
3373 }
3374 break;
3375 }
3376
3377 return false;
3378 }
3379 }
3380
3381 KnownBits Known(BitWidth);
3382 computeKnownBits(I, DemandedElts, Known, Depth, Q);
3383 return Known.One != 0;
3384}
3385
3386/// Return true if the given value is known to be non-zero when defined. For
3387/// vectors, return true if every demanded element is known to be non-zero when
3388/// defined. For pointers, if the context instruction and dominator tree are
3389/// specified, perform context-sensitive analysis and return true if the
3390/// pointer couldn't possibly be null at the specified instruction.
3391/// Supports values with integer or pointer type and vectors of integers.
3392bool isKnownNonZero(const Value *V, const APInt &DemandedElts,
3393 const SimplifyQuery &Q, unsigned Depth) {
3394 Type *Ty = V->getType();
3395
3396#ifndef NDEBUG
3397 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3398
3399 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3400 assert(
3401 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3402 "DemandedElt width should equal the fixed vector number of elements");
3403 } else {
3404 assert(DemandedElts == APInt(1, 1) &&
3405 "DemandedElt width should be 1 for scalars");
3406 }
3407#endif
3408
3409 if (auto *C = dyn_cast<Constant>(V)) {
3410 if (C->isNullValue())
3411 return false;
3412 if (isa<ConstantInt>(C))
3413 // Must be non-zero due to null test above.
3414 return true;
3415
3416 // For constant vectors, check that all elements are poison or known
3417 // non-zero to determine that the whole vector is known non-zero.
3418 if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3419 for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3420 if (!DemandedElts[i])
3421 continue;
3422 Constant *Elt = C->getAggregateElement(i);
3423 if (!Elt || Elt->isNullValue())
3424 return false;
3425 if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3426 return false;
3427 }
3428 return true;
3429 }
3430
3431 // Constant ptrauth can be null, iff the base pointer can be.
3432 if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3433 return isKnownNonZero(CPA->getPointer(), DemandedElts, Q, Depth);
3434
3435 // A global variable in address space 0 is non null unless extern weak
3436 // or an absolute symbol reference. Other address spaces may have null as a
3437 // valid address for a global, so we can't assume anything.
3438 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3439 if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3440 GV->getType()->getAddressSpace() == 0)
3441 return true;
3442 }
3443
3444 // For constant expressions, fall through to the Operator code below.
3445 if (!isa<ConstantExpr>(V))
3446 return false;
3447 }
3448
3449 if (const auto *A = dyn_cast<Argument>(V))
3450 if (std::optional<ConstantRange> Range = A->getRange()) {
3451 const APInt ZeroValue(Range->getBitWidth(), 0);
3452 if (!Range->contains(ZeroValue))
3453 return true;
3454 }
3455
3456 if (!isa<Constant>(V) && isKnownNonZeroFromAssume(V, Q))
3457 return true;
3458
3459 // Some of the tests below are recursive, so bail out if we hit the limit.
3461 return false;
3462
3463 // Check for pointer simplifications.
3464
3465 if (PointerType *PtrTy = dyn_cast<PointerType>(Ty)) {
3466 // A byval, inalloca may not be null in a non-default addres space. A
3467 // nonnull argument is assumed never 0.
3468 if (const Argument *A = dyn_cast<Argument>(V)) {
3469 if (((A->hasPassPointeeByValueCopyAttr() &&
3470 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3471 A->hasNonNullAttr()))
3472 return true;
3473 }
3474 }
3475
3476 if (const auto *I = dyn_cast<Operator>(V))
3477 if (isKnownNonZeroFromOperator(I, DemandedElts, Depth, Q))
3478 return true;
3479
3480 if (!isa<Constant>(V) &&
3482 return true;
3483
3484 return false;
3485}
3486
3488 unsigned Depth) {
3489 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3490 APInt DemandedElts =
3491 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
3492 return ::isKnownNonZero(V, DemandedElts, Q, Depth);
3493}
3494
3495/// If the pair of operators are the same invertible function, return the
3496/// the operands of the function corresponding to each input. Otherwise,
3497/// return std::nullopt. An invertible function is one that is 1-to-1 and maps
3498/// every input value to exactly one output value. This is equivalent to
3499/// saying that Op1 and Op2 are equal exactly when the specified pair of
3500/// operands are equal, (except that Op1 and Op2 may be poison more often.)
3501static std::optional<std::pair<Value*, Value*>>
3503 const Operator *Op2) {
3504 if (Op1->getOpcode() != Op2->getOpcode())
3505 return std::nullopt;
3506
3507 auto getOperands = [&](unsigned OpNum) -> auto {
3508 return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3509 };
3510
3511 switch (Op1->getOpcode()) {
3512 default:
3513 break;
3514 case Instruction::Or:
3515 if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3516 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3517 break;
3518 [[fallthrough]];
3519 case Instruction::Xor:
3520 case Instruction::Add: {
3521 Value *Other;
3522 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(0)), m_Value(Other))))
3523 return std::make_pair(Op1->getOperand(1), Other);
3524 if (match(Op2, m_c_BinOp(m_Specific(Op1->getOperand(1)), m_Value(Other))))
3525 return std::make_pair(Op1->getOperand(0), Other);
3526 break;
3527 }
3528 case Instruction::Sub:
3529 if (Op1->getOperand(0) == Op2->getOperand(0))
3530 return getOperands(1);
3531 if (Op1->getOperand(1) == Op2->getOperand(1))
3532 return getOperands(0);
3533 break;
3534 case Instruction::Mul: {
3535 // invertible if A * B == (A * B) mod 2^N where A, and B are integers
3536 // and N is the bitwdith. The nsw case is non-obvious, but proven by
3537 // alive2: https://alive2.llvm.org/ce/z/Z6D5qK
3538 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3539 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3540 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3541 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3542 break;
3543
3544 // Assume operand order has been canonicalized
3545 if (Op1->getOperand(1) == Op2->getOperand(1) &&
3546 isa<ConstantInt>(Op1->getOperand(1)) &&
3547 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3548 return getOperands(0);
3549 break;
3550 }
3551 case Instruction::Shl: {
3552 // Same as multiplies, with the difference that we don't need to check
3553 // for a non-zero multiply. Shifts always multiply by non-zero.
3554 auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3555 auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3556 if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3557 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3558 break;
3559
3560 if (Op1->getOperand(1) == Op2->getOperand(1))
3561 return getOperands(0);
3562 break;
3563 }
3564 case Instruction::AShr:
3565 case Instruction::LShr: {
3566 auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3567 auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3568 if (!PEO1->isExact() || !PEO2->isExact())
3569 break;
3570
3571 if (Op1->getOperand(1) == Op2->getOperand(1))
3572 return getOperands(0);
3573 break;
3574 }
3575 case Instruction::SExt:
3576 case Instruction::ZExt:
3577 if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3578 return getOperands(0);
3579 break;
3580 case Instruction::PHI: {
3581 const PHINode *PN1 = cast<PHINode>(Op1);
3582 const PHINode *PN2 = cast<PHINode>(Op2);
3583
3584 // If PN1 and PN2 are both recurrences, can we prove the entire recurrences
3585 // are a single invertible function of the start values? Note that repeated
3586 // application of an invertible function is also invertible
3587 BinaryOperator *BO1 = nullptr;
3588 Value *Start1 = nullptr, *Step1 = nullptr;
3589 BinaryOperator *BO2 = nullptr;
3590 Value *Start2 = nullptr, *Step2 = nullptr;
3591 if (PN1->getParent() != PN2->getParent() ||
3592 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3593 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3594 break;
3595
3596 auto Values = getInvertibleOperands(cast<Operator>(BO1),
3597 cast<Operator>(BO2));
3598 if (!Values)
3599 break;
3600
3601 // We have to be careful of mutually defined recurrences here. Ex:
3602 // * X_i = X_(i-1) OP Y_(i-1), and Y_i = X_(i-1) OP V
3603 // * X_i = Y_i = X_(i-1) OP Y_(i-1)
3604 // The invertibility of these is complicated, and not worth reasoning
3605 // about (yet?).
3606 if (Values->first != PN1 || Values->second != PN2)
3607 break;
3608
3609 return std::make_pair(Start1, Start2);
3610 }
3611 }
3612 return std::nullopt;
3613}
3614
3615/// Return true if V1 == (binop V2, X), where X is known non-zero.
3616/// Only handle a small subset of binops where (binop V2, X) with non-zero X
3617/// implies V2 != V1.
3618static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2,
3619 const APInt &DemandedElts, unsigned Depth,
3620 const SimplifyQuery &Q) {
3621 const BinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
3622 if (!BO)
3623 return false;
3624 switch (BO->getOpcode()) {
3625 default:
3626 break;
3627 case Instruction::Or:
3628 if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3629 break;
3630 [[fallthrough]];
3631 case Instruction::Xor:
3632 case Instruction::Add:
3633 Value *Op = nullptr;
3634 if (V2 == BO->getOperand(0))
3635 Op = BO->getOperand(1);
3636 else if (V2 == BO->getOperand(1))
3637 Op = BO->getOperand(0);
3638 else
3639 return false;
3640 return isKnownNonZero(Op, DemandedElts, Q, Depth + 1);
3641 }
3642 return false;
3643}
3644
3645/// Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and
3646/// the multiplication is nuw or nsw.
3647static bool isNonEqualMul(const Value *V1, const Value *V2,
3648 const APInt &DemandedElts, unsigned Depth,
3649 const SimplifyQuery &Q) {
3650 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3651 const APInt *C;
3652 return match(OBO, m_Mul(m_Specific(V1), m_APInt(C))) &&
3653 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3654 !C->isZero() && !C->isOne() &&
3655 isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3656 }
3657 return false;
3658}
3659
3660/// Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and
3661/// the shift is nuw or nsw.
3662static bool isNonEqualShl(const Value *V1, const Value *V2,
3663 const APInt &DemandedElts, unsigned Depth,
3664 const SimplifyQuery &Q) {
3665 if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3666 const APInt *C;
3667 return match(OBO, m_Shl(m_Specific(V1), m_APInt(C))) &&
3668 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3669 !C->isZero() && isKnownNonZero(V1, DemandedElts, Q, Depth + 1);
3670 }
3671 return false;
3672}
3673
3674static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2,
3675 const APInt &DemandedElts, unsigned Depth,
3676 const SimplifyQuery &Q) {
3677 // Check two PHIs are in same block.
3678 if (PN1->getParent() != PN2->getParent())
3679 return false;
3680
3682 bool UsedFullRecursion = false;
3683 for (const BasicBlock *IncomBB : PN1->blocks()) {
3684 if (!VisitedBBs.insert(IncomBB).second)
3685 continue; // Don't reprocess blocks that we have dealt with already.
3686 const Value *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3687 const Value *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3688 const APInt *C1, *C2;
3689 if (match(IV1, m_APInt(C1)) && match(IV2, m_APInt(C2)) && *C1 != *C2)
3690 continue;
3691
3692 // Only one pair of phi operands is allowed for full recursion.
3693 if (UsedFullRecursion)
3694 return false;
3695
3697 RecQ.CxtI = IncomBB->getTerminator();
3698 if (!isKnownNonEqual(IV1, IV2, DemandedElts, Depth + 1, RecQ))
3699 return false;
3700 UsedFullRecursion = true;
3701 }
3702 return true;
3703}
3704
3705static bool isNonEqualSelect(const Value *V1, const Value *V2,
3706 const APInt &DemandedElts, unsigned Depth,
3707 const SimplifyQuery &Q) {
3708 const SelectInst *SI1 = dyn_cast<SelectInst>(V1);
3709 if (!SI1)
3710 return false;
3711
3712 if (const SelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3713 const Value *Cond1 = SI1->getCondition();
3714 const Value *Cond2 = SI2->getCondition();
3715 if (Cond1 == Cond2)
3716 return isKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3717 DemandedElts, Depth + 1, Q) &&
3718 isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3719 DemandedElts, Depth + 1, Q);
3720 }
3721 return isKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts, Depth + 1, Q) &&
3722 isKnownNonEqual(SI1->getFalseValue(), V2, DemandedElts, Depth + 1, Q);
3723}
3724
3725// Check to see if A is both a GEP and is the incoming value for a PHI in the
3726// loop, and B is either a ptr or another GEP. If the PHI has 2 incoming values,
3727// one of them being the recursive GEP A and the other a ptr at same base and at
3728// the same/higher offset than B we are only incrementing the pointer further in
3729// loop if offset of recursive GEP is greater than 0.
3731 const SimplifyQuery &Q) {
3732 if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3733 return false;
3734
3735 auto *GEPA = dyn_cast<GEPOperator>(A);
3736 if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3737 return false;
3738
3739 // Handle 2 incoming PHI values with one being a recursive GEP.
3740 auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3741 if (!PN || PN->getNumIncomingValues() != 2)
3742 return false;
3743
3744 // Search for the recursive GEP as an incoming operand, and record that as
3745 // Step.
3746 Value *Start = nullptr;
3747 Value *Step = const_cast<Value *>(A);
3748 if (PN->getIncomingValue(0) == Step)
3749 Start = PN->getIncomingValue(1);
3750 else if (PN->getIncomingValue(1) == Step)
3751 Start = PN->getIncomingValue(0);
3752 else
3753 return false;
3754
3755 // Other incoming node base should match the B base.
3756 // StartOffset >= OffsetB && StepOffset > 0?
3757 // StartOffset <= OffsetB && StepOffset < 0?
3758 // Is non-equal if above are true.
3759 // We use stripAndAccumulateInBoundsConstantOffsets to restrict the
3760 // optimisation to inbounds GEPs only.
3761 unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3762 APInt StartOffset(IndexWidth, 0);
3763 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3764 APInt StepOffset(IndexWidth, 0);
3765 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3766
3767 // Check if Base Pointer of Step matches the PHI.
3768 if (Step != PN)
3769 return false;
3770 APInt OffsetB(IndexWidth, 0);
3771 B = B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3772 return Start == B &&
3773 ((StartOffset.sge(OffsetB) && StepOffset.isStrictlyPositive()) ||
3774 (StartOffset.sle(OffsetB) && StepOffset.isNegative()));
3775}
3776
3777/// Return true if it is known that V1 != V2.
3778static bool isKnownNonEqual(const Value *V1, const Value *V2,
3779 const APInt &DemandedElts, unsigned Depth,
3780 const SimplifyQuery &Q) {
3781 if (V1 == V2)
3782 return false;
3783 if (V1->getType() != V2->getType())
3784 // We can't look through casts yet.
3785 return false;
3786
3788 return false;
3789
3790 // See if we can recurse through (exactly one of) our operands. This
3791 // requires our operation be 1-to-1 and map every input value to exactly
3792 // one output value. Such an operation is invertible.
3793 auto *O1 = dyn_cast<Operator>(V1);
3794 auto *O2 = dyn_cast<Operator>(V2);
3795 if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3796 if (auto Values = getInvertibleOperands(O1, O2))
3797 return isKnownNonEqual(Values->first, Values->second, DemandedElts,
3798 Depth + 1, Q);
3799
3800 if (const PHINode *PN1 = dyn_cast<PHINode>(V1)) {
3801 const PHINode *PN2 = cast<PHINode>(V2);
3802 // FIXME: This is missing a generalization to handle the case where one is
3803 // a PHI and another one isn't.
3804 if (isNonEqualPHIs(PN1, PN2, DemandedElts, Depth, Q))
3805 return true;
3806 };
3807 }
3808
3809 if (isModifyingBinopOfNonZero(V1, V2, DemandedElts, Depth, Q) ||
3810 isModifyingBinopOfNonZero(V2, V1, DemandedElts, Depth, Q))
3811 return true;
3812
3813 if (isNonEqualMul(V1, V2, DemandedElts, Depth, Q) ||
3814 isNonEqualMul(V2, V1, DemandedElts, Depth, Q))
3815 return true;
3816
3817 if (isNonEqualShl(V1, V2, DemandedElts, Depth, Q) ||
3818 isNonEqualShl(V2, V1, DemandedElts, Depth, Q))
3819 return true;
3820
3821 if (V1->getType()->isIntOrIntVectorTy()) {
3822 // Are any known bits in V1 contradictory to known bits in V2? If V1
3823 // has a known zero where V2 has a known one, they must not be equal.
3824 KnownBits Known1 = computeKnownBits(V1, DemandedElts, Depth, Q);
3825 if (!Known1.isUnknown()) {
3826 KnownBits Known2 = computeKnownBits(V2, DemandedElts, Depth, Q);
3827 if (Known1.Zero.intersects(Known2.One) ||
3828 Known2.Zero.intersects(Known1.One))
3829 return true;
3830 }
3831 }
3832
3833 if (isNonEqualSelect(V1, V2, DemandedElts, Depth, Q) ||
3834 isNonEqualSelect(V2, V1, DemandedElts, Depth, Q))
3835 return true;
3836
3837 if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3839 return true;
3840
3841 Value *A, *B;
3842 // PtrToInts are NonEqual if their Ptrs are NonEqual.
3843 // Check PtrToInt type matches the pointer size.
3844 if (match(V1, m_PtrToIntSameSize(Q.DL, m_Value(A))) &&
3846 return isKnownNonEqual(A, B, DemandedElts, Depth + 1, Q);
3847
3848 return false;
3849}
3850
3851/// For vector constants, loop over the elements and find the constant with the
3852/// minimum number of sign bits. Return 0 if the value is not a vector constant
3853/// or if any element was not analyzed; otherwise, return the count for the
3854/// element with the minimum number of sign bits.
3856 const APInt &DemandedElts,
3857 unsigned TyBits) {
3858 const auto *CV = dyn_cast<Constant>(V);
3859 if (!CV || !isa<FixedVectorType>(CV->getType()))
3860 return 0;
3861
3862 unsigned MinSignBits = TyBits;
3863 unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3864 for (unsigned i = 0; i != NumElts; ++i) {
3865 if (!DemandedElts[i])
3866 continue;
3867 // If we find a non-ConstantInt, bail out.
3868 auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3869 if (!Elt)
3870 return 0;
3871
3872 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3873 }
3874
3875 return MinSignBits;
3876}
3877
3878static unsigned ComputeNumSignBitsImpl(const Value *V,
3879 const APInt &DemandedElts,
3880 unsigned Depth, const SimplifyQuery &Q);
3881
3882static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts,
3883 unsigned Depth, const SimplifyQuery &Q) {
3884 unsigned Result = ComputeNumSignBitsImpl(V, DemandedElts, Depth, Q);
3885 assert(Result > 0 && "At least one sign bit needs to be present!");
3886 return Result;
3887}
3888
3889/// Return the number of times the sign bit of the register is replicated into
3890/// the other bits. We know that at least 1 bit is always equal to the sign bit
3891/// (itself), but other cases can give us information. For example, immediately
3892/// after an "ashr X, 2", we know that the top 3 bits are all equal to each
3893/// other, so we return 3. For vectors, return the number of sign bits for the
3894/// vector element with the minimum number of known sign bits of the demanded
3895/// elements in the vector specified by DemandedElts.
3896static unsigned ComputeNumSignBitsImpl(const Value *V,
3897 const APInt &DemandedElts,
3898 unsigned Depth, const SimplifyQuery &Q) {
3899 Type *Ty = V->getType();
3900#ifndef NDEBUG
3901 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
3902
3903 if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3904 assert(
3905 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3906 "DemandedElt width should equal the fixed vector number of elements");
3907 } else {
3908 assert(DemandedElts == APInt(1, 1) &&
3909 "DemandedElt width should be 1 for scalars");
3910 }
3911#endif
3912
3913 // We return the minimum number of sign bits that are guaranteed to be present
3914 // in V, so for undef we have to conservatively return 1. We don't have the
3915 // same behavior for poison though -- that's a FIXME today.
3916
3917 Type *ScalarTy = Ty->getScalarType();
3918 unsigned TyBits = ScalarTy->isPointerTy() ?
3919 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
3920 Q.DL.getTypeSizeInBits(ScalarTy);
3921
3922 unsigned Tmp, Tmp2;
3923 unsigned FirstAnswer = 1;
3924
3925 // Note that ConstantInt is handled by the general computeKnownBits case
3926 // below.
3927
3929 return 1;
3930
3931 if (auto *U = dyn_cast<Operator>(V)) {
3932 switch (Operator::getOpcode(V)) {
3933 default: break;
3934 case Instruction::SExt:
3935 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
3936 return ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q) +
3937 Tmp;
3938
3939 case Instruction::SDiv: {
3940 const APInt *Denominator;
3941 // sdiv X, C -> adds log(C) sign bits.
3942 if (match(U->getOperand(1), m_APInt(Denominator))) {
3943
3944 // Ignore non-positive denominator.
3945 if (!Denominator->isStrictlyPositive())
3946 break;
3947
3948 // Calculate the incoming numerator bits.
3949 unsigned NumBits =
3950 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
3951
3952 // Add floor(log(C)) bits to the numerator bits.
3953 return std::min(TyBits, NumBits + Denominator->logBase2());
3954 }
3955 break;
3956 }
3957
3958 case Instruction::SRem: {
3959 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
3960
3961 const APInt *Denominator;
3962 // srem X, C -> we know that the result is within [-C+1,C) when C is a
3963 // positive constant. This let us put a lower bound on the number of sign
3964 // bits.
3965 if (match(U->getOperand(1), m_APInt(Denominator))) {
3966
3967 // Ignore non-positive denominator.
3968 if (Denominator->isStrictlyPositive()) {
3969 // Calculate the leading sign bit constraints by examining the
3970 // denominator. Given that the denominator is positive, there are two
3971 // cases:
3972 //
3973 // 1. The numerator is positive. The result range is [0,C) and
3974 // [0,C) u< (1 << ceilLogBase2(C)).
3975 //
3976 // 2. The numerator is negative. Then the result range is (-C,0] and
3977 // integers in (-C,0] are either 0 or >u (-1 << ceilLogBase2(C)).
3978 //
3979 // Thus a lower bound on the number of sign bits is `TyBits -
3980 // ceilLogBase2(C)`.
3981
3982 unsigned ResBits = TyBits - Denominator->ceilLogBase2();
3983 Tmp = std::max(Tmp, ResBits);
3984 }
3985 }
3986 return Tmp;
3987 }
3988
3989 case Instruction::AShr: {
3990 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
3991 // ashr X, C -> adds C sign bits. Vectors too.
3992 const APInt *ShAmt;
3993 if (match(U->getOperand(1), m_APInt(ShAmt))) {
3994 if (ShAmt->uge(TyBits))
3995 break; // Bad shift.
3996 unsigned ShAmtLimited = ShAmt->getZExtValue();
3997 Tmp += ShAmtLimited;
3998 if (Tmp > TyBits) Tmp = TyBits;
3999 }
4000 return Tmp;
4001 }
4002 case Instruction::Shl: {
4003 const APInt *ShAmt;
4004 Value *X = nullptr;
4005 if (match(U->getOperand(1), m_APInt(ShAmt))) {
4006 // shl destroys sign bits.
4007 if (ShAmt->uge(TyBits))
4008 break; // Bad shift.
4009 // We can look through a zext (more or less treating it as a sext) if
4010 // all extended bits are shifted out.
4011 if (match(U->getOperand(0), m_ZExt(m_Value(X))) &&
4012 ShAmt->uge(TyBits - X->getType()->getScalarSizeInBits())) {
4013 Tmp = ComputeNumSignBits(X, DemandedElts, Depth + 1, Q);
4014 Tmp += TyBits - X->getType()->getScalarSizeInBits();
4015 } else
4016 Tmp =
4017 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4018 if (ShAmt->uge(Tmp))
4019 break; // Shifted all sign bits out.
4020 Tmp2 = ShAmt->getZExtValue();
4021 return Tmp - Tmp2;
4022 }
4023 break;
4024 }
4025 case Instruction::And:
4026 case Instruction::Or:
4027 case Instruction::Xor: // NOT is handled here.
4028 // Logical binary ops preserve the number of sign bits at the worst.
4029 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4030 if (Tmp != 1) {
4031 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4032 FirstAnswer = std::min(Tmp, Tmp2);
4033 // We computed what we know about the sign bits as our first
4034 // answer. Now proceed to the generic code that uses
4035 // computeKnownBits, and pick whichever answer is better.
4036 }
4037 break;
4038
4039 case Instruction::Select: {
4040 // If we have a clamp pattern, we know that the number of sign bits will
4041 // be the minimum of the clamp min/max range.
4042 const Value *X;
4043 const APInt *CLow, *CHigh;
4044 if (isSignedMinMaxClamp(U, X, CLow, CHigh))
4045 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4046
4047 Tmp = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4048 if (Tmp == 1)
4049 break;
4050 Tmp2 = ComputeNumSignBits(U->getOperand(2), DemandedElts, Depth + 1, Q);
4051 return std::min(Tmp, Tmp2);
4052 }
4053
4054 case Instruction::Add:
4055 // Add can have at most one carry bit. Thus we know that the output
4056 // is, at worst, one more bit than the inputs.
4057 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
4058 if (Tmp == 1) break;
4059
4060 // Special case decrementing a value (ADD X, -1):
4061 if (const auto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4062 if (CRHS->isAllOnesValue()) {
4063 KnownBits Known(TyBits);
4064 computeKnownBits(U->getOperand(0), DemandedElts, Known, Depth + 1, Q);
4065
4066 // If the input is known to be 0 or 1, the output is 0/-1, which is
4067 // all sign bits set.
4068 if ((Known.Zero | 1).isAllOnes())
4069 return TyBits;
4070
4071 // If we are subtracting one from a positive number, there is no carry
4072 // out of the result.
4073 if (Known.isNonNegative())
4074 return Tmp;
4075 }
4076
4077 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4078 if (Tmp2 == 1)
4079 break;
4080 return std::min(Tmp, Tmp2) - 1;
4081
4082 case Instruction::Sub:
4083 Tmp2 = ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4084 if (Tmp2 == 1)
4085 break;
4086
4087 // Handle NEG.
4088 if (const auto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4089 if (CLHS->isNullValue()) {
4090 KnownBits Known(TyBits);
4091 computeKnownBits(U->getOperand(1), DemandedElts, Known, Depth + 1, Q);
4092 // If the input is known to be 0 or 1, the output is 0/-1, which is
4093 // all sign bits set.
4094 if ((Known.Zero | 1).isAllOnes())
4095 return TyBits;
4096
4097 // If the input is known to be positive (the sign bit is known clear),
4098 // the output of the NEG has the same number of sign bits as the
4099 // input.
4100 if (Known.isNonNegative())
4101 return Tmp2;
4102
4103 // Otherwise, we treat this like a SUB.
4104 }
4105
4106 // Sub can have at most one carry bit. Thus we know that the output
4107 // is, at worst, one more bit than the inputs.
4108 Tmp = ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4109 if (Tmp == 1)
4110 break;
4111 return std::min(Tmp, Tmp2) - 1;
4112
4113 case Instruction::Mul: {
4114 // The output of the Mul can be at most twice the valid bits in the
4115 // inputs.
4116 unsigned SignBitsOp0 =
4117 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4118 if (SignBitsOp0 == 1)
4119 break;
4120 unsigned SignBitsOp1 =
4121 ComputeNumSignBits(U->getOperand(1), DemandedElts, Depth + 1, Q);
4122 if (SignBitsOp1 == 1)
4123 break;
4124 unsigned OutValidBits =
4125 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4126 return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4127 }
4128
4129 case Instruction::PHI: {
4130 const PHINode *PN = cast<PHINode>(U);
4131 unsigned NumIncomingValues = PN->getNumIncomingValues();
4132 // Don't analyze large in-degree PHIs.
4133 if (NumIncomingValues > 4) break;
4134 // Unreachable blocks may have zero-operand PHI nodes.
4135 if (NumIncomingValues == 0) break;
4136
4137 // Take the minimum of all incoming values. This can't infinitely loop
4138 // because of our depth threshold.
4140 Tmp = TyBits;
4141 for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4142 if (Tmp == 1) return Tmp;
4143 RecQ.CxtI = PN->getIncomingBlock(i)->getTerminator();
4144 Tmp = std::min(Tmp, ComputeNumSignBits(PN->getIncomingValue(i),
4145 DemandedElts, Depth + 1, RecQ));
4146 }
4147 return Tmp;
4148 }
4149
4150 case Instruction::Trunc: {
4151 // If the input contained enough sign bits that some remain after the
4152 // truncation, then we can make use of that. Otherwise we don't know
4153 // anything.
4154 Tmp = ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
4155 unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4156 if (Tmp > (OperandTyBits - TyBits))
4157 return Tmp - (OperandTyBits - TyBits);
4158
4159 return 1;
4160 }
4161
4162 case Instruction::ExtractElement:
4163 // Look through extract element. At the moment we keep this simple and
4164 // skip tracking the specific element. But at least we might find
4165 // information valid for all elements of the vector (for example if vector
4166 // is sign extended, shifted, etc).
4167 return ComputeNumSignBits(U->getOperand(0), Depth + 1, Q);
4168
4169 case Instruction::ShuffleVector: {
4170 // Collect the minimum number of sign bits that are shared by every vector
4171 // element referenced by the shuffle.
4172 auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4173 if (!Shuf) {
4174 // FIXME: Add support for shufflevector constant expressions.
4175 return 1;
4176 }
4177 APInt DemandedLHS, DemandedRHS;
4178 // For undef elements, we don't know anything about the common state of
4179 // the shuffle result.
4180 if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4181 return 1;
4182 Tmp = std::numeric_limits<unsigned>::max();
4183 if (!!DemandedLHS) {
4184 const Value *LHS = Shuf->getOperand(0);
4185 Tmp = ComputeNumSignBits(LHS, DemandedLHS, Depth + 1, Q);
4186 }
4187 // If we don't know anything, early out and try computeKnownBits
4188 // fall-back.
4189 if (Tmp == 1)
4190 break;
4191 if (!!DemandedRHS) {
4192 const Value *RHS = Shuf->getOperand(1);
4193 Tmp2 = ComputeNumSignBits(RHS, DemandedRHS, Depth + 1, Q);
4194 Tmp = std::min(Tmp, Tmp2);
4195 }
4196 // If we don't know anything, early out and try computeKnownBits
4197 // fall-back.
4198 if (Tmp == 1)
4199 break;
4200 assert(Tmp <= TyBits && "Failed to determine minimum sign bits");
4201 return Tmp;
4202 }
4203 case Instruction::Call: {
4204 if (const auto *II = dyn_cast<IntrinsicInst>(U)) {
4205 switch (II->getIntrinsicID()) {
4206 default:
4207 break;
4208 case Intrinsic::abs:
4209 Tmp =
4210 ComputeNumSignBits(U->getOperand(0), DemandedElts, Depth + 1, Q);
4211 if (Tmp == 1)
4212 break;
4213
4214 // Absolute value reduces number of sign bits by at most 1.
4215 return Tmp - 1;
4216 case Intrinsic::smin:
4217 case Intrinsic::smax: {
4218 const APInt *CLow, *CHigh;
4219 if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4220 return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4221 }
4222 }
4223 }
4224 }
4225 }
4226 }
4227
4228 // Finally, if we can prove that the top bits of the result are 0's or 1's,
4229 // use this information.
4230
4231 // If we can examine all elements of a vector constant successfully, we're
4232 // done (we can't do any better than that). If not, keep trying.
4233 if (unsigned VecSignBits =
4234 computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4235 return VecSignBits;
4236
4237 KnownBits Known(TyBits);
4238 computeKnownBits(V, DemandedElts, Known, Depth, Q);
4239
4240 // If we know that the sign bit is either zero or one, determine the number of
4241 // identical bits in the top of the input value.
4242 return std::max(FirstAnswer, Known.countMinSignBits());
4243}
4244
4246 const TargetLibraryInfo *TLI) {
4247 const Function *F = CB.getCalledFunction();
4248 if (!F)
4250
4251 if (F->isIntrinsic())
4252 return F->getIntrinsicID();
4253
4254 // We are going to infer semantics of a library function based on mapping it
4255 // to an LLVM intrinsic. Check that the library function is available from
4256 // this callbase and in this environment.
4257 LibFunc Func;
4258 if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4259 !CB.onlyReadsMemory())
4261
4262 switch (Func) {
4263 default:
4264 break;
4265 case LibFunc_sin:
4266 case LibFunc_sinf:
4267 case LibFunc_sinl:
4268 return Intrinsic::sin;
4269 case LibFunc_cos:
4270 case LibFunc_cosf:
4271 case LibFunc_cosl:
4272 return Intrinsic::cos;
4273 case LibFunc_tan:
4274 case LibFunc_tanf:
4275 case LibFunc_tanl:
4276 return Intrinsic::tan;
4277 case LibFunc_asin:
4278 case LibFunc_asinf:
4279 case LibFunc_asinl:
4280 return Intrinsic::asin;
4281 case LibFunc_acos:
4282 case LibFunc_acosf:
4283 case LibFunc_acosl:
4284 return Intrinsic::acos;
4285 case LibFunc_atan:
4286 case LibFunc_atanf:
4287 case LibFunc_atanl:
4288 return Intrinsic::atan;
4289 case LibFunc_atan2:
4290 case LibFunc_atan2f:
4291 case LibFunc_atan2l:
4292 return Intrinsic::atan2;
4293 case LibFunc_sinh:
4294 case LibFunc_sinhf:
4295 case LibFunc_sinhl:
4296 return Intrinsic::sinh;
4297 case LibFunc_cosh:
4298 case LibFunc_coshf:
4299 case LibFunc_coshl:
4300 return Intrinsic::cosh;
4301 case LibFunc_tanh:
4302 case LibFunc_tanhf:
4303 case LibFunc_tanhl:
4304 return Intrinsic::tanh;
4305 case LibFunc_exp:
4306 case LibFunc_expf:
4307 case LibFunc_expl:
4308 return Intrinsic::exp;
4309 case LibFunc_exp2:
4310 case LibFunc_exp2f:
4311 case LibFunc_exp2l:
4312 return Intrinsic::exp2;
4313 case LibFunc_exp10:
4314 case LibFunc_exp10f:
4315 case LibFunc_exp10l:
4316 return Intrinsic::exp10;
4317 case LibFunc_log:
4318 case LibFunc_logf:
4319 case LibFunc_logl:
4320 return Intrinsic::log;
4321 case LibFunc_log10:
4322 case LibFunc_log10f:
4323 case LibFunc_log10l:
4324 return Intrinsic::log10;
4325 case LibFunc_log2:
4326 case LibFunc_log2f:
4327 case LibFunc_log2l:
4328 return Intrinsic::log2;
4329 case LibFunc_fabs:
4330 case LibFunc_fabsf:
4331 case LibFunc_fabsl:
4332 return Intrinsic::fabs;
4333 case LibFunc_fmin:
4334 case LibFunc_fminf:
4335 case LibFunc_fminl:
4336 return Intrinsic::minnum;
4337 case LibFunc_fmax:
4338 case LibFunc_fmaxf:
4339 case LibFunc_fmaxl:
4340 return Intrinsic::maxnum;
4341 case LibFunc_copysign:
4342 case LibFunc_copysignf:
4343 case LibFunc_copysignl:
4344 return Intrinsic::copysign;
4345 case LibFunc_floor:
4346 case LibFunc_floorf:
4347 case LibFunc_floorl:
4348 return Intrinsic::floor;
4349 case LibFunc_ceil:
4350 case LibFunc_ceilf:
4351 case LibFunc_ceill:
4352 return Intrinsic::ceil;
4353 case LibFunc_trunc:
4354 case LibFunc_truncf:
4355 case LibFunc_truncl:
4356 return Intrinsic::trunc;
4357 case LibFunc_rint:
4358 case LibFunc_rintf:
4359 case LibFunc_rintl:
4360 return Intrinsic::rint;
4361 case LibFunc_nearbyint:
4362 case LibFunc_nearbyintf:
4363 case LibFunc_nearbyintl:
4364 return Intrinsic::nearbyint;
4365 case LibFunc_round:
4366 case LibFunc_roundf:
4367 case LibFunc_roundl:
4368 return Intrinsic::round;
4369 case LibFunc_roundeven:
4370 case LibFunc_roundevenf:
4371 case LibFunc_roundevenl:
4372 return Intrinsic::roundeven;
4373 case LibFunc_pow:
4374 case LibFunc_powf:
4375 case LibFunc_powl:
4376 return Intrinsic::pow;
4377 case LibFunc_sqrt:
4378 case LibFunc_sqrtf:
4379 case LibFunc_sqrtl:
4380 return Intrinsic::sqrt;
4381 }
4382
4384}
4385
4386/// Return true if it's possible to assume IEEE treatment of input denormals in
4387/// \p F for \p Val.
4388static bool inputDenormalIsIEEE(const Function &F, const Type *Ty) {
4389 Ty = Ty->getScalarType();
4390 return F.getDenormalMode(Ty->getFltSemantics()).Input == DenormalMode::IEEE;
4391}
4392
4393static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4394 Ty = Ty->getScalarType();
4395 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4396 return Mode.Input == DenormalMode::IEEE ||
4397 Mode.Input == DenormalMode::PositiveZero;
4398}
4399
4400static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty) {
4401 Ty = Ty->getScalarType();
4402 DenormalMode Mode = F.getDenormalMode(Ty->getFltSemantics());
4403 return Mode.Output == DenormalMode::IEEE ||
4404 Mode.Output == DenormalMode::PositiveZero;
4405}
4406
4408 return isKnownNeverZero() &&
4410}
4411
4413 Type *Ty) const {
4414 return isKnownNeverNegZero() &&
4416}
4417
4419 Type *Ty) const {
4420 if (!isKnownNeverPosZero())
4421 return false;
4422
4423 // If we know there are no denormals, nothing can be flushed to zero.
4425 return true;
4426
4427 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4428 switch (Mode.Input) {
4429 case DenormalMode::IEEE:
4430 return true;
4432 // Negative subnormal won't flush to +0
4433 return isKnownNeverPosSubnormal();
4435 default:
4436 // Both positive and negative subnormal could flush to +0
4437 return false;
4438 }
4439
4440 llvm_unreachable("covered switch over denormal mode");
4441}
4442
4444 Type *Ty) {
4445 KnownFPClasses = Src.KnownFPClasses;
4446 // If we aren't assuming the source can't be a zero, we don't have to check if
4447 // a denormal input could be flushed.
4448 if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero())
4449 return;
4450
4451 // If we know the input can't be a denormal, it can't be flushed to 0.
4452 if (Src.isKnownNeverSubnormal())
4453 return;
4454
4455 DenormalMode Mode = F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4456
4457 if (!Src.isKnownNeverPosSubnormal() && Mode != DenormalMode::getIEEE())
4459
4460 if (!Src.isKnownNeverNegSubnormal() && Mode != DenormalMode::getIEEE()) {
4461 if (Mode != DenormalMode::getPositiveZero())
4463
4464 if (Mode.Input == DenormalMode::PositiveZero ||
4465 Mode.Output == DenormalMode::PositiveZero ||
4466 Mode.Input == DenormalMode::Dynamic ||
4467 Mode.Output == DenormalMode::Dynamic)
4469 }
4470}
4471
4473 const Function &F, Type *Ty) {
4474 propagateDenormal(Src, F, Ty);
4475 propagateNaN(Src, /*PreserveSign=*/true);
4476}
4477
4478/// Given an exploded icmp instruction, return true if the comparison only
4479/// checks the sign bit. If it only checks the sign bit, set TrueIfSigned if
4480/// the result of the comparison is true when the input value is signed.
4482 bool &TrueIfSigned) {
4483 switch (Pred) {
4484 case ICmpInst::ICMP_SLT: // True if LHS s< 0
4485 TrueIfSigned = true;
4486 return RHS.isZero();
4487 case ICmpInst::ICMP_SLE: // True if LHS s<= -1
4488 TrueIfSigned = true;
4489 return RHS.isAllOnes();
4490 case ICmpInst::ICMP_SGT: // True if LHS s> -1
4491 TrueIfSigned = false;
4492 return RHS.isAllOnes();
4493 case ICmpInst::ICMP_SGE: // True if LHS s>= 0
4494 TrueIfSigned = false;
4495 return RHS.isZero();
4496 case ICmpInst::ICMP_UGT:
4497 // True if LHS u> RHS and RHS == sign-bit-mask - 1
4498 TrueIfSigned = true;
4499 return RHS.isMaxSignedValue();
4500 case ICmpInst::ICMP_UGE:
4501 // True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4502 TrueIfSigned = true;
4503 return RHS.isMinSignedValue();
4504 case ICmpInst::ICMP_ULT:
4505 // True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4506 TrueIfSigned = false;
4507 return RHS.isMinSignedValue();
4508 case ICmpInst::ICMP_ULE:
4509 // True if LHS u<= RHS and RHS == sign-bit-mask - 1
4510 TrueIfSigned = false;
4511 return RHS.isMaxSignedValue();
4512 default:
4513 return false;
4514 }
4515}
4516
4517/// Returns a pair of values, which if passed to llvm.is.fpclass, returns the
4518/// same result as an fcmp with the given operands.
4519std::pair<Value *, FPClassTest> llvm::fcmpToClassTest(FCmpInst::Predicate Pred,
4520 const Function &F,
4521 Value *LHS, Value *RHS,
4522 bool LookThroughSrc) {
4523 const APFloat *ConstRHS;
4524 if (!match(RHS, m_APFloatAllowPoison(ConstRHS)))
4525 return {nullptr, fcAllFlags};
4526
4527 return fcmpToClassTest(Pred, F, LHS, ConstRHS, LookThroughSrc);
4528}
4529
4530std::pair<Value *, FPClassTest>
4532 const APFloat *ConstRHS, bool LookThroughSrc) {
4533
4534 auto [Src, ClassIfTrue, ClassIfFalse] =
4535 fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4536 if (Src && ClassIfTrue == ~ClassIfFalse)
4537 return {Src, ClassIfTrue};
4538 return {nullptr, fcAllFlags};
4539}
4540
4541/// Return the return value for fcmpImpliesClass for a compare that produces an
4542/// exact class test.
4543static std::tuple<Value *, FPClassTest, FPClassTest> exactClass(Value *V,
4544 FPClassTest M) {
4545 return {V, M, ~M};
4546}
4547
4548std::tuple<Value *, FPClassTest, FPClassTest>
4550 FPClassTest RHSClass, bool LookThroughSrc) {
4551 assert(RHSClass != fcNone);
4552 Value *Src = LHS;
4553
4554 if (Pred == FCmpInst::FCMP_TRUE)
4555 return exactClass(Src, fcAllFlags);
4556
4557 if (Pred == FCmpInst::FCMP_FALSE)
4558 return exactClass(Src, fcNone);
4559
4560 const FPClassTest OrigClass = RHSClass;
4561
4562 const bool IsNegativeRHS = (RHSClass & fcNegative) == RHSClass;
4563 const bool IsPositiveRHS = (RHSClass & fcPositive) == RHSClass;
4564 const bool IsNaN = (RHSClass & ~fcNan) == fcNone;
4565
4566 if (IsNaN) {
4567 // fcmp o__ x, nan -> false
4568 // fcmp u__ x, nan -> true
4569 return exactClass(Src, CmpInst::isOrdered(Pred) ? fcNone : fcAllFlags);
4570 }
4571
4572 // fcmp ord x, zero|normal|subnormal|inf -> ~fcNan
4573 if (Pred == FCmpInst::FCMP_ORD)
4574 return exactClass(Src, ~fcNan);
4575
4576 // fcmp uno x, zero|normal|subnormal|inf -> fcNan
4577 if (Pred == FCmpInst::FCMP_UNO)
4578 return exactClass(Src, fcNan);
4579
4580 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4581 if (IsFabs)
4582 RHSClass = llvm::inverse_fabs(RHSClass);
4583
4584 const bool IsZero = (OrigClass & fcZero) == OrigClass;
4585 if (IsZero) {
4586 assert(Pred != FCmpInst::FCMP_ORD && Pred != FCmpInst::FCMP_UNO);
4587 // Compares with fcNone are only exactly equal to fcZero if input denormals
4588 // are not flushed.
4589 // TODO: Handle DAZ by expanding masks to cover subnormal cases.
4590 if (!inputDenormalIsIEEE(F, LHS->getType()))
4591 return {nullptr, fcAllFlags, fcAllFlags};
4592
4593 switch (Pred) {
4594 case FCmpInst::FCMP_OEQ: // Match x == 0.0
4595 return exactClass(Src, fcZero);
4596 case FCmpInst::FCMP_UEQ: // Match isnan(x) || (x == 0.0)
4597 return exactClass(Src, fcZero | fcNan);
4598 case FCmpInst::FCMP_UNE: // Match (x != 0.0)
4599 return exactClass(Src, ~fcZero);
4600 case FCmpInst::FCMP_ONE: // Match !isnan(x) && x != 0.0
4601 return exactClass(Src, ~fcNan & ~fcZero);
4602 case FCmpInst::FCMP_ORD:
4603 // Canonical form of ord/uno is with a zero. We could also handle
4604 // non-canonical other non-NaN constants or LHS == RHS.
4605 return exactClass(Src, ~fcNan);
4606 case FCmpInst::FCMP_UNO:
4607 return exactClass(Src, fcNan);
4608 case FCmpInst::FCMP_OGT: // x > 0
4610 case FCmpInst::FCMP_UGT: // isnan(x) || x > 0
4612 case FCmpInst::FCMP_OGE: // x >= 0
4613 return exactClass(Src, fcPositive | fcNegZero);
4614 case FCmpInst::FCMP_UGE: // isnan(x) || x >= 0
4615 return exactClass(Src, fcPositive | fcNegZero | fcNan);
4616 case FCmpInst::FCMP_OLT: // x < 0
4618 case FCmpInst::FCMP_ULT: // isnan(x) || x < 0
4620 case FCmpInst::FCMP_OLE: // x <= 0
4621 return exactClass(Src, fcNegative | fcPosZero);
4622 case FCmpInst::FCMP_ULE: // isnan(x) || x <= 0
4623 return exactClass(Src, fcNegative | fcPosZero | fcNan);
4624 default:
4625 llvm_unreachable("all compare types are handled");
4626 }
4627
4628 return {nullptr, fcAllFlags, fcAllFlags};
4629 }
4630
4631 const bool IsDenormalRHS = (OrigClass & fcSubnormal) == OrigClass;
4632
4633 const bool IsInf = (OrigClass & fcInf) == OrigClass;
4634 if (IsInf) {
4635 FPClassTest Mask = fcAllFlags;
4636
4637 switch (Pred) {
4638 case FCmpInst::FCMP_OEQ:
4639 case FCmpInst::FCMP_UNE: {
4640 // Match __builtin_isinf patterns
4641 //
4642 // fcmp oeq x, +inf -> is_fpclass x, fcPosInf
4643 // fcmp oeq fabs(x), +inf -> is_fpclass x, fcInf
4644 // fcmp oeq x, -inf -> is_fpclass x, fcNegInf
4645 // fcmp oeq fabs(x), -inf -> is_fpclass x, 0 -> false
4646 //
4647 // fcmp une x, +inf -> is_fpclass x, ~fcPosInf
4648 // fcmp une fabs(x), +inf -> is_fpclass x, ~fcInf
4649 // fcmp une x, -inf -> is_fpclass x, ~fcNegInf
4650 // fcmp une fabs(x), -inf -> is_fpclass x, fcAllFlags -> true
4651 if (IsNegativeRHS) {
4652 Mask = fcNegInf;
4653 if (IsFabs)
4654 Mask = fcNone;
4655 } else {
4656 Mask = fcPosInf;
4657 if (IsFabs)
4658 Mask |= fcNegInf;
4659 }
4660 break;
4661 }
4662 case FCmpInst::FCMP_ONE:
4663 case FCmpInst::FCMP_UEQ: {
4664 // Match __builtin_isinf patterns
4665 // fcmp one x, -inf -> is_fpclass x, fcNegInf
4666 // fcmp one fabs(x), -inf -> is_fpclass x, ~fcNegInf & ~fcNan
4667 // fcmp one x, +inf -> is_fpclass x, ~fcNegInf & ~fcNan
4668 // fcmp one fabs(x), +inf -> is_fpclass x, ~fcInf & fcNan
4669 //
4670 // fcmp ueq x, +inf -> is_fpclass x, fcPosInf|fcNan
4671 // fcmp ueq (fabs x), +inf -> is_fpclass x, fcInf|fcNan
4672 // fcmp ueq x, -inf -> is_fpclass x, fcNegInf|fcNan
4673 // fcmp ueq fabs(x), -inf -> is_fpclass x, fcNan
4674 if (IsNegativeRHS) {
4675 Mask = ~fcNegInf & ~fcNan;
4676 if (IsFabs)
4677 Mask = ~fcNan;
4678 } else {
4679 Mask = ~fcPosInf & ~fcNan;
4680 if (IsFabs)
4681 Mask &= ~fcNegInf;
4682 }
4683
4684 break;
4685 }
4686 case FCmpInst::FCMP_OLT:
4687 case FCmpInst::FCMP_UGE: {
4688 if (IsNegativeRHS) {
4689 // No value is ordered and less than negative infinity.
4690 // All values are unordered with or at least negative infinity.
4691 // fcmp olt x, -inf -> false
4692 // fcmp uge x, -inf -> true
4693 Mask = fcNone;
4694 break;
4695 }
4696
4697 // fcmp olt fabs(x), +inf -> fcFinite
4698 // fcmp uge fabs(x), +inf -> ~fcFinite
4699 // fcmp olt x, +inf -> fcFinite|fcNegInf
4700 // fcmp uge x, +inf -> ~(fcFinite|fcNegInf)
4701 Mask = fcFinite;
4702 if (!IsFabs)
4703 Mask |= fcNegInf;
4704 break;
4705 }
4706 case FCmpInst::FCMP_OGE:
4707 case FCmpInst::FCMP_ULT: {
4708 if (IsNegativeRHS) {
4709 // fcmp oge x, -inf -> ~fcNan
4710 // fcmp oge fabs(x), -inf -> ~fcNan
4711 // fcmp ult x, -inf -> fcNan
4712 // fcmp ult fabs(x), -inf -> fcNan
4713 Mask = ~fcNan;
4714 break;
4715 }
4716
4717 // fcmp oge fabs(x), +inf -> fcInf
4718 // fcmp oge x, +inf -> fcPosInf
4719 // fcmp ult fabs(x), +inf -> ~fcInf
4720 // fcmp ult x, +inf -> ~fcPosInf
4721 Mask = fcPosInf;
4722 if (IsFabs)
4723 Mask |= fcNegInf;
4724 break;
4725 }
4726 case FCmpInst::FCMP_OGT:
4727 case FCmpInst::FCMP_ULE: {
4728 if (IsNegativeRHS) {
4729 // fcmp ogt x, -inf -> fcmp one x, -inf
4730 // fcmp ogt fabs(x), -inf -> fcmp ord x, x
4731 // fcmp ule x, -inf -> fcmp ueq x, -inf
4732 // fcmp ule fabs(x), -inf -> fcmp uno x, x
4733 Mask = IsFabs ? ~fcNan : ~(fcNegInf | fcNan);
4734 break;
4735 }
4736
4737 // No value is ordered and greater than infinity.
4738 Mask = fcNone;
4739 break;
4740 }
4741 case FCmpInst::FCMP_OLE:
4742 case FCmpInst::FCMP_UGT: {
4743 if (IsNegativeRHS) {
4744 Mask = IsFabs ? fcNone : fcNegInf;
4745 break;
4746 }
4747
4748 // fcmp ole x, +inf -> fcmp ord x, x
4749 // fcmp ole fabs(x), +inf -> fcmp ord x, x
4750 // fcmp ole x, -inf -> fcmp oeq x, -inf
4751 // fcmp ole fabs(x), -inf -> false
4752 Mask = ~fcNan;
4753 break;
4754 }
4755 default:
4756 llvm_unreachable("all compare types are handled");
4757 }
4758
4759 // Invert the comparison for the unordered cases.
4760 if (FCmpInst::isUnordered(Pred))
4761 Mask = ~Mask;
4762
4763 return exactClass(Src, Mask);
4764 }
4765
4766 if (Pred == FCmpInst::FCMP_OEQ)
4767 return {Src, RHSClass, fcAllFlags};
4768
4769 if (Pred == FCmpInst::FCMP_UEQ) {
4770 FPClassTest Class = RHSClass | fcNan;
4771 return {Src, Class, ~fcNan};
4772 }
4773
4774 if (Pred == FCmpInst::FCMP_ONE)
4775 return {Src, ~fcNan, RHSClass | fcNan};
4776
4777 if (Pred == FCmpInst::FCMP_UNE)
4778 return {Src, fcAllFlags, RHSClass};
4779
4780 assert((RHSClass == fcNone || RHSClass == fcPosNormal ||
4781 RHSClass == fcNegNormal || RHSClass == fcNormal ||
4782 RHSClass == fcPosSubnormal || RHSClass == fcNegSubnormal ||
4783 RHSClass == fcSubnormal) &&
4784 "should have been recognized as an exact class test");
4785
4786 if (IsNegativeRHS) {
4787 // TODO: Handle fneg(fabs)
4788 if (IsFabs) {
4789 // fabs(x) o> -k -> fcmp ord x, x
4790 // fabs(x) u> -k -> true
4791 // fabs(x) o< -k -> false
4792 // fabs(x) u< -k -> fcmp uno x, x
4793 switch (Pred) {
4794 case FCmpInst::FCMP_OGT:
4795 case FCmpInst::FCMP_OGE:
4796 return {Src, ~fcNan, fcNan};
4797 case FCmpInst::FCMP_UGT:
4798 case FCmpInst::FCMP_UGE:
4799 return {Src, fcAllFlags, fcNone};
4800 case FCmpInst::FCMP_OLT:
4801 case FCmpInst::FCMP_OLE:
4802 return {Src, fcNone, fcAllFlags};
4803 case FCmpInst::FCMP_ULT:
4804 case FCmpInst::FCMP_ULE:
4805 return {Src, fcNan, ~fcNan};
4806 default:
4807 break;
4808 }
4809
4810 return {nullptr, fcAllFlags, fcAllFlags};
4811 }
4812
4813 FPClassTest ClassesLE = fcNegInf | fcNegNormal;
4815
4816 if (IsDenormalRHS)
4817 ClassesLE |= fcNegSubnormal;
4818 else
4819 ClassesGE |= fcNegNormal;
4820
4821 switch (Pred) {
4822 case FCmpInst::FCMP_OGT:
4823 case FCmpInst::FCMP_OGE:
4824 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4825 case FCmpInst::FCMP_UGT:
4826 case FCmpInst::FCMP_UGE:
4827 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4828 case FCmpInst::FCMP_OLT:
4829 case FCmpInst::FCMP_OLE:
4830 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4831 case FCmpInst::FCMP_ULT:
4832 case FCmpInst::FCMP_ULE:
4833 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4834 default:
4835 break;
4836 }
4837 } else if (IsPositiveRHS) {
4838 FPClassTest ClassesGE = fcPosNormal | fcPosInf;
4840 if (IsDenormalRHS)
4841 ClassesGE |= fcPosSubnormal;
4842 else
4843 ClassesLE |= fcPosNormal;
4844
4845 if (IsFabs) {
4846 ClassesGE = llvm::inverse_fabs(ClassesGE);
4847 ClassesLE = llvm::inverse_fabs(ClassesLE);
4848 }
4849
4850 switch (Pred) {
4851 case FCmpInst::FCMP_OGT:
4852 case FCmpInst::FCMP_OGE:
4853 return {Src, ClassesGE, ~ClassesGE | RHSClass};
4854 case FCmpInst::FCMP_UGT:
4855 case FCmpInst::FCMP_UGE:
4856 return {Src, ClassesGE | fcNan, ~(ClassesGE | fcNan) | RHSClass};
4857 case FCmpInst::FCMP_OLT:
4858 case FCmpInst::FCMP_OLE:
4859 return {Src, ClassesLE, ~ClassesLE | RHSClass};
4860 case FCmpInst::FCMP_ULT:
4861 case FCmpInst::FCMP_ULE:
4862 return {Src, ClassesLE | fcNan, ~(ClassesLE | fcNan) | RHSClass};
4863 default:
4864 break;
4865 }
4866 }
4867
4868 return {nullptr, fcAllFlags, fcAllFlags};
4869}
4870
4871std::tuple<Value *, FPClassTest, FPClassTest>
4873 const APFloat &ConstRHS, bool LookThroughSrc) {
4874 // We can refine checks against smallest normal / largest denormal to an
4875 // exact class test.
4876 if (!ConstRHS.isNegative() && ConstRHS.isSmallestNormalized()) {
4877 Value *Src = LHS;
4878 const bool IsFabs = LookThroughSrc && match(LHS, m_FAbs(m_Value(Src)));
4879
4880 FPClassTest Mask;
4881 // Match pattern that's used in __builtin_isnormal.
4882 switch (Pred) {
4883 case FCmpInst::FCMP_OLT:
4884 case FCmpInst::FCMP_UGE: {
4885 // fcmp olt x, smallest_normal -> fcNegInf|fcNegNormal|fcSubnormal|fcZero
4886 // fcmp olt fabs(x), smallest_normal -> fcSubnormal|fcZero
4887 // fcmp uge x, smallest_normal -> fcNan|fcPosNormal|fcPosInf
4888 // fcmp uge fabs(x), smallest_normal -> ~(fcSubnormal|fcZero)
4889 Mask = fcZero | fcSubnormal;
4890 if (!IsFabs)
4891 Mask |= fcNegNormal | fcNegInf;
4892
4893 break;
4894 }
4895 case FCmpInst::FCMP_OGE:
4896 case FCmpInst::FCMP_ULT: {
4897 // fcmp oge x, smallest_normal -> fcPosNormal | fcPosInf
4898 // fcmp oge fabs(x), smallest_normal -> fcInf | fcNormal
4899 // fcmp ult x, smallest_normal -> ~(fcPosNormal | fcPosInf)
4900 // fcmp ult fabs(x), smallest_normal -> ~(fcInf | fcNormal)
4901 Mask = fcPosInf | fcPosNormal;
4902 if (IsFabs)
4903 Mask |= fcNegInf | fcNegNormal;
4904 break;
4905 }
4906 default:
4907 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(),
4908 LookThroughSrc);
4909 }
4910
4911 // Invert the comparison for the unordered cases.
4912 if (FCmpInst::isUnordered(Pred))
4913 Mask = ~Mask;
4914
4915 return exactClass(Src, Mask);
4916 }
4917
4918 return fcmpImpliesClass(Pred, F, LHS, ConstRHS.classify(), LookThroughSrc);
4919}
4920
4921std::tuple<Value *, FPClassTest, FPClassTest>
4923 Value *RHS, bool LookThroughSrc) {
4924 const APFloat *ConstRHS;
4925 if (!match(RHS, m_APFloatAllowPoison(ConstRHS)))
4926 return {nullptr, fcAllFlags, fcAllFlags};
4927
4928 // TODO: Just call computeKnownFPClass for RHS to handle non-constants.
4929 return fcmpImpliesClass(Pred, F, LHS, *ConstRHS, LookThroughSrc);
4930}
4931
4933 unsigned Depth, bool CondIsTrue,
4934 const Instruction *CxtI,
4935 KnownFPClass &KnownFromContext) {
4936 Value *A, *B;
4938 (CondIsTrue ? match(Cond, m_LogicalAnd(m_Value(A), m_Value(B)))
4939 : match(Cond, m_LogicalOr(m_Value(A), m_Value(B))))) {
4940 computeKnownFPClassFromCond(V, A, Depth + 1, CondIsTrue, CxtI,
4941 KnownFromContext);
4942 computeKnownFPClassFromCond(V, B, Depth + 1, CondIsTrue, CxtI,
4943 KnownFromContext);
4944 return;
4945 }
4946 CmpPredicate Pred;
4947 Value *LHS;
4948 uint64_t ClassVal = 0;
4949 const APFloat *CRHS;
4950 const APInt *RHS;
4951 if (match(Cond, m_FCmp(Pred, m_Value(LHS), m_APFloat(CRHS)))) {
4952 auto [CmpVal, MaskIfTrue, MaskIfFalse] = fcmpImpliesClass(
4953 Pred, *CxtI->getParent()->getParent(), LHS, *CRHS, LHS != V);
4954 if (CmpVal == V)
4955 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4956 } else if (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(
4957 m_Specific(V), m_ConstantInt(ClassVal)))) {
4958 FPClassTest Mask = static_cast<FPClassTest>(ClassVal);
4959 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4960 } else if (match(Cond, m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(V)),
4961 m_APInt(RHS)))) {
4962 bool TrueIfSigned;
4963 if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4964 return;
4965 if (TrueIfSigned == CondIsTrue)
4966 KnownFromContext.signBitMustBeOne();
4967 else
4968 KnownFromContext.signBitMustBeZero();
4969 }
4970}
4971
4973 const SimplifyQuery &Q) {
4974 KnownFPClass KnownFromContext;
4975
4976 if (!Q.CxtI)
4977 return KnownFromContext;
4978
4979 if (Q.DC && Q.DT) {
4980 // Handle dominating conditions.
4981 for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4982 Value *Cond = BI->getCondition();
4983
4984 BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4985 if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4986 computeKnownFPClassFromCond(V, Cond, /*Depth=*/0, /*CondIsTrue=*/true,
4987 Q.CxtI, KnownFromContext);
4988
4989 BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4990 if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4991 computeKnownFPClassFromCond(V, Cond, /*Depth=*/0, /*CondIsTrue=*/false,
4992 Q.CxtI, KnownFromContext);
4993 }
4994 }
4995
4996 if (!Q.AC)
4997 return KnownFromContext;
4998
4999 // Try to restrict the floating-point classes based on information from
5000 // assumptions.
5001 for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
5002 if (!AssumeVH)
5003 continue;
5004 CallInst *I = cast<CallInst>(AssumeVH);
5005
5006 assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
5007 "Got assumption for the wrong function!");
5008 assert(I->getIntrinsicID() == Intrinsic::assume &&
5009 "must be an assume intrinsic");
5010
5011 if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
5012 continue;
5013
5014 computeKnownFPClassFromCond(V, I->getArgOperand(0), /*Depth=*/0,
5015 /*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
5016 }
5017
5018 return KnownFromContext;
5019}
5020
5021void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5022 FPClassTest InterestedClasses, KnownFPClass &Known,
5023 unsigned Depth, const SimplifyQuery &Q);
5024
5025static void computeKnownFPClass(const Value *V, KnownFPClass &Known,
5026 FPClassTest InterestedClasses, unsigned Depth,
5027 const SimplifyQuery &Q) {
5028 auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5029 APInt DemandedElts =
5030 FVTy ? APInt::getAllOnes(FVTy->getNumElements()) : APInt(1, 1);
5031 computeKnownFPClass(V, DemandedElts, InterestedClasses, Known, Depth, Q);
5032}
5033
5035 const APInt &DemandedElts,
5036 FPClassTest InterestedClasses,
5037 KnownFPClass &Known, unsigned Depth,
5038 const SimplifyQuery &Q) {
5039 if ((InterestedClasses &
5041 return;
5042
5043 KnownFPClass KnownSrc;
5044 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5045 KnownSrc, Depth + 1, Q);
5046
5047 // Sign should be preserved
5048 // TODO: Handle cannot be ordered greater than zero
5049 if (KnownSrc.cannotBeOrderedLessThanZero())
5051
5052 Known.propagateNaN(KnownSrc, true);
5053
5054 // Infinity needs a range check.
5055}
5056
5057void computeKnownFPClass(const Value *V, const APInt &DemandedElts,
5058 FPClassTest InterestedClasses, KnownFPClass &Known,
5059 unsigned Depth, const SimplifyQuery &Q) {
5060 assert(Known.isUnknown() && "should not be called with known information");
5061
5062 if (!DemandedElts) {
5063 // No demanded elts, better to assume we don't know anything.
5064 Known.resetAll();
5065 return;
5066 }
5067
5068 assert(Depth <= MaxAnalysisRecursionDepth && "Limit Search Depth");
5069
5070 if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5071 Known.KnownFPClasses = CFP->getValueAPF().classify();
5072 Known.SignBit = CFP->isNegative();
5073 return;
5074 }
5075
5076 if (isa<ConstantAggregateZero>(V)) {
5077 Known.KnownFPClasses = fcPosZero;
5078 Known.SignBit = false;
5079 return;
5080 }
5081
5082 if (isa<PoisonValue>(V)) {
5083 Known.KnownFPClasses = fcNone;
5084 Known.SignBit = false;
5085 return;
5086 }
5087
5088 // Try to handle fixed width vector constants
5089 auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5090 const Constant *CV = dyn_cast<Constant>(V);
5091 if (VFVTy && CV) {
5092 Known.KnownFPClasses = fcNone;
5093 bool SignBitAllZero = true;
5094 bool SignBitAllOne = true;
5095
5096 // For vectors, verify that each element is not NaN.
5097 unsigned NumElts = VFVTy->getNumElements();
5098 for (unsigned i = 0; i != NumElts; ++i) {
5099 if (!DemandedElts[i])
5100 continue;
5101
5102 Constant *Elt = CV->getAggregateElement(i);
5103 if (!Elt) {
5104 Known = KnownFPClass();
5105 return;
5106 }
5107 if (isa<PoisonValue>(Elt))
5108 continue;
5109 auto *CElt = dyn_cast<ConstantFP>(Elt);
5110 if (!CElt) {
5111 Known = KnownFPClass();
5112 return;
5113 }
5114
5115 const APFloat &C = CElt->getValueAPF();
5116 Known.KnownFPClasses |= C.classify();
5117 if (C.isNegative())
5118 SignBitAllZero = false;
5119 else
5120 SignBitAllOne = false;
5121 }
5122 if (SignBitAllOne != SignBitAllZero)
5123 Known.SignBit = SignBitAllOne;
5124 return;
5125 }
5126
5127 FPClassTest KnownNotFromFlags = fcNone;
5128 if (const auto *CB = dyn_cast<CallBase>(V))
5129 KnownNotFromFlags |= CB->getRetNoFPClass();
5130 else if (const auto *Arg = dyn_cast<Argument>(V))
5131 KnownNotFromFlags |= Arg->getNoFPClass();
5132
5133 const Operator *Op = dyn_cast<Operator>(V);
5134 if (const FPMathOperator *FPOp = dyn_cast_or_null<FPMathOperator>(Op)) {
5135 if (FPOp->hasNoNaNs())
5136 KnownNotFromFlags |= fcNan;
5137 if (FPOp->hasNoInfs())
5138 KnownNotFromFlags |= fcInf;
5139 }
5140
5141 KnownFPClass AssumedClasses = computeKnownFPClassFromContext(V, Q);
5142 KnownNotFromFlags |= ~AssumedClasses.KnownFPClasses;
5143
5144 // We no longer need to find out about these bits from inputs if we can
5145 // assume this from flags/attributes.
5146 InterestedClasses &= ~KnownNotFromFlags;
5147
5148 auto ClearClassesFromFlags = make_scope_exit([=, &Known] {
5149 Known.knownNot(KnownNotFromFlags);
5150 if (!Known.SignBit && AssumedClasses.SignBit) {
5151 if (*AssumedClasses.SignBit)
5152 Known.signBitMustBeOne();
5153 else
5154 Known.signBitMustBeZero();
5155 }
5156 });
5157
5158 if (!Op)
5159 return;
5160
5161 // All recursive calls that increase depth must come after this.
5163 return;
5164
5165 const unsigned Opc = Op->getOpcode();
5166 switch (Opc) {
5167 case Instruction::FNeg: {
5168 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5169 Known, Depth + 1, Q);
5170 Known.fneg();
5171 break;
5172 }
5173 case Instruction::Select: {
5174 Value *Cond = Op->getOperand(0);
5175 Value *LHS = Op->getOperand(1);
5176 Value *RHS = Op->getOperand(2);
5177
5178 FPClassTest FilterLHS = fcAllFlags;
5179 FPClassTest FilterRHS = fcAllFlags;
5180
5181 Value *TestedValue = nullptr;
5182 FPClassTest MaskIfTrue = fcAllFlags;
5183 FPClassTest MaskIfFalse = fcAllFlags;
5184 uint64_t ClassVal = 0;
5185 const Function *F = cast<Instruction>(Op)->getFunction();
5186 CmpPredicate Pred;
5187 Value *CmpLHS, *CmpRHS;
5188 if (F && match(Cond, m_FCmp(Pred, m_Value(CmpLHS), m_Value(CmpRHS)))) {
5189 // If the select filters out a value based on the class, it no longer
5190 // participates in the class of the result
5191
5192 // TODO: In some degenerate cases we can infer something if we try again
5193 // without looking through sign operations.
5194 bool LookThroughFAbsFNeg = CmpLHS != LHS && CmpLHS != RHS;
5195 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
5196 fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
5197 } else if (match(Cond,
5198 m_Intrinsic<Intrinsic::is_fpclass>(
5199 m_Value(TestedValue), m_ConstantInt(ClassVal)))) {
5200 FPClassTest TestedMask = static_cast<FPClassTest>(ClassVal);
5201 MaskIfTrue = TestedMask;
5202 MaskIfFalse = ~TestedMask;
5203 }
5204
5205 if (TestedValue == LHS) {
5206 // match !isnan(x) ? x : y
5207 FilterLHS = MaskIfTrue;
5208 } else if (TestedValue == RHS) { // && IsExactClass
5209 // match !isnan(x) ? y : x
5210 FilterRHS = MaskIfFalse;
5211 }
5212
5213 KnownFPClass Known2;
5214 computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
5215 Depth + 1, Q);
5216 Known.KnownFPClasses &= FilterLHS;
5217
5218 computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
5219 Known2, Depth + 1, Q);
5220 Known2.KnownFPClasses &= FilterRHS;
5221
5222 Known |= Known2;
5223 break;
5224 }
5225 case Instruction::Call: {
5226 const CallInst *II = cast<CallInst>(Op);
5227 const Intrinsic::ID IID = II->getIntrinsicID();
5228 switch (IID) {
5229 case Intrinsic::fabs: {
5230 if ((InterestedClasses & (fcNan | fcPositive)) != fcNone) {
5231 // If we only care about the sign bit we don't need to inspect the
5232 // operand.
5233 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5234 InterestedClasses, Known, Depth + 1, Q);
5235 }
5236
5237 Known.fabs();
5238 break;
5239 }
5240 case Intrinsic::copysign: {
5241 KnownFPClass KnownSign;
5242
5243 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5244 Known, Depth + 1, Q);
5245 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5246 KnownSign, Depth + 1, Q);
5247 Known.copysign(KnownSign);
5248 break;
5249 }
5250 case Intrinsic::fma:
5251 case Intrinsic::fmuladd: {
5252 if ((InterestedClasses & fcNegative) == fcNone)
5253 break;
5254
5255 if (II->getArgOperand(0) != II->getArgOperand(1))
5256 break;
5257
5258 // The multiply cannot be -0 and therefore the add can't be -0
5259 Known.knownNot(fcNegZero);
5260
5261 // x * x + y is non-negative if y is non-negative.
5262 KnownFPClass KnownAddend;
5263 computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
5264 KnownAddend, Depth + 1, Q);
5265
5266 if (KnownAddend.cannotBeOrderedLessThanZero())
5267 Known.knownNot(fcNegative);
5268 break;
5269 }
5270 case Intrinsic::sqrt:
5271 case Intrinsic::experimental_constrained_sqrt: {
5272 KnownFPClass KnownSrc;
5273 FPClassTest InterestedSrcs = InterestedClasses;
5274 if (InterestedClasses & fcNan)
5275 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5276
5277 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5278 KnownSrc, Depth + 1, Q);
5279
5280 if (KnownSrc.isKnownNeverPosInfinity())
5281 Known.knownNot(fcPosInf);
5282 if (KnownSrc.isKnownNever(fcSNan))
5283 Known.knownNot(fcSNan);
5284
5285 // Any negative value besides -0 returns a nan.
5286 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5287 Known.knownNot(fcNan);
5288
5289 // The only negative value that can be returned is -0 for -0 inputs.
5291
5292 // If the input denormal mode could be PreserveSign, a negative
5293 // subnormal input could produce a negative zero output.
5294 const Function *F = II->getFunction();
5295 if (Q.IIQ.hasNoSignedZeros(II) ||
5296 (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType())))
5297 Known.knownNot(fcNegZero);
5298
5299 break;
5300 }
5301 case Intrinsic::sin:
5302 case Intrinsic::cos: {
5303 // Return NaN on infinite inputs.
5304 KnownFPClass KnownSrc;
5305 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5306 KnownSrc, Depth + 1, Q);
5307 Known.knownNot(fcInf);
5308 if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
5309 Known.knownNot(fcNan);
5310 break;
5311 }
5312 case Intrinsic::maxnum:
5313 case Intrinsic::minnum:
5314 case Intrinsic::minimum:
5315 case Intrinsic::maximum: {
5316 KnownFPClass KnownLHS, KnownRHS;
5317 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5318 KnownLHS, Depth + 1, Q);
5319 computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5320 KnownRHS, Depth + 1, Q);
5321
5322 bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
5323 Known = KnownLHS | KnownRHS;
5324
5325 // If either operand is not NaN, the result is not NaN.
5326 if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum))
5327 Known.knownNot(fcNan);
5328
5329 if (IID == Intrinsic::maxnum) {
5330 // If at least one operand is known to be positive, the result must be
5331 // positive.
5332 if ((KnownLHS.cannotBeOrderedLessThanZero() &&
5333 KnownLHS.isKnownNeverNaN()) ||
5334 (KnownRHS.cannotBeOrderedLessThanZero() &&
5335 KnownRHS.isKnownNeverNaN()))
5337 } else if (IID == Intrinsic::maximum) {
5338 // If at least one operand is known to be positive, the result must be
5339 // positive.
5340 if (KnownLHS.cannotBeOrderedLessThanZero() ||
5341 KnownRHS.cannotBeOrderedLessThanZero())
5343 } else if (IID == Intrinsic::minnum) {
5344 // If at least one operand is known to be negative, the result must be
5345 // negative.
5346 if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
5347 KnownLHS.isKnownNeverNaN()) ||
5348 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
5349 KnownRHS.isKnownNeverNaN()))
5351 } else {
5352 // If at least one operand is known to be negative, the result must be
5353 // negative.
5354 if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
5357 }
5358
5359 // Fixup zero handling if denormals could be returned as a zero.
5360 //
5361 // As there's no spec for denormal flushing, be conservative with the
5362 // treatment of denormals that could be flushed to zero. For older
5363 // subtargets on AMDGPU the min/max instructions would not flush the
5364 // output and return the original value.
5365 //
5366 if ((Known.KnownFPClasses & fcZero) != fcNone &&
5367 !Known.isKnownNeverSubnormal()) {
5368 const Function *Parent = II->getFunction();
5369 if (!Parent)
5370 break;
5371
5372 DenormalMode Mode = Parent->getDenormalMode(
5373 II->getType()->getScalarType()->getFltSemantics());
5374 if (Mode != DenormalMode::getIEEE())
5375 Known.KnownFPClasses |= fcZero;
5376 }
5377
5378 if (Known.isKnownNeverNaN()) {
5379 if (KnownLHS.SignBit && KnownRHS.SignBit &&
5380 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5381 if (*KnownLHS.SignBit)
5382 Known.signBitMustBeOne();
5383 else
5384 Known.signBitMustBeZero();
5385 } else if ((IID == Intrinsic::maximum || IID == Intrinsic::minimum) ||
5386 ((KnownLHS.isKnownNeverNegZero() ||
5387 KnownRHS.isKnownNeverPosZero()) &&
5388 (KnownLHS.isKnownNeverPosZero() ||
5389 KnownRHS.isKnownNeverNegZero()))) {
5390 if ((IID == Intrinsic::maximum || IID == Intrinsic::maxnum) &&
5391 (KnownLHS.SignBit == false || KnownRHS.SignBit == false))
5392 Known.signBitMustBeZero();
5393 else if ((IID == Intrinsic::minimum || IID == Intrinsic::minnum) &&
5394 (KnownLHS.SignBit == true || KnownRHS.SignBit == true))
5395 Known.signBitMustBeOne();
5396 }
5397 }
5398 break;
5399 }
5400 case Intrinsic::canonicalize: {
5401 KnownFPClass KnownSrc;
5402 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5403 KnownSrc, Depth + 1, Q);
5404
5405 // This is essentially a stronger form of
5406 // propagateCanonicalizingSrc. Other "canonicalizing" operations don't
5407 // actually have an IR canonicalization guarantee.
5408
5409 // Canonicalize may flush denormals to zero, so we have to consider the
5410 // denormal mode to preserve known-not-0 knowledge.
5411 Known.KnownFPClasses = KnownSrc.KnownFPClasses | fcZero | fcQNan;
5412
5413 // Stronger version of propagateNaN
5414 // Canonicalize is guaranteed to quiet signaling nans.
5415 if (KnownSrc.isKnownNeverNaN())
5416 Known.knownNot(fcNan);
5417 else
5418 Known.knownNot(fcSNan);
5419
5420 const Function *F = II->getFunction();
5421 if (!F)
5422 break;
5423
5424 // If the parent function flushes denormals, the canonical output cannot
5425 // be a denormal.
5426 const fltSemantics &FPType =
5427 II->getType()->getScalarType()->getFltSemantics();
5428 DenormalMode DenormMode = F->getDenormalMode(FPType);
5429 if (DenormMode == DenormalMode::getIEEE()) {
5430 if (KnownSrc.isKnownNever(fcPosZero))
5431 Known.knownNot(fcPosZero);
5432 if (KnownSrc.isKnownNever(fcNegZero))
5433 Known.knownNot(fcNegZero);
5434 break;
5435 }
5436
5437 if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5438 Known.knownNot(fcSubnormal);
5439
5440 if (DenormMode.Input == DenormalMode::PositiveZero ||
5441 (DenormMode.Output == DenormalMode::PositiveZero &&
5442 DenormMode.Input == DenormalMode::IEEE))
5443 Known.knownNot(fcNegZero);
5444
5445 break;
5446 }
5447 case Intrinsic::vector_reduce_fmax:
5448 case Intrinsic::vector_reduce_fmin:
5449 case Intrinsic::vector_reduce_fmaximum:
5450 case Intrinsic::vector_reduce_fminimum: {
5451 // reduce min/max will choose an element from one of the vector elements,
5452 // so we can infer and class information that is common to all elements.
5453 Known = computeKnownFPClass(II->getArgOperand(0), II->getFastMathFlags(),
5454 InterestedClasses, Depth + 1, Q);
5455 // Can only propagate sign if output is never NaN.
5456 if (!Known.isKnownNeverNaN())
5457 Known.SignBit.reset();
5458 break;
5459 }
5460 // reverse preserves all characteristics of the input vec's element.
5461 case Intrinsic::vector_reverse:
5462 Known = computeKnownFPClass(
5463 II->getArgOperand(0), DemandedElts.reverseBits(),
5464 II->getFastMathFlags(), InterestedClasses, Depth + 1, Q);
5465 break;
5466 case Intrinsic::trunc:
5467 case Intrinsic::floor:
5468 case Intrinsic::ceil:
5469 case Intrinsic::rint:
5470 case Intrinsic::nearbyint:
5471 case Intrinsic::round:
5472 case Intrinsic::roundeven: {
5473 KnownFPClass KnownSrc;
5474 FPClassTest InterestedSrcs = InterestedClasses;
5475 if (InterestedSrcs & fcPosFinite)
5476 InterestedSrcs |= fcPosFinite;
5477 if (InterestedSrcs & fcNegFinite)
5478 InterestedSrcs |= fcNegFinite;
5479 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5480 KnownSrc, Depth + 1, Q);
5481
5482 // Integer results cannot be subnormal.
5483 Known.knownNot(fcSubnormal);
5484
5485 Known.propagateNaN(KnownSrc, true);
5486
5487 // Pass through infinities, except PPC_FP128 is a special case for
5488 // intrinsics other than trunc.
5489 if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5490 if (KnownSrc.isKnownNeverPosInfinity())
5491 Known.knownNot(fcPosInf);
5492 if (KnownSrc.isKnownNeverNegInfinity())
5493 Known.knownNot(fcNegInf);
5494 }
5495
5496 // Negative round ups to 0 produce -0
5497 if (KnownSrc.isKnownNever(fcPosFinite))
5498 Known.knownNot(fcPosFinite);
5499 if (KnownSrc.isKnownNever(fcNegFinite))
5500 Known.knownNot(fcNegFinite);
5501
5502 break;
5503 }
5504 case Intrinsic::exp:
5505 case Intrinsic::exp2:
5506 case Intrinsic::exp10: {
5507 Known.knownNot(fcNegative);
5508 if ((InterestedClasses & fcNan) == fcNone)
5509 break;
5510
5511 KnownFPClass KnownSrc;
5512 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5513 KnownSrc, Depth + 1, Q);
5514 if (KnownSrc.isKnownNeverNaN()) {
5515 Known.knownNot(fcNan);
5516 Known.signBitMustBeZero();
5517 }
5518
5519 break;
5520 }
5521 case Intrinsic::fptrunc_round: {
5522 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5523 Depth, Q);
5524 break;
5525 }
5526 case Intrinsic::log:
5527 case Intrinsic::log10:
5528 case Intrinsic::log2:
5529 case Intrinsic::experimental_constrained_log:
5530 case Intrinsic::experimental_constrained_log10:
5531 case Intrinsic::experimental_constrained_log2: {
5532 // log(+inf) -> +inf
5533 // log([+-]0.0) -> -inf
5534 // log(-inf) -> nan
5535 // log(-x) -> nan
5536 if ((InterestedClasses & (fcNan | fcInf)) == fcNone)
5537 break;
5538
5539 FPClassTest InterestedSrcs = InterestedClasses;
5540 if ((InterestedClasses & fcNegInf) != fcNone)
5541 InterestedSrcs |= fcZero | fcSubnormal;
5542 if ((InterestedClasses & fcNan) != fcNone)
5543 InterestedSrcs |= fcNan | (fcNegative & ~fcNan);
5544
5545 KnownFPClass KnownSrc;
5546 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5547 KnownSrc, Depth + 1, Q);
5548
5549 if (KnownSrc.isKnownNeverPosInfinity())
5550 Known.knownNot(fcPosInf);
5551
5552 if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5553 Known.knownNot(fcNan);
5554
5555 const Function *F = II->getFunction();
5556 if (F && KnownSrc.isKnownNeverLogicalZero(*F, II->getType()))
5557 Known.knownNot(fcNegInf);
5558
5559 break;
5560 }
5561 case Intrinsic::powi: {
5562 if ((InterestedClasses & fcNegative) == fcNone)
5563 break;
5564
5565 const Value *Exp = II->getArgOperand(1);
5566 Type *ExpTy = Exp->getType();
5567 unsigned BitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5568 KnownBits ExponentKnownBits(BitWidth);
5569 computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts : APInt(1, 1),
5570 ExponentKnownBits, Depth + 1, Q);
5571
5572 if (ExponentKnownBits.Zero[0]) { // Is even
5573 Known.knownNot(fcNegative);
5574 break;
5575 }
5576
5577 // Given that exp is an integer, here are the
5578 // ways that pow can return a negative value:
5579 //
5580 // pow(-x, exp) --> negative if exp is odd and x is negative.
5581 // pow(-0, exp) --> -inf if exp is negative odd.
5582 // pow(-0, exp) --> -0 if exp is positive odd.
5583 // pow(-inf, exp) --> -0 if exp is negative odd.
5584 // pow(-inf, exp) --> -inf if exp is positive odd.
5585 KnownFPClass KnownSrc;
5586 computeKnownFPClass(II->getArgOperand(0), DemandedElts, fcNegative,
5587 KnownSrc, Depth + 1, Q);
5588 if (KnownSrc.isKnownNever(fcNegative))
5589 Known.knownNot(fcNegative);
5590 break;
5591 }
5592 case Intrinsic::ldexp: {
5593 KnownFPClass KnownSrc;
5594 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5595 KnownSrc, Depth + 1, Q);
5596 Known.propagateNaN(KnownSrc, /*PropagateSign=*/true);
5597
5598 // Sign is preserved, but underflows may produce zeroes.
5599 if (KnownSrc.isKnownNever(fcNegative))
5600 Known.knownNot(fcNegative);
5601 else if (KnownSrc.cannotBeOrderedLessThanZero())
5603
5604 if (KnownSrc.isKnownNever(fcPositive))
5605 Known.knownNot(fcPositive);
5606 else if (KnownSrc.cannotBeOrderedGreaterThanZero())
5608
5609 // Can refine inf/zero handling based on the exponent operand.
5610 const FPClassTest ExpInfoMask = fcZero | fcSubnormal | fcInf;
5611 if ((InterestedClasses & ExpInfoMask) == fcNone)
5612 break;
5613 if ((KnownSrc.KnownFPClasses & ExpInfoMask) == fcNone)
5614 break;
5615
5616 const fltSemantics &Flt =
5617 II->getType()->getScalarType()->getFltSemantics();
5618 unsigned Precision = APFloat::semanticsPrecision(Flt);
5619 const Value *ExpArg = II->getArgOperand(1);
5621 ExpArg, true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT, Depth + 1);
5622
5623 const int MantissaBits = Precision - 1;
5624 if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5625 Known.knownNot(fcSubnormal);
5626
5627 const Function *F = II->getFunction();
5628 const APInt *ConstVal = ExpRange.getSingleElement();
5629 if (ConstVal && ConstVal->isZero()) {
5630 // ldexp(x, 0) -> x, so propagate everything.
5631 Known.propagateCanonicalizingSrc(KnownSrc, *F, II->getType());
5632 } else if (ExpRange.isAllNegative()) {
5633 // If we know the power is <= 0, can't introduce inf
5634 if (KnownSrc.isKnownNeverPosInfinity())
5635 Known.knownNot(fcPosInf);
5636 if (KnownSrc.isKnownNeverNegInfinity())
5637 Known.knownNot(fcNegInf);
5638 } else if (ExpRange.isAllNonNegative()) {
5639 // If we know the power is >= 0, can't introduce subnormal or zero
5640 if (KnownSrc.isKnownNeverPosSubnormal())
5641 Known.knownNot(fcPosSubnormal);
5642 if (KnownSrc.isKnownNeverNegSubnormal())
5643 Known.knownNot(fcNegSubnormal);
5644 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, II->getType()))
5645 Known.knownNot(fcPosZero);
5646 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, II->getType()))
5647 Known.knownNot(fcNegZero);
5648 }
5649
5650 break;
5651 }
5652 case Intrinsic::arithmetic_fence: {
5653 computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5654 Known, Depth + 1, Q);
5655 break;
5656 }
5657 case Intrinsic::experimental_constrained_sitofp:
5658 case Intrinsic::experimental_constrained_uitofp:
5659 // Cannot produce nan
5660 Known.knownNot(fcNan);
5661
5662 // sitofp and uitofp turn into +0.0 for zero.
5663 Known.knownNot(fcNegZero);
5664
5665 // Integers cannot be subnormal
5666 Known.knownNot(fcSubnormal);
5667
5668 if (IID == Intrinsic::experimental_constrained_uitofp)
5669 Known.signBitMustBeZero();
5670
5671 // TODO: Copy inf handling from instructions
5672 break;
5673 default:
5674 break;
5675 }
5676
5677 break;
5678 }
5679 case Instruction::FAdd:
5680 case Instruction::FSub: {
5681 KnownFPClass KnownLHS, KnownRHS;
5682 bool WantNegative =
5683 Op->getOpcode() == Instruction::FAdd &&
5684 (InterestedClasses & KnownFPClass::OrderedLessThanZeroMask) != fcNone;
5685 bool WantNaN = (InterestedClasses & fcNan) != fcNone;
5686 bool WantNegZero = (InterestedClasses & fcNegZero) != fcNone;
5687
5688 if (!WantNaN && !WantNegative && !WantNegZero)
5689 break;
5690
5691 FPClassTest InterestedSrcs = InterestedClasses;
5692 if (WantNegative)
5693 InterestedSrcs |= KnownFPClass::OrderedLessThanZeroMask;
5694 if (InterestedClasses & fcNan)
5695 InterestedSrcs |= fcInf;
5696 computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5697 KnownRHS, Depth + 1, Q);
5698
5699 if ((WantNaN && KnownRHS.isKnownNeverNaN()) ||
5700 (WantNegative && KnownRHS.cannotBeOrderedLessThanZero()) ||
5701 WantNegZero || Opc == Instruction::FSub) {
5702
5703 // RHS is canonically cheaper to compute. Skip inspecting the LHS if
5704 // there's no point.
5705 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5706 KnownLHS, Depth + 1, Q);
5707 // Adding positive and negative infinity produces NaN.
5708 // TODO: Check sign of infinities.
5709 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5710 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5711 Known.knownNot(fcNan);
5712
5713 // FIXME: Context function should always be passed in separately
5714 const Function *F = cast<Instruction>(Op)->getFunction();
5715
5716 if (Op->getOpcode() == Instruction::FAdd) {
5717 if (KnownLHS.cannotBeOrderedLessThanZero() &&
5718 KnownRHS.cannotBeOrderedLessThanZero())
5720 if (!F)
5721 break;
5722
5723 // (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5724 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5725 KnownRHS.isKnownNeverLogicalNegZero(*F, Op->getType())) &&
5726 // Make sure output negative denormal can't flush to -0
5727 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5728 Known.knownNot(fcNegZero);
5729 } else {
5730 if (!F)
5731 break;
5732
5733 // Only fsub -0, +0 can return -0
5734 if ((KnownLHS.isKnownNeverLogicalNegZero(*F, Op->getType()) ||
5735 KnownRHS.isKnownNeverLogicalPosZero(*F, Op->getType())) &&
5736 // Make sure output negative denormal can't flush to -0
5737 outputDenormalIsIEEEOrPosZero(*F, Op->getType()))
5738 Known.knownNot(fcNegZero);
5739 }
5740 }
5741
5742 break;
5743 }
5744 case Instruction::FMul: {
5745 // X * X is always non-negative or a NaN.
5746 if (Op->getOperand(0) == Op->getOperand(1))
5747 Known.knownNot(fcNegative);
5748
5749 if ((InterestedClasses & fcNan) != fcNan)
5750 break;
5751
5752 // fcSubnormal is only needed in case of DAZ.
5753 const FPClassTest NeedForNan = fcNan | fcInf | fcZero | fcSubnormal;
5754
5755 KnownFPClass KnownLHS, KnownRHS;
5756 computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5757 Depth + 1, Q);
5758 if (!KnownRHS.isKnownNeverNaN())
5759 break;
5760
5761 computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5762 Depth + 1, Q);
5763 if (!KnownLHS.isKnownNeverNaN())
5764 break;
5765
5766 if (KnownLHS.SignBit && KnownRHS.SignBit) {
5767 if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5768 Known.signBitMustBeZero();
5769 else
5770 Known.signBitMustBeOne();
5771 }
5772
5773 // If 0 * +/-inf produces NaN.
5774 if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5775 Known.knownNot(fcNan);
5776 break;
5777 }
5778
5779 const Function *F = cast<Instruction>(Op)->getFunction();
5780 if (!F)
5781 break;
5782
5783 if ((KnownRHS.isKnownNeverInfinity() ||
5784 KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) &&
5785 (KnownLHS.isKnownNeverInfinity() ||
5786 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))
5787 Known.knownNot(fcNan);
5788
5789 break;
5790 }
5791 case Instruction::FDiv:
5792 case Instruction::FRem: {
5793 if (Op->getOperand(0) == Op->getOperand(1)) {
5794 // TODO: Could filter out snan if we inspect the operand
5795 if (Op->getOpcode() == Instruction::FDiv) {
5796 // X / X is always exactly 1.0 or a NaN.
5798 } else {
5799 // X % X is always exactly [+-]0.0 or a NaN.
5800 Known.KnownFPClasses = fcNan | fcZero;
5801 }
5802
5803 break;
5804 }
5805
5806 const bool WantNan = (InterestedClasses & fcNan) != fcNone;
5807 const bool WantNegative = (InterestedClasses & fcNegative) != fcNone;
5808 const bool WantPositive =
5809 Opc == Instruction::FRem && (InterestedClasses & fcPositive) != fcNone;
5810 if (!WantNan && !WantNegative && !WantPositive)
5811 break;
5812
5813 KnownFPClass KnownLHS, KnownRHS;
5814
5815 computeKnownFPClass(Op->getOperand(1), DemandedElts,
5816 fcNan | fcInf | fcZero | fcNegative, KnownRHS,
5817 Depth + 1, Q);
5818
5819 bool KnowSomethingUseful =
5820 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5821
5822 if (KnowSomethingUseful || WantPositive) {
5823 const FPClassTest InterestedLHS =
5824 WantPositive ? fcAllFlags
5826
5827 computeKnownFPClass(Op->getOperand(0), DemandedElts,
5828 InterestedClasses & InterestedLHS, KnownLHS,
5829 Depth + 1, Q);
5830 }
5831
5832 const Function *F = cast<Instruction>(Op)->getFunction();
5833
5834 if (Op->getOpcode() == Instruction::FDiv) {
5835 // Only 0/0, Inf/Inf produce NaN.
5836 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5837 (KnownLHS.isKnownNeverInfinity() ||
5838 KnownRHS.isKnownNeverInfinity()) &&
5839 ((F && KnownLHS.isKnownNeverLogicalZero(*F, Op->getType())) ||
5840 (F && KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())))) {
5841 Known.knownNot(fcNan);
5842 }
5843
5844 // X / -0.0 is -Inf (or NaN).
5845 // +X / +X is +X
5846 if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5847 Known.knownNot(fcNegative);
5848 } else {
5849 // Inf REM x and x REM 0 produce NaN.
5850 if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5851 KnownLHS.isKnownNeverInfinity() && F &&
5852 KnownRHS.isKnownNeverLogicalZero(*F, Op->getType())) {
5853 Known.knownNot(fcNan);
5854 }
5855
5856 // The sign for frem is the same as the first operand.
5857 if (KnownLHS.cannotBeOrderedLessThanZero())
5859 if (KnownLHS.cannotBeOrderedGreaterThanZero())
5861
5862 // See if we can be more aggressive about the sign of 0.
5863 if (KnownLHS.isKnownNever(fcNegative))
5864 Known.knownNot(fcNegative);
5865 if (KnownLHS.isKnownNever(fcPositive))
5866 Known.knownNot(fcPositive);
5867 }
5868
5869 break;
5870 }
5871 case Instruction::FPExt: {
5872 // Infinity, nan and zero propagate from source.
5873 computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5874 Known, Depth + 1, Q);
5875
5876 const fltSemantics &DstTy =
5877 Op->getType()->getScalarType()->getFltSemantics();
5878 const fltSemantics &SrcTy =
5879 Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5880
5881 // All subnormal inputs should be in the normal range in the result type.
5882 if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5883 if (Known.KnownFPClasses & fcPosSubnormal)
5884 Known.KnownFPClasses |= fcPosNormal;
5885 if (Known.KnownFPClasses & fcNegSubnormal)
5886 Known.KnownFPClasses |= fcNegNormal;
5887 Known.knownNot(fcSubnormal);
5888 }
5889
5890 // Sign bit of a nan isn't guaranteed.
5891 if (!Known.isKnownNeverNaN())
5892 Known.SignBit = std::nullopt;
5893 break;
5894 }
5895 case Instruction::FPTrunc: {
5896 computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5897 Depth, Q);
5898 break;
5899 }
5900 case Instruction::SIToFP:
5901 case Instruction::UIToFP: {
5902 // Cannot produce nan
5903 Known.knownNot(fcNan);
5904
5905 // Integers cannot be subnormal
5906 Known.knownNot(fcSubnormal);
5907
5908 // sitofp and uitofp turn into +0.0 for zero.
5909 Known.knownNot(fcNegZero);
5910 if (Op->getOpcode() == Instruction::UIToFP)
5911 Known.signBitMustBeZero();
5912
5913 if (InterestedClasses & fcInf) {
5914 // Get width of largest magnitude integer (remove a bit if signed).
5915 // This still works for a signed minimum value because the largest FP
5916 // value is scaled by some fraction close to 2.0 (1.0 + 0.xxxx).
5917 int IntSize = Op->getOperand(0)->getType()->getScalarSizeInBits();
5918 if (Op->getOpcode() == Instruction::SIToFP)
5919 --IntSize;
5920
5921 // If the exponent of the largest finite FP value can hold the largest
5922 // integer, the result of the cast must be finite.
5923 Type *FPTy = Op->getType()->getScalarType();
5924 if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5925 Known.knownNot(fcInf);
5926 }
5927
5928 break;
5929 }
5930 case Instruction::ExtractElement: {
5931 // Look through extract element. If the index is non-constant or
5932 // out-of-range demand all elements, otherwise just the extracted element.
5933 const Value *Vec = Op->getOperand(0);
5934 const Value *Idx = Op->getOperand(1);
5935 auto *CIdx = dyn_cast<ConstantInt>(Idx);
5936
5937 if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5938 unsigned NumElts = VecTy->getNumElements();
5939 APInt DemandedVecElts = APInt::getAllOnes(NumElts);
5940 if (CIdx && CIdx->getValue().ult(NumElts))
5941 DemandedVecElts = APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5942 return computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5943 Depth + 1, Q);
5944 }
5945
5946 break;
5947 }
5948 case Instruction::InsertElement: {
5949 if (isa<ScalableVectorType>(Op->getType()))
5950 return;
5951
5952 const Value *Vec = Op->getOperand(0);
5953 const Value *Elt = Op->getOperand(1);
5954 auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5955 unsigned NumElts = DemandedElts.getBitWidth();
5956 APInt DemandedVecElts = DemandedElts;
5957 bool NeedsElt = true;
5958 // If we know the index we are inserting to, clear it from Vec check.
5959 if (CIdx && CIdx->getValue().ult(NumElts)) {
5960 DemandedVecElts.clearBit(CIdx->getZExtValue());
5961 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5962 }
5963
5964 // Do we demand the inserted element?
5965 if (NeedsElt) {
5966 computeKnownFPClass(Elt, Known, InterestedClasses, Depth + 1, Q);
5967 // If we don't know any bits, early out.
5968 if (Known.isUnknown())
5969 break;
5970 } else {
5971 Known.KnownFPClasses = fcNone;
5972 }
5973
5974 // Do we need anymore elements from Vec?
5975 if (!DemandedVecElts.isZero()) {
5976 KnownFPClass Known2;
5977 computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
5978 Depth + 1, Q);
5979 Known |= Known2;
5980 }
5981
5982 break;
5983 }
5984 case Instruction::ShuffleVector: {
5985 // For undef elements, we don't know anything about the common state of
5986 // the shuffle result.
5987 APInt DemandedLHS, DemandedRHS;
5988 auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5989 if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5990 return;
5991
5992 if (!!DemandedLHS) {
5993 const Value *LHS = Shuf->getOperand(0);
5994 computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
5995 Depth + 1, Q);
5996
5997 // If we don't know any bits, early out.
5998 if (Known.isUnknown())
5999 break;
6000 } else {
6001 Known.KnownFPClasses = fcNone;
6002 }
6003
6004 if (!!DemandedRHS) {
6005 KnownFPClass Known2;
6006 const Value *RHS = Shuf->getOperand(1);
6007 computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
6008 Depth + 1, Q);
6009 Known |= Known2;
6010 }
6011
6012 break;
6013 }
6014 case Instruction::ExtractValue: {
6015 const ExtractValueInst *Extract = cast<ExtractValueInst>(Op);
6016 ArrayRef<unsigned> Indices = Extract->getIndices();
6017 const Value *Src = Extract->getAggregateOperand();
6018 if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
6019 Indices[0] == 0) {
6020 if (const auto *II = dyn_cast<IntrinsicInst>(Src)) {
6021 switch (II->getIntrinsicID()) {
6022 case Intrinsic::frexp: {
6023 Known.knownNot(fcSubnormal);
6024
6025 KnownFPClass KnownSrc;
6026 computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6027 InterestedClasses, KnownSrc, Depth + 1, Q);
6028
6029 const Function *F = cast<Instruction>(Op)->getFunction();
6030
6031 if (KnownSrc.isKnownNever(fcNegative))
6032 Known.knownNot(fcNegative);
6033 else {
6034 if (F && KnownSrc.isKnownNeverLogicalNegZero(*F, Op->getType()))
6035 Known.knownNot(fcNegZero);
6036 if (KnownSrc.isKnownNever(fcNegInf))
6037 Known.knownNot(fcNegInf);
6038 }
6039
6040 if (KnownSrc.isKnownNever(fcPositive))
6041 Known.knownNot(fcPositive);
6042 else {
6043 if (F && KnownSrc.isKnownNeverLogicalPosZero(*F, Op->getType()))
6044 Known.knownNot(fcPosZero);
6045 if (KnownSrc.isKnownNever(fcPosInf))
6046 Known.knownNot(fcPosInf);
6047 }
6048
6049 Known.propagateNaN(KnownSrc);
6050 return;
6051 }
6052 default:
6053 break;
6054 }
6055 }
6056 }
6057
6058 computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known, Depth + 1,
6059 Q);
6060 break;
6061 }
6062 case Instruction::PHI: {
6063 const PHINode *P = cast<PHINode>(Op);
6064 // Unreachable blocks may have zero-operand PHI nodes.
6065 if (P->getNumIncomingValues() == 0)
6066 break;
6067
6068 // Otherwise take the unions of the known bit sets of the operands,
6069 // taking conservative care to avoid excessive recursion.
6070 const unsigned PhiRecursionLimit = MaxAnalysisRecursionDepth - 2;
6071
6072 if (Depth < PhiRecursionLimit) {
6073 // Skip if every incoming value references to ourself.
6074 if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6075 break;
6076
6077 bool First = true;
6078
6079 for (const Use &U : P->operands()) {
6080 Value *IncValue;
6081 Instruction *CxtI;
6082 breakSelfRecursivePHI(&U, P, IncValue, CxtI);
6083 // Skip direct self references.
6084 if (IncValue == P)
6085 continue;
6086
6087 KnownFPClass KnownSrc;
6088 // Recurse, but cap the recursion to two levels, because we don't want
6089 // to waste time spinning around in loops. We need at least depth 2 to
6090 // detect known sign bits.
6091 computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6092 PhiRecursionLimit,
6094
6095 if (First) {
6096 Known = KnownSrc;
6097 First = false;
6098 } else {
6099 Known |= KnownSrc;
6100 }
6101
6102 if (Known.KnownFPClasses == fcAllFlags)
6103 break;
6104 }
6105 }
6106
6107 break;
6108 }
6109 case Instruction::BitCast: {
6110 const Value *Src;
6111 if (!match(Op, m_ElementWiseBitCast(m_Value(Src))) ||
6112 !Src->getType()->isIntOrIntVectorTy())
6113 break;
6114
6115 const Type *Ty = Op->getType()->getScalarType();
6116 KnownBits Bits(Ty->getScalarSizeInBits());
6117 computeKnownBits(Src, DemandedElts, Bits, Depth + 1, Q);
6118
6119 // Transfer information from the sign bit.
6120 if (Bits.isNonNegative())
6121 Known.signBitMustBeZero();
6122 else if (Bits.isNegative())
6123 Known.signBitMustBeOne();
6124
6125 if (Ty->isIEEE()) {
6126 // IEEE floats are NaN when all bits of the exponent plus at least one of
6127 // the fraction bits are 1. This means:
6128 // - If we assume unknown bits are 0 and the value is NaN, it will
6129 // always be NaN
6130 // - If we assume unknown bits are 1 and the value is not NaN, it can
6131 // never be NaN
6132 if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
6133 Known.KnownFPClasses = fcNan;
6134 else if (!APFloat(Ty->getFltSemantics(), ~Bits.Zero).isNaN())
6135 Known.knownNot(fcNan);
6136
6137 // Build KnownBits representing Inf and check if it must be equal or
6138 // unequal to this value.
6139 auto InfKB = KnownBits::makeConstant(
6141 InfKB.Zero.clearSignBit();
6142 if (const auto InfResult = KnownBits::eq(Bits, InfKB)) {
6143 assert(!InfResult.value());
6144 Known.knownNot(fcInf);
6145 } else if (Bits == InfKB) {
6146 Known.KnownFPClasses = fcInf;
6147 }
6148
6149 // Build KnownBits representing Zero and check if it must be equal or
6150 // unequal to this value.
6151 auto ZeroKB = KnownBits::makeConstant(
6153 ZeroKB.Zero.clearSignBit();
6154 if (const auto ZeroResult = KnownBits::eq(Bits, ZeroKB)) {
6155 assert(!ZeroResult.value());
6156 Known.knownNot(fcZero);
6157 } else if (Bits == ZeroKB) {
6158 Known.KnownFPClasses = fcZero;
6159 }
6160 }
6161
6162 break;
6163 }
6164 default:
6165 break;
6166 }
6167}
6168
6170 const APInt &DemandedElts,
6171 FPClassTest InterestedClasses,
6172 unsigned Depth,
6173 const SimplifyQuery &SQ) {
6174 KnownFPClass KnownClasses;
6175 ::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses, Depth,
6176 SQ);
6177 return KnownClasses;
6178}
6179
6181 FPClassTest InterestedClasses,
6182 unsigned Depth,
6183 const SimplifyQuery &SQ) {
6184 KnownFPClass Known;
6185 ::computeKnownFPClass(V, Known, InterestedClasses, Depth, SQ);
6186 return Known;
6187}
6188
6190
6191 // All byte-wide stores are splatable, even of arbitrary variables.
6192 if (V->getType()->isIntegerTy(8))
6193 return V;
6194
6195 LLVMContext &Ctx = V->getContext();
6196
6197 // Undef don't care.
6198 auto *UndefInt8 = UndefValue::get(Type::getInt8Ty(Ctx));
6199 if (isa<UndefValue>(V))
6200 return UndefInt8;
6201
6202 // Return poison for zero-sized type.
6203 if (DL.getTypeStoreSize(V->getType()).isZero())
6204 return PoisonValue::get(Type::getInt8Ty(Ctx));
6205
6206 Constant *C = dyn_cast<Constant>(V);
6207 if (!C) {
6208 // Conceptually, we could handle things like:
6209 // %a = zext i8 %X to i16
6210 // %b = shl i16 %a, 8
6211 // %c = or i16 %a, %b
6212 // but until there is an example that actually needs this, it doesn't seem
6213 // worth worrying about.
6214 return nullptr;
6215 }
6216
6217 // Handle 'null' ConstantArrayZero etc.
6218 if (C->isNullValue())
6220
6221 // Constant floating-point values can be handled as integer values if the
6222 // corresponding integer value is "byteable". An important case is 0.0.
6223 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6224 Type *Ty = nullptr;
6225 if (CFP->getType()->isHalfTy())
6226 Ty = Type::getInt16Ty(Ctx);
6227 else if (CFP->getType()->isFloatTy())
6228 Ty = Type::getInt32Ty(Ctx);
6229 else if (CFP->getType()->isDoubleTy())
6230 Ty = Type::getInt64Ty(Ctx);
6231 // Don't handle long double formats, which have strange constraints.
6232 return Ty ? isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty), DL)
6233 : nullptr;
6234 }
6235
6236 // We can handle constant integers that are multiple of 8 bits.
6237 if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6238 if (CI->getBitWidth() % 8 == 0) {
6239 assert(CI->getBitWidth() > 8 && "8 bits should be handled above!");
6240 if (!CI->getValue().isSplat(8))
6241 return nullptr;
6242 return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6243 }
6244 }
6245
6246 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6247 if (CE->getOpcode() == Instruction::IntToPtr) {
6248 if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6249 unsigned BitWidth = DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6251 CE->getOperand(0), Type::getIntNTy(Ctx, BitWidth), false, DL))
6252 return isBytewiseValue(Op, DL);
6253 }
6254 }
6255 }
6256
6257 auto Merge = [&](Value *LHS, Value *RHS) -> Value * {
6258 if (LHS == RHS)
6259 return LHS;
6260 if (!LHS || !RHS)
6261 return nullptr;
6262 if (LHS == UndefInt8)
6263 return RHS;
6264 if (RHS == UndefInt8)
6265 return LHS;
6266 return nullptr;
6267 };
6268
6269 if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
6270 Value *Val = UndefInt8;
6271 for (unsigned I = 0, E = CA->getNumElements(); I != E; ++I)
6272 if (!(Val = Merge(Val, isBytewiseValue(CA->getElementAsConstant(I), DL))))
6273 return nullptr;
6274 return Val;
6275 }
6276
6277 if (isa<ConstantAggregate>(C)) {
6278 Value *Val = UndefInt8;
6279 for (Value *Op : C->operands())
6280 if (!(Val = Merge(Val, isBytewiseValue(Op, DL))))
6281 return nullptr;
6282 return Val;
6283 }
6284
6285 // Don't try to handle the handful of other constants.
6286 return nullptr;
6287}
6288
6289// This is the recursive version of BuildSubAggregate. It takes a few different
6290// arguments. Idxs is the index within the nested struct From that we are
6291// looking at now (which is of type IndexedType). IdxSkip is the number of
6292// indices from Idxs that should be left out when inserting into the resulting
6293// struct. To is the result struct built so far, new insertvalue instructions
6294// build on that.
6295static Value *BuildSubAggregate(Value *From, Value *To, Type *IndexedType,
6297 unsigned IdxSkip,
6298 BasicBlock::iterator InsertBefore) {
6299 StructType *STy = dyn_cast<StructType>(IndexedType);
6300 if (STy) {
6301 // Save the original To argument so we can modify it
6302 Value *OrigTo = To;
6303 // General case, the type indexed by Idxs is a struct
6304 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6305 // Process each struct element recursively
6306 Idxs.push_back(i);
6307 Value *PrevTo = To;
6308 To = BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6309 InsertBefore);
6310 Idxs.pop_back();
6311 if (!To) {
6312 // Couldn't find any inserted value for this index? Cleanup
6313 while (PrevTo != OrigTo) {
6314 InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
6315 PrevTo = Del->getAggregateOperand();
6316 Del->eraseFromParent();
6317 }
6318 // Stop processing elements
6319 break;
6320 }
6321 }
6322 // If we successfully found a value for each of our subaggregates
6323 if (To)
6324 return To;
6325 }
6326 // Base case, the type indexed by SourceIdxs is not a struct, or not all of
6327 // the struct's elements had a value that was inserted directly. In the latter
6328 // case, perhaps we can't determine each of the subelements individually, but
6329 // we might be able to find the complete struct somewhere.
6330
6331 // Find the value that is at that particular spot
6332 Value *V = FindInsertedValue(From, Idxs);
6333
6334 if (!V)
6335 return nullptr;
6336
6337 // Insert the value in the new (sub) aggregate
6338 return InsertValueInst::Create(To, V, ArrayRef(Idxs).slice(IdxSkip), "tmp",
6339 InsertBefore);
6340}
6341
6342// This helper takes a nested struct and extracts a part of it (which is again a
6343// struct) into a new value. For example, given the struct:
6344// { a, { b, { c, d }, e } }
6345// and the indices "1, 1" this returns
6346// { c, d }.
6347//
6348// It does this by inserting an insertvalue for each element in the resulting
6349// struct, as opposed to just inserting a single struct. This will only work if
6350// each of the elements of the substruct are known (ie, inserted into From by an
6351// insertvalue instruction somewhere).
6352//
6353// All inserted insertvalue instructions are inserted before InsertBefore
6355 BasicBlock::iterator InsertBefore) {
6356 Type *IndexedType = ExtractValueInst::getIndexedType(From->getType(),
6357 idx_range);
6358 Value *To = PoisonValue::get(IndexedType);
6359 SmallVector<unsigned, 10> Idxs(idx_range);
6360 unsigned IdxSkip = Idxs.size();
6361
6362 return BuildSubAggregate(From, To, IndexedType, Idxs, IdxSkip, InsertBefore);
6363}
6364
6365/// Given an aggregate and a sequence of indices, see if the scalar value
6366/// indexed is already around as a register, for example if it was inserted
6367/// directly into the aggregate.
6368///
6369/// If InsertBefore is not null, this function will duplicate (modified)
6370/// insertvalues when a part of a nested struct is extracted.
6371Value *
6373 std::optional<BasicBlock::iterator> InsertBefore) {
6374 // Nothing to index? Just return V then (this is useful at the end of our
6375 // recursion).
6376 if (idx_range.empty())
6377 return V;
6378 // We have indices, so V should have an indexable type.
6379 assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6380 "Not looking at a struct or array?");
6381 assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6382 "Invalid indices for type?");
6383
6384 if (Constant *C = dyn_cast<Constant>(V)) {
6385 C = C->getAggregateElement(idx_range[0]);
6386 if (!C) return nullptr;
6387 return FindInsertedValue(C, idx_range.slice(1), InsertBefore);
6388 }
6389
6390 if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
6391 // Loop the indices for the insertvalue instruction in parallel with the
6392 // requested indices
6393 const unsigned *req_idx = idx_range.begin();
6394 for (const unsigned *i = I->idx_begin(), *e = I->idx_end();
6395 i != e; ++i, ++req_idx) {
6396 if (req_idx == idx_range.end()) {
6397 // We can't handle this without inserting insertvalues
6398 if (!InsertBefore)
6399 return nullptr;
6400
6401 // The requested index identifies a part of a nested aggregate. Handle
6402 // this specially. For example,
6403 // %A = insertvalue { i32, {i32, i32 } } undef, i32 10, 1, 0
6404 // %B = insertvalue { i32, {i32, i32 } } %A, i32 11, 1, 1
6405 // %C = extractvalue {i32, { i32, i32 } } %B, 1
6406 // This can be changed into
6407 // %A = insertvalue {i32, i32 } undef, i32 10, 0
6408 // %C = insertvalue {i32, i32 } %A, i32 11, 1
6409 // which allows the unused 0,0 element from the nested struct to be
6410 // removed.
6411 return BuildSubAggregate(V, ArrayRef(idx_range.begin(), req_idx),
6412 *InsertBefore);
6413 }
6414
6415 // This insert value inserts something else than what we are looking for.
6416 // See if the (aggregate) value inserted into has the value we are
6417 // looking for, then.
6418 if (*req_idx != *i)
6419 return FindInsertedValue(I->getAggregateOperand(), idx_range,
6420 InsertBefore);
6421 }
6422 // If we end up here, the indices of the insertvalue match with those
6423 // requested (though possibly only partially). Now we recursively look at
6424 // the inserted value, passing any remaining indices.
6425 return FindInsertedValue(I->getInsertedValueOperand(),
6426 ArrayRef(req_idx, idx_range.end()), InsertBefore);
6427 }
6428
6429 if (ExtractValueInst *I = dyn_cast<ExtractValueInst>(V)) {
6430 // If we're extracting a value from an aggregate that was extracted from
6431 // something else, we can extract from that something else directly instead.
6432 // However, we will need to chain I's indices with the requested indices.
6433
6434 // Calculate the number of indices required
6435 unsigned size = I->getNumIndices() + idx_range.size();
6436 // Allocate some space to put the new indices in
6438 Idxs.reserve(size);
6439 // Add indices from the extract value instruction
6440 Idxs.append(I->idx_begin(), I->idx_end());
6441
6442 // Add requested indices
6443 Idxs.append(idx_range.begin(), idx_range.end());
6444
6445 assert(Idxs.size() == size
6446 && "Number of indices added not correct?");
6447
6448 return FindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6449 }
6450 // Otherwise, we don't know (such as, extracting from a function return value
6451 // or load instruction)
6452 return nullptr;
6453}
6454
6456 unsigned CharSize) {
6457 // Make sure the GEP has exactly three arguments.
6458 if (GEP->getNumOperands() != 3)
6459 return false;
6460
6461 // Make sure the index-ee is a pointer to array of \p CharSize integers.
6462 // CharSize.
6463 ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
6464 if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
6465 return false;
6466
6467 // Check to make sure that the first operand of the GEP is an integer and
6468 // has value 0 so that we are sure we're indexing into the initializer.
6469 const ConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
6470 if (!FirstIdx || !FirstIdx->isZero())
6471 return false;
6472
6473 return true;
6474}
6475
6476// If V refers to an initialized global constant, set Slice either to
6477// its initializer if the size of its elements equals ElementSize, or,
6478// for ElementSize == 8, to its representation as an array of unsiged
6479// char. Return true on success.
6480// Offset is in the unit "nr of ElementSize sized elements".
6483 unsigned ElementSize, uint64_t Offset) {
6484 assert(V && "V should not be null.");
6485 assert((ElementSize % 8) == 0 &&
6486 "ElementSize expected to be a multiple of the size of a byte.");
6487 unsigned ElementSizeInBytes = ElementSize / 8;
6488
6489 // Drill down into the pointer expression V, ignoring any intervening
6490 // casts, and determine the identity of the object it references along
6491 // with the cumulative byte offset into it.
6492 const GlobalVariable *GV =
6493 dyn_cast<GlobalVariable>(getUnderlyingObject(V));
6494 if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6495 // Fail if V is not based on constant global object.
6496 return false;
6497
6498 const DataLayout &DL = GV->getDataLayout();
6499 APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6500
6501 if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6502 /*AllowNonInbounds*/ true))
6503 // Fail if a constant offset could not be determined.
6504 return false;
6505
6506 uint64_t StartIdx = Off.getLimitedValue();
6507 if (StartIdx == UINT64_MAX)
6508 // Fail if the constant offset is excessive.
6509 return false;
6510
6511 // Off/StartIdx is in the unit of bytes. So we need to convert to number of
6512 // elements. Simply bail out if that isn't possible.
6513 if ((StartIdx % ElementSizeInBytes) != 0)
6514 return false;
6515
6516 Offset += StartIdx / ElementSizeInBytes;
6517 ConstantDataArray *Array = nullptr;
6518 ArrayType *ArrayTy = nullptr;
6519
6520 if (GV->getInitializer()->isNullValue()) {
6521 Type *GVTy = GV->getValueType();
6522 uint64_t SizeInBytes = DL.getTypeStoreSize(GVTy).getFixedValue();
6523 uint64_t Length = SizeInBytes / ElementSizeInBytes;
6524
6525 Slice.Array = nullptr;
6526 Slice.Offset = 0;
6527 // Return an empty Slice for undersized constants to let callers
6528 // transform even undefined library calls into simpler, well-defined
6529 // expressions. This is preferable to making the calls although it
6530 // prevents sanitizers from detecting such calls.
6531 Slice.Length = Length < Offset ? 0 : Length - Offset;
6532 return true;
6533 }
6534
6535 auto *Init = const_cast<Constant *>(GV->getInitializer());
6536 if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6537 Type *InitElTy = ArrayInit->getElementType();
6538 if (InitElTy->isIntegerTy(ElementSize)) {
6539 // If Init is an initializer for an array of the expected type
6540 // and size, use it as is.
6541 Array = ArrayInit;
6542 ArrayTy = ArrayInit->getType();
6543 }
6544 }
6545
6546 if (!Array) {
6547 if (ElementSize != 8)
6548 // TODO: Handle conversions to larger integral types.
6549 return false;
6550
6551 // Otherwise extract the portion of the initializer starting
6552 // at Offset as an array of bytes, and reset Offset.
6554 if (!Init)
6555 return false;
6556
6557 Offset = 0;
6558 Array = dyn_cast<ConstantDataArray>(Init);
6559 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6560 }
6561
6562 uint64_t NumElts = ArrayTy->getArrayNumElements();
6563 if (Offset > NumElts)
6564 return false;
6565
6566 Slice.Array = Array;
6567 Slice.Offset = Offset;
6568 Slice.Length = NumElts - Offset;
6569 return true;
6570}
6571
6572/// Extract bytes from the initializer of the constant array V, which need
6573/// not be a nul-terminated string. On success, store the bytes in Str and
6574/// return true. When TrimAtNul is set, Str will contain only the bytes up
6575/// to but not including the first nul. Return false on failure.
6577 bool TrimAtNul) {
6579 if (!getConstantDataArrayInfo(V, Slice, 8))
6580 return false;
6581
6582 if (Slice.Array == nullptr) {
6583 if (TrimAtNul) {
6584 // Return a nul-terminated string even for an empty Slice. This is
6585 // safe because all existing SimplifyLibcalls callers require string
6586 // arguments and the behavior of the functions they fold is undefined
6587 // otherwise. Folding the calls this way is preferable to making
6588 // the undefined library calls, even though it prevents sanitizers
6589 // from reporting such calls.
6590 Str = StringRef();
6591 return true;
6592 }
6593 if (Slice.Length == 1) {
6594 Str = StringRef("", 1);
6595 return true;
6596 }
6597 // We cannot instantiate a StringRef as we do not have an appropriate string
6598 // of 0s at hand.
6599 return false;
6600 }
6601
6602 // Start out with the entire array in the StringRef.
6603 Str = Slice.Array->getAsString();
6604 // Skip over 'offset' bytes.
6605 Str = Str.substr(Slice.Offset);
6606
6607 if (TrimAtNul) {
6608 // Trim off the \0 and anything after it. If the array is not nul
6609 // terminated, we just return the whole end of string. The client may know
6610 // some other way that the string is length-bound.
6611 Str = Str.substr(0, Str.find('\0'));
6612 }
6613 return true;
6614}
6615
6616// These next two are very similar to the above, but also look through PHI
6617// nodes.
6618// TODO: See if we can integrate these two together.
6619
6620/// If we can compute the length of the string pointed to by
6621/// the specified pointer, return 'len+1'. If we can't, return 0.
6624 unsigned CharSize) {
6625 // Look through noop bitcast instructions.
6626 V = V->stripPointerCasts();
6627
6628 // If this is a PHI node, there are two cases: either we have already seen it
6629 // or we haven't.
6630 if (const PHINode *PN = dyn_cast<PHINode>(V)) {
6631 if (!PHIs.insert(PN).second)
6632 return ~0ULL; // already in the set.
6633
6634 // If it was new, see if all the input strings are the same length.
6635 uint64_t LenSoFar = ~0ULL;
6636 for (Value *IncValue : PN->incoming_values()) {
6637 uint64_t Len = GetStringLengthH(IncValue, PHIs, CharSize);
6638 if (Len == 0) return 0; // Unknown length -> unknown.
6639
6640 if (Len == ~0ULL) continue;
6641
6642 if (Len != LenSoFar && LenSoFar != ~0ULL)
6643 return 0; // Disagree -> unknown.
6644 LenSoFar = Len;
6645 }
6646
6647 // Success, all agree.
6648 return LenSoFar;
6649 }
6650
6651 // strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6652 if (const SelectInst *SI = dyn_cast<SelectInst>(V)) {
6653 uint64_t Len1 = GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6654 if (Len1 == 0) return 0;
6655 uint64_t Len2 = GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6656 if (Len2 == 0) return 0;
6657 if (Len1 == ~0ULL) return Len2;
6658 if (Len2 == ~0ULL) return Len1;
6659 if (Len1 != Len2) return 0;
6660 return Len1;
6661 }
6662
6663 // Otherwise, see if we can read the string.
6665 if (!getConstantDataArrayInfo(V, Slice, CharSize))
6666 return 0;
6667
6668 if (Slice.Array == nullptr)
6669 // Zeroinitializer (including an empty one).
6670 return 1;
6671
6672 // Search for the first nul character. Return a conservative result even
6673 // when there is no nul. This is safe since otherwise the string function
6674 // being folded such as strlen is undefined, and can be preferable to
6675 // making the undefined library call.
6676 unsigned NullIndex = 0;
6677 for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6678 if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6679 break;
6680 }
6681
6682 return NullIndex + 1;
6683}
6684
6685/// If we can compute the length of the string pointed to by
6686/// the specified pointer, return 'len+1'. If we can't, return 0.
6687uint64_t llvm::GetStringLength(const Value *V, unsigned CharSize) {
6688 if (!V->getType()->isPointerTy())
6689 return 0;
6690
6692 uint64_t Len = GetStringLengthH(V, PHIs, CharSize);
6693 // If Len is ~0ULL, we had an infinite phi cycle: this is dead code, so return
6694 // an empty string as a length.
6695 return Len == ~0ULL ? 1 : Len;
6696}
6697
6698const Value *
6700 bool MustPreserveNullness) {
6701 assert(Call &&
6702 "getArgumentAliasingToReturnedPointer only works on nonnull calls");
6703 if (const Value *RV = Call->getReturnedArgOperand())
6704 return RV;
6705 // This can be used only as a aliasing property.
6707 Call, MustPreserveNullness))
6708 return Call->getArgOperand(0);
6709 return nullptr;
6710}
6711
6713 const CallBase *Call, bool MustPreserveNullness) {
6714 switch (Call->getIntrinsicID()) {
6715 case Intrinsic::launder_invariant_group:
6716 case Intrinsic::strip_invariant_group:
6717 case Intrinsic::aarch64_irg:
6718 case Intrinsic::aarch64_tagp:
6719 // The amdgcn_make_buffer_rsrc function does not alter the address of the
6720 // input pointer (and thus preserve null-ness for the purposes of escape
6721 // analysis, which is where the MustPreserveNullness flag comes in to play).
6722 // However, it will not necessarily map ptr addrspace(N) null to ptr
6723 // addrspace(8) null, aka the "null descriptor", which has "all loads return
6724 // 0, all stores are dropped" semantics. Given the context of this intrinsic
6725 // list, no one should be relying on such a strict interpretation of
6726 // MustPreserveNullness (and, at time of writing, they are not), but we
6727 // document this fact out of an abundance of caution.
6728 case Intrinsic::amdgcn_make_buffer_rsrc:
6729 return true;
6730 case Intrinsic::ptrmask:
6731 return !MustPreserveNullness;
6732 case Intrinsic::threadlocal_address:
6733 // The underlying variable changes with thread ID. The Thread ID may change
6734 // at coroutine suspend points.
6735 return !Call->getParent()->getParent()->isPresplitCoroutine();
6736 default:
6737 return false;
6738 }
6739}
6740
6741/// \p PN defines a loop-variant pointer to an object. Check if the
6742/// previous iteration of the loop was referring to the same object as \p PN.
6744 const LoopInfo *LI) {
6745 // Find the loop-defined value.
6746 Loop *L = LI->getLoopFor(PN->getParent());
6747 if (PN->getNumIncomingValues() != 2)
6748 return true;
6749
6750 // Find the value from previous iteration.
6751 auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6752 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6753 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6754 if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6755 return true;
6756
6757 // If a new pointer is loaded in the loop, the pointer references a different
6758 // object in every iteration. E.g.:
6759 // for (i)
6760 // int *p = a[i];
6761 // ...
6762 if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6763 if (!L->isLoopInvariant(Load->getPointerOperand()))
6764 return false;
6765 return true;
6766}
6767
6768const Value *llvm::getUnderlyingObject(const Value *V, unsigned MaxLookup) {
6769 for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6770 if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6771 const Value *PtrOp = GEP->getPointerOperand();
6772 if (!PtrOp->getType()->isPointerTy()) // Only handle scalar pointer base.
6773 return V;
6774 V = PtrOp;
6775 } else if (Operator::getOpcode(V) == Instruction::BitCast ||
6776 Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6777 Value *NewV = cast<Operator>(V)->getOperand(0);
6778 if (!NewV->getType()->isPointerTy())
6779 return V;
6780 V = NewV;
6781 } else if (auto *GA = dyn_cast<GlobalAlias>(V)) {
6782 if (GA->isInterposable())
6783 return V;
6784 V = GA->getAliasee();
6785 } else {
6786 if (auto *PHI = dyn_cast<PHINode>(V)) {
6787 // Look through single-arg phi nodes created by LCSSA.
6788 if (PHI->getNumIncomingValues() == 1) {
6789 V = PHI->getIncomingValue(0);
6790 continue;
6791 }
6792 } else if (auto *Call = dyn_cast<CallBase>(V)) {
6793 // CaptureTracking can know about special capturing properties of some
6794 // intrinsics like launder.invariant.group, that can't be expressed with
6795 // the attributes, but have properties like returning aliasing pointer.
6796 // Because some analysis may assume that nocaptured pointer is not
6797 // returned from some special intrinsic (because function would have to
6798 // be marked with returns attribute), it is crucial to use this function
6799 // because it should be in sync with CaptureTracking. Not using it may
6800 // cause weird miscompilations where 2 aliasing pointers are assumed to
6801 // noalias.
6802 if (auto *RP = getArgumentAliasingToReturnedPointer(Call, false)) {
6803 V = RP;
6804 continue;
6805 }
6806 }
6807
6808 return V;
6809 }
6810 assert(V->getType()->isPointerTy() && "Unexpected operand type!");
6811 }
6812 return V;
6813}
6814
6817 const LoopInfo *LI, unsigned MaxLookup) {
6820 Worklist.push_back(V);
6821 do {
6822 const Value *P = Worklist.pop_back_val();
6823 P = getUnderlyingObject(P, MaxLookup);
6824
6825 if (!Visited.insert(P).second)
6826 continue;
6827
6828 if (auto *SI = dyn_cast<SelectInst>(P)) {
6829 Worklist.push_back(SI->getTrueValue());
6830 Worklist.push_back(SI->getFalseValue());
6831 continue;
6832 }
6833
6834 if (auto *PN = dyn_cast<PHINode>(P)) {
6835 // If this PHI changes the underlying object in every iteration of the
6836 // loop, don't look through it. Consider:
6837 // int **A;
6838 // for (i) {
6839 // Prev = Curr; // Prev = PHI (Prev_0, Curr)
6840 // Curr = A[i];
6841 // *Prev, *Curr;
6842 //
6843 // Prev is tracking Curr one iteration behind so they refer to different
6844 // underlying objects.
6845 if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6847 append_range(Worklist, PN->incoming_values());
6848 else
6849 Objects.push_back(P);
6850 continue;
6851 }
6852
6853 Objects.push_back(P);
6854 } while (!Worklist.empty());
6855}
6856
6858 const unsigned MaxVisited = 8;
6859
6862 Worklist.push_back(V);
6863 const Value *Object = nullptr;
6864 // Used as fallback if we can't find a common underlying object through
6865 // recursion.
6866 bool First = true;
6867 const Value *FirstObject = getUnderlyingObject(V);
6868 do {
6869 const Value *P = Worklist.pop_back_val();
6870 P = First ? FirstObject : getUnderlyingObject(P);
6871 First = false;
6872
6873 if (!Visited.insert(P).second)
6874 continue;
6875
6876 if (Visited.size() == MaxVisited)
6877 return FirstObject;
6878
6879 if (auto *SI = dyn_cast<SelectInst>(P)) {
6880 Worklist.push_back(SI->getTrueValue());
6881 Worklist.push_back(SI->getFalseValue());
6882 continue;
6883 }
6884
6885 if (auto *PN = dyn_cast<PHINode>(P)) {
6886 append_range(Worklist, PN->incoming_values());
6887 continue;
6888 }
6889
6890 if (!Object)
6891 Object = P;
6892 else if (Object != P)
6893 return FirstObject;
6894 } while (!Worklist.empty());
6895
6896 return Object ? Object : FirstObject;
6897}
6898
6899/// This is the function that does the work of looking through basic
6900/// ptrtoint+arithmetic+inttoptr sequences.
6901static const Value *getUnderlyingObjectFromInt(const Value *V) {
6902 do {
6903 if (const Operator *U = dyn_cast<Operator>(V)) {
6904 // If we find a ptrtoint, we can transfer control back to the
6905 // regular getUnderlyingObjectFromInt.
6906 if (U->getOpcode() == Instruction::PtrToInt)
6907 return U->getOperand(0);
6908 // If we find an add of a constant, a multiplied value, or a phi, it's
6909 // likely that the other operand will lead us to the base
6910 // object. We don't have to worry about the case where the
6911 // object address is somehow being computed by the multiply,
6912 // because our callers only care when the result is an
6913 // identifiable object.
6914 if (U->getOpcode() != Instruction::Add ||
6915 (!isa<ConstantInt>(U->getOperand(1)) &&
6916 Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6917 !isa<PHINode>(U->getOperand(1))))
6918 return V;
6919 V = U->getOperand(0);
6920 } else {
6921 return V;
6922 }
6923 assert(V->getType()->isIntegerTy() && "Unexpected operand type!");
6924 } while (true);
6925}
6926
6927/// This is a wrapper around getUnderlyingObjects and adds support for basic
6928/// ptrtoint+arithmetic+inttoptr sequences.
6929/// It returns false if unidentified object is found in getUnderlyingObjects.
6931 SmallVectorImpl<Value *> &Objects) {
6933 SmallVector<const Value *, 4> Working(1, V);
6934 do {
6935 V = Working.pop_back_val();
6936
6938 getUnderlyingObjects(V, Objs);
6939
6940 for (const Value *V : Objs) {
6941 if (!Visited.insert(V).second)
6942 continue;
6943 if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6944 const Value *O =
6945 getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6946 if (O->getType()->isPointerTy()) {
6947 Working.push_back(O);
6948 continue;
6949 }
6950 }
6951 // If getUnderlyingObjects fails to find an identifiable object,
6952 // getUnderlyingObjectsForCodeGen also fails for safety.
6953 if (!isIdentifiedObject(V)) {
6954 Objects.clear();
6955 return false;
6956 }
6957 Objects.push_back(const_cast<Value *>(V));
6958 }
6959 } while (!Working.empty());
6960 return true;
6961}
6962
6964 AllocaInst *Result = nullptr;
6966 SmallVector<Value *, 4> Worklist;
6967
6968 auto AddWork = [&](Value *V) {
6969 if (Visited.insert(V).second)
6970 Worklist.push_back(V);
6971 };
6972
6973 AddWork(V);
6974 do {
6975 V = Worklist.pop_back_val();
6976 assert(Visited.count(V));
6977
6978 if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6979 if (Result && Result != AI)
6980 return nullptr;
6981 Result = AI;
6982 } else if (CastInst *CI = dyn_cast<CastInst>(V)) {
6983 AddWork(CI->getOperand(0));
6984 } else if (PHINode *PN = dyn_cast<PHINode>(V)) {
6985 for (Value *IncValue : PN->incoming_values())
6986 AddWork(IncValue);
6987 } else if (auto *SI = dyn_cast<SelectInst>(V)) {
6988 AddWork(SI->getTrueValue());
6989 AddWork(SI->getFalseValue());
6990 } else if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
6991 if (OffsetZero && !GEP->hasAllZeroIndices())
6992 return nullptr;
6993 AddWork(GEP->getPointerOperand());
6994 } else if (CallBase *CB = dyn_cast<CallBase>(V)) {
6995 Value *Returned = CB->getReturnedArgOperand();
6996 if (Returned)
6997 AddWork(Returned);
6998 else
6999 return nullptr;
7000 } else {
7001 return nullptr;
7002 }
7003 } while (!Worklist.empty());
7004
7005 return Result;
7006}
7007
7009 const Value *V, bool AllowLifetime, bool AllowDroppable) {
7010 for (const User *U : V->users()) {
7011 const IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
7012 if (!II)
7013 return false;
7014
7015 if (AllowLifetime && II->isLifetimeStartOrEnd())
7016 continue;
7017
7018 if (AllowDroppable && II->isDroppable())
7019 continue;
7020
7021 return false;
7022 }
7023 return true;
7024}
7025
7028 V, /* AllowLifetime */ true, /* AllowDroppable */ false);
7029}
7032 V, /* AllowLifetime */ true, /* AllowDroppable */ true);
7033}
7034
7036 if (auto *II = dyn_cast<IntrinsicInst>(I))
7037 return isTriviallyVectorizable(II->getIntrinsicID());
7038 auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7039 return (!Shuffle || Shuffle->isSelect()) &&
7040 !isa<CallBase, BitCastInst, ExtractElementInst>(I);
7041}
7042
7044 const Instruction *CtxI,
7045 AssumptionCache *AC,
7046 const DominatorTree *DT,
7047 const TargetLibraryInfo *TLI,
7048 bool UseVariableInfo) {
7049 return isSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7050 AC, DT, TLI, UseVariableInfo);
7051}
7052
7054 unsigned Opcode, const Instruction *Inst, const Instruction *CtxI,
7055 AssumptionCache *AC, const DominatorTree *DT, const TargetLibraryInfo *TLI,
7056 bool UseVariableInfo) {
7057#ifndef NDEBUG
7058 if (Inst->getOpcode() != Opcode) {
7059 // Check that the operands are actually compatible with the Opcode override.
7060 auto hasEqualReturnAndLeadingOperandTypes =
7061 [](const Instruction *Inst, unsigned NumLeadingOperands) {
7062 if (Inst->getNumOperands() < NumLeadingOperands)
7063 return false;
7064 const Type *ExpectedType = Inst->getType();
7065 for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7066 if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7067 return false;
7068 return true;
7069 };
7071 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7072 assert(!Instruction::isUnaryOp(Opcode) ||
7073 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7074 }
7075#endif
7076
7077 switch (Opcode) {
7078 default:
7079 return true;
7080 case Instruction::UDiv:
7081 case Instruction::URem: {
7082 // x / y is undefined if y == 0.
7083 const APInt *V;
7084 if (match(Inst->getOperand(1), m_APInt(V)))
7085 return *V != 0;
7086 return false;
7087 }
7088 case Instruction::SDiv:
7089 case Instruction::SRem: {
7090 // x / y is undefined if y == 0 or x == INT_MIN and y == -1
7091 const APInt *Numerator, *Denominator;
7092 if (!match(Inst->getOperand(1), m_APInt(Denominator)))
7093 return false;
7094 // We cannot hoist this division if the denominator is 0.
7095 if (*Denominator == 0)
7096 return false;
7097 // It's safe to hoist if the denominator is not 0 or -1.
7098 if (!Denominator->isAllOnes())
7099 return true;
7100 // At this point we know that the denominator is -1. It is safe to hoist as
7101 // long we know that the numerator is not INT_MIN.
7102 if (match(Inst->getOperand(0), m_APInt(Numerator)))
7103 return !Numerator->isMinSignedValue();
7104 // The numerator *might* be MinSignedValue.
7105 return false;
7106 }
7107 case Instruction::Load: {
7108 if (!UseVariableInfo)
7109 return false;
7110
7111 const LoadInst *LI = dyn_cast<LoadInst>(Inst);
7112 if (!LI)
7113 return false;
7114 if (mustSuppressSpeculation(*LI))
7115 return false;
7116 const DataLayout &DL = LI->getDataLayout();
7118 LI->getType(), LI->getAlign(), DL,
7119 CtxI, AC, DT, TLI);
7120 }
7121 case Instruction::Call: {
7122 auto *CI = dyn_cast<const CallInst>(Inst);
7123 if (!CI)
7124 return false;
7125 const Function *Callee = CI->getCalledFunction();
7126
7127 // The called function could have undefined behavior or side-effects, even
7128 // if marked readnone nounwind.
7129 return Callee && Callee->isSpeculatable();
7130 }
7131 case Instruction::VAArg:
7132 case Instruction::Alloca:
7133 case Instruction::Invoke:
7134 case Instruction::CallBr:
7135 case Instruction::PHI:
7136 case Instruction::Store:
7137 case Instruction::Ret:
7138 case Instruction::Br:
7139 case Instruction::IndirectBr:
7140 case Instruction::Switch:
7141 case Instruction::Unreachable:
7142 case Instruction::Fence:
7143 case Instruction::AtomicRMW:
7144 case Instruction::AtomicCmpXchg:
7145 case Instruction::LandingPad:
7146 case Instruction::Resume:
7147 case Instruction::CatchSwitch:
7148 case Instruction::CatchPad:
7149 case Instruction::CatchRet:
7150 case Instruction::CleanupPad:
7151 case Instruction::CleanupRet:
7152 return false; // Misc instructions which have effects
7153 }
7154}
7155
7157 if (I.mayReadOrWriteMemory())
7158 // Memory dependency possible
7159 return true;
7161 // Can't move above a maythrow call or infinite loop. Or if an
7162 // inalloca alloca, above a stacksave call.
7163 return true;
7165 // 1) Can't reorder two inf-loop calls, even if readonly
7166 // 2) Also can't reorder an inf-loop call below a instruction which isn't
7167 // safe to speculative execute. (Inverse of above)
7168 return true;
7169 return false;
7170}
7171
7172/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7174 switch (OR) {
7183 }
7184 llvm_unreachable("Unknown OverflowResult");
7185}
7186
7187/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7190 bool ForSigned,
7191 const SimplifyQuery &SQ) {
7192 ConstantRange CR1 =
7193 ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7194 ConstantRange CR2 = computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7197 return CR1.intersectWith(CR2, RangeType);
7198}
7199
7201 const Value *RHS,
7202 const SimplifyQuery &SQ,
7203 bool IsNSW) {
7204 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
7205 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
7206
7207 // mul nsw of two non-negative numbers is also nuw.
7208 if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
7210
7211 ConstantRange LHSRange = ConstantRange::fromKnownBits(LHSKnown, false);
7212 ConstantRange RHSRange = ConstantRange::fromKnownBits(RHSKnown, false);
7213 return mapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7214}
7215
7217 const Value *RHS,
7218 const SimplifyQuery &SQ) {
7219 // Multiplying n * m significant bits yields a result of n + m significant
7220 // bits. If the total number of significant bits does not exceed the
7221 // result bit width (minus 1), there is no overflow.
7222 // This means if we have enough leading sign bits in the operands
7223 // we can guarantee that the result does not overflow.
7224 // Ref: "Hacker's Delight" by Henry Warren
7225 unsigned BitWidth = LHS->getType()->getScalarSizeInBits();
7226
7227 // Note that underestimating the number of sign bits gives a more
7228 // conservative answer.
7229 unsigned SignBits =
7231
7232 // First handle the easy case: if we have enough sign bits there's
7233 // definitely no overflow.
7234 if (SignBits > BitWidth + 1)
7236
7237 // There are two ambiguous cases where there can be no overflow:
7238 // SignBits == BitWidth + 1 and
7239 // SignBits == BitWidth
7240 // The second case is difficult to check, therefore we only handle the
7241 // first case.
7242 if (SignBits == BitWidth + 1) {
7243 // It overflows only when both arguments are negative and the true
7244 // product is exactly the minimum negative number.
7245 // E.g. mul i16 with 17 sign bits: 0xff00 * 0xff80 = 0x8000
7246 // For simplicity we just check if at least one side is not negative.
7247 KnownBits LHSKnown = computeKnownBits(LHS, /*Depth=*/0, SQ);
7248 KnownBits RHSKnown = computeKnownBits(RHS, /*Depth=*/0, SQ);
7249 if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7251 }
7253}
7254
7257 const WithCache<const Value *> &RHS,
7258 const SimplifyQuery &SQ) {
7259 ConstantRange LHSRange =
7260 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7261 ConstantRange RHSRange =
7262 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7263 return mapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7264}
7265
7266static OverflowResult
7268 const WithCache<const Value *> &RHS,
7269 const AddOperator *Add, const SimplifyQuery &SQ) {
7270 if (Add && Add->hasNoSignedWrap()) {
7272 }
7273
7274 // If LHS and RHS each have at least two sign bits, the addition will look
7275 // like
7276 //
7277 // XX..... +
7278 // YY.....
7279 //
7280 // If the carry into the most significant position is 0, X and Y can't both
7281 // be 1 and therefore the carry out of the addition is also 0.
7282 //
7283 // If the carry into the most significant position is 1, X and Y can't both
7284 // be 0 and therefore the carry out of the addition is also 1.
7285 //
7286 // Since the carry into the most significant position is always equal to
7287 // the carry out of the addition, there is no signed overflow.
7288 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
7289 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
7291
7292 ConstantRange LHSRange =
7293 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7294 ConstantRange RHSRange =
7295 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7296 OverflowResult OR =
7297 mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7299 return OR;
7300
7301 // The remaining code needs Add to be available. Early returns if not so.
7302 if (!Add)
7304
7305 // If the sign of Add is the same as at least one of the operands, this add
7306 // CANNOT overflow. If this can be determined from the known bits of the
7307 // operands the above signedAddMayOverflow() check will have already done so.
7308 // The only other way to improve on the known bits is from an assumption, so
7309 // call computeKnownBitsFromContext() directly.
7310 bool LHSOrRHSKnownNonNegative =
7311 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7312 bool LHSOrRHSKnownNegative =
7313 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7314 if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7315 KnownBits AddKnown(LHSRange.getBitWidth());
7316 computeKnownBitsFromContext(Add, AddKnown, /*Depth=*/0, SQ);
7317 if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7318 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7320 }
7321
7323}
7324
7326 const Value *RHS,
7327 const SimplifyQuery &SQ) {
7328 // X - (X % ?)
7329 // The remainder of a value can't have greater magnitude than itself,
7330 // so the subtraction can't overflow.
7331
7332 // X - (X -nuw ?)
7333 // In the minimal case, this would simplify to "?", so there's no subtract
7334 // at all. But if this analysis is used to peek through casts, for example,
7335 // then determining no-overflow may allow other transforms.
7336
7337 // TODO: There are other patterns like this.
7338 // See simplifyICmpWithBinOpOnLHS() for candidates.
7339 if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
7341 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7343
7345 SQ.DL)) {
7346 if (*C)
7349 }
7350
7351 ConstantRange LHSRange =
7352 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/false, SQ);
7353 ConstantRange RHSRange =
7354 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/false, SQ);
7355 return mapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7356}
7357
7359 const Value *RHS,
7360 const SimplifyQuery &SQ) {
7361 // X - (X % ?)
7362 // The remainder of a value can't have greater magnitude than itself,
7363 // so the subtraction can't overflow.
7364
7365 // X - (X -nsw ?)
7366 // In the minimal case, this would simplify to "?", so there's no subtract
7367 // at all. But if this analysis is used to peek through casts, for example,
7368 // then determining no-overflow may allow other transforms.
7369 if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
7371 if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7373
7374 // If LHS and RHS each have at least two sign bits, the subtraction
7375 // cannot overflow.
7376 if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
7377 ::ComputeNumSignBits(RHS, 0, SQ) > 1)
7379
7380 ConstantRange LHSRange =
7381 computeConstantRangeIncludingKnownBits(LHS, /*ForSigned=*/true, SQ);
7382 ConstantRange RHSRange =
7383 computeConstantRangeIncludingKnownBits(RHS, /*ForSigned=*/true, SQ);
7384 return mapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7385}
7386
7388 const DominatorTree &DT) {
7389 SmallVector<const BranchInst *, 2> GuardingBranches;
7391
7392 for (const User *U : WO->users()) {
7393 if (const auto *EVI = dyn_cast<ExtractValueInst>(U)) {
7394 assert(EVI->getNumIndices() == 1 && "Obvious from CI's type");
7395
7396 if (EVI->getIndices()[0] == 0)
7397 Results.push_back(EVI);
7398 else {
7399 assert(EVI->getIndices()[0] == 1 && "Obvious from CI's type");
7400
7401 for (const auto *U : EVI->users())
7402 if (const auto *B = dyn_cast<BranchInst>(U)) {
7403 assert(B->isConditional() && "How else is it using an i1?");
7404 GuardingBranches.push_back(B);
7405 }
7406 }
7407 } else {
7408 // We are using the aggregate directly in a way we don't want to analyze
7409 // here (storing it to a global, say).
7410 return false;
7411 }
7412 }
7413
7414 auto AllUsesGuardedByBranch = [&](const BranchInst *BI) {
7415 BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7416 if (!NoWrapEdge.isSingleEdge())
7417 return false;
7418
7419 // Check if all users of the add are provably no-wrap.
7420 for (const auto *Result : Results) {
7421 // If the extractvalue itself is not executed on overflow, the we don't
7422 // need to check each use separately, since domination is transitive.
7423 if (DT.dominates(NoWrapEdge, Result->getParent()))
7424 continue;
7425
7426 for (const auto &RU : Result->uses())
7427 if (!DT.dominates(NoWrapEdge, RU))
7428 return false;
7429 }
7430
7431 return true;
7432 };
7433
7434 return llvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7435}
7436
7437/// Shifts return poison if shiftwidth is larger than the bitwidth.
7438static bool shiftAmountKnownInRange(const Value *ShiftAmount) {
7439 auto *C = dyn_cast<Constant>(ShiftAmount);
7440 if (!C)
7441 return false;
7442
7443 // Shifts return poison if shiftwidth is larger than the bitwidth.
7445 if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7446 unsigned NumElts = FVTy->getNumElements();
7447 for (unsigned i = 0; i < NumElts; ++i)
7448 ShiftAmounts.push_back(C->getAggregateElement(i));
7449 } else if (isa<ScalableVectorType>(C->getType()))
7450 return false; // Can't tell, just return false to be safe
7451 else
7452 ShiftAmounts.push_back(C);
7453
7454 bool Safe = llvm::all_of(ShiftAmounts, [](const Constant *C) {
7455 auto *CI = dyn_cast_or_null<ConstantInt>(C);
7456 return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7457 });
7458
7459 return Safe;
7460}
7461
7463 PoisonOnly = (1 << 0),
7464 UndefOnly = (1 << 1),
7466};
7467
7469 return (unsigned(Kind) & unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7470}
7471
7473 return (unsigned(Kind) & unsigned(UndefPoisonKind::UndefOnly)) != 0;
7474}
7475
7477 bool ConsiderFlagsAndMetadata) {
7478
7479 if (ConsiderFlagsAndMetadata && includesPoison(Kind) &&
7480 Op->hasPoisonGeneratingAnnotations())
7481 return true;
7482
7483 unsigned Opcode = Op->getOpcode();
7484
7485 // Check whether opcode is a poison/undef-generating operation
7486 switch (Opcode) {
7487 case Instruction::Shl:
7488 case Instruction::AShr:
7489 case Instruction::LShr:
7490 return includesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7491 case Instruction::FPToSI:
7492 case Instruction::FPToUI:
7493 // fptosi/ui yields poison if the resulting value does not fit in the
7494 // destination type.
7495 return true;
7496 case Instruction::Call:
7497 if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7498 switch (II->getIntrinsicID()) {
7499 // TODO: Add more intrinsics.
7500 case Intrinsic::ctlz:
7501 case Intrinsic::cttz:
7502 case Intrinsic::abs:
7503 if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7504 return false;
7505 break;
7506 case Intrinsic::ctpop:
7507 case Intrinsic::bswap:
7508 case Intrinsic::bitreverse:
7509 case Intrinsic::fshl:
7510 case Intrinsic::fshr:
7511 case Intrinsic::smax:
7512 case Intrinsic::smin:
7513 case Intrinsic::umax:
7514 case Intrinsic::umin:
7515 case Intrinsic::ptrmask:
7516 case Intrinsic::fptoui_sat:
7517 case Intrinsic::fptosi_sat:
7518 case Intrinsic::sadd_with_overflow:
7519 case Intrinsic::ssub_with_overflow:
7520 case Intrinsic::smul_with_overflow:
7521 case Intrinsic::uadd_with_overflow:
7522 case Intrinsic::usub_with_overflow:
7523 case Intrinsic::umul_with_overflow:
7524 case Intrinsic::sadd_sat:
7525 case Intrinsic::uadd_sat:
7526 case Intrinsic::ssub_sat:
7527 case Intrinsic::usub_sat:
7528 return false;
7529 case Intrinsic::sshl_sat:
7530 case Intrinsic::ushl_sat:
7531 return includesPoison(Kind) &&
7532 !shiftAmountKnownInRange(II->getArgOperand(1));
7533 case Intrinsic::fma:
7534 case Intrinsic::fmuladd:
7535 case Intrinsic::sqrt:
7536 case Intrinsic::powi:
7537 case Intrinsic::sin:
7538 case Intrinsic::cos:
7539 case Intrinsic::pow:
7540 case Intrinsic::log:
7541 case Intrinsic::log10:
7542 case Intrinsic::log2:
7543 case Intrinsic::exp:
7544 case Intrinsic::exp2:
7545 case Intrinsic::exp10:
7546 case Intrinsic::fabs:
7547 case Intrinsic::copysign:
7548 case Intrinsic::floor:
7549 case Intrinsic::ceil:
7550 case Intrinsic::trunc:
7551 case Intrinsic::rint:
7552 case Intrinsic::nearbyint:
7553 case Intrinsic::round:
7554 case Intrinsic::roundeven:
7555 case Intrinsic::fptrunc_round:
7556 case Intrinsic::canonicalize:
7557 case Intrinsic::arithmetic_fence:
7558 case Intrinsic::minnum:
7559 case Intrinsic::maxnum:
7560 case Intrinsic::minimum:
7561 case Intrinsic::maximum:
7562 case Intrinsic::is_fpclass:
7563 case Intrinsic::ldexp:
7564 case Intrinsic::frexp:
7565 return false;
7566 case Intrinsic::lround:
7567 case Intrinsic::llround:
7568 case Intrinsic::lrint:
7569 case Intrinsic::llrint:
7570 // If the value doesn't fit an unspecified value is returned (but this
7571 // is not poison).
7572 return false;
7573 }
7574 }
7575 [[fallthrough]];
7576 case Instruction::CallBr:
7577 case Instruction::Invoke: {
7578 const auto *CB = cast<CallBase>(Op);
7579 return !CB->hasRetAttr(Attribute::NoUndef);
7580 }
7581 case Instruction::InsertElement:
7582 case Instruction::ExtractElement: {
7583 // If index exceeds the length of the vector, it returns poison
7584 auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7585 unsigned IdxOp = Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7586 auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7587 if (includesPoison(Kind))
7588 return !Idx ||
7589 Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7590 return false;
7591 }
7592 case Instruction::ShuffleVector: {
7593 ArrayRef<int> Mask = isa<ConstantExpr>(Op)
7594 ? cast<ConstantExpr>(Op)->getShuffleMask()
7595 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7596 return includesPoison(Kind) && is_contained(Mask, PoisonMaskElem);
7597 }
7598 case Instruction::FNeg:
7599 case Instruction::PHI:
7600 case Instruction::Select:
7601 case Instruction::URem:
7602 case Instruction::SRem:
7603 case Instruction::ExtractValue:
7604 case Instruction::InsertValue:
7605 case Instruction::Freeze:
7606 case Instruction::ICmp:
7607 case Instruction::FCmp:
7608 case Instruction::FAdd:
7609 case Instruction::FSub:
7610 case Instruction::FMul:
7611 case Instruction::FDiv:
7612 case Instruction::FRem:
7613 return false;
7614 case Instruction::GetElementPtr:
7615 // inbounds is handled above
7616 // TODO: what about inrange on constexpr?
7617 return false;
7618 default: {
7619 const auto *CE = dyn_cast<ConstantExpr>(Op);
7620 if (isa<CastInst>(Op) || (CE && CE->isCast()))
7621 return false;
7622 else if (Instruction::isBinaryOp(Opcode))
7623 return false;
7624 // Be conservative and return true.
7625 return true;
7626 }
7627 }
7628}
7629
7631 bool ConsiderFlagsAndMetadata) {
7632 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7633 ConsiderFlagsAndMetadata);
7634}
7635
7636bool llvm::canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata) {
7637 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7638 ConsiderFlagsAndMetadata);
7639}
7640
7641static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V,
7642 unsigned Depth) {
7643 if (ValAssumedPoison == V)
7644 return true;
7645
7646 const unsigned MaxDepth = 2;
7647 if (Depth >= MaxDepth)
7648 return false;
7649
7650 if (const auto *I = dyn_cast<Instruction>(V)) {
7651 if (any_of(I->operands(), [=](const Use &Op) {
7652 return propagatesPoison(Op) &&
7653 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7654 }))
7655 return true;
7656
7657 // V = extractvalue V0, idx
7658 // V2 = extractvalue V0, idx2
7659 // V0's elements are all poison or not. (e.g., add_with_overflow)
7660 const WithOverflowInst *II;
7662 (match(ValAssumedPoison, m_ExtractValue(m_Specific(II))) ||
7663 llvm::is_contained(II->args(), ValAssumedPoison)))
7664 return true;
7665 }
7666 return false;
7667}
7668
7669static bool impliesPoison(const Value *ValAssumedPoison, const Value *V,
7670 unsigned Depth) {
7671 if (isGuaranteedNotToBePoison(ValAssumedPoison))
7672 return true;
7673
7674 if (directlyImpliesPoison(ValAssumedPoison, V, /* Depth */ 0))
7675 return true;
7676
7677 const unsigned MaxDepth = 2;
7678 if (Depth >= MaxDepth)
7679 return false;
7680
7681 const auto *I = dyn_cast<Instruction>(ValAssumedPoison);
7682 if (I && !canCreatePoison(cast<Operator>(I))) {
7683 return all_of(I->operands(), [=](const Value *Op) {
7684 return impliesPoison(Op, V, Depth + 1);
7685 });
7686 }
7687 return false;
7688}
7689
7690bool llvm::impliesPoison(const Value *ValAssumedPoison, const Value *V) {
7691 return ::impliesPoison(ValAssumedPoison, V, /* Depth */ 0);
7692}
7693
7694static bool programUndefinedIfUndefOrPoison(const Value *V, bool PoisonOnly);
7695
7697 const Value *V, AssumptionCache *AC, const Instruction *CtxI,
7698 const DominatorTree *DT, unsigned Depth, UndefPoisonKind Kind) {
7700 return false;
7701
7702 if (isa<MetadataAsValue>(V))
7703 return false;
7704
7705 if (const auto *A = dyn_cast<Argument>(V)) {
7706 if (A->hasAttribute(Attribute::NoUndef) ||
7707 A->hasAttribute(Attribute::Dereferenceable) ||
7708 A->hasAttribute(Attribute::DereferenceableOrNull))
7709 return true;
7710 }
7711
7712 if (auto *C = dyn_cast<Constant>(V)) {
7713 if (isa<PoisonValue>(C))
7714 return !includesPoison(Kind);
7715
7716 if (isa<UndefValue>(C))
7717 return !includesUndef(Kind);
7718
7719 if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(V) ||
7720 isa<ConstantPointerNull>(C) || isa<Function>(C))
7721 return true;
7722
7723 if (C->getType()->isVectorTy() && !isa<ConstantExpr>(C)) {
7724 if (includesUndef(Kind) && C->containsUndefElement())
7725 return false;
7726 if (includesPoison(Kind) && C->containsPoisonElement())
7727 return false;
7728 return !C->containsConstantExpression();
7729 }
7730 }
7731
7732 // Strip cast operations from a pointer value.
7733 // Note that stripPointerCastsSameRepresentation can strip off getelementptr
7734 // inbounds with zero offset. To guarantee that the result isn't poison, the
7735 // stripped pointer is checked as it has to be pointing into an allocated
7736 // object or be null `null` to ensure `inbounds` getelement pointers with a
7737 // zero offset could not produce poison.
7738 // It can strip off addrspacecast that do not change bit representation as
7739 // well. We believe that such addrspacecast is equivalent to no-op.
7740 auto *StrippedV = V->stripPointerCastsSameRepresentation();
7741 if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7742 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7743 return true;
7744
7745 auto OpCheck = [&](const Value *V) {
7746 return isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth + 1, Kind);
7747 };
7748
7749 if (auto *Opr = dyn_cast<Operator>(V)) {
7750 // If the value is a freeze instruction, then it can never
7751 // be undef or poison.
7752 if (isa<FreezeInst>(V))
7753 return true;
7754
7755 if (const auto *CB = dyn_cast<CallBase>(V)) {
7756 if (CB->hasRetAttr(Attribute::NoUndef) ||
7757 CB->hasRetAttr(Attribute::Dereferenceable) ||
7758 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7759 return true;
7760 }
7761
7762 if (const auto *PN = dyn_cast<PHINode>(V)) {
7763 unsigned Num = PN->getNumIncomingValues();
7764 bool IsWellDefined = true;
7765 for (unsigned i = 0; i < Num; ++i) {
7766 auto *TI = PN->getIncomingBlock(i)->getTerminator();
7767 if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7768 DT, Depth + 1, Kind)) {
7769 IsWellDefined = false;
7770 break;
7771 }
7772 }
7773 if (IsWellDefined)
7774 return true;
7775 } else if (!::canCreateUndefOrPoison(Opr, Kind,
7776 /*ConsiderFlagsAndMetadata*/ true) &&
7777 all_of(Opr->operands(), OpCheck))
7778 return true;
7779 }
7780
7781 if (auto *I = dyn_cast<LoadInst>(V))
7782 if (I->hasMetadata(LLVMContext::MD_noundef) ||
7783 I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7784 I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7785 return true;
7786
7788 return true;
7789
7790 // CxtI may be null or a cloned instruction.
7791 if (!CtxI || !CtxI->getParent() || !DT)
7792 return false;
7793
7794 auto *DNode = DT->getNode(CtxI->getParent());
7795 if (!DNode)
7796 // Unreachable block
7797 return false;
7798
7799 // If V is used as a branch condition before reaching CtxI, V cannot be
7800 // undef or poison.
7801 // br V, BB1, BB2
7802 // BB1:
7803 // CtxI ; V cannot be undef or poison here
7804 auto *Dominator = DNode->getIDom();
7805 // This check is purely for compile time reasons: we can skip the IDom walk
7806 // if what we are checking for includes undef and the value is not an integer.
7807 if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7808 while (Dominator) {
7809 auto *TI = Dominator->getBlock()->getTerminator();
7810
7811 Value *Cond = nullptr;
7812 if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7813 if (BI->isConditional())
7814 Cond = BI->getCondition();
7815 } else if (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7816 Cond = SI->getCondition();
7817 }
7818
7819 if (Cond) {
7820 if (Cond == V)
7821 return true;
7822 else if (!includesUndef(Kind) && isa<Operator>(Cond)) {
7823 // For poison, we can analyze further
7824 auto *Opr = cast<Operator>(Cond);
7825 if (any_of(Opr->operands(), [V](const Use &U) {
7826 return V == U && propagatesPoison(U);
7827 }))
7828 return true;
7829 }
7830 }
7831
7832 Dominator = Dominator->getIDom();
7833 }
7834
7835 if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
7836 return true;
7837
7838 return false;
7839}
7840
7842 const Instruction *CtxI,
7843 const DominatorTree *DT,
7844 unsigned Depth) {
7845 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7846 UndefPoisonKind::UndefOrPoison);
7847}
7848
7850 const Instruction *CtxI,
7851 const DominatorTree *DT, unsigned Depth) {
7852 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7853 UndefPoisonKind::PoisonOnly);
7854}
7855
7857 const Instruction *CtxI,
7858 const DominatorTree *DT, unsigned Depth) {
7859 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth,
7860 UndefPoisonKind::UndefOnly);
7861}
7862
7863/// Return true if undefined behavior would provably be executed on the path to
7864/// OnPathTo if Root produced a posion result. Note that this doesn't say
7865/// anything about whether OnPathTo is actually executed or whether Root is
7866/// actually poison. This can be used to assess whether a new use of Root can
7867/// be added at a location which is control equivalent with OnPathTo (such as
7868/// immediately before it) without introducing UB which didn't previously
7869/// exist. Note that a false result conveys no information.
7871 Instruction *OnPathTo,
7872 DominatorTree *DT) {
7873 // Basic approach is to assume Root is poison, propagate poison forward
7874 // through all users we can easily track, and then check whether any of those
7875 // users are provable UB and must execute before out exiting block might
7876 // exit.
7877
7878 // The set of all recursive users we've visited (which are assumed to all be
7879 // poison because of said visit)
7880 SmallSet<const Value *, 16> KnownPoison;
7882 Worklist.push_back(Root);
7883 while (!Worklist.empty()) {
7884 const Instruction *I = Worklist.pop_back_val();
7885
7886 // If we know this must trigger UB on a path leading our target.
7887 if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7888 return true;
7889
7890 // If we can't analyze propagation through this instruction, just skip it
7891 // and transitive users. Safe as false is a conservative result.
7892 if (I != Root && !any_of(I->operands(), [&KnownPoison](const Use &U) {
7893 return KnownPoison.contains(U) && propagatesPoison(U);
7894 }))
7895 continue;
7896
7897 if (KnownPoison.insert(I).second)
7898 for (const User *User : I->users())
7899 Worklist.push_back(cast<Instruction>(User));
7900 }
7901
7902 // Might be non-UB, or might have a path we couldn't prove must execute on
7903 // way to exiting bb.
7904 return false;
7905}
7906
7908 const SimplifyQuery &SQ) {
7909 return ::computeOverflowForSignedAdd(Add->getOperand(0), Add->getOperand(1),
7910 Add, SQ);
7911}
7912
7915 const WithCache<const Value *> &RHS,
7916 const SimplifyQuery &SQ) {
7917 return ::computeOverflowForSignedAdd(LHS, RHS, nullptr, SQ);
7918}
7919
7921 // Note: An atomic operation isn't guaranteed to return in a reasonable amount
7922 // of time because it's possible for another thread to interfere with it for an
7923 // arbitrary length of time, but programs aren't allowed to rely on that.
7924
7925 // If there is no successor, then execution can't transfer to it.
7926 if (isa<ReturnInst>(I))
7927 return false;
7928 if (isa<UnreachableInst>(I))
7929 return false;
7930
7931 // Note: Do not add new checks here; instead, change Instruction::mayThrow or
7932 // Instruction::willReturn.
7933 //
7934 // FIXME: Move this check into Instruction::willReturn.
7935 if (isa<CatchPadInst>(I)) {
7936 switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7937 default:
7938 // A catchpad may invoke exception object constructors and such, which
7939 // in some languages can be arbitrary code, so be conservative by default.
7940 return false;
7942 // For CoreCLR, it just involves a type test.
7943 return true;
7944 }
7945 }
7946
7947 // An instruction that returns without throwing must transfer control flow
7948 // to a successor.
7949 return !I->mayThrow() && I->willReturn();
7950}
7951
7953 // TODO: This is slightly conservative for invoke instruction since exiting
7954 // via an exception *is* normal control for them.
7955 for (const Instruction &I : *BB)
7957 return false;
7958 return true;
7959}
7960
7963 unsigned ScanLimit) {
7965 ScanLimit);
7966}
7967
7970 assert(ScanLimit && "scan limit must be non-zero");
7971 for (const Instruction &I : Range) {
7972 if (isa<DbgInfoIntrinsic>(I))
7973 continue;
7974 if (--ScanLimit == 0)
7975 return false;
7977 return false;
7978 }
7979 return true;
7980}
7981
7983 const Loop *L) {
7984 // The loop header is guaranteed to be executed for every iteration.
7985 //
7986 // FIXME: Relax this constraint to cover all basic blocks that are
7987 // guaranteed to be executed at every iteration.
7988 if (I->getParent() != L->getHeader()) return false;
7989
7990 for (const Instruction &LI : *L->getHeader()) {
7991 if (&LI == I) return true;
7992 if (!isGuaranteedToTransferExecutionToSuccessor(&LI)) return false;
7993 }
7994 llvm_unreachable("Instruction not contained in its own parent basic block.");
7995}
7996
7997bool llvm::propagatesPoison(const Use &PoisonOp) {
7998 const Operator *I = cast<Operator>(PoisonOp.getUser());
7999 switch (I->getOpcode()) {
8000 case Instruction::Freeze:
8001 case Instruction::PHI:
8002 case Instruction::Invoke:
8003 return false;
8004 case Instruction::Select:
8005 return PoisonOp.getOperandNo() == 0;
8006 case Instruction::Call:
8007 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
8008 switch (II->getIntrinsicID()) {
8009 // TODO: Add more intrinsics.
8010 case Intrinsic::sadd_with_overflow:
8011 case Intrinsic::ssub_with_overflow:
8012 case Intrinsic::smul_with_overflow:
8013 case Intrinsic::uadd_with_overflow:
8014 case Intrinsic::usub_with_overflow:
8015 case Intrinsic::umul_with_overflow:
8016 // If an input is a vector containing a poison element, the
8017 // two output vectors (calculated results, overflow bits)'
8018 // corresponding lanes are poison.
8019 return true;
8020 case Intrinsic::ctpop:
8021 case Intrinsic::ctlz:
8022 case Intrinsic::cttz:
8023 case Intrinsic::abs:
8024 case Intrinsic::smax:
8025 case Intrinsic::smin:
8026 case Intrinsic::umax:
8027 case Intrinsic::umin:
8028 case Intrinsic::bitreverse:
8029 case Intrinsic::bswap:
8030 case Intrinsic::sadd_sat:
8031 case Intrinsic::ssub_sat:
8032 case Intrinsic::sshl_sat:
8033 case Intrinsic::uadd_sat:
8034 case Intrinsic::usub_sat:
8035 case Intrinsic::ushl_sat:
8036 return true;
8037 }
8038 }
8039 return false;
8040 case Instruction::ICmp:
8041 case Instruction::FCmp:
8042 case Instruction::GetElementPtr:
8043 return true;
8044 default:
8045 if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I))
8046 return true;
8047
8048 // Be conservative and return false.
8049 return false;
8050 }
8051}
8052
8053/// Enumerates all operands of \p I that are guaranteed to not be undef or
8054/// poison. If the callback \p Handle returns true, stop processing and return
8055/// true. Otherwise, return false.
8056template <typename CallableT>
8058 const CallableT &Handle) {
8059 switch (I->getOpcode()) {
8060 case Instruction::Store:
8061 if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8062 return true;
8063 break;
8064
8065 case Instruction::Load:
8066 if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8067 return true;
8068 break;
8069
8070 // Since dereferenceable attribute imply noundef, atomic operations
8071 // also implicitly have noundef pointers too
8072 case Instruction::AtomicCmpXchg:
8073 if (Handle(cast<AtomicCmpXchgInst>(I)->getPointerOperand()))
8074 return true;
8075 break;
8076
8077 case Instruction::AtomicRMW:
8078 if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8079 return true;
8080 break;
8081
8082 case Instruction::Call:
8083 case Instruction::Invoke: {
8084 const CallBase *CB = cast<CallBase>(I);
8085 if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8086 return true;
8087 for (unsigned i = 0; i < CB->arg_size(); ++i)
8088 if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8089 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8090 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8091 Handle(CB->getArgOperand(i)))
8092 return true;
8093 break;
8094 }
8095 case Instruction::Ret:
8096 if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8097 Handle(I->getOperand(0)))
8098 return true;
8099 break;
8100 case Instruction::Switch:
8101 if (Handle(cast<SwitchInst>(I)->getCondition()))
8102 return true;
8103 break;
8104 case Instruction::Br: {
8105 auto *BR = cast<BranchInst>(I);
8106 if (BR->isConditional() && Handle(BR->getCondition()))
8107 return true;
8108 break;
8109 }
8110 default:
8111 break;
8112 }
8113
8114 return false;
8115}
8116
8119 handleGuaranteedWellDefinedOps(I, [&](const Value *V) {
8120 Operands.push_back(V);
8121 return false;
8122 });
8123}
8124
8125/// Enumerates all operands of \p I that are guaranteed to not be poison.
8126template <typename CallableT>
8128 const CallableT &Handle) {
8129 if (handleGuaranteedWellDefinedOps(I, Handle))
8130 return true;
8131 switch (I->getOpcode()) {
8132 // Divisors of these operations are allowed to be partially undef.
8133 case Instruction::UDiv:
8134 case Instruction::SDiv:
8135 case Instruction::URem:
8136 case Instruction::SRem:
8137 return Handle(I->getOperand(1));
8138 default:
8139 return false;
8140 }
8141}
8142
8145 handleGuaranteedNonPoisonOps(I, [&](const Value *V) {
8146 Operands.push_back(V);
8147 return false;
8148 });
8149}
8150
8152 const SmallPtrSetImpl<const Value *> &KnownPoison) {
8154 I, [&](const Value *V) { return KnownPoison.count(V); });
8155}
8156
8158 bool PoisonOnly) {
8159 // We currently only look for uses of values within the same basic
8160 // block, as that makes it easier to guarantee that the uses will be
8161 // executed given that Inst is executed.
8162 //
8163 // FIXME: Expand this to consider uses beyond the same basic block. To do
8164 // this, look out for the distinction between post-dominance and strong
8165 // post-dominance.
8166 const BasicBlock *BB = nullptr;
8168 if (const auto *Inst = dyn_cast<Instruction>(V)) {
8169 BB = Inst->getParent();
8170 Begin = Inst->getIterator();
8171 Begin++;
8172 } else if (const auto *Arg = dyn_cast<Argument>(V)) {
8173 if (Arg->getParent()->isDeclaration())
8174 return false;
8175 BB = &Arg->getParent()->getEntryBlock();
8176 Begin = BB->begin();
8177 } else {
8178 return false;
8179 }
8180
8181 // Limit number of instructions we look at, to avoid scanning through large
8182 // blocks. The current limit is chosen arbitrarily.
8183 unsigned ScanLimit = 32;
8185
8186 if (!PoisonOnly) {
8187 // Since undef does not propagate eagerly, be conservative & just check
8188 // whether a value is directly passed to an instruction that must take
8189 // well-defined operands.
8190
8191 for (const auto &I : make_range(Begin, End)) {
8192 if (isa<DbgInfoIntrinsic>(I))
8193 continue;
8194 if (--ScanLimit == 0)
8195 break;
8196
8197 if (handleGuaranteedWellDefinedOps(&I, [V](const Value *WellDefinedOp) {
8198 return WellDefinedOp == V;
8199 }))
8200 return true;
8201
8203 break;
8204 }
8205 return false;
8206 }
8207
8208 // Set of instructions that we have proved will yield poison if Inst
8209 // does.
8210 SmallSet<const Value *, 16> YieldsPoison;
8212
8213 YieldsPoison.insert(V);
8214 Visited.insert(BB);
8215
8216 while (true) {
8217 for (const auto &I : make_range(Begin, End)) {
8218 if (isa<DbgInfoIntrinsic>(I))
8219 continue;
8220 if (--ScanLimit == 0)
8221 return false;
8222 if (mustTriggerUB(&I, YieldsPoison))
8223 return true;
8225 return false;
8226
8227 // If an operand is poison and propagates it, mark I as yielding poison.
8228 for (const Use &Op : I.operands()) {
8229 if (YieldsPoison.count(Op) && propagatesPoison(Op)) {
8230 YieldsPoison.insert(&I);
8231 break;
8232 }
8233 }
8234
8235 // Special handling for select, which returns poison if its operand 0 is
8236 // poison (handled in the loop above) *or* if both its true/false operands
8237 // are poison (handled here).
8238 if (I.getOpcode() == Instruction::Select &&
8239 YieldsPoison.count(I.getOperand(1)) &&
8240 YieldsPoison.count(I.getOperand(2))) {
8241 YieldsPoison.insert(&I);
8242 }
8243 }
8244
8245 BB = BB->getSingleSuccessor();
8246 if (!BB || !Visited.insert(BB).second)
8247 break;
8248
8249 Begin = BB->getFirstNonPHI()->getIterator();
8250 End = BB->end();
8251 }
8252 return false;
8253}
8254
8256 return ::programUndefinedIfUndefOrPoison(Inst, false);
8257}
8258
8260 return ::programUndefinedIfUndefOrPoison(Inst, true);
8261}
8262
8263static bool isKnownNonNaN(const Value *V, FastMathFlags FMF) {
8264 if (FMF.noNaNs())
8265 return true;
8266
8267 if (auto *C = dyn_cast<ConstantFP>(V))
8268 return !C->isNaN();
8269
8270 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8271 if (!C->getElementType()->isFloatingPointTy())
8272 return false;
8273 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8274 if (C->getElementAsAPFloat(I).isNaN())
8275 return false;
8276 }
8277 return true;
8278 }
8279
8280 if (isa<ConstantAggregateZero>(V))
8281 return true;
8282
8283 return false;
8284}
8285
8286static bool isKnownNonZero(const Value *V) {
8287 if (auto *C = dyn_cast<ConstantFP>(V))
8288 return !C->isZero();
8289
8290 if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8291 if (!C->getElementType()->isFloatingPointTy())
8292 return false;
8293 for (unsigned I = 0, E = C->getNumElements(); I < E; ++I) {
8294 if (C->getElementAsAPFloat(I).isZero())
8295 return false;
8296 }
8297 return true;
8298 }
8299
8300 return false;
8301}
8302
8303/// Match clamp pattern for float types without care about NaNs or signed zeros.
8304/// Given non-min/max outer cmp/select from the clamp pattern this
8305/// function recognizes if it can be substitued by a "canonical" min/max
8306/// pattern.
8308 Value *CmpLHS, Value *CmpRHS,
8309 Value *TrueVal, Value *FalseVal,
8310 Value *&LHS, Value *&RHS) {
8311 // Try to match
8312 // X < C1 ? C1 : Min(X, C2) --> Max(C1, Min(X, C2))
8313 // X > C1 ? C1 : Max(X, C2) --> Min(C1, Max(X, C2))
8314 // and return description of the outer Max/Min.
8315
8316 // First, check if select has inverse order:
8317 if (CmpRHS == FalseVal) {
8318 std::swap(TrueVal, FalseVal);
8319 Pred = CmpInst::getInversePredicate(Pred);
8320 }
8321
8322 // Assume success now. If there's no match, callers should not use these anyway.
8323 LHS = TrueVal;
8324 RHS = FalseVal;
8325
8326 const APFloat *FC1;
8327 if (CmpRHS != TrueVal || !match(CmpRHS, m_APFloat(FC1)) || !FC1->isFinite())
8328 return {SPF_UNKNOWN, SPNB_NA, false};
8329
8330 const APFloat *FC2;
8331 switch (Pred) {
8332 case CmpInst::FCMP_OLT:
8333 case CmpInst::FCMP_OLE:
8334 case CmpInst::FCMP_ULT:
8335 case CmpInst::FCMP_ULE:
8336 if (match(FalseVal, m_OrdOrUnordFMin(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8337 *FC1 < *FC2)
8338 return {SPF_FMAXNUM, SPNB_RETURNS_ANY, false};
8339 break;
8340 case CmpInst::FCMP_OGT:
8341 case CmpInst::FCMP_OGE:
8342 case CmpInst::FCMP_UGT:
8343 case CmpInst::FCMP_UGE:
8344 if (match(FalseVal, m_OrdOrUnordFMax(m_Specific(CmpLHS), m_APFloat(FC2))) &&
8345 *FC1 > *FC2)
8346 return {SPF_FMINNUM, SPNB_RETURNS_ANY, false};
8347 break;
8348 default:
8349 break;
8350 }
8351
8352 return {SPF_UNKNOWN, SPNB_NA, false};
8353}
8354
8355/// Recognize variations of:
8356/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8358 Value *CmpLHS, Value *CmpRHS,
8359 Value *TrueVal, Value *FalseVal) {
8360 // Swap the select operands and predicate to match the patterns below.
8361 if (CmpRHS != TrueVal) {
8362 Pred = ICmpInst::getSwappedPredicate(Pred);
8363 std::swap(TrueVal, FalseVal);
8364 }
8365 const APInt *C1;
8366 if (CmpRHS == TrueVal && match(CmpRHS, m_APInt(C1))) {
8367 const APInt *C2;
8368 // (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8369 if (match(FalseVal, m_SMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8370 C1->slt(*C2) && Pred == CmpInst::ICMP_SLT)
8371 return {SPF_SMAX, SPNB_NA, false};
8372
8373 // (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8374 if (match(FalseVal, m_SMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8375 C1->sgt(*C2) && Pred == CmpInst::ICMP_SGT)
8376 return {SPF_SMIN, SPNB_NA, false};
8377
8378 // (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8379 if (match(FalseVal, m_UMin(m_Specific(CmpLHS), m_APInt(C2))) &&
8380 C1->ult(*C2) && Pred == CmpInst::ICMP_ULT)
8381 return {SPF_UMAX, SPNB_NA, false};
8382
8383 // (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8384 if (match(FalseVal, m_UMax(m_Specific(CmpLHS), m_APInt(C2))) &&
8385 C1->ugt(*C2) && Pred == CmpInst::ICMP_UGT)
8386 return {SPF_UMIN, SPNB_NA, false};
8387 }
8388 return {SPF_UNKNOWN, SPNB_NA, false};
8389}
8390
8391/// Recognize variations of:
8392/// a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
8394 Value *CmpLHS, Value *CmpRHS,
8395 Value *TVal, Value *FVal,
8396 unsigned Depth) {
8397 // TODO: Allow FP min/max with nnan/nsz.
8398 assert(CmpInst::isIntPredicate(Pred) && "Expected integer comparison");
8399
8400 Value *A = nullptr, *B = nullptr;
8401 SelectPatternResult L = matchSelectPattern(TVal, A, B, nullptr, Depth + 1);
8402 if (!SelectPatternResult::isMinOrMax(L.Flavor))
8403 return {SPF_UNKNOWN, SPNB_NA, false};
8404
8405 Value *C = nullptr, *D = nullptr;
8406 SelectPatternResult R = matchSelectPattern(FVal, C, D, nullptr, Depth + 1);
8407 if (L.Flavor != R.Flavor)
8408 return {SPF_UNKNOWN, SPNB_NA, false};
8409
8410 // We have something like: x Pred y ? min(a, b) : min(c, d).
8411 // Try to match the compare to the min/max operations of the select operands.
8412 // First, make sure we have the right compare predicate.
8413 switch (L.Flavor) {
8414 case SPF_SMIN:
8415 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE) {
8416 Pred = ICmpInst::getSwappedPredicate(Pred);
8417 std::swap(CmpLHS, CmpRHS);
8418 }
8419 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE)
8420 break;
8421 return {SPF_UNKNOWN, SPNB_NA, false};
8422 case SPF_SMAX:
8423 if (Pred == ICmpInst::ICMP_SLT || Pred == ICmpInst::ICMP_SLE) {
8424 Pred = ICmpInst::getSwappedPredicate(Pred);
8425 std::swap(CmpLHS, CmpRHS);
8426 }
8427 if (Pred == ICmpInst::ICMP_SGT || Pred == ICmpInst::ICMP_SGE)
8428 break;
8429 return {SPF_UNKNOWN, SPNB_NA, false};
8430 case SPF_UMIN:
8431 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE) {
8432 Pred = ICmpInst::getSwappedPredicate(Pred);
8433 std::swap(CmpLHS, CmpRHS);
8434 }
8435 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE)
8436 break;
8437 return {SPF_UNKNOWN, SPNB_NA, false};
8438 case SPF_UMAX:
8439 if (Pred == ICmpInst::ICMP_ULT || Pred == ICmpInst::ICMP_ULE) {
8440 Pred = ICmpInst::getSwappedPredicate(Pred);
8441 std::swap(CmpLHS, CmpRHS);
8442 }
8443 if (Pred == ICmpInst::ICMP_UGT || Pred == ICmpInst::ICMP_UGE)
8444 break;
8445 return {SPF_UNKNOWN, SPNB_NA, false};
8446 default:
8447 return {SPF_UNKNOWN, SPNB_NA, false};
8448 }
8449
8450 // If there is a common operand in the already matched min/max and the other
8451 // min/max operands match the compare operands (either directly or inverted),
8452 // then this is min/max of the same flavor.
8453
8454 // a pred c ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8455 // ~c pred ~a ? m(a, b) : m(c, b) --> m(m(a, b), m(c, b))
8456 if (D == B) {
8457 if ((CmpLHS == A && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8458 match(A, m_Not(m_Specific(CmpRHS)))))
8459 return {L.Flavor, SPNB_NA, false};
8460 }
8461 // a pred d ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8462 // ~d pred ~a ? m(a, b) : m(b, d) --> m(m(a, b), m(b, d))
8463 if (C == B) {
8464 if ((CmpLHS == A && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8465 match(A, m_Not(m_Specific(CmpRHS)))))
8466 return {L.Flavor, SPNB_NA, false};
8467 }
8468 // b pred c ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8469 // ~c pred ~b ? m(a, b) : m(c, a) --> m(m(a, b), m(c, a))
8470 if (D == A) {
8471 if ((CmpLHS == B && CmpRHS == C) || (match(C, m_Not(m_Specific(CmpLHS))) &&
8472 match(B, m_Not(m_Specific(CmpRHS)))))
8473 return {L.Flavor, SPNB_NA, false};
8474 }
8475 // b pred d ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8476 // ~d pred ~b ? m(a, b) : m(a, d) --> m(m(a, b), m(a, d))
8477 if (C == A) {
8478 if ((CmpLHS == B && CmpRHS == D) || (match(D, m_Not(m_Specific(CmpLHS))) &&
8479 match(B, m_Not(m_Specific(CmpRHS)))))
8480 return {L.Flavor, SPNB_NA, false};
8481 }
8482
8483 return {SPF_UNKNOWN, SPNB_NA, false};
8484}
8485
8486/// If the input value is the result of a 'not' op, constant integer, or vector
8487/// splat of a constant integer, return the bitwise-not source value.
8488/// TODO: This could be extended to handle non-splat vector integer constants.
8490 Value *NotV;
8491 if (match(V, m_Not(m_Value(NotV))))
8492 return NotV;
8493
8494 const APInt *C;
8495 if (match(V, m_APInt(C)))
8496 return ConstantInt::get(V->getType(), ~(*C));
8497
8498 return nullptr;
8499}
8500
8501/// Match non-obvious integer minimum and maximum sequences.
8503 Value *CmpLHS, Value *CmpRHS,
8504 Value *TrueVal, Value *FalseVal,
8505 Value *&LHS, Value *&RHS,
8506 unsigned Depth) {
8507 // Assume success. If there's no match, callers should not use these anyway.
8508 LHS = TrueVal;
8509 RHS = FalseVal;
8510
8511 SelectPatternResult SPR = matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8513 return SPR;
8514
8515 SPR = matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, Depth);
8517 return SPR;
8518
8519 // Look through 'not' ops to find disguised min/max.
8520 // (X > Y) ? ~X : ~Y ==> (~X < ~Y) ? ~X : ~Y ==> MIN(~X, ~Y)
8521 // (X < Y) ? ~X : ~Y ==> (~X > ~Y) ? ~X : ~Y ==> MAX(~X, ~Y)
8522 if (CmpLHS == getNotValue(TrueVal) && CmpRHS == getNotValue(FalseVal)) {
8523 switch (Pred) {
8524 case CmpInst::ICMP_SGT: return {SPF_SMIN, SPNB_NA, false};
8525 case CmpInst::ICMP_SLT: return {SPF_SMAX, SPNB_NA, false};
8526 case CmpInst::ICMP_UGT: return {SPF_UMIN, SPNB_NA, false};
8527 case CmpInst::ICMP_ULT: return {SPF_UMAX, SPNB_NA, false};
8528 default: break;
8529 }
8530 }
8531
8532 // (X > Y) ? ~Y : ~X ==> (~X < ~Y) ? ~Y : ~X ==> MAX(~Y, ~X)
8533 // (X < Y) ? ~Y : ~X ==> (~X > ~Y) ? ~Y : ~X ==> MIN(~Y, ~X)
8534 if (CmpLHS == getNotValue(FalseVal) && CmpRHS == getNotValue(TrueVal)) {
8535 switch (Pred) {
8536 case CmpInst::ICMP_SGT: return {SPF_SMAX, SPNB_NA, false};
8537 case CmpInst::ICMP_SLT: return {SPF_SMIN, SPNB_NA, false};
8538 case CmpInst::ICMP_UGT: return {SPF_UMAX, SPNB_NA, false};
8539 case CmpInst::ICMP_ULT: return {SPF_UMIN, SPNB_NA, false};
8540 default: break;
8541 }
8542 }
8543
8544 if (Pred != CmpInst::ICMP_SGT && Pred != CmpInst::ICMP_SLT)
8545 return {SPF_UNKNOWN, SPNB_NA, false};
8546
8547 const APInt *C1;
8548 if (!match(CmpRHS, m_APInt(C1)))
8549 return {SPF_UNKNOWN, SPNB_NA, false};
8550
8551 // An unsigned min/max can be written with a signed compare.
8552 const APInt *C2;
8553 if ((CmpLHS == TrueVal && match(FalseVal, m_APInt(C2))) ||
8554 (CmpLHS == FalseVal && match(TrueVal, m_APInt(C2)))) {
8555 // Is the sign bit set?
8556 // (X <s 0) ? X : MAXVAL ==> (X >u MAXVAL) ? X : MAXVAL ==> UMAX
8557 // (X <s 0) ? MAXVAL : X ==> (X >u MAXVAL) ? MAXVAL : X ==> UMIN
8558 if (Pred == CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8559 return {CmpLHS == TrueVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8560
8561 // Is the sign bit clear?
8562 // (X >s -1) ? MINVAL : X ==> (X <u MINVAL) ? MINVAL : X ==> UMAX
8563 // (X >s -1) ? X : MINVAL ==> (X <u MINVAL) ? X : MINVAL ==> UMIN
8564 if (Pred == CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8565 return {CmpLHS == FalseVal ? SPF_UMAX : SPF_UMIN, SPNB_NA, false};
8566 }
8567
8568 return {SPF_UNKNOWN, SPNB_NA, false};
8569}
8570
8571bool llvm::isKnownNegation(const Value *X, const Value *Y, bool NeedNSW,
8572 bool AllowPoison) {
8573 assert(X && Y && "Invalid operand");
8574
8575 auto IsNegationOf = [&](const Value *X, const Value *Y) {
8576 if (!match(X, m_Neg(m_Specific(Y))))
8577 return false;
8578
8579 auto *BO = cast<BinaryOperator>(X);
8580 if (NeedNSW && !BO->hasNoSignedWrap())
8581 return false;
8582
8583 auto *Zero = cast<Constant>(BO->getOperand(0));
8584 if (!AllowPoison && !Zero->isNullValue())
8585 return false;
8586
8587 return true;
8588 };
8589
8590 // X = -Y or Y = -X
8591 if (IsNegationOf(X, Y) || IsNegationOf(Y, X))
8592 return true;
8593
8594 // X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8595 Value *A, *B;
8596 return (!NeedNSW && (match(X, m_Sub(m_Value(A), m_Value(B))) &&
8597 match(Y, m_Sub(m_Specific(B), m_Specific(A))))) ||
8598 (NeedNSW && (match(X, m_NSWSub(m_Value(A), m_Value(B))) &&
8600}
8601
8602bool llvm::isKnownInversion(const Value *X, const Value *Y) {
8603 // Handle X = icmp pred A, B, Y = icmp pred A, C.
8604 Value *A, *B, *C;
8605 CmpPredicate Pred1, Pred2;
8606 if (!match(X, m_ICmp(Pred1, m_Value(A), m_Value(B))) ||
8607 !match(Y, m_c_ICmp(Pred2, m_Specific(A), m_Value(C))))
8608 return false;
8609
8610 // They must both have samesign flag or not.
8611 if (cast<ICmpInst>(X)->hasSameSign() != cast<ICmpInst>(Y)->hasSameSign())
8612 return false;
8613
8614 if (B == C)
8615 return Pred1 == ICmpInst::getInversePredicate(Pred2);
8616
8617 // Try to infer the relationship from constant ranges.
8618 const APInt *RHSC1, *RHSC2;
8619 if (!match(B, m_APInt(RHSC1)) || !match(C, m_APInt(RHSC2)))
8620 return false;
8621
8622 // Sign bits of two RHSCs should match.
8623 if (cast<ICmpInst>(X)->hasSameSign() &&
8624 RHSC1->isNonNegative() != RHSC2->isNonNegative())
8625 return false;
8626
8627 const auto CR1 = ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8628 const auto CR2 = ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8629
8630 return CR1.inverse() == CR2;
8631}
8632
8634 SelectPatternNaNBehavior NaNBehavior,
8635 bool Ordered) {
8636 switch (Pred) {
8637 default:
8638 return {SPF_UNKNOWN, SPNB_NA, false}; // Equality.
8639 case ICmpInst::ICMP_UGT:
8640 case ICmpInst::ICMP_UGE:
8641 return {SPF_UMAX, SPNB_NA, false};
8642 case ICmpInst::ICMP_SGT:
8643 case ICmpInst::ICMP_SGE:
8644 return {SPF_SMAX, SPNB_NA, false};
8645 case ICmpInst::ICMP_ULT:
8646 case ICmpInst::ICMP_ULE:
8647 return {SPF_UMIN, SPNB_NA, false};
8648 case ICmpInst::ICMP_SLT:
8649 case ICmpInst::ICMP_SLE:
8650 return {SPF_SMIN, SPNB_NA, false};
8651 case FCmpInst::FCMP_UGT:
8652 case FCmpInst::FCMP_UGE:
8653 case FCmpInst::FCMP_OGT:
8654 case FCmpInst::FCMP_OGE:
8655 return {SPF_FMAXNUM, NaNBehavior, Ordered};
8656 case FCmpInst::FCMP_ULT:
8657 case FCmpInst::FCMP_ULE:
8658 case FCmpInst::FCMP_OLT:
8659 case FCmpInst::FCMP_OLE:
8660 return {SPF_FMINNUM, NaNBehavior, Ordered};
8661 }
8662}
8663
8664std::optional<std::pair<CmpPredicate, Constant *>>
8667 "Only for relational integer predicates.");
8668 if (isa<UndefValue>(C))
8669 return std::nullopt;
8670
8671 Type *Type = C->getType();
8672 bool IsSigned = ICmpInst::isSigned(Pred);
8673
8675 bool WillIncrement =
8676 UnsignedPred == ICmpInst::ICMP_ULE || UnsignedPred == ICmpInst::ICMP_UGT;
8677
8678 // Check if the constant operand can be safely incremented/decremented
8679 // without overflowing/underflowing.
8680 auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8681 return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8682 };
8683
8684 Constant *SafeReplacementConstant = nullptr;
8685 if (auto *CI = dyn_cast<ConstantInt>(C)) {
8686 // Bail out if the constant can't be safely incremented/decremented.
8687 if (!ConstantIsOk(CI))
8688 return std::nullopt;
8689 } else if (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8690 unsigned NumElts = FVTy->getNumElements();
8691 for (unsigned i = 0; i != NumElts; ++i) {
8692 Constant *Elt = C->getAggregateElement(i);
8693 if (!Elt)
8694 return std::nullopt;
8695
8696 if (isa<UndefValue>(Elt))
8697 continue;
8698
8699 // Bail out if we can't determine if this constant is min/max or if we
8700 // know that this constant is min/max.
8701 auto *CI = dyn_cast<ConstantInt>(Elt);
8702 if (!CI || !ConstantIsOk(CI))
8703 return std::nullopt;
8704
8705 if (!SafeReplacementConstant)
8706 SafeReplacementConstant = CI;
8707 }
8708 } else if (isa<VectorType>(C->getType())) {
8709 // Handle scalable splat
8710 Value *SplatC = C->getSplatValue();
8711 auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8712 // Bail out if the constant can't be safely incremented/decremented.
8713 if (!CI || !ConstantIsOk(CI))
8714 return std::nullopt;
8715 } else {
8716 // ConstantExpr?
8717 return std::nullopt;
8718 }
8719
8720 // It may not be safe to change a compare predicate in the presence of
8721 // undefined elements, so replace those elements with the first safe constant
8722 // that we found.
8723 // TODO: in case of poison, it is safe; let's replace undefs only.
8724 if (C->containsUndefOrPoisonElement()) {
8725 assert(SafeReplacementConstant && "Replacement constant not set");
8726 C = Constant::replaceUndefsWith(C, SafeReplacementConstant);
8727 }
8728
8730
8731 // Increment or decrement the constant.
8732 Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1, true);
8733 Constant *NewC = ConstantExpr::getAdd(C, OneOrNegOne);
8734
8735 return std::make_pair(NewPred, NewC);
8736}
8737
8739 FastMathFlags FMF,
8740 Value *CmpLHS, Value *CmpRHS,
8741 Value *TrueVal, Value *FalseVal,
8742 Value *&LHS, Value *&RHS,
8743 unsigned Depth) {
8744 bool HasMismatchedZeros = false;
8745 if (CmpInst::isFPPredicate(Pred)) {
8746 // IEEE-754 ignores the sign of 0.0 in comparisons. So if the select has one
8747 // 0.0 operand, set the compare's 0.0 operands to that same value for the
8748 // purpose of identifying min/max. Disregard vector constants with undefined
8749 // elements because those can not be back-propagated for analysis.
8750 Value *OutputZeroVal = nullptr;
8751 if (match(TrueVal, m_AnyZeroFP()) && !match(FalseVal, m_AnyZeroFP()) &&
8752 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8753 OutputZeroVal = TrueVal;
8754 else if (match(FalseVal, m_AnyZeroFP()) && !match(TrueVal, m_AnyZeroFP()) &&
8755 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8756 OutputZeroVal = FalseVal;
8757
8758 if (OutputZeroVal) {
8759 if (match(CmpLHS, m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8760 HasMismatchedZeros = true;
8761 CmpLHS = OutputZeroVal;
8762 }
8763 if (match(CmpRHS, m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8764 HasMismatchedZeros = true;
8765 CmpRHS = OutputZeroVal;
8766 }
8767 }
8768 }
8769
8770 LHS = CmpLHS;
8771 RHS = CmpRHS;
8772
8773 // Signed zero may return inconsistent results between implementations.
8774 // (0.0 <= -0.0) ? 0.0 : -0.0 // Returns 0.0
8775 // minNum(0.0, -0.0) // May return -0.0 or 0.0 (IEEE 754-2008 5.3.1)
8776 // Therefore, we behave conservatively and only proceed if at least one of the
8777 // operands is known to not be zero or if we don't care about signed zero.
8778 switch (Pred) {
8779 default: break;
8782 if (!HasMismatchedZeros)
8783 break;
8784 [[fallthrough]];
8787 if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8788 !isKnownNonZero(CmpRHS))
8789 return {SPF_UNKNOWN, SPNB_NA, false};
8790 }
8791
8792 SelectPatternNaNBehavior NaNBehavior = SPNB_NA;
8793 bool Ordered = false;
8794
8795 // When given one NaN and one non-NaN input:
8796 // - maxnum/minnum (C99 fmaxf()/fminf()) return the non-NaN input.
8797 // - A simple C99 (a < b ? a : b) construction will return 'b' (as the
8798 // ordered comparison fails), which could be NaN or non-NaN.
8799 // so here we discover exactly what NaN behavior is required/accepted.
8800 if (CmpInst::isFPPredicate(Pred)) {
8801 bool LHSSafe = isKnownNonNaN(CmpLHS, FMF);
8802 bool RHSSafe = isKnownNonNaN(CmpRHS, FMF);
8803
8804 if (LHSSafe && RHSSafe) {
8805 // Both operands are known non-NaN.
8806 NaNBehavior = SPNB_RETURNS_ANY;
8807 } else if (CmpInst::isOrdered(Pred)) {
8808 // An ordered comparison will return false when given a NaN, so it
8809 // returns the RHS.
8810 Ordered = true;
8811 if (LHSSafe)
8812 // LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8813 NaNBehavior = SPNB_RETURNS_NAN;
8814 else if (RHSSafe)
8815 NaNBehavior = SPNB_RETURNS_OTHER;
8816 else
8817 // Completely unsafe.
8818 return {SPF_UNKNOWN, SPNB_NA, false};
8819 } else {
8820 Ordered = false;
8821 // An unordered comparison will return true when given a NaN, so it
8822 // returns the LHS.
8823 if (LHSSafe)
8824 // LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8825 NaNBehavior = SPNB_RETURNS_OTHER;
8826 else if (RHSSafe)
8827 NaNBehavior = SPNB_RETURNS_NAN;
8828 else
8829 // Completely unsafe.
8830 return {SPF_UNKNOWN, SPNB_NA, false};
8831 }
8832 }
8833
8834 if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8835 std::swap(CmpLHS, CmpRHS);
8836 Pred = CmpInst::getSwappedPredicate(Pred);
8837 if (NaNBehavior == SPNB_RETURNS_NAN)
8838 NaNBehavior = SPNB_RETURNS_OTHER;
8839 else if (NaNBehavior == SPNB_RETURNS_OTHER)
8840 NaNBehavior = SPNB_RETURNS_NAN;
8841 Ordered = !Ordered;
8842 }
8843
8844 // ([if]cmp X, Y) ? X : Y
8845 if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8846 return getSelectPattern(Pred, NaNBehavior, Ordered);
8847
8848 if (isKnownNegation(TrueVal, FalseVal)) {
8849 // Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8850 // match against either LHS or sext(LHS).
8851 auto MaybeSExtCmpLHS =
8852 m_CombineOr(m_Specific(CmpLHS), m_SExt(m_Specific(CmpLHS)));
8853 auto ZeroOrAllOnes = m_CombineOr(m_ZeroInt(), m_AllOnes());
8854 auto ZeroOrOne = m_CombineOr(m_ZeroInt(), m_One());
8855 if (match(TrueVal, MaybeSExtCmpLHS)) {
8856 // Set the return values. If the compare uses the negated value (-X >s 0),
8857 // swap the return values because the negated value is always 'RHS'.
8858 LHS = TrueVal;
8859 RHS = FalseVal;
8860 if (match(CmpLHS, m_Neg(m_Specific(FalseVal))))
8861 std::swap(LHS, RHS);
8862
8863 // (X >s 0) ? X : -X or (X >s -1) ? X : -X --> ABS(X)
8864 // (-X >s 0) ? -X : X or (-X >s -1) ? -X : X --> ABS(X)
8865 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8866 return {SPF_ABS, SPNB_NA, false};
8867
8868 // (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8869 if (Pred == ICmpInst::ICMP_SGE && match(CmpRHS, ZeroOrOne))
8870 return {SPF_ABS, SPNB_NA, false};
8871
8872 // (X <s 0) ? X : -X or (X <s 1) ? X : -X --> NABS(X)
8873 // (-X <s 0) ? -X : X or (-X <s 1) ? -X : X --> NABS(X)
8874 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8875 return {SPF_NABS, SPNB_NA, false};
8876 }
8877 else if (match(FalseVal, MaybeSExtCmpLHS)) {
8878 // Set the return values. If the compare uses the negated value (-X >s 0),
8879 // swap the return values because the negated value is always 'RHS'.
8880 LHS = FalseVal;
8881 RHS = TrueVal;
8882 if (match(CmpLHS, m_Neg(m_Specific(TrueVal))))
8883 std::swap(LHS, RHS);
8884
8885 // (X >s 0) ? -X : X or (X >s -1) ? -X : X --> NABS(X)
8886 // (-X >s 0) ? X : -X or (-X >s -1) ? X : -X --> NABS(X)
8887 if (Pred == ICmpInst::ICMP_SGT && match(CmpRHS, ZeroOrAllOnes))
8888 return {SPF_NABS, SPNB_NA, false};
8889
8890 // (X <s 0) ? -X : X or (X <s 1) ? -X : X --> ABS(X)
8891 // (-X <s 0) ? X : -X or (-X <s 1) ? X : -X --> ABS(X)
8892 if (Pred == ICmpInst::ICMP_SLT && match(CmpRHS, ZeroOrOne))
8893 return {SPF_ABS, SPNB_NA, false};
8894 }
8895 }
8896
8897 if (CmpInst::isIntPredicate(Pred))
8898 return matchMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS, Depth);
8899
8900 // According to (IEEE 754-2008 5.3.1), minNum(0.0, -0.0) and similar
8901 // may return either -0.0 or 0.0, so fcmp/select pair has stricter
8902 // semantics than minNum. Be conservative in such case.
8903 if (NaNBehavior != SPNB_RETURNS_ANY ||
8904 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8905 !isKnownNonZero(CmpRHS)))
8906 return {SPF_UNKNOWN, SPNB_NA, false};
8907
8908 return matchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal, LHS, RHS);
8909}
8910
8912 Instruction::CastOps *CastOp) {
8913 const DataLayout &DL = CmpI->getDataLayout();
8914
8915 Constant *CastedTo = nullptr;
8916 switch (*CastOp) {
8917 case Instruction::ZExt:
8918 if (CmpI->isUnsigned())
8919 CastedTo = ConstantExpr::getTrunc(C, SrcTy);
8920 break;
8921 case Instruction::SExt:
8922 if (CmpI->isSigned())
8923 CastedTo = ConstantExpr::getTrunc(C, SrcTy, true);
8924 break;
8925 case Instruction::Trunc:
8926 Constant *CmpConst;
8927 if (match(CmpI->getOperand(1), m_Constant(CmpConst)) &&
8928 CmpConst->getType() == SrcTy) {
8929 // Here we have the following case:
8930 //
8931 // %cond = cmp iN %x, CmpConst
8932 // %tr = trunc iN %x to iK
8933 // %narrowsel = select i1 %cond, iK %t, iK C
8934 //
8935 // We can always move trunc after select operation:
8936 //
8937 // %cond = cmp iN %x, CmpConst
8938 // %widesel = select i1 %cond, iN %x, iN CmpConst
8939 // %tr = trunc iN %widesel to iK
8940 //
8941 // Note that C could be extended in any way because we don't care about
8942 // upper bits after truncation. It can't be abs pattern, because it would
8943 // look like:
8944 //
8945 // select i1 %cond, x, -x.
8946 //
8947 // So only min/max pattern could be matched. Such match requires widened C
8948 // == CmpConst. That is why set widened C = CmpConst, condition trunc
8949 // CmpConst == C is checked below.
8950 CastedTo = CmpConst;
8951 } else {
8952 unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8953 CastedTo = ConstantFoldCastOperand(ExtOp, C, SrcTy, DL);
8954 }
8955 break;
8956 case Instruction::FPTrunc:
8957 CastedTo = ConstantFoldCastOperand(Instruction::FPExt, C, SrcTy, DL);
8958 break;
8959 case Instruction::FPExt:
8960 CastedTo = ConstantFoldCastOperand(Instruction::FPTrunc, C, SrcTy, DL);
8961 break;
8962 case Instruction::FPToUI:
8963 CastedTo = ConstantFoldCastOperand(Instruction::UIToFP, C, SrcTy, DL);
8964 break;
8965 case Instruction::FPToSI:
8966 CastedTo = ConstantFoldCastOperand(Instruction::SIToFP, C, SrcTy, DL);
8967 break;
8968 case Instruction::UIToFP:
8969 CastedTo = ConstantFoldCastOperand(Instruction::FPToUI, C, SrcTy, DL);
8970 break;
8971 case Instruction::SIToFP:
8972 CastedTo = ConstantFoldCastOperand(Instruction::FPToSI, C, SrcTy, DL);
8973 break;
8974 default:
8975 break;
8976 }
8977
8978 if (!CastedTo)
8979 return nullptr;
8980
8981 // Make sure the cast doesn't lose any information.
8982 Constant *CastedBack =
8983 ConstantFoldCastOperand(*CastOp, CastedTo, C->getType(), DL);
8984 if (CastedBack && CastedBack != C)
8985 return nullptr;
8986
8987 return CastedTo;
8988}
8989
8990/// Helps to match a select pattern in case of a type mismatch.
8991///
8992/// The function processes the case when type of true and false values of a
8993/// select instruction differs from type of the cmp instruction operands because
8994/// of a cast instruction. The function checks if it is legal to move the cast
8995/// operation after "select". If yes, it returns the new second value of
8996/// "select" (with the assumption that cast is moved):
8997/// 1. As operand of cast instruction when both values of "select" are same cast
8998/// instructions.
8999/// 2. As restored constant (by applying reverse cast operation) when the first
9000/// value of the "select" is a cast operation and the second value is a
9001/// constant. It is implemented in lookThroughCastConst().
9002/// 3. As one operand is cast instruction and the other is not. The operands in
9003/// sel(cmp) are in different type integer.
9004/// NOTE: We return only the new second value because the first value could be
9005/// accessed as operand of cast instruction.
9006static Value *lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2,
9007 Instruction::CastOps *CastOp) {
9008 auto *Cast1 = dyn_cast<CastInst>(V1);
9009 if (!Cast1)
9010 return nullptr;
9011
9012 *CastOp = Cast1->getOpcode();
9013 Type *SrcTy = Cast1->getSrcTy();
9014 if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9015 // If V1 and V2 are both the same cast from the same type, look through V1.
9016 if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9017 return Cast2->getOperand(0);
9018 return nullptr;
9019 }
9020
9021 auto *C = dyn_cast<Constant>(V2);
9022 if (C)
9023 return lookThroughCastConst(CmpI, SrcTy, C, CastOp);
9024
9025 Value *CastedTo = nullptr;
9026 if (*CastOp == Instruction::Trunc) {
9027 if (match(CmpI->getOperand(1), m_ZExtOrSExt(m_Specific(V2)))) {
9028 // Here we have the following case:
9029 // %y_ext = sext iK %y to iN
9030 // %cond = cmp iN %x, %y_ext
9031 // %tr = trunc iN %x to iK
9032 // %narrowsel = select i1 %cond, iK %tr, iK %y
9033 //
9034 // We can always move trunc after select operation:
9035 // %y_ext = sext iK %y to iN
9036 // %cond = cmp iN %x, %y_ext
9037 // %widesel = select i1 %cond, iN %x, iN %y_ext
9038 // %tr = trunc iN %widesel to iK
9039 assert(V2->getType() == Cast1->getType() &&
9040 "V2 and Cast1 should be the same type.");
9041 CastedTo = CmpI->getOperand(1);
9042 }
9043 }
9044
9045 return CastedTo;
9046}
9048 Instruction::CastOps *CastOp,
9049 unsigned Depth) {
9051 return {SPF_UNKNOWN, SPNB_NA, false};
9052
9053 SelectInst *SI = dyn_cast<SelectInst>(V);
9054 if (!SI) return {SPF_UNKNOWN, SPNB_NA, false};
9055
9056 CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9057 if (!CmpI) return {SPF_UNKNOWN, SPNB_NA, false};
9058
9059 Value *TrueVal = SI->getTrueValue();
9060 Value *FalseVal = SI->getFalseValue();
9061
9062 return llvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal, LHS, RHS,
9063 CastOp, Depth);
9064}
9065
9067 CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS,
9068 Instruction::CastOps *CastOp, unsigned Depth) {
9069 CmpInst::Predicate Pred = CmpI->getPredicate();
9070 Value *CmpLHS = CmpI->getOperand(0);
9071 Value *CmpRHS = CmpI->getOperand(1);
9072 FastMathFlags FMF;
9073 if (isa<FPMathOperator>(CmpI))
9074 FMF = CmpI->getFastMathFlags();
9075
9076 // Bail out early.
9077 if (CmpI->isEquality())
9078 return {SPF_UNKNOWN, SPNB_NA, false};
9079
9080 // Deal with type mismatches.
9081 if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9082 if (Value *C = lookThroughCast(CmpI, TrueVal, FalseVal, CastOp)) {
9083 // If this is a potential fmin/fmax with a cast to integer, then ignore
9084 // -0.0 because there is no corresponding integer value.
9085 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9086 FMF.setNoSignedZeros();
9087 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9088 cast<CastInst>(TrueVal)->getOperand(0), C,
9089 LHS, RHS, Depth);
9090 }
9091 if (Value *C = lookThroughCast(CmpI, FalseVal, TrueVal, CastOp)) {
9092 // If this is a potential fmin/fmax with a cast to integer, then ignore
9093 // -0.0 because there is no corresponding integer value.
9094 if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9095 FMF.setNoSignedZeros();
9096 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9097 C, cast<CastInst>(FalseVal)->getOperand(0),
9098 LHS, RHS, Depth);
9099 }
9100 }
9101 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9102 LHS, RHS, Depth);
9103}
9104
9106 if (SPF == SPF_SMIN) return ICmpInst::ICMP_SLT;
9107 if (SPF == SPF_UMIN) return ICmpInst::ICMP_ULT;
9108 if (SPF == SPF_SMAX) return ICmpInst::ICMP_SGT;
9109 if (SPF == SPF_UMAX) return ICmpInst::ICMP_UGT;
9110 if (SPF == SPF_FMINNUM)
9111 return Ordered ? FCmpInst::FCMP_OLT : FCmpInst::FCMP_ULT;
9112 if (SPF == SPF_FMAXNUM)
9113 return Ordered ? FCmpInst::FCMP_OGT : FCmpInst::FCMP_UGT;
9114 llvm_unreachable("unhandled!");
9115}
9116
9118 switch (SPF) {
9120 return Intrinsic::umin;
9122 return Intrinsic::umax;
9124 return Intrinsic::smin;
9126 return Intrinsic::smax;
9127 default:
9128 llvm_unreachable("Unexpected SPF");
9129 }
9130}
9131
9133 if (SPF == SPF_SMIN) return SPF_SMAX;
9134 if (SPF == SPF_UMIN) return SPF_UMAX;
9135 if (SPF == SPF_SMAX) return SPF_SMIN;
9136 if (SPF == SPF_UMAX) return SPF_UMIN;
9137 llvm_unreachable("unhandled!");
9138}
9139
9141 switch (MinMaxID) {
9142 case Intrinsic::smax: return Intrinsic::smin;
9143 case Intrinsic::smin: return Intrinsic::smax;
9144 case Intrinsic::umax: return Intrinsic::umin;
9145 case Intrinsic::umin: return Intrinsic::umax;
9146 // Please note that next four intrinsics may produce the same result for
9147 // original and inverted case even if X != Y due to NaN is handled specially.
9148 case Intrinsic::maximum: return Intrinsic::minimum;
9149 case Intrinsic::minimum: return Intrinsic::maximum;
9150 case Intrinsic::maxnum: return Intrinsic::minnum;
9151 case Intrinsic::minnum: return Intrinsic::maxnum;
9152 default: llvm_unreachable("Unexpected intrinsic");
9153 }
9154}
9155
9157 switch (SPF) {
9160 case SPF_UMAX: return APInt::getMaxValue(BitWidth);
9161 case SPF_UMIN: return APInt::getMinValue(BitWidth);
9162 default: llvm_unreachable("Unexpected flavor");
9163 }
9164}
9165
9166std::pair<Intrinsic::ID, bool>
9168 // Check if VL contains select instructions that can be folded into a min/max
9169 // vector intrinsic and return the intrinsic if it is possible.
9170 // TODO: Support floating point min/max.
9171 bool AllCmpSingleUse = true;
9172 SelectPatternResult SelectPattern;
9173 SelectPattern.Flavor = SPF_UNKNOWN;
9174 if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9175 Value *LHS, *RHS;
9176 auto CurrentPattern = matchSelectPattern(I, LHS, RHS);
9177 if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9178 return false;
9179 if (SelectPattern.Flavor != SPF_UNKNOWN &&
9180 SelectPattern.Flavor != CurrentPattern.Flavor)
9181 return false;
9182 SelectPattern = CurrentPattern;
9183 AllCmpSingleUse &=
9185 return true;
9186 })) {
9187 switch (SelectPattern.Flavor) {
9188 case SPF_SMIN:
9189 return {Intrinsic::smin, AllCmpSingleUse};
9190 case SPF_UMIN:
9191 return {Intrinsic::umin, AllCmpSingleUse};
9192 case SPF_SMAX:
9193 return {Intrinsic::smax, AllCmpSingleUse};
9194 case SPF_UMAX:
9195 return {Intrinsic::umax, AllCmpSingleUse};
9196 case SPF_FMAXNUM:
9197 return {Intrinsic::maxnum, AllCmpSingleUse};
9198 case SPF_FMINNUM:
9199 return {Intrinsic::minnum, AllCmpSingleUse};
9200 default:
9201 llvm_unreachable("unexpected select pattern flavor");
9202 }
9203 }
9204 return {Intrinsic::not_intrinsic, false};
9205}
9206
9208 Value *&Start, Value *&Step) {
9209 // Handle the case of a simple two-predecessor recurrence PHI.
9210 // There's a lot more that could theoretically be done here, but
9211 // this is sufficient to catch some interesting cases.
9212 if (P->getNumIncomingValues() != 2)
9213 return false;
9214
9215 for (unsigned i = 0; i != 2; ++i) {
9216 Value *L = P->getIncomingValue(i);
9217 Value *R = P->getIncomingValue(!i);
9218 auto *LU = dyn_cast<BinaryOperator>(L);
9219 if (!LU)
9220 continue;
9221 unsigned Opcode = LU->getOpcode();
9222
9223 switch (Opcode) {
9224 default:
9225 continue;
9226 // TODO: Expand list -- xor, gep, uadd.sat etc.
9227 case Instruction::LShr:
9228 case Instruction::AShr:
9229 case Instruction::Shl:
9230 case Instruction::Add:
9231 case Instruction::Sub:
9232 case Instruction::UDiv:
9233 case Instruction::URem:
9234 case Instruction::And:
9235 case Instruction::Or:
9236 case Instruction::Mul:
9237 case Instruction::FMul: {
9238 Value *LL = LU->getOperand(0);
9239 Value *LR = LU->getOperand(1);
9240 // Find a recurrence.
9241 if (LL == P)
9242 L = LR;
9243 else if (LR == P)
9244 L = LL;
9245 else
9246 continue; // Check for recurrence with L and R flipped.
9247
9248 break; // Match!
9249 }
9250 };
9251
9252 // We have matched a recurrence of the form:
9253 // %iv = [R, %entry], [%iv.next, %backedge]
9254 // %iv.next = binop %iv, L
9255 // OR
9256 // %iv = [R, %entry], [%iv.next, %backedge]
9257 // %iv.next = binop L, %iv
9258 BO = LU;
9259 Start = R;
9260 Step = L;
9261 return true;
9262 }
9263 return false;
9264}
9265
9267 Value *&Start, Value *&Step) {
9268 BinaryOperator *BO = nullptr;
9269 P = dyn_cast<PHINode>(I->getOperand(0));
9270 if (!P)
9271 P = dyn_cast<PHINode>(I->getOperand(1));
9272 return P && matchSimpleRecurrence(P, BO, Start, Step) && BO == I;
9273}
9274
9275/// Return true if "icmp Pred LHS RHS" is always true.
9276static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS,
9277 const Value *RHS) {
9278 if (ICmpInst::isTrueWhenEqual(Pred) && LHS == RHS)
9279 return true;
9280
9281 switch (Pred) {
9282 default:
9283 return false;
9284
9285 case CmpInst::ICMP_SLE: {
9286 const APInt *C;
9287
9288 // LHS s<= LHS +_{nsw} C if C >= 0
9289 // LHS s<= LHS | C if C >= 0
9290 if (match(RHS, m_NSWAdd(m_Specific(LHS), m_APInt(C))) ||
9292 return !C->isNegative();
9293
9294 // LHS s<= smax(LHS, V) for any V
9296 return true;
9297
9298 // smin(RHS, V) s<= RHS for any V
9300 return true;
9301
9302 // Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9303 const Value *X;
9304 const APInt *CLHS, *CRHS;
9305 if (match(LHS, m_NSWAddLike(m_Value(X), m_APInt(CLHS))) &&
9307 return CLHS->sle(*CRHS);
9308
9309 return false;
9310 }
9311
9312 case CmpInst::ICMP_ULE: {
9313 // LHS u<= LHS +_{nuw} V for any V
9314 if (match(RHS, m_c_Add(m_Specific(LHS), m_Value())) &&
9315 cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
9316 return true;
9317
9318 // LHS u<= LHS | V for any V
9319 if (match(RHS, m_c_Or(m_Specific(LHS), m_Value())))
9320 return true;
9321
9322 // LHS u<= umax(LHS, V) for any V
9324 return true;
9325
9326 // RHS >> V u<= RHS for any V
9327 if (match(LHS, m_LShr(m_Specific(RHS), m_Value())))
9328 return true;
9329
9330 // RHS u/ C_ugt_1 u<= RHS
9331 const APInt *C;
9332 if (match(LHS, m_UDiv(m_Specific(RHS), m_APInt(C))) && C->ugt(1))
9333 return true;
9334
9335 // RHS & V u<= RHS for any V
9337 return true;
9338
9339 // umin(RHS, V) u<= RHS for any V
9341 return true;
9342
9343 // Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9344 const Value *X;
9345 const APInt *CLHS, *CRHS;
9346 if (match(LHS, m_NUWAddLike(m_Value(X), m_APInt(CLHS))) &&
9348 return CLHS->ule(*CRHS);
9349
9350 return false;
9351 }
9352 }
9353}
9354
9355/// Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred
9356/// ALHS ARHS" is true. Otherwise, return std::nullopt.
9357static std::optional<bool>
9359 const Value *ARHS, const Value *BLHS, const Value *BRHS) {
9360 switch (Pred) {
9361 default:
9362 return std::nullopt;
9363
9364 case CmpInst::ICMP_SLT:
9365 case CmpInst::ICMP_SLE:
9366 if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9368 return true;
9369 return std::nullopt;
9370
9371 case CmpInst::ICMP_SGT:
9372 case CmpInst::ICMP_SGE:
9373 if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9375 return true;
9376 return std::nullopt;
9377
9378 case CmpInst::ICMP_ULT:
9379 case CmpInst::ICMP_ULE:
9380 if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9382 return true;
9383 return std::nullopt;
9384
9385 case CmpInst::ICMP_UGT:
9386 case CmpInst::ICMP_UGE:
9387 if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9389 return true;
9390 return std::nullopt;
9391 }
9392}
9393
9394/// Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
9395/// Return false if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is false.
9396/// Otherwise, return std::nullopt if we can't infer anything.
9397static std::optional<bool>
9399 CmpPredicate RPred, const ConstantRange &RCR) {
9400 auto CRImpliesPred = [&](ConstantRange CR,
9401 CmpInst::Predicate Pred) -> std::optional<bool> {
9402 // If all true values for lhs and true for rhs, lhs implies rhs
9403 if (CR.icmp(Pred, RCR))
9404 return true;
9405
9406 // If there is no overlap, lhs implies not rhs
9407 if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9408 return false;
9409
9410 return std::nullopt;
9411 };
9412 if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9413 RPred))
9414 return Res;
9415 if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9417 : static_cast<CmpInst::Predicate>(LPred);
9419 : static_cast<CmpInst::Predicate>(RPred);
9420 return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9421 RPred);
9422 }
9423 return std::nullopt;
9424}
9425
9426/// Return true if LHS implies RHS (expanded to its components as "R0 RPred R1")
9427/// is true. Return false if LHS implies RHS is false. Otherwise, return
9428/// std::nullopt if we can't infer anything.
9429static std::optional<bool>
9430isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0,
9431 const Value *R1, const DataLayout &DL, bool LHSIsTrue) {
9432 Value *L0 = LHS->getOperand(0);
9433 Value *L1 = LHS->getOperand(1);
9434
9435 // The rest of the logic assumes the LHS condition is true. If that's not the
9436 // case, invert the predicate to make it so.
9437 CmpPredicate LPred =
9438 LHSIsTrue ? LHS->getCmpPredicate() : LHS->getInverseCmpPredicate();
9439
9440 // We can have non-canonical operands, so try to normalize any common operand
9441 // to L0/R0.
9442 if (L0 == R1) {
9443 std::swap(R0, R1);
9444 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9445 }
9446 if (R0 == L1) {
9447 std::swap(L0, L1);
9448 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9449 }
9450 if (L1 == R1) {
9451 // If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9452 if (L0 != R0 || match(L0, m_ImmConstant())) {
9453 std::swap(L0, L1);
9454 LPred = ICmpInst::getSwappedCmpPredicate(LPred);
9455 std::swap(R0, R1);
9456 RPred = ICmpInst::getSwappedCmpPredicate(RPred);
9457 }
9458 }
9459
9460 // See if we can infer anything if operand-0 matches and we have at least one
9461 // constant.
9462 const APInt *Unused;
9463 if (L0 == R0 && (match(L1, m_APInt(Unused)) || match(R1, m_APInt(Unused)))) {
9464 // Potential TODO: We could also further use the constant range of L0/R0 to
9465 // further constraint the constant ranges. At the moment this leads to
9466 // several regressions related to not transforming `multi_use(A + C0) eq/ne
9467 // C1` (see discussion: D58633).
9469 L1, ICmpInst::isSigned(LPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9470 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9472 R1, ICmpInst::isSigned(RPred), /* UseInstrInfo=*/true, /*AC=*/nullptr,
9473 /*CxtI=*/nullptr, /*DT=*/nullptr, MaxAnalysisRecursionDepth - 1);
9474 // Even if L1/R1 are not both constant, we can still sometimes deduce
9475 // relationship from a single constant. For example X u> Y implies X != 0.
9476 if (auto R = isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9477 return R;
9478 // If both L1/R1 were exact constant ranges and we didn't get anything
9479 // here, we won't be able to deduce this.
9480 if (match(L1, m_APInt(Unused)) && match(R1, m_APInt(Unused)))
9481 return std::nullopt;
9482 }
9483
9484 // Can we infer anything when the two compares have matching operands?
9485 if (L0 == R0 && L1 == R1)
9486 return ICmpInst::isImpliedByMatchingCmp(LPred, RPred);
9487
9488 // It only really makes sense in the context of signed comparison for "X - Y
9489 // must be positive if X >= Y and no overflow".
9490 // Take SGT as an example: L0:x > L1:y and C >= 0
9491 // ==> R0:(x -nsw y) < R1:(-C) is false
9492 CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9493 if ((SignedLPred == ICmpInst::ICMP_SGT ||
9494 SignedLPred == ICmpInst::ICMP_SGE) &&
9495 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9496 if (match(R1, m_NonPositive()) &&
9497 ICmpInst::isImpliedByMatchingCmp(LPred, RPred) == false)
9498 return false;
9499 }
9500
9501 // Take SLT as an example: L0:x < L1:y and C <= 0
9502 // ==> R0:(x -nsw y) < R1:(-C) is true
9503 if ((SignedLPred == ICmpInst::ICMP_SLT ||
9504 SignedLPred == ICmpInst::ICMP_SLE) &&
9505 match(R0, m_NSWSub(m_Specific(L0), m_Specific(L1)))) {
9506 if (match(R1, m_NonNegative()) &&
9507 ICmpInst::isImpliedByMatchingCmp(LPred, RPred) == true)
9508 return true;
9509 }
9510
9511 // L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9512 if (L0 == R0 &&
9513 (LPred == ICmpInst::ICMP_ULT || LPred == ICmpInst::ICMP_UGE) &&
9514 (RPred == ICmpInst::ICMP_ULT || RPred == ICmpInst::ICMP_UGE) &&
9515 match(L0, m_c_Add(m_Specific(L1), m_Specific(R1))))
9516 return CmpPredicate::getMatching(LPred, RPred).has_value();
9517
9518 if (auto P = CmpPredicate::getMatching(LPred, RPred))
9519 return isImpliedCondOperands(*P, L0, L1, R0, R1);
9520
9521 return std::nullopt;
9522}
9523
9524/// Return true if LHS implies RHS is true. Return false if LHS implies RHS is
9525/// false. Otherwise, return std::nullopt if we can't infer anything. We
9526/// expect the RHS to be an icmp and the LHS to be an 'and', 'or', or a 'select'
9527/// instruction.
9528static std::optional<bool>
9530 const Value *RHSOp0, const Value *RHSOp1,
9531 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9532 // The LHS must be an 'or', 'and', or a 'select' instruction.
9533 assert((LHS->getOpcode() == Instruction::And ||
9534 LHS->getOpcode() == Instruction::Or ||
9535 LHS->getOpcode() == Instruction::Select) &&
9536 "Expected LHS to be 'and', 'or', or 'select'.");
9537
9538 assert(Depth <= MaxAnalysisRecursionDepth && "Hit recursion limit");
9539
9540 // If the result of an 'or' is false, then we know both legs of the 'or' are
9541 // false. Similarly, if the result of an 'and' is true, then we know both
9542 // legs of the 'and' are true.
9543 const Value *ALHS, *ARHS;
9544 if ((!LHSIsTrue && match(LHS, m_LogicalOr(m_Value(ALHS), m_Value(ARHS)))) ||
9545 (LHSIsTrue && match(LHS, m_LogicalAnd(m_Value(ALHS), m_Value(ARHS))))) {
9546 // FIXME: Make this non-recursion.
9547 if (std::optional<bool> Implication = isImpliedCondition(
9548 ALHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9549 return Implication;
9550 if (std::optional<bool> Implication = isImpliedCondition(
9551 ARHS, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue, Depth + 1))
9552 return Implication;
9553 return std::nullopt;
9554 }
9555 return std::nullopt;
9556}
9557
9558std::optional<bool>
9560 const Value *RHSOp0, const Value *RHSOp1,
9561 const DataLayout &DL, bool LHSIsTrue, unsigned Depth) {
9562 // Bail out when we hit the limit.
9564 return std::nullopt;
9565
9566 // A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9567 // example.
9568 if (RHSOp0->getType()->isVectorTy() != LHS->getType()->isVectorTy())
9569 return std::nullopt;
9570
9572 "Expected integer type only!");
9573
9574 // Match not
9575 if (match(LHS, m_Not(m_Value(LHS))))
9576 LHSIsTrue = !LHSIsTrue;
9577
9578 // Both LHS and RHS are icmps.
9579 const ICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
9580 if (LHSCmp)
9581 return isImpliedCondICmps(LHSCmp, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue);
9582
9583 /// The LHS should be an 'or', 'and', or a 'select' instruction. We expect
9584 /// the RHS to be an icmp.
9585 /// FIXME: Add support for and/or/select on the RHS.
9586 if (const Instruction *LHSI = dyn_cast<Instruction>(LHS)) {
9587 if ((LHSI->getOpcode() == Instruction::And ||
9588 LHSI->getOpcode() == Instruction::Or ||
9589 LHSI->getOpcode() == Instruction::Select))
9590 return isImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1, DL, LHSIsTrue,
9591 Depth);
9592 }
9593 return std::nullopt;
9594}
9595
9596std::optional<bool> llvm::isImpliedCondition(const Value *LHS, const Value *RHS,
9597 const DataLayout &DL,
9598 bool LHSIsTrue, unsigned Depth) {
9599 // LHS ==> RHS by definition
9600 if (LHS == RHS)
9601 return LHSIsTrue;
9602
9603 // Match not
9604 bool InvertRHS = false;
9605 if (match(RHS, m_Not(m_Value(RHS)))) {
9606 if (LHS == RHS)
9607 return !LHSIsTrue;
9608 InvertRHS = true;
9609 }
9610
9611 if (const ICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9612 if (auto Implied = isImpliedCondition(
9613 LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9614 RHSCmp->getOperand(1), DL, LHSIsTrue, Depth))
9615 return InvertRHS ? !*Implied : *Implied;
9616 return std::nullopt;
9617 }
9618
9620 return std::nullopt;
9621
9622 // LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9623 // LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9624 const Value *RHS1, *RHS2;
9625 if (match(RHS, m_LogicalOr(m_Value(RHS1), m_Value(RHS2)))) {
9626 if (std::optional<bool> Imp =
9627 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9628 if (*Imp == true)
9629 return !InvertRHS;
9630 if (std::optional<bool> Imp =
9631 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9632 if (*Imp == true)
9633 return !InvertRHS;
9634 }
9635 if (match(RHS, m_LogicalAnd(m_Value(RHS1), m_Value(RHS2)))) {
9636 if (std::optional<bool> Imp =
9637 isImpliedCondition(LHS, RHS1, DL, LHSIsTrue, Depth + 1))
9638 if (*Imp == false)
9639 return InvertRHS;
9640 if (std::optional<bool> Imp =
9641 isImpliedCondition(LHS, RHS2, DL, LHSIsTrue, Depth + 1))
9642 if (*Imp == false)
9643 return InvertRHS;
9644 }
9645
9646 return std::nullopt;
9647}
9648
9649// Returns a pair (Condition, ConditionIsTrue), where Condition is a branch
9650// condition dominating ContextI or nullptr, if no condition is found.
9651static std::pair<Value *, bool>
9653 if (!ContextI || !ContextI->getParent())
9654 return {nullptr, false};
9655
9656 // TODO: This is a poor/cheap way to determine dominance. Should we use a
9657 // dominator tree (eg, from a SimplifyQuery) instead?
9658 const BasicBlock *ContextBB = ContextI->getParent();
9659 const BasicBlock *PredBB = ContextBB->getSinglePredecessor();
9660 if (!PredBB)
9661 return {nullptr, false};
9662
9663 // We need a conditional branch in the predecessor.
9664 Value *PredCond;
9665 BasicBlock *TrueBB, *FalseBB;
9666 if (!match(PredBB->getTerminator(), m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9667 return {nullptr, false};
9668
9669 // The branch should get simplified. Don't bother simplifying this condition.
9670 if (TrueBB == FalseBB)
9671 return {nullptr, false};
9672
9673 assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9674 "Predecessor block does not point to successor?");
9675
9676 // Is this condition implied by the predecessor condition?
9677 return {PredCond, TrueBB == ContextBB};
9678}
9679
9680std::optional<bool> llvm::isImpliedByDomCondition(const Value *Cond,
9681 const Instruction *ContextI,
9682 const DataLayout &DL) {
9683 assert(Cond->getType()->isIntOrIntVectorTy(1) && "Condition must be bool");
9684 auto PredCond = getDomPredecessorCondition(ContextI);
9685 if (PredCond.first)
9686 return isImpliedCondition(PredCond.first, Cond, DL, PredCond.second);
9687 return std::nullopt;
9688}
9689
9691 const Value *LHS,
9692 const Value *RHS,
9693 const Instruction *ContextI,
9694 const DataLayout &DL) {
9695 auto PredCond = getDomPredecessorCondition(ContextI);
9696 if (PredCond.first)
9697 return isImpliedCondition(PredCond.first, Pred, LHS, RHS, DL,
9698 PredCond.second);
9699 return std::nullopt;
9700}
9701
9703 APInt &Upper, const InstrInfoQuery &IIQ,
9704 bool PreferSignedRange) {
9705 unsigned Width = Lower.getBitWidth();
9706 const APInt *C;
9707 switch (BO.getOpcode()) {
9708 case Instruction::Add:
9709 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9710 bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9711 bool HasNUW = IIQ.hasNoUnsignedWrap(&BO);
9712
9713 // If the caller expects a signed compare, then try to use a signed range.
9714 // Otherwise if both no-wraps are set, use the unsigned range because it
9715 // is never larger than the signed range. Example:
9716 // "add nuw nsw i8 X, -2" is unsigned [254,255] vs. signed [-128, 125].
9717 if (PreferSignedRange && HasNSW && HasNUW)
9718 HasNUW = false;
9719
9720 if (HasNUW) {
9721 // 'add nuw x, C' produces [C, UINT_MAX].
9722 Lower = *C;
9723 } else if (HasNSW) {
9724 if (C->isNegative()) {
9725 // 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9727 Upper = APInt::getSignedMaxValue(Width) + *C + 1;
9728 } else {
9729 // 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9730 Lower = APInt::getSignedMinValue(Width) + *C;
9731 Upper = APInt::getSignedMaxValue(Width) + 1;
9732 }
9733 }
9734 }
9735 break;
9736
9737 case Instruction::And:
9738 if (match(BO.getOperand(1), m_APInt(C)))
9739 // 'and x, C' produces [0, C].
9740 Upper = *C + 1;
9741 // X & -X is a power of two or zero. So we can cap the value at max power of
9742 // two.
9743 if (match(BO.getOperand(0), m_Neg(m_Specific(BO.getOperand(1)))) ||
9744 match(BO.getOperand(1), m_Neg(m_Specific(BO.getOperand(0)))))
9745 Upper = APInt::getSignedMinValue(Width) + 1;
9746 break;
9747
9748 case Instruction::Or:
9749 if (match(BO.getOperand(1), m_APInt(C)))
9750 // 'or x, C' produces [C, UINT_MAX].
9751 Lower = *C;
9752 break;
9753
9754 case Instruction::AShr:
9755 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9756 // 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9758 Upper = APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9759 } else if (match(BO.getOperand(0), m_APInt(C))) {
9760 unsigned ShiftAmount = Width - 1;
9761 if (!C->isZero() && IIQ.isExact(&BO))
9762 ShiftAmount = C->countr_zero();
9763 if (C->isNegative()) {
9764 // 'ashr C, x' produces [C, C >> (Width-1)]
9765 Lower = *C;
9766 Upper = C->ashr(ShiftAmount) + 1;
9767 } else {
9768 // 'ashr C, x' produces [C >> (Width-1), C]
9769 Lower = C->ashr(ShiftAmount);
9770 Upper = *C + 1;
9771 }
9772 }
9773 break;
9774
9775 case Instruction::LShr:
9776 if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9777 // 'lshr x, C' produces [0, UINT_MAX >> C].
9778 Upper = APInt::getAllOnes(Width).lshr(*C) + 1;
9779 } else if (match(BO.getOperand(0), m_APInt(C))) {
9780 // 'lshr C, x' produces [C >> (Width-1), C].
9781 unsigned ShiftAmount = Width - 1;
9782 if (!C->isZero() && IIQ.isExact(&BO))
9783 ShiftAmount = C->countr_zero();
9784 Lower = C->lshr(ShiftAmount);
9785 Upper = *C + 1;
9786 }
9787 break;
9788
9789 case Instruction::Shl:
9790 if (match(BO.getOperand(0), m_APInt(C))) {
9791 if (IIQ.hasNoUnsignedWrap(&BO)) {
9792 // 'shl nuw C, x' produces [C, C << CLZ(C)]
9793 Lower = *C;
9794 Upper = Lower.shl(Lower.countl_zero()) + 1;
9795 } else if (BO.hasNoSignedWrap()) { // TODO: What if both nuw+nsw?
9796 if (C->isNegative()) {
9797 // 'shl nsw C, x' produces [C << CLO(C)-1, C]
9798 unsigned ShiftAmount = C->countl_one() - 1;
9799 Lower = C->shl(ShiftAmount);
9800 Upper = *C + 1;
9801 } else {
9802 // 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9803 unsigned ShiftAmount = C->countl_zero() - 1;
9804 Lower = *C;
9805 Upper = C->shl(ShiftAmount) + 1;
9806 }
9807 } else {
9808 // If lowbit is set, value can never be zero.
9809 if ((*C)[0])
9810 Lower = APInt::getOneBitSet(Width, 0);
9811 // If we are shifting a constant the largest it can be is if the longest
9812 // sequence of consecutive ones is shifted to the highbits (breaking
9813 // ties for which sequence is higher). At the moment we take a liberal
9814 // upper bound on this by just popcounting the constant.
9815 // TODO: There may be a bitwise trick for it longest/highest
9816 // consecutative sequence of ones (naive method is O(Width) loop).
9817 Upper = APInt::getHighBitsSet(Width, C->popcount()) + 1;
9818 }
9819 } else if (match(BO.getOperand(1), m_APInt(C)) && C->ult(Width)) {
9820 Upper = APInt::getBitsSetFrom(Width, C->getZExtValue()) + 1;
9821 }
9822 break;
9823
9824 case Instruction::SDiv:
9825 if (match(BO.getOperand(1), m_APInt(C))) {
9826 APInt IntMin = APInt::getSignedMinValue(Width);
9827 APInt IntMax = APInt::getSignedMaxValue(Width);
9828 if (C->isAllOnes()) {
9829 // 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9830 // where C != -1 and C != 0 and C != 1
9831 Lower = IntMin + 1;
9832 Upper = IntMax + 1;
9833 } else if (C->countl_zero() < Width - 1) {
9834 // 'sdiv x, C' produces [INT_MIN / C, INT_MAX / C]
9835 // where C != -1 and C != 0 and C != 1
9836 Lower = IntMin.sdiv(*C);
9837 Upper = IntMax.sdiv(*C);
9838 if (Lower.sgt(Upper))
9840 Upper = Upper + 1;
9841 assert(Upper != Lower && "Upper part of range has wrapped!");
9842 }
9843 } else if (match(BO.getOperand(0), m_APInt(C))) {
9844 if (C->isMinSignedValue()) {
9845 // 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9846 Lower = *C;
9847 Upper = Lower.lshr(1) + 1;
9848 } else {
9849 // 'sdiv C, x' produces [-|C|, |C|].
9850 Upper = C->abs() + 1;
9851 Lower = (-Upper) + 1;
9852 }
9853 }
9854 break;
9855
9856 case Instruction::UDiv:
9857 if (match(BO.getOperand(1), m_APInt(C)) && !C->isZero()) {
9858 // 'udiv x, C' produces [0, UINT_MAX / C].
9859 Upper = APInt::getMaxValue(Width).udiv(*C) + 1;
9860 } else if (match(BO.getOperand(0), m_APInt(C))) {
9861 // 'udiv C, x' produces [0, C].
9862 Upper = *C + 1;
9863 }
9864 break;
9865
9866 case Instruction::SRem:
9867 if (match(BO.getOperand(1), m_APInt(C))) {
9868 // 'srem x, C' produces (-|C|, |C|).
9869 Upper = C->abs();
9870 Lower = (-Upper) + 1;
9871 } else if (match(BO.getOperand(0), m_APInt(C))) {
9872 if (C->isNegative()) {
9873 // 'srem -|C|, x' produces [-|C|, 0].
9874 Upper = 1;
9875 Lower = *C;
9876 } else {
9877 // 'srem |C|, x' produces [0, |C|].
9878 Upper = *C + 1;
9879 }
9880 }
9881 break;
9882
9883 case Instruction::URem:
9884 if (match(BO.getOperand(1), m_APInt(C)))
9885 // 'urem x, C' produces [0, C).
9886 Upper = *C;
9887 else if (match(BO.getOperand(0), m_APInt(C)))
9888 // 'urem C, x' produces [0, C].
9889 Upper = *C + 1;
9890 break;
9891
9892 default:
9893 break;
9894 }
9895}
9896
9898 bool UseInstrInfo) {
9899 unsigned Width = II.getType()->getScalarSizeInBits();
9900 const APInt *C;
9901 switch (II.getIntrinsicID()) {
9902 case Intrinsic::ctlz:
9903 case Intrinsic::cttz: {
9904 APInt Upper(Width, Width);
9905 if (!UseInstrInfo || !match(II.getArgOperand(1), m_One()))
9906 Upper += 1;
9907 // Maximum of set/clear bits is the bit width.
9909 }
9910 case Intrinsic::ctpop:
9911 // Maximum of set/clear bits is the bit width.
9913 APInt(Width, Width) + 1);
9914 case Intrinsic::uadd_sat:
9915 // uadd.sat(x, C) produces [C, UINT_MAX].
9916 if (match(II.getOperand(0), m_APInt(C)) ||
9917 match(II.getOperand(1), m_APInt(C)))
9919 break;
9920 case Intrinsic::sadd_sat:
9921 if (match(II.getOperand(0), m_APInt(C)) ||
9922 match(II.getOperand(1), m_APInt(C))) {
9923 if (C->isNegative())
9924 // sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9926 APInt::getSignedMaxValue(Width) + *C +
9927 1);
9928
9929 // sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9931 APInt::getSignedMaxValue(Width) + 1);
9932 }
9933 break;
9934 case Intrinsic::usub_sat:
9935 // usub.sat(C, x) produces [0, C].
9936 if (match(II.getOperand(0), m_APInt(C)))
9937 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9938
9939 // usub.sat(x, C) produces [0, UINT_MAX - C].
9940 if (match(II.getOperand(1), m_APInt(C)))
9942 APInt::getMaxValue(Width) - *C + 1);
9943 break;
9944 case Intrinsic::ssub_sat:
9945 if (match(II.getOperand(0), m_APInt(C))) {
9946 if (C->isNegative())
9947 // ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9949 *C - APInt::getSignedMinValue(Width) +
9950 1);
9951
9952 // ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9954 APInt::getSignedMaxValue(Width) + 1);
9955 } else if (match(II.getOperand(1), m_APInt(C))) {
9956 if (C->isNegative())
9957 // ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9959 APInt::getSignedMaxValue(Width) + 1);
9960
9961 // ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9963 APInt::getSignedMaxValue(Width) - *C +
9964 1);
9965 }
9966 break;
9967 case Intrinsic::umin:
9968 case Intrinsic::umax:
9969 case Intrinsic::smin:
9970 case Intrinsic::smax:
9971 if (!match(II.getOperand(0), m_APInt(C)) &&
9972 !match(II.getOperand(1), m_APInt(C)))
9973 break;
9974
9975 switch (II.getIntrinsicID()) {
9976 case Intrinsic::umin:
9977 return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9978 case Intrinsic::umax:
9980 case Intrinsic::smin:
9982 *C + 1);
9983 case Intrinsic::smax:
9985 APInt::getSignedMaxValue(Width) + 1);
9986 default:
9987 llvm_unreachable("Must be min/max intrinsic");
9988 }
9989 break;
9990 case Intrinsic::abs:
9991 // If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
9992 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
9993 if (match(II.getOperand(1), m_One()))
9995 APInt::getSignedMaxValue(Width) + 1);
9996
9998 APInt::getSignedMinValue(Width) + 1);
9999 case Intrinsic::vscale:
10000 if (!II.getParent() || !II.getFunction())
10001 break;
10002 return getVScaleRange(II.getFunction(), Width);
10003 case Intrinsic::scmp:
10004 case Intrinsic::ucmp:
10006 APInt(Width, 2));
10007 default:
10008 break;
10009 }
10010
10011 return ConstantRange::getFull(Width);
10012}
10013
10015 const InstrInfoQuery &IIQ) {
10016 unsigned BitWidth = SI.getType()->getScalarSizeInBits();
10017 const Value *LHS = nullptr, *RHS = nullptr;
10019 if (R.Flavor == SPF_UNKNOWN)
10020 return ConstantRange::getFull(BitWidth);
10021
10022 if (R.Flavor == SelectPatternFlavor::SPF_ABS) {
10023 // If the negation part of the abs (in RHS) has the NSW flag,
10024 // then the result of abs(X) is [0..SIGNED_MAX],
10025 // otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
10026 if (match(RHS, m_Neg(m_Specific(LHS))) &&
10027 IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
10030
10033 }
10034
10035 if (R.Flavor == SelectPatternFlavor::SPF_NABS) {
10036 // The result of -abs(X) is <= 0.
10038 APInt(BitWidth, 1));
10039 }
10040
10041 const APInt *C;
10042 if (!match(LHS, m_APInt(C)) && !match(RHS, m_APInt(C)))
10043 return ConstantRange::getFull(BitWidth);
10044
10045 switch (R.Flavor) {
10046 case SPF_UMIN:
10048 case SPF_UMAX:
10050 case SPF_SMIN:
10052 *C + 1);
10053 case SPF_SMAX:
10056 default:
10057 return ConstantRange::getFull(BitWidth);
10058 }
10059}
10060
10062 // The maximum representable value of a half is 65504. For floats the maximum
10063 // value is 3.4e38 which requires roughly 129 bits.
10064 unsigned BitWidth = I->getType()->getScalarSizeInBits();
10065 if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10066 return;
10067 if (isa<FPToSIInst>(I) && BitWidth >= 17) {
10068 Lower = APInt(BitWidth, -65504, true);
10069 Upper = APInt(BitWidth, 65505);
10070 }
10071
10072 if (isa<FPToUIInst>(I) && BitWidth >= 16) {
10073 // For a fptoui the lower limit is left as 0.
10074 Upper = APInt(BitWidth, 65505);
10075 }
10076}
10077
10079 bool UseInstrInfo, AssumptionCache *AC,
10080 const Instruction *CtxI,
10081 const DominatorTree *DT,
10082 unsigned Depth) {
10083 assert(V->getType()->isIntOrIntVectorTy() && "Expected integer instruction");
10084
10086 return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10087
10088 if (auto *C = dyn_cast<Constant>(V))
10089 return C->toConstantRange();
10090
10091 unsigned BitWidth = V->getType()->getScalarSizeInBits();
10092 InstrInfoQuery IIQ(UseInstrInfo);
10093 ConstantRange CR = ConstantRange::getFull(BitWidth);
10094 if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10095 APInt Lower = APInt(BitWidth, 0);
10096 APInt Upper = APInt(BitWidth, 0);
10097 // TODO: Return ConstantRange.
10098 setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
10100 } else if (auto *II = dyn_cast<IntrinsicInst>(V))
10101 CR = getRangeForIntrinsic(*II, UseInstrInfo);
10102 else if (auto *SI = dyn_cast<SelectInst>(V)) {
10104 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10106 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
10107 CR = CRTrue.unionWith(CRFalse);
10108 CR = CR.intersectWith(getRangeForSelectPattern(*SI, IIQ));
10109 } else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10110 APInt Lower = APInt(BitWidth, 0);
10111 APInt Upper = APInt(BitWidth, 0);
10112 // TODO: Return ConstantRange.
10113 setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
10115 } else if (const auto *A = dyn_cast<Argument>(V))
10116 if (std::optional<ConstantRange> Range = A->getRange())
10117 CR = *Range;
10118
10119 if (auto *I = dyn_cast<Instruction>(V)) {
10120 if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10122
10123 if (const auto *CB = dyn_cast<CallBase>(V))
10124 if (std::optional<ConstantRange> Range = CB->getRange())
10125 CR = CR.intersectWith(*Range);
10126 }
10127
10128 if (CtxI && AC) {
10129 // Try to restrict the range based on information from assumptions.
10130 for (auto &AssumeVH : AC->assumptionsFor(V)) {
10131 if (!AssumeVH)
10132 continue;
10133 CallInst *I = cast<CallInst>(AssumeVH);
10134 assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10135 "Got assumption for the wrong function!");
10136 assert(I->getIntrinsicID() == Intrinsic::assume &&
10137 "must be an assume intrinsic");
10138
10139 if (!isValidAssumeForContext(I, CtxI, DT))
10140 continue;
10141 Value *Arg = I->getArgOperand(0);
10142 ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10143 // Currently we just use information from comparisons.
10144 if (!Cmp || Cmp->getOperand(0) != V)
10145 continue;
10146 // TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10148 computeConstantRange(Cmp->getOperand(1), /* ForSigned */ false,
10149 UseInstrInfo, AC, I, DT, Depth + 1);
10150 CR = CR.intersectWith(
10151 ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(), RHS));
10152 }
10153 }
10154
10155 return CR;
10156}
10157
10158static void
10160 function_ref<void(Value *)> InsertAffected) {
10161 assert(V != nullptr);
10162 if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10163 InsertAffected(V);
10164 } else if (auto *I = dyn_cast<Instruction>(V)) {
10165 InsertAffected(V);
10166
10167 // Peek through unary operators to find the source of the condition.
10168 Value *Op;
10170 if (isa<Instruction>(Op) || isa<Argument>(Op))
10171 InsertAffected(Op);
10172 }
10173 }
10174}
10175
10177 Value *Cond, bool IsAssume, function_ref<void(Value *)> InsertAffected) {
10178 auto AddAffected = [&InsertAffected](Value *V) {
10179 addValueAffectedByCondition(V, InsertAffected);
10180 };
10181
10182 auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS, Value *RHS) {
10183 if (IsAssume) {
10184 AddAffected(LHS);
10185 AddAffected(RHS);
10186 } else if (match(RHS, m_Constant()))
10187 AddAffected(LHS);
10188 };
10189
10190 SmallVector<Value *, 8> Worklist;
10192 Worklist.push_back(Cond);
10193 while (!Worklist.empty()) {
10194 Value *V = Worklist.pop_back_val();
10195 if (!Visited.insert(V).second)
10196 continue;
10197
10198 CmpPredicate Pred;
10199 Value *A, *B, *X;
10200
10201 if (IsAssume) {
10202 AddAffected(V);
10203 if (match(V, m_Not(m_Value(X))))
10204 AddAffected(X);
10205 }
10206
10207 if (match(V, m_LogicalOp(m_Value(A), m_Value(B)))) {
10208 // assume(A && B) is split to -> assume(A); assume(B);
10209 // assume(!(A || B)) is split to -> assume(!A); assume(!B);
10210 // Finally, assume(A || B) / assume(!(A && B)) generally don't provide
10211 // enough information to be worth handling (intersection of information as
10212 // opposed to union).
10213 if (!IsAssume) {
10214 Worklist.push_back(A);
10215 Worklist.push_back(B);
10216 }
10217 } else if (match(V, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
10218 AddCmpOperands(A, B);
10219
10220 bool HasRHSC = match(B, m_ConstantInt());
10221 if (ICmpInst::isEquality(Pred)) {
10222 if (HasRHSC) {
10223 Value *Y;
10224 // (X & C) or (X | C) or (X ^ C).
10225 // (X << C) or (X >>_s C) or (X >>_u C).
10228 AddAffected(X);
10229 else if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10230 match(A, m_Or(m_Value(X), m_Value(Y)))) {
10231 AddAffected(X);
10232 AddAffected(Y);
10233 }
10234 }
10235 } else {
10236 if (HasRHSC) {
10237 // Handle (A + C1) u< C2, which is the canonical form of
10238 // A > C3 && A < C4.
10240 AddAffected(X);
10241
10242 if (ICmpInst::isUnsigned(Pred)) {
10243 Value *Y;
10244 // X & Y u> C -> X >u C && Y >u C
10245 // X | Y u< C -> X u< C && Y u< C
10246 // X nuw+ Y u< C -> X u< C && Y u< C
10247 if (match(A, m_And(m_Value(X), m_Value(Y))) ||
10248 match(A, m_Or(m_Value(X), m_Value(Y))) ||
10249 match(A, m_NUWAdd(m_Value(X), m_Value(Y)))) {
10250 AddAffected(X);
10251 AddAffected(Y);
10252 }
10253 // X nuw- Y u> C -> X u> C
10254 if (match(A, m_NUWSub(m_Value(X), m_Value())))
10255 AddAffected(X);
10256 }
10257 }
10258
10259 // Handle icmp slt/sgt (bitcast X to int), 0/-1, which is supported
10260 // by computeKnownFPClass().
10262 if (Pred == ICmpInst::ICMP_SLT && match(B, m_Zero()))
10263 InsertAffected(X);
10264 else if (Pred == ICmpInst::ICMP_SGT && match(B, m_AllOnes()))
10265 InsertAffected(X);
10266 }
10267 }
10268
10269 if (HasRHSC && match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10270 AddAffected(X);
10271 } else if (match(V, m_FCmp(Pred, m_Value(A), m_Value(B)))) {
10272 AddCmpOperands(A, B);
10273
10274 // fcmp fneg(x), y
10275 // fcmp fabs(x), y
10276 // fcmp fneg(fabs(x)), y
10277 if (match(A, m_FNeg(m_Value(A))))
10278 AddAffected(A);
10279 if (match(A, m_FAbs(m_Value(A))))
10280 AddAffected(A);
10281
10282 } else if (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
10283 m_Value()))) {
10284 // Handle patterns that computeKnownFPClass() support.
10285 AddAffected(A);
10286 }
10287 }
10288}
AMDGPU Register Bank Select
Rewrite undef for PHI
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Function Alias Analysis Results
This file contains the simple types necessary to represent the attributes associated with functions a...
BlockVerifier::State From
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1315
bool End
Definition: ELF_riscv.cpp:480
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
Hexagon Common GEP
static MaybeAlign getAlign(Value *Ptr)
Definition: IRBuilder.cpp:500
Module.h This file contains the declarations for the Module class.
static bool hasNoUnsignedWrap(BinaryOperator &I)
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
mir Rename Register Operands
This file contains the declarations for metadata subclasses.
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
#define P(N)
R600 Clause Merge
const SmallVectorImpl< MachineOperand > & Cond
static bool mayHaveSideEffects(MachineInstr &MI)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
This file defines the SmallPtrSet class.
This file defines the SmallSet class.
This file defines the SmallVector class.
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition: VPlanSLP.cpp: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 isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW)
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, const KnownBits &KnownVal)
static bool rangeMetadataExcludesValue(const MDNode *Ranges, const APInt &Value)
Does the 'Range' metadata (which must be a valid MD_range operand list) ensure that the value it's at...
static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static bool inputDenormalIsIEEE(const Function &F, const Type *Ty)
Return true if it's possible to assume IEEE treatment of input denormals in F for Val.
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
static bool isNonEqualShl(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if V2 == V1 << C, where V1 is known non-zero, C is not 0 and the shift is nuw or nsw.
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
static std::tuple< Value *, FPClassTest, FPClassTest > exactClass(Value *V, FPClassTest M)
Return the return value for fcmpImpliesClass for a compare that produces an exact class test.
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
static bool isKnownNonEqual(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if it is known that V1 != V2.
static bool isKnownNonZero(const Value *V, const APInt &DemandedElts, const SimplifyQuery &Q, unsigned Depth)
Return true if the given value is known to be non-zero when defined.
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
UndefPoisonKind
static bool includesPoison(UndefPoisonKind Kind)
static bool isNonEqualMul(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if V2 == V1 * C, where V1 is known non-zero, C is not 0/1 and the multiplication is nuw o...
static SelectPatternResult matchFastFloatClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS)
Match clamp pattern for float types without care about NaNs or signed zeros.
static bool includesUndef(UndefPoisonKind Kind)
static std::optional< bool > isImpliedCondCommonOperandWithCR(CmpPredicate LPred, const ConstantRange &LCR, CmpPredicate RPred, const ConstantRange &RCR)
Return true if "icmp LPred X, LCR" implies "icmp RPred X, RCR" is true.
static bool isPowerOfTwoRecurrence(const PHINode *PN, bool OrZero, unsigned Depth, SimplifyQuery &Q)
Try to detect a recurrence that the value of the induction variable is always a power of two (or zero...
static bool isModifyingBinopOfNonZero(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return true if V1 == (binop V2, X), where X is known non-zero.
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, FastMathFlags FMF, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
static uint64_t GetStringLengthH(const Value *V, SmallPtrSetImpl< const PHINode * > &PHIs, unsigned CharSize)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
static std::optional< bool > isImpliedCondAndOr(const Instruction *LHS, CmpPredicate RHSPred, const Value *RHSOp0, const Value *RHSOp1, const DataLayout &DL, bool LHSIsTrue, unsigned Depth)
Return true if LHS implies RHS is true.
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, unsigned Depth, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext)
static std::optional< bool > isImpliedCondICmps(const ICmpInst *LHS, CmpPredicate RPred, const Value *R0, const Value *R1, const DataLayout &DL, bool LHSIsTrue)
Return true if LHS implies RHS (expanded to its components as "R0 RPred R1") is true.
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
static void computeKnownBitsAddSub(bool Add, const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &KnownOut, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q)
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
static SelectPatternResult matchMinMaxOfMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TVal, Value *FVal, unsigned Depth)
Recognize variations of: a < c ? min(a,b) : min(b,c) ==> min(min(a,b),min(b,c))
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth, const SimplifyQuery &Q)
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
static void computeKnownBits(const Value *V, const APInt &DemandedElts, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
Determine which bits of V are known to be either zero or one and return them in the Known bit set.
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
static Value * getNotValue(Value *V)
If the input value is the result of a 'not' op, constant integer, or vector splat of a constant integ...
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, unsigned Depth, const SimplifyQuery &SQ, bool Invert)
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW, bool NUW, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q)
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut)
static SelectPatternResult matchClamp(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal)
Recognize variations of: CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
static SelectPatternResult matchMinMax(CmpInst::Predicate Pred, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Match non-obvious integer minimum and maximum sequences.
static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, const SimplifyQuery &Q)
Test whether a GEP's result is known to be non-null.
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y)
static std::optional< std::pair< Value *, Value * > > getInvertibleOperands(const Operator *Op1, const Operator *Op2)
If the pair of operators are the same invertible function, return the the operands of the function co...
static void computeKnownBitsFromShiftOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, KnownBits &Known2, unsigned Depth, const SimplifyQuery &Q, function_ref< KnownBits(const KnownBits &, const KnownBits &, bool)> KF)
Compute known bits from a shift operator, including those with a non-constant shift amount.
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, unsigned Depth, const SimplifyQuery &Q)
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
static std::optional< bool > isImpliedCondOperands(CmpInst::Predicate Pred, const Value *ALHS, const Value *ARHS, const Value *BLHS, const Value *BRHS)
Return true if "icmp Pred BLHS BRHS" is true whenever "icmp Pred ALHS ARHS" is true.
static unsigned ComputeNumSignBitsImpl(const Value *V, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Return the number of times the sign bit of the register is replicated into the other bits.
static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW)
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
static bool isImpliedToBeAPowerOfTwoFromCond(const Value *V, bool OrZero, const Value *Cond, bool CondIsTrue)
Return true if we can infer that V is known to be a power of 2 from dominating condition Cond (e....
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth, const SimplifyQuery &Q)
Value * RHS
Value * LHS
bool isNegative() const
Definition: APFloat.h:1445
bool isFinite() const
Definition: APFloat.h:1450
bool isNaN() const
Definition: APFloat.h:1443
APInt bitcastToAPInt() const
Definition: APFloat.h:1351
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition: APFloat.h:1140
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition: APFloat.h:1100
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition: APFloat.cpp:5450
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition: APFloat.h:1081
bool isSmallestNormalized() const
Definition: APFloat.h:1465
Class for arbitrary precision integers.
Definition: APInt.h:78
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition: APInt.cpp:1547
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition: APInt.h:234
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition: APInt.h:1407
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition: APInt.h:423
uint64_t getZExtValue() const
Get zero extended value.
Definition: APInt.h:1520
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition: APInt.h:1392
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition: APInt.h:1386
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition: APInt.cpp:1007
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition: APInt.h:206
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition: APInt.h:1330
unsigned ceilLogBase2() const
Definition: APInt.h:1742
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition: APInt.h:1201
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition: APInt.h:371
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition: APInt.h:1182
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition: APInt.h:380
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition: APInt.cpp:1640
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition: APInt.h:1468
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition: APInt.h:1111
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition: APInt.h:209
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition: APInt.h:216
bool isNegative() const
Determine sign of this APInt.
Definition: APInt.h:329
bool intersects(const APInt &RHS) const
This operation tests if there are any pairs of corresponding bits between this APInt and RHS that are...
Definition: APInt.h:1249
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition: APInt.cpp:1618
void clearAllBits()
Set every bit to 0.
Definition: APInt.h:1397
APInt reverseBits() const
Definition: APInt.cpp:741
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition: APInt.h:1166
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition: APInt.h:1607
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition: APInt.h:219
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition: APInt.cpp:1015
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition: APInt.h:356
unsigned logBase2() const
Definition: APInt.h:1739
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition: APInt.h:827
void setAllBits()
Set every bit to 1.
Definition: APInt.h:1319
bool getBoolValue() const
Convert APInt to a boolean value.
Definition: APInt.h:471
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition: APInt.h:405
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition: APInt.h:334
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition: APInt.h:1150
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition: APInt.h:873
APInt byteSwap() const
Definition: APInt.cpp:719
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition: APInt.h:1130
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition: APInt.h:296
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition: APInt.h:200
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition: APInt.h:1389
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition: APInt.h:1237
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition: APInt.h:286
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition: APInt.h:239
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition: APInt.h:858
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition: APInt.h:851
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition: APInt.h:1221
void clearSignBit()
Set the sign bit to 0.
Definition: APInt.h:1431
an instruction to allocate memory on the stack
Definition: Instructions.h:63
This class represents an incoming formal argument to a Function.
Definition: Argument.h:31
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
iterator end() const
Definition: ArrayRef.h:157
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:168
iterator begin() const
Definition: ArrayRef.h:156
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:163
ArrayRef< T > slice(size_t N, size_t M) const
slice(n, m) - Chop off the first N elements of the array, and keep M elements in the array.
Definition: ArrayRef.h:198
Class to represent array types.
Definition: DerivedTypes.h:395
Type * getElementType() const
Definition: DerivedTypes.h:408
This represents the llvm.assume intrinsic.
A cache of @llvm.assume calls within a function.
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
Definition: Attributes.cpp:466
unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
Definition: Attributes.cpp:460
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition: Attributes.h:208
bool isSingleEdge() const
Check if this is the only edge between Start and End.
Definition: Dominators.cpp:51
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
iterator end()
Definition: BasicBlock.h:461
iterator begin()
Instruction iterator methods.
Definition: BasicBlock.h:448
InstListType::const_iterator const_iterator
Definition: BasicBlock.h:178
const Instruction * getFirstNonPHI() const
Returns a pointer to the first instruction in this block that is not a PHINode instruction.
Definition: BasicBlock.cpp:367
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition: BasicBlock.cpp:459
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition: BasicBlock.cpp:489
const Function * getParent() const
Return the enclosing method, or null if none.
Definition: BasicBlock.h:219
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:177
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition: BasicBlock.h:239
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
BinaryOps getOpcode() const
Definition: InstrTypes.h:370
Conditional or Unconditional Branch instruction.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1112
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition: InstrTypes.h:1341
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
bool isIndirectCall() const
Return true if the callsite is an indirect call.
bool onlyReadsMemory(unsigned OpNo) const
Definition: InstrTypes.h:1721
Value * getCalledOperand() const
Definition: InstrTypes.h:1334
Value * getArgOperand(unsigned i) const
Definition: InstrTypes.h:1286
unsigned arg_size() const
Definition: InstrTypes.h:1284
This class represents a function call, abstracting a target machine's calling convention.
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:444
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:661
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:673
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:676
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:690
@ ICMP_SLT
signed less than
Definition: InstrTypes.h:702
@ ICMP_SLE
signed less or equal
Definition: InstrTypes.h:703
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition: InstrTypes.h:679
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition: InstrTypes.h:688
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition: InstrTypes.h:677
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition: InstrTypes.h:678
@ ICMP_UGE
unsigned greater or equal
Definition: InstrTypes.h:697
@ ICMP_UGT
unsigned greater than
Definition: InstrTypes.h:696
@ ICMP_SGT
signed greater than
Definition: InstrTypes.h:700
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition: InstrTypes.h:687
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:681
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:684
@ ICMP_ULT
unsigned less than
Definition: InstrTypes.h:698
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition: InstrTypes.h:685
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition: InstrTypes.h:680
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:682
@ ICMP_SGE
signed greater or equal
Definition: InstrTypes.h:701
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:689
@ ICMP_ULE
unsigned less or equal
Definition: InstrTypes.h:699
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition: InstrTypes.h:686
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:675
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:683
bool isSigned() const
Definition: InstrTypes.h:928
static bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:825
bool isTrueWhenEqual() const
This is just a convenience.
Definition: InstrTypes.h:940
bool isFPPredicate() const
Definition: InstrTypes.h:780
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:787
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:763
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition: InstrTypes.h:891
bool isIntPredicate() const
Definition: InstrTypes.h:781
static bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
bool isUnsigned() const
Definition: InstrTypes.h:934
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition: CmpPredicate.h:22
static std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
bool hasSameSign() const
Query samesign information, for optimizations.
Definition: CmpPredicate.h:42
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:696
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition: Constants.h:587
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition: Constants.h:662
uint64_t getElementAsInteger(unsigned i) const
If this is a sequential container of integers (of any size), return the specified element in the low ...
Definition: Constants.cpp:3114
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition: Constants.h:770
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition: Constants.cpp:2637
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2321
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition: Constants.cpp:2279
ConstantFP - Floating Point Values [float, double].
Definition: Constants.h:271
This is the shared class of boolean and integer constants.
Definition: Constants.h:83
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:866
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition: Constants.h:208
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:157
This class represents a range of values.
Definition: ConstantRange.h:47
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
bool isAllNegative() const
Return true if all values in this range are negative.
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
KnownBits toKnownBits() const
Return known bits for values in this range.
APInt getUnsignedMin() const
Return the smallest unsigned value contained in the ConstantRange.
bool icmp(CmpInst::Predicate Pred, const ConstantRange &Other) const
Does the predicate Pred hold between ranges this and Other? NOTE: false does not mean that inverse pr...
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
bool isAllNonNegative() const
Return true if all values in this range are non-negative.
static ConstantRange makeAllowedICmpRegion(CmpInst::Predicate Pred, const ConstantRange &Other)
Produce the smallest range such that all values that may satisfy the given predicate with any value c...
ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
static ConstantRange makeExactICmpRegion(CmpInst::Predicate Pred, const APInt &Other)
Produce the exact range such that all values in the returned range satisfy the given predicate with a...
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
Definition: ConstantRange.h:84
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
This is an important base class in LLVM.
Definition: Constant.h:42
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Definition: Constants.cpp:784
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
Definition: Constants.cpp:1708
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition: Constants.cpp:373
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition: Constants.cpp:435
bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition: Constants.cpp:76
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition: Constants.cpp:90
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
bool isLittleEndian() const
Layout endianness...
Definition: DataLayout.h:197
const StructLayout * getStructLayout(StructType *Ty) const
Returns a StructLayout object, indicating the alignment of the struct, its size, and the offsets of i...
Definition: DataLayout.cpp:709
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
Definition: DataLayout.cpp:754
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition: DataLayout.cpp:743
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition: DataLayout.h:617
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
DomTreeNodeBase * getIDom() const
NodeT * getBlock() const
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
bool dominates(const BasicBlock *BB, const Use &U) const
Return true if the (end of the) basic block BB dominates the use U.
Definition: Dominators.cpp:122
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static Type * getIndexedType(Type *Agg, ArrayRef< unsigned > Idxs)
Returns the type of the element that would be extracted with an extractvalue instruction with the spe...
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition: Operator.h:205
Convenience struct for specifying and reasoning about fast-math flags.
Definition: FMF.h:20
bool noSignedZeros() const
Definition: FMF.h:68
void setNoSignedZeros(bool B=true)
Definition: FMF.h:85
bool noNaNs() const
Definition: FMF.h:66
const BasicBlock & getEntryBlock() const
Definition: Function.h:809
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition: Function.cpp:807
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:933
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition: Globals.cpp:130
Type * getValueType() const
Definition: GlobalValue.h:297
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
This instruction compares its operands according to the predicate given to the constructor.
CmpPredicate getSwappedCmpPredicate() const
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
bool isEquality() const
Return true if this predicate is either EQ or NE.
static std::optional< bool > isImpliedByMatchingCmp(CmpPredicate Pred1, CmpPredicate Pred2)
Determine if Pred1 implies Pred2 is true, false, or if nothing can be inferred about the implication,...
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
This instruction inserts a struct field of array element value into an aggregate value.
Value * getAggregateOperand()
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
bool isBinaryOp() const
Definition: Instruction.h:279
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition: Instruction.cpp:94
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
const Function * getFunction() const
Return the function this instruction belongs to.
Definition: Instruction.cpp:72
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:274
bool isUnaryOp() const
Definition: Instruction.h:278
const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Definition: Instruction.cpp:76
A wrapper class for inspecting calls to intrinsic functions.
Definition: IntrinsicInst.h:48
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
An instruction for reading from memory.
Definition: Instructions.h:176
Value * getPointerOperand()
Definition: Instructions.h:255
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:211
bool isLoopHeader(const BlockT *BB) const
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Represents a single loop in the control flow graph.
Definition: LoopInfo.h:39
Metadata node.
Definition: Metadata.h:1073
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition: Operator.h:32
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition: Operator.h:42
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition: Operator.h:77
iterator_range< const_block_iterator > blocks() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition: Constants.cpp:1878
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed.
Definition: Operator.h:155
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition: Operator.h:174
This class represents the LLVM 'select' instruction.
const Value * getFalseValue() const
const Value * getCondition() const
const Value * getTrueValue() const
This instruction constructs a fixed permutation of two input vectors.
VectorType * getType() const
Overload to return most specific vector type.
static void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
size_type size() const
Definition: SmallPtrSet.h:94
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition: SmallPtrSet.h:363
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition: SmallPtrSet.h:452
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
Definition: SmallPtrSet.h:384
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition: SmallPtrSet.h:519
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:132
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:175
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:181
bool empty() const
Definition: SmallVector.h:81
size_t size() const
Definition: SmallVector.h:78
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:573
void reserve(size_type N)
Definition: SmallVector.h:663
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition: SmallVector.h:683
void push_back(const T &Elt)
Definition: SmallVector.h:413
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1196
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition: DataLayout.h:567
TypeSize getElementOffset(unsigned Idx) const
Definition: DataLayout.h:596
Class to represent struct types.
Definition: DerivedTypes.h:218
unsigned getNumElements() const
Random access to the elements.
Definition: DerivedTypes.h:365
Type * getElementType(unsigned N) const
Definition: DerivedTypes.h:366
Provides information about what library functions are available for the current target.
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
unsigned getIntegerBitWidth() const
const fltSemantics & getFltSemantics() const
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:270
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition: Type.h:243
bool isPointerTy() const
True if this is an instance of PointerType.
Definition: Type.h:264
uint64_t getArrayNumElements() const
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition: Type.h:310
static IntegerType * getInt16Ty(LLVMContext &C)
static IntegerType * getInt8Ty(LLVMContext &C)
bool isIEEE() const
Return whether the type is IEEE compatible, as defined by the eponymous method in APFloat.
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition: Type.h:267
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition: Type.h:252
static IntegerType * getInt32Ty(LLVMContext &C)
static IntegerType * getInt64Ty(LLVMContext &C)
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition: Type.h:237
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition: Type.h:355
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition: Constants.cpp:1859
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
User * getUser() const
Returns the User that contains this Use.
Definition: Use.h:72
unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition: Use.cpp:31
op_range operands()
Definition: User.h:288
Value * getOperand(unsigned i) const
Definition: User.h:228
unsigned getNumOperands() const
Definition: User.h:250
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
const Value * stripAndAccumulateInBoundsConstantOffsets(const DataLayout &DL, APInt &Offset) const
This is a wrapper around stripAndAccumulateConstantOffsets with the in-bounds requirement set to fals...
Definition: Value.h:740
iterator_range< user_iterator > users()
Definition: Value.h:421
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition: WithCache.h:58
PointerType getValue() const
Definition: WithCache.h:56
Represents an op.with.overflow intrinsic.
constexpr ScalarTy getFixedValue() const
Definition: TypeSize.h:202
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition: TypeSize.h:171
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition: TypeSize.h:168
An efficient, type-erasing, non-owning reference to a callable.
TypeSize getSequentialElementStride(const DataLayout &DL) const
const ParentTy * getParent() const
Definition: ilist_node.h:32
self_iterator getIterator()
Definition: ilist_node.h:132
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define UINT64_MAX
Definition: DataTypes.h:77
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition: PatternMatch.h:524
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition: PatternMatch.h:664
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition: PatternMatch.h:619
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition: PatternMatch.h:165
BinaryOp_match< LHS, RHS, Instruction::And, true > m_c_And(const LHS &L, const RHS &R)
Matches an And with LHS and RHS in either order.
cst_pred_ty< is_power2_or_zero > m_Power2OrZero()
Match an integer or vector of 0 or power-of-2 values.
Definition: PatternMatch.h:652
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
bool match(Val *V, const Pattern &P)
Definition: PatternMatch.h:49
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition: PatternMatch.h:764
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition: PatternMatch.h:885
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
CmpClass_match< LHS, RHS, ICmpInst, true > m_c_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Matches an ICmp with a predicate over LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
Definition: PatternMatch.h:560
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition: PatternMatch.h:168
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition: PatternMatch.h:592
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmin_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmin_pred_ty > > m_OrdOrUnordFMin(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point minimum function.
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
Definition: PatternMatch.h:832
BinaryOp_match< LHS, RHS, Instruction::Xor, true > m_c_Xor(const LHS &L, const RHS &R)
Matches an Xor with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
deferredval_ty< Value > m_Deferred(Value *const &V)
Like m_Specific(), but works if the specific value to match is determined as part of the same match()...
Definition: PatternMatch.h:903
cst_pred_ty< is_zero_int > m_ZeroInt()
Match an integer 0 or a vector with all elements equal to 0.
Definition: PatternMatch.h:599
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition: PatternMatch.h:67
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty, true > m_c_SMin(const LHS &L, const RHS &R)
Matches an SMin with LHS and RHS in either order.
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition: PatternMatch.h:864
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty, true > m_c_UMax(const LHS &L, const RHS &R)
Matches a UMax with LHS and RHS in either order.
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty, true > m_c_UMin(const LHS &L, const RHS &R)
Matches a UMin with LHS and RHS in either order.
BinaryOp_match< LHS, RHS, Instruction::Add, true > m_c_Add(const LHS &L, const RHS &R)
Matches a Add with LHS and RHS in either order.
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
Definition: PatternMatch.h:322
match_combine_or< BinaryOp_match< LHS, RHS, Instruction::Add >, DisjointOr_match< LHS, RHS > > m_AddLike(const LHS &L, const RHS &R)
Match either "add" or "or disjoint".
match_combine_or< MaxMin_match< FCmpInst, LHS, RHS, ofmax_pred_ty >, MaxMin_match< FCmpInst, LHS, RHS, ufmax_pred_ty > > m_OrdOrUnordFMax(const LHS &L, const RHS &R)
Match an 'ordered' or 'unordered' floating point maximum function.
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty, true > m_c_SMax(const LHS &L, const RHS &R)
Matches an SMax with LHS and RHS in either order.
VScaleVal_match m_VScale()
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
apint_match m_APInt(const APInt *&Res)
Match a ConstantInt or splatted ConstantVector, binding the specified pointer to the contained APInt.
Definition: PatternMatch.h:299
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap >, DisjointOr_match< LHS, RHS > > m_NSWAddLike(const LHS &L, const RHS &R)
Match either "add nsw" or "or disjoint".
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition: PatternMatch.h:92
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
apfloat_match m_APFloat(const APFloat *&Res)
Match a ConstantFP or splatted ConstantVector, binding the specified pointer to the contained APFloat...
Definition: PatternMatch.h:316
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
Definition: PatternMatch.h:189
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
Definition: PatternMatch.h:582
BinaryOp_match< cst_pred_ty< is_all_ones >, ValTy, Instruction::Xor, true > m_Not(const ValTy &V)
Matches a 'Not' as 'xor V, -1' or 'xor -1, V'.
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition: PatternMatch.h:612
BinaryOp_match< LHS, RHS, Instruction::Or, true > m_c_Or(const LHS &L, const RHS &R)
Matches an Or with LHS and RHS in either order.
match_combine_or< OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap >, DisjointOr_match< LHS, RHS > > m_NUWAddLike(const LHS &L, const RHS &R)
Match either "add nuw" or "or disjoint".
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition: PatternMatch.h:239
static unsigned decodeVSEW(unsigned VSEW)
unsigned getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul)
static constexpr unsigned RVVBitsPerBlock
initializer< Ty > init(const Ty &Val)
Definition: CommandLine.h:443
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool haveNoCommonBitsSet(const WithCache< const Value * > &LHSCache, const WithCache< const Value * > &RHSCache, const SimplifyQuery &SQ)
Return true if LHS and RHS have no common bits set.
bool mustExecuteUBIfPoisonOnPathTo(Instruction *Root, Instruction *OnPathTo, DominatorTree *DT)
Return true if undefined behavior would provable be executed on the path to OnPathTo if Root produced...
Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
@ Offset
Definition: DWP.cpp:480
@ Length
Definition: DWP.cpp:480
OverflowResult
@ NeverOverflows
Never overflows.
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
@ MayOverflow
May or may not overflow.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1739
bool isValidAssumeForContext(const Instruction *I, const Instruction *CxtI, const DominatorTree *DT=nullptr, bool AllowEphemerals=false)
Return true if it is valid to use the assumptions provided by an assume intrinsic,...
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition: STLExtras.h:1697
bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
bool mustTriggerUB(const Instruction *I, const SmallPtrSetImpl< const Value * > &KnownPoison)
Return true if the given instruction must trigger undefined behavior when I is executed with any oper...
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition: ScopeExit.h:59
bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
bool isSignBitCheck(ICmpInst::Predicate Pred, const APInt &RHS, bool &TrueIfSigned)
Given an exploded icmp instruction, return true if the comparison only checks the sign bit.
const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
void getGuaranteedNonPoisonOps(const Instruction *I, SmallVectorImpl< const Value * > &Ops)
Insert operands of I into Ops such that I will trigger undefined behavior if I is executed and that o...
bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
bool isDereferenceableAndAlignedPointer(const Value *V, Type *Ty, Align Alignment, const DataLayout &DL, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr)
Returns true if V is always a dereferenceable pointer with alignment greater or equal than requested.
Definition: Loads.cpp:216
bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
std::pair< Intrinsic::ID, bool > canConvertToMinOrMaxIntrinsic(ArrayRef< Value * > VL)
Check if the values in VL are select instructions that can be converted to a min or max (vector) intr...
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
bool getConstantDataArrayInfo(const Value *V, ConstantDataArraySlice &Slice, unsigned ElementSize, uint64_t Offset=0)
Returns true if the value V is a pointer into a ConstantDataArray.
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition: bit.h:317
bool isGuaranteedToExecuteForEveryIteration(const Instruction *I, const Loop *L)
Return true if this function can prove that the instruction I is executed for every iteration of the ...
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition: STLExtras.h:2115
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
bool isKnownToBeAPowerOfTwo(const Value *V, const DataLayout &DL, bool OrZero=false, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given value is known to have exactly one bit set when defined.
bool mustSuppressSpeculation(const LoadInst &LI)
Return true if speculation of the given load must be suppressed to avoid ordering or interfering with...
Definition: Loads.cpp:371
constexpr bool isPowerOf2_64(uint64_t Value)
Return true if the argument is a power of two > 0 (64 bit edition.)
Definition: MathExtras.h:298
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:348
bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be undef, but may be poison.
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
ConstantRange computeConstantRange(const Value *V, bool ForSigned, bool UseInstrInfo=true, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Determine the possible constant range of an integer or vector of integer value.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition: bit.h:215
bool isOverflowIntrinsicNoWrap(const WithOverflowInst *WO, const DominatorTree &DT)
Returns true if the arithmetic part of the WO 's result is used only along the paths control dependen...
RetainedKnowledge getKnowledgeValidInContext(const Value *V, ArrayRef< Attribute::AttrKind > AttrKinds, const Instruction *CtxI, const DominatorTree *DT=nullptr, AssumptionCache *AC=nullptr)
Return a valid Knowledge associated to the Value V if its Attribute kind is in AttrKinds and the know...
RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
bool matchSimpleRecurrence(const PHINode *P, BinaryOperator *&BO, Value *&Start, Value *&Step)
Attempt to match a simple first order recurrence cycle of the form: iv = phi Ty [Start,...
bool isSafeToSpeculativelyExecuteWithOpcode(unsigned Opcode, const Instruction *Inst, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
This returns the same result as isSafeToSpeculativelyExecute if Opcode is the actual opcode of Inst.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1746
KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I, const KnownBits &KnownLHS, const KnownBits &KnownRHS, unsigned Depth, const SimplifyQuery &SQ)
Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
bool getShuffleDemandedElts(int SrcWidth, ArrayRef< int > Mask, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS, bool AllowUndefElts=false)
Transform a shuffle mask's output demanded element mask into demanded element masks for the 2 operand...
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition: MathExtras.h:342
bool isGuard(const User *U)
Returns true iff U has semantics of a guard expressed in a form of call of llvm.experimental....
Definition: GuardUtils.cpp:18
int countl_zero(T Val)
Count number of 0's from the most significant bit to the least stopping at the first 1.
Definition: bit.h:281
SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
constexpr unsigned MaxAnalysisRecursionDepth
Definition: ValueTracking.h:44
bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
void getGuaranteedWellDefinedOps(const Instruction *I, SmallVectorImpl< const Value * > &Ops)
Insert operands of I into Ops such that I will trigger undefined behavior if I is executed and that o...
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
std::tuple< Value *, FPClassTest, FPClassTest > fcmpImpliesClass(CmpInst::Predicate Pred, const Function &F, Value *LHS, Value *RHS, bool LookThroughSrc=true)
Compute the possible floating-point classes that LHS could be based on fcmp \Pred LHS,...
SelectPatternFlavor
Specific patterns of select instructions we can match.
@ SPF_ABS
Floating point maxnum.
@ SPF_NABS
Absolute value.
@ SPF_FMAXNUM
Floating point minnum.
@ SPF_UMIN
Signed minimum.
@ SPF_UMAX
Signed maximum.
@ SPF_SMAX
Unsigned minimum.
@ SPF_UNKNOWN
@ SPF_FMINNUM
Unsigned maximum.
bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
void adjustKnownBitsForSelectArm(KnownBits &Known, Value *Cond, Value *Arm, bool Invert, unsigned Depth, const SimplifyQuery &Q)
Adjust Known for the given select Arm to include information from the select Cond.
bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
bool programUndefinedIfPoison(const Instruction *Inst)
SelectPatternResult matchSelectPattern(Value *V, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Pattern match integer [SU]MIN, [SU]MAX and ABS idioms, returning the kind and providing the out param...
bool NullPointerIsDefined(const Function *F, unsigned AS=0)
Check whether null pointer dereferencing is considered undefined behavior for a given function or an ...
Definition: Function.cpp:1187
bool programUndefinedIfUndefOrPoison(const Instruction *Inst)
Return true if this function can prove that if Inst is executed and yields a poison value or undef bi...
bool isSafeToSpeculativelyExecute(const Instruction *I, const Instruction *CtxI=nullptr, AssumptionCache *AC=nullptr, const DominatorTree *DT=nullptr, const TargetLibraryInfo *TLI=nullptr, bool UseVariableInfo=true)
Return true if the instruction does not have any effects besides calculating the result and does not ...
FPClassTest inverse_fabs(FPClassTest Mask)
Return the test mask which returns true after fabs is applied to the value.
uint64_t GetStringLength(const Value *V, unsigned CharSize=8)
If we can compute the length of the string pointed to by the specified pointer, return 'len+1'.
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
ConstantRange getVScaleRange(const Function *F, unsigned BitWidth)
Determine the possible constant range of vscale with the given bit width, based on the vscale_range f...
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
bool isKnownNonZero(const Value *V, const SimplifyQuery &Q, unsigned Depth=0)
Return true if the given value is known to be non-zero when defined.
constexpr int PoisonMaskElem
bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
bool isKnownNonEqual(const Value *V1, const Value *V2, const DataLayout &DL, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return true if the given values are known to be non-equal when defined.
@ Add
Sum of integers.
ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
void computeKnownBits(const Value *V, KnownBits &Known, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Determine which bits of V are known to be either zero or one and return them in the KnownZero/KnownOn...
DWARFExpression::Operation Op
bool isGuaranteedNotToBeUndefOrPoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Return true if this function can prove that V does not have undef bits and is never poison.
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:217
SelectPatternResult matchDecomposedSelectPattern(CmpInst *CmpI, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, Instruction::CastOps *CastOp=nullptr, unsigned Depth=0)
Determine the pattern that a select with the given compare as its predicate and given values as its t...
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
gep_type_iterator gep_type_begin(const User *GEP)
std::pair< Value *, FPClassTest > fcmpToClassTest(CmpInst::Predicate Pred, const Function &F, Value *LHS, Value *RHS, bool LookThroughSrc=true)
Returns a pair of values, which if passed to llvm.is.fpclass, returns the same result as an fcmp with...
Value * isBytewiseValue(Value *V, const DataLayout &DL)
If the specified value can be set by repeating the same byte in memory, return the i8 value that it i...
std::optional< std::pair< CmpPredicate, Constant * > > getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred, Constant *C)
Convert an integer comparison with a constant RHS into an equivalent form with the strictness flipped...
void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=6)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition: STLExtras.h:1903
unsigned ComputeNumSignBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr, bool UseInstrInfo=true)
Return the number of times the sign bit of the register is replicated into the other bits.
OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
std::optional< bool > isImpliedByDomCondition(const Value *Cond, const Instruction *ContextI, const DataLayout &DL)
Return the boolean condition value in the context of the given instruction if it is known based on do...
bool isGEPBasedOnPointerToString(const GEPOperator *GEP, unsigned CharSize=8)
Returns true if the GEP is based on a pointer to a string (array of.
bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC=nullptr, const Instruction *CtxI=nullptr, const DominatorTree *DT=nullptr, unsigned Depth=0)
Returns true if V cannot be poison, but may be undef.
KnownFPClass computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, unsigned Depth, const SimplifyQuery &SQ)
Determine which floating-point classes are valid for V, and return them in KnownFPClass bit sets.
void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
Value * FindInsertedValue(Value *V, ArrayRef< unsigned > idx_range, std::optional< BasicBlock::iterator > InsertBefore=std::nullopt)
Given an aggregate and an sequence of indices, see if the scalar value indexed is already around as a...
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
unsigned ComputeMaxSignificantBits(const Value *Op, const DataLayout &DL, unsigned Depth=0, AssumptionCache *AC=nullptr, const Instruction *CxtI=nullptr, const DominatorTree *DT=nullptr)
Get the upper bound on bit size for this Value Op as a signed integer.
bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
Definition: VectorUtils.cpp:46
bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
std::optional< bool > isImpliedCondition(const Value *LHS, const Value *RHS, const DataLayout &DL, bool LHSIsTrue=true, unsigned Depth=0)
Return true if RHS is known to be implied true by LHS.
void findValuesAffectedByCondition(Value *Cond, bool IsAssume, function_ref< void(Value *)> InsertAffected)
Call InsertAffected on all Values whose known bits / value may be affected by the condition Cond.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition: BitVector.h:860
static unsigned int semanticsPrecision(const fltSemantics &)
Definition: APFloat.cpp:315
static bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition: APFloat.cpp:356
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
SmallPtrSet< Value *, 4 > AffectedValues
Definition: SimplifyQuery.h:65
Represents offset+length into a ConstantDataArray.
uint64_t Length
Length of the slice.
uint64_t Offset
Slice starts at this Offset.
const ConstantDataArray * Array
ConstantDataArray pointer.
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
constexpr bool outputsAreZero() const
Return true if output denormals should be flushed to 0.
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
@ PositiveZero
Denormals are flushed to positive zero.
@ Dynamic
Denormals have unknown treatment.
@ IEEE
IEEE-754 denormal numbers preserved.
static constexpr DenormalMode getPositiveZero()
constexpr bool inputsAreZero() const
Return true if input denormals must be implicitly treated as 0.
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
static constexpr DenormalMode getIEEE()
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
Definition: SimplifyQuery.h:25
bool isExact(const BinaryOperator *Op) const
Definition: SimplifyQuery.h:48
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
Definition: SimplifyQuery.h:30
bool hasNoSignedZeros(const InstT *Op) const
Definition: SimplifyQuery.h:54
bool hasNoSignedWrap(const InstT *Op) const
Definition: SimplifyQuery.h:42
bool hasNoUnsignedWrap(const InstT *Op) const
Definition: SimplifyQuery.h:36
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition: KnownBits.h:293
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
Definition: KnownBits.cpp:765
static std::optional< bool > eq(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_EQ result.
Definition: KnownBits.cpp:488
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition: KnownBits.h:178
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition: KnownBits.cpp:909
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition: KnownBits.h:247
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition: KnownBits.cpp:211
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition: KnownBits.h:100
KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
Definition: KnownBits.cpp:1120
void makeNonNegative()
Make this value non-negative.
Definition: KnownBits.h:116
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition: KnownBits.cpp:774
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition: KnownBits.h:243
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition: KnownBits.h:234
static KnownBits ashr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for ashr(LHS, RHS).
Definition: KnownBits.cpp:428
static KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
Definition: KnownBits.cpp:768
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition: KnownBits.cpp:1049
bool isUnknown() const
Returns true if we don't know any bits.
Definition: KnownBits.h:65
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition: KnownBits.h:266
KnownBits blsmsk() const
Compute known bits for X ^ (X - 1), which has all bits up to and including the lowest set bit of X se...
Definition: KnownBits.cpp:1131
void makeNegative()
Make this value negative.
Definition: KnownBits.h:111
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition: KnownBits.h:153
bool hasConflict() const
Returns true if there is conflicting information.
Definition: KnownBits.h:50
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition: KnownBits.h:281
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition: KnownBits.h:85
unsigned getBitWidth() const
Get the bit width of this value.
Definition: KnownBits.h:43
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition: KnownBits.cpp:187
bool isConstant() const
Returns true if we know the value of all bits.
Definition: KnownBits.h:53
void resetAll()
Resets the known state of all bits.
Definition: KnownBits.h:73
KnownBits unionWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for either this or RHS or both.
Definition: KnownBits.h:313
static KnownBits lshr(const KnownBits &LHS, const KnownBits &RHS, bool ShAmtNonZero=false, bool Exact=false)
Compute known bits for lshr(LHS, RHS).
Definition: KnownBits.cpp:370
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition: KnownBits.h:103
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition: KnownBits.h:303
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition: KnownBits.h:172
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition: KnownBits.h:237
static KnownBits add(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from addition of LHS and RHS.
Definition: KnownBits.h:336
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition: KnownBits.h:188
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition: KnownBits.h:240
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition: KnownBits.h:137
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition: KnownBits.cpp:215
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition: KnownBits.cpp:901
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition: KnownBits.cpp:1066
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition: KnownBits.cpp:1009
static KnownBits computeForAddSub(bool Add, bool NSW, bool NUW, const KnownBits &LHS, const KnownBits &RHS)
Compute known bits resulting from adding LHS and RHS.
Definition: KnownBits.cpp:60
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition: KnownBits.cpp:953
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition: KnownBits.h:318
bool isNegative() const
Returns true if this value is known to be negative.
Definition: KnownBits.h:97
static KnownBits sub(const KnownBits &LHS, const KnownBits &RHS, bool NSW=false, bool NUW=false)
Compute knownbits resulting from subtraction of LHS and RHS.
Definition: KnownBits.h:342
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition: KnownBits.h:272
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition: KnownBits.h:91
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition: KnownBits.h:211
static KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
Definition: KnownBits.cpp:771
static KnownBits mul(const KnownBits &LHS, const KnownBits &RHS, bool NoUndefSelfMultiply=false)
Compute known bits resulting from multiplying LHS and RHS.
Definition: KnownBits.cpp:804
KnownBits anyext(unsigned BitWidth) const
Return known bits for an "any" extension of the value we're tracking, where we don't know anything ab...
Definition: KnownBits.h:159
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition: KnownBits.cpp:550
static std::optional< bool > sgt(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_SGT result.
Definition: KnownBits.cpp:526
static std::optional< bool > uge(const KnownBits &LHS, const KnownBits &RHS)
Determine if these known bits always give the same ICMP_UGE result.
Definition: KnownBits.cpp:512
static KnownBits shl(const KnownBits &LHS, const KnownBits &RHS, bool NUW=false, bool NSW=false, bool ShAmtNonZero=false)
Compute known bits for shl(LHS, RHS).
Definition: KnownBits.cpp:285
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition: KnownBits.cpp:205
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition: KnownBits.h:198
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition: KnownBits.h:59
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
static constexpr FPClassTest OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
void knownNot(FPClassTest RuleOut)
bool isKnownNeverZero() const
Return true if it's known this can never be a zero.
void copysign(const KnownFPClass &Sign)
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
bool isKnownNeverLogicalNegZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a negative zero.
bool isKnownNeverLogicalPosZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a positive zero.
void propagateCanonicalizingSrc(const KnownFPClass &Src, const Function &F, Type *Ty)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
void propagateDenormal(const KnownFPClass &Src, const Function &F, Type *Ty)
Propagate knowledge from a source value that could be a denormal or zero.
bool isUnknown() const
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
std::optional< bool > SignBit
std::nullopt if the sign bit is unknown, true if the sign bit is definitely set or false if the sign ...
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
bool isKnownNeverLogicalZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a zero.
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
void signBitMustBeOne()
Assume the sign bit is one.
void signBitMustBeZero()
Assume the sign bit is zero.
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.
Represent one information held inside an operand bundle of an llvm.assume.
SelectPatternFlavor Flavor
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
const DataLayout & DL
Definition: SimplifyQuery.h:71
SimplifyQuery getWithoutCondContext() const
const Instruction * CxtI
Definition: SimplifyQuery.h:75
const DominatorTree * DT
Definition: SimplifyQuery.h:73
SimplifyQuery getWithInstruction(const Instruction *I) const
AssumptionCache * AC
Definition: SimplifyQuery.h:74
const DomConditionCache * DC
Definition: SimplifyQuery.h:76
const InstrInfoQuery IIQ
Definition: SimplifyQuery.h:82
const CondContext * CC
Definition: SimplifyQuery.h:77