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