Movatterモバイル変換


[0]ホーム

URL:


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
14#include "llvm/Analysis/ValueTracking.h"
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"
20#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallSet.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/StringRef.h"
24#include "llvm/ADT/iterator_range.h"
25#include "llvm/Analysis/AliasAnalysis.h"
26#include "llvm/Analysis/AssumeBundleQueries.h"
27#include "llvm/Analysis/AssumptionCache.h"
28#include "llvm/Analysis/ConstantFolding.h"
29#include "llvm/Analysis/DomConditionCache.h"
30#include "llvm/Analysis/GuardUtils.h"
31#include "llvm/Analysis/InstructionSimplify.h"
32#include "llvm/Analysis/Loads.h"
33#include "llvm/Analysis/LoopInfo.h"
34#include "llvm/Analysis/TargetLibraryInfo.h"
35#include "llvm/Analysis/VectorUtils.h"
36#include "llvm/Analysis/WithCache.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"
41#include "llvm/IR/ConstantRange.h"
42#include "llvm/IR/Constants.h"
43#include "llvm/IR/DerivedTypes.h"
44#include "llvm/IR/DiagnosticInfo.h"
45#include "llvm/IR/Dominators.h"
46#include "llvm/IR/EHPersonalities.h"
47#include "llvm/IR/Function.h"
48#include "llvm/IR/GetElementPtrTypeIterator.h"
49#include "llvm/IR/GlobalAlias.h"
50#include "llvm/IR/GlobalValue.h"
51#include "llvm/IR/GlobalVariable.h"
52#include "llvm/IR/InstrTypes.h"
53#include "llvm/IR/Instruction.h"
54#include "llvm/IR/Instructions.h"
55#include "llvm/IR/IntrinsicInst.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"
65#include "llvm/IR/PatternMatch.h"
66#include "llvm/IR/Type.h"
67#include "llvm/IR/User.h"
68#include "llvm/IR/Value.h"
69#include "llvm/Support/Casting.h"
70#include "llvm/Support/CommandLine.h"
71#include "llvm/Support/Compiler.h"
72#include "llvm/Support/ErrorHandling.h"
73#include "llvm/Support/KnownBits.h"
74#include "llvm/Support/MathExtras.h"
75#include "llvm/TargetParser/RISCVTargetParser.h"
76#include <algorithm>
77#include <cassert>
78#include <cstdint>
79#include <optional>
80#include <utility>
81
82using namespacellvm;
83using namespacellvm::PatternMatch;
84
85// Controls the number of uses of the value searched for possible
86// dominating comparisons.
87staticcl::opt<unsigned>DomConditionsMaxUses("dom-conditions-max-uses",
88cl::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.
93staticunsignedgetBitWidth(Type *Ty,constDataLayout &DL) {
94if (unsignedBitWidth = Ty->getScalarSizeInBits())
95returnBitWidth;
96
97returnDL.getPointerTypeSizeInBits(Ty);
98}
99
100// Given the provided Value and, potentially, a context instruction, return
101// the preferred context instruction (if any).
102staticconstInstruction *safeCxtI(constValue *V,constInstruction *CxtI) {
103// If we've been provided with a context instruction, then use that (provided
104// it has been inserted).
105if (CxtI && CxtI->getParent())
106return CxtI;
107
108// If the value is really an already-inserted instruction, then use that.
109 CxtI = dyn_cast<Instruction>(V);
110if (CxtI && CxtI->getParent())
111return CxtI;
112
113returnnullptr;
114}
115
116staticconstInstruction *safeCxtI(constValue *V1,constValue *V2,constInstruction *CxtI) {
117// If we've been provided with a context instruction, then use that (provided
118// it has been inserted).
119if (CxtI && CxtI->getParent())
120return CxtI;
121
122// If the value is really an already-inserted instruction, then use that.
123 CxtI = dyn_cast<Instruction>(V1);
124if (CxtI && CxtI->getParent())
125return CxtI;
126
127 CxtI = dyn_cast<Instruction>(V2);
128if (CxtI && CxtI->getParent())
129return CxtI;
130
131returnnullptr;
132}
133
134staticboolgetShuffleDemandedElts(constShuffleVectorInst *Shuf,
135constAPInt &DemandedElts,
136APInt &DemandedLHS,APInt &DemandedRHS) {
137if (isa<ScalableVectorType>(Shuf->getType())) {
138assert(DemandedElts ==APInt(1,1));
139 DemandedLHS = DemandedRHS = DemandedElts;
140returntrue;
141 }
142
143int NumElts =
144 cast<FixedVectorType>(Shuf->getOperand(0)->getType())->getNumElements();
145returnllvm::getShuffleDemandedElts(NumElts, Shuf->getShuffleMask(),
146 DemandedElts, DemandedLHS, DemandedRHS);
147}
148
149staticvoidcomputeKnownBits(constValue *V,constAPInt &DemandedElts,
150KnownBits &Known,unsignedDepth,
151constSimplifyQuery &Q);
152
153voidllvm::computeKnownBits(constValue *V,KnownBits &Known,unsignedDepth,
154constSimplifyQuery &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.
158auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
159APInt DemandedElts =
160 FVTy ?APInt::getAllOnes(FVTy->getNumElements()) :APInt(1, 1);
161::computeKnownBits(V, DemandedElts, Known,Depth, Q);
162}
163
164voidllvm::computeKnownBits(constValue *V,KnownBits &Known,
165constDataLayout &DL,unsignedDepth,
166AssumptionCache *AC,constInstruction *CxtI,
167constDominatorTree *DT,bool UseInstrInfo) {
168computeKnownBits(
169 V, Known,Depth,
170SimplifyQuery(DL, DT, AC,safeCxtI(V, CxtI), UseInstrInfo));
171}
172
173KnownBitsllvm::computeKnownBits(constValue *V,constDataLayout &DL,
174unsignedDepth,AssumptionCache *AC,
175constInstruction *CxtI,
176constDominatorTree *DT,bool UseInstrInfo) {
177returncomputeKnownBits(
178 V,Depth,SimplifyQuery(DL, DT, AC,safeCxtI(V, CxtI), UseInstrInfo));
179}
180
181KnownBitsllvm::computeKnownBits(constValue *V,constAPInt &DemandedElts,
182constDataLayout &DL,unsignedDepth,
183AssumptionCache *AC,constInstruction *CxtI,
184constDominatorTree *DT,bool UseInstrInfo) {
185returncomputeKnownBits(
186 V, DemandedElts,Depth,
187SimplifyQuery(DL, DT, AC,safeCxtI(V, CxtI), UseInstrInfo));
188}
189
190staticboolhaveNoCommonBitsSetSpecialCases(constValue *LHS,constValue *RHS,
191constSimplifyQuery &SQ) {
192// Look for an inverted mask: (X & ~M) op (Y & M).
193 {
194Value *M;
195if (match(LHS,m_c_And(m_Not(m_Value(M)),m_Value())) &&
196match(RHS,m_c_And(m_Specific(M),m_Value())) &&
197isGuaranteedNotToBeUndef(M, SQ.AC, SQ.CxtI, SQ.DT))
198returntrue;
199 }
200
201// X op (Y & ~X)
202if (match(RHS,m_c_And(m_Not(m_Specific(LHS)),m_Value())) &&
203isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
204returntrue;
205
206// X op ((X & Y) ^ Y) -- this is the canonical form of the previous pattern
207// for constant Y.
208Value *Y;
209if (match(RHS,
210m_c_Xor(m_c_And(m_Specific(LHS),m_Value(Y)),m_Deferred(Y))) &&
211isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT) &&
212isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
213returntrue;
214
215// Peek through extends to find a 'not' of the other side:
216// (ext Y) op ext(~Y)
217if (match(LHS,m_ZExtOrSExt(m_Value(Y))) &&
218match(RHS,m_ZExtOrSExt(m_Not(m_Specific(Y)))) &&
219isGuaranteedNotToBeUndef(Y, SQ.AC, SQ.CxtI, SQ.DT))
220returntrue;
221
222// Look for: (A & B) op ~(A | B)
223 {
224Value *A, *B;
225if (match(LHS,m_And(m_Value(A),m_Value(B))) &&
226match(RHS,m_Not(m_c_Or(m_Specific(A),m_Specific(B)))) &&
227isGuaranteedNotToBeUndef(A, SQ.AC, SQ.CxtI, SQ.DT) &&
228isGuaranteedNotToBeUndef(B, SQ.AC, SQ.CxtI, SQ.DT))
229returntrue;
230 }
231
232// Look for: (X << V) op (Y >> (BitWidth - V))
233// or (X >> V) op (Y << (BitWidth - V))
234 {
235constValue *V;
236constAPInt *R;
237if (((match(RHS,m_Shl(m_Value(),m_Sub(m_APInt(R),m_Value(V)))) &&
238match(LHS,m_LShr(m_Value(),m_Specific(V)))) ||
239 (match(RHS,m_LShr(m_Value(),m_Sub(m_APInt(R),m_Value(V)))) &&
240match(LHS,m_Shl(m_Value(),m_Specific(V))))) &&
241 R->uge(LHS->getType()->getScalarSizeInBits()))
242returntrue;
243 }
244
245returnfalse;
246}
247
248boolllvm::haveNoCommonBitsSet(constWithCache<const Value *> &LHSCache,
249constWithCache<const Value *> &RHSCache,
250constSimplifyQuery &SQ) {
251constValue *LHS = LHSCache.getValue();
252constValue *RHS = RHSCache.getValue();
253
254assert(LHS->getType() ==RHS->getType() &&
255"LHS and RHS should have the same type");
256assert(LHS->getType()->isIntOrIntVectorTy() &&
257"LHS and RHS should be integers");
258
259if (haveNoCommonBitsSetSpecialCases(LHS,RHS, SQ) ||
260haveNoCommonBitsSetSpecialCases(RHS,LHS, SQ))
261returntrue;
262
263returnKnownBits::haveNoCommonBitsSet(LHSCache.getKnownBits(SQ),
264 RHSCache.getKnownBits(SQ));
265}
266
267boolllvm::isOnlyUsedInZeroComparison(constInstruction *I) {
268return !I->user_empty() &&all_of(I->users(), [](constUser *U) {
269 return match(U, m_ICmp(m_Value(), m_Zero()));
270 });
271}
272
273boolllvm::isOnlyUsedInZeroEqualityComparison(constInstruction *I) {
274return !I->user_empty() &&all_of(I->users(), [](constUser *U) {
275 CmpPredicate P;
276 return match(U, m_ICmp(P, m_Value(), m_Zero())) && ICmpInst::isEquality(P);
277 });
278}
279
280boolllvm::isKnownToBeAPowerOfTwo(constValue *V,constDataLayout &DL,
281bool OrZero,unsignedDepth,
282AssumptionCache *AC,constInstruction *CxtI,
283constDominatorTree *DT,bool UseInstrInfo) {
284 return ::isKnownToBeAPowerOfTwo(
285 V, OrZero,Depth,
286SimplifyQuery(DL, DT, AC,safeCxtI(V, CxtI), UseInstrInfo));
287}
288
289staticboolisKnownNonZero(constValue *V,constAPInt &DemandedElts,
290constSimplifyQuery &Q,unsignedDepth);
291
292boolllvm::isKnownNonNegative(constValue *V,constSimplifyQuery &SQ,
293unsignedDepth) {
294returncomputeKnownBits(V,Depth, SQ).isNonNegative();
295}
296
297boolllvm::isKnownPositive(constValue *V,constSimplifyQuery &SQ,
298unsignedDepth) {
299if (auto *CI = dyn_cast<ConstantInt>(V))
300return CI->getValue().isStrictlyPositive();
301
302// If `isKnownNonNegative` ever becomes more sophisticated, make sure to keep
303// this updated.
304KnownBits Known =computeKnownBits(V,Depth, SQ);
305return Known.isNonNegative() &&
306 (Known.isNonZero() ||isKnownNonZero(V, SQ,Depth));
307}
308
309boolllvm::isKnownNegative(constValue *V,constSimplifyQuery &SQ,
310unsignedDepth) {
311returncomputeKnownBits(V,Depth, SQ).isNegative();
312}
313
314staticboolisKnownNonEqual(constValue *V1,constValue *V2,
315constAPInt &DemandedElts,unsignedDepth,
316constSimplifyQuery &Q);
317
318boolllvm::isKnownNonEqual(constValue *V1,constValue *V2,
319constDataLayout &DL,AssumptionCache *AC,
320constInstruction *CxtI,constDominatorTree *DT,
321bool UseInstrInfo) {
322// We don't support looking through casts.
323if (V1 == V2 || V1->getType() != V2->getType())
324returnfalse;
325auto *FVTy = dyn_cast<FixedVectorType>(V1->getType());
326APInt DemandedElts =
327 FVTy ?APInt::getAllOnes(FVTy->getNumElements()) :APInt(1, 1);
328 return ::isKnownNonEqual(
329 V1, V2, DemandedElts, 0,
330SimplifyQuery(DL, DT, AC,safeCxtI(V2, V1, CxtI), UseInstrInfo));
331}
332
333boolllvm::MaskedValueIsZero(constValue *V,constAPInt &Mask,
334constSimplifyQuery &SQ,unsignedDepth) {
335KnownBits Known(Mask.getBitWidth());
336computeKnownBits(V, Known,Depth, SQ);
337return Mask.isSubsetOf(Known.Zero);
338}
339
340staticunsignedComputeNumSignBits(constValue *V,constAPInt &DemandedElts,
341unsignedDepth,constSimplifyQuery &Q);
342
343staticunsignedComputeNumSignBits(constValue *V,unsignedDepth,
344constSimplifyQuery &Q) {
345auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
346APInt DemandedElts =
347 FVTy ?APInt::getAllOnes(FVTy->getNumElements()) :APInt(1, 1);
348returnComputeNumSignBits(V, DemandedElts,Depth, Q);
349}
350
351unsignedllvm::ComputeNumSignBits(constValue *V,constDataLayout &DL,
352unsignedDepth,AssumptionCache *AC,
353constInstruction *CxtI,
354constDominatorTree *DT,bool UseInstrInfo) {
355 return ::ComputeNumSignBits(
356 V,Depth,SimplifyQuery(DL, DT, AC,safeCxtI(V, CxtI), UseInstrInfo));
357}
358
359unsignedllvm::ComputeMaxSignificantBits(constValue *V,constDataLayout &DL,
360unsignedDepth,AssumptionCache *AC,
361constInstruction *CxtI,
362constDominatorTree *DT) {
363unsigned SignBits =ComputeNumSignBits(V,DL,Depth, AC, CxtI, DT);
364return V->getType()->getScalarSizeInBits() - SignBits + 1;
365}
366
367staticvoidcomputeKnownBitsAddSub(boolAdd,constValue *Op0,constValue *Op1,
368bool NSW,bool NUW,
369constAPInt &DemandedElts,
370KnownBits &KnownOut,KnownBits &Known2,
371unsignedDepth,constSimplifyQuery &Q) {
372computeKnownBits(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.
376if (KnownOut.isUnknown() && !NSW && !NUW)
377return;
378
379computeKnownBits(Op0, DemandedElts, Known2,Depth + 1, Q);
380 KnownOut =KnownBits::computeForAddSub(Add, NSW, NUW, Known2, KnownOut);
381}
382
383staticvoidcomputeKnownBitsMul(constValue *Op0,constValue *Op1,bool NSW,
384bool NUW,constAPInt &DemandedElts,
385KnownBits &Known,KnownBits &Known2,
386unsignedDepth,constSimplifyQuery &Q) {
387computeKnownBits(Op1, DemandedElts, Known,Depth + 1, Q);
388computeKnownBits(Op0, DemandedElts, Known2,Depth + 1, Q);
389
390boolisKnownNegative =false;
391boolisKnownNonNegative =false;
392// If the multiplication is known not to overflow, compute the sign bit.
393if (NSW) {
394if (Op0 == Op1) {
395// The product of a number with itself is non-negative.
396isKnownNonNegative =true;
397 }else {
398bool isKnownNonNegativeOp1 = Known.isNonNegative();
399bool isKnownNonNegativeOp0 = Known2.isNonNegative();
400bool isKnownNegativeOp1 = Known.isNegative();
401bool isKnownNegativeOp0 = Known2.isNegative();
402// The product of two numbers with the same sign is non-negative.
403isKnownNonNegative = (isKnownNegativeOp1 && isKnownNegativeOp0) ||
404 (isKnownNonNegativeOp1 && isKnownNonNegativeOp0);
405if (!isKnownNonNegative && NUW) {
406// mul nuw nsw with a factor > 1 is non-negative.
407KnownBits One =KnownBits::makeConstant(APInt(Known.getBitWidth(), 1));
408isKnownNonNegative =KnownBits::sgt(Known, One).value_or(false) ||
409KnownBits::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.
414if (!isKnownNonNegative)
415isKnownNegative =
416 (isKnownNegativeOp1 && isKnownNonNegativeOp0 &&
417 Known2.isNonZero()) ||
418 (isKnownNegativeOp0 && isKnownNonNegativeOp1 && Known.isNonZero());
419 }
420 }
421
422bool SelfMultiply = Op0 == Op1;
423if (SelfMultiply)
424 SelfMultiply &=
425isGuaranteedNotToBeUndef(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.
433if (isKnownNonNegative && !Known.isNegative())
434 Known.makeNonNegative();
435elseif (isKnownNegative && !Known.isNonNegative())
436 Known.makeNegative();
437}
438
439voidllvm::computeKnownBitsFromRangeMetadata(constMDNode &Ranges,
440KnownBits &Known) {
441unsignedBitWidth = Known.getBitWidth();
442unsigned NumRanges = Ranges.getNumOperands() / 2;
443assert(NumRanges >= 1);
444
445 Known.Zero.setAllBits();
446 Known.One.setAllBits();
447
448for (unsigned i = 0; i < NumRanges; ++i) {
449ConstantInt *Lower =
450 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 0));
451ConstantInt *Upper =
452 mdconst::extract<ConstantInt>(Ranges.getOperand(2 * i + 1));
453ConstantRangeRange(Lower->getValue(),Upper->getValue());
454
455// The first CommonPrefixBits of all values in Range are equal.
456unsigned CommonPrefixBits =
457 (Range.getUnsignedMax() ^Range.getUnsignedMin()).countl_zero();
458APInt Mask =APInt::getHighBitsSet(BitWidth, CommonPrefixBits);
459APInt UnsignedMax =Range.getUnsignedMax().zextOrTrunc(BitWidth);
460 Known.One &= UnsignedMax & Mask;
461 Known.Zero &= ~UnsignedMax & Mask;
462 }
463}
464
465staticboolisEphemeralValueOf(constInstruction *I,constValue *E) {
466SmallVector<const Value *, 16> WorkSet(1,I);
467SmallPtrSet<const Value *, 32> Visited;
468SmallPtrSet<const Value *, 16> EphValues;
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.
473if (is_contained(I->operands(), E))
474returntrue;
475
476while (!WorkSet.empty()) {
477constValue *V = WorkSet.pop_back_val();
478if (!Visited.insert(V).second)
479continue;
480
481// If all uses of this value are ephemeral, then so is this value.
482if (llvm::all_of(V->users(), [&](constUser *U) {
483 return EphValues.count(U);
484 })) {
485if (V == E)
486returntrue;
487
488if (V ==I || (isa<Instruction>(V) &&
489 !cast<Instruction>(V)->mayHaveSideEffects() &&
490 !cast<Instruction>(V)->isTerminator())) {
491 EphValues.insert(V);
492if (constUser *U = dyn_cast<User>(V))
493append_range(WorkSet, U->operands());
494 }
495 }
496 }
497
498returnfalse;
499}
500
501// Is this an intrinsic that cannot be speculated but also cannot trap?
502boolllvm::isAssumeLikeIntrinsic(constInstruction *I) {
503if (constIntrinsicInst *CI = dyn_cast<IntrinsicInst>(I))
504return CI->isAssumeLikeIntrinsic();
505
506returnfalse;
507}
508
509boolllvm::isValidAssumeForContext(constInstruction *Inv,
510constInstruction *CxtI,
511constDominatorTree *DT,
512bool 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
521if (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.
524if (Inv->comesBefore(CxtI))
525returntrue;
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.
530if (!AllowEphemerals && Inv == CxtI)
531returnfalse;
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).
539autoRange =make_range(CxtI->getIterator(), Inv->getIterator());
540if (!isGuaranteedToTransferExecutionToSuccessor(Range, 15))
541returnfalse;
542
543return AllowEphemerals || !isEphemeralValueOf(Inv, CxtI);
544 }
545
546// Inv and CxtI are in different blocks.
547if (DT) {
548if (DT->dominates(Inv, CxtI))
549returntrue;
550 }elseif (Inv->getParent() == CxtI->getParent()->getSinglePredecessor() ||
551 Inv->getParent()->isEntryBlock()) {
552// We don't have a DT, but this trivially dominates.
553returntrue;
554 }
555
556returnfalse;
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.
564staticboolcmpExcludesZero(CmpInst::Predicate Pred,constValue *RHS) {
565// v u> y implies v != 0.
566if (Pred == ICmpInst::ICMP_UGT)
567returntrue;
568
569// Special-case v != 0 to also handle v != null.
570if (Pred == ICmpInst::ICMP_NE)
571returnmatch(RHS,m_Zero());
572
573// All other predicates - rely on generic ConstantRange handling.
574constAPInt *C;
575auto Zero =APInt::getZero(RHS->getType()->getScalarSizeInBits());
576if (match(RHS,m_APInt(C))) {
577ConstantRange TrueValues =ConstantRange::makeExactICmpRegion(Pred, *C);
578return !TrueValues.contains(Zero);
579 }
580
581auto *VC = dyn_cast<ConstantDataVector>(RHS);
582if (VC ==nullptr)
583returnfalse;
584
585for (unsigned ElemIdx = 0, NElem = VC->getNumElements(); ElemIdx < NElem;
586 ++ElemIdx) {
587ConstantRange TrueValues =ConstantRange::makeExactICmpRegion(
588 Pred, VC->getElementAsAPInt(ElemIdx));
589if (TrueValues.contains(Zero))
590returnfalse;
591 }
592returntrue;
593}
594
595staticvoidbreakSelfRecursivePHI(constUse *U,constPHINode *PHI,
596Value *&ValOut,Instruction *&CtxIOut,
597constPHINode **PhiOut =nullptr) {
598 ValOut = U->get();
599if (ValOut ==PHI)
600return;
601 CtxIOut =PHI->getIncomingBlock(*U)->getTerminator();
602if (PhiOut)
603 *PhiOut =PHI;
604Value *V;
605// If the Use is a select of this phi, compute analysis on other arm to break
606// recursion.
607// TODO: Min/Max
608if (match(ValOut,m_Select(m_Value(),m_Specific(PHI),m_Value(V))) ||
609match(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.
616if (auto *IncPhi = dyn_cast<PHINode>(ValOut);
617 IncPhi && IncPhi->getNumIncomingValues() == 2) {
618for (intIdx = 0;Idx < 2; ++Idx) {
619if (IncPhi->getIncomingValue(Idx) ==PHI) {
620 ValOut = IncPhi->getIncomingValue(1 -Idx);
621if (PhiOut)
622 *PhiOut = IncPhi;
623 CtxIOut = IncPhi->getIncomingBlock(1 -Idx)->getTerminator();
624break;
625 }
626 }
627 }
628}
629
630staticboolisKnownNonZeroFromAssume(constValue *V,constSimplifyQuery &Q) {
631// Use of assumptions is context-sensitive. If we don't have a context, we
632// cannot use them!
633if (!Q.AC || !Q.CxtI)
634returnfalse;
635
636for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
637if (!Elem.Assume)
638continue;
639
640AssumeInst *I = cast<AssumeInst>(Elem.Assume);
641assert(I->getFunction() == Q.CxtI->getFunction() &&
642"Got assumption for the wrong function!");
643
644if (Elem.Index !=AssumptionCache::ExprResultIdx) {
645if (!V->getType()->isPointerTy())
646continue;
647if (RetainedKnowledge RK =getKnowledgeFromBundle(
648 *I,I->bundle_op_info_begin()[Elem.Index])) {
649if (RK.WasOn == V &&
650 (RK.AttrKind == Attribute::NonNull ||
651 (RK.AttrKind == Attribute::Dereferenceable &&
652 !NullPointerIsDefined(Q.CxtI->getFunction(),
653 V->getType()->getPointerAddressSpace()))) &&
654isValidAssumeForContext(I, Q.CxtI, Q.DT))
655returntrue;
656 }
657continue;
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
664Value *RHS;
665CmpPredicate Pred;
666auto m_V =m_CombineOr(m_Specific(V),m_PtrToInt(m_Specific(V)));
667if (!match(I->getArgOperand(0),m_c_ICmp(Pred, m_V,m_Value(RHS))))
668continue;
669
670if (cmpExcludesZero(Pred,RHS) &&isValidAssumeForContext(I, Q.CxtI, Q.DT))
671returntrue;
672 }
673
674returnfalse;
675}
676
677staticvoidcomputeKnownBitsFromCmp(constValue *V,CmpInst::Predicate Pred,
678Value *LHS,Value *RHS,KnownBits &Known,
679constSimplifyQuery &Q) {
680if (RHS->getType()->isPointerTy()) {
681// Handle comparison of pointer to null explicitly, as it will not be
682// covered by the m_APInt() logic below.
683if (LHS == V &&match(RHS,m_Zero())) {
684switch (Pred) {
685case ICmpInst::ICMP_EQ:
686 Known.setAllZero();
687break;
688case ICmpInst::ICMP_SGE:
689case ICmpInst::ICMP_SGT:
690 Known.makeNonNegative();
691break;
692case ICmpInst::ICMP_SLT:
693 Known.makeNegative();
694break;
695default:
696break;
697 }
698 }
699return;
700 }
701
702unsignedBitWidth = Known.getBitWidth();
703auto m_V =
704m_CombineOr(m_Specific(V),m_PtrToIntSameSize(Q.DL,m_Specific(V)));
705
706Value *Y;
707constAPInt *Mask, *C;
708uint64_t ShAmt;
709switch (Pred) {
710case ICmpInst::ICMP_EQ:
711// assume(V = C)
712if (match(LHS, m_V) &&match(RHS,m_APInt(C))) {
713 Known = Known.unionWith(KnownBits::makeConstant(*C));
714// assume(V & Mask = C)
715 }elseif (match(LHS,m_c_And(m_V,m_Value(Y))) &&
716match(RHS,m_APInt(C))) {
717// For one bits in Mask, we can propagate bits from C to V.
718 Known.One |= *C;
719if (match(Y,m_APInt(Mask)))
720 Known.Zero |= ~*C & *Mask;
721// assume(V | Mask = C)
722 }elseif (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;
725if (match(Y,m_APInt(Mask)))
726 Known.One |= *C & ~*Mask;
727// assume(V ^ Mask = C)
728 }elseif (match(LHS,m_Xor(m_V,m_APInt(Mask))) &&
729match(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 }elseif (match(LHS,m_Shl(m_V,m_ConstantInt(ShAmt))) &&
734match(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.
737KnownBits RHSKnown =KnownBits::makeConstant(*C);
738 RHSKnown.Zero.lshrInPlace(ShAmt);
739 RHSKnown.One.lshrInPlace(ShAmt);
740 Known = Known.unionWith(RHSKnown);
741// assume(V >> ShAmt = C)
742 }elseif (match(LHS,m_Shr(m_V,m_ConstantInt(ShAmt))) &&
743match(RHS,m_APInt(C)) && ShAmt <BitWidth) {
744KnownBits RHSKnown =KnownBits::makeConstant(*C);
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 }
750break;
751case ICmpInst::ICMP_NE: {
752// assume (V & B != 0) where B is a power of 2
753constAPInt *BPow2;
754if (match(LHS,m_And(m_V,m_Power2(BPow2))) &&match(RHS,m_Zero()))
755 Known.One |= *BPow2;
756break;
757 }
758default:
759if (match(RHS,m_APInt(C))) {
760constAPInt *Offset =nullptr;
761if (match(LHS,m_CombineOr(m_V,m_AddLike(m_V,m_APInt(Offset))))) {
762ConstantRange LHSRange =ConstantRange::makeAllowedICmpRegion(Pred, *C);
763if (Offset)
764 LHSRange = LHSRange.sub(*Offset);
765 Known = Known.unionWith(LHSRange.toKnownBits());
766 }
767if (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
770if (match(LHS,m_c_And(m_V,m_Value())) ||
771match(LHS,m_NUWSub(m_V,m_Value())))
772 Known.One.setHighBits(
773 (*C + (Pred == ICmpInst::ICMP_UGT)).countLeadingOnes());
774 }
775if (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
778if (match(LHS,m_c_Or(m_V,m_Value())) ||
779match(LHS,m_c_NUWAdd(m_V,m_Value()))) {
780 Known.Zero.setHighBits(
781 (*C - (Pred == ICmpInst::ICMP_ULT)).countLeadingZeros());
782 }
783 }
784 }
785break;
786 }
787}
788
789staticvoidcomputeKnownBitsFromICmpCond(constValue *V,ICmpInst *Cmp,
790KnownBits &Known,
791constSimplifyQuery &SQ,bool Invert) {
792ICmpInst::Predicate Pred =
793 Invert ? Cmp->getInversePredicate() : Cmp->getPredicate();
794Value *LHS = Cmp->getOperand(0);
795Value *RHS = Cmp->getOperand(1);
796
797// Handle icmp pred (trunc V), C
798if (match(LHS,m_Trunc(m_Specific(V)))) {
799KnownBits DstKnown(LHS->getType()->getScalarSizeInBits());
800computeKnownBitsFromCmp(LHS, Pred,LHS,RHS, DstKnown, SQ);
801 Known = Known.unionWith(DstKnown.anyext(Known.getBitWidth()));
802return;
803 }
804
805computeKnownBitsFromCmp(V, Pred,LHS,RHS, Known, SQ);
806}
807
808staticvoidcomputeKnownBitsFromCond(constValue *V,Value *Cond,
809KnownBits &Known,unsignedDepth,
810constSimplifyQuery &SQ,bool Invert) {
811Value *A, *B;
812if (Depth <MaxAnalysisRecursionDepth &&
813match(Cond,m_LogicalOp(m_Value(A),m_Value(B)))) {
814KnownBits Known2(Known.getBitWidth());
815KnownBits Known3(Known.getBitWidth());
816computeKnownBitsFromCond(V,A, Known2,Depth + 1, SQ, Invert);
817computeKnownBitsFromCond(V,B, Known3,Depth + 1, SQ, Invert);
818if (Invert ?match(Cond,m_LogicalOr(m_Value(),m_Value()))
819 :match(Cond,m_LogicalAnd(m_Value(),m_Value())))
820 Known2 = Known2.unionWith(Known3);
821else
822 Known2 = Known2.intersectWith(Known3);
823 Known = Known.unionWith(Known2);
824 }
825
826if (auto *Cmp = dyn_cast<ICmpInst>(Cond))
827computeKnownBitsFromICmpCond(V, Cmp, Known, SQ, Invert);
828}
829
830voidllvm::computeKnownBitsFromContext(constValue *V,KnownBits &Known,
831unsignedDepth,constSimplifyQuery &Q) {
832// Handle injected condition.
833if (Q.CC && Q.CC->AffectedValues.contains(V))
834computeKnownBitsFromCond(V, Q.CC->Cond, Known,Depth, Q, Q.CC->Invert);
835
836if (!Q.CxtI)
837return;
838
839if (Q.DC && Q.DT) {
840// Handle dominating conditions.
841for (BranchInst *BI : Q.DC->conditionsFor(V)) {
842BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
843if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
844computeKnownBitsFromCond(V, BI->getCondition(), Known,Depth, Q,
845/*Invert*/false);
846
847BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
848if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
849computeKnownBitsFromCond(V, BI->getCondition(), Known,Depth, Q,
850/*Invert*/true);
851 }
852
853if (Known.hasConflict())
854 Known.resetAll();
855 }
856
857if (!Q.AC)
858return;
859
860unsignedBitWidth = Known.getBitWidth();
861
862// Note that the patterns below need to be kept in sync with the code
863// in AssumptionCache::updateAffectedValues.
864
865for (AssumptionCache::ResultElem &Elem : Q.AC->assumptionsFor(V)) {
866if (!Elem.Assume)
867continue;
868
869AssumeInst *I = cast<AssumeInst>(Elem.Assume);
870assert(I->getParent()->getParent() == Q.CxtI->getParent()->getParent() &&
871"Got assumption for the wrong function!");
872
873if (Elem.Index !=AssumptionCache::ExprResultIdx) {
874if (!V->getType()->isPointerTy())
875continue;
876if (RetainedKnowledge RK =getKnowledgeFromBundle(
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.
881if (RK.WasOn == V && RK.AttrKind == Attribute::Alignment &&
882isPowerOf2_64(RK.ArgValue) &&
883isValidAssumeForContext(I, Q.CxtI, Q.DT,/*AllowEphemerals*/true))
884 Known.Zero.setLowBits(Log2_64(RK.ArgValue));
885 }
886continue;
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
893Value *Arg =I->getArgOperand(0);
894
895if (Arg == V &&isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
896assert(BitWidth == 1 &&"assume operand is not i1?");
897 (void)BitWidth;
898 Known.setAllOnes();
899return;
900 }
901if (match(Arg,m_Not(m_Specific(V))) &&
902isValidAssumeForContext(I, Q.CxtI, Q.DT)) {
903assert(BitWidth == 1 &&"assume operand is not i1?");
904 (void)BitWidth;
905 Known.setAllZero();
906return;
907 }
908
909// The remaining tests are all recursive, so bail out if we hit the limit.
910if (Depth ==MaxAnalysisRecursionDepth)
911continue;
912
913ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
914if (!Cmp)
915continue;
916
917if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
918continue;
919
920computeKnownBitsFromICmpCond(V, Cmp, Known, Q,/*Invert=*/false);
921 }
922
923// Conflicting assumption: Undefined behavior will occur on this execution
924// path.
925if (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.
937staticvoidcomputeKnownBitsFromShiftOperator(
938constOperator *I,constAPInt &DemandedElts,KnownBits &Known,
939KnownBits &Known2,unsignedDepth,constSimplifyQuery &Q,
940function_ref<KnownBits(constKnownBits &,constKnownBits &,bool)> KF) {
941computeKnownBits(I->getOperand(0), DemandedElts, Known2,Depth + 1, Q);
942computeKnownBits(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.
945bool ShAmtNonZero =
946 Known.isNonZero() ||
947 (Known.getMaxValue().ult(Known.getBitWidth()) &&
948isKnownNonZero(I->getOperand(1), DemandedElts, Q,Depth + 1));
949 Known = KF(Known2, Known, ShAmtNonZero);
950}
951
952staticKnownBits
953getKnownBitsFromAndXorOr(constOperator *I,constAPInt &DemandedElts,
954constKnownBits &KnownLHS,constKnownBits &KnownRHS,
955unsignedDepth,constSimplifyQuery &Q) {
956unsignedBitWidth = KnownLHS.getBitWidth();
957KnownBits KnownOut(BitWidth);
958bool IsAnd =false;
959bool HasKnownOne = !KnownLHS.One.isZero() || !KnownRHS.One.isZero();
960Value *X =nullptr, *Y =nullptr;
961
962switch (I->getOpcode()) {
963case 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).
971if (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.
973if (KnownLHS.countMaxTrailingZeros() <= KnownRHS.countMaxTrailingZeros())
974 KnownOut = KnownLHS.blsi();
975else
976 KnownOut = KnownRHS.blsi();
977 }
978break;
979case Instruction::Or:
980 KnownOut = KnownLHS | KnownRHS;
981break;
982case 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).
991if (HasKnownOne &&
992match(I,m_c_Xor(m_Value(X),m_Add(m_Deferred(X),m_AllOnes())))) {
993constKnownBits &XBits =I->getOperand(0) ==X ? KnownLHS : KnownRHS;
994 KnownOut = XBits.blsmsk();
995 }
996break;
997default:
998llvm_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.
1007if (!KnownOut.Zero[0] && !KnownOut.One[0] &&
1008 (match(I,m_c_BinOp(m_Value(X),m_c_Add(m_Deferred(X),m_Value(Y)))) ||
1009match(I,m_c_BinOp(m_Value(X),m_Sub(m_Deferred(X),m_Value(Y)))) ||
1010match(I,m_c_BinOp(m_Value(X),m_Sub(m_Value(Y),m_Deferred(X)))))) {
1011KnownBits KnownY(BitWidth);
1012computeKnownBits(Y, DemandedElts, KnownY,Depth + 1, Q);
1013if (KnownY.countMinTrailingOnes() > 0) {
1014if (IsAnd)
1015 KnownOut.Zero.setBit(0);
1016else
1017 KnownOut.One.setBit(0);
1018 }
1019 }
1020return KnownOut;
1021}
1022
1023staticKnownBitscomputeKnownBitsForHorizontalOperation(
1024constOperator *I,constAPInt &DemandedElts,unsignedDepth,
1025constSimplifyQuery &Q,
1026constfunction_ref<KnownBits(constKnownBits &,constKnownBits &)>
1027 KnownBitsFunc) {
1028APInt DemandedEltsLHS, DemandedEltsRHS;
1029getHorizDemandedEltsForFirstOperand(Q.DL.getTypeSizeInBits(I->getType()),
1030 DemandedElts, DemandedEltsLHS,
1031 DemandedEltsRHS);
1032
1033constauto ComputeForSingleOpFunc =
1034 [Depth, &Q, KnownBitsFunc](constValue *Op,APInt &DemandedEltsOp) {
1035return KnownBitsFunc(
1036computeKnownBits(Op, DemandedEltsOp,Depth + 1, Q),
1037computeKnownBits(Op, DemandedEltsOp << 1,Depth + 1, Q));
1038 };
1039
1040if (DemandedEltsRHS.isZero())
1041return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS);
1042if (DemandedEltsLHS.isZero())
1043return ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS);
1044
1045return ComputeForSingleOpFunc(I->getOperand(0), DemandedEltsLHS)
1046 .intersectWith(ComputeForSingleOpFunc(I->getOperand(1), DemandedEltsRHS));
1047}
1048
1049// Public so this can be used in `SimplifyDemandedUseBits`.
1050KnownBitsllvm::analyzeKnownBitsFromAndXorOr(constOperator *I,
1051constKnownBits &KnownLHS,
1052constKnownBits &KnownRHS,
1053unsignedDepth,
1054constSimplifyQuery &SQ) {
1055auto *FVTy = dyn_cast<FixedVectorType>(I->getType());
1056APInt DemandedElts =
1057 FVTy ?APInt::getAllOnes(FVTy->getNumElements()) :APInt(1, 1);
1058
1059returngetKnownBitsFromAndXorOr(I, DemandedElts, KnownLHS, KnownRHS,Depth,
1060 SQ);
1061}
1062
1063ConstantRangellvm::getVScaleRange(constFunction *F,unsignedBitWidth) {
1064Attribute Attr =F->getFnAttribute(Attribute::VScaleRange);
1065// Without vscale_range, we only know that vscale is non-zero.
1066if (!Attr.isValid())
1067returnConstantRange(APInt(BitWidth, 1),APInt::getZero(BitWidth));
1068
1069unsigned AttrMin = Attr.getVScaleRangeMin();
1070// Minimum is larger than vscale width, result is always poison.
1071if ((unsigned)llvm::bit_width(AttrMin) >BitWidth)
1072return ConstantRange::getEmpty(BitWidth);
1073
1074APInt Min(BitWidth, AttrMin);
1075 std::optional<unsigned> AttrMax = Attr.getVScaleRangeMax();
1076if (!AttrMax || (unsigned)llvm::bit_width(*AttrMax) >BitWidth)
1077returnConstantRange(Min,APInt::getZero(BitWidth));
1078
1079returnConstantRange(Min,APInt(BitWidth, *AttrMax) + 1);
1080}
1081
1082voidllvm::adjustKnownBitsForSelectArm(KnownBits &Known,Value *Cond,
1083Value *Arm,bool Invert,unsignedDepth,
1084constSimplifyQuery &Q) {
1085// If we have a constant arm, we are done.
1086if (Known.isConstant())
1087return;
1088
1089// See what condition implies about the bits of the select arm.
1090KnownBits CondRes(Known.getBitWidth());
1091computeKnownBitsFromCond(Arm,Cond, CondRes,Depth + 1, Q, Invert);
1092// If we don't get any information from the condition, no reason to
1093// proceed.
1094if (CondRes.isUnknown())
1095return;
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);
1103if (CondRes.hasConflict())
1104return;
1105
1106// Finally make sure the information we found is valid. This is relatively
1107// expensive so it's left for the very end.
1108if (!isGuaranteedNotToBeUndef(Arm, Q.AC, Q.CxtI, Q.DT,Depth + 1))
1109return;
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.
1118staticboolisSignedMinMaxClamp(constValue *Select,constValue *&In,
1119constAPInt *&CLow,constAPInt *&CHigh) {
1120assert(isa<Operator>(Select) &&
1121 cast<Operator>(Select)->getOpcode() == Instruction::Select &&
1122"Input should be a Select!");
1123
1124constValue *LHS =nullptr, *RHS =nullptr;
1125SelectPatternFlavor SPF =matchSelectPattern(Select,LHS,RHS).Flavor;
1126if (SPF !=SPF_SMAX && SPF !=SPF_SMIN)
1127returnfalse;
1128
1129if (!match(RHS,m_APInt(CLow)))
1130returnfalse;
1131
1132constValue *LHS2 =nullptr, *RHS2 =nullptr;
1133SelectPatternFlavor SPF2 =matchSelectPattern(LHS, LHS2, RHS2).Flavor;
1134if (getInverseMinMaxFlavor(SPF) != SPF2)
1135returnfalse;
1136
1137if (!match(RHS2,m_APInt(CHigh)))
1138returnfalse;
1139
1140if (SPF ==SPF_SMIN)
1141std::swap(CLow, CHigh);
1142
1143 In = LHS2;
1144return CLow->sle(*CHigh);
1145}
1146
1147staticboolisSignedMinMaxIntrinsicClamp(constIntrinsicInst *II,
1148constAPInt *&CLow,
1149constAPInt *&CHigh) {
1150assert((II->getIntrinsicID() == Intrinsic::smin ||
1151II->getIntrinsicID() == Intrinsic::smax) &&
1152"Must be smin/smax");
1153
1154Intrinsic::ID InverseID =getInverseMinMaxIntrinsic(II->getIntrinsicID());
1155auto *InnerII = dyn_cast<IntrinsicInst>(II->getArgOperand(0));
1156if (!InnerII || InnerII->getIntrinsicID() != InverseID ||
1157 !match(II->getArgOperand(1),m_APInt(CLow)) ||
1158 !match(InnerII->getArgOperand(1),m_APInt(CHigh)))
1159returnfalse;
1160
1161if (II->getIntrinsicID() == Intrinsic::smin)
1162std::swap(CLow, CHigh);
1163return CLow->sle(*CHigh);
1164}
1165
1166staticvoidunionWithMinMaxIntrinsicClamp(constIntrinsicInst *II,
1167KnownBits &Known) {
1168constAPInt *CLow, *CHigh;
1169if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
1170 Known = Known.unionWith(
1171ConstantRange::getNonEmpty(*CLow, *CHigh + 1).toKnownBits());
1172}
1173
1174staticvoidcomputeKnownBitsFromOperator(constOperator *I,
1175constAPInt &DemandedElts,
1176KnownBits &Known,unsignedDepth,
1177constSimplifyQuery &Q) {
1178unsignedBitWidth = Known.getBitWidth();
1179
1180KnownBits Known2(BitWidth);
1181switch (I->getOpcode()) {
1182default:break;
1183case Instruction::Load:
1184if (MDNode *MD =
1185 Q.IIQ.getMetadata(cast<LoadInst>(I), LLVMContext::MD_range))
1186computeKnownBitsFromRangeMetadata(*MD, Known);
1187break;
1188case Instruction::And:
1189computeKnownBits(I->getOperand(1), DemandedElts, Known,Depth + 1, Q);
1190computeKnownBits(I->getOperand(0), DemandedElts, Known2,Depth + 1, Q);
1191
1192 Known =getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known,Depth, Q);
1193break;
1194case Instruction::Or:
1195computeKnownBits(I->getOperand(1), DemandedElts, Known,Depth + 1, Q);
1196computeKnownBits(I->getOperand(0), DemandedElts, Known2,Depth + 1, Q);
1197
1198 Known =getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known,Depth, Q);
1199break;
1200case Instruction::Xor:
1201computeKnownBits(I->getOperand(1), DemandedElts, Known,Depth + 1, Q);
1202computeKnownBits(I->getOperand(0), DemandedElts, Known2,Depth + 1, Q);
1203
1204 Known =getKnownBitsFromAndXorOr(I, DemandedElts, Known2, Known,Depth, Q);
1205break;
1206case Instruction::Mul: {
1207bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1208bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1209computeKnownBitsMul(I->getOperand(0),I->getOperand(1), NSW, NUW,
1210 DemandedElts, Known, Known2,Depth, Q);
1211break;
1212 }
1213case Instruction::UDiv: {
1214computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1215computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1216 Known =
1217KnownBits::udiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1218break;
1219 }
1220case Instruction::SDiv: {
1221computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1222computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1223 Known =
1224KnownBits::sdiv(Known, Known2, Q.IIQ.isExact(cast<BinaryOperator>(I)));
1225break;
1226 }
1227case Instruction::Select: {
1228auto ComputeForArm = [&](Value *Arm,bool Invert) {
1229KnownBits Res(Known.getBitWidth());
1230computeKnownBits(Arm, DemandedElts, Res,Depth + 1, Q);
1231adjustKnownBitsForSelectArm(Res,I->getOperand(0), Arm, Invert,Depth, Q);
1232return 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));
1238break;
1239 }
1240case Instruction::FPTrunc:
1241case Instruction::FPExt:
1242case Instruction::FPToUI:
1243case Instruction::FPToSI:
1244case Instruction::SIToFP:
1245case Instruction::UIToFP:
1246break;// Can't work with floating point.
1247case Instruction::PtrToInt:
1248case Instruction::IntToPtr:
1249// Fall through and handle them the same as zext/trunc.
1250 [[fallthrough]];
1251case Instruction::ZExt:
1252case Instruction::Trunc: {
1253Type *SrcTy =I->getOperand(0)->getType();
1254
1255unsigned SrcBitWidth;
1256// Note that we handle pointer operands here because of inttoptr/ptrtoint
1257// which fall through here.
1258Type *ScalarTy = SrcTy->getScalarType();
1259 SrcBitWidth = ScalarTy->isPointerTy() ?
1260 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
1261 Q.DL.getTypeSizeInBits(ScalarTy);
1262
1263assert(SrcBitWidth &&"SrcBitWidth can't be zero");
1264 Known = Known.anyextOrTrunc(SrcBitWidth);
1265computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1266if (auto *Inst = dyn_cast<PossiblyNonNegInst>(I);
1267 Inst && Inst->hasNonNeg() && !Known.isNegative())
1268 Known.makeNonNegative();
1269 Known = Known.zextOrTrunc(BitWidth);
1270break;
1271 }
1272case Instruction::BitCast: {
1273Type *SrcTy =I->getOperand(0)->getType();
1274if (SrcTy->isIntOrPtrTy() &&
1275// TODO: For now, not handling conversions like:
1276// (bitcast i64 %x to <2 x i32>)
1277 !I->getType()->isVectorTy()) {
1278computeKnownBits(I->getOperand(0), Known,Depth + 1, Q);
1279break;
1280 }
1281
1282constValue *V;
1283// Handle bitcast from floating point to integer.
1284if (match(I,m_ElementWiseBitCast(m_Value(V))) &&
1285 V->getType()->isFPOrFPVectorTy()) {
1286Type *FPType = V->getType()->getScalarType();
1287KnownFPClass Result =
1288computeKnownFPClass(V, DemandedElts,fcAllFlags,Depth + 1, Q);
1289FPClassTest FPClasses = Result.KnownFPClasses;
1290
1291// TODO: Treat it as zero/poison if the use of I is unreachable.
1292if (FPClasses ==fcNone)
1293break;
1294
1295if (Result.isKnownNever(fcNormal |fcSubnormal |fcNan)) {
1296 Known.Zero.setAllBits();
1297 Known.One.setAllBits();
1298
1299if (FPClasses &fcInf)
1300 Known = Known.intersectWith(KnownBits::makeConstant(
1301APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt()));
1302
1303if (FPClasses &fcZero)
1304 Known = Known.intersectWith(KnownBits::makeConstant(
1305APInt::getZero(FPType->getScalarSizeInBits())));
1306
1307 Known.Zero.clearSignBit();
1308 Known.One.clearSignBit();
1309 }
1310
1311if (Result.SignBit) {
1312if (*Result.SignBit)
1313 Known.makeNegative();
1314else
1315 Known.makeNonNegative();
1316 }
1317
1318break;
1319 }
1320
1321// Handle cast from vector integer type to scalar or vector integer.
1322auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcTy);
1323if (!SrcVecTy || !SrcVecTy->getElementType()->isIntegerTy() ||
1324 !I->getType()->isIntOrIntVectorTy() ||
1325 isa<ScalableVectorType>(I->getType()))
1326break;
1327
1328// Look through a cast from narrow vector elements to wider type.
1329// Examples: v4i32 -> v2i64, v3i8 -> v24
1330unsigned SubBitWidth = SrcVecTy->getScalarSizeInBits();
1331if (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.
1346unsigned NumElts = DemandedElts.getBitWidth();
1347unsigned SubScale =BitWidth / SubBitWidth;
1348APInt SubDemandedElts =APInt::getZero(NumElts * SubScale);
1349for (unsigned i = 0; i != NumElts; ++i) {
1350if (DemandedElts[i])
1351 SubDemandedElts.setBit(i * SubScale);
1352 }
1353
1354KnownBits KnownSrc(SubBitWidth);
1355for (unsigned i = 0; i != SubScale; ++i) {
1356computeKnownBits(I->getOperand(0), SubDemandedElts.shl(i), KnownSrc,
1357Depth + 1, Q);
1358unsigned ShiftElt = Q.DL.isLittleEndian() ? i : SubScale - 1 - i;
1359 Known.insertBits(KnownSrc, ShiftElt * SubBitWidth);
1360 }
1361 }
1362break;
1363 }
1364case Instruction::SExt: {
1365// Compute the bits in the result that are not present in the input.
1366unsigned SrcBitWidth =I->getOperand(0)->getType()->getScalarSizeInBits();
1367
1368 Known = Known.trunc(SrcBitWidth);
1369computeKnownBits(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);
1373break;
1374 }
1375case Instruction::Shl: {
1376bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1377bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1378auto KF = [NUW, NSW](constKnownBits &KnownVal,constKnownBits &KnownAmt,
1379bool ShAmtNonZero) {
1380returnKnownBits::shl(KnownVal, KnownAmt, NUW, NSW, ShAmtNonZero);
1381 };
1382computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2,Depth, Q,
1383 KF);
1384// Trailing zeros of a right-shifted constant never decrease.
1385constAPInt *C;
1386if (match(I->getOperand(0),m_APInt(C)))
1387 Known.Zero.setLowBits(C->countr_zero());
1388break;
1389 }
1390case Instruction::LShr: {
1391bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1392auto KF = [Exact](constKnownBits &KnownVal,constKnownBits &KnownAmt,
1393bool ShAmtNonZero) {
1394returnKnownBits::lshr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1395 };
1396computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2,Depth, Q,
1397 KF);
1398// Leading zeros of a left-shifted constant never decrease.
1399constAPInt *C;
1400if (match(I->getOperand(0),m_APInt(C)))
1401 Known.Zero.setHighBits(C->countl_zero());
1402break;
1403 }
1404case Instruction::AShr: {
1405bool Exact = Q.IIQ.isExact(cast<BinaryOperator>(I));
1406auto KF = [Exact](constKnownBits &KnownVal,constKnownBits &KnownAmt,
1407bool ShAmtNonZero) {
1408returnKnownBits::ashr(KnownVal, KnownAmt, ShAmtNonZero, Exact);
1409 };
1410computeKnownBitsFromShiftOperator(I, DemandedElts, Known, Known2,Depth, Q,
1411 KF);
1412break;
1413 }
1414case Instruction::Sub: {
1415bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1416bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1417computeKnownBitsAddSub(false,I->getOperand(0),I->getOperand(1), NSW, NUW,
1418 DemandedElts, Known, Known2,Depth, Q);
1419break;
1420 }
1421case Instruction::Add: {
1422bool NSW = Q.IIQ.hasNoSignedWrap(cast<OverflowingBinaryOperator>(I));
1423bool NUW = Q.IIQ.hasNoUnsignedWrap(cast<OverflowingBinaryOperator>(I));
1424computeKnownBitsAddSub(true,I->getOperand(0),I->getOperand(1), NSW, NUW,
1425 DemandedElts, Known, Known2,Depth, Q);
1426break;
1427 }
1428case Instruction::SRem:
1429computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1430computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1431 Known =KnownBits::srem(Known, Known2);
1432break;
1433
1434case Instruction::URem:
1435computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1436computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1437 Known =KnownBits::urem(Known, Known2);
1438break;
1439case Instruction::Alloca:
1440 Known.Zero.setLowBits(Log2(cast<AllocaInst>(I)->getAlign()));
1441break;
1442case Instruction::GetElementPtr: {
1443// Analyze all of the subscripts of this getelementptr instruction
1444// to determine if we can prove known low zero bits.
1445computeKnownBits(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.
1448APInt AccConstIndices(BitWidth, 0,/*IsSigned*/true);
1449
1450gep_type_iterator GTI =gep_type_begin(I);
1451for (unsigned i = 1, e =I->getNumOperands(); i != e; ++i, ++GTI) {
1452// TrailZ can only become smaller, short-circuit if we hit zero.
1453if (Known.isUnknown())
1454break;
1455
1456Value *Index =I->getOperand(i);
1457
1458// Handle case when index is zero.
1459Constant *CIndex = dyn_cast<Constant>(Index);
1460if (CIndex && CIndex->isZeroValue())
1461continue;
1462
1463if (StructType *STy = GTI.getStructTypeOrNull()) {
1464// Handle struct member offset arithmetic.
1465
1466assert(CIndex &&
1467"Access to structure field must be known at compile time");
1468
1469if (CIndex->getType()->isVectorTy())
1470 Index = CIndex->getSplatValue();
1471
1472unsignedIdx = cast<ConstantInt>(Index)->getZExtValue();
1473constStructLayout *SL = Q.DL.getStructLayout(STy);
1474uint64_tOffset = SL->getElementOffset(Idx);
1475 AccConstIndices +=Offset;
1476continue;
1477 }
1478
1479// Handle array index arithmetic.
1480Type *IndexedTy = GTI.getIndexedType();
1481if (!IndexedTy->isSized()) {
1482 Known.resetAll();
1483break;
1484 }
1485
1486unsigned IndexBitWidth = Index->getType()->getScalarSizeInBits();
1487KnownBits IndexBits(IndexBitWidth);
1488computeKnownBits(Index, IndexBits,Depth + 1, Q);
1489TypeSize IndexTypeSize = GTI.getSequentialElementStride(Q.DL);
1490uint64_t TypeSizeInBytes = IndexTypeSize.getKnownMinValue();
1491KnownBits ScalingFactor(IndexBitWidth);
1492// Multiply by current sizeof type.
1493// &A[i] == A + i * sizeof(*A[i]).
1494if (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 }elseif (IndexBits.isConstant()) {
1499APInt IndexConst = IndexBits.getConstant();
1500APInt ScalingFactor(IndexBitWidth, TypeSizeInBytes);
1501 IndexConst *= ScalingFactor;
1502 AccConstIndices += IndexConst.sextOrTrunc(BitWidth);
1503continue;
1504 }else {
1505 ScalingFactor =
1506KnownBits::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 }
1519if (!Known.isUnknown() && !AccConstIndices.isZero()) {
1520KnownBits Index =KnownBits::makeConstant(AccConstIndices);
1521 Known =KnownBits::add(Known, Index);
1522 }
1523break;
1524 }
1525case Instruction::PHI: {
1526constPHINode *P = cast<PHINode>(I);
1527BinaryOperator *BO =nullptr;
1528Value *R =nullptr, *L =nullptr;
1529if (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.
1533unsigned Opcode = BO->getOpcode();
1534
1535switch (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.
1541case Instruction::LShr:
1542case Instruction::AShr:
1543case Instruction::Shl:
1544case Instruction::UDiv:
1545if (BO->getOperand(0) !=I)
1546break;
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.
1551case 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.
1560SimplifyQuery RecQ = Q.getWithoutCondContext();
1561 RecQ.CxtI =P;
1562computeKnownBits(R, DemandedElts, Known2,Depth + 1, RecQ);
1563switch (Opcode) {
1564case Instruction::Shl:
1565// A shl recurrence will only increase the tailing zeros
1566 Known.Zero.setLowBits(Known2.countMinTrailingZeros());
1567break;
1568case Instruction::LShr:
1569case Instruction::UDiv:
1570case Instruction::URem:
1571// lshr, udiv, and urem recurrences will preserve the leading zeros of
1572// the start value.
1573 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1574break;
1575case Instruction::AShr:
1576// An ashr recurrence will extend the initial sign bit
1577 Known.Zero.setHighBits(Known2.countMinLeadingZeros());
1578 Known.One.setHighBits(Known2.countMinLeadingOnes());
1579break;
1580 }
1581break;
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.
1587case Instruction::Add:
1588case Instruction::Sub:
1589case Instruction::And:
1590case Instruction::Or:
1591case 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).
1596SimplifyQuery RecQ = Q.getWithoutCondContext();
1597
1598unsigned OpNum =P->getOperand(0) == R ? 0 : 1;
1599Instruction *RInst =P->getIncomingBlock(OpNum)->getTerminator();
1600Instruction *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;
1605computeKnownBits(R, DemandedElts, Known2,Depth + 1, RecQ);
1606
1607// We need to take the minimum number of known bits
1608KnownBits Known3(BitWidth);
1609 RecQ.CxtI = LInst;
1610computeKnownBits(L, DemandedElts, Known3,Depth + 1, RecQ);
1611
1612 Known.Zero.setLowBits(std::min(Known2.countMinTrailingZeros(),
1613 Known3.countMinTrailingZeros()));
1614
1615auto *OverflowOp = dyn_cast<OverflowingBinaryOperator>(BO);
1616if (!OverflowOp || !Q.IIQ.hasNoSignedWrap(OverflowOp))
1617break;
1618
1619switch (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
1629case Instruction::Add: {
1630if (Known2.isNonNegative() && Known3.isNonNegative())
1631 Known.makeNonNegative();
1632elseif (Known2.isNegative() && Known3.isNegative())
1633 Known.makeNegative();
1634break;
1635 }
1636
1637// (sub nsw non-negative, negative) --> non-negative
1638// (sub nsw negative, non-negative) --> negative
1639case Instruction::Sub: {
1640if (BO->getOperand(0) !=I)
1641break;
1642if (Known2.isNonNegative() && Known3.isNegative())
1643 Known.makeNonNegative();
1644elseif (Known2.isNegative() && Known3.isNonNegative())
1645 Known.makeNegative();
1646break;
1647 }
1648
1649// (mul nsw non-negative, non-negative) --> non-negative
1650case Instruction::Mul:
1651if (Known2.isNonNegative() && Known3.isNonNegative())
1652 Known.makeNonNegative();
1653break;
1654
1655default:
1656break;
1657 }
1658break;
1659 }
1660
1661default:
1662break;
1663 }
1664 }
1665
1666// Unreachable blocks may have zero-operand PHI nodes.
1667if (P->getNumIncomingValues() == 0)
1668break;
1669
1670// Otherwise take the unions of the known bit sets of the operands,
1671// taking conservative care to avoid excessive recursion.
1672if (Depth <MaxAnalysisRecursionDepth - 1 && Known.isUnknown()) {
1673// Skip if every incoming value references to ourself.
1674if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
1675break;
1676
1677 Known.Zero.setAllBits();
1678 Known.One.setAllBits();
1679for (constUse &U :P->operands()) {
1680Value *IncValue;
1681constPHINode *CxtPhi;
1682Instruction *CxtI;
1683breakSelfRecursivePHI(&U,P, IncValue, CxtI, &CxtPhi);
1684// Skip direct self references.
1685if (IncValue ==P)
1686continue;
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).
1692SimplifyQuery RecQ = Q.getWithoutCondContext().getWithInstruction(CxtI);
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.
1700computeKnownBits(IncValue, DemandedElts, Known2,
1701MaxAnalysisRecursionDepth - 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.
1705if (!Known2.isConstant()) {
1706CmpPredicate Pred;
1707constAPInt *RHSC;
1708BasicBlock *TrueSucc, *FalseSucc;
1709// TODO: Use RHS Value and compute range from its known bits.
1710if (match(RecQ.CxtI,
1711m_Br(m_c_ICmp(Pred,m_Specific(IncValue),m_APInt(RHSC)),
1712m_BasicBlock(TrueSucc),m_BasicBlock(FalseSucc)))) {
1713// Check for cases of duplicate successors.
1714if ((TrueSucc == CxtPhi->getParent()) !=
1715 (FalseSucc == CxtPhi->getParent())) {
1716// If we're using the false successor, invert the predicate.
1717if (FalseSucc == CxtPhi->getParent())
1718 Pred =CmpInst::getInversePredicate(Pred);
1719// Get the knownbits implied by the incoming phi condition.
1720auto CR =ConstantRange::makeExactICmpRegion(Pred, *RHSC);
1721KnownBits 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).
1724if (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();
1729break;
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.
1739if (Known.isUnknown())
1740break;
1741 }
1742 }
1743break;
1744 }
1745case Instruction::Call:
1746case 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.
1750if (MDNode *MD =
1751 Q.IIQ.getMetadata(cast<Instruction>(I), LLVMContext::MD_range))
1752computeKnownBitsFromRangeMetadata(*MD, Known);
1753
1754constauto *CB = cast<CallBase>(I);
1755
1756if (std::optional<ConstantRange>Range = CB->getRange())
1757 Known = Known.unionWith(Range->toKnownBits());
1758
1759if (constValue *RV = CB->getReturnedArgOperand()) {
1760if (RV->getType() ==I->getType()) {
1761computeKnownBits(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.
1767if (Known.hasConflict())
1768 Known.resetAll();
1769 }
1770 }
1771if (constIntrinsicInst *II = dyn_cast<IntrinsicInst>(I)) {
1772switch (II->getIntrinsicID()) {
1773default:
1774break;
1775case Intrinsic::abs: {
1776computeKnownBits(I->getOperand(0), DemandedElts, Known2,Depth + 1, Q);
1777bool IntMinIsPoison =match(II->getArgOperand(1),m_One());
1778 Known = Known2.abs(IntMinIsPoison);
1779break;
1780 }
1781case Intrinsic::bitreverse:
1782computeKnownBits(I->getOperand(0), DemandedElts, Known2,Depth + 1, Q);
1783 Known.Zero |= Known2.Zero.reverseBits();
1784 Known.One |= Known2.One.reverseBits();
1785break;
1786case Intrinsic::bswap:
1787computeKnownBits(I->getOperand(0), DemandedElts, Known2,Depth + 1, Q);
1788 Known.Zero |= Known2.Zero.byteSwap();
1789 Known.One |= Known2.One.byteSwap();
1790break;
1791case Intrinsic::ctlz: {
1792computeKnownBits(I->getOperand(0), DemandedElts, Known2,Depth + 1, Q);
1793// If we have a known 1, its position is our upper bound.
1794unsigned PossibleLZ = Known2.countMaxLeadingZeros();
1795// If this call is poison for 0 input, the result will be less than 2^n.
1796if (II->getArgOperand(1) ==ConstantInt::getTrue(II->getContext()))
1797 PossibleLZ = std::min(PossibleLZ,BitWidth - 1);
1798unsigned LowBits =llvm::bit_width(PossibleLZ);
1799 Known.Zero.setBitsFrom(LowBits);
1800break;
1801 }
1802case Intrinsic::cttz: {
1803computeKnownBits(I->getOperand(0), DemandedElts, Known2,Depth + 1, Q);
1804// If we have a known 1, its position is our upper bound.
1805unsigned PossibleTZ = Known2.countMaxTrailingZeros();
1806// If this call is poison for 0 input, the result will be less than 2^n.
1807if (II->getArgOperand(1) ==ConstantInt::getTrue(II->getContext()))
1808 PossibleTZ = std::min(PossibleTZ,BitWidth - 1);
1809unsigned LowBits =llvm::bit_width(PossibleTZ);
1810 Known.Zero.setBitsFrom(LowBits);
1811break;
1812 }
1813case Intrinsic::ctpop: {
1814computeKnownBits(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.
1817unsigned BitsPossiblySet = Known2.countMaxPopulation();
1818unsigned 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.
1822break;
1823 }
1824case Intrinsic::fshr:
1825case Intrinsic::fshl: {
1826constAPInt *SA;
1827if (!match(I->getOperand(2),m_APInt(SA)))
1828break;
1829
1830// Normalize to funnel shift left.
1831uint64_t ShiftAmt = SA->urem(BitWidth);
1832if (II->getIntrinsicID() == Intrinsic::fshr)
1833 ShiftAmt =BitWidth - ShiftAmt;
1834
1835KnownBits Known3(BitWidth);
1836computeKnownBits(I->getOperand(0), DemandedElts, Known2,Depth + 1, Q);
1837computeKnownBits(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);
1843break;
1844 }
1845case Intrinsic::uadd_sat:
1846computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1847computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1848 Known =KnownBits::uadd_sat(Known, Known2);
1849break;
1850case Intrinsic::usub_sat:
1851computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1852computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1853 Known =KnownBits::usub_sat(Known, Known2);
1854break;
1855case Intrinsic::sadd_sat:
1856computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1857computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1858 Known =KnownBits::sadd_sat(Known, Known2);
1859break;
1860case Intrinsic::ssub_sat:
1861computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1862computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1863 Known =KnownBits::ssub_sat(Known, Known2);
1864break;
1865// Vec reverse preserves bits from input vec.
1866case Intrinsic::vector_reverse:
1867computeKnownBits(I->getOperand(0), DemandedElts.reverseBits(), Known,
1868Depth + 1, Q);
1869break;
1870// for min/max/and/or reduce, any bit common to each element in the
1871// input vec is set in the output.
1872case Intrinsic::vector_reduce_and:
1873case Intrinsic::vector_reduce_or:
1874case Intrinsic::vector_reduce_umax:
1875case Intrinsic::vector_reduce_umin:
1876case Intrinsic::vector_reduce_smax:
1877case Intrinsic::vector_reduce_smin:
1878computeKnownBits(I->getOperand(0), Known,Depth + 1, Q);
1879break;
1880case Intrinsic::vector_reduce_xor: {
1881computeKnownBits(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.
1885auto *VecTy = cast<VectorType>(I->getOperand(0)->getType());
1886// Even, so the ones become zeros.
1887bool EvenCnt = VecTy->getElementCount().isKnownEven();
1888if (EvenCnt)
1889 Known.Zero |= Known.One;
1890// Maybe even element count so need to clear ones.
1891if (VecTy->isScalableTy() || EvenCnt)
1892 Known.One.clearAllBits();
1893break;
1894 }
1895case Intrinsic::umin:
1896computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1897computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1898 Known =KnownBits::umin(Known, Known2);
1899break;
1900case Intrinsic::umax:
1901computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1902computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1903 Known =KnownBits::umax(Known, Known2);
1904break;
1905case Intrinsic::smin:
1906computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1907computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1908 Known =KnownBits::smin(Known, Known2);
1909unionWithMinMaxIntrinsicClamp(II, Known);
1910break;
1911case Intrinsic::smax:
1912computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1913computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1914 Known =KnownBits::smax(Known, Known2);
1915unionWithMinMaxIntrinsicClamp(II, Known);
1916break;
1917case Intrinsic::ptrmask: {
1918computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1919
1920constValue *Mask =I->getOperand(1);
1921 Known2 =KnownBits(Mask->getType()->getScalarSizeInBits());
1922computeKnownBits(Mask, DemandedElts, Known2,Depth + 1, Q);
1923// TODO: 1-extend would be more precise.
1924 Known &= Known2.anyextOrTrunc(BitWidth);
1925break;
1926 }
1927case Intrinsic::x86_sse2_pmulh_w:
1928case Intrinsic::x86_avx2_pmulh_w:
1929case Intrinsic::x86_avx512_pmulh_w_512:
1930computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1931computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1932 Known =KnownBits::mulhs(Known, Known2);
1933break;
1934case Intrinsic::x86_sse2_pmulhu_w:
1935case Intrinsic::x86_avx2_pmulhu_w:
1936case Intrinsic::x86_avx512_pmulhu_w_512:
1937computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth + 1, Q);
1938computeKnownBits(I->getOperand(1), DemandedElts, Known2,Depth + 1, Q);
1939 Known =KnownBits::mulhu(Known, Known2);
1940break;
1941case Intrinsic::x86_sse42_crc32_64_64:
1942 Known.Zero.setBitsFrom(32);
1943break;
1944case Intrinsic::x86_ssse3_phadd_d_128:
1945case Intrinsic::x86_ssse3_phadd_w_128:
1946case Intrinsic::x86_avx2_phadd_d:
1947case Intrinsic::x86_avx2_phadd_w: {
1948 Known =computeKnownBitsForHorizontalOperation(
1949I, DemandedElts,Depth, Q,
1950 [](constKnownBits &KnownLHS,constKnownBits &KnownRHS) {
1951returnKnownBits::add(KnownLHS, KnownRHS);
1952 });
1953break;
1954 }
1955case Intrinsic::x86_ssse3_phadd_sw_128:
1956case Intrinsic::x86_avx2_phadd_sw: {
1957 Known =computeKnownBitsForHorizontalOperation(I, DemandedElts,Depth,
1958 Q,KnownBits::sadd_sat);
1959break;
1960 }
1961case Intrinsic::x86_ssse3_phsub_d_128:
1962case Intrinsic::x86_ssse3_phsub_w_128:
1963case Intrinsic::x86_avx2_phsub_d:
1964case Intrinsic::x86_avx2_phsub_w: {
1965 Known =computeKnownBitsForHorizontalOperation(
1966I, DemandedElts,Depth, Q,
1967 [](constKnownBits &KnownLHS,constKnownBits &KnownRHS) {
1968returnKnownBits::sub(KnownLHS, KnownRHS);
1969 });
1970break;
1971 }
1972case Intrinsic::x86_ssse3_phsub_sw_128:
1973case Intrinsic::x86_avx2_phsub_sw: {
1974 Known =computeKnownBitsForHorizontalOperation(I, DemandedElts,Depth,
1975 Q,KnownBits::ssub_sat);
1976break;
1977 }
1978case Intrinsic::riscv_vsetvli:
1979case Intrinsic::riscv_vsetvlimax: {
1980bool HasAVL =II->getIntrinsicID() == Intrinsic::riscv_vsetvli;
1981constConstantRangeRange =getVScaleRange(II->getFunction(),BitWidth);
1982uint64_t SEW =RISCVVType::decodeVSEW(
1983 cast<ConstantInt>(II->getArgOperand(HasAVL))->getZExtValue());
1984RISCVII::VLMUL VLMUL =static_cast<RISCVII::VLMUL>(
1985 cast<ConstantInt>(II->getArgOperand(1 + HasAVL))->getZExtValue());
1986uint64_t MaxVLEN =
1987Range.getUnsignedMax().getZExtValue() *RISCV::RVVBitsPerBlock;
1988uint64_t MaxVL = MaxVLEN /RISCVVType::getSEWLMULRatio(SEW, VLMUL);
1989
1990// Result of vsetvli must be not larger than AVL.
1991if (HasAVL)
1992if (auto *CI = dyn_cast<ConstantInt>(II->getArgOperand(0)))
1993 MaxVL = std::min(MaxVL, CI->getZExtValue());
1994
1995unsigned KnownZeroFirstBit =Log2_32(MaxVL) + 1;
1996if (BitWidth > KnownZeroFirstBit)
1997 Known.Zero.setBitsFrom(KnownZeroFirstBit);
1998break;
1999 }
2000case Intrinsic::vscale: {
2001if (!II->getParent() || !II->getFunction())
2002break;
2003
2004 Known =getVScaleRange(II->getFunction(),BitWidth).toKnownBits();
2005break;
2006 }
2007 }
2008 }
2009break;
2010 }
2011case Instruction::ShuffleVector: {
2012auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
2013// FIXME: Do we need to handle ConstantExpr involving shufflevectors?
2014if (!Shuf) {
2015 Known.resetAll();
2016return;
2017 }
2018// For undef elements, we don't know anything about the common state of
2019// the shuffle result.
2020APInt DemandedLHS, DemandedRHS;
2021if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS)) {
2022 Known.resetAll();
2023return;
2024 }
2025 Known.One.setAllBits();
2026 Known.Zero.setAllBits();
2027if (!!DemandedLHS) {
2028constValue *LHS = Shuf->getOperand(0);
2029computeKnownBits(LHS, DemandedLHS, Known,Depth + 1, Q);
2030// If we don't know any bits, early out.
2031if (Known.isUnknown())
2032break;
2033 }
2034if (!!DemandedRHS) {
2035constValue *RHS = Shuf->getOperand(1);
2036computeKnownBits(RHS, DemandedRHS, Known2,Depth + 1, Q);
2037 Known = Known.intersectWith(Known2);
2038 }
2039break;
2040 }
2041case Instruction::InsertElement: {
2042if (isa<ScalableVectorType>(I->getType())) {
2043 Known.resetAll();
2044return;
2045 }
2046constValue *Vec =I->getOperand(0);
2047constValue *Elt =I->getOperand(1);
2048auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
2049unsigned NumElts = DemandedElts.getBitWidth();
2050APInt DemandedVecElts = DemandedElts;
2051bool NeedsElt =true;
2052// If we know the index we are inserting too, clear it from Vec check.
2053if (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();
2060if (NeedsElt) {
2061computeKnownBits(Elt, Known,Depth + 1, Q);
2062// If we don't know any bits, early out.
2063if (Known.isUnknown())
2064break;
2065 }
2066
2067if (!DemandedVecElts.isZero()) {
2068computeKnownBits(Vec, DemandedVecElts, Known2,Depth + 1, Q);
2069 Known = Known.intersectWith(Known2);
2070 }
2071break;
2072 }
2073case 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.
2076constValue *Vec =I->getOperand(0);
2077constValue *Idx =I->getOperand(1);
2078auto *CIdx = dyn_cast<ConstantInt>(Idx);
2079if (isa<ScalableVectorType>(Vec->getType())) {
2080// FIXME: there's probably *something* we can do with scalable vectors
2081 Known.resetAll();
2082break;
2083 }
2084unsigned NumElts = cast<FixedVectorType>(Vec->getType())->getNumElements();
2085APInt DemandedVecElts =APInt::getAllOnes(NumElts);
2086if (CIdx && CIdx->getValue().ult(NumElts))
2087 DemandedVecElts =APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
2088computeKnownBits(Vec, DemandedVecElts, Known,Depth + 1, Q);
2089break;
2090 }
2091case Instruction::ExtractValue:
2092if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(I->getOperand(0))) {
2093constExtractValueInst *EVI = cast<ExtractValueInst>(I);
2094if (EVI->getNumIndices() != 1)break;
2095if (EVI->getIndices()[0] == 0) {
2096switch (II->getIntrinsicID()) {
2097default:break;
2098case Intrinsic::uadd_with_overflow:
2099case Intrinsic::sadd_with_overflow:
2100computeKnownBitsAddSub(
2101true,II->getArgOperand(0),II->getArgOperand(1),/*NSW=*/false,
2102/* NUW=*/false, DemandedElts, Known, Known2,Depth, Q);
2103break;
2104case Intrinsic::usub_with_overflow:
2105case Intrinsic::ssub_with_overflow:
2106computeKnownBitsAddSub(
2107false,II->getArgOperand(0),II->getArgOperand(1),/*NSW=*/false,
2108/* NUW=*/false, DemandedElts, Known, Known2,Depth, Q);
2109break;
2110case Intrinsic::umul_with_overflow:
2111case Intrinsic::smul_with_overflow:
2112computeKnownBitsMul(II->getArgOperand(0),II->getArgOperand(1),false,
2113false, DemandedElts, Known, Known2,Depth, Q);
2114break;
2115 }
2116 }
2117 }
2118break;
2119case Instruction::Freeze:
2120if (isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
2121Depth + 1))
2122computeKnownBits(I->getOperand(0), Known,Depth + 1, Q);
2123break;
2124 }
2125}
2126
2127/// Determine which bits of V are known to be either zero or one and return
2128/// them.
2129KnownBitsllvm::computeKnownBits(constValue *V,constAPInt &DemandedElts,
2130unsignedDepth,constSimplifyQuery &Q) {
2131KnownBits Known(getBitWidth(V->getType(), Q.DL));
2132::computeKnownBits(V, DemandedElts, Known,Depth, Q);
2133return Known;
2134}
2135
2136/// Determine which bits of V are known to be either zero or one and return
2137/// them.
2138KnownBitsllvm::computeKnownBits(constValue *V,unsignedDepth,
2139constSimplifyQuery &Q) {
2140KnownBits Known(getBitWidth(V->getType(), Q.DL));
2141computeKnownBits(V, Known,Depth, Q);
2142return 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.
2160voidcomputeKnownBits(constValue *V,constAPInt &DemandedElts,
2161KnownBits &Known,unsignedDepth,
2162constSimplifyQuery &Q) {
2163if (!DemandedElts) {
2164// No demanded elts, better to assume we don't know anything.
2165 Known.resetAll();
2166return;
2167 }
2168
2169assert(V &&"No Value?");
2170assert(Depth <=MaxAnalysisRecursionDepth &&"Limit Search Depth");
2171
2172#ifndef NDEBUG
2173Type *Ty = V->getType();
2174unsignedBitWidth = Known.getBitWidth();
2175
2176assert((Ty->isIntOrIntVectorTy(BitWidth) || Ty->isPtrOrPtrVectorTy()) &&
2177"Not integer or pointer type!");
2178
2179if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
2180assert(
2181 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
2182"DemandedElt width should equal the fixed vector number of elements");
2183 }else {
2184assert(DemandedElts ==APInt(1, 1) &&
2185"DemandedElt width should be 1 for scalars or scalable vectors");
2186 }
2187
2188Type *ScalarTy = Ty->getScalarType();
2189if (ScalarTy->isPointerTy()) {
2190assert(BitWidth == Q.DL.getPointerTypeSizeInBits(ScalarTy) &&
2191"V and Known should have same BitWidth");
2192 }else {
2193assert(BitWidth == Q.DL.getTypeSizeInBits(ScalarTy) &&
2194"V and Known should have same BitWidth");
2195 }
2196#endif
2197
2198constAPInt *C;
2199if (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);
2202return;
2203 }
2204// Null and aggregate-zero are all-zeros.
2205if (isa<ConstantPointerNull>(V) || isa<ConstantAggregateZero>(V)) {
2206 Known.setAllZero();
2207return;
2208 }
2209// Handle a constant vector by taking the intersection of the known bits of
2210// each element.
2211if (constConstantDataVector *CDV = dyn_cast<ConstantDataVector>(V)) {
2212assert(!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();
2216for (unsigned i = 0, e = CDV->getNumElements(); i != e; ++i) {
2217if (!DemandedElts[i])
2218continue;
2219APInt Elt = CDV->getElementAsAPInt(i);
2220 Known.Zero &= ~Elt;
2221 Known.One &= Elt;
2222 }
2223if (Known.hasConflict())
2224 Known.resetAll();
2225return;
2226 }
2227
2228if (constauto *CV = dyn_cast<ConstantVector>(V)) {
2229assert(!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();
2233for (unsigned i = 0, e = CV->getNumOperands(); i != e; ++i) {
2234if (!DemandedElts[i])
2235continue;
2236Constant *Element = CV->getAggregateElement(i);
2237if (isa<PoisonValue>(Element))
2238continue;
2239auto *ElementCI = dyn_cast_or_null<ConstantInt>(Element);
2240if (!ElementCI) {
2241 Known.resetAll();
2242return;
2243 }
2244constAPInt &Elt = ElementCI->getValue();
2245 Known.Zero &= ~Elt;
2246 Known.One &= Elt;
2247 }
2248if (Known.hasConflict())
2249 Known.resetAll();
2250return;
2251 }
2252
2253// Start out not knowing anything.
2254 Known.resetAll();
2255
2256// We can't imply anything about undefs.
2257if (isa<UndefValue>(V))
2258return;
2259
2260// There's no point in looking through other users of ConstantData for
2261// assumptions. Confirm that we've handled them all.
2262assert(!isa<ConstantData>(V) &&"Unhandled constant data!");
2263
2264if (constauto *A = dyn_cast<Argument>(V))
2265if (std::optional<ConstantRange>Range =A->getRange())
2266 Known =Range->toKnownBits();
2267
2268// All recursive calls that increase depth must come after this.
2269if (Depth ==MaxAnalysisRecursionDepth)
2270return;
2271
2272// A weak GlobalAlias is totally unknown. A non-weak GlobalAlias has
2273// the bits of its aliasee.
2274if (constGlobalAlias *GA = dyn_cast<GlobalAlias>(V)) {
2275if (!GA->isInterposable())
2276computeKnownBits(GA->getAliasee(), Known,Depth + 1, Q);
2277return;
2278 }
2279
2280if (constOperator *I = dyn_cast<Operator>(V))
2281computeKnownBitsFromOperator(I, DemandedElts, Known,Depth, Q);
2282elseif (constGlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2283if (std::optional<ConstantRange> CR = GV->getAbsoluteSymbolRange())
2284 Known = CR->toKnownBits();
2285 }
2286
2287// Aligned pointers have trailing zeros - refine Known.Zero set
2288if (isa<PointerType>(V->getType())) {
2289Align 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.
2297computeKnownBitsFromContext(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).
2302staticboolisPowerOfTwoRecurrence(constPHINode *PN,bool OrZero,
2303unsignedDepth,SimplifyQuery &Q) {
2304BinaryOperator *BO =nullptr;
2305Value *Start =nullptr, *Step =nullptr;
2306if (!matchSimpleRecurrence(PN, BO, Start, Step))
2307returnfalse;
2308
2309// Initial value must be a power of two.
2310for (constUse &U : PN->operands()) {
2311if (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();
2315if (!isKnownToBeAPowerOfTwo(Start, OrZero,Depth, Q))
2316returnfalse;
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.
2322if (BO->getOpcode() != Instruction::Mul && BO->getOperand(1) != Step)
2323returnfalse;
2324
2325 Q.CxtI = BO->getParent()->getTerminator();
2326switch (BO->getOpcode()) {
2327case Instruction::Mul:
2328// Power of two is closed under multiplication.
2329return (OrZero || Q.IIQ.hasNoUnsignedWrap(BO) ||
2330 Q.IIQ.hasNoSignedWrap(BO)) &&
2331isKnownToBeAPowerOfTwo(Step, OrZero,Depth, Q);
2332case 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.
2335if (!match(Start,m_Power2()) ||match(Start,m_SignMask()))
2336returnfalse;
2337 [[fallthrough]];
2338case 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.
2342return (OrZero || Q.IIQ.isExact(BO)) &&
2343isKnownToBeAPowerOfTwo(Step,false,Depth, Q);
2344case Instruction::Shl:
2345return OrZero || Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO);
2346case Instruction::AShr:
2347if (!match(Start,m_Power2()) ||match(Start,m_SignMask()))
2348returnfalse;
2349 [[fallthrough]];
2350case Instruction::LShr:
2351return OrZero || Q.IIQ.isExact(BO);
2352default:
2353returnfalse;
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).
2359staticboolisImpliedToBeAPowerOfTwoFromCond(constValue *V,bool OrZero,
2360constValue *Cond,
2361bool CondIsTrue) {
2362CmpPredicate Pred;
2363constAPInt *RHSC;
2364if (!match(Cond,m_ICmp(Pred, m_Intrinsic<Intrinsic::ctpop>(m_Specific(V)),
2365m_APInt(RHSC))))
2366returnfalse;
2367if (!CondIsTrue)
2368 Pred = ICmpInst::getInversePredicate(Pred);
2369// ctpop(V) u< 2
2370if (OrZero && Pred == ICmpInst::ICMP_ULT && *RHSC == 2)
2371returntrue;
2372// ctpop(V) == 1
2373return 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.
2380boolllvm::isKnownToBeAPowerOfTwo(constValue *V,bool OrZero,unsignedDepth,
2381constSimplifyQuery &Q) {
2382assert(Depth <=MaxAnalysisRecursionDepth &&"Limit Search Depth");
2383
2384if (isa<Constant>(V))
2385return OrZero ?match(V,m_Power2OrZero()) :match(V,m_Power2());
2386
2387// i1 is by definition a power of 2 or zero.
2388if (OrZero && V->getType()->getScalarSizeInBits() == 1)
2389returntrue;
2390
2391// Try to infer from assumptions.
2392if (Q.AC && Q.CxtI) {
2393for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
2394if (!AssumeVH)
2395continue;
2396CallInst *I = cast<CallInst>(AssumeVH);
2397if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero,I->getArgOperand(0),
2398/*CondIsTrue=*/true) &&
2399isValidAssumeForContext(I, Q.CxtI, Q.DT))
2400returntrue;
2401 }
2402 }
2403
2404// Handle dominating conditions.
2405if (Q.DC && Q.CxtI && Q.DT) {
2406for (BranchInst *BI : Q.DC->conditionsFor(V)) {
2407Value *Cond = BI->getCondition();
2408
2409BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
2410if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero,Cond,
2411/*CondIsTrue=*/true) &&
2412 Q.DT->dominates(Edge0, Q.CxtI->getParent()))
2413returntrue;
2414
2415BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
2416if (isImpliedToBeAPowerOfTwoFromCond(V, OrZero,Cond,
2417/*CondIsTrue=*/false) &&
2418 Q.DT->dominates(Edge1, Q.CxtI->getParent()))
2419returntrue;
2420 }
2421 }
2422
2423auto *I = dyn_cast<Instruction>(V);
2424if (!I)
2425returnfalse;
2426
2427if (Q.CxtI &&match(V,m_VScale())) {
2428constFunction *F = Q.CxtI->getFunction();
2429// The vscale_range indicates vscale is a power-of-two.
2430returnF->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.
2435if (match(I,m_Shl(m_One(),m_Value())))
2436returntrue;
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.
2440if (match(I,m_LShr(m_SignMask(),m_Value())))
2441returntrue;
2442
2443// The remaining tests are all recursive, so bail out if we hit the limit.
2444if (Depth++ ==MaxAnalysisRecursionDepth)
2445returnfalse;
2446
2447switch (I->getOpcode()) {
2448case Instruction::ZExt:
2449returnisKnownToBeAPowerOfTwo(I->getOperand(0), OrZero,Depth, Q);
2450case Instruction::Trunc:
2451return OrZero &&isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero,Depth, Q);
2452case Instruction::Shl:
2453if (OrZero || Q.IIQ.hasNoUnsignedWrap(I) || Q.IIQ.hasNoSignedWrap(I))
2454returnisKnownToBeAPowerOfTwo(I->getOperand(0), OrZero,Depth, Q);
2455returnfalse;
2456case Instruction::LShr:
2457if (OrZero || Q.IIQ.isExact(cast<BinaryOperator>(I)))
2458returnisKnownToBeAPowerOfTwo(I->getOperand(0), OrZero,Depth, Q);
2459returnfalse;
2460case Instruction::UDiv:
2461if (Q.IIQ.isExact(cast<BinaryOperator>(I)))
2462returnisKnownToBeAPowerOfTwo(I->getOperand(0), OrZero,Depth, Q);
2463returnfalse;
2464case Instruction::Mul:
2465returnisKnownToBeAPowerOfTwo(I->getOperand(1), OrZero,Depth, Q) &&
2466isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero,Depth, Q) &&
2467 (OrZero ||isKnownNonZero(I, Q,Depth));
2468case Instruction::And:
2469// A power of two and'd with anything is a power of two or zero.
2470if (OrZero &&
2471 (isKnownToBeAPowerOfTwo(I->getOperand(1),/*OrZero*/true,Depth, Q) ||
2472isKnownToBeAPowerOfTwo(I->getOperand(0),/*OrZero*/true,Depth, Q)))
2473returntrue;
2474// X & (-X) is always a power of two or zero.
2475if (match(I->getOperand(0),m_Neg(m_Specific(I->getOperand(1)))) ||
2476match(I->getOperand(1),m_Neg(m_Specific(I->getOperand(0)))))
2477return OrZero ||isKnownNonZero(I->getOperand(0), Q,Depth);
2478returnfalse;
2479case 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.
2482constOverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
2483if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO) ||
2484 Q.IIQ.hasNoSignedWrap(VOBO)) {
2485if (match(I->getOperand(0),
2486m_c_And(m_Specific(I->getOperand(1)),m_Value())) &&
2487isKnownToBeAPowerOfTwo(I->getOperand(1), OrZero,Depth, Q))
2488returntrue;
2489if (match(I->getOperand(1),
2490m_c_And(m_Specific(I->getOperand(0)),m_Value())) &&
2491isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero,Depth, Q))
2492returntrue;
2493
2494unsignedBitWidth = V->getType()->getScalarSizeInBits();
2495KnownBits LHSBits(BitWidth);
2496computeKnownBits(I->getOperand(0), LHSBits,Depth, Q);
2497
2498KnownBits RHSBits(BitWidth);
2499computeKnownBits(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
2503if ((~(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.
2506if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
2507returntrue;
2508 }
2509
2510// LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
2511if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
2512if (match(I,m_Add(m_LShr(m_AllOnes(),m_Value()),m_One())))
2513returntrue;
2514returnfalse;
2515 }
2516case Instruction::Select:
2517returnisKnownToBeAPowerOfTwo(I->getOperand(1), OrZero,Depth, Q) &&
2518isKnownToBeAPowerOfTwo(I->getOperand(2), OrZero,Depth, Q);
2519case 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.
2523auto *PN = cast<PHINode>(I);
2524SimplifyQuery RecQ = Q.getWithoutCondContext();
2525
2526// Check if it is an induction variable and always power of two.
2527if (isPowerOfTwoRecurrence(PN, OrZero,Depth, RecQ))
2528returntrue;
2529
2530// Recursively check all incoming values. Limit recursion to 2 levels, so
2531// that search complexity is limited to number of operands^2.
2532unsigned NewDepth = std::max(Depth,MaxAnalysisRecursionDepth - 1);
2533returnllvm::all_of(PN->operands(), [&](constUse &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 }
2544case Instruction::Invoke:
2545case Instruction::Call: {
2546if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2547switch (II->getIntrinsicID()) {
2548case Intrinsic::umax:
2549case Intrinsic::smax:
2550case Intrinsic::umin:
2551case Intrinsic::smin:
2552returnisKnownToBeAPowerOfTwo(II->getArgOperand(1), OrZero,Depth, Q) &&
2553isKnownToBeAPowerOfTwo(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.
2556case Intrinsic::bitreverse:
2557case Intrinsic::bswap:
2558returnisKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero,Depth, Q);
2559case Intrinsic::fshr:
2560case Intrinsic::fshl:
2561// If Op0 == Op1, this is a rotate. is_pow2(rotate(x, y)) == is_pow2(x)
2562if (II->getArgOperand(0) ==II->getArgOperand(1))
2563returnisKnownToBeAPowerOfTwo(II->getArgOperand(0), OrZero,Depth, Q);
2564break;
2565default:
2566break;
2567 }
2568 }
2569returnfalse;
2570 }
2571default:
2572returnfalse;
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.
2582staticboolisGEPKnownNonNull(constGEPOperator *GEP,unsignedDepth,
2583constSimplifyQuery &Q) {
2584constFunction *F =nullptr;
2585if (constInstruction *I = dyn_cast<Instruction>(GEP))
2586F =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.
2590if (!GEP->hasNoUnsignedWrap() &&
2591 !(GEP->isInBounds() &&
2592 !NullPointerIsDefined(F,GEP->getPointerAddressSpace())))
2593returnfalse;
2594
2595// FIXME: Support vector-GEPs.
2596assert(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.
2600if (isKnownNonZero(GEP->getPointerOperand(), Q,Depth))
2601returntrue;
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.
2606for (gep_type_iterator GTI =gep_type_begin(GEP), GTE =gep_type_end(GEP);
2607 GTI != GTE; ++GTI) {
2608// Struct types are easy -- they must always be indexed by a constant.
2609if (StructType *STy = GTI.getStructTypeOrNull()) {
2610ConstantInt *OpC = cast<ConstantInt>(GTI.getOperand());
2611unsigned ElementIdx = OpC->getZExtValue();
2612constStructLayout *SL = Q.DL.getStructLayout(STy);
2613uint64_t ElementOffset = SL->getElementOffset(ElementIdx);
2614if (ElementOffset > 0)
2615returntrue;
2616continue;
2617 }
2618
2619// If we have a zero-sized type, the index doesn't matter. Keep looping.
2620if (GTI.getSequentialElementStride(Q.DL).isZero())
2621continue;
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.
2625if (ConstantInt *OpC = dyn_cast<ConstantInt>(GTI.getOperand())) {
2626if (!OpC->isZero())
2627returntrue;
2628continue;
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.
2636if (Depth++ >=MaxAnalysisRecursionDepth)
2637continue;
2638
2639if (isKnownNonZero(GTI.getOperand(), Q,Depth))
2640returntrue;
2641 }
2642
2643returnfalse;
2644}
2645
2646staticboolisKnownNonNullFromDominatingCondition(constValue *V,
2647constInstruction *CtxI,
2648constDominatorTree *DT) {
2649assert(!isa<Constant>(V) &&"Called for constant?");
2650
2651if (!CtxI || !DT)
2652returnfalse;
2653
2654unsigned NumUsesExplored = 0;
2655for (constauto *U : V->users()) {
2656// Avoid massive lists
2657if (NumUsesExplored >=DomConditionsMaxUses)
2658break;
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.
2663if (constauto *CB = dyn_cast<CallBase>(U))
2664if (auto *CalledFunc = CB->getCalledFunction())
2665for (constArgument &Arg : CalledFunc->args())
2666if (CB->getArgOperand(Arg.getArgNo()) == V &&
2667 Arg.hasNonNullAttr(/* AllowUndefOrPoison */false) &&
2668 DT->dominates(CB, CtxI))
2669returntrue;
2670
2671// If the value is used as a load/store, then the pointer must be non null.
2672if (V ==getLoadStorePointerOperand(U)) {
2673constInstruction *I = cast<Instruction>(U);
2674if (!NullPointerIsDefined(I->getFunction(),
2675 V->getType()->getPointerAddressSpace()) &&
2676 DT->dominates(I, CtxI))
2677returntrue;
2678 }
2679
2680if ((match(U,m_IDiv(m_Value(),m_Specific(V))) ||
2681match(U,m_IRem(m_Value(),m_Specific(V)))) &&
2682isValidAssumeForContext(cast<Instruction>(U), CtxI, DT))
2683returntrue;
2684
2685// Consider only compare instructions uniquely controlling a branch
2686Value *RHS;
2687CmpPredicate Pred;
2688if (!match(U,m_c_ICmp(Pred,m_Specific(V),m_Value(RHS))))
2689continue;
2690
2691bool NonNullIfTrue;
2692if (cmpExcludesZero(Pred,RHS))
2693 NonNullIfTrue =true;
2694elseif (cmpExcludesZero(CmpInst::getInversePredicate(Pred),RHS))
2695 NonNullIfTrue =false;
2696else
2697continue;
2698
2699SmallVector<const User *, 4> WorkList;
2700SmallPtrSet<const User *, 4> Visited;
2701for (constauto *CmpU : U->users()) {
2702assert(WorkList.empty() &&"Should be!");
2703if (Visited.insert(CmpU).second)
2704 WorkList.push_back(CmpU);
2705
2706while (!WorkList.empty()) {
2707auto *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?
2713if (NonNullIfTrue)
2714if (match(Curr,m_LogicalAnd(m_Value(),m_Value()))) {
2715for (constauto *CurrU : Curr->users())
2716if (Visited.insert(CurrU).second)
2717 WorkList.push_back(CurrU);
2718continue;
2719 }
2720
2721if (constBranchInst *BI = dyn_cast<BranchInst>(Curr)) {
2722assert(BI->isConditional() &&"uses a comparison!");
2723
2724BasicBlock *NonNullSuccessor =
2725 BI->getSuccessor(NonNullIfTrue ? 0 : 1);
2726BasicBlockEdge Edge(BI->getParent(), NonNullSuccessor);
2727if (Edge.isSingleEdge() && DT->dominates(Edge, CtxI->getParent()))
2728returntrue;
2729 }elseif (NonNullIfTrue &&isGuard(Curr) &&
2730 DT->dominates(cast<Instruction>(Curr), CtxI)) {
2731returntrue;
2732 }
2733 }
2734 }
2735 }
2736
2737returnfalse;
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.
2743staticboolrangeMetadataExcludesValue(constMDNode* Ranges,constAPInt&Value) {
2744constunsigned NumRanges = Ranges->getNumOperands() / 2;
2745assert(NumRanges >= 1);
2746for (unsigned i = 0; i < NumRanges; ++i) {
2747ConstantInt *Lower =
2748 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 0));
2749ConstantInt *Upper =
2750 mdconst::extract<ConstantInt>(Ranges->getOperand(2 * i + 1));
2751ConstantRangeRange(Lower->getValue(),Upper->getValue());
2752if (Range.contains(Value))
2753returnfalse;
2754 }
2755returntrue;
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.
2760staticboolisNonZeroRecurrence(constPHINode *PN) {
2761BinaryOperator *BO =nullptr;
2762Value *Start =nullptr, *Step =nullptr;
2763constAPInt *StartC, *StepC;
2764if (!matchSimpleRecurrence(PN, BO, Start, Step) ||
2765 !match(Start,m_APInt(StartC)) || StartC->isZero())
2766returnfalse;
2767
2768switch (BO->getOpcode()) {
2769case Instruction::Add:
2770// Starting from non-zero and stepping away from zero can never wrap back
2771// to zero.
2772return BO->hasNoUnsignedWrap() ||
2773 (BO->hasNoSignedWrap() &&match(Step,m_APInt(StepC)) &&
2774 StartC->isNegative() == StepC->isNegative());
2775case Instruction::Mul:
2776return (BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap()) &&
2777match(Step,m_APInt(StepC)) && !StepC->isZero();
2778case Instruction::Shl:
2779return BO->hasNoUnsignedWrap() || BO->hasNoSignedWrap();
2780case Instruction::AShr:
2781case Instruction::LShr:
2782return BO->isExact();
2783default:
2784returnfalse;
2785 }
2786}
2787
2788staticboolmatchOpWithOpEqZero(Value *Op0,Value *Op1) {
2789returnmatch(Op0,m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,
2790m_Specific(Op1),m_Zero()))) ||
2791match(Op1,m_ZExtOrSExt(m_SpecificICmp(ICmpInst::ICMP_EQ,
2792m_Specific(Op0),m_Zero())));
2793}
2794
2795staticboolisNonZeroAdd(constAPInt &DemandedElts,unsignedDepth,
2796constSimplifyQuery &Q,unsignedBitWidth,Value *X,
2797Value *Y,bool NSW,bool NUW) {
2798// (X + (X != 0)) is non zero
2799if (matchOpWithOpEqZero(X,Y))
2800returntrue;
2801
2802if (NUW)
2803returnisKnownNonZero(Y, DemandedElts, Q,Depth) ||
2804isKnownNonZero(X, DemandedElts, Q,Depth);
2805
2806KnownBits XKnown =computeKnownBits(X, DemandedElts,Depth, Q);
2807KnownBits 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.
2811if (XKnown.isNonNegative() && YKnown.isNonNegative())
2812if (isKnownNonZero(Y, DemandedElts, Q,Depth) ||
2813isKnownNonZero(X, DemandedElts, Q,Depth))
2814returntrue;
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.
2818if (XKnown.isNegative() && YKnown.isNegative()) {
2819APInt Mask =APInt::getSignedMaxValue(BitWidth);
2820// The sign bit of X is set. If some other bit is set then X is not equal
2821// to INT_MIN.
2822if (XKnown.One.intersects(Mask))
2823returntrue;
2824// The sign bit of Y is set. If some other bit is set then Y is not equal
2825// to INT_MIN.
2826if (YKnown.One.intersects(Mask))
2827returntrue;
2828 }
2829
2830// The sum of a non-negative number and a power of two is not zero.
2831if (XKnown.isNonNegative() &&
2832isKnownToBeAPowerOfTwo(Y,/*OrZero*/false,Depth, Q))
2833returntrue;
2834if (YKnown.isNonNegative() &&
2835isKnownToBeAPowerOfTwo(X,/*OrZero*/false,Depth, Q))
2836returntrue;
2837
2838returnKnownBits::add(XKnown, YKnown, NSW, NUW).isNonZero();
2839}
2840
2841staticboolisNonZeroSub(constAPInt &DemandedElts,unsignedDepth,
2842constSimplifyQuery &Q,unsignedBitWidth,Value *X,
2843Value *Y) {
2844// (X - (X != 0)) is non zero
2845// ((X != 0) - X) is non zero
2846if (matchOpWithOpEqZero(X,Y))
2847returntrue;
2848
2849// TODO: Move this case into isKnownNonEqual().
2850if (auto *C = dyn_cast<Constant>(X))
2851if (C->isNullValue() &&isKnownNonZero(Y, DemandedElts, Q,Depth))
2852returntrue;
2853
2854 return ::isKnownNonEqual(X,Y, DemandedElts,Depth, Q);
2855}
2856
2857staticboolisNonZeroMul(constAPInt &DemandedElts,unsignedDepth,
2858constSimplifyQuery &Q,unsignedBitWidth,Value *X,
2859Value *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.
2862if (NSW || NUW)
2863returnisKnownNonZero(X, DemandedElts, Q,Depth) &&
2864isKnownNonZero(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.
2868KnownBits XKnown =computeKnownBits(X, DemandedElts,Depth, Q);
2869if (XKnown.One[0])
2870returnisKnownNonZero(Y, DemandedElts, Q,Depth);
2871
2872KnownBits YKnown =computeKnownBits(Y, DemandedElts,Depth, Q);
2873if (YKnown.One[0])
2874return 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.
2881return (XKnown.countMaxTrailingZeros() + YKnown.countMaxTrailingZeros()) <
2882BitWidth;
2883}
2884
2885staticboolisNonZeroShift(constOperator *I,constAPInt &DemandedElts,
2886unsignedDepth,constSimplifyQuery &Q,
2887constKnownBits &KnownVal) {
2888auto ShiftOp = [&](constAPInt &Lhs,constAPInt &Rhs) {
2889switch (I->getOpcode()) {
2890case Instruction::Shl:
2891return Lhs.shl(Rhs);
2892case Instruction::LShr:
2893return Lhs.lshr(Rhs);
2894case Instruction::AShr:
2895return Lhs.ashr(Rhs);
2896default:
2897llvm_unreachable("Unknown Shift Opcode");
2898 }
2899 };
2900
2901auto InvShiftOp = [&](constAPInt &Lhs,constAPInt &Rhs) {
2902switch (I->getOpcode()) {
2903case Instruction::Shl:
2904return Lhs.lshr(Rhs);
2905case Instruction::LShr:
2906case Instruction::AShr:
2907return Lhs.shl(Rhs);
2908default:
2909llvm_unreachable("Unknown Shift Opcode");
2910 }
2911 };
2912
2913if (KnownVal.isUnknown())
2914returnfalse;
2915
2916KnownBits KnownCnt =
2917computeKnownBits(I->getOperand(1), DemandedElts,Depth, Q);
2918APInt MaxShift = KnownCnt.getMaxValue();
2919unsigned NumBits = KnownVal.getBitWidth();
2920if (MaxShift.uge(NumBits))
2921returnfalse;
2922
2923if (!ShiftOp(KnownVal.One, MaxShift).isZero())
2924returntrue;
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.
2928if (InvShiftOp(KnownVal.Zero, NumBits - MaxShift)
2929 .eq(InvShiftOp(APInt::getAllOnes(NumBits), NumBits - MaxShift)) &&
2930isKnownNonZero(I->getOperand(0), DemandedElts, Q,Depth))
2931returntrue;
2932
2933returnfalse;
2934}
2935
2936staticboolisKnownNonZeroFromOperator(constOperator *I,
2937constAPInt &DemandedElts,
2938unsignedDepth,constSimplifyQuery &Q) {
2939unsignedBitWidth =getBitWidth(I->getType()->getScalarType(), Q.DL);
2940switch (I->getOpcode()) {
2941case Instruction::Alloca:
2942// Alloca never returns null, malloc might.
2943returnI->getType()->getPointerAddressSpace() == 0;
2944case Instruction::GetElementPtr:
2945if (I->getType()->isPointerTy())
2946returnisGEPKnownNonNull(cast<GEPOperator>(I),Depth, Q);
2947break;
2948case 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.
2976Type *FromTy =I->getOperand(0)->getType();
2977if ((FromTy->isIntOrIntVectorTy() || FromTy->isPtrOrPtrVectorTy()) &&
2978 (BitWidth %getBitWidth(FromTy->getScalarType(), Q.DL)) == 0)
2979returnisKnownNonZero(I->getOperand(0), Q,Depth);
2980 }break;
2981case 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.
2985if (!isa<ScalableVectorType>(I->getType()) &&
2986 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2987 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2988returnisKnownNonZero(I->getOperand(0), DemandedElts, Q,Depth);
2989break;
2990case 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.
2993if (!isa<ScalableVectorType>(I->getType()) &&
2994 Q.DL.getTypeSizeInBits(I->getOperand(0)->getType()).getFixedValue() <=
2995 Q.DL.getTypeSizeInBits(I->getType()).getFixedValue())
2996returnisKnownNonZero(I->getOperand(0), DemandedElts, Q,Depth);
2997break;
2998case Instruction::Trunc:
2999// nuw/nsw trunc preserves zero/non-zero status of input.
3000if (auto *TI = dyn_cast<TruncInst>(I))
3001if (TI->hasNoSignedWrap() || TI->hasNoUnsignedWrap())
3002returnisKnownNonZero(TI->getOperand(0), DemandedElts, Q,Depth);
3003break;
3004
3005case Instruction::Sub:
3006returnisNonZeroSub(DemandedElts,Depth, Q,BitWidth,I->getOperand(0),
3007I->getOperand(1));
3008case Instruction::Xor:
3009// (X ^ (X != 0)) is non zero
3010if (matchOpWithOpEqZero(I->getOperand(0),I->getOperand(1)))
3011returntrue;
3012break;
3013case Instruction::Or:
3014// (X | (X != 0)) is non zero
3015if (matchOpWithOpEqZero(I->getOperand(0),I->getOperand(1)))
3016returntrue;
3017// X | Y != 0 if X != 0 or Y != 0.
3018returnisKnownNonZero(I->getOperand(1), DemandedElts, Q,Depth) ||
3019isKnownNonZero(I->getOperand(0), DemandedElts, Q,Depth);
3020case Instruction::SExt:
3021case Instruction::ZExt:
3022// ext X != 0 if X != 0.
3023returnisKnownNonZero(I->getOperand(0), DemandedElts, Q,Depth);
3024
3025case Instruction::Shl: {
3026// shl nsw/nuw can't remove any non-zero bits.
3027constOverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
3028if (Q.IIQ.hasNoUnsignedWrap(BO) || Q.IIQ.hasNoSignedWrap(BO))
3029returnisKnownNonZero(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.
3033KnownBits Known(BitWidth);
3034computeKnownBits(I->getOperand(0), DemandedElts, Known,Depth, Q);
3035if (Known.One[0])
3036returntrue;
3037
3038returnisNonZeroShift(I, DemandedElts,Depth, Q, Known);
3039 }
3040case Instruction::LShr:
3041case Instruction::AShr: {
3042// shr exact can only shift out zero bits.
3043constPossiblyExactOperator *BO = cast<PossiblyExactOperator>(I);
3044if (BO->isExact())
3045returnisKnownNonZero(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.
3049KnownBits Known =
3050computeKnownBits(I->getOperand(0), DemandedElts,Depth, Q);
3051if (Known.isNegative())
3052returntrue;
3053
3054returnisNonZeroShift(I, DemandedElts,Depth, Q, Known);
3055 }
3056case Instruction::UDiv:
3057case Instruction::SDiv: {
3058// X / Y
3059// div exact can only produce a zero if the dividend is zero.
3060if (cast<PossiblyExactOperator>(I)->isExact())
3061returnisKnownNonZero(I->getOperand(0), DemandedElts, Q,Depth);
3062
3063KnownBits XKnown =
3064computeKnownBits(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.
3067if (XKnown.isUnknown())
3068returnfalse;
3069
3070KnownBits YKnown =
3071computeKnownBits(I->getOperand(1), DemandedElts,Depth, Q);
3072if (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.
3081return XUgeY && *XUgeY;
3082 }
3083case 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.
3088auto *BO = cast<OverflowingBinaryOperator>(I);
3089returnisNonZeroAdd(DemandedElts,Depth, Q,BitWidth,I->getOperand(0),
3090I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3091 Q.IIQ.hasNoUnsignedWrap(BO));
3092 }
3093case Instruction::Mul: {
3094constOverflowingBinaryOperator *BO = cast<OverflowingBinaryOperator>(I);
3095returnisNonZeroMul(DemandedElts,Depth, Q,BitWidth,I->getOperand(0),
3096I->getOperand(1), Q.IIQ.hasNoSignedWrap(BO),
3097 Q.IIQ.hasNoUnsignedWrap(BO));
3098 }
3099case 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`.
3106auto SelectArmIsNonZero = [&](bool IsTrueArm) {
3107Value *Op;
3108Op = IsTrueArm ?I->getOperand(1) :I->getOperand(2);
3109// Op is trivially non-zero.
3110if (isKnownNonZero(Op, DemandedElts, Q,Depth))
3111returntrue;
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.
3115Value *X;
3116CmpPredicate Pred;
3117if (!match(I->getOperand(0),m_c_ICmp(Pred,m_Specific(Op),m_Value(X))))
3118returnfalse;
3119
3120if (!IsTrueArm)
3121 Pred = ICmpInst::getInversePredicate(Pred);
3122
3123returncmpExcludesZero(Pred,X);
3124 };
3125
3126if (SelectArmIsNonZero(/* IsTrueArm */true) &&
3127 SelectArmIsNonZero(/* IsTrueArm */false))
3128returntrue;
3129break;
3130 }
3131case Instruction::PHI: {
3132auto *PN = cast<PHINode>(I);
3133if (Q.IIQ.UseInstrInfo &&isNonZeroRecurrence(PN))
3134returntrue;
3135
3136// Check if all incoming values are non-zero using recursion.
3137SimplifyQuery RecQ = Q.getWithoutCondContext();
3138unsigned NewDepth = std::max(Depth,MaxAnalysisRecursionDepth - 1);
3139returnllvm::all_of(PN->operands(), [&](constUse &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.
3160returnisKnownNonZero(U.get(), DemandedElts, RecQ, NewDepth);
3161 });
3162 }
3163case Instruction::InsertElement: {
3164if (isa<ScalableVectorType>(I->getType()))
3165break;
3166
3167constValue *Vec =I->getOperand(0);
3168constValue *Elt =I->getOperand(1);
3169auto *CIdx = dyn_cast<ConstantInt>(I->getOperand(2));
3170
3171unsigned NumElts = DemandedElts.getBitWidth();
3172APInt DemandedVecElts = DemandedElts;
3173bool SkipElt =false;
3174// If we know the index we are inserting too, clear it from Vec check.
3175if (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.
3182return (SkipElt ||isKnownNonZero(Elt, Q,Depth)) &&
3183 (DemandedVecElts.isZero() ||
3184isKnownNonZero(Vec, DemandedVecElts, Q,Depth));
3185 }
3186case Instruction::ExtractElement:
3187if (constauto *EEI = dyn_cast<ExtractElementInst>(I)) {
3188constValue *Vec = EEI->getVectorOperand();
3189constValue *Idx = EEI->getIndexOperand();
3190auto *CIdx = dyn_cast<ConstantInt>(Idx);
3191if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
3192unsigned NumElts = VecTy->getNumElements();
3193APInt DemandedVecElts =APInt::getAllOnes(NumElts);
3194if (CIdx && CIdx->getValue().ult(NumElts))
3195 DemandedVecElts =APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
3196returnisKnownNonZero(Vec, DemandedVecElts, Q,Depth);
3197 }
3198 }
3199break;
3200case Instruction::ShuffleVector: {
3201auto *Shuf = dyn_cast<ShuffleVectorInst>(I);
3202if (!Shuf)
3203break;
3204APInt DemandedLHS, DemandedRHS;
3205// For undef elements, we don't know anything about the common state of
3206// the shuffle result.
3207if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
3208break;
3209// If demanded elements for both vecs are non-zero, the shuffle is non-zero.
3210return (DemandedRHS.isZero() ||
3211isKnownNonZero(Shuf->getOperand(1), DemandedRHS, Q,Depth)) &&
3212 (DemandedLHS.isZero() ||
3213isKnownNonZero(Shuf->getOperand(0), DemandedLHS, Q,Depth));
3214 }
3215case Instruction::Freeze:
3216returnisKnownNonZero(I->getOperand(0), Q,Depth) &&
3217isGuaranteedNotToBePoison(I->getOperand(0), Q.AC, Q.CxtI, Q.DT,
3218Depth);
3219case Instruction::Load: {
3220auto *LI = cast<LoadInst>(I);
3221// A Load tagged with nonnull or dereferenceable with null pointer undefined
3222// is never null.
3223if (auto *PtrT = dyn_cast<PointerType>(I->getType())) {
3224if (Q.IIQ.getMetadata(LI, LLVMContext::MD_nonnull) ||
3225 (Q.IIQ.getMetadata(LI, LLVMContext::MD_dereferenceable) &&
3226 !NullPointerIsDefined(LI->getFunction(), PtrT->getAddressSpace())))
3227returntrue;
3228 }elseif (MDNode *Ranges = Q.IIQ.getMetadata(LI, LLVMContext::MD_range)) {
3229returnrangeMetadataExcludesValue(Ranges,APInt::getZero(BitWidth));
3230 }
3231
3232// No need to fall through to computeKnownBits as range metadata is already
3233// handled in isKnownNonZero.
3234returnfalse;
3235 }
3236case Instruction::ExtractValue: {
3237constWithOverflowInst *WO;
3238if (match(I, m_ExtractValue<0>(m_WithOverflowInst(WO)))) {
3239switch (WO->getBinaryOp()) {
3240default:
3241break;
3242case Instruction::Add:
3243returnisNonZeroAdd(DemandedElts,Depth, Q,BitWidth,
3244 WO->getArgOperand(0), WO->getArgOperand(1),
3245/*NSW=*/false,
3246/*NUW=*/false);
3247case Instruction::Sub:
3248returnisNonZeroSub(DemandedElts,Depth, Q,BitWidth,
3249 WO->getArgOperand(0), WO->getArgOperand(1));
3250case Instruction::Mul:
3251returnisNonZeroMul(DemandedElts,Depth, Q,BitWidth,
3252 WO->getArgOperand(0), WO->getArgOperand(1),
3253/*NSW=*/false,/*NUW=*/false);
3254break;
3255 }
3256 }
3257break;
3258 }
3259case Instruction::Call:
3260case Instruction::Invoke: {
3261constauto *Call = cast<CallBase>(I);
3262if (I->getType()->isPointerTy()) {
3263if (Call->isReturnNonNull())
3264returntrue;
3265if (constauto *RP =getArgumentAliasingToReturnedPointer(Call,true))
3266returnisKnownNonZero(RP, Q,Depth);
3267 }else {
3268if (MDNode *Ranges = Q.IIQ.getMetadata(Call, LLVMContext::MD_range))
3269returnrangeMetadataExcludesValue(Ranges,APInt::getZero(BitWidth));
3270if (std::optional<ConstantRange>Range = Call->getRange()) {
3271constAPInt ZeroValue(Range->getBitWidth(), 0);
3272if (!Range->contains(ZeroValue))
3273returntrue;
3274 }
3275if (constValue *RV = Call->getReturnedArgOperand())
3276if (RV->getType() ==I->getType() &&isKnownNonZero(RV, Q,Depth))
3277returntrue;
3278 }
3279
3280if (auto *II = dyn_cast<IntrinsicInst>(I)) {
3281switch (II->getIntrinsicID()) {
3282case Intrinsic::sshl_sat:
3283case Intrinsic::ushl_sat:
3284case Intrinsic::abs:
3285case Intrinsic::bitreverse:
3286case Intrinsic::bswap:
3287case Intrinsic::ctpop:
3288returnisKnownNonZero(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.
3291case Intrinsic::ssub_sat:
3292returnisNonZeroSub(DemandedElts,Depth, Q,BitWidth,
3293II->getArgOperand(0),II->getArgOperand(1));
3294case Intrinsic::sadd_sat:
3295returnisNonZeroAdd(DemandedElts,Depth, Q,BitWidth,
3296II->getArgOperand(0),II->getArgOperand(1),
3297/*NSW=*/true,/* NUW=*/false);
3298// Vec reverse preserves zero/non-zero status from input vec.
3299case Intrinsic::vector_reverse:
3300returnisKnownNonZero(II->getArgOperand(0), DemandedElts.reverseBits(),
3301 Q,Depth);
3302// umin/smin/smax/smin/or of all non-zero elements is always non-zero.
3303case Intrinsic::vector_reduce_or:
3304case Intrinsic::vector_reduce_umax:
3305case Intrinsic::vector_reduce_umin:
3306case Intrinsic::vector_reduce_smax:
3307case Intrinsic::vector_reduce_smin:
3308returnisKnownNonZero(II->getArgOperand(0), Q,Depth);
3309case Intrinsic::umax:
3310case Intrinsic::uadd_sat:
3311// umax(X, (X != 0)) is non zero
3312// X +usat (X != 0) is non zero
3313if (matchOpWithOpEqZero(II->getArgOperand(0),II->getArgOperand(1)))
3314returntrue;
3315
3316returnisKnownNonZero(II->getArgOperand(1), DemandedElts, Q,Depth) ||
3317isKnownNonZero(II->getArgOperand(0), DemandedElts, Q,Depth);
3318case 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.
3321auto IsNonZero = [&](Value *Op, std::optional<bool> &OpNonZero,
3322constKnownBits &OpKnown) {
3323if (!OpNonZero.has_value())
3324 OpNonZero = OpKnown.isNonZero() ||
3325isKnownNonZero(Op, DemandedElts, Q,Depth);
3326return *OpNonZero;
3327 };
3328// Avoid re-computing isKnownNonZero.
3329 std::optional<bool> Op0NonZero, Op1NonZero;
3330KnownBits Op1Known =
3331computeKnownBits(II->getArgOperand(1), DemandedElts,Depth, Q);
3332if (Op1Known.isNonNegative() &&
3333 IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known))
3334returntrue;
3335KnownBits Op0Known =
3336computeKnownBits(II->getArgOperand(0), DemandedElts,Depth, Q);
3337if (Op0Known.isNonNegative() &&
3338 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known))
3339returntrue;
3340return IsNonZero(II->getArgOperand(1), Op1NonZero, Op1Known) &&
3341 IsNonZero(II->getArgOperand(0), Op0NonZero, Op0Known);
3342 }
3343case 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.
3346KnownBits Op1Known =
3347computeKnownBits(II->getArgOperand(1), DemandedElts,Depth, Q);
3348if (Op1Known.isNegative())
3349returntrue;
3350KnownBits Op0Known =
3351computeKnownBits(II->getArgOperand(0), DemandedElts,Depth, Q);
3352if (Op0Known.isNegative())
3353returntrue;
3354
3355if (Op1Known.isNonZero() && Op0Known.isNonZero())
3356returntrue;
3357 }
3358 [[fallthrough]];
3359case Intrinsic::umin:
3360returnisKnownNonZero(II->getArgOperand(0), DemandedElts, Q,Depth) &&
3361isKnownNonZero(II->getArgOperand(1), DemandedElts, Q,Depth);
3362case Intrinsic::cttz:
3363returncomputeKnownBits(II->getArgOperand(0), DemandedElts,Depth, Q)
3364 .Zero[0];
3365case Intrinsic::ctlz:
3366returncomputeKnownBits(II->getArgOperand(0), DemandedElts,Depth, Q)
3367 .isNonNegative();
3368case Intrinsic::fshr:
3369case Intrinsic::fshl:
3370// If Op0 == Op1, this is a rotate. rotate(x, y) != 0 iff x != 0.
3371if (II->getArgOperand(0) ==II->getArgOperand(1))
3372returnisKnownNonZero(II->getArgOperand(0), DemandedElts, Q,Depth);
3373break;
3374case Intrinsic::vscale:
3375returntrue;
3376case Intrinsic::experimental_get_vector_length:
3377returnisKnownNonZero(I->getOperand(0), Q,Depth);
3378default:
3379break;
3380 }
3381break;
3382 }
3383
3384returnfalse;
3385 }
3386 }
3387
3388KnownBits Known(BitWidth);
3389computeKnownBits(I, DemandedElts, Known,Depth, Q);
3390return 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.
3399boolisKnownNonZero(constValue *V,constAPInt &DemandedElts,
3400constSimplifyQuery &Q,unsignedDepth) {
3401Type *Ty = V->getType();
3402
3403#ifndef NDEBUG
3404assert(Depth <=MaxAnalysisRecursionDepth &&"Limit Search Depth");
3405
3406if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3407assert(
3408 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3409"DemandedElt width should equal the fixed vector number of elements");
3410 }else {
3411assert(DemandedElts ==APInt(1, 1) &&
3412"DemandedElt width should be 1 for scalars");
3413 }
3414#endif
3415
3416if (auto *C = dyn_cast<Constant>(V)) {
3417if (C->isNullValue())
3418returnfalse;
3419if (isa<ConstantInt>(C))
3420// Must be non-zero due to null test above.
3421returntrue;
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.
3425if (auto *VecTy = dyn_cast<FixedVectorType>(Ty)) {
3426for (unsigned i = 0, e = VecTy->getNumElements(); i != e; ++i) {
3427if (!DemandedElts[i])
3428continue;
3429Constant *Elt =C->getAggregateElement(i);
3430if (!Elt || Elt->isNullValue())
3431returnfalse;
3432if (!isa<PoisonValue>(Elt) && !isa<ConstantInt>(Elt))
3433returnfalse;
3434 }
3435returntrue;
3436 }
3437
3438// Constant ptrauth can be null, iff the base pointer can be.
3439if (auto *CPA = dyn_cast<ConstantPtrAuth>(V))
3440returnisKnownNonZero(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.
3445if (constGlobalValue *GV = dyn_cast<GlobalValue>(V)) {
3446if (!GV->isAbsoluteSymbolRef() && !GV->hasExternalWeakLinkage() &&
3447 GV->getType()->getAddressSpace() == 0)
3448returntrue;
3449 }
3450
3451// For constant expressions, fall through to the Operator code below.
3452if (!isa<ConstantExpr>(V))
3453returnfalse;
3454 }
3455
3456if (constauto *A = dyn_cast<Argument>(V))
3457if (std::optional<ConstantRange>Range =A->getRange()) {
3458constAPInt ZeroValue(Range->getBitWidth(), 0);
3459if (!Range->contains(ZeroValue))
3460returntrue;
3461 }
3462
3463if (!isa<Constant>(V) &&isKnownNonZeroFromAssume(V, Q))
3464returntrue;
3465
3466// Some of the tests below are recursive, so bail out if we hit the limit.
3467if (Depth++ >=MaxAnalysisRecursionDepth)
3468returnfalse;
3469
3470// Check for pointer simplifications.
3471
3472if (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.
3475if (constArgument *A = dyn_cast<Argument>(V)) {
3476if (((A->hasPassPointeeByValueCopyAttr() &&
3477 !NullPointerIsDefined(A->getParent(), PtrTy->getAddressSpace())) ||
3478A->hasNonNullAttr()))
3479returntrue;
3480 }
3481 }
3482
3483if (constauto *I = dyn_cast<Operator>(V))
3484if (isKnownNonZeroFromOperator(I, DemandedElts,Depth, Q))
3485returntrue;
3486
3487if (!isa<Constant>(V) &&
3488isKnownNonNullFromDominatingCondition(V, Q.CxtI, Q.DT))
3489returntrue;
3490
3491returnfalse;
3492}
3493
3494boolllvm::isKnownNonZero(constValue *V,constSimplifyQuery &Q,
3495unsignedDepth) {
3496auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
3497APInt 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*>>
3509getInvertibleOperands(constOperator *Op1,
3510constOperator *Op2) {
3511if (Op1->getOpcode() != Op2->getOpcode())
3512return std::nullopt;
3513
3514autogetOperands = [&](unsigned OpNum) ->auto {
3515return std::make_pair(Op1->getOperand(OpNum), Op2->getOperand(OpNum));
3516 };
3517
3518switch (Op1->getOpcode()) {
3519default:
3520break;
3521case Instruction::Or:
3522if (!cast<PossiblyDisjointInst>(Op1)->isDisjoint() ||
3523 !cast<PossiblyDisjointInst>(Op2)->isDisjoint())
3524break;
3525 [[fallthrough]];
3526case Instruction::Xor:
3527case Instruction::Add: {
3528Value *Other;
3529if (match(Op2,m_c_BinOp(m_Specific(Op1->getOperand(0)),m_Value(Other))))
3530return std::make_pair(Op1->getOperand(1),Other);
3531if (match(Op2,m_c_BinOp(m_Specific(Op1->getOperand(1)),m_Value(Other))))
3532return std::make_pair(Op1->getOperand(0),Other);
3533break;
3534 }
3535case Instruction::Sub:
3536if (Op1->getOperand(0) == Op2->getOperand(0))
3537returngetOperands(1);
3538if (Op1->getOperand(1) == Op2->getOperand(1))
3539returngetOperands(0);
3540break;
3541case 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
3545auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3546auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3547if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3548 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3549break;
3550
3551// Assume operand order has been canonicalized
3552if (Op1->getOperand(1) == Op2->getOperand(1) &&
3553 isa<ConstantInt>(Op1->getOperand(1)) &&
3554 !cast<ConstantInt>(Op1->getOperand(1))->isZero())
3555returngetOperands(0);
3556break;
3557 }
3558case 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.
3561auto *OBO1 = cast<OverflowingBinaryOperator>(Op1);
3562auto *OBO2 = cast<OverflowingBinaryOperator>(Op2);
3563if ((!OBO1->hasNoUnsignedWrap() || !OBO2->hasNoUnsignedWrap()) &&
3564 (!OBO1->hasNoSignedWrap() || !OBO2->hasNoSignedWrap()))
3565break;
3566
3567if (Op1->getOperand(1) == Op2->getOperand(1))
3568returngetOperands(0);
3569break;
3570 }
3571case Instruction::AShr:
3572case Instruction::LShr: {
3573auto *PEO1 = cast<PossiblyExactOperator>(Op1);
3574auto *PEO2 = cast<PossiblyExactOperator>(Op2);
3575if (!PEO1->isExact() || !PEO2->isExact())
3576break;
3577
3578if (Op1->getOperand(1) == Op2->getOperand(1))
3579returngetOperands(0);
3580break;
3581 }
3582case Instruction::SExt:
3583case Instruction::ZExt:
3584if (Op1->getOperand(0)->getType() == Op2->getOperand(0)->getType())
3585returngetOperands(0);
3586break;
3587case Instruction::PHI: {
3588constPHINode *PN1 = cast<PHINode>(Op1);
3589constPHINode *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
3594BinaryOperator *BO1 =nullptr;
3595Value *Start1 =nullptr, *Step1 =nullptr;
3596BinaryOperator *BO2 =nullptr;
3597Value *Start2 =nullptr, *Step2 =nullptr;
3598if (PN1->getParent() != PN2->getParent() ||
3599 !matchSimpleRecurrence(PN1, BO1, Start1, Step1) ||
3600 !matchSimpleRecurrence(PN2, BO2, Start2, Step2))
3601break;
3602
3603auto Values =getInvertibleOperands(cast<Operator>(BO1),
3604 cast<Operator>(BO2));
3605if (!Values)
3606break;
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?).
3613if (Values->first != PN1 || Values->second != PN2)
3614break;
3615
3616return std::make_pair(Start1, Start2);
3617 }
3618 }
3619return 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.
3625staticboolisModifyingBinopOfNonZero(constValue *V1,constValue *V2,
3626constAPInt &DemandedElts,unsignedDepth,
3627constSimplifyQuery &Q) {
3628constBinaryOperator *BO = dyn_cast<BinaryOperator>(V1);
3629if (!BO)
3630returnfalse;
3631switch (BO->getOpcode()) {
3632default:
3633break;
3634case Instruction::Or:
3635if (!cast<PossiblyDisjointInst>(V1)->isDisjoint())
3636break;
3637 [[fallthrough]];
3638case Instruction::Xor:
3639case Instruction::Add:
3640Value *Op =nullptr;
3641if (V2 == BO->getOperand(0))
3642Op = BO->getOperand(1);
3643elseif (V2 == BO->getOperand(1))
3644Op = BO->getOperand(0);
3645else
3646returnfalse;
3647returnisKnownNonZero(Op, DemandedElts, Q,Depth + 1);
3648 }
3649returnfalse;
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.
3654staticboolisNonEqualMul(constValue *V1,constValue *V2,
3655constAPInt &DemandedElts,unsignedDepth,
3656constSimplifyQuery &Q) {
3657if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3658constAPInt *C;
3659returnmatch(OBO,m_Mul(m_Specific(V1),m_APInt(C))) &&
3660 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3661 !C->isZero() && !C->isOne() &&
3662isKnownNonZero(V1, DemandedElts, Q,Depth + 1);
3663 }
3664returnfalse;
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.
3669staticboolisNonEqualShl(constValue *V1,constValue *V2,
3670constAPInt &DemandedElts,unsignedDepth,
3671constSimplifyQuery &Q) {
3672if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(V2)) {
3673constAPInt *C;
3674returnmatch(OBO,m_Shl(m_Specific(V1),m_APInt(C))) &&
3675 (OBO->hasNoUnsignedWrap() || OBO->hasNoSignedWrap()) &&
3676 !C->isZero() &&isKnownNonZero(V1, DemandedElts, Q,Depth + 1);
3677 }
3678returnfalse;
3679}
3680
3681staticboolisNonEqualPHIs(constPHINode *PN1,constPHINode *PN2,
3682constAPInt &DemandedElts,unsignedDepth,
3683constSimplifyQuery &Q) {
3684// Check two PHIs are in same block.
3685if (PN1->getParent() != PN2->getParent())
3686returnfalse;
3687
3688SmallPtrSet<const BasicBlock *, 8> VisitedBBs;
3689bool UsedFullRecursion =false;
3690for (constBasicBlock *IncomBB : PN1->blocks()) {
3691if (!VisitedBBs.insert(IncomBB).second)
3692continue;// Don't reprocess blocks that we have dealt with already.
3693constValue *IV1 = PN1->getIncomingValueForBlock(IncomBB);
3694constValue *IV2 = PN2->getIncomingValueForBlock(IncomBB);
3695constAPInt *C1, *C2;
3696if (match(IV1,m_APInt(C1)) &&match(IV2,m_APInt(C2)) && *C1 != *C2)
3697continue;
3698
3699// Only one pair of phi operands is allowed for full recursion.
3700if (UsedFullRecursion)
3701returnfalse;
3702
3703SimplifyQuery RecQ = Q.getWithoutCondContext();
3704 RecQ.CxtI = IncomBB->getTerminator();
3705if (!isKnownNonEqual(IV1, IV2, DemandedElts,Depth + 1, RecQ))
3706returnfalse;
3707 UsedFullRecursion =true;
3708 }
3709returntrue;
3710}
3711
3712staticboolisNonEqualSelect(constValue *V1,constValue *V2,
3713constAPInt &DemandedElts,unsignedDepth,
3714constSimplifyQuery &Q) {
3715constSelectInst *SI1 = dyn_cast<SelectInst>(V1);
3716if (!SI1)
3717returnfalse;
3718
3719if (constSelectInst *SI2 = dyn_cast<SelectInst>(V2)) {
3720constValue *Cond1 = SI1->getCondition();
3721constValue *Cond2 = SI2->getCondition();
3722if (Cond1 == Cond2)
3723returnisKnownNonEqual(SI1->getTrueValue(), SI2->getTrueValue(),
3724 DemandedElts,Depth + 1, Q) &&
3725isKnownNonEqual(SI1->getFalseValue(), SI2->getFalseValue(),
3726 DemandedElts,Depth + 1, Q);
3727 }
3728returnisKnownNonEqual(SI1->getTrueValue(), V2, DemandedElts,Depth + 1, Q) &&
3729isKnownNonEqual(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.
3737staticboolisNonEqualPointersWithRecursiveGEP(constValue *A,constValue *B,
3738constSimplifyQuery &Q) {
3739if (!A->getType()->isPointerTy() || !B->getType()->isPointerTy())
3740returnfalse;
3741
3742auto *GEPA = dyn_cast<GEPOperator>(A);
3743if (!GEPA || GEPA->getNumIndices() != 1 || !isa<Constant>(GEPA->idx_begin()))
3744returnfalse;
3745
3746// Handle 2 incoming PHI values with one being a recursive GEP.
3747auto *PN = dyn_cast<PHINode>(GEPA->getPointerOperand());
3748if (!PN || PN->getNumIncomingValues() != 2)
3749returnfalse;
3750
3751// Search for the recursive GEP as an incoming operand, and record that as
3752// Step.
3753Value *Start =nullptr;
3754Value *Step =const_cast<Value *>(A);
3755if (PN->getIncomingValue(0) == Step)
3756 Start = PN->getIncomingValue(1);
3757elseif (PN->getIncomingValue(1) == Step)
3758 Start = PN->getIncomingValue(0);
3759else
3760returnfalse;
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.
3768unsigned IndexWidth = Q.DL.getIndexTypeSizeInBits(Start->getType());
3769APInt StartOffset(IndexWidth, 0);
3770 Start = Start->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StartOffset);
3771APInt StepOffset(IndexWidth, 0);
3772 Step = Step->stripAndAccumulateInBoundsConstantOffsets(Q.DL, StepOffset);
3773
3774// Check if Base Pointer of Step matches the PHI.
3775if (Step != PN)
3776returnfalse;
3777APInt OffsetB(IndexWidth, 0);
3778B =B->stripAndAccumulateInBoundsConstantOffsets(Q.DL, OffsetB);
3779return 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.
3785staticboolisKnownNonEqual(constValue *V1,constValue *V2,
3786constAPInt &DemandedElts,unsignedDepth,
3787constSimplifyQuery &Q) {
3788if (V1 == V2)
3789returnfalse;
3790if (V1->getType() != V2->getType())
3791// We can't look through casts yet.
3792returnfalse;
3793
3794if (Depth >=MaxAnalysisRecursionDepth)
3795returnfalse;
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.
3800auto *O1 = dyn_cast<Operator>(V1);
3801auto *O2 = dyn_cast<Operator>(V2);
3802if (O1 && O2 && O1->getOpcode() == O2->getOpcode()) {
3803if (auto Values =getInvertibleOperands(O1, O2))
3804returnisKnownNonEqual(Values->first, Values->second, DemandedElts,
3805Depth + 1, Q);
3806
3807if (constPHINode *PN1 = dyn_cast<PHINode>(V1)) {
3808constPHINode *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.
3811if (isNonEqualPHIs(PN1, PN2, DemandedElts,Depth, Q))
3812returntrue;
3813 };
3814 }
3815
3816if (isModifyingBinopOfNonZero(V1, V2, DemandedElts,Depth, Q) ||
3817isModifyingBinopOfNonZero(V2, V1, DemandedElts,Depth, Q))
3818returntrue;
3819
3820if (isNonEqualMul(V1, V2, DemandedElts,Depth, Q) ||
3821isNonEqualMul(V2, V1, DemandedElts,Depth, Q))
3822returntrue;
3823
3824if (isNonEqualShl(V1, V2, DemandedElts,Depth, Q) ||
3825isNonEqualShl(V2, V1, DemandedElts,Depth, Q))
3826returntrue;
3827
3828if (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.
3831KnownBits Known1 =computeKnownBits(V1, DemandedElts,Depth, Q);
3832if (!Known1.isUnknown()) {
3833KnownBits Known2 =computeKnownBits(V2, DemandedElts,Depth, Q);
3834if (Known1.Zero.intersects(Known2.One) ||
3835 Known2.Zero.intersects(Known1.One))
3836returntrue;
3837 }
3838 }
3839
3840if (isNonEqualSelect(V1, V2, DemandedElts,Depth, Q) ||
3841isNonEqualSelect(V2, V1, DemandedElts,Depth, Q))
3842returntrue;
3843
3844if (isNonEqualPointersWithRecursiveGEP(V1, V2, Q) ||
3845isNonEqualPointersWithRecursiveGEP(V2, V1, Q))
3846returntrue;
3847
3848Value *A, *B;
3849// PtrToInts are NonEqual if their Ptrs are NonEqual.
3850// Check PtrToInt type matches the pointer size.
3851if (match(V1,m_PtrToIntSameSize(Q.DL,m_Value(A))) &&
3852match(V2,m_PtrToIntSameSize(Q.DL,m_Value(B))))
3853returnisKnownNonEqual(A,B, DemandedElts,Depth + 1, Q);
3854
3855returnfalse;
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.
3862staticunsignedcomputeNumSignBitsVectorConstant(constValue *V,
3863constAPInt &DemandedElts,
3864unsigned TyBits) {
3865constauto *CV = dyn_cast<Constant>(V);
3866if (!CV || !isa<FixedVectorType>(CV->getType()))
3867return 0;
3868
3869unsigned MinSignBits = TyBits;
3870unsigned NumElts = cast<FixedVectorType>(CV->getType())->getNumElements();
3871for (unsigned i = 0; i != NumElts; ++i) {
3872if (!DemandedElts[i])
3873continue;
3874// If we find a non-ConstantInt, bail out.
3875auto *Elt = dyn_cast_or_null<ConstantInt>(CV->getAggregateElement(i));
3876if (!Elt)
3877return 0;
3878
3879 MinSignBits = std::min(MinSignBits, Elt->getValue().getNumSignBits());
3880 }
3881
3882return MinSignBits;
3883}
3884
3885staticunsignedComputeNumSignBitsImpl(constValue *V,
3886constAPInt &DemandedElts,
3887unsignedDepth,constSimplifyQuery &Q);
3888
3889staticunsignedComputeNumSignBits(constValue *V,constAPInt &DemandedElts,
3890unsignedDepth,constSimplifyQuery &Q) {
3891unsigned Result =ComputeNumSignBitsImpl(V, DemandedElts,Depth, Q);
3892assert(Result > 0 &&"At least one sign bit needs to be present!");
3893return 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.
3903staticunsignedComputeNumSignBitsImpl(constValue *V,
3904constAPInt &DemandedElts,
3905unsignedDepth,constSimplifyQuery &Q) {
3906Type *Ty = V->getType();
3907#ifndef NDEBUG
3908assert(Depth <=MaxAnalysisRecursionDepth &&"Limit Search Depth");
3909
3910if (auto *FVTy = dyn_cast<FixedVectorType>(Ty)) {
3911assert(
3912 FVTy->getNumElements() == DemandedElts.getBitWidth() &&
3913"DemandedElt width should equal the fixed vector number of elements");
3914 }else {
3915assert(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
3924Type *ScalarTy = Ty->getScalarType();
3925unsigned TyBits = ScalarTy->isPointerTy() ?
3926 Q.DL.getPointerTypeSizeInBits(ScalarTy) :
3927 Q.DL.getTypeSizeInBits(ScalarTy);
3928
3929unsigned Tmp, Tmp2;
3930unsigned FirstAnswer = 1;
3931
3932// Note that ConstantInt is handled by the general computeKnownBits case
3933// below.
3934
3935if (Depth ==MaxAnalysisRecursionDepth)
3936return 1;
3937
3938if (auto *U = dyn_cast<Operator>(V)) {
3939switch (Operator::getOpcode(V)) {
3940default:break;
3941case Instruction::SExt:
3942 Tmp = TyBits - U->getOperand(0)->getType()->getScalarSizeInBits();
3943returnComputeNumSignBits(U->getOperand(0), DemandedElts,Depth + 1, Q) +
3944 Tmp;
3945
3946case Instruction::SDiv: {
3947constAPInt *Denominator;
3948// sdiv X, C -> adds log(C) sign bits.
3949if (match(U->getOperand(1),m_APInt(Denominator))) {
3950
3951// Ignore non-positive denominator.
3952if (!Denominator->isStrictlyPositive())
3953break;
3954
3955// Calculate the incoming numerator bits.
3956unsigned NumBits =
3957ComputeNumSignBits(U->getOperand(0), DemandedElts,Depth + 1, Q);
3958
3959// Add floor(log(C)) bits to the numerator bits.
3960return std::min(TyBits, NumBits + Denominator->logBase2());
3961 }
3962break;
3963 }
3964
3965case Instruction::SRem: {
3966 Tmp =ComputeNumSignBits(U->getOperand(0), DemandedElts,Depth + 1, Q);
3967
3968constAPInt *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.
3972if (match(U->getOperand(1),m_APInt(Denominator))) {
3973
3974// Ignore non-positive denominator.
3975if (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
3989unsigned ResBits = TyBits - Denominator->ceilLogBase2();
3990 Tmp = std::max(Tmp, ResBits);
3991 }
3992 }
3993return Tmp;
3994 }
3995
3996case Instruction::AShr: {
3997 Tmp =ComputeNumSignBits(U->getOperand(0), DemandedElts,Depth + 1, Q);
3998// ashr X, C -> adds C sign bits. Vectors too.
3999constAPInt *ShAmt;
4000if (match(U->getOperand(1),m_APInt(ShAmt))) {
4001if (ShAmt->uge(TyBits))
4002break;// Bad shift.
4003unsigned ShAmtLimited = ShAmt->getZExtValue();
4004 Tmp += ShAmtLimited;
4005if (Tmp > TyBits) Tmp = TyBits;
4006 }
4007return Tmp;
4008 }
4009case Instruction::Shl: {
4010constAPInt *ShAmt;
4011Value *X =nullptr;
4012if (match(U->getOperand(1),m_APInt(ShAmt))) {
4013// shl destroys sign bits.
4014if (ShAmt->uge(TyBits))
4015break;// 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.
4018if (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 =
4024ComputeNumSignBits(U->getOperand(0), DemandedElts,Depth + 1, Q);
4025if (ShAmt->uge(Tmp))
4026break;// Shifted all sign bits out.
4027 Tmp2 = ShAmt->getZExtValue();
4028return Tmp - Tmp2;
4029 }
4030break;
4031 }
4032case Instruction::And:
4033case Instruction::Or:
4034case 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);
4037if (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 }
4044break;
4045
4046case 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.
4049constValue *X;
4050constAPInt *CLow, *CHigh;
4051if (isSignedMinMaxClamp(U,X, CLow, CHigh))
4052return std::min(CLow->getNumSignBits(), CHigh->getNumSignBits());
4053
4054 Tmp =ComputeNumSignBits(U->getOperand(1), DemandedElts,Depth + 1, Q);
4055if (Tmp == 1)
4056break;
4057 Tmp2 =ComputeNumSignBits(U->getOperand(2), DemandedElts,Depth + 1, Q);
4058return std::min(Tmp, Tmp2);
4059 }
4060
4061case 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);
4065if (Tmp == 1)break;
4066
4067// Special case decrementing a value (ADD X, -1):
4068if (constauto *CRHS = dyn_cast<Constant>(U->getOperand(1)))
4069if (CRHS->isAllOnesValue()) {
4070KnownBits Known(TyBits);
4071computeKnownBits(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.
4075if ((Known.Zero | 1).isAllOnes())
4076return TyBits;
4077
4078// If we are subtracting one from a positive number, there is no carry
4079// out of the result.
4080if (Known.isNonNegative())
4081return Tmp;
4082 }
4083
4084 Tmp2 =ComputeNumSignBits(U->getOperand(1), DemandedElts,Depth + 1, Q);
4085if (Tmp2 == 1)
4086break;
4087return std::min(Tmp, Tmp2) - 1;
4088
4089case Instruction::Sub:
4090 Tmp2 =ComputeNumSignBits(U->getOperand(1), DemandedElts,Depth + 1, Q);
4091if (Tmp2 == 1)
4092break;
4093
4094// Handle NEG.
4095if (constauto *CLHS = dyn_cast<Constant>(U->getOperand(0)))
4096if (CLHS->isNullValue()) {
4097KnownBits Known(TyBits);
4098computeKnownBits(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.
4101if ((Known.Zero | 1).isAllOnes())
4102return 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.
4107if (Known.isNonNegative())
4108return 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);
4116if (Tmp == 1)
4117break;
4118return std::min(Tmp, Tmp2) - 1;
4119
4120case Instruction::Mul: {
4121// The output of the Mul can be at most twice the valid bits in the
4122// inputs.
4123unsigned SignBitsOp0 =
4124ComputeNumSignBits(U->getOperand(0), DemandedElts,Depth + 1, Q);
4125if (SignBitsOp0 == 1)
4126break;
4127unsigned SignBitsOp1 =
4128ComputeNumSignBits(U->getOperand(1), DemandedElts,Depth + 1, Q);
4129if (SignBitsOp1 == 1)
4130break;
4131unsigned OutValidBits =
4132 (TyBits - SignBitsOp0 + 1) + (TyBits - SignBitsOp1 + 1);
4133return OutValidBits > TyBits ? 1 : TyBits - OutValidBits + 1;
4134 }
4135
4136case Instruction::PHI: {
4137constPHINode *PN = cast<PHINode>(U);
4138unsigned NumIncomingValues = PN->getNumIncomingValues();
4139// Don't analyze large in-degree PHIs.
4140if (NumIncomingValues > 4)break;
4141// Unreachable blocks may have zero-operand PHI nodes.
4142if (NumIncomingValues == 0)break;
4143
4144// Take the minimum of all incoming values. This can't infinitely loop
4145// because of our depth threshold.
4146SimplifyQuery RecQ = Q.getWithoutCondContext();
4147 Tmp = TyBits;
4148for (unsigned i = 0, e = NumIncomingValues; i != e; ++i) {
4149if (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 }
4154return Tmp;
4155 }
4156
4157case 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);
4162unsigned OperandTyBits = U->getOperand(0)->getType()->getScalarSizeInBits();
4163if (Tmp > (OperandTyBits - TyBits))
4164return Tmp - (OperandTyBits - TyBits);
4165
4166return 1;
4167 }
4168
4169case 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).
4174returnComputeNumSignBits(U->getOperand(0),Depth + 1, Q);
4175
4176case Instruction::ShuffleVector: {
4177// Collect the minimum number of sign bits that are shared by every vector
4178// element referenced by the shuffle.
4179auto *Shuf = dyn_cast<ShuffleVectorInst>(U);
4180if (!Shuf) {
4181// FIXME: Add support for shufflevector constant expressions.
4182return 1;
4183 }
4184APInt DemandedLHS, DemandedRHS;
4185// For undef elements, we don't know anything about the common state of
4186// the shuffle result.
4187if (!getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
4188return 1;
4189 Tmp = std::numeric_limits<unsigned>::max();
4190if (!!DemandedLHS) {
4191constValue *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.
4196if (Tmp == 1)
4197break;
4198if (!!DemandedRHS) {
4199constValue *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.
4205if (Tmp == 1)
4206break;
4207assert(Tmp <= TyBits &&"Failed to determine minimum sign bits");
4208return Tmp;
4209 }
4210case Instruction::Call: {
4211if (constauto *II = dyn_cast<IntrinsicInst>(U)) {
4212switch (II->getIntrinsicID()) {
4213default:
4214break;
4215case Intrinsic::abs:
4216 Tmp =
4217ComputeNumSignBits(U->getOperand(0), DemandedElts,Depth + 1, Q);
4218if (Tmp == 1)
4219break;
4220
4221// Absolute value reduces number of sign bits by at most 1.
4222return Tmp - 1;
4223case Intrinsic::smin:
4224case Intrinsic::smax: {
4225constAPInt *CLow, *CHigh;
4226if (isSignedMinMaxIntrinsicClamp(II, CLow, CHigh))
4227return 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.
4240if (unsigned VecSignBits =
4241computeNumSignBitsVectorConstant(V, DemandedElts, TyBits))
4242return VecSignBits;
4243
4244KnownBits Known(TyBits);
4245computeKnownBits(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.
4249return std::max(FirstAnswer, Known.countMinSignBits());
4250}
4251
4252Intrinsic::IDllvm::getIntrinsicForCallSite(constCallBase &CB,
4253constTargetLibraryInfo *TLI) {
4254constFunction *F = CB.getCalledFunction();
4255if (!F)
4256returnIntrinsic::not_intrinsic;
4257
4258if (F->isIntrinsic())
4259returnF->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.
4264LibFunc Func;
4265if (F->hasLocalLinkage() || !TLI || !TLI->getLibFunc(CB, Func) ||
4266 !CB.onlyReadsMemory())
4267returnIntrinsic::not_intrinsic;
4268
4269switch (Func) {
4270default:
4271break;
4272case LibFunc_sin:
4273case LibFunc_sinf:
4274case LibFunc_sinl:
4275return Intrinsic::sin;
4276case LibFunc_cos:
4277case LibFunc_cosf:
4278case LibFunc_cosl:
4279return Intrinsic::cos;
4280case LibFunc_tan:
4281case LibFunc_tanf:
4282case LibFunc_tanl:
4283return Intrinsic::tan;
4284case LibFunc_asin:
4285case LibFunc_asinf:
4286case LibFunc_asinl:
4287return Intrinsic::asin;
4288case LibFunc_acos:
4289case LibFunc_acosf:
4290case LibFunc_acosl:
4291return Intrinsic::acos;
4292case LibFunc_atan:
4293case LibFunc_atanf:
4294case LibFunc_atanl:
4295return Intrinsic::atan;
4296case LibFunc_atan2:
4297case LibFunc_atan2f:
4298case LibFunc_atan2l:
4299return Intrinsic::atan2;
4300case LibFunc_sinh:
4301case LibFunc_sinhf:
4302case LibFunc_sinhl:
4303return Intrinsic::sinh;
4304case LibFunc_cosh:
4305case LibFunc_coshf:
4306case LibFunc_coshl:
4307return Intrinsic::cosh;
4308case LibFunc_tanh:
4309case LibFunc_tanhf:
4310case LibFunc_tanhl:
4311return Intrinsic::tanh;
4312case LibFunc_exp:
4313case LibFunc_expf:
4314case LibFunc_expl:
4315return Intrinsic::exp;
4316case LibFunc_exp2:
4317case LibFunc_exp2f:
4318case LibFunc_exp2l:
4319return Intrinsic::exp2;
4320case LibFunc_exp10:
4321case LibFunc_exp10f:
4322case LibFunc_exp10l:
4323return Intrinsic::exp10;
4324case LibFunc_log:
4325case LibFunc_logf:
4326case LibFunc_logl:
4327return Intrinsic::log;
4328case LibFunc_log10:
4329case LibFunc_log10f:
4330case LibFunc_log10l:
4331return Intrinsic::log10;
4332case LibFunc_log2:
4333case LibFunc_log2f:
4334case LibFunc_log2l:
4335return Intrinsic::log2;
4336case LibFunc_fabs:
4337case LibFunc_fabsf:
4338case LibFunc_fabsl:
4339return Intrinsic::fabs;
4340case LibFunc_fmin:
4341case LibFunc_fminf:
4342case LibFunc_fminl:
4343return Intrinsic::minnum;
4344case LibFunc_fmax:
4345case LibFunc_fmaxf:
4346case LibFunc_fmaxl:
4347return Intrinsic::maxnum;
4348case LibFunc_copysign:
4349case LibFunc_copysignf:
4350case LibFunc_copysignl:
4351return Intrinsic::copysign;
4352case LibFunc_floor:
4353case LibFunc_floorf:
4354case LibFunc_floorl:
4355return Intrinsic::floor;
4356case LibFunc_ceil:
4357case LibFunc_ceilf:
4358case LibFunc_ceill:
4359return Intrinsic::ceil;
4360case LibFunc_trunc:
4361case LibFunc_truncf:
4362case LibFunc_truncl:
4363return Intrinsic::trunc;
4364case LibFunc_rint:
4365case LibFunc_rintf:
4366case LibFunc_rintl:
4367return Intrinsic::rint;
4368case LibFunc_nearbyint:
4369case LibFunc_nearbyintf:
4370case LibFunc_nearbyintl:
4371return Intrinsic::nearbyint;
4372case LibFunc_round:
4373case LibFunc_roundf:
4374case LibFunc_roundl:
4375return Intrinsic::round;
4376case LibFunc_roundeven:
4377case LibFunc_roundevenf:
4378case LibFunc_roundevenl:
4379return Intrinsic::roundeven;
4380case LibFunc_pow:
4381case LibFunc_powf:
4382case LibFunc_powl:
4383return Intrinsic::pow;
4384case LibFunc_sqrt:
4385case LibFunc_sqrtf:
4386case LibFunc_sqrtl:
4387return Intrinsic::sqrt;
4388 }
4389
4390returnIntrinsic::not_intrinsic;
4391}
4392
4393/// Return true if it's possible to assume IEEE treatment of input denormals in
4394/// \p F for \p Val.
4395staticboolinputDenormalIsIEEE(constFunction &F,constType *Ty) {
4396 Ty = Ty->getScalarType();
4397returnF.getDenormalMode(Ty->getFltSemantics()).Input ==DenormalMode::IEEE;
4398}
4399
4400staticboolinputDenormalIsIEEEOrPosZero(constFunction &F,constType *Ty) {
4401 Ty = Ty->getScalarType();
4402DenormalMode Mode =F.getDenormalMode(Ty->getFltSemantics());
4403return Mode.Input ==DenormalMode::IEEE ||
4404 Mode.Input ==DenormalMode::PositiveZero;
4405}
4406
4407staticbooloutputDenormalIsIEEEOrPosZero(constFunction &F,constType *Ty) {
4408 Ty = Ty->getScalarType();
4409DenormalMode Mode =F.getDenormalMode(Ty->getFltSemantics());
4410return Mode.Output ==DenormalMode::IEEE ||
4411 Mode.Output ==DenormalMode::PositiveZero;
4412}
4413
4414boolKnownFPClass::isKnownNeverLogicalZero(constFunction &F,Type *Ty) const{
4415returnisKnownNeverZero() &&
4416 (isKnownNeverSubnormal() ||inputDenormalIsIEEE(F, Ty));
4417}
4418
4419boolKnownFPClass::isKnownNeverLogicalNegZero(constFunction &F,
4420Type *Ty) const{
4421returnisKnownNeverNegZero() &&
4422 (isKnownNeverNegSubnormal() ||inputDenormalIsIEEEOrPosZero(F, Ty));
4423}
4424
4425boolKnownFPClass::isKnownNeverLogicalPosZero(constFunction &F,
4426Type *Ty) const{
4427if (!isKnownNeverPosZero())
4428returnfalse;
4429
4430// If we know there are no denormals, nothing can be flushed to zero.
4431if (isKnownNeverSubnormal())
4432returntrue;
4433
4434DenormalMode Mode =F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4435switch (Mode.Input) {
4436caseDenormalMode::IEEE:
4437returntrue;
4438caseDenormalMode::PreserveSign:
4439// Negative subnormal won't flush to +0
4440returnisKnownNeverPosSubnormal();
4441caseDenormalMode::PositiveZero:
4442default:
4443// Both positive and negative subnormal could flush to +0
4444returnfalse;
4445 }
4446
4447llvm_unreachable("covered switch over denormal mode");
4448}
4449
4450voidKnownFPClass::propagateDenormal(constKnownFPClass &Src,constFunction &F,
4451Type *Ty) {
4452KnownFPClasses = 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.
4455if (!Src.isKnownNeverPosZero() && !Src.isKnownNeverNegZero())
4456return;
4457
4458// If we know the input can't be a denormal, it can't be flushed to 0.
4459if (Src.isKnownNeverSubnormal())
4460return;
4461
4462DenormalMode Mode =F.getDenormalMode(Ty->getScalarType()->getFltSemantics());
4463
4464if (!Src.isKnownNeverPosSubnormal() && Mode !=DenormalMode::getIEEE())
4465KnownFPClasses |=fcPosZero;
4466
4467if (!Src.isKnownNeverNegSubnormal() && Mode !=DenormalMode::getIEEE()) {
4468if (Mode !=DenormalMode::getPositiveZero())
4469KnownFPClasses |=fcNegZero;
4470
4471if (Mode.Input ==DenormalMode::PositiveZero ||
4472 Mode.Output ==DenormalMode::PositiveZero ||
4473 Mode.Input ==DenormalMode::Dynamic ||
4474 Mode.Output ==DenormalMode::Dynamic)
4475KnownFPClasses |=fcPosZero;
4476 }
4477}
4478
4479voidKnownFPClass::propagateCanonicalizingSrc(constKnownFPClass &Src,
4480constFunction &F,Type *Ty) {
4481propagateDenormal(Src,F, Ty);
4482propagateNaN(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.
4488boolllvm::isSignBitCheck(ICmpInst::Predicate Pred,constAPInt &RHS,
4489bool &TrueIfSigned) {
4490switch (Pred) {
4491caseICmpInst::ICMP_SLT:// True if LHS s< 0
4492 TrueIfSigned =true;
4493returnRHS.isZero();
4494caseICmpInst::ICMP_SLE:// True if LHS s<= -1
4495 TrueIfSigned =true;
4496returnRHS.isAllOnes();
4497caseICmpInst::ICMP_SGT:// True if LHS s> -1
4498 TrueIfSigned =false;
4499returnRHS.isAllOnes();
4500caseICmpInst::ICMP_SGE:// True if LHS s>= 0
4501 TrueIfSigned =false;
4502returnRHS.isZero();
4503caseICmpInst::ICMP_UGT:
4504// True if LHS u> RHS and RHS == sign-bit-mask - 1
4505 TrueIfSigned =true;
4506returnRHS.isMaxSignedValue();
4507caseICmpInst::ICMP_UGE:
4508// True if LHS u>= RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4509 TrueIfSigned =true;
4510returnRHS.isMinSignedValue();
4511caseICmpInst::ICMP_ULT:
4512// True if LHS u< RHS and RHS == sign-bit-mask (2^7, 2^15, 2^31, etc)
4513 TrueIfSigned =false;
4514returnRHS.isMinSignedValue();
4515caseICmpInst::ICMP_ULE:
4516// True if LHS u<= RHS and RHS == sign-bit-mask - 1
4517 TrueIfSigned =false;
4518returnRHS.isMaxSignedValue();
4519default:
4520returnfalse;
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,
4527constFunction &F,
4528Value *LHS,Value *RHS,
4529bool LookThroughSrc) {
4530constAPFloat *ConstRHS;
4531if (!match(RHS,m_APFloatAllowPoison(ConstRHS)))
4532return {nullptr,fcAllFlags};
4533
4534returnfcmpToClassTest(Pred,F,LHS, ConstRHS, LookThroughSrc);
4535}
4536
4537std::pair<Value *, FPClassTest>
4538llvm::fcmpToClassTest(FCmpInst::Predicate Pred,constFunction &F,Value *LHS,
4539constAPFloat *ConstRHS,bool LookThroughSrc) {
4540
4541auto [Src, ClassIfTrue, ClassIfFalse] =
4542fcmpImpliesClass(Pred,F,LHS, *ConstRHS, LookThroughSrc);
4543if (Src && ClassIfTrue == ~ClassIfFalse)
4544return {Src, ClassIfTrue};
4545return {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,
4551FPClassTest M) {
4552return {V, M, ~M};
4553}
4554
4555std::tuple<Value *, FPClassTest, FPClassTest>
4556llvm::fcmpImpliesClass(CmpInst::Predicate Pred,constFunction &F,Value *LHS,
4557FPClassTest RHSClass,bool LookThroughSrc) {
4558assert(RHSClass !=fcNone);
4559Value *Src =LHS;
4560
4561if (Pred ==FCmpInst::FCMP_TRUE)
4562returnexactClass(Src,fcAllFlags);
4563
4564if (Pred ==FCmpInst::FCMP_FALSE)
4565returnexactClass(Src,fcNone);
4566
4567constFPClassTest OrigClass = RHSClass;
4568
4569constbool IsNegativeRHS = (RHSClass &fcNegative) == RHSClass;
4570constbool IsPositiveRHS = (RHSClass &fcPositive) == RHSClass;
4571constbool IsNaN = (RHSClass & ~fcNan) ==fcNone;
4572
4573if (IsNaN) {
4574// fcmp o__ x, nan -> false
4575// fcmp u__ x, nan -> true
4576returnexactClass(Src,CmpInst::isOrdered(Pred) ?fcNone :fcAllFlags);
4577 }
4578
4579// fcmp ord x, zero|normal|subnormal|inf -> ~fcNan
4580if (Pred ==FCmpInst::FCMP_ORD)
4581returnexactClass(Src, ~fcNan);
4582
4583// fcmp uno x, zero|normal|subnormal|inf -> fcNan
4584if (Pred ==FCmpInst::FCMP_UNO)
4585returnexactClass(Src,fcNan);
4586
4587constbool IsFabs = LookThroughSrc &&match(LHS,m_FAbs(m_Value(Src)));
4588if (IsFabs)
4589 RHSClass =llvm::inverse_fabs(RHSClass);
4590
4591constbool IsZero = (OrigClass &fcZero) == OrigClass;
4592if (IsZero) {
4593assert(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.
4597if (!inputDenormalIsIEEE(F,LHS->getType()))
4598return {nullptr,fcAllFlags,fcAllFlags};
4599
4600switch (Pred) {
4601caseFCmpInst::FCMP_OEQ:// Match x == 0.0
4602returnexactClass(Src,fcZero);
4603caseFCmpInst::FCMP_UEQ:// Match isnan(x) || (x == 0.0)
4604returnexactClass(Src,fcZero |fcNan);
4605caseFCmpInst::FCMP_UNE:// Match (x != 0.0)
4606returnexactClass(Src, ~fcZero);
4607caseFCmpInst::FCMP_ONE:// Match !isnan(x) && x != 0.0
4608returnexactClass(Src, ~fcNan & ~fcZero);
4609caseFCmpInst::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.
4612returnexactClass(Src, ~fcNan);
4613caseFCmpInst::FCMP_UNO:
4614returnexactClass(Src,fcNan);
4615caseFCmpInst::FCMP_OGT:// x > 0
4616returnexactClass(Src,fcPosSubnormal |fcPosNormal |fcPosInf);
4617caseFCmpInst::FCMP_UGT:// isnan(x) || x > 0
4618returnexactClass(Src,fcPosSubnormal |fcPosNormal |fcPosInf |fcNan);
4619caseFCmpInst::FCMP_OGE:// x >= 0
4620returnexactClass(Src,fcPositive |fcNegZero);
4621caseFCmpInst::FCMP_UGE:// isnan(x) || x >= 0
4622returnexactClass(Src,fcPositive |fcNegZero |fcNan);
4623caseFCmpInst::FCMP_OLT:// x < 0
4624returnexactClass(Src,fcNegSubnormal |fcNegNormal |fcNegInf);
4625caseFCmpInst::FCMP_ULT:// isnan(x) || x < 0
4626returnexactClass(Src,fcNegSubnormal |fcNegNormal |fcNegInf |fcNan);
4627caseFCmpInst::FCMP_OLE:// x <= 0
4628returnexactClass(Src,fcNegative |fcPosZero);
4629caseFCmpInst::FCMP_ULE:// isnan(x) || x <= 0
4630returnexactClass(Src,fcNegative |fcPosZero |fcNan);
4631default:
4632llvm_unreachable("all compare types are handled");
4633 }
4634
4635return {nullptr,fcAllFlags,fcAllFlags};
4636 }
4637
4638constbool IsDenormalRHS = (OrigClass &fcSubnormal) == OrigClass;
4639
4640constbool IsInf = (OrigClass &fcInf) == OrigClass;
4641if (IsInf) {
4642FPClassTest Mask =fcAllFlags;
4643
4644switch (Pred) {
4645caseFCmpInst::FCMP_OEQ:
4646caseFCmpInst::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
4658if (IsNegativeRHS) {
4659 Mask =fcNegInf;
4660if (IsFabs)
4661 Mask =fcNone;
4662 }else {
4663 Mask =fcPosInf;
4664if (IsFabs)
4665 Mask |=fcNegInf;
4666 }
4667break;
4668 }
4669caseFCmpInst::FCMP_ONE:
4670caseFCmpInst::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
4681if (IsNegativeRHS) {
4682 Mask = ~fcNegInf & ~fcNan;
4683if (IsFabs)
4684 Mask = ~fcNan;
4685 }else {
4686 Mask = ~fcPosInf & ~fcNan;
4687if (IsFabs)
4688 Mask &= ~fcNegInf;
4689 }
4690
4691break;
4692 }
4693caseFCmpInst::FCMP_OLT:
4694caseFCmpInst::FCMP_UGE: {
4695if (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;
4701break;
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;
4709if (!IsFabs)
4710 Mask |=fcNegInf;
4711break;
4712 }
4713caseFCmpInst::FCMP_OGE:
4714caseFCmpInst::FCMP_ULT: {
4715if (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;
4721break;
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;
4729if (IsFabs)
4730 Mask |=fcNegInf;
4731break;
4732 }
4733caseFCmpInst::FCMP_OGT:
4734caseFCmpInst::FCMP_ULE: {
4735if (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);
4741break;
4742 }
4743
4744// No value is ordered and greater than infinity.
4745 Mask =fcNone;
4746break;
4747 }
4748caseFCmpInst::FCMP_OLE:
4749caseFCmpInst::FCMP_UGT: {
4750if (IsNegativeRHS) {
4751 Mask = IsFabs ?fcNone :fcNegInf;
4752break;
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;
4760break;
4761 }
4762default:
4763llvm_unreachable("all compare types are handled");
4764 }
4765
4766// Invert the comparison for the unordered cases.
4767if (FCmpInst::isUnordered(Pred))
4768 Mask = ~Mask;
4769
4770returnexactClass(Src, Mask);
4771 }
4772
4773if (Pred ==FCmpInst::FCMP_OEQ)
4774return {Src, RHSClass,fcAllFlags};
4775
4776if (Pred ==FCmpInst::FCMP_UEQ) {
4777FPClassTest Class = RHSClass |fcNan;
4778return {Src, Class, ~fcNan};
4779 }
4780
4781if (Pred ==FCmpInst::FCMP_ONE)
4782return {Src, ~fcNan, RHSClass |fcNan};
4783
4784if (Pred ==FCmpInst::FCMP_UNE)
4785return {Src,fcAllFlags, RHSClass};
4786
4787assert((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
4793if (IsNegativeRHS) {
4794// TODO: Handle fneg(fabs)
4795if (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
4800switch (Pred) {
4801caseFCmpInst::FCMP_OGT:
4802caseFCmpInst::FCMP_OGE:
4803return {Src, ~fcNan,fcNan};
4804caseFCmpInst::FCMP_UGT:
4805caseFCmpInst::FCMP_UGE:
4806return {Src,fcAllFlags,fcNone};
4807caseFCmpInst::FCMP_OLT:
4808caseFCmpInst::FCMP_OLE:
4809return {Src,fcNone,fcAllFlags};
4810caseFCmpInst::FCMP_ULT:
4811caseFCmpInst::FCMP_ULE:
4812return {Src,fcNan, ~fcNan};
4813default:
4814break;
4815 }
4816
4817return {nullptr,fcAllFlags,fcAllFlags};
4818 }
4819
4820FPClassTest ClassesLE =fcNegInf |fcNegNormal;
4821FPClassTest ClassesGE =fcPositive |fcNegZero |fcNegSubnormal;
4822
4823if (IsDenormalRHS)
4824 ClassesLE |=fcNegSubnormal;
4825else
4826 ClassesGE |=fcNegNormal;
4827
4828switch (Pred) {
4829caseFCmpInst::FCMP_OGT:
4830caseFCmpInst::FCMP_OGE:
4831return {Src, ClassesGE, ~ClassesGE | RHSClass};
4832caseFCmpInst::FCMP_UGT:
4833caseFCmpInst::FCMP_UGE:
4834return {Src, ClassesGE |fcNan, ~(ClassesGE |fcNan) | RHSClass};
4835caseFCmpInst::FCMP_OLT:
4836caseFCmpInst::FCMP_OLE:
4837return {Src, ClassesLE, ~ClassesLE | RHSClass};
4838caseFCmpInst::FCMP_ULT:
4839caseFCmpInst::FCMP_ULE:
4840return {Src, ClassesLE |fcNan, ~(ClassesLE |fcNan) | RHSClass};
4841default:
4842break;
4843 }
4844 }elseif (IsPositiveRHS) {
4845FPClassTest ClassesGE =fcPosNormal |fcPosInf;
4846FPClassTest ClassesLE =fcNegative |fcPosZero |fcPosSubnormal;
4847if (IsDenormalRHS)
4848 ClassesGE |=fcPosSubnormal;
4849else
4850 ClassesLE |=fcPosNormal;
4851
4852if (IsFabs) {
4853 ClassesGE =llvm::inverse_fabs(ClassesGE);
4854 ClassesLE =llvm::inverse_fabs(ClassesLE);
4855 }
4856
4857switch (Pred) {
4858caseFCmpInst::FCMP_OGT:
4859caseFCmpInst::FCMP_OGE:
4860return {Src, ClassesGE, ~ClassesGE | RHSClass};
4861caseFCmpInst::FCMP_UGT:
4862caseFCmpInst::FCMP_UGE:
4863return {Src, ClassesGE |fcNan, ~(ClassesGE |fcNan) | RHSClass};
4864caseFCmpInst::FCMP_OLT:
4865caseFCmpInst::FCMP_OLE:
4866return {Src, ClassesLE, ~ClassesLE | RHSClass};
4867caseFCmpInst::FCMP_ULT:
4868caseFCmpInst::FCMP_ULE:
4869return {Src, ClassesLE |fcNan, ~(ClassesLE |fcNan) | RHSClass};
4870default:
4871break;
4872 }
4873 }
4874
4875return {nullptr,fcAllFlags,fcAllFlags};
4876}
4877
4878std::tuple<Value *, FPClassTest, FPClassTest>
4879llvm::fcmpImpliesClass(CmpInst::Predicate Pred,constFunction &F,Value *LHS,
4880constAPFloat &ConstRHS,bool LookThroughSrc) {
4881// We can refine checks against smallest normal / largest denormal to an
4882// exact class test.
4883if (!ConstRHS.isNegative() && ConstRHS.isSmallestNormalized()) {
4884Value *Src =LHS;
4885constbool IsFabs = LookThroughSrc &&match(LHS,m_FAbs(m_Value(Src)));
4886
4887FPClassTest Mask;
4888// Match pattern that's used in __builtin_isnormal.
4889switch (Pred) {
4890caseFCmpInst::FCMP_OLT:
4891caseFCmpInst::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;
4897if (!IsFabs)
4898 Mask |=fcNegNormal |fcNegInf;
4899
4900break;
4901 }
4902caseFCmpInst::FCMP_OGE:
4903caseFCmpInst::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;
4909if (IsFabs)
4910 Mask |=fcNegInf |fcNegNormal;
4911break;
4912 }
4913default:
4914returnfcmpImpliesClass(Pred,F,LHS, ConstRHS.classify(),
4915 LookThroughSrc);
4916 }
4917
4918// Invert the comparison for the unordered cases.
4919if (FCmpInst::isUnordered(Pred))
4920 Mask = ~Mask;
4921
4922returnexactClass(Src, Mask);
4923 }
4924
4925returnfcmpImpliesClass(Pred,F,LHS, ConstRHS.classify(), LookThroughSrc);
4926}
4927
4928std::tuple<Value *, FPClassTest, FPClassTest>
4929llvm::fcmpImpliesClass(CmpInst::Predicate Pred,constFunction &F,Value *LHS,
4930Value *RHS,bool LookThroughSrc) {
4931constAPFloat *ConstRHS;
4932if (!match(RHS,m_APFloatAllowPoison(ConstRHS)))
4933return {nullptr,fcAllFlags,fcAllFlags};
4934
4935// TODO: Just call computeKnownFPClass for RHS to handle non-constants.
4936returnfcmpImpliesClass(Pred,F,LHS, *ConstRHS, LookThroughSrc);
4937}
4938
4939staticvoidcomputeKnownFPClassFromCond(constValue *V,Value *Cond,
4940unsignedDepth,bool CondIsTrue,
4941constInstruction *CxtI,
4942KnownFPClass &KnownFromContext) {
4943Value *A, *B;
4944if (Depth <MaxAnalysisRecursionDepth &&
4945 (CondIsTrue ?match(Cond,m_LogicalAnd(m_Value(A),m_Value(B)))
4946 :match(Cond,m_LogicalOr(m_Value(A),m_Value(B))))) {
4947computeKnownFPClassFromCond(V,A,Depth + 1, CondIsTrue, CxtI,
4948 KnownFromContext);
4949computeKnownFPClassFromCond(V,B,Depth + 1, CondIsTrue, CxtI,
4950 KnownFromContext);
4951return;
4952 }
4953CmpPredicate Pred;
4954Value *LHS;
4955uint64_t ClassVal = 0;
4956constAPFloat *CRHS;
4957constAPInt *RHS;
4958if (match(Cond,m_FCmp(Pred,m_Value(LHS),m_APFloat(CRHS)))) {
4959auto [CmpVal, MaskIfTrue, MaskIfFalse] =fcmpImpliesClass(
4960 Pred, *CxtI->getParent()->getParent(),LHS, *CRHS,LHS != V);
4961if (CmpVal == V)
4962 KnownFromContext.knownNot(~(CondIsTrue ? MaskIfTrue : MaskIfFalse));
4963 }elseif (match(Cond, m_Intrinsic<Intrinsic::is_fpclass>(
4964m_Specific(V),m_ConstantInt(ClassVal)))) {
4965FPClassTest Mask =static_cast<FPClassTest>(ClassVal);
4966 KnownFromContext.knownNot(CondIsTrue ? ~Mask : Mask);
4967 }elseif (match(Cond,m_ICmp(Pred,m_ElementWiseBitCast(m_Specific(V)),
4968m_APInt(RHS)))) {
4969bool TrueIfSigned;
4970if (!isSignBitCheck(Pred, *RHS, TrueIfSigned))
4971return;
4972if (TrueIfSigned == CondIsTrue)
4973 KnownFromContext.signBitMustBeOne();
4974else
4975 KnownFromContext.signBitMustBeZero();
4976 }
4977}
4978
4979staticKnownFPClasscomputeKnownFPClassFromContext(constValue *V,
4980constSimplifyQuery &Q) {
4981KnownFPClass KnownFromContext;
4982
4983if (!Q.CxtI)
4984return KnownFromContext;
4985
4986if (Q.DC && Q.DT) {
4987// Handle dominating conditions.
4988for (BranchInst *BI : Q.DC->conditionsFor(V)) {
4989Value *Cond = BI->getCondition();
4990
4991BasicBlockEdge Edge0(BI->getParent(), BI->getSuccessor(0));
4992if (Q.DT->dominates(Edge0, Q.CxtI->getParent()))
4993computeKnownFPClassFromCond(V,Cond,/*Depth=*/0,/*CondIsTrue=*/true,
4994 Q.CxtI, KnownFromContext);
4995
4996BasicBlockEdge Edge1(BI->getParent(), BI->getSuccessor(1));
4997if (Q.DT->dominates(Edge1, Q.CxtI->getParent()))
4998computeKnownFPClassFromCond(V,Cond,/*Depth=*/0,/*CondIsTrue=*/false,
4999 Q.CxtI, KnownFromContext);
5000 }
5001 }
5002
5003if (!Q.AC)
5004return KnownFromContext;
5005
5006// Try to restrict the floating-point classes based on information from
5007// assumptions.
5008for (auto &AssumeVH : Q.AC->assumptionsFor(V)) {
5009if (!AssumeVH)
5010continue;
5011CallInst *I = cast<CallInst>(AssumeVH);
5012
5013assert(I->getFunction() == Q.CxtI->getParent()->getParent() &&
5014"Got assumption for the wrong function!");
5015assert(I->getIntrinsicID() == Intrinsic::assume &&
5016"must be an assume intrinsic");
5017
5018if (!isValidAssumeForContext(I, Q.CxtI, Q.DT))
5019continue;
5020
5021computeKnownFPClassFromCond(V,I->getArgOperand(0),/*Depth=*/0,
5022/*CondIsTrue=*/true, Q.CxtI, KnownFromContext);
5023 }
5024
5025return KnownFromContext;
5026}
5027
5028voidcomputeKnownFPClass(constValue *V,constAPInt &DemandedElts,
5029FPClassTest InterestedClasses,KnownFPClass &Known,
5030unsignedDepth,constSimplifyQuery &Q);
5031
5032staticvoidcomputeKnownFPClass(constValue *V,KnownFPClass &Known,
5033FPClassTest InterestedClasses,unsignedDepth,
5034constSimplifyQuery &Q) {
5035auto *FVTy = dyn_cast<FixedVectorType>(V->getType());
5036APInt DemandedElts =
5037 FVTy ?APInt::getAllOnes(FVTy->getNumElements()) :APInt(1, 1);
5038computeKnownFPClass(V, DemandedElts, InterestedClasses, Known,Depth, Q);
5039}
5040
5041staticvoidcomputeKnownFPClassForFPTrunc(constOperator *Op,
5042constAPInt &DemandedElts,
5043FPClassTest InterestedClasses,
5044KnownFPClass &Known,unsignedDepth,
5045constSimplifyQuery &Q) {
5046if ((InterestedClasses &
5047 (KnownFPClass::OrderedLessThanZeroMask |fcNan)) ==fcNone)
5048return;
5049
5050KnownFPClass KnownSrc;
5051computeKnownFPClass(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
5056if (KnownSrc.cannotBeOrderedLessThanZero())
5057 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);
5058
5059 Known.propagateNaN(KnownSrc,true);
5060
5061// Infinity needs a range check.
5062}
5063
5064voidcomputeKnownFPClass(constValue *V,constAPInt &DemandedElts,
5065FPClassTest InterestedClasses,KnownFPClass &Known,
5066unsignedDepth,constSimplifyQuery &Q) {
5067assert(Known.isUnknown() &&"should not be called with known information");
5068
5069if (!DemandedElts) {
5070// No demanded elts, better to assume we don't know anything.
5071 Known.resetAll();
5072return;
5073 }
5074
5075assert(Depth <=MaxAnalysisRecursionDepth &&"Limit Search Depth");
5076
5077if (auto *CFP = dyn_cast<ConstantFP>(V)) {
5078 Known.KnownFPClasses = CFP->getValueAPF().classify();
5079 Known.SignBit = CFP->isNegative();
5080return;
5081 }
5082
5083if (isa<ConstantAggregateZero>(V)) {
5084 Known.KnownFPClasses =fcPosZero;
5085 Known.SignBit =false;
5086return;
5087 }
5088
5089if (isa<PoisonValue>(V)) {
5090 Known.KnownFPClasses =fcNone;
5091 Known.SignBit =false;
5092return;
5093 }
5094
5095// Try to handle fixed width vector constants
5096auto *VFVTy = dyn_cast<FixedVectorType>(V->getType());
5097constConstant *CV = dyn_cast<Constant>(V);
5098if (VFVTy && CV) {
5099 Known.KnownFPClasses =fcNone;
5100bool SignBitAllZero =true;
5101bool SignBitAllOne =true;
5102
5103// For vectors, verify that each element is not NaN.
5104unsigned NumElts = VFVTy->getNumElements();
5105for (unsigned i = 0; i != NumElts; ++i) {
5106if (!DemandedElts[i])
5107continue;
5108
5109Constant *Elt = CV->getAggregateElement(i);
5110if (!Elt) {
5111 Known =KnownFPClass();
5112return;
5113 }
5114if (isa<PoisonValue>(Elt))
5115continue;
5116auto *CElt = dyn_cast<ConstantFP>(Elt);
5117if (!CElt) {
5118 Known =KnownFPClass();
5119return;
5120 }
5121
5122constAPFloat &C = CElt->getValueAPF();
5123 Known.KnownFPClasses |=C.classify();
5124if (C.isNegative())
5125 SignBitAllZero =false;
5126else
5127 SignBitAllOne =false;
5128 }
5129if (SignBitAllOne != SignBitAllZero)
5130 Known.SignBit = SignBitAllOne;
5131return;
5132 }
5133
5134FPClassTest KnownNotFromFlags =fcNone;
5135if (constauto *CB = dyn_cast<CallBase>(V))
5136 KnownNotFromFlags |= CB->getRetNoFPClass();
5137elseif (constauto *Arg = dyn_cast<Argument>(V))
5138 KnownNotFromFlags |= Arg->getNoFPClass();
5139
5140constOperator *Op = dyn_cast<Operator>(V);
5141if (constFPMathOperator *FPOp = dyn_cast_or_null<FPMathOperator>(Op)) {
5142if (FPOp->hasNoNaNs())
5143 KnownNotFromFlags |=fcNan;
5144if (FPOp->hasNoInfs())
5145 KnownNotFromFlags |=fcInf;
5146 }
5147
5148KnownFPClass 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
5155auto ClearClassesFromFlags =make_scope_exit([=, &Known] {
5156 Known.knownNot(KnownNotFromFlags);
5157if (!Known.SignBit && AssumedClasses.SignBit) {
5158 if (*AssumedClasses.SignBit)
5159 Known.signBitMustBeOne();
5160 else
5161 Known.signBitMustBeZero();
5162 }
5163 });
5164
5165if (!Op)
5166return;
5167
5168// All recursive calls that increase depth must come after this.
5169if (Depth ==MaxAnalysisRecursionDepth)
5170return;
5171
5172constunsigned Opc =Op->getOpcode();
5173switch (Opc) {
5174case Instruction::FNeg: {
5175computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5176 Known,Depth + 1, Q);
5177 Known.fneg();
5178break;
5179 }
5180case Instruction::Select: {
5181Value *Cond =Op->getOperand(0);
5182Value *LHS =Op->getOperand(1);
5183Value *RHS =Op->getOperand(2);
5184
5185FPClassTest FilterLHS =fcAllFlags;
5186FPClassTest FilterRHS =fcAllFlags;
5187
5188Value *TestedValue =nullptr;
5189FPClassTest MaskIfTrue =fcAllFlags;
5190FPClassTest MaskIfFalse =fcAllFlags;
5191uint64_t ClassVal = 0;
5192constFunction *F = cast<Instruction>(Op)->getFunction();
5193CmpPredicate Pred;
5194Value *CmpLHS, *CmpRHS;
5195if (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.
5201bool LookThroughFAbsFNeg = CmpLHS !=LHS && CmpLHS !=RHS;
5202 std::tie(TestedValue, MaskIfTrue, MaskIfFalse) =
5203fcmpImpliesClass(Pred, *F, CmpLHS, CmpRHS, LookThroughFAbsFNeg);
5204 }elseif (match(Cond,
5205 m_Intrinsic<Intrinsic::is_fpclass>(
5206m_Value(TestedValue),m_ConstantInt(ClassVal)))) {
5207FPClassTest TestedMask =static_cast<FPClassTest>(ClassVal);
5208 MaskIfTrue = TestedMask;
5209 MaskIfFalse = ~TestedMask;
5210 }
5211
5212if (TestedValue ==LHS) {
5213// match !isnan(x) ? x : y
5214 FilterLHS = MaskIfTrue;
5215 }elseif (TestedValue ==RHS) {// && IsExactClass
5216// match !isnan(x) ? y : x
5217 FilterRHS = MaskIfFalse;
5218 }
5219
5220KnownFPClass Known2;
5221computeKnownFPClass(LHS, DemandedElts, InterestedClasses & FilterLHS, Known,
5222Depth + 1, Q);
5223 Known.KnownFPClasses &= FilterLHS;
5224
5225computeKnownFPClass(RHS, DemandedElts, InterestedClasses & FilterRHS,
5226 Known2,Depth + 1, Q);
5227 Known2.KnownFPClasses &= FilterRHS;
5228
5229 Known |= Known2;
5230break;
5231 }
5232case Instruction::Call: {
5233constCallInst *II = cast<CallInst>(Op);
5234constIntrinsic::ID IID =II->getIntrinsicID();
5235switch (IID) {
5236case Intrinsic::fabs: {
5237if ((InterestedClasses & (fcNan |fcPositive)) !=fcNone) {
5238// If we only care about the sign bit we don't need to inspect the
5239// operand.
5240computeKnownFPClass(II->getArgOperand(0), DemandedElts,
5241 InterestedClasses, Known,Depth + 1, Q);
5242 }
5243
5244 Known.fabs();
5245break;
5246 }
5247case Intrinsic::copysign: {
5248KnownFPClass KnownSign;
5249
5250computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5251 Known,Depth + 1, Q);
5252computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5253 KnownSign,Depth + 1, Q);
5254 Known.copysign(KnownSign);
5255break;
5256 }
5257case Intrinsic::fma:
5258case Intrinsic::fmuladd: {
5259if ((InterestedClasses &fcNegative) ==fcNone)
5260break;
5261
5262if (II->getArgOperand(0) !=II->getArgOperand(1))
5263break;
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.
5269KnownFPClass KnownAddend;
5270computeKnownFPClass(II->getArgOperand(2), DemandedElts, InterestedClasses,
5271 KnownAddend,Depth + 1, Q);
5272
5273if (KnownAddend.cannotBeOrderedLessThanZero())
5274 Known.knownNot(fcNegative);
5275break;
5276 }
5277case Intrinsic::sqrt:
5278case Intrinsic::experimental_constrained_sqrt: {
5279KnownFPClass KnownSrc;
5280FPClassTest InterestedSrcs = InterestedClasses;
5281if (InterestedClasses &fcNan)
5282 InterestedSrcs |=KnownFPClass::OrderedLessThanZeroMask;
5283
5284computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5285 KnownSrc,Depth + 1, Q);
5286
5287if (KnownSrc.isKnownNeverPosInfinity())
5288 Known.knownNot(fcPosInf);
5289if (KnownSrc.isKnownNever(fcSNan))
5290 Known.knownNot(fcSNan);
5291
5292// Any negative value besides -0 returns a nan.
5293if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5294 Known.knownNot(fcNan);
5295
5296// The only negative value that can be returned is -0 for -0 inputs.
5297 Known.knownNot(fcNegInf |fcNegSubnormal |fcNegNormal);
5298
5299// If the input denormal mode could be PreserveSign, a negative
5300// subnormal input could produce a negative zero output.
5301constFunction *F =II->getFunction();
5302if (Q.IIQ.hasNoSignedZeros(II) ||
5303 (F && KnownSrc.isKnownNeverLogicalNegZero(*F,II->getType())))
5304 Known.knownNot(fcNegZero);
5305
5306break;
5307 }
5308case Intrinsic::sin:
5309case Intrinsic::cos: {
5310// Return NaN on infinite inputs.
5311KnownFPClass KnownSrc;
5312computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5313 KnownSrc,Depth + 1, Q);
5314 Known.knownNot(fcInf);
5315if (KnownSrc.isKnownNeverNaN() && KnownSrc.isKnownNeverInfinity())
5316 Known.knownNot(fcNan);
5317break;
5318 }
5319case Intrinsic::maxnum:
5320case Intrinsic::minnum:
5321case Intrinsic::minimum:
5322case Intrinsic::maximum: {
5323KnownFPClass KnownLHS, KnownRHS;
5324computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5325 KnownLHS,Depth + 1, Q);
5326computeKnownFPClass(II->getArgOperand(1), DemandedElts, InterestedClasses,
5327 KnownRHS,Depth + 1, Q);
5328
5329bool NeverNaN = KnownLHS.isKnownNeverNaN() || KnownRHS.isKnownNeverNaN();
5330 Known = KnownLHS | KnownRHS;
5331
5332// If either operand is not NaN, the result is not NaN.
5333if (NeverNaN && (IID == Intrinsic::minnum || IID == Intrinsic::maxnum))
5334 Known.knownNot(fcNan);
5335
5336if (IID == Intrinsic::maxnum) {
5337// If at least one operand is known to be positive, the result must be
5338// positive.
5339if ((KnownLHS.cannotBeOrderedLessThanZero() &&
5340 KnownLHS.isKnownNeverNaN()) ||
5341 (KnownRHS.cannotBeOrderedLessThanZero() &&
5342 KnownRHS.isKnownNeverNaN()))
5343 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);
5344 }elseif (IID == Intrinsic::maximum) {
5345// If at least one operand is known to be positive, the result must be
5346// positive.
5347if (KnownLHS.cannotBeOrderedLessThanZero() ||
5348 KnownRHS.cannotBeOrderedLessThanZero())
5349 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);
5350 }elseif (IID == Intrinsic::minnum) {
5351// If at least one operand is known to be negative, the result must be
5352// negative.
5353if ((KnownLHS.cannotBeOrderedGreaterThanZero() &&
5354 KnownLHS.isKnownNeverNaN()) ||
5355 (KnownRHS.cannotBeOrderedGreaterThanZero() &&
5356 KnownRHS.isKnownNeverNaN()))
5357 Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask);
5358 }else {
5359// If at least one operand is known to be negative, the result must be
5360// negative.
5361if (KnownLHS.cannotBeOrderedGreaterThanZero() ||
5362 KnownRHS.cannotBeOrderedGreaterThanZero())
5363 Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask);
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//
5373if ((Known.KnownFPClasses &fcZero) !=fcNone &&
5374 !Known.isKnownNeverSubnormal()) {
5375constFunction *Parent =II->getFunction();
5376if (!Parent)
5377break;
5378
5379DenormalMode Mode = Parent->getDenormalMode(
5380II->getType()->getScalarType()->getFltSemantics());
5381if (Mode !=DenormalMode::getIEEE())
5382 Known.KnownFPClasses |=fcZero;
5383 }
5384
5385if (Known.isKnownNeverNaN()) {
5386if (KnownLHS.SignBit && KnownRHS.SignBit &&
5387 *KnownLHS.SignBit == *KnownRHS.SignBit) {
5388if (*KnownLHS.SignBit)
5389 Known.signBitMustBeOne();
5390else
5391 Known.signBitMustBeZero();
5392 }elseif ((IID == Intrinsic::maximum || IID == Intrinsic::minimum) ||
5393 ((KnownLHS.isKnownNeverNegZero() ||
5394 KnownRHS.isKnownNeverPosZero()) &&
5395 (KnownLHS.isKnownNeverPosZero() ||
5396 KnownRHS.isKnownNeverNegZero()))) {
5397if ((IID == Intrinsic::maximum || IID == Intrinsic::maxnum) &&
5398 (KnownLHS.SignBit ==false || KnownRHS.SignBit ==false))
5399 Known.signBitMustBeZero();
5400elseif ((IID == Intrinsic::minimum || IID == Intrinsic::minnum) &&
5401 (KnownLHS.SignBit ==true || KnownRHS.SignBit ==true))
5402 Known.signBitMustBeOne();
5403 }
5404 }
5405break;
5406 }
5407case Intrinsic::canonicalize: {
5408KnownFPClass KnownSrc;
5409computeKnownFPClass(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.
5422if (KnownSrc.isKnownNeverNaN())
5423 Known.knownNot(fcNan);
5424else
5425 Known.knownNot(fcSNan);
5426
5427constFunction *F =II->getFunction();
5428if (!F)
5429break;
5430
5431// If the parent function flushes denormals, the canonical output cannot
5432// be a denormal.
5433constfltSemantics &FPType =
5434II->getType()->getScalarType()->getFltSemantics();
5435DenormalMode DenormMode =F->getDenormalMode(FPType);
5436if (DenormMode ==DenormalMode::getIEEE()) {
5437if (KnownSrc.isKnownNever(fcPosZero))
5438 Known.knownNot(fcPosZero);
5439if (KnownSrc.isKnownNever(fcNegZero))
5440 Known.knownNot(fcNegZero);
5441break;
5442 }
5443
5444if (DenormMode.inputsAreZero() || DenormMode.outputsAreZero())
5445 Known.knownNot(fcSubnormal);
5446
5447if (DenormMode.Input ==DenormalMode::PositiveZero ||
5448 (DenormMode.Output ==DenormalMode::PositiveZero &&
5449 DenormMode.Input ==DenormalMode::IEEE))
5450 Known.knownNot(fcNegZero);
5451
5452break;
5453 }
5454case Intrinsic::vector_reduce_fmax:
5455case Intrinsic::vector_reduce_fmin:
5456case Intrinsic::vector_reduce_fmaximum:
5457case 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.
5463if (!Known.isKnownNeverNaN())
5464 Known.SignBit.reset();
5465break;
5466 }
5467// reverse preserves all characteristics of the input vec's element.
5468case Intrinsic::vector_reverse:
5469 Known =computeKnownFPClass(
5470II->getArgOperand(0), DemandedElts.reverseBits(),
5471II->getFastMathFlags(), InterestedClasses,Depth + 1, Q);
5472break;
5473case Intrinsic::trunc:
5474case Intrinsic::floor:
5475case Intrinsic::ceil:
5476case Intrinsic::rint:
5477case Intrinsic::nearbyint:
5478case Intrinsic::round:
5479case Intrinsic::roundeven: {
5480KnownFPClass KnownSrc;
5481FPClassTest InterestedSrcs = InterestedClasses;
5482if (InterestedSrcs &fcPosFinite)
5483 InterestedSrcs |=fcPosFinite;
5484if (InterestedSrcs &fcNegFinite)
5485 InterestedSrcs |=fcNegFinite;
5486computeKnownFPClass(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.
5496if (IID == Intrinsic::trunc || !V->getType()->isMultiUnitFPType()) {
5497if (KnownSrc.isKnownNeverPosInfinity())
5498 Known.knownNot(fcPosInf);
5499if (KnownSrc.isKnownNeverNegInfinity())
5500 Known.knownNot(fcNegInf);
5501 }
5502
5503// Negative round ups to 0 produce -0
5504if (KnownSrc.isKnownNever(fcPosFinite))
5505 Known.knownNot(fcPosFinite);
5506if (KnownSrc.isKnownNever(fcNegFinite))
5507 Known.knownNot(fcNegFinite);
5508
5509break;
5510 }
5511case Intrinsic::exp:
5512case Intrinsic::exp2:
5513case Intrinsic::exp10: {
5514 Known.knownNot(fcNegative);
5515if ((InterestedClasses &fcNan) ==fcNone)
5516break;
5517
5518KnownFPClass KnownSrc;
5519computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5520 KnownSrc,Depth + 1, Q);
5521if (KnownSrc.isKnownNeverNaN()) {
5522 Known.knownNot(fcNan);
5523 Known.signBitMustBeZero();
5524 }
5525
5526break;
5527 }
5528case Intrinsic::fptrunc_round: {
5529computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5530Depth, Q);
5531break;
5532 }
5533case Intrinsic::log:
5534case Intrinsic::log10:
5535case Intrinsic::log2:
5536case Intrinsic::experimental_constrained_log:
5537case Intrinsic::experimental_constrained_log10:
5538case Intrinsic::experimental_constrained_log2: {
5539// log(+inf) -> +inf
5540// log([+-]0.0) -> -inf
5541// log(-inf) -> nan
5542// log(-x) -> nan
5543if ((InterestedClasses & (fcNan |fcInf)) ==fcNone)
5544break;
5545
5546FPClassTest InterestedSrcs = InterestedClasses;
5547if ((InterestedClasses &fcNegInf) !=fcNone)
5548 InterestedSrcs |=fcZero |fcSubnormal;
5549if ((InterestedClasses &fcNan) !=fcNone)
5550 InterestedSrcs |=fcNan | (fcNegative & ~fcNan);
5551
5552KnownFPClass KnownSrc;
5553computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedSrcs,
5554 KnownSrc,Depth + 1, Q);
5555
5556if (KnownSrc.isKnownNeverPosInfinity())
5557 Known.knownNot(fcPosInf);
5558
5559if (KnownSrc.isKnownNeverNaN() && KnownSrc.cannotBeOrderedLessThanZero())
5560 Known.knownNot(fcNan);
5561
5562constFunction *F =II->getFunction();
5563if (F && KnownSrc.isKnownNeverLogicalZero(*F,II->getType()))
5564 Known.knownNot(fcNegInf);
5565
5566break;
5567 }
5568case Intrinsic::powi: {
5569if ((InterestedClasses &fcNegative) ==fcNone)
5570break;
5571
5572constValue *Exp =II->getArgOperand(1);
5573Type *ExpTy = Exp->getType();
5574unsignedBitWidth = ExpTy->getScalarType()->getIntegerBitWidth();
5575KnownBits ExponentKnownBits(BitWidth);
5576computeKnownBits(Exp, isa<VectorType>(ExpTy) ? DemandedElts :APInt(1, 1),
5577 ExponentKnownBits,Depth + 1, Q);
5578
5579if (ExponentKnownBits.Zero[0]) {// Is even
5580 Known.knownNot(fcNegative);
5581break;
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.
5592KnownFPClass KnownSrc;
5593computeKnownFPClass(II->getArgOperand(0), DemandedElts,fcNegative,
5594 KnownSrc,Depth + 1, Q);
5595if (KnownSrc.isKnownNever(fcNegative))
5596 Known.knownNot(fcNegative);
5597break;
5598 }
5599case Intrinsic::ldexp: {
5600KnownFPClass KnownSrc;
5601computeKnownFPClass(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.
5606if (KnownSrc.isKnownNever(fcNegative))
5607 Known.knownNot(fcNegative);
5608elseif (KnownSrc.cannotBeOrderedLessThanZero())
5609 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);
5610
5611if (KnownSrc.isKnownNever(fcPositive))
5612 Known.knownNot(fcPositive);
5613elseif (KnownSrc.cannotBeOrderedGreaterThanZero())
5614 Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask);
5615
5616// Can refine inf/zero handling based on the exponent operand.
5617constFPClassTest ExpInfoMask =fcZero |fcSubnormal |fcInf;
5618if ((InterestedClasses & ExpInfoMask) ==fcNone)
5619break;
5620if ((KnownSrc.KnownFPClasses & ExpInfoMask) ==fcNone)
5621break;
5622
5623constfltSemantics &Flt =
5624II->getType()->getScalarType()->getFltSemantics();
5625unsigned Precision =APFloat::semanticsPrecision(Flt);
5626constValue *ExpArg =II->getArgOperand(1);
5627ConstantRange ExpRange =computeConstantRange(
5628 ExpArg,true, Q.IIQ.UseInstrInfo, Q.AC, Q.CxtI, Q.DT,Depth + 1);
5629
5630constint MantissaBits = Precision - 1;
5631if (ExpRange.getSignedMin().sge(static_cast<int64_t>(MantissaBits)))
5632 Known.knownNot(fcSubnormal);
5633
5634constFunction *F =II->getFunction();
5635constAPInt *ConstVal = ExpRange.getSingleElement();
5636if (ConstVal && ConstVal->isZero()) {
5637// ldexp(x, 0) -> x, so propagate everything.
5638 Known.propagateCanonicalizingSrc(KnownSrc, *F,II->getType());
5639 }elseif (ExpRange.isAllNegative()) {
5640// If we know the power is <= 0, can't introduce inf
5641if (KnownSrc.isKnownNeverPosInfinity())
5642 Known.knownNot(fcPosInf);
5643if (KnownSrc.isKnownNeverNegInfinity())
5644 Known.knownNot(fcNegInf);
5645 }elseif (ExpRange.isAllNonNegative()) {
5646// If we know the power is >= 0, can't introduce subnormal or zero
5647if (KnownSrc.isKnownNeverPosSubnormal())
5648 Known.knownNot(fcPosSubnormal);
5649if (KnownSrc.isKnownNeverNegSubnormal())
5650 Known.knownNot(fcNegSubnormal);
5651if (F && KnownSrc.isKnownNeverLogicalPosZero(*F,II->getType()))
5652 Known.knownNot(fcPosZero);
5653if (F && KnownSrc.isKnownNeverLogicalNegZero(*F,II->getType()))
5654 Known.knownNot(fcNegZero);
5655 }
5656
5657break;
5658 }
5659case Intrinsic::arithmetic_fence: {
5660computeKnownFPClass(II->getArgOperand(0), DemandedElts, InterestedClasses,
5661 Known,Depth + 1, Q);
5662break;
5663 }
5664case Intrinsic::experimental_constrained_sitofp:
5665case 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
5675if (IID == Intrinsic::experimental_constrained_uitofp)
5676 Known.signBitMustBeZero();
5677
5678// TODO: Copy inf handling from instructions
5679break;
5680default:
5681break;
5682 }
5683
5684break;
5685 }
5686case Instruction::FAdd:
5687case Instruction::FSub: {
5688KnownFPClass KnownLHS, KnownRHS;
5689bool WantNegative =
5690Op->getOpcode() == Instruction::FAdd &&
5691 (InterestedClasses &KnownFPClass::OrderedLessThanZeroMask) !=fcNone;
5692bool WantNaN = (InterestedClasses &fcNan) !=fcNone;
5693bool WantNegZero = (InterestedClasses &fcNegZero) !=fcNone;
5694
5695if (!WantNaN && !WantNegative && !WantNegZero)
5696break;
5697
5698FPClassTest InterestedSrcs = InterestedClasses;
5699if (WantNegative)
5700 InterestedSrcs |=KnownFPClass::OrderedLessThanZeroMask;
5701if (InterestedClasses &fcNan)
5702 InterestedSrcs |=fcInf;
5703computeKnownFPClass(Op->getOperand(1), DemandedElts, InterestedSrcs,
5704 KnownRHS,Depth + 1, Q);
5705
5706if ((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.
5712computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedSrcs,
5713 KnownLHS,Depth + 1, Q);
5714// Adding positive and negative infinity produces NaN.
5715// TODO: Check sign of infinities.
5716if (KnownLHS.isKnownNeverNaN() && KnownRHS.isKnownNeverNaN() &&
5717 (KnownLHS.isKnownNeverInfinity() || KnownRHS.isKnownNeverInfinity()))
5718 Known.knownNot(fcNan);
5719
5720// FIXME: Context function should always be passed in separately
5721constFunction *F = cast<Instruction>(Op)->getFunction();
5722
5723if (Op->getOpcode() == Instruction::FAdd) {
5724if (KnownLHS.cannotBeOrderedLessThanZero() &&
5725 KnownRHS.cannotBeOrderedLessThanZero())
5726 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);
5727if (!F)
5728break;
5729
5730// (fadd x, 0.0) is guaranteed to return +0.0, not -0.0.
5731if ((KnownLHS.isKnownNeverLogicalNegZero(*F,Op->getType()) ||
5732 KnownRHS.isKnownNeverLogicalNegZero(*F,Op->getType())) &&
5733// Make sure output negative denormal can't flush to -0
5734outputDenormalIsIEEEOrPosZero(*F,Op->getType()))
5735 Known.knownNot(fcNegZero);
5736 }else {
5737if (!F)
5738break;
5739
5740// Only fsub -0, +0 can return -0
5741if ((KnownLHS.isKnownNeverLogicalNegZero(*F,Op->getType()) ||
5742 KnownRHS.isKnownNeverLogicalPosZero(*F,Op->getType())) &&
5743// Make sure output negative denormal can't flush to -0
5744outputDenormalIsIEEEOrPosZero(*F,Op->getType()))
5745 Known.knownNot(fcNegZero);
5746 }
5747 }
5748
5749break;
5750 }
5751case Instruction::FMul: {
5752// X * X is always non-negative or a NaN.
5753if (Op->getOperand(0) ==Op->getOperand(1))
5754 Known.knownNot(fcNegative);
5755
5756if ((InterestedClasses &fcNan) !=fcNan)
5757break;
5758
5759// fcSubnormal is only needed in case of DAZ.
5760constFPClassTest NeedForNan =fcNan |fcInf |fcZero |fcSubnormal;
5761
5762KnownFPClass KnownLHS, KnownRHS;
5763computeKnownFPClass(Op->getOperand(1), DemandedElts, NeedForNan, KnownRHS,
5764Depth + 1, Q);
5765if (!KnownRHS.isKnownNeverNaN())
5766break;
5767
5768computeKnownFPClass(Op->getOperand(0), DemandedElts, NeedForNan, KnownLHS,
5769Depth + 1, Q);
5770if (!KnownLHS.isKnownNeverNaN())
5771break;
5772
5773if (KnownLHS.SignBit && KnownRHS.SignBit) {
5774if (*KnownLHS.SignBit == *KnownRHS.SignBit)
5775 Known.signBitMustBeZero();
5776else
5777 Known.signBitMustBeOne();
5778 }
5779
5780// If 0 * +/-inf produces NaN.
5781if (KnownLHS.isKnownNeverInfinity() && KnownRHS.isKnownNeverInfinity()) {
5782 Known.knownNot(fcNan);
5783break;
5784 }
5785
5786constFunction *F = cast<Instruction>(Op)->getFunction();
5787if (!F)
5788break;
5789
5790if ((KnownRHS.isKnownNeverInfinity() ||
5791 KnownLHS.isKnownNeverLogicalZero(*F,Op->getType())) &&
5792 (KnownLHS.isKnownNeverInfinity() ||
5793 KnownRHS.isKnownNeverLogicalZero(*F,Op->getType())))
5794 Known.knownNot(fcNan);
5795
5796break;
5797 }
5798case Instruction::FDiv:
5799case Instruction::FRem: {
5800if (Op->getOperand(0) ==Op->getOperand(1)) {
5801// TODO: Could filter out snan if we inspect the operand
5802if (Op->getOpcode() == Instruction::FDiv) {
5803// X / X is always exactly 1.0 or a NaN.
5804 Known.KnownFPClasses =fcNan |fcPosNormal;
5805 }else {
5806// X % X is always exactly [+-]0.0 or a NaN.
5807 Known.KnownFPClasses =fcNan |fcZero;
5808 }
5809
5810break;
5811 }
5812
5813constbool WantNan = (InterestedClasses &fcNan) !=fcNone;
5814constbool WantNegative = (InterestedClasses &fcNegative) !=fcNone;
5815constbool WantPositive =
5816 Opc == Instruction::FRem && (InterestedClasses &fcPositive) !=fcNone;
5817if (!WantNan && !WantNegative && !WantPositive)
5818break;
5819
5820KnownFPClass KnownLHS, KnownRHS;
5821
5822computeKnownFPClass(Op->getOperand(1), DemandedElts,
5823fcNan |fcInf |fcZero |fcNegative, KnownRHS,
5824Depth + 1, Q);
5825
5826bool KnowSomethingUseful =
5827 KnownRHS.isKnownNeverNaN() || KnownRHS.isKnownNever(fcNegative);
5828
5829if (KnowSomethingUseful || WantPositive) {
5830constFPClassTest InterestedLHS =
5831 WantPositive ?fcAllFlags
5832 :fcNan |fcInf |fcZero |fcSubnormal |fcNegative;
5833
5834computeKnownFPClass(Op->getOperand(0), DemandedElts,
5835 InterestedClasses & InterestedLHS, KnownLHS,
5836Depth + 1, Q);
5837 }
5838
5839constFunction *F = cast<Instruction>(Op)->getFunction();
5840
5841if (Op->getOpcode() == Instruction::FDiv) {
5842// Only 0/0, Inf/Inf produce NaN.
5843if (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
5853if (KnownLHS.isKnownNever(fcNegative) && KnownRHS.isKnownNever(fcNegative))
5854 Known.knownNot(fcNegative);
5855 }else {
5856// Inf REM x and x REM 0 produce NaN.
5857if (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.
5864if (KnownLHS.cannotBeOrderedLessThanZero())
5865 Known.knownNot(KnownFPClass::OrderedLessThanZeroMask);
5866if (KnownLHS.cannotBeOrderedGreaterThanZero())
5867 Known.knownNot(KnownFPClass::OrderedGreaterThanZeroMask);
5868
5869// See if we can be more aggressive about the sign of 0.
5870if (KnownLHS.isKnownNever(fcNegative))
5871 Known.knownNot(fcNegative);
5872if (KnownLHS.isKnownNever(fcPositive))
5873 Known.knownNot(fcPositive);
5874 }
5875
5876break;
5877 }
5878case Instruction::FPExt: {
5879// Infinity, nan and zero propagate from source.
5880computeKnownFPClass(Op->getOperand(0), DemandedElts, InterestedClasses,
5881 Known,Depth + 1, Q);
5882
5883constfltSemantics &DstTy =
5884Op->getType()->getScalarType()->getFltSemantics();
5885constfltSemantics &SrcTy =
5886Op->getOperand(0)->getType()->getScalarType()->getFltSemantics();
5887
5888// All subnormal inputs should be in the normal range in the result type.
5889if (APFloat::isRepresentableAsNormalIn(SrcTy, DstTy)) {
5890if (Known.KnownFPClasses &fcPosSubnormal)
5891 Known.KnownFPClasses |=fcPosNormal;
5892if (Known.KnownFPClasses &fcNegSubnormal)
5893 Known.KnownFPClasses |=fcNegNormal;
5894 Known.knownNot(fcSubnormal);
5895 }
5896
5897// Sign bit of a nan isn't guaranteed.
5898if (!Known.isKnownNeverNaN())
5899 Known.SignBit = std::nullopt;
5900break;
5901 }
5902case Instruction::FPTrunc: {
5903computeKnownFPClassForFPTrunc(Op, DemandedElts, InterestedClasses, Known,
5904Depth, Q);
5905break;
5906 }
5907case Instruction::SIToFP:
5908case 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);
5917if (Op->getOpcode() == Instruction::UIToFP)
5918 Known.signBitMustBeZero();
5919
5920if (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).
5924int IntSize =Op->getOperand(0)->getType()->getScalarSizeInBits();
5925if (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.
5930Type *FPTy =Op->getType()->getScalarType();
5931if (ilogb(APFloat::getLargest(FPTy->getFltSemantics())) >= IntSize)
5932 Known.knownNot(fcInf);
5933 }
5934
5935break;
5936 }
5937case 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.
5940constValue *Vec =Op->getOperand(0);
5941constValue *Idx =Op->getOperand(1);
5942auto *CIdx = dyn_cast<ConstantInt>(Idx);
5943
5944if (auto *VecTy = dyn_cast<FixedVectorType>(Vec->getType())) {
5945unsigned NumElts = VecTy->getNumElements();
5946APInt DemandedVecElts =APInt::getAllOnes(NumElts);
5947if (CIdx && CIdx->getValue().ult(NumElts))
5948 DemandedVecElts =APInt::getOneBitSet(NumElts, CIdx->getZExtValue());
5949returncomputeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known,
5950Depth + 1, Q);
5951 }
5952
5953break;
5954 }
5955case Instruction::InsertElement: {
5956if (isa<ScalableVectorType>(Op->getType()))
5957return;
5958
5959constValue *Vec =Op->getOperand(0);
5960constValue *Elt =Op->getOperand(1);
5961auto *CIdx = dyn_cast<ConstantInt>(Op->getOperand(2));
5962unsigned NumElts = DemandedElts.getBitWidth();
5963APInt DemandedVecElts = DemandedElts;
5964bool NeedsElt =true;
5965// If we know the index we are inserting to, clear it from Vec check.
5966if (CIdx && CIdx->getValue().ult(NumElts)) {
5967 DemandedVecElts.clearBit(CIdx->getZExtValue());
5968 NeedsElt = DemandedElts[CIdx->getZExtValue()];
5969 }
5970
5971// Do we demand the inserted element?
5972if (NeedsElt) {
5973computeKnownFPClass(Elt, Known, InterestedClasses,Depth + 1, Q);
5974// If we don't know any bits, early out.
5975if (Known.isUnknown())
5976break;
5977 }else {
5978 Known.KnownFPClasses =fcNone;
5979 }
5980
5981// Do we need anymore elements from Vec?
5982if (!DemandedVecElts.isZero()) {
5983KnownFPClass Known2;
5984computeKnownFPClass(Vec, DemandedVecElts, InterestedClasses, Known2,
5985Depth + 1, Q);
5986 Known |= Known2;
5987 }
5988
5989break;
5990 }
5991case Instruction::ShuffleVector: {
5992// For undef elements, we don't know anything about the common state of
5993// the shuffle result.
5994APInt DemandedLHS, DemandedRHS;
5995auto *Shuf = dyn_cast<ShuffleVectorInst>(Op);
5996if (!Shuf || !getShuffleDemandedElts(Shuf, DemandedElts, DemandedLHS, DemandedRHS))
5997return;
5998
5999if (!!DemandedLHS) {
6000constValue *LHS = Shuf->getOperand(0);
6001computeKnownFPClass(LHS, DemandedLHS, InterestedClasses, Known,
6002Depth + 1, Q);
6003
6004// If we don't know any bits, early out.
6005if (Known.isUnknown())
6006break;
6007 }else {
6008 Known.KnownFPClasses =fcNone;
6009 }
6010
6011if (!!DemandedRHS) {
6012KnownFPClass Known2;
6013constValue *RHS = Shuf->getOperand(1);
6014computeKnownFPClass(RHS, DemandedRHS, InterestedClasses, Known2,
6015Depth + 1, Q);
6016 Known |= Known2;
6017 }
6018
6019break;
6020 }
6021case Instruction::ExtractValue: {
6022constExtractValueInst *Extract = cast<ExtractValueInst>(Op);
6023ArrayRef<unsigned> Indices = Extract->getIndices();
6024constValue *Src = Extract->getAggregateOperand();
6025if (isa<StructType>(Src->getType()) && Indices.size() == 1 &&
6026 Indices[0] == 0) {
6027if (constauto *II = dyn_cast<IntrinsicInst>(Src)) {
6028switch (II->getIntrinsicID()) {
6029case Intrinsic::frexp: {
6030 Known.knownNot(fcSubnormal);
6031
6032KnownFPClass KnownSrc;
6033computeKnownFPClass(II->getArgOperand(0), DemandedElts,
6034 InterestedClasses, KnownSrc,Depth + 1, Q);
6035
6036constFunction *F = cast<Instruction>(Op)->getFunction();
6037
6038if (KnownSrc.isKnownNever(fcNegative))
6039 Known.knownNot(fcNegative);
6040else {
6041if (F && KnownSrc.isKnownNeverLogicalNegZero(*F,Op->getType()))
6042 Known.knownNot(fcNegZero);
6043if (KnownSrc.isKnownNever(fcNegInf))
6044 Known.knownNot(fcNegInf);
6045 }
6046
6047if (KnownSrc.isKnownNever(fcPositive))
6048 Known.knownNot(fcPositive);
6049else {
6050if (F && KnownSrc.isKnownNeverLogicalPosZero(*F,Op->getType()))
6051 Known.knownNot(fcPosZero);
6052if (KnownSrc.isKnownNever(fcPosInf))
6053 Known.knownNot(fcPosInf);
6054 }
6055
6056 Known.propagateNaN(KnownSrc);
6057return;
6058 }
6059default:
6060break;
6061 }
6062 }
6063 }
6064
6065computeKnownFPClass(Src, DemandedElts, InterestedClasses, Known,Depth + 1,
6066 Q);
6067break;
6068 }
6069case Instruction::PHI: {
6070constPHINode *P = cast<PHINode>(Op);
6071// Unreachable blocks may have zero-operand PHI nodes.
6072if (P->getNumIncomingValues() == 0)
6073break;
6074
6075// Otherwise take the unions of the known bit sets of the operands,
6076// taking conservative care to avoid excessive recursion.
6077constunsigned PhiRecursionLimit =MaxAnalysisRecursionDepth - 2;
6078
6079if (Depth < PhiRecursionLimit) {
6080// Skip if every incoming value references to ourself.
6081if (isa_and_nonnull<UndefValue>(P->hasConstantValue()))
6082break;
6083
6084boolFirst =true;
6085
6086for (constUse &U :P->operands()) {
6087Value *IncValue;
6088Instruction *CxtI;
6089breakSelfRecursivePHI(&U,P, IncValue, CxtI);
6090// Skip direct self references.
6091if (IncValue ==P)
6092continue;
6093
6094KnownFPClass 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.
6098computeKnownFPClass(IncValue, DemandedElts, InterestedClasses, KnownSrc,
6099 PhiRecursionLimit,
6100 Q.getWithoutCondContext().getWithInstruction(CxtI));
6101
6102if (First) {
6103 Known = KnownSrc;
6104First =false;
6105 }else {
6106 Known |= KnownSrc;
6107 }
6108
6109if (Known.KnownFPClasses ==fcAllFlags)
6110break;
6111 }
6112 }
6113
6114break;
6115 }
6116case Instruction::BitCast: {
6117constValue *Src;
6118if (!match(Op,m_ElementWiseBitCast(m_Value(Src))) ||
6119 !Src->getType()->isIntOrIntVectorTy())
6120break;
6121
6122constType *Ty =Op->getType()->getScalarType();
6123KnownBits Bits(Ty->getScalarSizeInBits());
6124computeKnownBits(Src, DemandedElts, Bits,Depth + 1, Q);
6125
6126// Transfer information from the sign bit.
6127if (Bits.isNonNegative())
6128 Known.signBitMustBeZero();
6129elseif (Bits.isNegative())
6130 Known.signBitMustBeOne();
6131
6132if (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
6139if (APFloat(Ty->getFltSemantics(), Bits.One).isNaN())
6140 Known.KnownFPClasses =fcNan;
6141elseif (!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.
6146auto InfKB =KnownBits::makeConstant(
6147APFloat::getInf(Ty->getFltSemantics()).bitcastToAPInt());
6148 InfKB.Zero.clearSignBit();
6149if (constauto InfResult =KnownBits::eq(Bits, InfKB)) {
6150assert(!InfResult.value());
6151 Known.knownNot(fcInf);
6152 }elseif (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.
6158auto ZeroKB =KnownBits::makeConstant(
6159APFloat::getZero(Ty->getFltSemantics()).bitcastToAPInt());
6160 ZeroKB.Zero.clearSignBit();
6161if (constauto ZeroResult =KnownBits::eq(Bits, ZeroKB)) {
6162assert(!ZeroResult.value());
6163 Known.knownNot(fcZero);
6164 }elseif (Bits == ZeroKB) {
6165 Known.KnownFPClasses =fcZero;
6166 }
6167 }
6168
6169break;
6170 }
6171default:
6172break;
6173 }
6174}
6175
6176KnownFPClassllvm::computeKnownFPClass(constValue *V,
6177constAPInt &DemandedElts,
6178FPClassTest InterestedClasses,
6179unsignedDepth,
6180constSimplifyQuery &SQ) {
6181KnownFPClass KnownClasses;
6182::computeKnownFPClass(V, DemandedElts, InterestedClasses, KnownClasses,Depth,
6183 SQ);
6184return KnownClasses;
6185}
6186
6187KnownFPClassllvm::computeKnownFPClass(constValue *V,
6188FPClassTest InterestedClasses,
6189unsignedDepth,
6190constSimplifyQuery &SQ) {
6191KnownFPClass Known;
6192::computeKnownFPClass(V, Known, InterestedClasses,Depth, SQ);
6193return Known;
6194}
6195
6196Value *llvm::isBytewiseValue(Value *V,constDataLayout &DL) {
6197
6198// All byte-wide stores are splatable, even of arbitrary variables.
6199if (V->getType()->isIntegerTy(8))
6200return V;
6201
6202LLVMContext &Ctx = V->getContext();
6203
6204// Undef don't care.
6205auto *UndefInt8 =UndefValue::get(Type::getInt8Ty(Ctx));
6206if (isa<UndefValue>(V))
6207return UndefInt8;
6208
6209// Return poison for zero-sized type.
6210if (DL.getTypeStoreSize(V->getType()).isZero())
6211returnPoisonValue::get(Type::getInt8Ty(Ctx));
6212
6213Constant *C = dyn_cast<Constant>(V);
6214if (!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.
6221returnnullptr;
6222 }
6223
6224// Handle 'null' ConstantArrayZero etc.
6225if (C->isNullValue())
6226returnConstant::getNullValue(Type::getInt8Ty(Ctx));
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.
6230if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
6231Type *Ty =nullptr;
6232if (CFP->getType()->isHalfTy())
6233 Ty =Type::getInt16Ty(Ctx);
6234elseif (CFP->getType()->isFloatTy())
6235 Ty =Type::getInt32Ty(Ctx);
6236elseif (CFP->getType()->isDoubleTy())
6237 Ty =Type::getInt64Ty(Ctx);
6238// Don't handle long double formats, which have strange constraints.
6239return Ty ?isBytewiseValue(ConstantExpr::getBitCast(CFP, Ty),DL)
6240 :nullptr;
6241 }
6242
6243// We can handle constant integers that are multiple of 8 bits.
6244if (ConstantInt *CI = dyn_cast<ConstantInt>(C)) {
6245if (CI->getBitWidth() % 8 == 0) {
6246assert(CI->getBitWidth() > 8 &&"8 bits should be handled above!");
6247if (!CI->getValue().isSplat(8))
6248returnnullptr;
6249return ConstantInt::get(Ctx, CI->getValue().trunc(8));
6250 }
6251 }
6252
6253if (auto *CE = dyn_cast<ConstantExpr>(C)) {
6254if (CE->getOpcode() == Instruction::IntToPtr) {
6255if (auto *PtrTy = dyn_cast<PointerType>(CE->getType())) {
6256unsignedBitWidth =DL.getPointerSizeInBits(PtrTy->getAddressSpace());
6257if (Constant *Op =ConstantFoldIntegerCast(
6258 CE->getOperand(0),Type::getIntNTy(Ctx,BitWidth),false,DL))
6259returnisBytewiseValue(Op,DL);
6260 }
6261 }
6262 }
6263
6264autoMerge = [&](Value *LHS,Value *RHS) ->Value * {
6265if (LHS ==RHS)
6266returnLHS;
6267if (!LHS || !RHS)
6268returnnullptr;
6269if (LHS == UndefInt8)
6270returnRHS;
6271if (RHS == UndefInt8)
6272returnLHS;
6273returnnullptr;
6274 };
6275
6276if (ConstantDataSequential *CA = dyn_cast<ConstantDataSequential>(C)) {
6277Value *Val = UndefInt8;
6278for (unsignedI = 0, E = CA->getNumElements();I != E; ++I)
6279if (!(Val =Merge(Val,isBytewiseValue(CA->getElementAsConstant(I),DL))))
6280returnnullptr;
6281return Val;
6282 }
6283
6284if (isa<ConstantAggregate>(C)) {
6285Value *Val = UndefInt8;
6286for (Value *Op :C->operands())
6287if (!(Val =Merge(Val,isBytewiseValue(Op,DL))))
6288returnnullptr;
6289return Val;
6290 }
6291
6292// Don't try to handle the handful of other constants.
6293returnnullptr;
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.
6302staticValue *BuildSubAggregate(Value *From,Value *To,Type *IndexedType,
6303SmallVectorImpl<unsigned> &Idxs,
6304unsigned IdxSkip,
6305BasicBlock::iterator InsertBefore) {
6306StructType *STy = dyn_cast<StructType>(IndexedType);
6307if (STy) {
6308// Save the original To argument so we can modify it
6309Value *OrigTo = To;
6310// General case, the type indexed by Idxs is a struct
6311for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
6312// Process each struct element recursively
6313 Idxs.push_back(i);
6314Value *PrevTo = To;
6315 To =BuildSubAggregate(From, To, STy->getElementType(i), Idxs, IdxSkip,
6316 InsertBefore);
6317 Idxs.pop_back();
6318if (!To) {
6319// Couldn't find any inserted value for this index? Cleanup
6320while (PrevTo != OrigTo) {
6321InsertValueInst* Del = cast<InsertValueInst>(PrevTo);
6322 PrevTo = Del->getAggregateOperand();
6323 Del->eraseFromParent();
6324 }
6325// Stop processing elements
6326break;
6327 }
6328 }
6329// If we successfully found a value for each of our subaggregates
6330if (To)
6331return 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
6339Value *V =FindInsertedValue(From, Idxs);
6340
6341if (!V)
6342returnnullptr;
6343
6344// Insert the value in the new (sub) aggregate
6345returnInsertValueInst::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
6361staticValue *BuildSubAggregate(Value *From,ArrayRef<unsigned> idx_range,
6362BasicBlock::iterator InsertBefore) {
6363Type *IndexedType =ExtractValueInst::getIndexedType(From->getType(),
6364 idx_range);
6365Value *To =PoisonValue::get(IndexedType);
6366SmallVector<unsigned, 10> Idxs(idx_range);
6367unsigned IdxSkip = Idxs.size();
6368
6369returnBuildSubAggregate(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 *
6379llvm::FindInsertedValue(Value *V,ArrayRef<unsigned> idx_range,
6380 std::optional<BasicBlock::iterator> InsertBefore) {
6381// Nothing to index? Just return V then (this is useful at the end of our
6382// recursion).
6383if (idx_range.empty())
6384return V;
6385// We have indices, so V should have an indexable type.
6386assert((V->getType()->isStructTy() || V->getType()->isArrayTy()) &&
6387"Not looking at a struct or array?");
6388assert(ExtractValueInst::getIndexedType(V->getType(), idx_range) &&
6389"Invalid indices for type?");
6390
6391if (Constant *C = dyn_cast<Constant>(V)) {
6392C =C->getAggregateElement(idx_range[0]);
6393if (!C)returnnullptr;
6394returnFindInsertedValue(C, idx_range.slice(1), InsertBefore);
6395 }
6396
6397if (InsertValueInst *I = dyn_cast<InsertValueInst>(V)) {
6398// Loop the indices for the insertvalue instruction in parallel with the
6399// requested indices
6400constunsigned *req_idx = idx_range.begin();
6401for (constunsigned *i =I->idx_begin(), *e =I->idx_end();
6402 i != e; ++i, ++req_idx) {
6403if (req_idx == idx_range.end()) {
6404// We can't handle this without inserting insertvalues
6405if (!InsertBefore)
6406returnnullptr;
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.
6418returnBuildSubAggregate(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.
6425if (*req_idx != *i)
6426returnFindInsertedValue(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.
6432returnFindInsertedValue(I->getInsertedValueOperand(),
6433ArrayRef(req_idx, idx_range.end()), InsertBefore);
6434 }
6435
6436if (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
6442unsignedsize =I->getNumIndices() + idx_range.size();
6443// Allocate some space to put the new indices in
6444SmallVector<unsigned, 5> Idxs;
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
6452assert(Idxs.size() ==size
6453 &&"Number of indices added not correct?");
6454
6455returnFindInsertedValue(I->getAggregateOperand(), Idxs, InsertBefore);
6456 }
6457// Otherwise, we don't know (such as, extracting from a function return value
6458// or load instruction)
6459returnnullptr;
6460}
6461
6462boolllvm::isGEPBasedOnPointerToString(constGEPOperator *GEP,
6463unsigned CharSize) {
6464// Make sure the GEP has exactly three arguments.
6465if (GEP->getNumOperands() != 3)
6466returnfalse;
6467
6468// Make sure the index-ee is a pointer to array of \p CharSize integers.
6469// CharSize.
6470ArrayType *AT = dyn_cast<ArrayType>(GEP->getSourceElementType());
6471if (!AT || !AT->getElementType()->isIntegerTy(CharSize))
6472returnfalse;
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.
6476constConstantInt *FirstIdx = dyn_cast<ConstantInt>(GEP->getOperand(1));
6477if (!FirstIdx || !FirstIdx->isZero())
6478returnfalse;
6479
6480returntrue;
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".
6488boolllvm::getConstantDataArrayInfo(constValue *V,
6489ConstantDataArraySlice &Slice,
6490unsigned ElementSize,uint64_tOffset) {
6491assert(V &&"V should not be null.");
6492assert((ElementSize % 8) == 0 &&
6493"ElementSize expected to be a multiple of the size of a byte.");
6494unsigned 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.
6499constGlobalVariable *GV =
6500 dyn_cast<GlobalVariable>(getUnderlyingObject(V));
6501if (!GV || !GV->isConstant() || !GV->hasDefinitiveInitializer())
6502// Fail if V is not based on constant global object.
6503returnfalse;
6504
6505constDataLayout &DL = GV->getDataLayout();
6506APInt Off(DL.getIndexTypeSizeInBits(V->getType()), 0);
6507
6508if (GV != V->stripAndAccumulateConstantOffsets(DL, Off,
6509/*AllowNonInbounds*/true))
6510// Fail if a constant offset could not be determined.
6511returnfalse;
6512
6513uint64_t StartIdx = Off.getLimitedValue();
6514if (StartIdx ==UINT64_MAX)
6515// Fail if the constant offset is excessive.
6516returnfalse;
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.
6520if ((StartIdx % ElementSizeInBytes) != 0)
6521returnfalse;
6522
6523Offset += StartIdx / ElementSizeInBytes;
6524ConstantDataArray *Array =nullptr;
6525ArrayType *ArrayTy =nullptr;
6526
6527if (GV->getInitializer()->isNullValue()) {
6528Type *GVTy = GV->getValueType();
6529uint64_t SizeInBytes =DL.getTypeStoreSize(GVTy).getFixedValue();
6530uint64_tLength = 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;
6539returntrue;
6540 }
6541
6542auto *Init =const_cast<Constant *>(GV->getInitializer());
6543if (auto *ArrayInit = dyn_cast<ConstantDataArray>(Init)) {
6544Type *InitElTy = ArrayInit->getElementType();
6545if (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
6553if (!Array) {
6554if (ElementSize != 8)
6555// TODO: Handle conversions to larger integral types.
6556returnfalse;
6557
6558// Otherwise extract the portion of the initializer starting
6559// at Offset as an array of bytes, and reset Offset.
6560Init =ReadByteArrayFromGlobal(GV,Offset);
6561if (!Init)
6562returnfalse;
6563
6564Offset = 0;
6565 Array = dyn_cast<ConstantDataArray>(Init);
6566 ArrayTy = dyn_cast<ArrayType>(Init->getType());
6567 }
6568
6569uint64_t NumElts = ArrayTy->getArrayNumElements();
6570if (Offset > NumElts)
6571returnfalse;
6572
6573 Slice.Array = Array;
6574 Slice.Offset =Offset;
6575 Slice.Length = NumElts -Offset;
6576returntrue;
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.
6583boolllvm::getConstantStringInfo(constValue *V,StringRef &Str,
6584bool TrimAtNul) {
6585ConstantDataArraySlice Slice;
6586if (!getConstantDataArrayInfo(V, Slice, 8))
6587returnfalse;
6588
6589if (Slice.Array ==nullptr) {
6590if (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();
6598returntrue;
6599 }
6600if (Slice.Length == 1) {
6601 Str =StringRef("", 1);
6602returntrue;
6603 }
6604// We cannot instantiate a StringRef as we do not have an appropriate string
6605// of 0s at hand.
6606returnfalse;
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
6614if (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 }
6620returntrue;
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.
6629staticuint64_tGetStringLengthH(constValue *V,
6630SmallPtrSetImpl<const PHINode*> &PHIs,
6631unsigned 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.
6637if (constPHINode *PN = dyn_cast<PHINode>(V)) {
6638if (!PHIs.insert(PN).second)
6639return ~0ULL;// already in the set.
6640
6641// If it was new, see if all the input strings are the same length.
6642uint64_t LenSoFar = ~0ULL;
6643for (Value *IncValue : PN->incoming_values()) {
6644uint64_t Len =GetStringLengthH(IncValue, PHIs, CharSize);
6645if (Len == 0)return 0;// Unknown length -> unknown.
6646
6647if (Len == ~0ULL)continue;
6648
6649if (Len != LenSoFar && LenSoFar != ~0ULL)
6650return 0;// Disagree -> unknown.
6651 LenSoFar = Len;
6652 }
6653
6654// Success, all agree.
6655return LenSoFar;
6656 }
6657
6658// strlen(select(c,x,y)) -> strlen(x) ^ strlen(y)
6659if (constSelectInst *SI = dyn_cast<SelectInst>(V)) {
6660uint64_t Len1 =GetStringLengthH(SI->getTrueValue(), PHIs, CharSize);
6661if (Len1 == 0)return 0;
6662uint64_t Len2 =GetStringLengthH(SI->getFalseValue(), PHIs, CharSize);
6663if (Len2 == 0)return 0;
6664if (Len1 == ~0ULL)return Len2;
6665if (Len2 == ~0ULL)return Len1;
6666if (Len1 != Len2)return 0;
6667return Len1;
6668 }
6669
6670// Otherwise, see if we can read the string.
6671ConstantDataArraySlice Slice;
6672if (!getConstantDataArrayInfo(V, Slice, CharSize))
6673return 0;
6674
6675if (Slice.Array ==nullptr)
6676// Zeroinitializer (including an empty one).
6677return 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.
6683unsigned NullIndex = 0;
6684for (unsigned E = Slice.Length; NullIndex < E; ++NullIndex) {
6685if (Slice.Array->getElementAsInteger(Slice.Offset + NullIndex) == 0)
6686break;
6687 }
6688
6689return 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_tllvm::GetStringLength(constValue *V,unsigned CharSize) {
6695if (!V->getType()->isPointerTy())
6696return 0;
6697
6698SmallPtrSet<const PHINode*, 32> PHIs;
6699uint64_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.
6702return Len == ~0ULL ? 1 : Len;
6703}
6704
6705constValue *
6706llvm::getArgumentAliasingToReturnedPointer(constCallBase *Call,
6707bool MustPreserveNullness) {
6708assert(Call &&
6709"getArgumentAliasingToReturnedPointer only works on nonnull calls");
6710if (constValue *RV = Call->getReturnedArgOperand())
6711return RV;
6712// This can be used only as a aliasing property.
6713if (isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
6714 Call, MustPreserveNullness))
6715return Call->getArgOperand(0);
6716returnnullptr;
6717}
6718
6719boolllvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(
6720constCallBase *Call,bool MustPreserveNullness) {
6721switch (Call->getIntrinsicID()) {
6722case Intrinsic::launder_invariant_group:
6723case Intrinsic::strip_invariant_group:
6724case Intrinsic::aarch64_irg:
6725case 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.
6735case Intrinsic::amdgcn_make_buffer_rsrc:
6736returntrue;
6737case Intrinsic::ptrmask:
6738return !MustPreserveNullness;
6739case Intrinsic::threadlocal_address:
6740// The underlying variable changes with thread ID. The Thread ID may change
6741// at coroutine suspend points.
6742return !Call->getParent()->getParent()->isPresplitCoroutine();
6743default:
6744returnfalse;
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.
6750staticboolisSameUnderlyingObjectInLoop(constPHINode *PN,
6751constLoopInfo *LI) {
6752// Find the loop-defined value.
6753Loop *L = LI->getLoopFor(PN->getParent());
6754if (PN->getNumIncomingValues() != 2)
6755returntrue;
6756
6757// Find the value from previous iteration.
6758auto *PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(0));
6759if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6760 PrevValue = dyn_cast<Instruction>(PN->getIncomingValue(1));
6761if (!PrevValue || LI->getLoopFor(PrevValue->getParent()) != L)
6762returntrue;
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// ...
6769if (auto *Load = dyn_cast<LoadInst>(PrevValue))
6770if (!L->isLoopInvariant(Load->getPointerOperand()))
6771returnfalse;
6772returntrue;
6773}
6774
6775constValue *llvm::getUnderlyingObject(constValue *V,unsigned MaxLookup) {
6776for (unsigned Count = 0; MaxLookup == 0 || Count < MaxLookup; ++Count) {
6777if (auto *GEP = dyn_cast<GEPOperator>(V)) {
6778constValue *PtrOp =GEP->getPointerOperand();
6779if (!PtrOp->getType()->isPointerTy())// Only handle scalar pointer base.
6780return V;
6781 V = PtrOp;
6782 }elseif (Operator::getOpcode(V) == Instruction::BitCast ||
6783Operator::getOpcode(V) == Instruction::AddrSpaceCast) {
6784Value *NewV = cast<Operator>(V)->getOperand(0);
6785if (!NewV->getType()->isPointerTy())
6786return V;
6787 V = NewV;
6788 }elseif (auto *GA = dyn_cast<GlobalAlias>(V)) {
6789if (GA->isInterposable())
6790return V;
6791 V = GA->getAliasee();
6792 }else {
6793if (auto *PHI = dyn_cast<PHINode>(V)) {
6794// Look through single-arg phi nodes created by LCSSA.
6795if (PHI->getNumIncomingValues() == 1) {
6796 V =PHI->getIncomingValue(0);
6797continue;
6798 }
6799 }elseif (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.
6809if (auto *RP =getArgumentAliasingToReturnedPointer(Call,false)) {
6810 V = RP;
6811continue;
6812 }
6813 }
6814
6815return V;
6816 }
6817assert(V->getType()->isPointerTy() &&"Unexpected operand type!");
6818 }
6819return V;
6820}
6821
6822voidllvm::getUnderlyingObjects(constValue *V,
6823SmallVectorImpl<const Value *> &Objects,
6824constLoopInfo *LI,unsigned MaxLookup) {
6825SmallPtrSet<const Value *, 4> Visited;
6826SmallVector<const Value *, 4> Worklist;
6827 Worklist.push_back(V);
6828do {
6829constValue *P = Worklist.pop_back_val();
6830P =getUnderlyingObject(P, MaxLookup);
6831
6832if (!Visited.insert(P).second)
6833continue;
6834
6835if (auto *SI = dyn_cast<SelectInst>(P)) {
6836 Worklist.push_back(SI->getTrueValue());
6837 Worklist.push_back(SI->getFalseValue());
6838continue;
6839 }
6840
6841if (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.
6852if (!LI || !LI->isLoopHeader(PN->getParent()) ||
6853isSameUnderlyingObjectInLoop(PN, LI))
6854append_range(Worklist, PN->incoming_values());
6855else
6856 Objects.push_back(P);
6857continue;
6858 }
6859
6860 Objects.push_back(P);
6861 }while (!Worklist.empty());
6862}
6863
6864constValue *llvm::getUnderlyingObjectAggressive(constValue *V) {
6865constunsigned MaxVisited = 8;
6866
6867SmallPtrSet<const Value *, 8> Visited;
6868SmallVector<const Value *, 8> Worklist;
6869 Worklist.push_back(V);
6870constValue *Object =nullptr;
6871// Used as fallback if we can't find a common underlying object through
6872// recursion.
6873boolFirst =true;
6874constValue *FirstObject =getUnderlyingObject(V);
6875do {
6876constValue *P = Worklist.pop_back_val();
6877P =First ? FirstObject :getUnderlyingObject(P);
6878First =false;
6879
6880if (!Visited.insert(P).second)
6881continue;
6882
6883if (Visited.size() == MaxVisited)
6884return FirstObject;
6885
6886if (auto *SI = dyn_cast<SelectInst>(P)) {
6887 Worklist.push_back(SI->getTrueValue());
6888 Worklist.push_back(SI->getFalseValue());
6889continue;
6890 }
6891
6892if (auto *PN = dyn_cast<PHINode>(P)) {
6893append_range(Worklist, PN->incoming_values());
6894continue;
6895 }
6896
6897if (!Object)
6898 Object =P;
6899elseif (Object !=P)
6900return FirstObject;
6901 }while (!Worklist.empty());
6902
6903return Object ? Object : FirstObject;
6904}
6905
6906/// This is the function that does the work of looking through basic
6907/// ptrtoint+arithmetic+inttoptr sequences.
6908staticconstValue *getUnderlyingObjectFromInt(constValue *V) {
6909do {
6910if (constOperator *U = dyn_cast<Operator>(V)) {
6911// If we find a ptrtoint, we can transfer control back to the
6912// regular getUnderlyingObjectFromInt.
6913if (U->getOpcode() == Instruction::PtrToInt)
6914return 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.
6921if (U->getOpcode() != Instruction::Add ||
6922 (!isa<ConstantInt>(U->getOperand(1)) &&
6923Operator::getOpcode(U->getOperand(1)) != Instruction::Mul &&
6924 !isa<PHINode>(U->getOperand(1))))
6925return V;
6926 V = U->getOperand(0);
6927 }else {
6928return V;
6929 }
6930assert(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.
6937boolllvm::getUnderlyingObjectsForCodeGen(constValue *V,
6938SmallVectorImpl<Value *> &Objects) {
6939SmallPtrSet<const Value *, 16> Visited;
6940SmallVector<const Value *, 4> Working(1, V);
6941do {
6942 V = Working.pop_back_val();
6943
6944SmallVector<const Value *, 4> Objs;
6945getUnderlyingObjects(V, Objs);
6946
6947for (constValue *V : Objs) {
6948if (!Visited.insert(V).second)
6949continue;
6950if (Operator::getOpcode(V) == Instruction::IntToPtr) {
6951constValue *O =
6952getUnderlyingObjectFromInt(cast<User>(V)->getOperand(0));
6953if (O->getType()->isPointerTy()) {
6954 Working.push_back(O);
6955continue;
6956 }
6957 }
6958// If getUnderlyingObjects fails to find an identifiable object,
6959// getUnderlyingObjectsForCodeGen also fails for safety.
6960if (!isIdentifiedObject(V)) {
6961 Objects.clear();
6962returnfalse;
6963 }
6964 Objects.push_back(const_cast<Value *>(V));
6965 }
6966 }while (!Working.empty());
6967returntrue;
6968}
6969
6970AllocaInst *llvm::findAllocaForValue(Value *V,bool OffsetZero) {
6971AllocaInst *Result =nullptr;
6972SmallPtrSet<Value *, 4> Visited;
6973SmallVector<Value *, 4> Worklist;
6974
6975auto AddWork = [&](Value *V) {
6976if (Visited.insert(V).second)
6977 Worklist.push_back(V);
6978 };
6979
6980 AddWork(V);
6981do {
6982 V = Worklist.pop_back_val();
6983assert(Visited.count(V));
6984
6985if (AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
6986if (Result && Result != AI)
6987returnnullptr;
6988 Result = AI;
6989 }elseif (CastInst *CI = dyn_cast<CastInst>(V)) {
6990 AddWork(CI->getOperand(0));
6991 }elseif (PHINode *PN = dyn_cast<PHINode>(V)) {
6992for (Value *IncValue : PN->incoming_values())
6993 AddWork(IncValue);
6994 }elseif (auto *SI = dyn_cast<SelectInst>(V)) {
6995 AddWork(SI->getTrueValue());
6996 AddWork(SI->getFalseValue());
6997 }elseif (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(V)) {
6998if (OffsetZero && !GEP->hasAllZeroIndices())
6999returnnullptr;
7000 AddWork(GEP->getPointerOperand());
7001 }elseif (CallBase *CB = dyn_cast<CallBase>(V)) {
7002Value *Returned = CB->getReturnedArgOperand();
7003if (Returned)
7004 AddWork(Returned);
7005else
7006returnnullptr;
7007 }else {
7008returnnullptr;
7009 }
7010 }while (!Worklist.empty());
7011
7012return Result;
7013}
7014
7015staticboolonlyUsedByLifetimeMarkersOrDroppableInstsHelper(
7016constValue *V,bool AllowLifetime,bool AllowDroppable) {
7017for (constUser *U : V->users()) {
7018constIntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
7019if (!II)
7020returnfalse;
7021
7022if (AllowLifetime &&II->isLifetimeStartOrEnd())
7023continue;
7024
7025if (AllowDroppable &&II->isDroppable())
7026continue;
7027
7028returnfalse;
7029 }
7030returntrue;
7031}
7032
7033boolllvm::onlyUsedByLifetimeMarkers(constValue *V) {
7034returnonlyUsedByLifetimeMarkersOrDroppableInstsHelper(
7035 V,/* AllowLifetime */true,/* AllowDroppable */false);
7036}
7037boolllvm::onlyUsedByLifetimeMarkersOrDroppableInsts(constValue *V) {
7038returnonlyUsedByLifetimeMarkersOrDroppableInstsHelper(
7039 V,/* AllowLifetime */true,/* AllowDroppable */true);
7040}
7041
7042boolllvm::isNotCrossLaneOperation(constInstruction *I) {
7043if (auto *II = dyn_cast<IntrinsicInst>(I))
7044returnisTriviallyVectorizable(II->getIntrinsicID());
7045auto *Shuffle = dyn_cast<ShuffleVectorInst>(I);
7046return (!Shuffle || Shuffle->isSelect()) &&
7047 !isa<CallBase, BitCastInst, ExtractElementInst>(I);
7048}
7049
7050boolllvm::isSafeToSpeculativelyExecute(constInstruction *Inst,
7051constInstruction *CtxI,
7052AssumptionCache *AC,
7053constDominatorTree *DT,
7054constTargetLibraryInfo *TLI,
7055bool UseVariableInfo) {
7056returnisSafeToSpeculativelyExecuteWithOpcode(Inst->getOpcode(), Inst, CtxI,
7057 AC, DT, TLI, UseVariableInfo);
7058}
7059
7060boolllvm::isSafeToSpeculativelyExecuteWithOpcode(
7061unsigned Opcode,constInstruction *Inst,constInstruction *CtxI,
7062AssumptionCache *AC,constDominatorTree *DT,constTargetLibraryInfo *TLI,
7063bool UseVariableInfo) {
7064#ifndef NDEBUG
7065if (Inst->getOpcode() != Opcode) {
7066// Check that the operands are actually compatible with the Opcode override.
7067auto hasEqualReturnAndLeadingOperandTypes =
7068 [](constInstruction *Inst,unsigned NumLeadingOperands) {
7069if (Inst->getNumOperands() < NumLeadingOperands)
7070returnfalse;
7071constType *ExpectedType = Inst->getType();
7072for (unsigned ItOp = 0; ItOp < NumLeadingOperands; ++ItOp)
7073if (Inst->getOperand(ItOp)->getType() != ExpectedType)
7074returnfalse;
7075returntrue;
7076 };
7077assert(!Instruction::isBinaryOp(Opcode) ||
7078 hasEqualReturnAndLeadingOperandTypes(Inst, 2));
7079assert(!Instruction::isUnaryOp(Opcode) ||
7080 hasEqualReturnAndLeadingOperandTypes(Inst, 1));
7081 }
7082#endif
7083
7084switch (Opcode) {
7085default:
7086returntrue;
7087case Instruction::UDiv:
7088case Instruction::URem: {
7089// x / y is undefined if y == 0.
7090constAPInt *V;
7091if (match(Inst->getOperand(1),m_APInt(V)))
7092return *V != 0;
7093returnfalse;
7094 }
7095case Instruction::SDiv:
7096case Instruction::SRem: {
7097// x / y is undefined if y == 0 or x == INT_MIN and y == -1
7098constAPInt *Numerator, *Denominator;
7099if (!match(Inst->getOperand(1),m_APInt(Denominator)))
7100returnfalse;
7101// We cannot hoist this division if the denominator is 0.
7102if (*Denominator == 0)
7103returnfalse;
7104// It's safe to hoist if the denominator is not 0 or -1.
7105if (!Denominator->isAllOnes())
7106returntrue;
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.
7109if (match(Inst->getOperand(0),m_APInt(Numerator)))
7110return !Numerator->isMinSignedValue();
7111// The numerator *might* be MinSignedValue.
7112returnfalse;
7113 }
7114case Instruction::Load: {
7115if (!UseVariableInfo)
7116returnfalse;
7117
7118constLoadInst *LI = dyn_cast<LoadInst>(Inst);
7119if (!LI)
7120returnfalse;
7121if (mustSuppressSpeculation(*LI))
7122returnfalse;
7123constDataLayout &DL = LI->getDataLayout();
7124returnisDereferenceableAndAlignedPointer(LI->getPointerOperand(),
7125 LI->getType(), LI->getAlign(),DL,
7126 CtxI, AC, DT, TLI);
7127 }
7128case Instruction::Call: {
7129auto *CI = dyn_cast<const CallInst>(Inst);
7130if (!CI)
7131returnfalse;
7132constFunction *Callee = CI->getCalledFunction();
7133
7134// The called function could have undefined behavior or side-effects, even
7135// if marked readnone nounwind.
7136return Callee && Callee->isSpeculatable();
7137 }
7138case Instruction::VAArg:
7139case Instruction::Alloca:
7140case Instruction::Invoke:
7141case Instruction::CallBr:
7142case Instruction::PHI:
7143case Instruction::Store:
7144case Instruction::Ret:
7145case Instruction::Br:
7146case Instruction::IndirectBr:
7147case Instruction::Switch:
7148case Instruction::Unreachable:
7149case Instruction::Fence:
7150case Instruction::AtomicRMW:
7151case Instruction::AtomicCmpXchg:
7152case Instruction::LandingPad:
7153case Instruction::Resume:
7154case Instruction::CatchSwitch:
7155case Instruction::CatchPad:
7156case Instruction::CatchRet:
7157case Instruction::CleanupPad:
7158case Instruction::CleanupRet:
7159returnfalse;// Misc instructions which have effects
7160 }
7161}
7162
7163boolllvm::mayHaveNonDefUseDependency(constInstruction &I) {
7164if (I.mayReadOrWriteMemory())
7165// Memory dependency possible
7166returntrue;
7167if (!isSafeToSpeculativelyExecute(&I))
7168// Can't move above a maythrow call or infinite loop. Or if an
7169// inalloca alloca, above a stacksave call.
7170returntrue;
7171if (!isGuaranteedToTransferExecutionToSuccessor(&I))
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)
7175returntrue;
7176returnfalse;
7177}
7178
7179/// Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
7180staticOverflowResultmapOverflowResult(ConstantRange::OverflowResult OR) {
7181switch (OR) {
7182caseConstantRange::OverflowResult::MayOverflow:
7183returnOverflowResult::MayOverflow;
7184caseConstantRange::OverflowResult::AlwaysOverflowsLow:
7185returnOverflowResult::AlwaysOverflowsLow;
7186caseConstantRange::OverflowResult::AlwaysOverflowsHigh:
7187returnOverflowResult::AlwaysOverflowsHigh;
7188caseConstantRange::OverflowResult::NeverOverflows:
7189returnOverflowResult::NeverOverflows;
7190 }
7191llvm_unreachable("Unknown OverflowResult");
7192}
7193
7194/// Combine constant ranges from computeConstantRange() and computeKnownBits().
7195ConstantRange
7196llvm::computeConstantRangeIncludingKnownBits(constWithCache<const Value *> &V,
7197bool ForSigned,
7198constSimplifyQuery &SQ) {
7199ConstantRange CR1 =
7200ConstantRange::fromKnownBits(V.getKnownBits(SQ), ForSigned);
7201ConstantRange CR2 =computeConstantRange(V, ForSigned, SQ.IIQ.UseInstrInfo);
7202ConstantRange::PreferredRangeType RangeType =
7203 ForSigned ?ConstantRange::Signed :ConstantRange::Unsigned;
7204return CR1.intersectWith(CR2, RangeType);
7205}
7206
7207OverflowResultllvm::computeOverflowForUnsignedMul(constValue *LHS,
7208constValue *RHS,
7209constSimplifyQuery &SQ,
7210bool IsNSW) {
7211KnownBits LHSKnown =computeKnownBits(LHS,/*Depth=*/0, SQ);
7212KnownBits RHSKnown =computeKnownBits(RHS,/*Depth=*/0, SQ);
7213
7214// mul nsw of two non-negative numbers is also nuw.
7215if (IsNSW && LHSKnown.isNonNegative() && RHSKnown.isNonNegative())
7216returnOverflowResult::NeverOverflows;
7217
7218ConstantRange LHSRange =ConstantRange::fromKnownBits(LHSKnown,false);
7219ConstantRange RHSRange =ConstantRange::fromKnownBits(RHSKnown,false);
7220returnmapOverflowResult(LHSRange.unsignedMulMayOverflow(RHSRange));
7221}
7222
7223OverflowResultllvm::computeOverflowForSignedMul(constValue *LHS,
7224constValue *RHS,
7225constSimplifyQuery &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
7232unsignedBitWidth =LHS->getType()->getScalarSizeInBits();
7233
7234// Note that underestimating the number of sign bits gives a more
7235// conservative answer.
7236unsigned SignBits =
7237::ComputeNumSignBits(LHS, 0, SQ) +::ComputeNumSignBits(RHS, 0, SQ);
7238
7239// First handle the easy case: if we have enough sign bits there's
7240// definitely no overflow.
7241if (SignBits >BitWidth + 1)
7242returnOverflowResult::NeverOverflows;
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.
7249if (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.
7254KnownBits LHSKnown =computeKnownBits(LHS,/*Depth=*/0, SQ);
7255KnownBits RHSKnown =computeKnownBits(RHS,/*Depth=*/0, SQ);
7256if (LHSKnown.isNonNegative() || RHSKnown.isNonNegative())
7257returnOverflowResult::NeverOverflows;
7258 }
7259returnOverflowResult::MayOverflow;
7260}
7261
7262OverflowResult
7263llvm::computeOverflowForUnsignedAdd(constWithCache<const Value *> &LHS,
7264constWithCache<const Value *> &RHS,
7265constSimplifyQuery &SQ) {
7266ConstantRange LHSRange =
7267computeConstantRangeIncludingKnownBits(LHS,/*ForSigned=*/false, SQ);
7268ConstantRange RHSRange =
7269computeConstantRangeIncludingKnownBits(RHS,/*ForSigned=*/false, SQ);
7270returnmapOverflowResult(LHSRange.unsignedAddMayOverflow(RHSRange));
7271}
7272
7273staticOverflowResult
7274computeOverflowForSignedAdd(constWithCache<const Value *> &LHS,
7275constWithCache<const Value *> &RHS,
7276constAddOperator *Add,constSimplifyQuery &SQ) {
7277if (Add &&Add->hasNoSignedWrap()) {
7278returnOverflowResult::NeverOverflows;
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.
7295if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
7296::ComputeNumSignBits(RHS, 0, SQ) > 1)
7297returnOverflowResult::NeverOverflows;
7298
7299ConstantRange LHSRange =
7300computeConstantRangeIncludingKnownBits(LHS,/*ForSigned=*/true, SQ);
7301ConstantRange RHSRange =
7302computeConstantRangeIncludingKnownBits(RHS,/*ForSigned=*/true, SQ);
7303OverflowResult OR =
7304mapOverflowResult(LHSRange.signedAddMayOverflow(RHSRange));
7305if (OR !=OverflowResult::MayOverflow)
7306return OR;
7307
7308// The remaining code needs Add to be available. Early returns if not so.
7309if (!Add)
7310returnOverflowResult::MayOverflow;
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.
7317bool LHSOrRHSKnownNonNegative =
7318 (LHSRange.isAllNonNegative() || RHSRange.isAllNonNegative());
7319bool LHSOrRHSKnownNegative =
7320 (LHSRange.isAllNegative() || RHSRange.isAllNegative());
7321if (LHSOrRHSKnownNonNegative || LHSOrRHSKnownNegative) {
7322KnownBits AddKnown(LHSRange.getBitWidth());
7323computeKnownBitsFromContext(Add, AddKnown,/*Depth=*/0, SQ);
7324if ((AddKnown.isNonNegative() && LHSOrRHSKnownNonNegative) ||
7325 (AddKnown.isNegative() && LHSOrRHSKnownNegative))
7326returnOverflowResult::NeverOverflows;
7327 }
7328
7329returnOverflowResult::MayOverflow;
7330}
7331
7332OverflowResultllvm::computeOverflowForUnsignedSub(constValue *LHS,
7333constValue *RHS,
7334constSimplifyQuery &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.
7346if (match(RHS,m_URem(m_Specific(LHS),m_Value())) ||
7347match(RHS,m_NUWSub(m_Specific(LHS),m_Value())))
7348if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7349returnOverflowResult::NeverOverflows;
7350
7351if (autoC =isImpliedByDomCondition(CmpInst::ICMP_UGE,LHS,RHS, SQ.CxtI,
7352 SQ.DL)) {
7353if (*C)
7354returnOverflowResult::NeverOverflows;
7355returnOverflowResult::AlwaysOverflowsLow;
7356 }
7357
7358ConstantRange LHSRange =
7359computeConstantRangeIncludingKnownBits(LHS,/*ForSigned=*/false, SQ);
7360ConstantRange RHSRange =
7361computeConstantRangeIncludingKnownBits(RHS,/*ForSigned=*/false, SQ);
7362returnmapOverflowResult(LHSRange.unsignedSubMayOverflow(RHSRange));
7363}
7364
7365OverflowResultllvm::computeOverflowForSignedSub(constValue *LHS,
7366constValue *RHS,
7367constSimplifyQuery &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.
7376if (match(RHS,m_SRem(m_Specific(LHS),m_Value())) ||
7377match(RHS,m_NSWSub(m_Specific(LHS),m_Value())))
7378if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
7379returnOverflowResult::NeverOverflows;
7380
7381// If LHS and RHS each have at least two sign bits, the subtraction
7382// cannot overflow.
7383if (::ComputeNumSignBits(LHS, 0, SQ) > 1 &&
7384::ComputeNumSignBits(RHS, 0, SQ) > 1)
7385returnOverflowResult::NeverOverflows;
7386
7387ConstantRange LHSRange =
7388computeConstantRangeIncludingKnownBits(LHS,/*ForSigned=*/true, SQ);
7389ConstantRange RHSRange =
7390computeConstantRangeIncludingKnownBits(RHS,/*ForSigned=*/true, SQ);
7391returnmapOverflowResult(LHSRange.signedSubMayOverflow(RHSRange));
7392}
7393
7394boolllvm::isOverflowIntrinsicNoWrap(constWithOverflowInst *WO,
7395constDominatorTree &DT) {
7396SmallVector<const BranchInst *, 2> GuardingBranches;
7397SmallVector<const ExtractValueInst *, 2>Results;
7398
7399for (constUser *U : WO->users()) {
7400if (constauto *EVI = dyn_cast<ExtractValueInst>(U)) {
7401assert(EVI->getNumIndices() == 1 &&"Obvious from CI's type");
7402
7403if (EVI->getIndices()[0] == 0)
7404Results.push_back(EVI);
7405else {
7406assert(EVI->getIndices()[0] == 1 &&"Obvious from CI's type");
7407
7408for (constauto *U : EVI->users())
7409if (constauto *B = dyn_cast<BranchInst>(U)) {
7410assert(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).
7417returnfalse;
7418 }
7419 }
7420
7421auto AllUsesGuardedByBranch = [&](constBranchInst *BI) {
7422BasicBlockEdge NoWrapEdge(BI->getParent(), BI->getSuccessor(1));
7423if (!NoWrapEdge.isSingleEdge())
7424returnfalse;
7425
7426// Check if all users of the add are provably no-wrap.
7427for (constauto *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.
7430if (DT.dominates(NoWrapEdge, Result->getParent()))
7431continue;
7432
7433for (constauto &RU : Result->uses())
7434if (!DT.dominates(NoWrapEdge, RU))
7435returnfalse;
7436 }
7437
7438returntrue;
7439 };
7440
7441returnllvm::any_of(GuardingBranches, AllUsesGuardedByBranch);
7442}
7443
7444/// Shifts return poison if shiftwidth is larger than the bitwidth.
7445staticboolshiftAmountKnownInRange(constValue *ShiftAmount) {
7446auto *C = dyn_cast<Constant>(ShiftAmount);
7447if (!C)
7448returnfalse;
7449
7450// Shifts return poison if shiftwidth is larger than the bitwidth.
7451SmallVector<const Constant *, 4> ShiftAmounts;
7452if (auto *FVTy = dyn_cast<FixedVectorType>(C->getType())) {
7453unsigned NumElts = FVTy->getNumElements();
7454for (unsigned i = 0; i < NumElts; ++i)
7455 ShiftAmounts.push_back(C->getAggregateElement(i));
7456 }elseif (isa<ScalableVectorType>(C->getType()))
7457returnfalse;// Can't tell, just return false to be safe
7458else
7459 ShiftAmounts.push_back(C);
7460
7461bool Safe =llvm::all_of(ShiftAmounts, [](constConstant *C) {
7462auto *CI = dyn_cast_or_null<ConstantInt>(C);
7463return CI && CI->getValue().ult(C->getType()->getIntegerBitWidth());
7464 });
7465
7466return Safe;
7467}
7468
7469enum classUndefPoisonKind {
7470PoisonOnly = (1 << 0),
7471UndefOnly = (1 << 1),
7472UndefOrPoison =PoisonOnly |UndefOnly,
7473};
7474
7475staticboolincludesPoison(UndefPoisonKind Kind) {
7476return (unsigned(Kind) &unsigned(UndefPoisonKind::PoisonOnly)) != 0;
7477}
7478
7479staticboolincludesUndef(UndefPoisonKind Kind) {
7480return (unsigned(Kind) &unsigned(UndefPoisonKind::UndefOnly)) != 0;
7481}
7482
7483staticboolcanCreateUndefOrPoison(constOperator *Op,UndefPoisonKind Kind,
7484bool ConsiderFlagsAndMetadata) {
7485
7486if (ConsiderFlagsAndMetadata &&includesPoison(Kind) &&
7487Op->hasPoisonGeneratingAnnotations())
7488returntrue;
7489
7490unsigned Opcode =Op->getOpcode();
7491
7492// Check whether opcode is a poison/undef-generating operation
7493switch (Opcode) {
7494case Instruction::Shl:
7495case Instruction::AShr:
7496case Instruction::LShr:
7497returnincludesPoison(Kind) && !shiftAmountKnownInRange(Op->getOperand(1));
7498case Instruction::FPToSI:
7499case Instruction::FPToUI:
7500// fptosi/ui yields poison if the resulting value does not fit in the
7501// destination type.
7502returntrue;
7503case Instruction::Call:
7504if (auto *II = dyn_cast<IntrinsicInst>(Op)) {
7505switch (II->getIntrinsicID()) {
7506// TODO: Add more intrinsics.
7507case Intrinsic::ctlz:
7508case Intrinsic::cttz:
7509case Intrinsic::abs:
7510if (cast<ConstantInt>(II->getArgOperand(1))->isNullValue())
7511returnfalse;
7512break;
7513case Intrinsic::ctpop:
7514case Intrinsic::bswap:
7515case Intrinsic::bitreverse:
7516case Intrinsic::fshl:
7517case Intrinsic::fshr:
7518case Intrinsic::smax:
7519case Intrinsic::smin:
7520case Intrinsic::umax:
7521case Intrinsic::umin:
7522case Intrinsic::ptrmask:
7523case Intrinsic::fptoui_sat:
7524case Intrinsic::fptosi_sat:
7525case Intrinsic::sadd_with_overflow:
7526case Intrinsic::ssub_with_overflow:
7527case Intrinsic::smul_with_overflow:
7528case Intrinsic::uadd_with_overflow:
7529case Intrinsic::usub_with_overflow:
7530case Intrinsic::umul_with_overflow:
7531case Intrinsic::sadd_sat:
7532case Intrinsic::uadd_sat:
7533case Intrinsic::ssub_sat:
7534case Intrinsic::usub_sat:
7535returnfalse;
7536case Intrinsic::sshl_sat:
7537case Intrinsic::ushl_sat:
7538returnincludesPoison(Kind) &&
7539 !shiftAmountKnownInRange(II->getArgOperand(1));
7540case Intrinsic::fma:
7541case Intrinsic::fmuladd:
7542case Intrinsic::sqrt:
7543case Intrinsic::powi:
7544case Intrinsic::sin:
7545case Intrinsic::cos:
7546case Intrinsic::pow:
7547case Intrinsic::log:
7548case Intrinsic::log10:
7549case Intrinsic::log2:
7550case Intrinsic::exp:
7551case Intrinsic::exp2:
7552case Intrinsic::exp10:
7553case Intrinsic::fabs:
7554case Intrinsic::copysign:
7555case Intrinsic::floor:
7556case Intrinsic::ceil:
7557case Intrinsic::trunc:
7558case Intrinsic::rint:
7559case Intrinsic::nearbyint:
7560case Intrinsic::round:
7561case Intrinsic::roundeven:
7562case Intrinsic::fptrunc_round:
7563case Intrinsic::canonicalize:
7564case Intrinsic::arithmetic_fence:
7565case Intrinsic::minnum:
7566case Intrinsic::maxnum:
7567case Intrinsic::minimum:
7568case Intrinsic::maximum:
7569case Intrinsic::is_fpclass:
7570case Intrinsic::ldexp:
7571case Intrinsic::frexp:
7572returnfalse;
7573case Intrinsic::lround:
7574case Intrinsic::llround:
7575case Intrinsic::lrint:
7576case Intrinsic::llrint:
7577// If the value doesn't fit an unspecified value is returned (but this
7578// is not poison).
7579returnfalse;
7580 }
7581 }
7582 [[fallthrough]];
7583case Instruction::CallBr:
7584case Instruction::Invoke: {
7585constauto *CB = cast<CallBase>(Op);
7586return !CB->hasRetAttr(Attribute::NoUndef);
7587 }
7588case Instruction::InsertElement:
7589case Instruction::ExtractElement: {
7590// If index exceeds the length of the vector, it returns poison
7591auto *VTy = cast<VectorType>(Op->getOperand(0)->getType());
7592unsigned IdxOp =Op->getOpcode() == Instruction::InsertElement ? 2 : 1;
7593auto *Idx = dyn_cast<ConstantInt>(Op->getOperand(IdxOp));
7594if (includesPoison(Kind))
7595return !Idx ||
7596Idx->getValue().uge(VTy->getElementCount().getKnownMinValue());
7597returnfalse;
7598 }
7599case Instruction::ShuffleVector: {
7600ArrayRef<int> Mask = isa<ConstantExpr>(Op)
7601 ? cast<ConstantExpr>(Op)->getShuffleMask()
7602 : cast<ShuffleVectorInst>(Op)->getShuffleMask();
7603returnincludesPoison(Kind) &&is_contained(Mask,PoisonMaskElem);
7604 }
7605case Instruction::FNeg:
7606case Instruction::PHI:
7607case Instruction::Select:
7608case Instruction::URem:
7609case Instruction::SRem:
7610case Instruction::ExtractValue:
7611case Instruction::InsertValue:
7612case Instruction::Freeze:
7613case Instruction::ICmp:
7614case Instruction::FCmp:
7615case Instruction::FAdd:
7616case Instruction::FSub:
7617case Instruction::FMul:
7618case Instruction::FDiv:
7619case Instruction::FRem:
7620returnfalse;
7621case Instruction::GetElementPtr:
7622// inbounds is handled above
7623// TODO: what about inrange on constexpr?
7624returnfalse;
7625default: {
7626constauto *CE = dyn_cast<ConstantExpr>(Op);
7627if (isa<CastInst>(Op) || (CE && CE->isCast()))
7628returnfalse;
7629elseif (Instruction::isBinaryOp(Opcode))
7630returnfalse;
7631// Be conservative and return true.
7632returntrue;
7633 }
7634 }
7635}
7636
7637boolllvm::canCreateUndefOrPoison(constOperator *Op,
7638bool ConsiderFlagsAndMetadata) {
7639 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::UndefOrPoison,
7640 ConsiderFlagsAndMetadata);
7641}
7642
7643boolllvm::canCreatePoison(constOperator *Op,bool ConsiderFlagsAndMetadata) {
7644 return ::canCreateUndefOrPoison(Op, UndefPoisonKind::PoisonOnly,
7645 ConsiderFlagsAndMetadata);
7646}
7647
7648staticbooldirectlyImpliesPoison(constValue *ValAssumedPoison,constValue *V,
7649unsignedDepth) {
7650if (ValAssumedPoison == V)
7651returntrue;
7652
7653constunsigned MaxDepth = 2;
7654if (Depth >= MaxDepth)
7655returnfalse;
7656
7657if (constauto *I = dyn_cast<Instruction>(V)) {
7658if (any_of(I->operands(), [=](constUse &Op) {
7659 return propagatesPoison(Op) &&
7660 directlyImpliesPoison(ValAssumedPoison, Op, Depth + 1);
7661 }))
7662returntrue;
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)
7667constWithOverflowInst *II;
7668if (match(I,m_ExtractValue(m_WithOverflowInst(II))) &&
7669 (match(ValAssumedPoison,m_ExtractValue(m_Specific(II))) ||
7670llvm::is_contained(II->args(), ValAssumedPoison)))
7671returntrue;
7672 }
7673returnfalse;
7674}
7675
7676staticboolimpliesPoison(constValue *ValAssumedPoison,constValue *V,
7677unsignedDepth) {
7678if (isGuaranteedNotToBePoison(ValAssumedPoison))
7679returntrue;
7680
7681if (directlyImpliesPoison(ValAssumedPoison, V,/* Depth */ 0))
7682returntrue;
7683
7684constunsigned MaxDepth = 2;
7685if (Depth >= MaxDepth)
7686returnfalse;
7687
7688constauto *I = dyn_cast<Instruction>(ValAssumedPoison);
7689if (I && !canCreatePoison(cast<Operator>(I))) {
7690returnall_of(I->operands(), [=](constValue *Op) {
7691 return impliesPoison(Op, V, Depth + 1);
7692 });
7693 }
7694returnfalse;
7695}
7696
7697boolllvm::impliesPoison(constValue *ValAssumedPoison,constValue *V) {
7698 return ::impliesPoison(ValAssumedPoison, V,/* Depth */ 0);
7699}
7700
7701staticboolprogramUndefinedIfUndefOrPoison(constValue *V,boolPoisonOnly);
7702
7703staticboolisGuaranteedNotToBeUndefOrPoison(
7704constValue *V,AssumptionCache *AC,constInstruction *CtxI,
7705constDominatorTree *DT,unsignedDepth,UndefPoisonKind Kind) {
7706if (Depth >=MaxAnalysisRecursionDepth)
7707returnfalse;
7708
7709if (isa<MetadataAsValue>(V))
7710returnfalse;
7711
7712if (constauto *A = dyn_cast<Argument>(V)) {
7713if (A->hasAttribute(Attribute::NoUndef) ||
7714A->hasAttribute(Attribute::Dereferenceable) ||
7715A->hasAttribute(Attribute::DereferenceableOrNull))
7716returntrue;
7717 }
7718
7719if (auto *C = dyn_cast<Constant>(V)) {
7720if (isa<PoisonValue>(C))
7721return !includesPoison(Kind);
7722
7723if (isa<UndefValue>(C))
7724return !includesUndef(Kind);
7725
7726if (isa<ConstantInt>(C) || isa<GlobalVariable>(C) || isa<ConstantFP>(V) ||
7727 isa<ConstantPointerNull>(C) || isa<Function>(C))
7728returntrue;
7729
7730if (C->getType()->isVectorTy() && !isa<ConstantExpr>(C)) {
7731if (includesUndef(Kind) &&C->containsUndefElement())
7732returnfalse;
7733if (includesPoison(Kind) &&C->containsPoisonElement())
7734returnfalse;
7735return !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.
7747auto *StrippedV = V->stripPointerCastsSameRepresentation();
7748if (isa<AllocaInst>(StrippedV) || isa<GlobalVariable>(StrippedV) ||
7749 isa<Function>(StrippedV) || isa<ConstantPointerNull>(StrippedV))
7750returntrue;
7751
7752auto OpCheck = [&](constValue *V) {
7753returnisGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT,Depth + 1, Kind);
7754 };
7755
7756if (auto *Opr = dyn_cast<Operator>(V)) {
7757// If the value is a freeze instruction, then it can never
7758// be undef or poison.
7759if (isa<FreezeInst>(V))
7760returntrue;
7761
7762if (constauto *CB = dyn_cast<CallBase>(V)) {
7763if (CB->hasRetAttr(Attribute::NoUndef) ||
7764 CB->hasRetAttr(Attribute::Dereferenceable) ||
7765 CB->hasRetAttr(Attribute::DereferenceableOrNull))
7766returntrue;
7767 }
7768
7769if (constauto *PN = dyn_cast<PHINode>(V)) {
7770unsigned Num = PN->getNumIncomingValues();
7771bool IsWellDefined =true;
7772for (unsigned i = 0; i < Num; ++i) {
7773auto *TI = PN->getIncomingBlock(i)->getTerminator();
7774if (!isGuaranteedNotToBeUndefOrPoison(PN->getIncomingValue(i), AC, TI,
7775 DT,Depth + 1, Kind)) {
7776 IsWellDefined =false;
7777break;
7778 }
7779 }
7780if (IsWellDefined)
7781returntrue;
7782 }elseif (!::canCreateUndefOrPoison(Opr, Kind,
7783/*ConsiderFlagsAndMetadata*/true) &&
7784all_of(Opr->operands(), OpCheck))
7785returntrue;
7786 }
7787
7788if (auto *I = dyn_cast<LoadInst>(V))
7789if (I->hasMetadata(LLVMContext::MD_noundef) ||
7790I->hasMetadata(LLVMContext::MD_dereferenceable) ||
7791I->hasMetadata(LLVMContext::MD_dereferenceable_or_null))
7792returntrue;
7793
7794if (programUndefinedIfUndefOrPoison(V, !includesUndef(Kind)))
7795returntrue;
7796
7797// CxtI may be null or a cloned instruction.
7798if (!CtxI || !CtxI->getParent() || !DT)
7799returnfalse;
7800
7801auto *DNode = DT->getNode(CtxI->getParent());
7802if (!DNode)
7803// Unreachable block
7804returnfalse;
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
7811auto *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.
7814if (!includesUndef(Kind) || V->getType()->isIntegerTy())
7815while (Dominator) {
7816auto *TI = Dominator->getBlock()->getTerminator();
7817
7818Value *Cond =nullptr;
7819if (auto BI = dyn_cast_or_null<BranchInst>(TI)) {
7820if (BI->isConditional())
7821Cond = BI->getCondition();
7822 }elseif (auto SI = dyn_cast_or_null<SwitchInst>(TI)) {
7823Cond = SI->getCondition();
7824 }
7825
7826if (Cond) {
7827if (Cond == V)
7828returntrue;
7829elseif (!includesUndef(Kind) && isa<Operator>(Cond)) {
7830// For poison, we can analyze further
7831auto *Opr = cast<Operator>(Cond);
7832if (any_of(Opr->operands(), [V](constUse &U) {
7833 return V == U && propagatesPoison(U);
7834 }))
7835returntrue;
7836 }
7837 }
7838
7839 Dominator = Dominator->getIDom();
7840 }
7841
7842if (getKnowledgeValidInContext(V, {Attribute::NoUndef}, CtxI, DT, AC))
7843returntrue;
7844
7845returnfalse;
7846}
7847
7848boolllvm::isGuaranteedNotToBeUndefOrPoison(constValue *V,AssumptionCache *AC,
7849constInstruction *CtxI,
7850constDominatorTree *DT,
7851unsignedDepth) {
7852 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT,Depth,
7853 UndefPoisonKind::UndefOrPoison);
7854}
7855
7856boolllvm::isGuaranteedNotToBePoison(constValue *V,AssumptionCache *AC,
7857constInstruction *CtxI,
7858constDominatorTree *DT,unsignedDepth) {
7859 return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT,Depth,
7860 UndefPoisonKind::PoisonOnly);
7861}
7862
7863boolllvm::isGuaranteedNotToBeUndef(constValue *V,AssumptionCache *AC,
7864constInstruction *CtxI,
7865constDominatorTree *DT,unsignedDepth) {
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.
7877boolllvm::mustExecuteUBIfPoisonOnPathTo(Instruction *Root,
7878Instruction *OnPathTo,
7879DominatorTree *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)
7887SmallSet<const Value *, 16> KnownPoison;
7888SmallVector<const Instruction*, 16> Worklist;
7889 Worklist.push_back(Root);
7890while (!Worklist.empty()) {
7891constInstruction *I = Worklist.pop_back_val();
7892
7893// If we know this must trigger UB on a path leading our target.
7894if (mustTriggerUB(I, KnownPoison) && DT->dominates(I, OnPathTo))
7895returntrue;
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.
7899if (I != Root && !any_of(I->operands(), [&KnownPoison](constUse &U) {
7900 return KnownPoison.contains(U) && propagatesPoison(U);
7901 }))
7902continue;
7903
7904if (KnownPoison.insert(I).second)
7905for (constUser *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.
7911returnfalse;
7912}
7913
7914OverflowResultllvm::computeOverflowForSignedAdd(constAddOperator *Add,
7915constSimplifyQuery &SQ) {
7916 return ::computeOverflowForSignedAdd(Add->getOperand(0),Add->getOperand(1),
7917Add, SQ);
7918}
7919
7920OverflowResult
7921llvm::computeOverflowForSignedAdd(constWithCache<const Value *> &LHS,
7922constWithCache<const Value *> &RHS,
7923constSimplifyQuery &SQ) {
7924 return ::computeOverflowForSignedAdd(LHS,RHS,nullptr, SQ);
7925}
7926
7927boolllvm::isGuaranteedToTransferExecutionToSuccessor(constInstruction *I) {
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.
7933if (isa<ReturnInst>(I))
7934returnfalse;
7935if (isa<UnreachableInst>(I))
7936returnfalse;
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.
7942if (isa<CatchPadInst>(I)) {
7943switch (classifyEHPersonality(I->getFunction()->getPersonalityFn())) {
7944default:
7945// A catchpad may invoke exception object constructors and such, which
7946// in some languages can be arbitrary code, so be conservative by default.
7947returnfalse;
7948caseEHPersonality::CoreCLR:
7949// For CoreCLR, it just involves a type test.
7950returntrue;
7951 }
7952 }
7953
7954// An instruction that returns without throwing must transfer control flow
7955// to a successor.
7956return !I->mayThrow() &&I->willReturn();
7957}
7958
7959boolllvm::isGuaranteedToTransferExecutionToSuccessor(constBasicBlock *BB) {
7960// TODO: This is slightly conservative for invoke instruction since exiting
7961// via an exception *is* normal control for them.
7962for (constInstruction &I : *BB)
7963if (!isGuaranteedToTransferExecutionToSuccessor(&I))
7964returnfalse;
7965returntrue;
7966}
7967
7968boolllvm::isGuaranteedToTransferExecutionToSuccessor(
7969BasicBlock::const_iterator Begin,BasicBlock::const_iteratorEnd,
7970unsigned ScanLimit) {
7971returnisGuaranteedToTransferExecutionToSuccessor(make_range(Begin,End),
7972 ScanLimit);
7973}
7974
7975boolllvm::isGuaranteedToTransferExecutionToSuccessor(
7976iterator_range<BasicBlock::const_iterator>Range,unsigned ScanLimit) {
7977assert(ScanLimit &&"scan limit must be non-zero");
7978for (constInstruction &I :Range) {
7979if (isa<DbgInfoIntrinsic>(I))
7980continue;
7981if (--ScanLimit == 0)
7982returnfalse;
7983if (!isGuaranteedToTransferExecutionToSuccessor(&I))
7984returnfalse;
7985 }
7986returntrue;
7987}
7988
7989boolllvm::isGuaranteedToExecuteForEveryIteration(constInstruction *I,
7990constLoop *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.
7995if (I->getParent() != L->getHeader())returnfalse;
7996
7997for (constInstruction &LI : *L->getHeader()) {
7998if (&LI ==I)returntrue;
7999if (!isGuaranteedToTransferExecutionToSuccessor(&LI))returnfalse;
8000 }
8001llvm_unreachable("Instruction not contained in its own parent basic block.");
8002}
8003
8004boolllvm::propagatesPoison(constUse &PoisonOp) {
8005constOperator *I = cast<Operator>(PoisonOp.getUser());
8006switch (I->getOpcode()) {
8007case Instruction::Freeze:
8008case Instruction::PHI:
8009case Instruction::Invoke:
8010returnfalse;
8011case Instruction::Select:
8012return PoisonOp.getOperandNo() == 0;
8013case Instruction::Call:
8014if (auto *II = dyn_cast<IntrinsicInst>(I)) {
8015switch (II->getIntrinsicID()) {
8016// TODO: Add more intrinsics.
8017case Intrinsic::sadd_with_overflow:
8018case Intrinsic::ssub_with_overflow:
8019case Intrinsic::smul_with_overflow:
8020case Intrinsic::uadd_with_overflow:
8021case Intrinsic::usub_with_overflow:
8022case 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.
8026returntrue;
8027case Intrinsic::ctpop:
8028case Intrinsic::ctlz:
8029case Intrinsic::cttz:
8030case Intrinsic::abs:
8031case Intrinsic::smax:
8032case Intrinsic::smin:
8033case Intrinsic::umax:
8034case Intrinsic::umin:
8035case Intrinsic::bitreverse:
8036case Intrinsic::bswap:
8037case Intrinsic::sadd_sat:
8038case Intrinsic::ssub_sat:
8039case Intrinsic::sshl_sat:
8040case Intrinsic::uadd_sat:
8041case Intrinsic::usub_sat:
8042case Intrinsic::ushl_sat:
8043returntrue;
8044 }
8045 }
8046returnfalse;
8047case Instruction::ICmp:
8048case Instruction::FCmp:
8049case Instruction::GetElementPtr:
8050returntrue;
8051default:
8052if (isa<BinaryOperator>(I) || isa<UnaryOperator>(I) || isa<CastInst>(I))
8053returntrue;
8054
8055// Be conservative and return false.
8056returnfalse;
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>
8064staticboolhandleGuaranteedWellDefinedOps(constInstruction *I,
8065const CallableT &Handle) {
8066switch (I->getOpcode()) {
8067case Instruction::Store:
8068if (Handle(cast<StoreInst>(I)->getPointerOperand()))
8069returntrue;
8070break;
8071
8072case Instruction::Load:
8073if (Handle(cast<LoadInst>(I)->getPointerOperand()))
8074returntrue;
8075break;
8076
8077// Since dereferenceable attribute imply noundef, atomic operations
8078// also implicitly have noundef pointers too
8079case Instruction::AtomicCmpXchg:
8080if (Handle(cast<AtomicCmpXchgInst>(I)->getPointerOperand()))
8081returntrue;
8082break;
8083
8084case Instruction::AtomicRMW:
8085if (Handle(cast<AtomicRMWInst>(I)->getPointerOperand()))
8086returntrue;
8087break;
8088
8089case Instruction::Call:
8090case Instruction::Invoke: {
8091constCallBase *CB = cast<CallBase>(I);
8092if (CB->isIndirectCall() && Handle(CB->getCalledOperand()))
8093returntrue;
8094for (unsigned i = 0; i < CB->arg_size(); ++i)
8095if ((CB->paramHasAttr(i, Attribute::NoUndef) ||
8096 CB->paramHasAttr(i, Attribute::Dereferenceable) ||
8097 CB->paramHasAttr(i, Attribute::DereferenceableOrNull)) &&
8098 Handle(CB->getArgOperand(i)))
8099returntrue;
8100break;
8101 }
8102case Instruction::Ret:
8103if (I->getFunction()->hasRetAttribute(Attribute::NoUndef) &&
8104 Handle(I->getOperand(0)))
8105returntrue;
8106break;
8107case Instruction::Switch:
8108if (Handle(cast<SwitchInst>(I)->getCondition()))
8109returntrue;
8110break;
8111case Instruction::Br: {
8112auto *BR = cast<BranchInst>(I);
8113if (BR->isConditional() && Handle(BR->getCondition()))
8114returntrue;
8115break;
8116 }
8117default:
8118break;
8119 }
8120
8121returnfalse;
8122}
8123
8124voidllvm::getGuaranteedWellDefinedOps(
8125constInstruction *I,SmallVectorImpl<const Value *> &Operands) {
8126handleGuaranteedWellDefinedOps(I, [&](constValue *V) {
8127Operands.push_back(V);
8128returnfalse;
8129 });
8130}
8131
8132/// Enumerates all operands of \p I that are guaranteed to not be poison.
8133template <typename CallableT>
8134staticboolhandleGuaranteedNonPoisonOps(constInstruction *I,
8135const CallableT &Handle) {
8136if (handleGuaranteedWellDefinedOps(I, Handle))
8137returntrue;
8138switch (I->getOpcode()) {
8139// Divisors of these operations are allowed to be partially undef.
8140case Instruction::UDiv:
8141case Instruction::SDiv:
8142case Instruction::URem:
8143case Instruction::SRem:
8144return Handle(I->getOperand(1));
8145default:
8146returnfalse;
8147 }
8148}
8149
8150voidllvm::getGuaranteedNonPoisonOps(constInstruction *I,
8151SmallVectorImpl<const Value *> &Operands) {
8152handleGuaranteedNonPoisonOps(I, [&](constValue *V) {
8153Operands.push_back(V);
8154returnfalse;
8155 });
8156}
8157
8158boolllvm::mustTriggerUB(constInstruction *I,
8159constSmallPtrSetImpl<const Value *> &KnownPoison) {
8160returnhandleGuaranteedNonPoisonOps(
8161I, [&](constValue *V) {return KnownPoison.count(V); });
8162}
8163
8164staticboolprogramUndefinedIfUndefOrPoison(constValue *V,
8165boolPoisonOnly) {
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.
8173constBasicBlock *BB =nullptr;
8174BasicBlock::const_iterator Begin;
8175if (constauto *Inst = dyn_cast<Instruction>(V)) {
8176 BB = Inst->getParent();
8177 Begin = Inst->getIterator();
8178 Begin++;
8179 }elseif (constauto *Arg = dyn_cast<Argument>(V)) {
8180if (Arg->getParent()->isDeclaration())
8181returnfalse;
8182 BB = &Arg->getParent()->getEntryBlock();
8183 Begin = BB->begin();
8184 }else {
8185returnfalse;
8186 }
8187
8188// Limit number of instructions we look at, to avoid scanning through large
8189// blocks. The current limit is chosen arbitrarily.
8190unsigned ScanLimit = 32;
8191BasicBlock::const_iteratorEnd = BB->end();
8192
8193if (!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
8198for (constauto &I :make_range(Begin,End)) {
8199if (isa<DbgInfoIntrinsic>(I))
8200continue;
8201if (--ScanLimit == 0)
8202break;
8203
8204if (handleGuaranteedWellDefinedOps(&I, [V](constValue *WellDefinedOp) {
8205return WellDefinedOp == V;
8206 }))
8207returntrue;
8208
8209if (!isGuaranteedToTransferExecutionToSuccessor(&I))
8210break;
8211 }
8212returnfalse;
8213 }
8214
8215// Set of instructions that we have proved will yield poison if Inst
8216// does.
8217SmallSet<const Value *, 16> YieldsPoison;
8218SmallSet<const BasicBlock *, 4> Visited;
8219
8220 YieldsPoison.insert(V);
8221 Visited.insert(BB);
8222
8223while (true) {
8224for (constauto &I :make_range(Begin,End)) {
8225if (isa<DbgInfoIntrinsic>(I))
8226continue;
8227if (--ScanLimit == 0)
8228returnfalse;
8229if (mustTriggerUB(&I, YieldsPoison))
8230returntrue;
8231if (!isGuaranteedToTransferExecutionToSuccessor(&I))
8232returnfalse;
8233
8234// If an operand is poison and propagates it, mark I as yielding poison.
8235for (constUse &Op :I.operands()) {
8236if (YieldsPoison.count(Op) &&propagatesPoison(Op)) {
8237 YieldsPoison.insert(&I);
8238break;
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).
8245if (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();
8253if (!BB || !Visited.insert(BB).second)
8254break;
8255
8256 Begin = BB->getFirstNonPHIIt();
8257End = BB->end();
8258 }
8259returnfalse;
8260}
8261
8262boolllvm::programUndefinedIfUndefOrPoison(constInstruction *Inst) {
8263 return ::programUndefinedIfUndefOrPoison(Inst,false);
8264}
8265
8266boolllvm::programUndefinedIfPoison(constInstruction *Inst) {
8267 return ::programUndefinedIfUndefOrPoison(Inst,true);
8268}
8269
8270staticboolisKnownNonNaN(constValue *V,FastMathFlags FMF) {
8271if (FMF.noNaNs())
8272returntrue;
8273
8274if (auto *C = dyn_cast<ConstantFP>(V))
8275return !C->isNaN();
8276
8277if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8278if (!C->getElementType()->isFloatingPointTy())
8279returnfalse;
8280for (unsignedI = 0, E =C->getNumElements();I < E; ++I) {
8281if (C->getElementAsAPFloat(I).isNaN())
8282returnfalse;
8283 }
8284returntrue;
8285 }
8286
8287if (isa<ConstantAggregateZero>(V))
8288returntrue;
8289
8290returnfalse;
8291}
8292
8293staticboolisKnownNonZero(constValue *V) {
8294if (auto *C = dyn_cast<ConstantFP>(V))
8295return !C->isZero();
8296
8297if (auto *C = dyn_cast<ConstantDataVector>(V)) {
8298if (!C->getElementType()->isFloatingPointTy())
8299returnfalse;
8300for (unsignedI = 0, E =C->getNumElements();I < E; ++I) {
8301if (C->getElementAsAPFloat(I).isZero())
8302returnfalse;
8303 }
8304returntrue;
8305 }
8306
8307returnfalse;
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.
8314staticSelectPatternResultmatchFastFloatClamp(CmpInst::Predicate Pred,
8315Value *CmpLHS,Value *CmpRHS,
8316Value *TrueVal,Value *FalseVal,
8317Value *&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:
8324if (CmpRHS == FalseVal) {
8325std::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.
8330LHS = TrueVal;
8331RHS = FalseVal;
8332
8333constAPFloat *FC1;
8334if (CmpRHS != TrueVal || !match(CmpRHS,m_APFloat(FC1)) || !FC1->isFinite())
8335return {SPF_UNKNOWN,SPNB_NA,false};
8336
8337constAPFloat *FC2;
8338switch (Pred) {
8339caseCmpInst::FCMP_OLT:
8340caseCmpInst::FCMP_OLE:
8341caseCmpInst::FCMP_ULT:
8342caseCmpInst::FCMP_ULE:
8343if (match(FalseVal,m_OrdOrUnordFMin(m_Specific(CmpLHS),m_APFloat(FC2))) &&
8344 *FC1 < *FC2)
8345return {SPF_FMAXNUM,SPNB_RETURNS_ANY,false};
8346break;
8347caseCmpInst::FCMP_OGT:
8348caseCmpInst::FCMP_OGE:
8349caseCmpInst::FCMP_UGT:
8350caseCmpInst::FCMP_UGE:
8351if (match(FalseVal,m_OrdOrUnordFMax(m_Specific(CmpLHS),m_APFloat(FC2))) &&
8352 *FC1 > *FC2)
8353return {SPF_FMINNUM,SPNB_RETURNS_ANY,false};
8354break;
8355default:
8356break;
8357 }
8358
8359return {SPF_UNKNOWN,SPNB_NA,false};
8360}
8361
8362/// Recognize variations of:
8363/// CLAMP(v,l,h) ==> ((v) < (l) ? (l) : ((v) > (h) ? (h) : (v)))
8364staticSelectPatternResultmatchClamp(CmpInst::Predicate Pred,
8365Value *CmpLHS,Value *CmpRHS,
8366Value *TrueVal,Value *FalseVal) {
8367// Swap the select operands and predicate to match the patterns below.
8368if (CmpRHS != TrueVal) {
8369 Pred =ICmpInst::getSwappedPredicate(Pred);
8370std::swap(TrueVal, FalseVal);
8371 }
8372constAPInt *C1;
8373if (CmpRHS == TrueVal &&match(CmpRHS,m_APInt(C1))) {
8374constAPInt *C2;
8375// (X <s C1) ? C1 : SMIN(X, C2) ==> SMAX(SMIN(X, C2), C1)
8376if (match(FalseVal,m_SMin(m_Specific(CmpLHS),m_APInt(C2))) &&
8377 C1->slt(*C2) && Pred ==CmpInst::ICMP_SLT)
8378return {SPF_SMAX,SPNB_NA,false};
8379
8380// (X >s C1) ? C1 : SMAX(X, C2) ==> SMIN(SMAX(X, C2), C1)
8381if (match(FalseVal,m_SMax(m_Specific(CmpLHS),m_APInt(C2))) &&
8382 C1->sgt(*C2) && Pred ==CmpInst::ICMP_SGT)
8383return {SPF_SMIN,SPNB_NA,false};
8384
8385// (X <u C1) ? C1 : UMIN(X, C2) ==> UMAX(UMIN(X, C2), C1)
8386if (match(FalseVal,m_UMin(m_Specific(CmpLHS),m_APInt(C2))) &&
8387 C1->ult(*C2) && Pred ==CmpInst::ICMP_ULT)
8388return {SPF_UMAX,SPNB_NA,false};
8389
8390// (X >u C1) ? C1 : UMAX(X, C2) ==> UMIN(UMAX(X, C2), C1)
8391if (match(FalseVal,m_UMax(m_Specific(CmpLHS),m_APInt(C2))) &&
8392 C1->ugt(*C2) && Pred ==CmpInst::ICMP_UGT)
8393return {SPF_UMIN,SPNB_NA,false};
8394 }
8395return {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))
8400staticSelectPatternResultmatchMinMaxOfMinMax(CmpInst::Predicate Pred,
8401Value *CmpLHS,Value *CmpRHS,
8402Value *TVal,Value *FVal,
8403unsignedDepth) {
8404// TODO: Allow FP min/max with nnan/nsz.
8405assert(CmpInst::isIntPredicate(Pred) &&"Expected integer comparison");
8406
8407Value *A =nullptr, *B =nullptr;
8408SelectPatternResult L =matchSelectPattern(TVal,A,B,nullptr,Depth + 1);
8409if (!SelectPatternResult::isMinOrMax(L.Flavor))
8410return {SPF_UNKNOWN,SPNB_NA,false};
8411
8412Value *C =nullptr, *D =nullptr;
8413SelectPatternResult R =matchSelectPattern(FVal,C,D,nullptr,Depth + 1);
8414if (L.Flavor != R.Flavor)
8415return {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.
8420switch (L.Flavor) {
8421caseSPF_SMIN:
8422if (Pred ==ICmpInst::ICMP_SGT || Pred ==ICmpInst::ICMP_SGE) {
8423 Pred =ICmpInst::getSwappedPredicate(Pred);
8424std::swap(CmpLHS, CmpRHS);
8425 }
8426if (Pred ==ICmpInst::ICMP_SLT || Pred ==ICmpInst::ICMP_SLE)
8427break;
8428return {SPF_UNKNOWN,SPNB_NA,false};
8429caseSPF_SMAX:
8430if (Pred ==ICmpInst::ICMP_SLT || Pred ==ICmpInst::ICMP_SLE) {
8431 Pred =ICmpInst::getSwappedPredicate(Pred);
8432std::swap(CmpLHS, CmpRHS);
8433 }
8434if (Pred ==ICmpInst::ICMP_SGT || Pred ==ICmpInst::ICMP_SGE)
8435break;
8436return {SPF_UNKNOWN,SPNB_NA,false};
8437caseSPF_UMIN:
8438if (Pred ==ICmpInst::ICMP_UGT || Pred ==ICmpInst::ICMP_UGE) {
8439 Pred =ICmpInst::getSwappedPredicate(Pred);
8440std::swap(CmpLHS, CmpRHS);
8441 }
8442if (Pred ==ICmpInst::ICMP_ULT || Pred ==ICmpInst::ICMP_ULE)
8443break;
8444return {SPF_UNKNOWN,SPNB_NA,false};
8445caseSPF_UMAX:
8446if (Pred ==ICmpInst::ICMP_ULT || Pred ==ICmpInst::ICMP_ULE) {
8447 Pred =ICmpInst::getSwappedPredicate(Pred);
8448std::swap(CmpLHS, CmpRHS);
8449 }
8450if (Pred ==ICmpInst::ICMP_UGT || Pred ==ICmpInst::ICMP_UGE)
8451break;
8452return {SPF_UNKNOWN,SPNB_NA,false};
8453default:
8454return {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))
8463if (D ==B) {
8464if ((CmpLHS ==A && CmpRHS ==C) || (match(C,m_Not(m_Specific(CmpLHS))) &&
8465match(A,m_Not(m_Specific(CmpRHS)))))
8466return {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))
8470if (C ==B) {
8471if ((CmpLHS ==A && CmpRHS ==D) || (match(D,m_Not(m_Specific(CmpLHS))) &&
8472match(A,m_Not(m_Specific(CmpRHS)))))
8473return {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))
8477if (D ==A) {
8478if ((CmpLHS ==B && CmpRHS ==C) || (match(C,m_Not(m_Specific(CmpLHS))) &&
8479match(B,m_Not(m_Specific(CmpRHS)))))
8480return {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))
8484if (C ==A) {
8485if ((CmpLHS ==B && CmpRHS ==D) || (match(D,m_Not(m_Specific(CmpLHS))) &&
8486match(B,m_Not(m_Specific(CmpRHS)))))
8487return {L.Flavor,SPNB_NA,false};
8488 }
8489
8490return {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.
8496staticValue *getNotValue(Value *V) {
8497Value *NotV;
8498if (match(V,m_Not(m_Value(NotV))))
8499return NotV;
8500
8501constAPInt *C;
8502if (match(V,m_APInt(C)))
8503return ConstantInt::get(V->getType(), ~(*C));
8504
8505returnnullptr;
8506}
8507
8508/// Match non-obvious integer minimum and maximum sequences.
8509staticSelectPatternResultmatchMinMax(CmpInst::Predicate Pred,
8510Value *CmpLHS,Value *CmpRHS,
8511Value *TrueVal,Value *FalseVal,
8512Value *&LHS,Value *&RHS,
8513unsignedDepth) {
8514// Assume success. If there's no match, callers should not use these anyway.
8515LHS = TrueVal;
8516RHS = FalseVal;
8517
8518SelectPatternResult SPR =matchClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal);
8519if (SPR.Flavor !=SelectPatternFlavor::SPF_UNKNOWN)
8520return SPR;
8521
8522 SPR =matchMinMaxOfMinMax(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal,Depth);
8523if (SPR.Flavor !=SelectPatternFlavor::SPF_UNKNOWN)
8524return 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)
8529if (CmpLHS ==getNotValue(TrueVal) && CmpRHS ==getNotValue(FalseVal)) {
8530switch (Pred) {
8531caseCmpInst::ICMP_SGT:return {SPF_SMIN,SPNB_NA,false};
8532caseCmpInst::ICMP_SLT:return {SPF_SMAX,SPNB_NA,false};
8533caseCmpInst::ICMP_UGT:return {SPF_UMIN,SPNB_NA,false};
8534caseCmpInst::ICMP_ULT:return {SPF_UMAX,SPNB_NA,false};
8535default: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)
8541if (CmpLHS ==getNotValue(FalseVal) && CmpRHS ==getNotValue(TrueVal)) {
8542switch (Pred) {
8543caseCmpInst::ICMP_SGT:return {SPF_SMAX,SPNB_NA,false};
8544caseCmpInst::ICMP_SLT:return {SPF_SMIN,SPNB_NA,false};
8545caseCmpInst::ICMP_UGT:return {SPF_UMAX,SPNB_NA,false};
8546caseCmpInst::ICMP_ULT:return {SPF_UMIN,SPNB_NA,false};
8547default:break;
8548 }
8549 }
8550
8551if (Pred !=CmpInst::ICMP_SGT && Pred !=CmpInst::ICMP_SLT)
8552return {SPF_UNKNOWN,SPNB_NA,false};
8553
8554constAPInt *C1;
8555if (!match(CmpRHS,m_APInt(C1)))
8556return {SPF_UNKNOWN,SPNB_NA,false};
8557
8558// An unsigned min/max can be written with a signed compare.
8559constAPInt *C2;
8560if ((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
8565if (Pred ==CmpInst::ICMP_SLT && C1->isZero() && C2->isMaxSignedValue())
8566return {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
8571if (Pred ==CmpInst::ICMP_SGT && C1->isAllOnes() && C2->isMinSignedValue())
8572return {CmpLHS == FalseVal ?SPF_UMAX :SPF_UMIN,SPNB_NA,false};
8573 }
8574
8575return {SPF_UNKNOWN,SPNB_NA,false};
8576}
8577
8578boolllvm::isKnownNegation(constValue *X,constValue *Y,bool NeedNSW,
8579bool AllowPoison) {
8580assert(X &&Y &&"Invalid operand");
8581
8582auto IsNegationOf = [&](constValue *X,constValue *Y) {
8583if (!match(X,m_Neg(m_Specific(Y))))
8584returnfalse;
8585
8586auto *BO = cast<BinaryOperator>(X);
8587if (NeedNSW && !BO->hasNoSignedWrap())
8588returnfalse;
8589
8590auto *Zero = cast<Constant>(BO->getOperand(0));
8591if (!AllowPoison && !Zero->isNullValue())
8592returnfalse;
8593
8594returntrue;
8595 };
8596
8597// X = -Y or Y = -X
8598if (IsNegationOf(X,Y) || IsNegationOf(Y,X))
8599returntrue;
8600
8601// X = sub (A, B), Y = sub (B, A) || X = sub nsw (A, B), Y = sub nsw (B, A)
8602Value *A, *B;
8603return (!NeedNSW && (match(X,m_Sub(m_Value(A),m_Value(B))) &&
8604match(Y,m_Sub(m_Specific(B),m_Specific(A))))) ||
8605 (NeedNSW && (match(X,m_NSWSub(m_Value(A),m_Value(B))) &&
8606match(Y,m_NSWSub(m_Specific(B),m_Specific(A)))));
8607}
8608
8609boolllvm::isKnownInversion(constValue *X,constValue *Y) {
8610// Handle X = icmp pred A, B, Y = icmp pred A, C.
8611Value *A, *B, *C;
8612CmpPredicate Pred1, Pred2;
8613if (!match(X,m_ICmp(Pred1,m_Value(A),m_Value(B))) ||
8614 !match(Y,m_c_ICmp(Pred2,m_Specific(A),m_Value(C))))
8615returnfalse;
8616
8617// They must both have samesign flag or not.
8618if (cast<ICmpInst>(X)->hasSameSign() != cast<ICmpInst>(Y)->hasSameSign())
8619returnfalse;
8620
8621if (B ==C)
8622return Pred1 ==ICmpInst::getInversePredicate(Pred2);
8623
8624// Try to infer the relationship from constant ranges.
8625constAPInt *RHSC1, *RHSC2;
8626if (!match(B,m_APInt(RHSC1)) || !match(C,m_APInt(RHSC2)))
8627returnfalse;
8628
8629// Sign bits of two RHSCs should match.
8630if (cast<ICmpInst>(X)->hasSameSign() &&
8631 RHSC1->isNonNegative() != RHSC2->isNonNegative())
8632returnfalse;
8633
8634constauto CR1 =ConstantRange::makeExactICmpRegion(Pred1, *RHSC1);
8635constauto CR2 =ConstantRange::makeExactICmpRegion(Pred2, *RHSC2);
8636
8637return CR1.inverse() == CR2;
8638}
8639
8640SelectPatternResultllvm::getSelectPattern(CmpInst::Predicate Pred,
8641SelectPatternNaNBehavior NaNBehavior,
8642bool Ordered) {
8643switch (Pred) {
8644default:
8645return {SPF_UNKNOWN,SPNB_NA,false};// Equality.
8646caseICmpInst::ICMP_UGT:
8647caseICmpInst::ICMP_UGE:
8648return {SPF_UMAX,SPNB_NA,false};
8649caseICmpInst::ICMP_SGT:
8650caseICmpInst::ICMP_SGE:
8651return {SPF_SMAX,SPNB_NA,false};
8652caseICmpInst::ICMP_ULT:
8653caseICmpInst::ICMP_ULE:
8654return {SPF_UMIN,SPNB_NA,false};
8655caseICmpInst::ICMP_SLT:
8656caseICmpInst::ICMP_SLE:
8657return {SPF_SMIN,SPNB_NA,false};
8658caseFCmpInst::FCMP_UGT:
8659caseFCmpInst::FCMP_UGE:
8660caseFCmpInst::FCMP_OGT:
8661caseFCmpInst::FCMP_OGE:
8662return {SPF_FMAXNUM, NaNBehavior, Ordered};
8663caseFCmpInst::FCMP_ULT:
8664caseFCmpInst::FCMP_ULE:
8665caseFCmpInst::FCMP_OLT:
8666caseFCmpInst::FCMP_OLE:
8667return {SPF_FMINNUM, NaNBehavior, Ordered};
8668 }
8669}
8670
8671std::optional<std::pair<CmpPredicate, Constant *>>
8672llvm::getFlippedStrictnessPredicateAndConstant(CmpPredicate Pred,Constant *C) {
8673assert(ICmpInst::isRelational(Pred) &&ICmpInst::isIntPredicate(Pred) &&
8674"Only for relational integer predicates.");
8675if (isa<UndefValue>(C))
8676return std::nullopt;
8677
8678Type *Type =C->getType();
8679bool IsSigned =ICmpInst::isSigned(Pred);
8680
8681CmpInst::Predicate UnsignedPred =ICmpInst::getUnsignedPredicate(Pred);
8682bool 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.
8687auto ConstantIsOk = [WillIncrement, IsSigned](ConstantInt *C) {
8688return WillIncrement ? !C->isMaxValue(IsSigned) : !C->isMinValue(IsSigned);
8689 };
8690
8691Constant *SafeReplacementConstant =nullptr;
8692if (auto *CI = dyn_cast<ConstantInt>(C)) {
8693// Bail out if the constant can't be safely incremented/decremented.
8694if (!ConstantIsOk(CI))
8695return std::nullopt;
8696 }elseif (auto *FVTy = dyn_cast<FixedVectorType>(Type)) {
8697unsigned NumElts = FVTy->getNumElements();
8698for (unsigned i = 0; i != NumElts; ++i) {
8699Constant *Elt =C->getAggregateElement(i);
8700if (!Elt)
8701return std::nullopt;
8702
8703if (isa<UndefValue>(Elt))
8704continue;
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.
8708auto *CI = dyn_cast<ConstantInt>(Elt);
8709if (!CI || !ConstantIsOk(CI))
8710return std::nullopt;
8711
8712if (!SafeReplacementConstant)
8713 SafeReplacementConstant = CI;
8714 }
8715 }elseif (isa<VectorType>(C->getType())) {
8716// Handle scalable splat
8717Value *SplatC =C->getSplatValue();
8718auto *CI = dyn_cast_or_null<ConstantInt>(SplatC);
8719// Bail out if the constant can't be safely incremented/decremented.
8720if (!CI || !ConstantIsOk(CI))
8721return std::nullopt;
8722 }else {
8723// ConstantExpr?
8724return 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.
8731if (C->containsUndefOrPoisonElement()) {
8732assert(SafeReplacementConstant &&"Replacement constant not set");
8733C =Constant::replaceUndefsWith(C, SafeReplacementConstant);
8734 }
8735
8736CmpInst::Predicate NewPred =CmpInst::getFlippedStrictnessPredicate(Pred);
8737
8738// Increment or decrement the constant.
8739Constant *OneOrNegOne = ConstantInt::get(Type, WillIncrement ? 1 : -1,true);
8740Constant *NewC =ConstantExpr::getAdd(C, OneOrNegOne);
8741
8742return std::make_pair(NewPred, NewC);
8743}
8744
8745staticSelectPatternResultmatchSelectPattern(CmpInst::Predicate Pred,
8746FastMathFlags FMF,
8747Value *CmpLHS,Value *CmpRHS,
8748Value *TrueVal,Value *FalseVal,
8749Value *&LHS,Value *&RHS,
8750unsignedDepth) {
8751bool HasMismatchedZeros =false;
8752if (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.
8757Value *OutputZeroVal =nullptr;
8758if (match(TrueVal,m_AnyZeroFP()) && !match(FalseVal,m_AnyZeroFP()) &&
8759 !cast<Constant>(TrueVal)->containsUndefOrPoisonElement())
8760 OutputZeroVal = TrueVal;
8761elseif (match(FalseVal,m_AnyZeroFP()) && !match(TrueVal,m_AnyZeroFP()) &&
8762 !cast<Constant>(FalseVal)->containsUndefOrPoisonElement())
8763 OutputZeroVal = FalseVal;
8764
8765if (OutputZeroVal) {
8766if (match(CmpLHS,m_AnyZeroFP()) && CmpLHS != OutputZeroVal) {
8767 HasMismatchedZeros =true;
8768 CmpLHS = OutputZeroVal;
8769 }
8770if (match(CmpRHS,m_AnyZeroFP()) && CmpRHS != OutputZeroVal) {
8771 HasMismatchedZeros =true;
8772 CmpRHS = OutputZeroVal;
8773 }
8774 }
8775 }
8776
8777LHS = CmpLHS;
8778RHS = 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.
8785switch (Pred) {
8786default:break;
8787caseCmpInst::FCMP_OGT:caseCmpInst::FCMP_OLT:
8788caseCmpInst::FCMP_UGT:caseCmpInst::FCMP_ULT:
8789if (!HasMismatchedZeros)
8790break;
8791 [[fallthrough]];
8792caseCmpInst::FCMP_OGE:caseCmpInst::FCMP_OLE:
8793caseCmpInst::FCMP_UGE:caseCmpInst::FCMP_ULE:
8794if (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8795 !isKnownNonZero(CmpRHS))
8796return {SPF_UNKNOWN,SPNB_NA,false};
8797 }
8798
8799SelectPatternNaNBehavior NaNBehavior =SPNB_NA;
8800bool 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.
8807if (CmpInst::isFPPredicate(Pred)) {
8808bool LHSSafe =isKnownNonNaN(CmpLHS, FMF);
8809bool RHSSafe =isKnownNonNaN(CmpRHS, FMF);
8810
8811if (LHSSafe && RHSSafe) {
8812// Both operands are known non-NaN.
8813 NaNBehavior =SPNB_RETURNS_ANY;
8814 }elseif (CmpInst::isOrdered(Pred)) {
8815// An ordered comparison will return false when given a NaN, so it
8816// returns the RHS.
8817 Ordered =true;
8818if (LHSSafe)
8819// LHS is non-NaN, so if RHS is NaN then NaN will be returned.
8820 NaNBehavior =SPNB_RETURNS_NAN;
8821elseif (RHSSafe)
8822 NaNBehavior =SPNB_RETURNS_OTHER;
8823else
8824// Completely unsafe.
8825return {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.
8830if (LHSSafe)
8831// LHS is non-NaN, so if RHS is NaN then non-NaN will be returned.
8832 NaNBehavior =SPNB_RETURNS_OTHER;
8833elseif (RHSSafe)
8834 NaNBehavior =SPNB_RETURNS_NAN;
8835else
8836// Completely unsafe.
8837return {SPF_UNKNOWN,SPNB_NA,false};
8838 }
8839 }
8840
8841if (TrueVal == CmpRHS && FalseVal == CmpLHS) {
8842std::swap(CmpLHS, CmpRHS);
8843 Pred =CmpInst::getSwappedPredicate(Pred);
8844if (NaNBehavior ==SPNB_RETURNS_NAN)
8845 NaNBehavior =SPNB_RETURNS_OTHER;
8846elseif (NaNBehavior ==SPNB_RETURNS_OTHER)
8847 NaNBehavior =SPNB_RETURNS_NAN;
8848 Ordered = !Ordered;
8849 }
8850
8851// ([if]cmp X, Y) ? X : Y
8852if (TrueVal == CmpLHS && FalseVal == CmpRHS)
8853returngetSelectPattern(Pred, NaNBehavior, Ordered);
8854
8855if (isKnownNegation(TrueVal, FalseVal)) {
8856// Sign-extending LHS does not change its sign, so TrueVal/FalseVal can
8857// match against either LHS or sext(LHS).
8858auto MaybeSExtCmpLHS =
8859m_CombineOr(m_Specific(CmpLHS),m_SExt(m_Specific(CmpLHS)));
8860auto ZeroOrAllOnes =m_CombineOr(m_ZeroInt(),m_AllOnes());
8861auto ZeroOrOne =m_CombineOr(m_ZeroInt(),m_One());
8862if (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'.
8865LHS = TrueVal;
8866RHS = FalseVal;
8867if (match(CmpLHS,m_Neg(m_Specific(FalseVal))))
8868std::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)
8872if (Pred ==ICmpInst::ICMP_SGT &&match(CmpRHS, ZeroOrAllOnes))
8873return {SPF_ABS,SPNB_NA,false};
8874
8875// (X >=s 0) ? X : -X or (X >=s 1) ? X : -X --> ABS(X)
8876if (Pred ==ICmpInst::ICMP_SGE &&match(CmpRHS, ZeroOrOne))
8877return {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)
8881if (Pred ==ICmpInst::ICMP_SLT &&match(CmpRHS, ZeroOrOne))
8882return {SPF_NABS,SPNB_NA,false};
8883 }
8884elseif (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'.
8887LHS = FalseVal;
8888RHS = TrueVal;
8889if (match(CmpLHS,m_Neg(m_Specific(TrueVal))))
8890std::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)
8894if (Pred ==ICmpInst::ICMP_SGT &&match(CmpRHS, ZeroOrAllOnes))
8895return {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)
8899if (Pred ==ICmpInst::ICMP_SLT &&match(CmpRHS, ZeroOrOne))
8900return {SPF_ABS,SPNB_NA,false};
8901 }
8902 }
8903
8904if (CmpInst::isIntPredicate(Pred))
8905returnmatchMinMax(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.
8910if (NaNBehavior !=SPNB_RETURNS_ANY ||
8911 (!FMF.noSignedZeros() && !isKnownNonZero(CmpLHS) &&
8912 !isKnownNonZero(CmpRHS)))
8913return {SPF_UNKNOWN,SPNB_NA,false};
8914
8915returnmatchFastFloatClamp(Pred, CmpLHS, CmpRHS, TrueVal, FalseVal,LHS,RHS);
8916}
8917
8918staticValue *lookThroughCastConst(CmpInst *CmpI,Type *SrcTy,Constant *C,
8919Instruction::CastOps *CastOp) {
8920constDataLayout &DL = CmpI->getDataLayout();
8921
8922Constant *CastedTo =nullptr;
8923switch (*CastOp) {
8924case Instruction::ZExt:
8925if (CmpI->isUnsigned())
8926 CastedTo =ConstantExpr::getTrunc(C, SrcTy);
8927break;
8928case Instruction::SExt:
8929if (CmpI->isSigned())
8930 CastedTo =ConstantExpr::getTrunc(C, SrcTy,true);
8931break;
8932case Instruction::Trunc:
8933Constant *CmpConst;
8934if (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 {
8959unsigned ExtOp = CmpI->isSigned() ? Instruction::SExt : Instruction::ZExt;
8960 CastedTo =ConstantFoldCastOperand(ExtOp,C, SrcTy,DL);
8961 }
8962break;
8963case Instruction::FPTrunc:
8964 CastedTo =ConstantFoldCastOperand(Instruction::FPExt,C, SrcTy,DL);
8965break;
8966case Instruction::FPExt:
8967 CastedTo =ConstantFoldCastOperand(Instruction::FPTrunc,C, SrcTy,DL);
8968break;
8969case Instruction::FPToUI:
8970 CastedTo =ConstantFoldCastOperand(Instruction::UIToFP,C, SrcTy,DL);
8971break;
8972case Instruction::FPToSI:
8973 CastedTo =ConstantFoldCastOperand(Instruction::SIToFP,C, SrcTy,DL);
8974break;
8975case Instruction::UIToFP:
8976 CastedTo =ConstantFoldCastOperand(Instruction::FPToUI,C, SrcTy,DL);
8977break;
8978case Instruction::SIToFP:
8979 CastedTo =ConstantFoldCastOperand(Instruction::FPToSI,C, SrcTy,DL);
8980break;
8981default:
8982break;
8983 }
8984
8985if (!CastedTo)
8986returnnullptr;
8987
8988// Make sure the cast doesn't lose any information.
8989Constant *CastedBack =
8990ConstantFoldCastOperand(*CastOp, CastedTo,C->getType(),DL);
8991if (CastedBack && CastedBack !=C)
8992returnnullptr;
8993
8994return 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.
9013staticValue *lookThroughCast(CmpInst *CmpI,Value *V1,Value *V2,
9014Instruction::CastOps *CastOp) {
9015auto *Cast1 = dyn_cast<CastInst>(V1);
9016if (!Cast1)
9017returnnullptr;
9018
9019 *CastOp = Cast1->getOpcode();
9020Type *SrcTy = Cast1->getSrcTy();
9021if (auto *Cast2 = dyn_cast<CastInst>(V2)) {
9022// If V1 and V2 are both the same cast from the same type, look through V1.
9023if (*CastOp == Cast2->getOpcode() && SrcTy == Cast2->getSrcTy())
9024return Cast2->getOperand(0);
9025returnnullptr;
9026 }
9027
9028auto *C = dyn_cast<Constant>(V2);
9029if (C)
9030returnlookThroughCastConst(CmpI, SrcTy,C, CastOp);
9031
9032Value *CastedTo =nullptr;
9033if (*CastOp == Instruction::Trunc) {
9034if (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
9046assert(V2->getType() == Cast1->getType() &&
9047"V2 and Cast1 should be the same type.");
9048 CastedTo = CmpI->getOperand(1);
9049 }
9050 }
9051
9052return CastedTo;
9053}
9054SelectPatternResultllvm::matchSelectPattern(Value *V,Value *&LHS,Value *&RHS,
9055Instruction::CastOps *CastOp,
9056unsignedDepth) {
9057if (Depth >=MaxAnalysisRecursionDepth)
9058return {SPF_UNKNOWN,SPNB_NA,false};
9059
9060SelectInst *SI = dyn_cast<SelectInst>(V);
9061if (!SI)return {SPF_UNKNOWN,SPNB_NA,false};
9062
9063CmpInst *CmpI = dyn_cast<CmpInst>(SI->getCondition());
9064if (!CmpI)return {SPF_UNKNOWN,SPNB_NA,false};
9065
9066Value *TrueVal = SI->getTrueValue();
9067Value *FalseVal = SI->getFalseValue();
9068
9069returnllvm::matchDecomposedSelectPattern(CmpI, TrueVal, FalseVal,LHS,RHS,
9070 CastOp,Depth);
9071}
9072
9073SelectPatternResultllvm::matchDecomposedSelectPattern(
9074CmpInst *CmpI,Value *TrueVal,Value *FalseVal,Value *&LHS,Value *&RHS,
9075Instruction::CastOps *CastOp,unsignedDepth) {
9076CmpInst::Predicate Pred = CmpI->getPredicate();
9077Value *CmpLHS = CmpI->getOperand(0);
9078Value *CmpRHS = CmpI->getOperand(1);
9079FastMathFlags FMF;
9080if (isa<FPMathOperator>(CmpI))
9081 FMF = CmpI->getFastMathFlags();
9082
9083// Bail out early.
9084if (CmpI->isEquality())
9085return {SPF_UNKNOWN,SPNB_NA,false};
9086
9087// Deal with type mismatches.
9088if (CastOp && CmpLHS->getType() != TrueVal->getType()) {
9089if (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.
9092if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9093 FMF.setNoSignedZeros();
9094 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9095 cast<CastInst>(TrueVal)->getOperand(0),C,
9096LHS,RHS,Depth);
9097 }
9098if (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.
9101if (*CastOp == Instruction::FPToSI || *CastOp == Instruction::FPToUI)
9102 FMF.setNoSignedZeros();
9103 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS,
9104C, cast<CastInst>(FalseVal)->getOperand(0),
9105LHS,RHS,Depth);
9106 }
9107 }
9108 return ::matchSelectPattern(Pred, FMF, CmpLHS, CmpRHS, TrueVal, FalseVal,
9109LHS,RHS,Depth);
9110}
9111
9112CmpInst::Predicatellvm::getMinMaxPred(SelectPatternFlavor SPF,bool Ordered) {
9113if (SPF ==SPF_SMIN)returnICmpInst::ICMP_SLT;
9114if (SPF ==SPF_UMIN)returnICmpInst::ICMP_ULT;
9115if (SPF ==SPF_SMAX)returnICmpInst::ICMP_SGT;
9116if (SPF ==SPF_UMAX)returnICmpInst::ICMP_UGT;
9117if (SPF ==SPF_FMINNUM)
9118return Ordered ?FCmpInst::FCMP_OLT :FCmpInst::FCMP_ULT;
9119if (SPF ==SPF_FMAXNUM)
9120return Ordered ?FCmpInst::FCMP_OGT :FCmpInst::FCMP_UGT;
9121llvm_unreachable("unhandled!");
9122}
9123
9124Intrinsic::IDllvm::getMinMaxIntrinsic(SelectPatternFlavor SPF) {
9125switch (SPF) {
9126caseSelectPatternFlavor::SPF_UMIN:
9127return Intrinsic::umin;
9128caseSelectPatternFlavor::SPF_UMAX:
9129return Intrinsic::umax;
9130caseSelectPatternFlavor::SPF_SMIN:
9131return Intrinsic::smin;
9132caseSelectPatternFlavor::SPF_SMAX:
9133return Intrinsic::smax;
9134default:
9135llvm_unreachable("Unexpected SPF");
9136 }
9137}
9138
9139SelectPatternFlavorllvm::getInverseMinMaxFlavor(SelectPatternFlavor SPF) {
9140if (SPF ==SPF_SMIN)returnSPF_SMAX;
9141if (SPF ==SPF_UMIN)returnSPF_UMAX;
9142if (SPF ==SPF_SMAX)returnSPF_SMIN;
9143if (SPF ==SPF_UMAX)returnSPF_UMIN;
9144llvm_unreachable("unhandled!");
9145}
9146
9147Intrinsic::IDllvm::getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID) {
9148switch (MinMaxID) {
9149case Intrinsic::smax:return Intrinsic::smin;
9150case Intrinsic::smin:return Intrinsic::smax;
9151case Intrinsic::umax:return Intrinsic::umin;
9152case 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.
9155case Intrinsic::maximum:return Intrinsic::minimum;
9156case Intrinsic::minimum:return Intrinsic::maximum;
9157case Intrinsic::maxnum:return Intrinsic::minnum;
9158case Intrinsic::minnum:return Intrinsic::maxnum;
9159default:llvm_unreachable("Unexpected intrinsic");
9160 }
9161}
9162
9163APIntllvm::getMinMaxLimit(SelectPatternFlavor SPF,unsignedBitWidth) {
9164switch (SPF) {
9165caseSPF_SMAX:returnAPInt::getSignedMaxValue(BitWidth);
9166caseSPF_SMIN:returnAPInt::getSignedMinValue(BitWidth);
9167caseSPF_UMAX:returnAPInt::getMaxValue(BitWidth);
9168caseSPF_UMIN:returnAPInt::getMinValue(BitWidth);
9169default:llvm_unreachable("Unexpected flavor");
9170 }
9171}
9172
9173std::pair<Intrinsic::ID, bool>
9174llvm::canConvertToMinOrMaxIntrinsic(ArrayRef<Value *> VL) {
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.
9178bool AllCmpSingleUse =true;
9179SelectPatternResult SelectPattern;
9180 SelectPattern.Flavor =SPF_UNKNOWN;
9181if (all_of(VL, [&SelectPattern, &AllCmpSingleUse](Value *I) {
9182Value *LHS, *RHS;
9183auto CurrentPattern =matchSelectPattern(I,LHS,RHS);
9184if (!SelectPatternResult::isMinOrMax(CurrentPattern.Flavor))
9185returnfalse;
9186if (SelectPattern.Flavor !=SPF_UNKNOWN &&
9187 SelectPattern.Flavor != CurrentPattern.Flavor)
9188returnfalse;
9189 SelectPattern = CurrentPattern;
9190 AllCmpSingleUse &=
9191match(I,m_Select(m_OneUse(m_Value()),m_Value(),m_Value()));
9192returntrue;
9193 })) {
9194switch (SelectPattern.Flavor) {
9195caseSPF_SMIN:
9196return {Intrinsic::smin, AllCmpSingleUse};
9197caseSPF_UMIN:
9198return {Intrinsic::umin, AllCmpSingleUse};
9199caseSPF_SMAX:
9200return {Intrinsic::smax, AllCmpSingleUse};
9201caseSPF_UMAX:
9202return {Intrinsic::umax, AllCmpSingleUse};
9203caseSPF_FMAXNUM:
9204return {Intrinsic::maxnum, AllCmpSingleUse};
9205caseSPF_FMINNUM:
9206return {Intrinsic::minnum, AllCmpSingleUse};
9207default:
9208llvm_unreachable("unexpected select pattern flavor");
9209 }
9210 }
9211return {Intrinsic::not_intrinsic,false};
9212}
9213
9214boolllvm::matchSimpleRecurrence(constPHINode *P,BinaryOperator *&BO,
9215Value *&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.
9219if (P->getNumIncomingValues() != 2)
9220returnfalse;
9221
9222for (unsigned i = 0; i != 2; ++i) {
9223Value *L =P->getIncomingValue(i);
9224Value *R =P->getIncomingValue(!i);
9225auto *LU = dyn_cast<BinaryOperator>(L);
9226if (!LU)
9227continue;
9228unsigned Opcode = LU->getOpcode();
9229
9230switch (Opcode) {
9231default:
9232continue;
9233// TODO: Expand list -- xor, gep, uadd.sat etc.
9234case Instruction::LShr:
9235case Instruction::AShr:
9236case Instruction::Shl:
9237case Instruction::Add:
9238case Instruction::Sub:
9239case Instruction::UDiv:
9240case Instruction::URem:
9241case Instruction::And:
9242case Instruction::Or:
9243case Instruction::Mul:
9244case Instruction::FMul: {
9245Value *LL = LU->getOperand(0);
9246Value *LR = LU->getOperand(1);
9247// Find a recurrence.
9248if (LL ==P)
9249 L = LR;
9250elseif (LR ==P)
9251 L = LL;
9252else
9253continue;// Check for recurrence with L and R flipped.
9254
9255break;// 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;
9268returntrue;
9269 }
9270returnfalse;
9271}
9272
9273boolllvm::matchSimpleRecurrence(constBinaryOperator *I,PHINode *&P,
9274Value *&Start,Value *&Step) {
9275BinaryOperator *BO =nullptr;
9276P = dyn_cast<PHINode>(I->getOperand(0));
9277if (!P)
9278P = dyn_cast<PHINode>(I->getOperand(1));
9279returnP &&matchSimpleRecurrence(P, BO, Start, Step) && BO ==I;
9280}
9281
9282/// Return true if "icmp Pred LHS RHS" is always true.
9283staticboolisTruePredicate(CmpInst::Predicate Pred,constValue *LHS,
9284constValue *RHS) {
9285if (ICmpInst::isTrueWhenEqual(Pred) &&LHS ==RHS)
9286returntrue;
9287
9288switch (Pred) {
9289default:
9290returnfalse;
9291
9292caseCmpInst::ICMP_SLE: {
9293constAPInt *C;
9294
9295// LHS s<= LHS +_{nsw} C if C >= 0
9296// LHS s<= LHS | C if C >= 0
9297if (match(RHS,m_NSWAdd(m_Specific(LHS),m_APInt(C))) ||
9298match(RHS,m_Or(m_Specific(LHS),m_APInt(C))))
9299return !C->isNegative();
9300
9301// LHS s<= smax(LHS, V) for any V
9302if (match(RHS,m_c_SMax(m_Specific(LHS),m_Value())))
9303returntrue;
9304
9305// smin(RHS, V) s<= RHS for any V
9306if (match(LHS,m_c_SMin(m_Specific(RHS),m_Value())))
9307returntrue;
9308
9309// Match A to (X +_{nsw} CA) and B to (X +_{nsw} CB)
9310constValue *X;
9311constAPInt *CLHS, *CRHS;
9312if (match(LHS,m_NSWAddLike(m_Value(X),m_APInt(CLHS))) &&
9313match(RHS,m_NSWAddLike(m_Specific(X),m_APInt(CRHS))))
9314return CLHS->sle(*CRHS);
9315
9316returnfalse;
9317 }
9318
9319caseCmpInst::ICMP_ULE: {
9320// LHS u<= LHS +_{nuw} V for any V
9321if (match(RHS,m_c_Add(m_Specific(LHS),m_Value())) &&
9322 cast<OverflowingBinaryOperator>(RHS)->hasNoUnsignedWrap())
9323returntrue;
9324
9325// LHS u<= LHS | V for any V
9326if (match(RHS,m_c_Or(m_Specific(LHS),m_Value())))
9327returntrue;
9328
9329// LHS u<= umax(LHS, V) for any V
9330if (match(RHS,m_c_UMax(m_Specific(LHS),m_Value())))
9331returntrue;
9332
9333// RHS >> V u<= RHS for any V
9334if (match(LHS,m_LShr(m_Specific(RHS),m_Value())))
9335returntrue;
9336
9337// RHS u/ C_ugt_1 u<= RHS
9338constAPInt *C;
9339if (match(LHS,m_UDiv(m_Specific(RHS),m_APInt(C))) &&C->ugt(1))
9340returntrue;
9341
9342// RHS & V u<= RHS for any V
9343if (match(LHS,m_c_And(m_Specific(RHS),m_Value())))
9344returntrue;
9345
9346// umin(RHS, V) u<= RHS for any V
9347if (match(LHS,m_c_UMin(m_Specific(RHS),m_Value())))
9348returntrue;
9349
9350// Match A to (X +_{nuw} CA) and B to (X +_{nuw} CB)
9351constValue *X;
9352constAPInt *CLHS, *CRHS;
9353if (match(LHS,m_NUWAddLike(m_Value(X),m_APInt(CLHS))) &&
9354match(RHS,m_NUWAddLike(m_Specific(X),m_APInt(CRHS))))
9355return CLHS->ule(*CRHS);
9356
9357returnfalse;
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>
9365isImpliedCondOperands(CmpInst::Predicate Pred,constValue *ALHS,
9366constValue *ARHS,constValue *BLHS,constValue *BRHS) {
9367switch (Pred) {
9368default:
9369return std::nullopt;
9370
9371caseCmpInst::ICMP_SLT:
9372caseCmpInst::ICMP_SLE:
9373if (isTruePredicate(CmpInst::ICMP_SLE, BLHS, ALHS) &&
9374isTruePredicate(CmpInst::ICMP_SLE, ARHS, BRHS))
9375returntrue;
9376return std::nullopt;
9377
9378caseCmpInst::ICMP_SGT:
9379caseCmpInst::ICMP_SGE:
9380if (isTruePredicate(CmpInst::ICMP_SLE, ALHS, BLHS) &&
9381isTruePredicate(CmpInst::ICMP_SLE, BRHS, ARHS))
9382returntrue;
9383return std::nullopt;
9384
9385caseCmpInst::ICMP_ULT:
9386caseCmpInst::ICMP_ULE:
9387if (isTruePredicate(CmpInst::ICMP_ULE, BLHS, ALHS) &&
9388isTruePredicate(CmpInst::ICMP_ULE, ARHS, BRHS))
9389returntrue;
9390return std::nullopt;
9391
9392caseCmpInst::ICMP_UGT:
9393caseCmpInst::ICMP_UGE:
9394if (isTruePredicate(CmpInst::ICMP_ULE, ALHS, BLHS) &&
9395isTruePredicate(CmpInst::ICMP_ULE, BRHS, ARHS))
9396returntrue;
9397return 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>
9405isImpliedCondCommonOperandWithCR(CmpPredicate LPred,constConstantRange &LCR,
9406CmpPredicate RPred,constConstantRange &RCR) {
9407auto CRImpliesPred = [&](ConstantRange CR,
9408CmpInst::Predicate Pred) -> std::optional<bool> {
9409// If all true values for lhs and true for rhs, lhs implies rhs
9410if (CR.icmp(Pred, RCR))
9411returntrue;
9412
9413// If there is no overlap, lhs implies not rhs
9414if (CR.icmp(CmpInst::getInversePredicate(Pred), RCR))
9415returnfalse;
9416
9417return std::nullopt;
9418 };
9419if (auto Res = CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9420 RPred))
9421return Res;
9422if (LPred.hasSameSign() ^ RPred.hasSameSign()) {
9423 LPred = LPred.hasSameSign() ?ICmpInst::getFlippedSignednessPredicate(LPred)
9424 :static_cast<CmpInst::Predicate>(LPred);
9425 RPred = RPred.hasSameSign() ?ICmpInst::getFlippedSignednessPredicate(RPred)
9426 :static_cast<CmpInst::Predicate>(RPred);
9427return CRImpliesPred(ConstantRange::makeAllowedICmpRegion(LPred, LCR),
9428 RPred);
9429 }
9430return 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(constICmpInst *LHS,CmpPredicate RPred,constValue *R0,
9438constValue *R1,constDataLayout &DL,bool LHSIsTrue) {
9439Value *L0 =LHS->getOperand(0);
9440Value *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.
9444CmpPredicate 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.
9449if (L0 == R1) {
9450std::swap(R0, R1);
9451 RPred =ICmpInst::getSwappedCmpPredicate(RPred);
9452 }
9453if (R0 == L1) {
9454std::swap(L0, L1);
9455 LPred =ICmpInst::getSwappedCmpPredicate(LPred);
9456 }
9457if (L1 == R1) {
9458// If we have L0 == R0 and L1 == R1, then make L1/R1 the constants.
9459if (L0 != R0 ||match(L0,m_ImmConstant())) {
9460std::swap(L0, L1);
9461 LPred =ICmpInst::getSwappedCmpPredicate(LPred);
9462std::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.
9469constAPInt *Unused;
9470if (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).
9475ConstantRange LCR =computeConstantRange(
9476 L1,ICmpInst::isSigned(LPred),/* UseInstrInfo=*/true,/*AC=*/nullptr,
9477/*CxtI=*/nullptr,/*DT=*/nullptr,MaxAnalysisRecursionDepth - 1);
9478ConstantRange RCR =computeConstantRange(
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.
9483if (auto R =isImpliedCondCommonOperandWithCR(LPred, LCR, RPred, RCR))
9484return 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.
9487if (match(L1,m_APInt(Unused)) &&match(R1,m_APInt(Unused)))
9488return std::nullopt;
9489 }
9490
9491// Can we infer anything when the two compares have matching operands?
9492if (L0 == R0 && L1 == R1)
9493returnICmpInst::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
9499CmpInst::Predicate SignedLPred = LPred.getPreferredSignedPredicate();
9500if ((SignedLPred ==ICmpInst::ICMP_SGT ||
9501 SignedLPred ==ICmpInst::ICMP_SGE) &&
9502match(R0,m_NSWSub(m_Specific(L0),m_Specific(L1)))) {
9503if (match(R1,m_NonPositive()) &&
9504ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) ==false)
9505returnfalse;
9506 }
9507
9508// Take SLT as an example: L0:x < L1:y and C <= 0
9509// ==> R0:(x -nsw y) < R1:(-C) is true
9510if ((SignedLPred ==ICmpInst::ICMP_SLT ||
9511 SignedLPred ==ICmpInst::ICMP_SLE) &&
9512match(R0,m_NSWSub(m_Specific(L0),m_Specific(L1)))) {
9513if (match(R1,m_NonNegative()) &&
9514ICmpInst::isImpliedByMatchingCmp(SignedLPred, RPred) ==true)
9515returntrue;
9516 }
9517
9518// L0 = R0 = L1 + R1, L0 >=u L1 implies R0 >=u R1, L0 <u L1 implies R0 <u R1
9519if (L0 == R0 &&
9520 (LPred ==ICmpInst::ICMP_ULT || LPred ==ICmpInst::ICMP_UGE) &&
9521 (RPred ==ICmpInst::ICMP_ULT || RPred ==ICmpInst::ICMP_UGE) &&
9522match(L0,m_c_Add(m_Specific(L1),m_Specific(R1))))
9523returnCmpPredicate::getMatching(LPred, RPred).has_value();
9524
9525if (autoP =CmpPredicate::getMatching(LPred, RPred))
9526returnisImpliedCondOperands(*P, L0, L1, R0, R1);
9527
9528return 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>
9536isImpliedCondAndOr(constInstruction *LHS,CmpPredicate RHSPred,
9537constValue *RHSOp0,constValue *RHSOp1,
9538constDataLayout &DL,bool LHSIsTrue,unsignedDepth) {
9539// The LHS must be an 'or', 'and', or a 'select' instruction.
9540assert((LHS->getOpcode() == Instruction::And ||
9541LHS->getOpcode() == Instruction::Or ||
9542LHS->getOpcode() == Instruction::Select) &&
9543"Expected LHS to be 'and', 'or', or 'select'.");
9544
9545assert(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.
9550constValue *ALHS, *ARHS;
9551if ((!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.
9554if (std::optional<bool> Implication =isImpliedCondition(
9555 ALHS, RHSPred, RHSOp0, RHSOp1,DL, LHSIsTrue,Depth + 1))
9556return Implication;
9557if (std::optional<bool> Implication =isImpliedCondition(
9558 ARHS, RHSPred, RHSOp0, RHSOp1,DL, LHSIsTrue,Depth + 1))
9559return Implication;
9560return std::nullopt;
9561 }
9562return std::nullopt;
9563}
9564
9565std::optional<bool>
9566llvm::isImpliedCondition(constValue *LHS,CmpPredicate RHSPred,
9567constValue *RHSOp0,constValue *RHSOp1,
9568constDataLayout &DL,bool LHSIsTrue,unsignedDepth) {
9569// Bail out when we hit the limit.
9570if (Depth ==MaxAnalysisRecursionDepth)
9571return std::nullopt;
9572
9573// A mismatch occurs when we compare a scalar cmp to a vector cmp, for
9574// example.
9575if (RHSOp0->getType()->isVectorTy() !=LHS->getType()->isVectorTy())
9576return std::nullopt;
9577
9578assert(LHS->getType()->isIntOrIntVectorTy(1) &&
9579"Expected integer type only!");
9580
9581// Match not
9582if (match(LHS,m_Not(m_Value(LHS))))
9583 LHSIsTrue = !LHSIsTrue;
9584
9585// Both LHS and RHS are icmps.
9586constICmpInst *LHSCmp = dyn_cast<ICmpInst>(LHS);
9587if (LHSCmp)
9588returnisImpliedCondICmps(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.
9593if (constInstruction *LHSI = dyn_cast<Instruction>(LHS)) {
9594if ((LHSI->getOpcode() == Instruction::And ||
9595 LHSI->getOpcode() == Instruction::Or ||
9596 LHSI->getOpcode() == Instruction::Select))
9597returnisImpliedCondAndOr(LHSI, RHSPred, RHSOp0, RHSOp1,DL, LHSIsTrue,
9598Depth);
9599 }
9600return std::nullopt;
9601}
9602
9603std::optional<bool>llvm::isImpliedCondition(constValue *LHS,constValue *RHS,
9604constDataLayout &DL,
9605bool LHSIsTrue,unsignedDepth) {
9606// LHS ==> RHS by definition
9607if (LHS ==RHS)
9608return LHSIsTrue;
9609
9610// Match not
9611bool InvertRHS =false;
9612if (match(RHS,m_Not(m_Value(RHS)))) {
9613if (LHS ==RHS)
9614return !LHSIsTrue;
9615 InvertRHS =true;
9616 }
9617
9618if (constICmpInst *RHSCmp = dyn_cast<ICmpInst>(RHS)) {
9619if (auto Implied =isImpliedCondition(
9620LHS, RHSCmp->getCmpPredicate(), RHSCmp->getOperand(0),
9621 RHSCmp->getOperand(1),DL, LHSIsTrue,Depth))
9622return InvertRHS ? !*Implied : *Implied;
9623return std::nullopt;
9624 }
9625
9626if (Depth ==MaxAnalysisRecursionDepth)
9627return std::nullopt;
9628
9629// LHS ==> (RHS1 || RHS2) if LHS ==> RHS1 or LHS ==> RHS2
9630// LHS ==> !(RHS1 && RHS2) if LHS ==> !RHS1 or LHS ==> !RHS2
9631constValue *RHS1, *RHS2;
9632if (match(RHS,m_LogicalOr(m_Value(RHS1),m_Value(RHS2)))) {
9633if (std::optional<bool> Imp =
9634isImpliedCondition(LHS, RHS1,DL, LHSIsTrue,Depth + 1))
9635if (*Imp ==true)
9636return !InvertRHS;
9637if (std::optional<bool> Imp =
9638isImpliedCondition(LHS, RHS2,DL, LHSIsTrue,Depth + 1))
9639if (*Imp ==true)
9640return !InvertRHS;
9641 }
9642if (match(RHS,m_LogicalAnd(m_Value(RHS1),m_Value(RHS2)))) {
9643if (std::optional<bool> Imp =
9644isImpliedCondition(LHS, RHS1,DL, LHSIsTrue,Depth + 1))
9645if (*Imp ==false)
9646return InvertRHS;
9647if (std::optional<bool> Imp =
9648isImpliedCondition(LHS, RHS2,DL, LHSIsTrue,Depth + 1))
9649if (*Imp ==false)
9650return InvertRHS;
9651 }
9652
9653return 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>
9659getDomPredecessorCondition(constInstruction *ContextI) {
9660if (!ContextI || !ContextI->getParent())
9661return {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?
9665constBasicBlock *ContextBB = ContextI->getParent();
9666constBasicBlock *PredBB = ContextBB->getSinglePredecessor();
9667if (!PredBB)
9668return {nullptr,false};
9669
9670// We need a conditional branch in the predecessor.
9671Value *PredCond;
9672BasicBlock *TrueBB, *FalseBB;
9673if (!match(PredBB->getTerminator(),m_Br(m_Value(PredCond), TrueBB, FalseBB)))
9674return {nullptr,false};
9675
9676// The branch should get simplified. Don't bother simplifying this condition.
9677if (TrueBB == FalseBB)
9678return {nullptr,false};
9679
9680assert((TrueBB == ContextBB || FalseBB == ContextBB) &&
9681"Predecessor block does not point to successor?");
9682
9683// Is this condition implied by the predecessor condition?
9684return {PredCond, TrueBB == ContextBB};
9685}
9686
9687std::optional<bool>llvm::isImpliedByDomCondition(constValue *Cond,
9688constInstruction *ContextI,
9689constDataLayout &DL) {
9690assert(Cond->getType()->isIntOrIntVectorTy(1) &&"Condition must be bool");
9691auto PredCond =getDomPredecessorCondition(ContextI);
9692if (PredCond.first)
9693returnisImpliedCondition(PredCond.first,Cond,DL, PredCond.second);
9694return std::nullopt;
9695}
9696
9697std::optional<bool>llvm::isImpliedByDomCondition(CmpPredicate Pred,
9698constValue *LHS,
9699constValue *RHS,
9700constInstruction *ContextI,
9701constDataLayout &DL) {
9702auto PredCond =getDomPredecessorCondition(ContextI);
9703if (PredCond.first)
9704returnisImpliedCondition(PredCond.first, Pred,LHS,RHS,DL,
9705 PredCond.second);
9706return std::nullopt;
9707}
9708
9709staticvoidsetLimitsForBinOp(constBinaryOperator &BO,APInt &Lower,
9710APInt &Upper,constInstrInfoQuery &IIQ,
9711bool PreferSignedRange) {
9712unsigned Width =Lower.getBitWidth();
9713constAPInt *C;
9714switch (BO.getOpcode()) {
9715case Instruction::Add:
9716if (match(BO.getOperand(1),m_APInt(C)) && !C->isZero()) {
9717bool HasNSW = IIQ.hasNoSignedWrap(&BO);
9718bool 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].
9724if (PreferSignedRange && HasNSW && HasNUW)
9725 HasNUW =false;
9726
9727if (HasNUW) {
9728// 'add nuw x, C' produces [C, UINT_MAX].
9729Lower = *C;
9730 }elseif (HasNSW) {
9731if (C->isNegative()) {
9732// 'add nsw x, -C' produces [SINT_MIN, SINT_MAX - C].
9733Lower =APInt::getSignedMinValue(Width);
9734Upper =APInt::getSignedMaxValue(Width) + *C + 1;
9735 }else {
9736// 'add nsw x, +C' produces [SINT_MIN + C, SINT_MAX].
9737Lower =APInt::getSignedMinValue(Width) + *C;
9738Upper =APInt::getSignedMaxValue(Width) + 1;
9739 }
9740 }
9741 }
9742break;
9743
9744case Instruction::And:
9745if (match(BO.getOperand(1),m_APInt(C)))
9746// 'and x, C' produces [0, C].
9747Upper = *C + 1;
9748// X & -X is a power of two or zero. So we can cap the value at max power of
9749// two.
9750if (match(BO.getOperand(0),m_Neg(m_Specific(BO.getOperand(1)))) ||
9751match(BO.getOperand(1),m_Neg(m_Specific(BO.getOperand(0)))))
9752Upper =APInt::getSignedMinValue(Width) + 1;
9753break;
9754
9755case Instruction::Or:
9756if (match(BO.getOperand(1),m_APInt(C)))
9757// 'or x, C' produces [C, UINT_MAX].
9758Lower = *C;
9759break;
9760
9761case Instruction::AShr:
9762if (match(BO.getOperand(1),m_APInt(C)) &&C->ult(Width)) {
9763// 'ashr x, C' produces [INT_MIN >> C, INT_MAX >> C].
9764Lower =APInt::getSignedMinValue(Width).ashr(*C);
9765Upper =APInt::getSignedMaxValue(Width).ashr(*C) + 1;
9766 }elseif (match(BO.getOperand(0),m_APInt(C))) {
9767unsigned ShiftAmount = Width - 1;
9768if (!C->isZero() && IIQ.isExact(&BO))
9769 ShiftAmount =C->countr_zero();
9770if (C->isNegative()) {
9771// 'ashr C, x' produces [C, C >> (Width-1)]
9772Lower = *C;
9773Upper =C->ashr(ShiftAmount) + 1;
9774 }else {
9775// 'ashr C, x' produces [C >> (Width-1), C]
9776Lower =C->ashr(ShiftAmount);
9777Upper = *C + 1;
9778 }
9779 }
9780break;
9781
9782case Instruction::LShr:
9783if (match(BO.getOperand(1),m_APInt(C)) &&C->ult(Width)) {
9784// 'lshr x, C' produces [0, UINT_MAX >> C].
9785Upper =APInt::getAllOnes(Width).lshr(*C) + 1;
9786 }elseif (match(BO.getOperand(0),m_APInt(C))) {
9787// 'lshr C, x' produces [C >> (Width-1), C].
9788unsigned ShiftAmount = Width - 1;
9789if (!C->isZero() && IIQ.isExact(&BO))
9790 ShiftAmount =C->countr_zero();
9791Lower =C->lshr(ShiftAmount);
9792Upper = *C + 1;
9793 }
9794break;
9795
9796case Instruction::Shl:
9797if (match(BO.getOperand(0),m_APInt(C))) {
9798if (IIQ.hasNoUnsignedWrap(&BO)) {
9799// 'shl nuw C, x' produces [C, C << CLZ(C)]
9800Lower = *C;
9801Upper =Lower.shl(Lower.countl_zero()) + 1;
9802 }elseif (BO.hasNoSignedWrap()) {// TODO: What if both nuw+nsw?
9803if (C->isNegative()) {
9804// 'shl nsw C, x' produces [C << CLO(C)-1, C]
9805unsigned ShiftAmount =C->countl_one() - 1;
9806Lower =C->shl(ShiftAmount);
9807Upper = *C + 1;
9808 }else {
9809// 'shl nsw C, x' produces [C, C << CLZ(C)-1]
9810unsigned ShiftAmount =C->countl_zero() - 1;
9811Lower = *C;
9812Upper =C->shl(ShiftAmount) + 1;
9813 }
9814 }else {
9815// If lowbit is set, value can never be zero.
9816if ((*C)[0])
9817Lower =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).
9824Upper =APInt::getHighBitsSet(Width,C->popcount()) + 1;
9825 }
9826 }elseif (match(BO.getOperand(1),m_APInt(C)) &&C->ult(Width)) {
9827Upper =APInt::getBitsSetFrom(Width,C->getZExtValue()) + 1;
9828 }
9829break;
9830
9831case Instruction::SDiv:
9832if (match(BO.getOperand(1),m_APInt(C))) {
9833APInt IntMin =APInt::getSignedMinValue(Width);
9834APInt IntMax =APInt::getSignedMaxValue(Width);
9835if (C->isAllOnes()) {
9836// 'sdiv x, -1' produces [INT_MIN + 1, INT_MAX]
9837// where C != -1 and C != 0 and C != 1
9838Lower = IntMin + 1;
9839Upper = IntMax + 1;
9840 }elseif (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
9843Lower = IntMin.sdiv(*C);
9844Upper = IntMax.sdiv(*C);
9845if (Lower.sgt(Upper))
9846std::swap(Lower,Upper);
9847Upper =Upper + 1;
9848assert(Upper !=Lower &&"Upper part of range has wrapped!");
9849 }
9850 }elseif (match(BO.getOperand(0),m_APInt(C))) {
9851if (C->isMinSignedValue()) {
9852// 'sdiv INT_MIN, x' produces [INT_MIN, INT_MIN / -2].
9853Lower = *C;
9854Upper =Lower.lshr(1) + 1;
9855 }else {
9856// 'sdiv C, x' produces [-|C|, |C|].
9857Upper =C->abs() + 1;
9858Lower = (-Upper) + 1;
9859 }
9860 }
9861break;
9862
9863case Instruction::UDiv:
9864if (match(BO.getOperand(1),m_APInt(C)) && !C->isZero()) {
9865// 'udiv x, C' produces [0, UINT_MAX / C].
9866Upper =APInt::getMaxValue(Width).udiv(*C) + 1;
9867 }elseif (match(BO.getOperand(0),m_APInt(C))) {
9868// 'udiv C, x' produces [0, C].
9869Upper = *C + 1;
9870 }
9871break;
9872
9873case Instruction::SRem:
9874if (match(BO.getOperand(1),m_APInt(C))) {
9875// 'srem x, C' produces (-|C|, |C|).
9876Upper =C->abs();
9877Lower = (-Upper) + 1;
9878 }elseif (match(BO.getOperand(0),m_APInt(C))) {
9879if (C->isNegative()) {
9880// 'srem -|C|, x' produces [-|C|, 0].
9881Upper = 1;
9882Lower = *C;
9883 }else {
9884// 'srem |C|, x' produces [0, |C|].
9885Upper = *C + 1;
9886 }
9887 }
9888break;
9889
9890case Instruction::URem:
9891if (match(BO.getOperand(1),m_APInt(C)))
9892// 'urem x, C' produces [0, C).
9893Upper = *C;
9894elseif (match(BO.getOperand(0),m_APInt(C)))
9895// 'urem C, x' produces [0, C].
9896Upper = *C + 1;
9897break;
9898
9899default:
9900break;
9901 }
9902}
9903
9904staticConstantRangegetRangeForIntrinsic(constIntrinsicInst &II,
9905bool UseInstrInfo) {
9906unsigned Width =II.getType()->getScalarSizeInBits();
9907constAPInt *C;
9908switch (II.getIntrinsicID()) {
9909case Intrinsic::ctlz:
9910case Intrinsic::cttz: {
9911APIntUpper(Width, Width);
9912if (!UseInstrInfo || !match(II.getArgOperand(1),m_One()))
9913Upper += 1;
9914// Maximum of set/clear bits is the bit width.
9915returnConstantRange::getNonEmpty(APInt::getZero(Width),Upper);
9916 }
9917case Intrinsic::ctpop:
9918// Maximum of set/clear bits is the bit width.
9919returnConstantRange::getNonEmpty(APInt::getZero(Width),
9920APInt(Width, Width) + 1);
9921case Intrinsic::uadd_sat:
9922// uadd.sat(x, C) produces [C, UINT_MAX].
9923if (match(II.getOperand(0),m_APInt(C)) ||
9924match(II.getOperand(1),m_APInt(C)))
9925returnConstantRange::getNonEmpty(*C,APInt::getZero(Width));
9926break;
9927case Intrinsic::sadd_sat:
9928if (match(II.getOperand(0),m_APInt(C)) ||
9929match(II.getOperand(1),m_APInt(C))) {
9930if (C->isNegative())
9931// sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
9932returnConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),
9933APInt::getSignedMaxValue(Width) + *C +
9934 1);
9935
9936// sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
9937returnConstantRange::getNonEmpty(APInt::getSignedMinValue(Width) + *C,
9938APInt::getSignedMaxValue(Width) + 1);
9939 }
9940break;
9941case Intrinsic::usub_sat:
9942// usub.sat(C, x) produces [0, C].
9943if (match(II.getOperand(0),m_APInt(C)))
9944returnConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9945
9946// usub.sat(x, C) produces [0, UINT_MAX - C].
9947if (match(II.getOperand(1),m_APInt(C)))
9948returnConstantRange::getNonEmpty(APInt::getZero(Width),
9949APInt::getMaxValue(Width) - *C + 1);
9950break;
9951case Intrinsic::ssub_sat:
9952if (match(II.getOperand(0),m_APInt(C))) {
9953if (C->isNegative())
9954// ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
9955returnConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),
9956 *C -APInt::getSignedMinValue(Width) +
9957 1);
9958
9959// ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
9960returnConstantRange::getNonEmpty(*C -APInt::getSignedMaxValue(Width),
9961APInt::getSignedMaxValue(Width) + 1);
9962 }elseif (match(II.getOperand(1),m_APInt(C))) {
9963if (C->isNegative())
9964// ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
9965returnConstantRange::getNonEmpty(APInt::getSignedMinValue(Width) - *C,
9966APInt::getSignedMaxValue(Width) + 1);
9967
9968// ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
9969returnConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),
9970APInt::getSignedMaxValue(Width) - *C +
9971 1);
9972 }
9973break;
9974case Intrinsic::umin:
9975case Intrinsic::umax:
9976case Intrinsic::smin:
9977case Intrinsic::smax:
9978if (!match(II.getOperand(0),m_APInt(C)) &&
9979 !match(II.getOperand(1),m_APInt(C)))
9980break;
9981
9982switch (II.getIntrinsicID()) {
9983case Intrinsic::umin:
9984returnConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
9985case Intrinsic::umax:
9986returnConstantRange::getNonEmpty(*C,APInt::getZero(Width));
9987case Intrinsic::smin:
9988returnConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),
9989 *C + 1);
9990case Intrinsic::smax:
9991returnConstantRange::getNonEmpty(*C,
9992APInt::getSignedMaxValue(Width) + 1);
9993default:
9994llvm_unreachable("Must be min/max intrinsic");
9995 }
9996break;
9997case 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.
10000if (match(II.getOperand(1),m_One()))
10001returnConstantRange::getNonEmpty(APInt::getZero(Width),
10002APInt::getSignedMaxValue(Width) + 1);
10003
10004returnConstantRange::getNonEmpty(APInt::getZero(Width),
10005APInt::getSignedMinValue(Width) + 1);
10006case Intrinsic::vscale:
10007if (!II.getParent() || !II.getFunction())
10008break;
10009returngetVScaleRange(II.getFunction(), Width);
10010case Intrinsic::scmp:
10011case Intrinsic::ucmp:
10012returnConstantRange::getNonEmpty(APInt::getAllOnes(Width),
10013APInt(Width, 2));
10014default:
10015break;
10016 }
10017
10018return ConstantRange::getFull(Width);
10019}
10020
10021staticConstantRangegetRangeForSelectPattern(constSelectInst &SI,
10022constInstrInfoQuery &IIQ) {
10023unsignedBitWidth = SI.getType()->getScalarSizeInBits();
10024constValue *LHS =nullptr, *RHS =nullptr;
10025SelectPatternResult R =matchSelectPattern(&SI,LHS,RHS);
10026if (R.Flavor ==SPF_UNKNOWN)
10027return ConstantRange::getFull(BitWidth);
10028
10029if (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.
10033if (match(RHS,m_Neg(m_Specific(LHS))) &&
10034 IIQ.hasNoSignedWrap(cast<Instruction>(RHS)))
10035returnConstantRange::getNonEmpty(APInt::getZero(BitWidth),
10036APInt::getSignedMaxValue(BitWidth) + 1);
10037
10038returnConstantRange::getNonEmpty(APInt::getZero(BitWidth),
10039APInt::getSignedMinValue(BitWidth) + 1);
10040 }
10041
10042if (R.Flavor ==SelectPatternFlavor::SPF_NABS) {
10043// The result of -abs(X) is <= 0.
10044returnConstantRange::getNonEmpty(APInt::getSignedMinValue(BitWidth),
10045APInt(BitWidth, 1));
10046 }
10047
10048constAPInt *C;
10049if (!match(LHS,m_APInt(C)) && !match(RHS,m_APInt(C)))
10050return ConstantRange::getFull(BitWidth);
10051
10052switch (R.Flavor) {
10053caseSPF_UMIN:
10054returnConstantRange::getNonEmpty(APInt::getZero(BitWidth), *C + 1);
10055caseSPF_UMAX:
10056returnConstantRange::getNonEmpty(*C,APInt::getZero(BitWidth));
10057caseSPF_SMIN:
10058returnConstantRange::getNonEmpty(APInt::getSignedMinValue(BitWidth),
10059 *C + 1);
10060caseSPF_SMAX:
10061returnConstantRange::getNonEmpty(*C,
10062APInt::getSignedMaxValue(BitWidth) + 1);
10063default:
10064return ConstantRange::getFull(BitWidth);
10065 }
10066}
10067
10068staticvoidsetLimitForFPToI(constInstruction *I,APInt &Lower,APInt &Upper) {
10069// The maximum representable value of a half is 65504. For floats the maximum
10070// value is 3.4e38 which requires roughly 129 bits.
10071unsignedBitWidth =I->getType()->getScalarSizeInBits();
10072if (!I->getOperand(0)->getType()->getScalarType()->isHalfTy())
10073return;
10074if (isa<FPToSIInst>(I) &&BitWidth >= 17) {
10075Lower =APInt(BitWidth, -65504,true);
10076Upper =APInt(BitWidth, 65505);
10077 }
10078
10079if (isa<FPToUIInst>(I) &&BitWidth >= 16) {
10080// For a fptoui the lower limit is left as 0.
10081Upper =APInt(BitWidth, 65505);
10082 }
10083}
10084
10085ConstantRangellvm::computeConstantRange(constValue *V,bool ForSigned,
10086bool UseInstrInfo,AssumptionCache *AC,
10087constInstruction *CtxI,
10088constDominatorTree *DT,
10089unsignedDepth) {
10090assert(V->getType()->isIntOrIntVectorTy() &&"Expected integer instruction");
10091
10092if (Depth ==MaxAnalysisRecursionDepth)
10093return ConstantRange::getFull(V->getType()->getScalarSizeInBits());
10094
10095if (auto *C = dyn_cast<Constant>(V))
10096returnC->toConstantRange();
10097
10098unsignedBitWidth = V->getType()->getScalarSizeInBits();
10099InstrInfoQuery IIQ(UseInstrInfo);
10100ConstantRange CR = ConstantRange::getFull(BitWidth);
10101if (auto *BO = dyn_cast<BinaryOperator>(V)) {
10102APIntLower =APInt(BitWidth, 0);
10103APIntUpper =APInt(BitWidth, 0);
10104// TODO: Return ConstantRange.
10105setLimitsForBinOp(*BO,Lower,Upper, IIQ, ForSigned);
10106 CR =ConstantRange::getNonEmpty(Lower,Upper);
10107 }elseif (auto *II = dyn_cast<IntrinsicInst>(V))
10108 CR =getRangeForIntrinsic(*II, UseInstrInfo);
10109elseif (auto *SI = dyn_cast<SelectInst>(V)) {
10110ConstantRange CRTrue =computeConstantRange(
10111 SI->getTrueValue(), ForSigned, UseInstrInfo, AC, CtxI, DT,Depth + 1);
10112ConstantRange CRFalse =computeConstantRange(
10113 SI->getFalseValue(), ForSigned, UseInstrInfo, AC, CtxI, DT,Depth + 1);
10114 CR = CRTrue.unionWith(CRFalse);
10115 CR = CR.intersectWith(getRangeForSelectPattern(*SI, IIQ));
10116 }elseif (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
10117APIntLower =APInt(BitWidth, 0);
10118APIntUpper =APInt(BitWidth, 0);
10119// TODO: Return ConstantRange.
10120setLimitForFPToI(cast<Instruction>(V),Lower,Upper);
10121 CR =ConstantRange::getNonEmpty(Lower,Upper);
10122 }elseif (constauto *A = dyn_cast<Argument>(V))
10123if (std::optional<ConstantRange>Range =A->getRange())
10124 CR = *Range;
10125
10126if (auto *I = dyn_cast<Instruction>(V)) {
10127if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))
10128 CR = CR.intersectWith(getConstantRangeFromMetadata(*Range));
10129
10130if (constauto *CB = dyn_cast<CallBase>(V))
10131if (std::optional<ConstantRange>Range = CB->getRange())
10132 CR = CR.intersectWith(*Range);
10133 }
10134
10135if (CtxI && AC) {
10136// Try to restrict the range based on information from assumptions.
10137for (auto &AssumeVH : AC->assumptionsFor(V)) {
10138if (!AssumeVH)
10139continue;
10140CallInst *I = cast<CallInst>(AssumeVH);
10141assert(I->getParent()->getParent() == CtxI->getParent()->getParent() &&
10142"Got assumption for the wrong function!");
10143assert(I->getIntrinsicID() == Intrinsic::assume &&
10144"must be an assume intrinsic");
10145
10146if (!isValidAssumeForContext(I, CtxI, DT))
10147continue;
10148Value *Arg =I->getArgOperand(0);
10149ICmpInst *Cmp = dyn_cast<ICmpInst>(Arg);
10150// Currently we just use information from comparisons.
10151if (!Cmp || Cmp->getOperand(0) != V)
10152continue;
10153// TODO: Set "ForSigned" parameter via Cmp->isSigned()?
10154ConstantRangeRHS =
10155computeConstantRange(Cmp->getOperand(1),/* ForSigned */false,
10156 UseInstrInfo, AC,I, DT,Depth + 1);
10157 CR = CR.intersectWith(
10158ConstantRange::makeAllowedICmpRegion(Cmp->getPredicate(),RHS));
10159 }
10160 }
10161
10162return CR;
10163}
10164
10165staticvoid
10166addValueAffectedByCondition(Value *V,
10167function_ref<void(Value *)> InsertAffected) {
10168assert(V !=nullptr);
10169if (isa<Argument>(V) || isa<GlobalValue>(V)) {
10170 InsertAffected(V);
10171 }elseif (auto *I = dyn_cast<Instruction>(V)) {
10172 InsertAffected(V);
10173
10174// Peek through unary operators to find the source of the condition.
10175Value *Op;
10176if (match(I,m_CombineOr(m_PtrToInt(m_Value(Op)),m_Trunc(m_Value(Op))))) {
10177if (isa<Instruction>(Op) || isa<Argument>(Op))
10178 InsertAffected(Op);
10179 }
10180 }
10181}
10182
10183voidllvm::findValuesAffectedByCondition(
10184Value *Cond,bool IsAssume,function_ref<void(Value *)> InsertAffected) {
10185auto AddAffected = [&InsertAffected](Value *V) {
10186addValueAffectedByCondition(V, InsertAffected);
10187 };
10188
10189auto AddCmpOperands = [&AddAffected, IsAssume](Value *LHS,Value *RHS) {
10190if (IsAssume) {
10191 AddAffected(LHS);
10192 AddAffected(RHS);
10193 }elseif (match(RHS,m_Constant()))
10194 AddAffected(LHS);
10195 };
10196
10197SmallVector<Value *, 8> Worklist;
10198SmallPtrSet<Value *, 8> Visited;
10199 Worklist.push_back(Cond);
10200while (!Worklist.empty()) {
10201Value *V = Worklist.pop_back_val();
10202if (!Visited.insert(V).second)
10203continue;
10204
10205CmpPredicate Pred;
10206Value *A, *B, *X;
10207
10208if (IsAssume) {
10209 AddAffected(V);
10210if (match(V,m_Not(m_Value(X))))
10211 AddAffected(X);
10212 }
10213
10214if (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).
10220if (!IsAssume) {
10221 Worklist.push_back(A);
10222 Worklist.push_back(B);
10223 }
10224 }elseif (match(V,m_ICmp(Pred,m_Value(A),m_Value(B)))) {
10225 AddCmpOperands(A,B);
10226
10227bool HasRHSC =match(B,m_ConstantInt());
10228if (ICmpInst::isEquality(Pred)) {
10229if (HasRHSC) {
10230Value *Y;
10231// (X & C) or (X | C) or (X ^ C).
10232// (X << C) or (X >>_s C) or (X >>_u C).
10233if (match(A,m_BitwiseLogic(m_Value(X),m_ConstantInt())) ||
10234match(A,m_Shift(m_Value(X),m_ConstantInt())))
10235 AddAffected(X);
10236elseif (match(A,m_And(m_Value(X),m_Value(Y))) ||
10237match(A,m_Or(m_Value(X),m_Value(Y)))) {
10238 AddAffected(X);
10239 AddAffected(Y);
10240 }
10241 }
10242 }else {
10243if (HasRHSC) {
10244// Handle (A + C1) u< C2, which is the canonical form of
10245// A > C3 && A < C4.
10246if (match(A,m_AddLike(m_Value(X),m_ConstantInt())))
10247 AddAffected(X);
10248
10249if (ICmpInst::isUnsigned(Pred)) {
10250Value *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
10254if (match(A,m_And(m_Value(X),m_Value(Y))) ||
10255match(A,m_Or(m_Value(X),m_Value(Y))) ||
10256match(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
10261if (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().
10268if (match(A,m_ElementWiseBitCast(m_Value(X)))) {
10269if (Pred ==ICmpInst::ICMP_SLT &&match(B,m_Zero()))
10270 InsertAffected(X);
10271elseif (Pred ==ICmpInst::ICMP_SGT &&match(B,m_AllOnes()))
10272 InsertAffected(X);
10273 }
10274 }
10275
10276if (HasRHSC &&match(A, m_Intrinsic<Intrinsic::ctpop>(m_Value(X))))
10277 AddAffected(X);
10278 }elseif (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
10284if (match(A,m_FNeg(m_Value(A))))
10285 AddAffected(A);
10286if (match(A,m_FAbs(m_Value(A))))
10287 AddAffected(A);
10288
10289 }elseif (match(V, m_Intrinsic<Intrinsic::is_fpclass>(m_Value(A),
10290m_Value()))) {
10291// Handle patterns that computeKnownFPClass() support.
10292 AddAffected(A);
10293 }
10294 }
10295}
Select
AMDGPU Register Bank Select
Definition:AMDGPURegBankSelect.cpp:71
PHI
Rewrite undef for PHI
Definition:AMDGPURewriteUndefForPHI.cpp:100
APFloat.h
This file declares a class to represent arbitrary precision floating point values and provide a varie...
APInt.h
This file implements a class to represent arbitrary precision integral constant values and operations...
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition:ARMSLSHardening.cpp:73
Results
Function Alias Analysis Results
Definition:AliasAnalysis.cpp:731
AliasAnalysis.h
GuardUtils.h
ArrayRef.h
AssumeBundleQueries.h
AssumptionCache.h
Attributes.h
This file contains the simple types necessary to represent the attributes associated with functions a...
From
BlockVerifier::State From
Definition:BlockVerifier.cpp:57
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
A
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
D
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
Casting.h
CommandLine.h
Compiler.h
ConstantFolding.h
ConstantRange.h
Constants.h
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Idx
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
Definition:DeadArgumentElimination.cpp:353
DerivedTypes.h
DiagnosticInfo.h
DomConditionCache.h
Dominators.h
EHPersonalities.h
Other
std::optional< std::vector< StOtherPiece > > Other
Definition:ELFYAML.cpp:1315
End
bool End
Definition:ELF_riscv.cpp:480
X
static GCMetadataPrinterRegistry::Add< ErlangGCPrinter > X("erlang", "erlang-compatible garbage collector")
GetElementPtrTypeIterator.h
GlobalAlias.h
GlobalValue.h
GlobalVariable.h
GEP
Hexagon Common GEP
Definition:HexagonCommonGEP.cpp:170
getAlign
static MaybeAlign getAlign(Value *Ptr)
Definition:IRBuilder.cpp:500
Argument.h
BasicBlock.h
Constant.h
Function.h
Instruction.h
IntrinsicInst.h
Module.h
Module.h This file contains the declarations for the Module class.
Operator.h
Type.h
User.h
Value.h
InstrTypes.h
hasNoUnsignedWrap
static bool hasNoUnsignedWrap(BinaryOperator &I)
Definition:InstructionCombining.cpp:314
InstructionSimplify.h
Instructions.h
Intrinsics.h
KnownBits.h
LLVMContext.h
Loads.h
LoopInfo.h
F
#define F(x, y, z)
Definition:MD5.cpp:55
I
#define I(x, y, z)
Definition:MD5.cpp:58
Operands
mir Rename Register Operands
Definition:MIRNamerPass.cpp:74
MathExtras.h
Metadata.h
This file contains the declarations for metadata subclasses.
Range
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
II
uint64_t IntrinsicInst * II
Definition:NVVMIntrRange.cpp:51
Y
static GCMetadataPrinterRegistry::Add< OcamlGCMetadataPrinter > Y("ocaml", "ocaml 3.10-compatible collector")
P
#define P(N)
PatternMatch.h
Merge
R600 Clause Merge
Definition:R600ClauseMergePass.cpp:70
Cond
const SmallVectorImpl< MachineOperand > & Cond
Definition:RISCVRedundantCopyElimination.cpp:75
RISCVTargetParser.h
mayHaveSideEffects
static bool mayHaveSideEffects(MachineInstr &MI)
Definition:ReachingDefAnalysis.cpp:538
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
STLExtras.h
This file contains some templates that are useful if you are working with the STL at all.
ScopeExit.h
This file defines the make_scope_exit function, which executes user-defined cleanup logic at scope ex...
SmallPtrSet.h
This file defines the SmallPtrSet class.
SmallSet.h
This file defines the SmallSet class.
SmallVector.h
This file defines the SmallVector class.
StringRef.h
Flt
@ Flt
Definition:TargetLibraryInfo.cpp:73
TargetLibraryInfo.h
getOpcode
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
getOperands
static SmallVector< VPValue *, 4 > getOperands(ArrayRef< VPValue * > Values, unsigned OperandIndex)
Definition:VPlanSLP.cpp:154
getShuffleDemandedElts
static bool getShuffleDemandedElts(const ShuffleVectorInst *Shuf, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Definition:ValueTracking.cpp:134
DomConditionsMaxUses
static cl::opt< unsigned > DomConditionsMaxUses("dom-conditions-max-uses", cl::Hidden, cl::init(20))
computeNumSignBitsVectorConstant
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...
Definition:ValueTracking.cpp:3862
isKnownNonZeroFromOperator
static bool isKnownNonZeroFromOperator(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:2936
isTruePredicate
static bool isTruePredicate(CmpInst::Predicate Pred, const Value *LHS, const Value *RHS)
Return true if "icmp Pred LHS RHS" is always true.
Definition:ValueTracking.cpp:9283
isNonZeroMul
static bool isNonZeroMul(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW)
Definition:ValueTracking.cpp:2857
isKnownNonNullFromDominatingCondition
static bool isKnownNonNullFromDominatingCondition(const Value *V, const Instruction *CtxI, const DominatorTree *DT)
Definition:ValueTracking.cpp:2646
getUnderlyingObjectFromInt
static const Value * getUnderlyingObjectFromInt(const Value *V)
This is the function that does the work of looking through basic ptrtoint+arithmetic+inttoptr sequenc...
Definition:ValueTracking.cpp:6908
isNonZeroShift
static bool isNonZeroShift(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, const KnownBits &KnownVal)
Definition:ValueTracking.cpp:2885
rangeMetadataExcludesValue
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...
Definition:ValueTracking.cpp:2743
outputDenormalIsIEEEOrPosZero
static bool outputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
Definition:ValueTracking.cpp:4407
breakSelfRecursivePHI
static void breakSelfRecursivePHI(const Use *U, const PHINode *PHI, Value *&ValOut, Instruction *&CtxIOut, const PHINode **PhiOut=nullptr)
Definition:ValueTracking.cpp:595
inputDenormalIsIEEE
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.
Definition:ValueTracking.cpp:4395
mapOverflowResult
static OverflowResult mapOverflowResult(ConstantRange::OverflowResult OR)
Convert ConstantRange OverflowResult into ValueTracking OverflowResult.
Definition:ValueTracking.cpp:7180
isNonEqualPHIs
static bool isNonEqualPHIs(const PHINode *PN1, const PHINode *PN2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:3681
isNonEqualShl
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.
Definition:ValueTracking.cpp:3669
addValueAffectedByCondition
static void addValueAffectedByCondition(Value *V, function_ref< void(Value *)> InsertAffected)
Definition:ValueTracking.cpp:10166
getBitWidth
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Definition:ValueTracking.cpp:93
exactClass
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.
Definition:ValueTracking.cpp:4550
haveNoCommonBitsSetSpecialCases
static bool haveNoCommonBitsSetSpecialCases(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
Definition:ValueTracking.cpp:190
setLimitsForBinOp
static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower, APInt &Upper, const InstrInfoQuery &IIQ, bool PreferSignedRange)
Definition:ValueTracking.cpp:9709
lookThroughCast
static Value * lookThroughCast(CmpInst *CmpI, Value *V1, Value *V2, Instruction::CastOps *CastOp)
Helps to match a select pattern in case of a type mismatch.
Definition:ValueTracking.cpp:9013
getDomPredecessorCondition
static std::pair< Value *, bool > getDomPredecessorCondition(const Instruction *ContextI)
Definition:ValueTracking.cpp:9659
isKnownNonEqual
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.
Definition:ValueTracking.cpp:3785
isKnownNonZero
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.
Definition:ValueTracking.cpp:3399
isNonEqualSelect
static bool isNonEqualSelect(const Value *V1, const Value *V2, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:3712
ComputeNumSignBits
static unsigned ComputeNumSignBits(const Value *V, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:3889
UndefPoisonKind
UndefPoisonKind
Definition:ValueTracking.cpp:7469
UndefPoisonKind::UndefOnly
@ UndefOnly
UndefPoisonKind::UndefOrPoison
@ UndefOrPoison
UndefPoisonKind::PoisonOnly
@ PoisonOnly
includesPoison
static bool includesPoison(UndefPoisonKind Kind)
Definition:ValueTracking.cpp:7475
isNonEqualMul
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...
Definition:ValueTracking.cpp:3654
matchFastFloatClamp
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.
Definition:ValueTracking.cpp:8314
includesUndef
static bool includesUndef(UndefPoisonKind Kind)
Definition:ValueTracking.cpp:7479
isImpliedCondCommonOperandWithCR
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.
Definition:ValueTracking.cpp:9405
isPowerOfTwoRecurrence
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...
Definition:ValueTracking.cpp:2302
isModifyingBinopOfNonZero
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.
Definition:ValueTracking.cpp:3625
getRangeForSelectPattern
static ConstantRange getRangeForSelectPattern(const SelectInst &SI, const InstrInfoQuery &IIQ)
Definition:ValueTracking.cpp:10021
matchSelectPattern
static SelectPatternResult matchSelectPattern(CmpInst::Predicate Pred, FastMathFlags FMF, Value *CmpLHS, Value *CmpRHS, Value *TrueVal, Value *FalseVal, Value *&LHS, Value *&RHS, unsigned Depth)
Definition:ValueTracking.cpp:8745
GetStringLengthH
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'.
Definition:ValueTracking.cpp:6629
onlyUsedByLifetimeMarkersOrDroppableInstsHelper
static bool onlyUsedByLifetimeMarkersOrDroppableInstsHelper(const Value *V, bool AllowLifetime, bool AllowDroppable)
Definition:ValueTracking.cpp:7015
isImpliedCondAndOr
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.
Definition:ValueTracking.cpp:9536
computeKnownFPClassFromCond
static void computeKnownFPClassFromCond(const Value *V, Value *Cond, unsigned Depth, bool CondIsTrue, const Instruction *CxtI, KnownFPClass &KnownFromContext)
Definition:ValueTracking.cpp:4939
isImpliedCondICmps
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.
Definition:ValueTracking.cpp:9437
isSignedMinMaxClamp
static bool isSignedMinMaxClamp(const Value *Select, const Value *&In, const APInt *&CLow, const APInt *&CHigh)
Definition:ValueTracking.cpp:1118
computeKnownBitsAddSub
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)
Definition:ValueTracking.cpp:367
computeKnownBitsFromOperator
static void computeKnownBitsFromOperator(const Operator *I, const APInt &DemandedElts, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:1174
directlyImpliesPoison
static bool directlyImpliesPoison(const Value *ValAssumedPoison, const Value *V, unsigned Depth)
Definition:ValueTracking.cpp:7648
computeKnownBitsFromCmp
static void computeKnownBitsFromCmp(const Value *V, CmpInst::Predicate Pred, Value *LHS, Value *RHS, KnownBits &Known, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:677
matchMinMaxOfMinMax
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))
Definition:ValueTracking.cpp:8400
unionWithMinMaxIntrinsicClamp
static void unionWithMinMaxIntrinsicClamp(const IntrinsicInst *II, KnownBits &Known)
Definition:ValueTracking.cpp:1166
setLimitForFPToI
static void setLimitForFPToI(const Instruction *I, APInt &Lower, APInt &Upper)
Definition:ValueTracking.cpp:10068
isSameUnderlyingObjectInLoop
static bool isSameUnderlyingObjectInLoop(const PHINode *PN, const LoopInfo *LI)
PN defines a loop-variant pointer to an object.
Definition:ValueTracking.cpp:6750
isNonEqualPointersWithRecursiveGEP
static bool isNonEqualPointersWithRecursiveGEP(const Value *A, const Value *B, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:3737
isSignedMinMaxIntrinsicClamp
static bool isSignedMinMaxIntrinsicClamp(const IntrinsicInst *II, const APInt *&CLow, const APInt *&CHigh)
Definition:ValueTracking.cpp:1147
lookThroughCastConst
static Value * lookThroughCastConst(CmpInst *CmpI, Type *SrcTy, Constant *C, Instruction::CastOps *CastOp)
Definition:ValueTracking.cpp:8918
computeKnownFPClassForFPTrunc
static void computeKnownFPClassForFPTrunc(const Operator *Op, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:5041
handleGuaranteedWellDefinedOps
static bool handleGuaranteedWellDefinedOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be undef or poison.
Definition:ValueTracking.cpp:8064
computeKnownBits
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.
Definition:ValueTracking.cpp:2160
computeKnownFPClassFromContext
static KnownFPClass computeKnownFPClassFromContext(const Value *V, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:4979
getNotValue
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...
Definition:ValueTracking.cpp:8496
computeKnownBitsFromCond
static void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known, unsigned Depth, const SimplifyQuery &SQ, bool Invert)
Definition:ValueTracking.cpp:808
computeKnownBitsFromICmpCond
static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp, KnownBits &Known, const SimplifyQuery &SQ, bool Invert)
Definition:ValueTracking.cpp:789
computeKnownBitsForHorizontalOperation
static KnownBits computeKnownBitsForHorizontalOperation(const Operator *I, const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, const function_ref< KnownBits(const KnownBits &, const KnownBits &)> KnownBitsFunc)
Definition:ValueTracking.cpp:1023
matchOpWithOpEqZero
static bool matchOpWithOpEqZero(Value *Op0, Value *Op1)
Definition:ValueTracking.cpp:2788
isNonZeroRecurrence
static bool isNonZeroRecurrence(const PHINode *PN)
Try to detect a recurrence that monotonically increases/decreases from a non-zero starting value.
Definition:ValueTracking.cpp:2760
computeKnownBitsMul
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)
Definition:ValueTracking.cpp:383
matchClamp
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)))
Definition:ValueTracking.cpp:8364
shiftAmountKnownInRange
static bool shiftAmountKnownInRange(const Value *ShiftAmount)
Shifts return poison if shiftwidth is larger than the bitwidth.
Definition:ValueTracking.cpp:7445
isEphemeralValueOf
static bool isEphemeralValueOf(const Instruction *I, const Value *E)
Definition:ValueTracking.cpp:465
matchMinMax
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.
Definition:ValueTracking.cpp:8509
isGEPKnownNonNull
static bool isGEPKnownNonNull(const GEPOperator *GEP, unsigned Depth, const SimplifyQuery &Q)
Test whether a GEP's result is known to be non-null.
Definition:ValueTracking.cpp:2582
handleGuaranteedNonPoisonOps
static bool handleGuaranteedNonPoisonOps(const Instruction *I, const CallableT &Handle)
Enumerates all operands of I that are guaranteed to not be poison.
Definition:ValueTracking.cpp:8134
isNonZeroSub
static bool isNonZeroSub(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y)
Definition:ValueTracking.cpp:2841
getInvertibleOperands
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...
Definition:ValueTracking.cpp:3509
computeKnownBitsFromShiftOperator
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.
Definition:ValueTracking.cpp:937
cmpExcludesZero
static bool cmpExcludesZero(CmpInst::Predicate Pred, const Value *RHS)
Definition:ValueTracking.cpp:564
inputDenormalIsIEEEOrPosZero
static bool inputDenormalIsIEEEOrPosZero(const Function &F, const Type *Ty)
Definition:ValueTracking.cpp:4400
getKnownBitsFromAndXorOr
static KnownBits getKnownBitsFromAndXorOr(const Operator *I, const APInt &DemandedElts, const KnownBits &KnownLHS, const KnownBits &KnownRHS, unsigned Depth, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:953
isKnownNonZeroFromAssume
static bool isKnownNonZeroFromAssume(const Value *V, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:630
isImpliedCondOperands
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.
Definition:ValueTracking.cpp:9365
ComputeNumSignBitsImpl
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.
Definition:ValueTracking.cpp:3903
isNonZeroAdd
static bool isNonZeroAdd(const APInt &DemandedElts, unsigned Depth, const SimplifyQuery &Q, unsigned BitWidth, Value *X, Value *Y, bool NSW, bool NUW)
Definition:ValueTracking.cpp:2795
safeCxtI
static const Instruction * safeCxtI(const Value *V, const Instruction *CxtI)
Definition:ValueTracking.cpp:102
isImpliedToBeAPowerOfTwoFromCond
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....
Definition:ValueTracking.cpp:2359
isKnownNonNaN
static bool isKnownNonNaN(const Value *V, FastMathFlags FMF)
Definition:ValueTracking.cpp:8270
getRangeForIntrinsic
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II, bool UseInstrInfo)
Definition:ValueTracking.cpp:9904
BuildSubAggregate
static Value * BuildSubAggregate(Value *From, Value *To, Type *IndexedType, SmallVectorImpl< unsigned > &Idxs, unsigned IdxSkip, BasicBlock::iterator InsertBefore)
Definition:ValueTracking.cpp:6302
computeKnownFPClass
void computeKnownFPClass(const Value *V, const APInt &DemandedElts, FPClassTest InterestedClasses, KnownFPClass &Known, unsigned Depth, const SimplifyQuery &Q)
Definition:ValueTracking.cpp:5064
ValueTracking.h
VectorUtils.h
WithCache.h
RHS
Value * RHS
Definition:X86PartialReduction.cpp:74
LHS
Value * LHS
Definition:X86PartialReduction.cpp:73
PointerType
Definition:ItaniumDemangle.h:627
llvm::APFloat
Definition:APFloat.h:904
llvm::APFloat::isNegative
bool isNegative() const
Definition:APFloat.h:1445
llvm::APFloat::isFinite
bool isFinite() const
Definition:APFloat.h:1450
llvm::APFloat::isNaN
bool isNaN() const
Definition:APFloat.h:1443
llvm::APFloat::bitcastToAPInt
APInt bitcastToAPInt() const
Definition:APFloat.h:1351
llvm::APFloat::getLargest
static APFloat getLargest(const fltSemantics &Sem, bool Negative=false)
Returns the largest finite number in the given semantics.
Definition:APFloat.h:1140
llvm::APFloat::getInf
static APFloat getInf(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Infinity.
Definition:APFloat.h:1100
llvm::APFloat::classify
FPClassTest classify() const
Return the FPClassTest which will return true for the value.
Definition:APFloat.cpp:5450
llvm::APFloat::getZero
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
Definition:APFloat.h:1081
llvm::APFloat::isSmallestNormalized
bool isSmallestNormalized() const
Definition:APFloat.h:1465
llvm::APInt
Class for arbitrary precision integers.
Definition:APInt.h:78
llvm::APInt::udiv
APInt udiv(const APInt &RHS) const
Unsigned division operation.
Definition:APInt.cpp:1547
llvm::APInt::getAllOnes
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
Definition:APInt.h:234
llvm::APInt::clearBit
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition:APInt.h:1407
llvm::APInt::isMinSignedValue
bool isMinSignedValue() const
Determine if this is the smallest signed value.
Definition:APInt.h:423
llvm::APInt::getZExtValue
uint64_t getZExtValue() const
Get zero extended value.
Definition:APInt.h:1520
llvm::APInt::setHighBits
void setHighBits(unsigned hiBits)
Set the top hiBits bits.
Definition:APInt.h:1392
llvm::APInt::setBitsFrom
void setBitsFrom(unsigned loBit)
Set the top bits starting from loBit.
Definition:APInt.h:1386
llvm::APInt::zextOrTrunc
APInt zextOrTrunc(unsigned width) const
Zero extend or truncate to width.
Definition:APInt.cpp:1007
llvm::APInt::getMaxValue
static APInt getMaxValue(unsigned numBits)
Gets maximum unsigned value of APInt for specific bit width.
Definition:APInt.h:206
llvm::APInt::setBit
void setBit(unsigned BitPosition)
Set the given bit to 1 whose position is given as "bitPosition".
Definition:APInt.h:1330
llvm::APInt::ceilLogBase2
unsigned ceilLogBase2() const
Definition:APInt.h:1742
llvm::APInt::sgt
bool sgt(const APInt &RHS) const
Signed greater than comparison.
Definition:APInt.h:1201
llvm::APInt::isAllOnes
bool isAllOnes() const
Determine if all bits are set. This is true for zero-width values.
Definition:APInt.h:371
llvm::APInt::ugt
bool ugt(const APInt &RHS) const
Unsigned greater than comparison.
Definition:APInt.h:1182
llvm::APInt::isZero
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition:APInt.h:380
llvm::APInt::urem
APInt urem(const APInt &RHS) const
Unsigned remainder operation.
Definition:APInt.cpp:1640
llvm::APInt::getBitWidth
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition:APInt.h:1468
llvm::APInt::ult
bool ult(const APInt &RHS) const
Unsigned less than comparison.
Definition:APInt.h:1111
llvm::APInt::getSignedMaxValue
static APInt getSignedMaxValue(unsigned numBits)
Gets maximum signed value of APInt for a specific bit width.
Definition:APInt.h:209
llvm::APInt::getMinValue
static APInt getMinValue(unsigned numBits)
Gets minimum unsigned value of APInt for a specific bit width.
Definition:APInt.h:216
llvm::APInt::isNegative
bool isNegative() const
Determine sign of this APInt.
Definition:APInt.h:329
llvm::APInt::intersects
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
llvm::APInt::sdiv
APInt sdiv(const APInt &RHS) const
Signed division function for APInt.
Definition:APInt.cpp:1618
llvm::APInt::clearAllBits
void clearAllBits()
Set every bit to 0.
Definition:APInt.h:1397
llvm::APInt::reverseBits
APInt reverseBits() const
Definition:APInt.cpp:741
llvm::APInt::sle
bool sle(const APInt &RHS) const
Signed less or equal comparison.
Definition:APInt.h:1166
llvm::APInt::getNumSignBits
unsigned getNumSignBits() const
Computes the number of leading bits of this APInt that are equal to its sign bit.
Definition:APInt.h:1607
llvm::APInt::getSignedMinValue
static APInt getSignedMinValue(unsigned numBits)
Gets minimum signed value of APInt for a specific bit width.
Definition:APInt.h:219
llvm::APInt::sextOrTrunc
APInt sextOrTrunc(unsigned width) const
Sign extend or truncate to width.
Definition:APInt.cpp:1015
llvm::APInt::isStrictlyPositive
bool isStrictlyPositive() const
Determine if this APInt Value is positive.
Definition:APInt.h:356
llvm::APInt::logBase2
unsigned logBase2() const
Definition:APInt.h:1739
llvm::APInt::ashr
APInt ashr(unsigned ShiftAmt) const
Arithmetic right-shift function.
Definition:APInt.h:827
llvm::APInt::setAllBits
void setAllBits()
Set every bit to 1.
Definition:APInt.h:1319
llvm::APInt::getBoolValue
bool getBoolValue() const
Convert APInt to a boolean value.
Definition:APInt.h:471
llvm::APInt::isMaxSignedValue
bool isMaxSignedValue() const
Determine if this is the largest signed value.
Definition:APInt.h:405
llvm::APInt::isNonNegative
bool isNonNegative() const
Determine if this APInt Value is non-negative (>= 0)
Definition:APInt.h:334
llvm::APInt::ule
bool ule(const APInt &RHS) const
Unsigned less or equal comparison.
Definition:APInt.h:1150
llvm::APInt::shl
APInt shl(unsigned shiftAmt) const
Left-shift function.
Definition:APInt.h:873
llvm::APInt::byteSwap
APInt byteSwap() const
Definition:APInt.cpp:719
llvm::APInt::slt
bool slt(const APInt &RHS) const
Signed less than comparison.
Definition:APInt.h:1130
llvm::APInt::getHighBitsSet
static APInt getHighBitsSet(unsigned numBits, unsigned hiBitsSet)
Constructs an APInt value that has the top hiBitsSet bits set.
Definition:APInt.h:296
llvm::APInt::getZero
static APInt getZero(unsigned numBits)
Get the '0' value for the specified bit-width.
Definition:APInt.h:200
llvm::APInt::setLowBits
void setLowBits(unsigned loBits)
Set the bottom loBits bits.
Definition:APInt.h:1389
llvm::APInt::sge
bool sge(const APInt &RHS) const
Signed greater or equal comparison.
Definition:APInt.h:1237
llvm::APInt::getBitsSetFrom
static APInt getBitsSetFrom(unsigned numBits, unsigned loBit)
Constructs an APInt value that has a contiguous range of bits set.
Definition:APInt.h:286
llvm::APInt::getOneBitSet
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition:APInt.h:239
llvm::APInt::lshrInPlace
void lshrInPlace(unsigned ShiftAmt)
Logical right-shift this APInt by ShiftAmt in place.
Definition:APInt.h:858
llvm::APInt::lshr
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition:APInt.h:851
llvm::APInt::uge
bool uge(const APInt &RHS) const
Unsigned greater or equal comparison.
Definition:APInt.h:1221
llvm::APInt::clearSignBit
void clearSignBit()
Set the sign bit to 0.
Definition:APInt.h:1431
llvm::AddOperator
Definition:Operator.h:405
llvm::AllocaInst
an instruction to allocate memory on the stack
Definition:Instructions.h:63
llvm::Argument
This class represents an incoming formal argument to a Function.
Definition:Argument.h:31
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition:ArrayRef.h:41
llvm::ArrayRef::end
iterator end() const
Definition:ArrayRef.h:157
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition:ArrayRef.h:168
llvm::ArrayRef::begin
iterator begin() const
Definition:ArrayRef.h:156
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition:ArrayRef.h:163
llvm::ArrayRef::slice
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
llvm::ArrayType
Class to represent array types.
Definition:DerivedTypes.h:395
llvm::ArrayType::getElementType
Type * getElementType() const
Definition:DerivedTypes.h:408
llvm::AssumeInst
This represents the llvm.assume intrinsic.
Definition:IntrinsicInst.h:1843
llvm::AssumptionCache
A cache of @llvm.assume calls within a function.
Definition:AssumptionCache.h:42
llvm::AssumptionCache::ExprResultIdx
@ ExprResultIdx
Definition:AssumptionCache.h:46
llvm::AssumptionCache::assumptionsFor
MutableArrayRef< ResultElem > assumptionsFor(const Value *V)
Access the list of assumptions which affect this value.
Definition:AssumptionCache.h:157
llvm::Attribute
Definition:Attributes.h:67
llvm::Attribute::getVScaleRangeMax
std::optional< unsigned > getVScaleRangeMax() const
Returns the maximum value for the vscale_range attribute or std::nullopt when unknown.
Definition:Attributes.cpp:466
llvm::Attribute::getVScaleRangeMin
unsigned getVScaleRangeMin() const
Returns the minimum value for the vscale_range attribute.
Definition:Attributes.cpp:460
llvm::Attribute::isValid
bool isValid() const
Return true if the attribute is any kind of attribute.
Definition:Attributes.h:208
llvm::BasicBlockEdge
Definition:Dominators.h:94
llvm::BasicBlockEdge::isSingleEdge
bool isSingleEdge() const
Check if this is the only edge between Start and End.
Definition:Dominators.cpp:51
llvm::BasicBlock
LLVM Basic Block Representation.
Definition:BasicBlock.h:61
llvm::BasicBlock::end
iterator end()
Definition:BasicBlock.h:474
llvm::BasicBlock::begin
iterator begin()
Instruction iterator methods.
Definition:BasicBlock.h:461
llvm::BasicBlock::getFirstNonPHIIt
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
llvm::BasicBlock::const_iterator
InstListType::const_iterator const_iterator
Definition:BasicBlock.h:178
llvm::BasicBlock::getSinglePredecessor
const BasicBlock * getSinglePredecessor() const
Return the predecessor of this block if it has a single predecessor block.
Definition:BasicBlock.cpp:481
llvm::BasicBlock::getSingleSuccessor
const BasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
Definition:BasicBlock.cpp:511
llvm::BasicBlock::getParent
const Function * getParent() const
Return the enclosing method, or null if none.
Definition:BasicBlock.h:220
llvm::BasicBlock::iterator
InstListType::iterator iterator
Instruction iterators...
Definition:BasicBlock.h:177
llvm::BasicBlock::getTerminator
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
llvm::BinaryOpIntrinsic::getBinaryOp
Instruction::BinaryOps getBinaryOp() const
Returns the binary operation underlying the intrinsic.
Definition:IntrinsicInst.cpp:802
llvm::BinaryOperator
Definition:InstrTypes.h:170
llvm::BinaryOperator::getOpcode
BinaryOps getOpcode() const
Definition:InstrTypes.h:370
llvm::BranchInst
Conditional or Unconditional Branch instruction.
Definition:Instructions.h:3016
llvm::CallBase
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition:InstrTypes.h:1112
llvm::CallBase::getCalledFunction
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
Definition:InstrTypes.h:1341
llvm::CallBase::paramHasAttr
bool paramHasAttr(unsigned ArgNo, Attribute::AttrKind Kind) const
Determine whether the argument or parameter has the given attribute.
Definition:Instructions.cpp:409
llvm::CallBase::isIndirectCall
bool isIndirectCall() const
Return true if the callsite is an indirect call.
Definition:Instructions.cpp:334
llvm::CallBase::onlyReadsMemory
bool onlyReadsMemory(unsigned OpNo) const
Definition:InstrTypes.h:1721
llvm::CallBase::getCalledOperand
Value * getCalledOperand() const
Definition:InstrTypes.h:1334
llvm::CallBase::getArgOperand
Value * getArgOperand(unsigned i) const
Definition:InstrTypes.h:1286
llvm::CallBase::arg_size
unsigned arg_size() const
Definition:InstrTypes.h:1284
llvm::CallInst
This class represents a function call, abstracting a target machine's calling convention.
Definition:Instructions.h:1479
llvm::CastInst
This is the base class for all instructions that perform data casts.
Definition:InstrTypes.h:444
llvm::CmpInst
This class is the base class for the comparison instructions.
Definition:InstrTypes.h:661
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition:InstrTypes.h:673
llvm::CmpInst::FCMP_OEQ
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition:InstrTypes.h:676
llvm::CmpInst::FCMP_TRUE
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition:InstrTypes.h:690
llvm::CmpInst::ICMP_SLT
@ ICMP_SLT
signed less than
Definition:InstrTypes.h:702
llvm::CmpInst::ICMP_SLE
@ ICMP_SLE
signed less or equal
Definition:InstrTypes.h:703
llvm::CmpInst::FCMP_OLT
@ FCMP_OLT
0 1 0 0 True if ordered and less than
Definition:InstrTypes.h:679
llvm::CmpInst::FCMP_ULE
@ FCMP_ULE
1 1 0 1 True if unordered, less than, or equal
Definition:InstrTypes.h:688
llvm::CmpInst::FCMP_OGT
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
Definition:InstrTypes.h:677
llvm::CmpInst::FCMP_OGE
@ FCMP_OGE
0 0 1 1 True if ordered and greater than or equal
Definition:InstrTypes.h:678
llvm::CmpInst::ICMP_UGE
@ ICMP_UGE
unsigned greater or equal
Definition:InstrTypes.h:697
llvm::CmpInst::ICMP_UGT
@ ICMP_UGT
unsigned greater than
Definition:InstrTypes.h:696
llvm::CmpInst::ICMP_SGT
@ ICMP_SGT
signed greater than
Definition:InstrTypes.h:700
llvm::CmpInst::FCMP_ULT
@ FCMP_ULT
1 1 0 0 True if unordered or less than
Definition:InstrTypes.h:687
llvm::CmpInst::FCMP_ONE
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition:InstrTypes.h:681
llvm::CmpInst::FCMP_UEQ
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition:InstrTypes.h:684
llvm::CmpInst::ICMP_ULT
@ ICMP_ULT
unsigned less than
Definition:InstrTypes.h:698
llvm::CmpInst::FCMP_UGT
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
Definition:InstrTypes.h:685
llvm::CmpInst::FCMP_OLE
@ FCMP_OLE
0 1 0 1 True if ordered and less than or equal
Definition:InstrTypes.h:680
llvm::CmpInst::FCMP_ORD
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition:InstrTypes.h:682
llvm::CmpInst::ICMP_SGE
@ ICMP_SGE
signed greater or equal
Definition:InstrTypes.h:701
llvm::CmpInst::FCMP_UNE
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition:InstrTypes.h:689
llvm::CmpInst::ICMP_ULE
@ ICMP_ULE
unsigned less or equal
Definition:InstrTypes.h:699
llvm::CmpInst::FCMP_UGE
@ FCMP_UGE
1 0 1 1 True if unordered, greater than, or equal
Definition:InstrTypes.h:686
llvm::CmpInst::FCMP_FALSE
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition:InstrTypes.h:675
llvm::CmpInst::FCMP_UNO
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition:InstrTypes.h:683
llvm::CmpInst::isSigned
bool isSigned() const
Definition:InstrTypes.h:928
llvm::CmpInst::isEquality
static bool isEquality(Predicate pred)
Determine if this is an equals/not equals predicate.
Definition:Instructions.cpp:3476
llvm::CmpInst::getSwappedPredicate
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition:InstrTypes.h:825
llvm::CmpInst::isTrueWhenEqual
bool isTrueWhenEqual() const
This is just a convenience.
Definition:InstrTypes.h:940
llvm::CmpInst::isFPPredicate
bool isFPPredicate() const
Definition:InstrTypes.h:780
llvm::CmpInst::getInversePredicate
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition:InstrTypes.h:787
llvm::CmpInst::getPredicate
Predicate getPredicate() const
Return the predicate for this instruction.
Definition:InstrTypes.h:763
llvm::CmpInst::isUnordered
static bool isUnordered(Predicate predicate)
Determine if the predicate is an unordered operation.
Definition:Instructions.cpp:3864
llvm::CmpInst::getFlippedStrictnessPredicate
Predicate getFlippedStrictnessPredicate() const
For predicate of kind "is X or equal to 0" returns the predicate "is X".
Definition:InstrTypes.h:891
llvm::CmpInst::isIntPredicate
bool isIntPredicate() const
Definition:InstrTypes.h:781
llvm::CmpInst::isOrdered
static bool isOrdered(Predicate predicate)
Determine if the predicate is an ordered operation.
Definition:Instructions.cpp:3855
llvm::CmpInst::isUnsigned
bool isUnsigned() const
Definition:InstrTypes.h:934
llvm::CmpPredicate
An abstraction over a floating-point predicate, and a pack of an integer predicate with samesign info...
Definition:CmpPredicate.h:22
llvm::CmpPredicate::getMatching
static std::optional< CmpPredicate > getMatching(CmpPredicate A, CmpPredicate B)
Compares two CmpPredicates taking samesign into account and returns the canonicalized CmpPredicate if...
Definition:Instructions.cpp:3938
llvm::CmpPredicate::getPreferredSignedPredicate
CmpInst::Predicate getPreferredSignedPredicate() const
Attempts to return a signed CmpInst::Predicate from the CmpPredicate.
Definition:Instructions.cpp:3953
llvm::CmpPredicate::hasSameSign
bool hasSameSign() const
Query samesign information, for optimizations.
Definition:CmpPredicate.h:42
llvm::ConstantDataArray
An array constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition:Constants.h:696
llvm::ConstantDataSequential
ConstantDataSequential - A vector or array constant whose element type is a simple 1/2/4/8-byte integ...
Definition:Constants.h:587
llvm::ConstantDataSequential::getAsString
StringRef getAsString() const
If this array is isString(), then this method returns the array as a StringRef.
Definition:Constants.h:662
llvm::ConstantDataSequential::getElementAsInteger
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
llvm::ConstantDataVector
A vector constant whose element type is a simple 1/2/4/8-byte integer or float/double,...
Definition:Constants.h:770
llvm::ConstantExpr::getAdd
static Constant * getAdd(Constant *C1, Constant *C2, bool HasNUW=false, bool HasNSW=false)
Definition:Constants.cpp:2638
llvm::ConstantExpr::getBitCast
static Constant * getBitCast(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition:Constants.cpp:2321
llvm::ConstantExpr::getTrunc
static Constant * getTrunc(Constant *C, Type *Ty, bool OnlyIfReduced=false)
Definition:Constants.cpp:2279
llvm::ConstantFP
ConstantFP - Floating Point Values [float, double].
Definition:Constants.h:271
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition:Constants.h:83
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition:Constants.cpp:866
llvm::ConstantInt::isZero
bool isZero() const
This is just a convenience method to make client code smaller for a common code.
Definition:Constants.h:208
llvm::ConstantInt::getZExtValue
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
llvm::ConstantRange
This class represents a range of values.
Definition:ConstantRange.h:47
llvm::ConstantRange::PreferredRangeType
PreferredRangeType
If represented precisely, the result of some range operations may consist of multiple disjoint ranges...
Definition:ConstantRange.h:327
llvm::ConstantRange::Signed
@ Signed
Definition:ConstantRange.h:327
llvm::ConstantRange::Unsigned
@ Unsigned
Definition:ConstantRange.h:327
llvm::ConstantRange::getSingleElement
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
Definition:ConstantRange.h:251
llvm::ConstantRange::fromKnownBits
static ConstantRange fromKnownBits(const KnownBits &Known, bool IsSigned)
Initialize a range based on a known bits constraint.
Definition:ConstantRange.cpp:60
llvm::ConstantRange::unsignedSubMayOverflow
OverflowResult unsignedSubMayOverflow(const ConstantRange &Other) const
Return whether unsigned sub of the two ranges always/never overflows.
Definition:ConstantRange.cpp:2183
llvm::ConstantRange::isAllNegative
bool isAllNegative() const
Return true if all values in this range are negative.
Definition:ConstantRange.cpp:458
llvm::ConstantRange::unsignedAddMayOverflow
OverflowResult unsignedAddMayOverflow(const ConstantRange &Other) const
Return whether unsigned add of the two ranges always/never overflows.
Definition:ConstantRange.cpp:2137
llvm::ConstantRange::toKnownBits
KnownBits toKnownBits() const
Return known bits for values in this range.
Definition:ConstantRange.cpp:80
llvm::ConstantRange::getUnsignedMin
APInt getUnsignedMin() const
Return the smallest unsigned value contained in the ConstantRange.
Definition:ConstantRange.cpp:489
llvm::ConstantRange::icmp
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...
Definition:ConstantRange.cpp:243
llvm::ConstantRange::getSignedMin
APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
Definition:ConstantRange.cpp:501
llvm::ConstantRange::unsignedMulMayOverflow
OverflowResult unsignedMulMayOverflow(const ConstantRange &Other) const
Return whether unsigned mul of the two ranges always/never overflows.
Definition:ConstantRange.cpp:2229
llvm::ConstantRange::isAllNonNegative
bool isAllNonNegative() const
Return true if all values in this range are non-negative.
Definition:ConstantRange.cpp:468
llvm::ConstantRange::makeAllowedICmpRegion
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...
Definition:ConstantRange.cpp:98
llvm::ConstantRange::unionWith
ConstantRange unionWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the union of this range with another range.
Definition:ConstantRange.cpp:687
llvm::ConstantRange::makeExactICmpRegion
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...
Definition:ConstantRange.cpp:158
llvm::ConstantRange::contains
bool contains(const APInt &Val) const
Return true if the specified value is in the set.
Definition:ConstantRange.cpp:507
llvm::ConstantRange::signedAddMayOverflow
OverflowResult signedAddMayOverflow(const ConstantRange &Other) const
Return whether signed add of the two ranges always/never overflows.
Definition:ConstantRange.cpp:2153
llvm::ConstantRange::getUnsignedMax
APInt getUnsignedMax() const
Return the largest unsigned value contained in the ConstantRange.
Definition:ConstantRange.cpp:483
llvm::ConstantRange::intersectWith
ConstantRange intersectWith(const ConstantRange &CR, PreferredRangeType Type=Smallest) const
Return the range that results from the intersection of this range with another range.
Definition:ConstantRange.cpp:581
llvm::ConstantRange::OverflowResult
OverflowResult
Represents whether an operation on the given constant range is known to always or never overflow.
Definition:ConstantRange.h:567
llvm::ConstantRange::OverflowResult::NeverOverflows
@ NeverOverflows
Never overflows.
llvm::ConstantRange::OverflowResult::AlwaysOverflowsHigh
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
llvm::ConstantRange::OverflowResult::AlwaysOverflowsLow
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
llvm::ConstantRange::OverflowResult::MayOverflow
@ MayOverflow
May or may not overflow.
llvm::ConstantRange::getNonEmpty
static ConstantRange getNonEmpty(APInt Lower, APInt Upper)
Create non-empty constant range with the given bounds.
Definition:ConstantRange.h:84
llvm::ConstantRange::getBitWidth
uint32_t getBitWidth() const
Get the bit width of this ConstantRange.
Definition:ConstantRange.h:209
llvm::ConstantRange::signedSubMayOverflow
OverflowResult signedSubMayOverflow(const ConstantRange &Other) const
Return whether signed sub of the two ranges always/never overflows.
Definition:ConstantRange.cpp:2199
llvm::ConstantRange::sub
ConstantRange sub(const ConstantRange &Other) const
Return a new range representing the possible values resulting from a subtraction of a value in this r...
Definition:ConstantRange.cpp:1114
llvm::Constant
This is an important base class in LLVM.
Definition:Constant.h:42
llvm::Constant::replaceUndefsWith
static Constant * replaceUndefsWith(Constant *C, Constant *Replacement)
Try to replace undefined constant C or undefined elements in C with Replacement.
Definition:Constants.cpp:784
llvm::Constant::getSplatValue
Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
Definition:Constants.cpp:1708
llvm::Constant::getNullValue
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
Definition:Constants.cpp:373
llvm::Constant::getAggregateElement
Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
Definition:Constants.cpp:435
llvm::Constant::isZeroValue
bool isZeroValue() const
Return true if the value is negative zero or null value.
Definition:Constants.cpp:76
llvm::Constant::isNullValue
bool isNullValue() const
Return true if this is the value that would be returned by getNullValue.
Definition:Constants.cpp:90
llvm::DWARFExpression::Operation
This class represents an Operation in the Expression.
Definition:DWARFExpression.h:32
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition:DataLayout.h:63
llvm::DataLayout::isLittleEndian
bool isLittleEndian() const
Layout endianness...
Definition:DataLayout.h:197
llvm::DataLayout::getStructLayout
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
llvm::DataLayout::getIndexTypeSizeInBits
unsigned getIndexTypeSizeInBits(Type *Ty) const
Layout size of the index used in GEP calculation.
Definition:DataLayout.cpp:754
llvm::DataLayout::getPointerTypeSizeInBits
unsigned getPointerTypeSizeInBits(Type *) const
Layout pointer size, in bits, based on the type.
Definition:DataLayout.cpp:743
llvm::DataLayout::getTypeSizeInBits
TypeSize getTypeSizeInBits(Type *Ty) const
Size examples:
Definition:DataLayout.h:617
llvm::DomConditionCache::conditionsFor
ArrayRef< BranchInst * > conditionsFor(const Value *V) const
Access the list of branches which affect this value.
Definition:DomConditionCache.h:43
llvm::DomTreeNodeBase::getIDom
DomTreeNodeBase * getIDom() const
Definition:GenericDomTree.h:90
llvm::DomTreeNodeBase::getBlock
NodeT * getBlock() const
Definition:GenericDomTree.h:89
llvm::DominatorTreeBase::getNode
DomTreeNodeBase< NodeT > * getNode(const NodeT *BB) const
getNode - return the (Post)DominatorTree node for the specified basic block.
Definition:GenericDomTree.h:401
llvm::DominatorTree
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition:Dominators.h:162
llvm::DominatorTree::dominates
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
llvm::ExtractValueInst
This instruction extracts a struct member or array element value from an aggregate value.
Definition:Instructions.h:2397
llvm::ExtractValueInst::getIndices
ArrayRef< unsigned > getIndices() const
Definition:Instructions.h:2449
llvm::ExtractValueInst::getNumIndices
unsigned getNumIndices() const
Definition:Instructions.h:2453
llvm::ExtractValueInst::getIndexedType
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...
Definition:Instructions.cpp:2499
llvm::ExtractValueInst::getAggregateOperand
Value * getAggregateOperand()
Definition:Instructions.h:2439
llvm::FPMathOperator
Utility class for floating point operations which can have information about relaxed accuracy require...
Definition:Operator.h:205
llvm::FastMathFlags
Convenience struct for specifying and reasoning about fast-math flags.
Definition:FMF.h:20
llvm::FastMathFlags::noSignedZeros
bool noSignedZeros() const
Definition:FMF.h:68
llvm::FastMathFlags::setNoSignedZeros
void setNoSignedZeros(bool B=true)
Definition:FMF.h:85
llvm::FastMathFlags::noNaNs
bool noNaNs() const
Definition:FMF.h:66
llvm::Function
Definition:Function.h:63
llvm::Function::getEntryBlock
const BasicBlock & getEntryBlock() const
Definition:Function.h:809
llvm::Function::getDenormalMode
DenormalMode getDenormalMode(const fltSemantics &FPType) const
Returns the denormal handling type for the default rounding mode of the function.
Definition:Function.cpp:807
llvm::GEPOperator
Definition:Operator.h:425
llvm::GetElementPtrInst
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition:Instructions.h:933
llvm::GlobalAlias
Definition:GlobalAlias.h:28
llvm::GlobalValue
Definition:GlobalValue.h:48
llvm::GlobalValue::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout of the module this global belongs to.
Definition:Globals.cpp:130
llvm::GlobalValue::getValueType
Type * getValueType() const
Definition:GlobalValue.h:297
llvm::GlobalVariable
Definition:GlobalVariable.h:39
llvm::GlobalVariable::getInitializer
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
Definition:GlobalVariable.h:150
llvm::GlobalVariable::isConstant
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
Definition:GlobalVariable.h:173
llvm::GlobalVariable::hasDefinitiveInitializer
bool hasDefinitiveInitializer() const
hasDefinitiveInitializer - Whether the global variable has an initializer, and any other instances of...
Definition:GlobalVariable.h:124
llvm::ICmpInst
This instruction compares its operands according to the predicate given to the constructor.
Definition:Instructions.h:1158
llvm::ICmpInst::getSwappedCmpPredicate
CmpPredicate getSwappedCmpPredicate() const
Definition:Instructions.h:1230
llvm::ICmpInst::getFlippedSignednessPredicate
Predicate getFlippedSignednessPredicate() const
For example, SLT->ULT, ULT->SLT, SLE->ULE, ULE->SLE, EQ->EQ.
Definition:Instructions.h:1265
llvm::ICmpInst::isEquality
bool isEquality() const
Return true if this predicate is either EQ or NE.
Definition:Instructions.h:1291
llvm::ICmpInst::isImpliedByMatchingCmp
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,...
Definition:Instructions.cpp:3925
llvm::ICmpInst::isRelational
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Definition:Instructions.h:1305
llvm::ICmpInst::getUnsignedPredicate
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
Definition:Instructions.h:1249
llvm::Init
Definition:Record.h:285
llvm::InsertValueInst
This instruction inserts a struct field of array element value into an aggregate value.
Definition:Instructions.h:2485
llvm::InsertValueInst::getAggregateOperand
Value * getAggregateOperand()
Definition:Instructions.h:2537
llvm::InsertValueInst::Create
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Definition:Instructions.h:2519
llvm::Instruction
Definition:Instruction.h:68
llvm::Instruction::hasNoUnsignedWrap
bool hasNoUnsignedWrap() const LLVM_READONLY
Determine whether the no unsigned wrap flag is set.
Definition:Instruction.cpp:403
llvm::Instruction::hasNoSignedWrap
bool hasNoSignedWrap() const LLVM_READONLY
Determine whether the no signed wrap flag is set.
Definition:Instruction.cpp:410
llvm::Instruction::isBinaryOp
bool isBinaryOp() const
Definition:Instruction.h:315
llvm::Instruction::eraseFromParent
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Definition:Instruction.cpp:94
llvm::Instruction::isExact
bool isExact() const LLVM_READONLY
Determine whether the exact flag is set.
Definition:Instruction.cpp:557
llvm::Instruction::getFunction
const Function * getFunction() const
Return the function this instruction belongs to.
Definition:Instruction.cpp:72
llvm::Instruction::comesBefore
bool comesBefore(const Instruction *Other) const
Given an instruction Other in the same basic block as this instruction, return true if this instructi...
Definition:Instruction.cpp:334
llvm::Instruction::getFastMathFlags
FastMathFlags getFastMathFlags() const LLVM_READONLY
Convenience function for getting all the fast-math flags, which must be an operator which supports th...
Definition:Instruction.cpp:651
llvm::Instruction::getOpcode
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition:Instruction.h:310
llvm::Instruction::isUnaryOp
bool isUnaryOp() const
Definition:Instruction.h:314
llvm::Instruction::getDataLayout
const DataLayout & getDataLayout() const
Get the data layout of the module this instruction belongs to.
Definition:Instruction.cpp:76
llvm::Instruction::CastOps
CastOps
Definition:Instruction.h:1022
llvm::IntrinsicInst
A wrapper class for inspecting calls to intrinsic functions.
Definition:IntrinsicInst.h:48
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition:LLVMContext.h:67
llvm::LoadInst
An instruction for reading from memory.
Definition:Instructions.h:176
llvm::LoadInst::getPointerOperand
Value * getPointerOperand()
Definition:Instructions.h:255
llvm::LoadInst::getAlign
Align getAlign() const
Return the alignment of the access that is being performed.
Definition:Instructions.h:211
llvm::LoopInfoBase::isLoopHeader
bool isLoopHeader(const BlockT *BB) const
Definition:GenericLoopInfo.h:619
llvm::LoopInfoBase::getLoopFor
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
Definition:GenericLoopInfo.h:606
llvm::LoopInfo
Definition:LoopInfo.h:407
llvm::Loop
Represents a single loop in the control flow graph.
Definition:LoopInfo.h:39
llvm::MDNode
Metadata node.
Definition:Metadata.h:1073
llvm::Operator
This is a utility class that provides an abstraction for the common functionality between Instruction...
Definition:Operator.h:32
llvm::Operator::getOpcode
unsigned getOpcode() const
Return the opcode for this Instruction or ConstantExpr.
Definition:Operator.h:42
llvm::OverflowingBinaryOperator
Utility class for integer operators which may exhibit overflow - Add, Sub, Mul, and Shl.
Definition:Operator.h:77
llvm::PHINode
Definition:Instructions.h:2600
llvm::PHINode::blocks
iterator_range< const_block_iterator > blocks() const
Definition:Instructions.h:2661
llvm::PHINode::getIncomingValueForBlock
Value * getIncomingValueForBlock(const BasicBlock *BB) const
Definition:Instructions.h:2775
llvm::PHINode::getIncomingBlock
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Definition:Instructions.h:2695
llvm::PHINode::getIncomingValue
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
Definition:Instructions.h:2675
llvm::PHINode::getNumIncomingValues
unsigned getNumIncomingValues() const
Return the number of incoming edges.
Definition:Instructions.h:2671
llvm::PoisonValue::get
static PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
Definition:Constants.cpp:1878
llvm::PossiblyExactOperator
A udiv or sdiv instruction, which can be marked as "exact", indicating that no bits are destroyed.
Definition:Operator.h:155
llvm::PossiblyExactOperator::isExact
bool isExact() const
Test whether this division is known to be exact, with zero remainder.
Definition:Operator.h:174
llvm::SelectInst
This class represents the LLVM 'select' instruction.
Definition:Instructions.h:1657
llvm::SelectInst::getFalseValue
const Value * getFalseValue() const
Definition:Instructions.h:1695
llvm::SelectInst::getCondition
const Value * getCondition() const
Definition:Instructions.h:1693
llvm::SelectInst::getTrueValue
const Value * getTrueValue() const
Definition:Instructions.h:1694
llvm::ShuffleVectorInst
This instruction constructs a fixed permutation of two input vectors.
Definition:Instructions.h:1901
llvm::ShuffleVectorInst::getType
VectorType * getType() const
Overload to return most specific vector type.
Definition:Instructions.h:1941
llvm::ShuffleVectorInst::getShuffleMask
static void getShuffleMask(const Constant *Mask, SmallVectorImpl< int > &Result)
Convert the input shuffle mask operand to a vector of integers.
Definition:Instructions.cpp:1788
llvm::SmallPtrSetImplBase::size
size_type size() const
Definition:SmallPtrSet.h:94
llvm::SmallPtrSetImpl
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
Definition:SmallPtrSet.h:363
llvm::SmallPtrSetImpl::count
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
Definition:SmallPtrSet.h:452
llvm::SmallPtrSetImpl::insert
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
llvm::SmallPtrSet
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
Definition:SmallPtrSet.h:519
llvm::SmallSet
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition:SmallSet.h:132
llvm::SmallSet::count
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition:SmallSet.h:175
llvm::SmallSet::insert
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
llvm::SmallVectorBase::empty
bool empty() const
Definition:SmallVector.h:81
llvm::SmallVectorBase::size
size_t size() const
Definition:SmallVector.h:78
llvm::SmallVectorImpl
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition:SmallVector.h:573
llvm::SmallVectorImpl::pop_back_val
T pop_back_val()
Definition:SmallVector.h:673
llvm::SmallVectorImpl::reserve
void reserve(size_type N)
Definition:SmallVector.h:663
llvm::SmallVectorImpl::append
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
Definition:SmallVector.h:683
llvm::SmallVectorImpl::clear
void clear()
Definition:SmallVector.h:610
llvm::SmallVectorTemplateBase::pop_back
void pop_back()
Definition:SmallVector.h:425
llvm::SmallVectorTemplateBase::push_back
void push_back(const T &Elt)
Definition:SmallVector.h:413
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition:SmallVector.h:1196
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::StructLayout
Used to lazily calculate structure layout information for a target machine, based on the DataLayout s...
Definition:DataLayout.h:567
llvm::StructLayout::getElementOffset
TypeSize getElementOffset(unsigned Idx) const
Definition:DataLayout.h:596
llvm::StructType
Class to represent struct types.
Definition:DerivedTypes.h:218
llvm::StructType::getNumElements
unsigned getNumElements() const
Random access to the elements.
Definition:DerivedTypes.h:365
llvm::StructType::getElementType
Type * getElementType(unsigned N) const
Definition:DerivedTypes.h:366
llvm::TargetLibraryInfo
Provides information about what library functions are available for the current target.
Definition:TargetLibraryInfo.h:280
llvm::TargetLibraryInfo::getLibFunc
bool getLibFunc(StringRef funcName, LibFunc &F) const
Searches for a particular function name.
Definition:TargetLibraryInfo.h:345
llvm::TypeSize
Definition:TypeSize.h:334
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition:Type.h:45
llvm::Type::getIntegerBitWidth
unsigned getIntegerBitWidth() const
llvm::Type::getFltSemantics
const fltSemantics & getFltSemantics() const
llvm::Type::isVectorTy
bool isVectorTy() const
True if this is an instance of VectorType.
Definition:Type.h:270
llvm::Type::isIntOrIntVectorTy
bool isIntOrIntVectorTy() const
Return true if this is an integer type or a vector of integer types.
Definition:Type.h:243
llvm::Type::isPointerTy
bool isPointerTy() const
True if this is an instance of PointerType.
Definition:Type.h:264
llvm::Type::getArrayNumElements
uint64_t getArrayNumElements() const
llvm::Type::getIntNTy
static IntegerType * getIntNTy(LLVMContext &C, unsigned N)
llvm::Type::getScalarSizeInBits
unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
llvm::Type::isSized
bool isSized(SmallPtrSetImpl< Type * > *Visited=nullptr) const
Return true if it makes sense to take the size of this type.
Definition:Type.h:310
llvm::Type::getInt16Ty
static IntegerType * getInt16Ty(LLVMContext &C)
llvm::Type::getInt8Ty
static IntegerType * getInt8Ty(LLVMContext &C)
llvm::Type::isIEEE
bool isIEEE() const
Return whether the type is IEEE compatible, as defined by the eponymous method in APFloat.
llvm::Type::isPtrOrPtrVectorTy
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition:Type.h:267
llvm::Type::isIntOrPtrTy
bool isIntOrPtrTy() const
Return true if this is an integer type or a pointer type.
Definition:Type.h:252
llvm::Type::getInt32Ty
static IntegerType * getInt32Ty(LLVMContext &C)
llvm::Type::getInt64Ty
static IntegerType * getInt64Ty(LLVMContext &C)
llvm::Type::isIntegerTy
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition:Type.h:237
llvm::Type::getScalarType
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition:Type.h:355
llvm::UndefValue::get
static UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
Definition:Constants.cpp:1859
llvm::Use
A Use represents the edge between a Value definition and its users.
Definition:Use.h:43
llvm::Use::getUser
User * getUser() const
Returns the User that contains this Use.
Definition:Use.h:72
llvm::Use::getOperandNo
unsigned getOperandNo() const
Return the operand # of this use in its User.
Definition:Use.cpp:31
llvm::User
Definition:User.h:44
llvm::User::operands
op_range operands()
Definition:User.h:288
llvm::User::getOperand
Value * getOperand(unsigned i) const
Definition:User.h:228
llvm::User::getNumOperands
unsigned getNumOperands() const
Definition:User.h:250
llvm::Value
LLVM Value Representation.
Definition:Value.h:74
llvm::Value::getType
Type * getType() const
All values are typed, get the type of this value.
Definition:Value.h:255
llvm::Value::stripAndAccumulateInBoundsConstantOffsets
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
llvm::Value::users
iterator_range< user_iterator > users()
Definition:Value.h:421
llvm::WithCache
Definition:WithCache.h:27
llvm::WithCache::getKnownBits
const KnownBits & getKnownBits(const SimplifyQuery &Q) const
Definition:WithCache.h:58
llvm::WithCache::getValue
PointerType getValue() const
Definition:WithCache.h:56
llvm::WithOverflowInst
Represents an op.with.overflow intrinsic.
Definition:IntrinsicInst.h:927
llvm::cl::opt
Definition:CommandLine.h:1423
llvm::details::FixedOrScalableQuantity::getFixedValue
constexpr ScalarTy getFixedValue() const
Definition:TypeSize.h:202
llvm::details::FixedOrScalableQuantity::isScalable
constexpr bool isScalable() const
Returns whether the quantity is scaled by a runtime quantity (vscale).
Definition:TypeSize.h:171
llvm::details::FixedOrScalableQuantity::getKnownMinValue
constexpr ScalarTy getKnownMinValue() const
Returns the minimum value this quantity can represent.
Definition:TypeSize.h:168
llvm::function_ref
An efficient, type-erasing, non-owning reference to a callable.
Definition:STLFunctionalExtras.h:37
llvm::generic_gep_type_iterator
Definition:GetElementPtrTypeIterator.h:31
llvm::generic_gep_type_iterator::getStructTypeOrNull
StructType * getStructTypeOrNull() const
Definition:GetElementPtrTypeIterator.h:166
llvm::generic_gep_type_iterator::getSequentialElementStride
TypeSize getSequentialElementStride(const DataLayout &DL) const
Definition:GetElementPtrTypeIterator.h:154
llvm::generic_gep_type_iterator::getIndexedType
Type * getIndexedType() const
Definition:GetElementPtrTypeIterator.h:102
llvm::ilist_detail::node_parent_access::getParent
const ParentTy * getParent() const
Definition:ilist_node.h:32
llvm::ilist_node_impl::getIterator
self_iterator getIterator()
Definition:ilist_node.h:132
llvm::iterator_range
A range adaptor for a pair of iterators.
Definition:iterator_range.h:42
uint64_t
unsigned
iterator_range.h
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
UINT64_MAX
#define UINT64_MAX
Definition:DataTypes.h:77
ErrorHandling.h
llvm_unreachable
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Definition:ErrorHandling.h:143
llvm::CallingConv::C
@ C
The default llvm calling convention, compatible with C.
Definition:CallingConv.h:34
llvm::Intrinsic::not_intrinsic
@ not_intrinsic
Definition:Intrinsics.h:44
llvm::Intrinsic::ID
unsigned ID
Definition:GenericSSAContext.h:28
llvm::PatternMatch
Definition:PatternMatch.h:47
llvm::PatternMatch::m_AllOnes
cst_pred_ty< is_all_ones > m_AllOnes()
Match an integer or vector with all bits set.
Definition:PatternMatch.h:524
llvm::PatternMatch::m_And
BinaryOp_match< LHS, RHS, Instruction::And > m_And(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1216
llvm::PatternMatch::m_PtrToIntSameSize
PtrToIntSameSize_match< OpTy > m_PtrToIntSameSize(const DataLayout &DL, const OpTy &Op)
Definition:PatternMatch.h:2061
llvm::PatternMatch::m_Add
BinaryOp_match< LHS, RHS, Instruction::Add > m_Add(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1102
llvm::PatternMatch::m_FCmp
CmpClass_match< LHS, RHS, FCmpInst > m_FCmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Definition:PatternMatch.h:1633
llvm::PatternMatch::m_SignMask
cst_pred_ty< is_sign_mask > m_SignMask()
Match an integer or vector with only the sign bit(s) set.
Definition:PatternMatch.h:664
llvm::PatternMatch::m_NUWAdd
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWAdd(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1314
llvm::PatternMatch::m_Power2
cst_pred_ty< is_power2 > m_Power2()
Match an integer or vector power-of-2.
Definition:PatternMatch.h:619
llvm::PatternMatch::m_URem
BinaryOp_match< LHS, RHS, Instruction::URem > m_URem(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1198
llvm::PatternMatch::m_LogicalOp
auto m_LogicalOp()
Matches either L && R or L || R where L and R are arbitrary values.
Definition:PatternMatch.h:3119
llvm::PatternMatch::m_Constant
class_match< Constant > m_Constant()
Match an arbitrary Constant and ignore it.
Definition:PatternMatch.h:165
llvm::PatternMatch::m_c_And
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.
Definition:PatternMatch.h:2798
llvm::PatternMatch::m_Power2OrZero
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
llvm::PatternMatch::m_Trunc
CastInst_match< OpTy, TruncInst > m_Trunc(const OpTy &Op)
Matches Trunc.
Definition:PatternMatch.h:2075
llvm::PatternMatch::m_Xor
BinaryOp_match< LHS, RHS, Instruction::Xor > m_Xor(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1228
llvm::PatternMatch::m_NSWSub
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap > m_NSWSub(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1289
llvm::PatternMatch::match
bool match(Val *V, const Pattern &P)
Definition:PatternMatch.h:49
llvm::PatternMatch::m_IDiv
BinOpPred_match< LHS, RHS, is_idiv_op > m_IDiv(const LHS &L, const RHS &R)
Matches integer division operations.
Definition:PatternMatch.h:1553
llvm::PatternMatch::m_AnyZeroFP
cstfp_pred_ty< is_any_zero_fp > m_AnyZeroFP()
Match a floating-point negative zero or positive zero.
Definition:PatternMatch.h:764
llvm::PatternMatch::m_Specific
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
Definition:PatternMatch.h:885
llvm::PatternMatch::m_Shr
BinOpPred_match< LHS, RHS, is_right_shift_op > m_Shr(const LHS &L, const RHS &R)
Matches logical shift operations.
Definition:PatternMatch.h:1525
llvm::PatternMatch::m_c_ICmp
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.
Definition:PatternMatch.h:2765
llvm::PatternMatch::m_c_NUWAdd
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, true > m_c_NUWAdd(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1323
llvm::PatternMatch::m_NonNegative
cst_pred_ty< is_nonnegative > m_NonNegative()
Match an integer or vector of non-negative values.
Definition:PatternMatch.h:560
llvm::PatternMatch::m_ConstantInt
class_match< ConstantInt > m_ConstantInt()
Match an arbitrary ConstantInt and ignore it.
Definition:PatternMatch.h:168
llvm::PatternMatch::m_One
cst_pred_ty< is_one > m_One()
Match an integer 1 or a vector with all elements equal to 1.
Definition:PatternMatch.h:592
llvm::PatternMatch::m_Select
ThreeOps_match< Cond, LHS, RHS, Instruction::Select > m_Select(const Cond &C, const LHS &L, const RHS &R)
Matches SelectInst.
Definition:PatternMatch.h:1799
llvm::PatternMatch::m_OrdOrUnordFMin
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.
Definition:PatternMatch.h:2457
llvm::PatternMatch::m_ExtractValue
ExtractValue_match< Ind, Val_t > m_ExtractValue(const Val_t &V)
Match a single index ExtractValue instruction.
Definition:PatternMatch.h:2950
llvm::PatternMatch::m_SMin
MaxMin_match< ICmpInst, LHS, RHS, smin_pred_ty > m_SMin(const LHS &L, const RHS &R)
Definition:PatternMatch.h:2348
llvm::PatternMatch::m_WithOverflowInst
bind_ty< WithOverflowInst > m_WithOverflowInst(WithOverflowInst *&I)
Match a with overflow intrinsic, capturing it if we match.
Definition:PatternMatch.h:832
llvm::PatternMatch::m_c_Xor
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.
Definition:PatternMatch.h:2812
llvm::PatternMatch::m_Mul
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1168
llvm::PatternMatch::m_Deferred
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
llvm::PatternMatch::m_ZeroInt
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
llvm::PatternMatch::m_OneUse
OneUse_match< T > m_OneUse(const T &SubPattern)
Definition:PatternMatch.h:67
llvm::PatternMatch::m_c_SMin
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.
Definition:PatternMatch.h:2836
llvm::PatternMatch::m_LogicalOr
auto m_LogicalOr()
Matches L || R where L and R are arbitrary values.
Definition:PatternMatch.h:3099
llvm::PatternMatch::m_Neg
BinaryOp_match< cst_pred_ty< is_zero_int >, ValTy, Instruction::Sub > m_Neg(const ValTy &V)
Matches a 'Neg' as 'sub 0, V'.
Definition:PatternMatch.h:2820
llvm::PatternMatch::m_ImmConstant
match_combine_and< class_match< Constant >, match_unless< constantexpr_match > > m_ImmConstant()
Match an arbitrary immediate Constant and ignore it.
Definition:PatternMatch.h:864
llvm::PatternMatch::m_c_UMax
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.
Definition:PatternMatch.h:2854
llvm::PatternMatch::m_SpecificICmp
SpecificCmpClass_match< LHS, RHS, ICmpInst > m_SpecificICmp(CmpPredicate MatchPred, const LHS &L, const RHS &R)
Definition:PatternMatch.h:1690
llvm::PatternMatch::m_ZExt
CastInst_match< OpTy, ZExtInst > m_ZExt(const OpTy &Op)
Matches ZExt.
Definition:PatternMatch.h:2107
llvm::PatternMatch::m_UDiv
BinaryOp_match< LHS, RHS, Instruction::UDiv > m_UDiv(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1180
llvm::PatternMatch::m_UMax
MaxMin_match< ICmpInst, LHS, RHS, umax_pred_ty > m_UMax(const LHS &L, const RHS &R)
Definition:PatternMatch.h:2354
llvm::PatternMatch::m_Br
brc_match< Cond_t, bind_ty< BasicBlock >, bind_ty< BasicBlock > > m_Br(const Cond_t &C, BasicBlock *&T, BasicBlock *&F)
Definition:PatternMatch.h:2220
llvm::PatternMatch::m_c_UMin
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.
Definition:PatternMatch.h:2848
llvm::PatternMatch::m_c_Add
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.
Definition:PatternMatch.h:2784
llvm::PatternMatch::m_APFloatAllowPoison
apfloat_match m_APFloatAllowPoison(const APFloat *&Res)
Match APFloat while allowing poison in splat vector constants.
Definition:PatternMatch.h:322
llvm::PatternMatch::m_AddLike
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".
Definition:PatternMatch.h:1409
llvm::PatternMatch::m_OrdOrUnordFMax
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.
Definition:PatternMatch.h:2444
llvm::PatternMatch::m_c_SMax
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.
Definition:PatternMatch.h:2842
llvm::PatternMatch::m_VScale
VScaleVal_match m_VScale()
Definition:PatternMatch.h:3010
llvm::PatternMatch::m_NUWSub
OverflowingBinaryOp_match< LHS, RHS, Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap > m_NUWSub(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1332
llvm::PatternMatch::m_SMax
MaxMin_match< ICmpInst, LHS, RHS, smax_pred_ty > m_SMax(const LHS &L, const RHS &R)
Definition:PatternMatch.h:2342
llvm::PatternMatch::m_APInt
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
llvm::PatternMatch::m_NSWAddLike
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".
Definition:PatternMatch.h:1419
llvm::PatternMatch::m_Value
class_match< Value > m_Value()
Match an arbitrary value and ignore it.
Definition:PatternMatch.h:92
llvm::PatternMatch::m_c_BinOp
AnyBinaryOp_match< LHS, RHS, true > m_c_BinOp(const LHS &L, const RHS &R)
Matches a BinaryOperator with LHS and RHS in either order.
Definition:PatternMatch.h:2757
llvm::PatternMatch::m_NSWAdd
OverflowingBinaryOp_match< LHS, RHS, Instruction::Add, OverflowingBinaryOperator::NoSignedWrap > m_NSWAdd(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1281
llvm::PatternMatch::m_LShr
BinaryOp_match< LHS, RHS, Instruction::LShr > m_LShr(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1240
llvm::PatternMatch::m_ICmp
CmpClass_match< LHS, RHS, ICmpInst > m_ICmp(CmpPredicate &Pred, const LHS &L, const RHS &R)
Definition:PatternMatch.h:1627
llvm::PatternMatch::m_ZExtOrSExt
match_combine_or< CastInst_match< OpTy, ZExtInst >, CastInst_match< OpTy, SExtInst > > m_ZExtOrSExt(const OpTy &Op)
Definition:PatternMatch.h:2138
llvm::PatternMatch::m_FNeg
FNeg_match< OpTy > m_FNeg(const OpTy &X)
Match 'fneg X' as 'fsub -0.0, X'.
Definition:PatternMatch.h:1156
llvm::PatternMatch::m_Shift
BinOpPred_match< LHS, RHS, is_shift_op > m_Shift(const LHS &L, const RHS &R)
Matches shift operations.
Definition:PatternMatch.h:1518
llvm::PatternMatch::m_Shl
BinaryOp_match< LHS, RHS, Instruction::Shl > m_Shl(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1234
llvm::PatternMatch::m_IRem
BinOpPred_match< LHS, RHS, is_irem_op > m_IRem(const LHS &L, const RHS &R)
Matches integer remainder operations.
Definition:PatternMatch.h:1560
llvm::PatternMatch::m_APFloat
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
llvm::PatternMatch::m_LogicalAnd
auto m_LogicalAnd()
Matches L && R where L and R are arbitrary values.
Definition:PatternMatch.h:3081
llvm::PatternMatch::m_BasicBlock
class_match< BasicBlock > m_BasicBlock()
Match an arbitrary basic block value and ignore it.
Definition:PatternMatch.h:189
llvm::PatternMatch::m_SRem
BinaryOp_match< LHS, RHS, Instruction::SRem > m_SRem(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1204
llvm::PatternMatch::m_NonPositive
cst_pred_ty< is_nonpositive > m_NonPositive()
Match an integer or vector of non-positive values.
Definition:PatternMatch.h:582
llvm::PatternMatch::m_Not
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'.
Definition:PatternMatch.h:2467
llvm::PatternMatch::m_Or
BinaryOp_match< LHS, RHS, Instruction::Or > m_Or(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1222
llvm::PatternMatch::m_SExt
CastInst_match< OpTy, SExtInst > m_SExt(const OpTy &Op)
Matches SExt.
Definition:PatternMatch.h:2101
llvm::PatternMatch::m_Zero
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
Definition:PatternMatch.h:612
llvm::PatternMatch::m_c_Or
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.
Definition:PatternMatch.h:2805
llvm::PatternMatch::m_NUWAddLike
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".
Definition:PatternMatch.h:1429
llvm::PatternMatch::m_BitwiseLogic
BinOpPred_match< LHS, RHS, is_bitwiselogic_op > m_BitwiseLogic(const LHS &L, const RHS &R)
Matches bitwise logic operations.
Definition:PatternMatch.h:1540
llvm::PatternMatch::m_ElementWiseBitCast
ElementWiseBitCast_match< OpTy > m_ElementWiseBitCast(const OpTy &Op)
Definition:PatternMatch.h:2049
llvm::PatternMatch::m_FAbs
m_Intrinsic_Ty< Opnd0 >::Ty m_FAbs(const Opnd0 &Op0)
Definition:PatternMatch.h:2702
llvm::PatternMatch::m_PtrToInt
CastOperator_match< OpTy, Instruction::PtrToInt > m_PtrToInt(const OpTy &Op)
Matches PtrToInt.
Definition:PatternMatch.h:2056
llvm::PatternMatch::m_Sub
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
Definition:PatternMatch.h:1114
llvm::PatternMatch::m_UMin
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
Definition:PatternMatch.h:2360
llvm::PatternMatch::m_CombineOr
match_combine_or< LTy, RTy > m_CombineOr(const LTy &L, const RTy &R)
Combine two pattern matchers matching L || R.
Definition:PatternMatch.h:239
llvm::RISCVII::VLMUL
VLMUL
Definition:RISCVTargetParser.h:69
llvm::RISCVVType::decodeVSEW
static unsigned decodeVSEW(unsigned VSEW)
Definition:RISCVTargetParser.h:115
llvm::RISCVVType::getSEWLMULRatio
unsigned getSEWLMULRatio(unsigned SEW, RISCVII::VLMUL VLMul)
Definition:RISCVTargetParser.cpp:223
llvm::RISCV::RVVBitsPerBlock
static constexpr unsigned RVVBitsPerBlock
Definition:RISCVTargetParser.h:51
llvm::cl::Hidden
@ Hidden
Definition:CommandLine.h:137
llvm::cl::init
initializer< Ty > init(const Ty &Val)
Definition:CommandLine.h:443
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::haveNoCommonBitsSet
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.
Definition:ValueTracking.cpp:248
llvm::mustExecuteUBIfPoisonOnPathTo
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...
Definition:ValueTracking.cpp:7877
llvm::getInverseMinMaxIntrinsic
Intrinsic::ID getInverseMinMaxIntrinsic(Intrinsic::ID MinMaxID)
Definition:ValueTracking.cpp:9147
llvm::Offset
@ Offset
Definition:DWP.cpp:480
llvm::Length
@ Length
Definition:DWP.cpp:480
llvm::OverflowResult
OverflowResult
Definition:ValueTracking.h:899
llvm::OverflowResult::NeverOverflows
@ NeverOverflows
Never overflows.
llvm::OverflowResult::AlwaysOverflowsHigh
@ AlwaysOverflowsHigh
Always overflows in the direction of signed/unsigned max value.
llvm::OverflowResult::AlwaysOverflowsLow
@ AlwaysOverflowsLow
Always overflows in the direction of signed/unsigned min value.
llvm::OverflowResult::MayOverflow
@ MayOverflow
May or may not overflow.
llvm::all_of
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
llvm::isValidAssumeForContext
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,...
Definition:ValueTracking.cpp:509
llvm::size
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
llvm::canCreatePoison
bool canCreatePoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
Definition:ValueTracking.cpp:7643
llvm::mustTriggerUB
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...
Definition:ValueTracking.cpp:8158
llvm::make_scope_exit
detail::scope_exit< std::decay_t< Callable > > make_scope_exit(Callable &&F)
Definition:ScopeExit.h:59
llvm::isOnlyUsedInZeroEqualityComparison
bool isOnlyUsedInZeroEqualityComparison(const Instruction *CxtI)
Definition:ValueTracking.cpp:273
llvm::isSignBitCheck
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.
Definition:ValueTracking.cpp:4488
llvm::Depth
@ Depth
Definition:SIMachineScheduler.h:36
llvm::getArgumentAliasingToReturnedPointer
const Value * getArgumentAliasingToReturnedPointer(const CallBase *Call, bool MustPreserveNullness)
This function returns call pointer argument that is considered the same by aliasing rules.
Definition:ValueTracking.cpp:6706
llvm::isAssumeLikeIntrinsic
bool isAssumeLikeIntrinsic(const Instruction *I)
Return true if it is an intrinsic that cannot be speculated but also cannot trap.
Definition:ValueTracking.cpp:502
llvm::findAllocaForValue
AllocaInst * findAllocaForValue(Value *V, bool OffsetZero=false)
Returns unique alloca where the value comes from, or nullptr.
Definition:ValueTracking.cpp:6970
llvm::getMinMaxLimit
APInt getMinMaxLimit(SelectPatternFlavor SPF, unsigned BitWidth)
Return the minimum or maximum constant value for the specified integer min/max flavor and type.
Definition:ValueTracking.cpp:9163
llvm::getGuaranteedNonPoisonOps
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...
Definition:ValueTracking.cpp:8150
llvm::isOnlyUsedInZeroComparison
bool isOnlyUsedInZeroComparison(const Instruction *CxtI)
Definition:ValueTracking.cpp:267
llvm::LibFunc
LibFunc
Definition:TargetLibraryInfo.h:68
llvm::getLoadStorePointerOperand
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
Definition:Instructions.h:4984
llvm::getConstantStringInfo
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.
Definition:ValueTracking.cpp:6583
llvm::isDereferenceableAndAlignedPointer
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
llvm::onlyUsedByLifetimeMarkersOrDroppableInsts
bool onlyUsedByLifetimeMarkersOrDroppableInsts(const Value *V)
Return true if the only users of this pointer are lifetime markers or droppable instructions.
Definition:ValueTracking.cpp:7037
llvm::ReadByteArrayFromGlobal
Constant * ReadByteArrayFromGlobal(const GlobalVariable *GV, uint64_t Offset)
Definition:ConstantFolding.cpp:648
llvm::getUnderlyingObjectsForCodeGen
bool getUnderlyingObjectsForCodeGen(const Value *V, SmallVectorImpl< Value * > &Objects)
This is a wrapper around getUnderlyingObjects and adds support for basic ptrtoint+arithmetic+inttoptr...
Definition:ValueTracking.cpp:6937
llvm::canConvertToMinOrMaxIntrinsic
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...
Definition:ValueTracking.cpp:9174
llvm::make_range
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
Definition:iterator_range.h:77
llvm::getConstantDataArrayInfo
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.
Definition:ValueTracking.cpp:6488
llvm::bit_width
int bit_width(T Value)
Returns the number of bits needed to represent Value if Value is nonzero.
Definition:bit.h:317
llvm::isGuaranteedToExecuteForEveryIteration
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 ...
Definition:ValueTracking.cpp:7989
llvm::append_range
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition:STLExtras.h:2115
llvm::getUnderlyingObject
const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=6)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
Definition:ValueTracking.cpp:6775
llvm::isKnownToBeAPowerOfTwo
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.
Definition:ValueTracking.cpp:280
llvm::mustSuppressSpeculation
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
llvm::isPowerOf2_64
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
llvm::gep_type_end
gep_type_iterator gep_type_end(const User *GEP)
Definition:GetElementPtrTypeIterator.h:180
llvm::getMinMaxPred
CmpInst::Predicate getMinMaxPred(SelectPatternFlavor SPF, bool Ordered=false)
Return the canonical comparison predicate for the specified minimum/maximum flavor.
Definition:ValueTracking.cpp:9112
llvm::computeKnownBitsFromContext
void computeKnownBitsFromContext(const Value *V, KnownBits &Known, unsigned Depth, const SimplifyQuery &Q)
Merge bits known from context-dependent facts into Known.
Definition:ValueTracking.cpp:830
llvm::Log2_64
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
llvm::isGuaranteedNotToBeUndef
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.
Definition:ValueTracking.cpp:7863
llvm::getConstantRangeFromMetadata
ConstantRange getConstantRangeFromMetadata(const MDNode &RangeMD)
Parse out a conservative ConstantRange from !range metadata.
Definition:ConstantRange.cpp:2264
llvm::computeConstantRange
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.
Definition:ValueTracking.cpp:10085
llvm::getPointerOperand
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
Definition:Instructions.h:4998
llvm::MaskedValueIsZero
bool MaskedValueIsZero(const Value *V, const APInt &Mask, const SimplifyQuery &SQ, unsigned Depth=0)
Return true if 'V & Mask' is known to be zero.
Definition:ValueTracking.cpp:333
llvm::countr_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
llvm::isOverflowIntrinsicNoWrap
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...
Definition:ValueTracking.cpp:7394
llvm::getKnowledgeValidInContext
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...
Definition:AssumeBundleQueries.cpp:197
llvm::getKnowledgeFromBundle
RetainedKnowledge getKnowledgeFromBundle(AssumeInst &Assume, const CallBase::BundleOpInfo &BOI)
This extracts the Knowledge from an element of an operand bundle.
Definition:AssumeBundleQueries.cpp:99
llvm::matchSimpleRecurrence
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,...
Definition:ValueTracking.cpp:9214
llvm::EHPersonality::CoreCLR
@ CoreCLR
llvm::isSafeToSpeculativelyExecuteWithOpcode
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.
Definition:ValueTracking.cpp:7060
llvm::any_of
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
llvm::analyzeKnownBitsFromAndXorOr
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).
Definition:ValueTracking.cpp:1050
llvm::computeOverflowForUnsignedMul
OverflowResult computeOverflowForUnsignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ, bool IsNSW=false)
Definition:ValueTracking.cpp:7207
llvm::getShuffleDemandedElts
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...
Definition:VectorUtils.cpp:373
llvm::Log2_32
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
llvm::isGuard
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
llvm::countl_zero
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
llvm::getInverseMinMaxFlavor
SelectPatternFlavor getInverseMinMaxFlavor(SelectPatternFlavor SPF)
Return the inverse minimum/maximum flavor of the specified flavor.
Definition:ValueTracking.cpp:9139
llvm::MaxAnalysisRecursionDepth
constexpr unsigned MaxAnalysisRecursionDepth
Definition:ValueTracking.h:44
llvm::HexPrintStyle::Upper
@ Upper
llvm::HexPrintStyle::Lower
@ Lower
llvm::isKnownNegative
bool isKnownNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be negative (i.e.
Definition:ValueTracking.cpp:309
llvm::getGuaranteedWellDefinedOps
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...
Definition:ValueTracking.cpp:8124
llvm::computeOverflowForSignedSub
OverflowResult computeOverflowForSignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
Definition:ValueTracking.cpp:7365
llvm::fcmpImpliesClass
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,...
Definition:ValueTracking.cpp:4929
llvm::SelectPatternFlavor
SelectPatternFlavor
Specific patterns of select instructions we can match.
Definition:ValueTracking.h:1113
llvm::SPF_ABS
@ SPF_ABS
Floating point maxnum.
Definition:ValueTracking.h:1121
llvm::SPF_NABS
@ SPF_NABS
Absolute value.
Definition:ValueTracking.h:1122
llvm::SPF_FMAXNUM
@ SPF_FMAXNUM
Floating point minnum.
Definition:ValueTracking.h:1120
llvm::SPF_UMIN
@ SPF_UMIN
Signed minimum.
Definition:ValueTracking.h:1116
llvm::SPF_UMAX
@ SPF_UMAX
Signed maximum.
Definition:ValueTracking.h:1118
llvm::SPF_SMIN
@ SPF_SMIN
Definition:ValueTracking.h:1115
llvm::SPF_SMAX
@ SPF_SMAX
Unsigned minimum.
Definition:ValueTracking.h:1117
llvm::SPF_UNKNOWN
@ SPF_UNKNOWN
Definition:ValueTracking.h:1114
llvm::SPF_FMINNUM
@ SPF_FMINNUM
Unsigned maximum.
Definition:ValueTracking.h:1119
llvm::isIntrinsicReturningPointerAliasingArgumentWithoutCapturing
bool isIntrinsicReturningPointerAliasingArgumentWithoutCapturing(const CallBase *Call, bool MustPreserveNullness)
{launder,strip}.invariant.group returns pointer that aliases its argument, and it only captures point...
Definition:ValueTracking.cpp:6719
llvm::adjustKnownBitsForSelectArm
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.
Definition:ValueTracking.cpp:1082
llvm::impliesPoison
bool impliesPoison(const Value *ValAssumedPoison, const Value *V)
Return true if V is poison given that ValAssumedPoison is already poison.
Definition:ValueTracking.cpp:7697
llvm::getHorizDemandedEltsForFirstOperand
void getHorizDemandedEltsForFirstOperand(unsigned VectorBitWidth, const APInt &DemandedElts, APInt &DemandedLHS, APInt &DemandedRHS)
Compute the demanded elements mask of horizontal binary operations.
Definition:VectorUtils.cpp:671
llvm::getSelectPattern
SelectPatternResult getSelectPattern(CmpInst::Predicate Pred, SelectPatternNaNBehavior NaNBehavior=SPNB_NA, bool Ordered=false)
Determine the pattern for predicate X Pred Y ? X : Y.
Definition:ValueTracking.cpp:8640
llvm::FPClassTest
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
Definition:FloatingPointMode.h:239
llvm::fcInf
@ fcInf
Definition:FloatingPointMode.h:254
llvm::fcNegSubnormal
@ fcNegSubnormal
Definition:FloatingPointMode.h:246
llvm::fcPosNormal
@ fcPosNormal
Definition:FloatingPointMode.h:250
llvm::fcQNan
@ fcQNan
Definition:FloatingPointMode.h:243
llvm::fcNegZero
@ fcNegZero
Definition:FloatingPointMode.h:247
llvm::fcNegInf
@ fcNegInf
Definition:FloatingPointMode.h:244
llvm::fcFinite
@ fcFinite
Definition:FloatingPointMode.h:260
llvm::fcSubnormal
@ fcSubnormal
Definition:FloatingPointMode.h:256
llvm::fcNone
@ fcNone
Definition:FloatingPointMode.h:240
llvm::fcPositive
@ fcPositive
Definition:FloatingPointMode.h:261
llvm::fcNegFinite
@ fcNegFinite
Definition:FloatingPointMode.h:259
llvm::fcPosZero
@ fcPosZero
Definition:FloatingPointMode.h:248
llvm::fcSNan
@ fcSNan
Definition:FloatingPointMode.h:242
llvm::fcPosFinite
@ fcPosFinite
Definition:FloatingPointMode.h:258
llvm::fcNegNormal
@ fcNegNormal
Definition:FloatingPointMode.h:245
llvm::fcZero
@ fcZero
Definition:FloatingPointMode.h:257
llvm::fcNegative
@ fcNegative
Definition:FloatingPointMode.h:262
llvm::fcAllFlags
@ fcAllFlags
Definition:FloatingPointMode.h:264
llvm::fcPosSubnormal
@ fcPosSubnormal
Definition:FloatingPointMode.h:249
llvm::fcPosInf
@ fcPosInf
Definition:FloatingPointMode.h:251
llvm::fcNormal
@ fcNormal
Definition:FloatingPointMode.h:255
llvm::fcNan
@ fcNan
Definition:FloatingPointMode.h:253
llvm::programUndefinedIfPoison
bool programUndefinedIfPoison(const Instruction *Inst)
Definition:ValueTracking.cpp:8266
llvm::matchSelectPattern
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...
Definition:ValueTracking.cpp:9054
llvm::NullPointerIsDefined
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
llvm::programUndefinedIfUndefOrPoison
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...
Definition:ValueTracking.cpp:8262
llvm::isSafeToSpeculativelyExecute
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 ...
Definition:ValueTracking.cpp:7050
llvm::inverse_fabs
FPClassTest inverse_fabs(FPClassTest Mask)
Return the test mask which returns true after fabs is applied to the value.
Definition:FloatingPointMode.cpp:35
llvm::GetStringLength
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'.
Definition:ValueTracking.cpp:6694
llvm::computeOverflowForSignedMul
OverflowResult computeOverflowForSignedMul(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
Definition:ValueTracking.cpp:7223
llvm::getVScaleRange
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...
Definition:ValueTracking.cpp:1063
llvm::ConstantFoldCastOperand
Constant * ConstantFoldCastOperand(unsigned Opcode, Constant *C, Type *DestTy, const DataLayout &DL)
Attempt to constant fold a cast with the specified operand.
Definition:ConstantFolding.cpp:1462
llvm::canCreateUndefOrPoison
bool canCreateUndefOrPoison(const Operator *Op, bool ConsiderFlagsAndMetadata=true)
canCreateUndefOrPoison returns true if Op can create undef or poison from non-undef & non-poison oper...
Definition:ValueTracking.cpp:7637
llvm::classifyEHPersonality
EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
Definition:EHPersonalities.cpp:23
llvm::isKnownInversion
bool isKnownInversion(const Value *X, const Value *Y)
Return true iff:
Definition:ValueTracking.cpp:8609
llvm::isNotCrossLaneOperation
bool isNotCrossLaneOperation(const Instruction *I)
Return true if the instruction doesn't potentially cross vector lanes.
Definition:ValueTracking.cpp:7042
llvm::isKnownNonZero
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.
Definition:ValueTracking.cpp:3494
llvm::PoisonMaskElem
constexpr int PoisonMaskElem
Definition:Instructions.h:1889
llvm::onlyUsedByLifetimeMarkers
bool onlyUsedByLifetimeMarkers(const Value *V)
Return true if the only users of this pointer are lifetime markers.
Definition:ValueTracking.cpp:7033
llvm::getIntrinsicForCallSite
Intrinsic::ID getIntrinsicForCallSite(const CallBase &CB, const TargetLibraryInfo *TLI)
Map a call instruction to an intrinsic ID.
Definition:ValueTracking.cpp:4252
llvm::IRMemLocation::First
@ First
Helpers to iterate all locations in the MemoryEffectsBase class.
llvm::getUnderlyingObjectAggressive
const Value * getUnderlyingObjectAggressive(const Value *V)
Like getUnderlyingObject(), but will try harder to find a single underlying object.
Definition:ValueTracking.cpp:6864
llvm::getMinMaxIntrinsic
Intrinsic::ID getMinMaxIntrinsic(SelectPatternFlavor SPF)
Convert given SPF to equivalent min/max intrinsic.
Definition:ValueTracking.cpp:9124
llvm::computeOverflowForSignedAdd
OverflowResult computeOverflowForSignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
Definition:ValueTracking.cpp:7921
llvm::propagatesPoison
bool propagatesPoison(const Use &PoisonOp)
Return true if PoisonOp's user yields poison or raises UB if its operand PoisonOp is poison.
Definition:ValueTracking.cpp:8004
llvm::isKnownNonEqual
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.
Definition:ValueTracking.cpp:318
llvm::RecurKind::Add
@ Add
Sum of integers.
llvm::computeConstantRangeIncludingKnownBits
ConstantRange computeConstantRangeIncludingKnownBits(const WithCache< const Value * > &V, bool ForSigned, const SimplifyQuery &SQ)
Combine constant ranges from computeConstantRange() and computeKnownBits().
Definition:ValueTracking.cpp:7196
llvm::SelectPatternNaNBehavior
SelectPatternNaNBehavior
Behavior when a floating point min/max is given one NaN and one non-NaN as input.
Definition:ValueTracking.h:1127
llvm::SPNB_RETURNS_NAN
@ SPNB_RETURNS_NAN
NaN behavior not applicable.
Definition:ValueTracking.h:1129
llvm::SPNB_RETURNS_OTHER
@ SPNB_RETURNS_OTHER
Given one NaN input, returns the NaN.
Definition:ValueTracking.h:1130
llvm::SPNB_RETURNS_ANY
@ SPNB_RETURNS_ANY
Given one NaN input, returns the non-NaN.
Definition:ValueTracking.h:1131
llvm::SPNB_NA
@ SPNB_NA
Definition:ValueTracking.h:1128
llvm::computeKnownBits
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...
Definition:ValueTracking.cpp:164
llvm::Op
DWARFExpression::Operation Op
Definition:DWARFExpression.cpp:22
llvm::isGuaranteedNotToBeUndefOrPoison
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.
Definition:ValueTracking.cpp:7848
llvm::BitWidth
constexpr unsigned BitWidth
Definition:BitmaskEnum.h:217
llvm::matchDecomposedSelectPattern
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...
Definition:ValueTracking.cpp:9073
llvm::computeOverflowForUnsignedSub
OverflowResult computeOverflowForUnsignedSub(const Value *LHS, const Value *RHS, const SimplifyQuery &SQ)
Definition:ValueTracking.cpp:7332
llvm::isGuaranteedToTransferExecutionToSuccessor
bool isGuaranteedToTransferExecutionToSuccessor(const Instruction *I)
Return true if this function can prove that the instruction I will always transfer execution to one o...
Definition:ValueTracking.cpp:7927
llvm::gep_type_begin
gep_type_iterator gep_type_begin(const User *GEP)
Definition:GetElementPtrTypeIterator.h:173
llvm::fcmpToClassTest
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...
Definition:ValueTracking.cpp:4526
llvm::isBytewiseValue
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...
Definition:ValueTracking.cpp:6196
llvm::getFlippedStrictnessPredicateAndConstant
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...
Definition:ValueTracking.cpp:8672
llvm::getUnderlyingObjects
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...
Definition:ValueTracking.cpp:6822
llvm::is_contained
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition:STLExtras.h:1903
llvm::ComputeNumSignBits
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.
Definition:ValueTracking.cpp:351
llvm::computeOverflowForUnsignedAdd
OverflowResult computeOverflowForUnsignedAdd(const WithCache< const Value * > &LHS, const WithCache< const Value * > &RHS, const SimplifyQuery &SQ)
Definition:ValueTracking.cpp:7263
llvm::Log2
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition:Alignment.h:208
llvm::isImpliedByDomCondition
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...
Definition:ValueTracking.cpp:9687
llvm::isGEPBasedOnPointerToString
bool isGEPBasedOnPointerToString(const GEPOperator *GEP, unsigned CharSize=8)
Returns true if the GEP is based on a pointer to a string (array of.
Definition:ValueTracking.cpp:6462
llvm::isGuaranteedNotToBePoison
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.
Definition:ValueTracking.cpp:7856
llvm::computeKnownFPClass
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.
Definition:ValueTracking.cpp:6176
llvm::computeKnownBitsFromRangeMetadata
void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known)
Compute known bits from the range metadata.
Definition:ValueTracking.cpp:439
llvm::FindInsertedValue
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...
Definition:ValueTracking.cpp:6379
llvm::isKnownNegation
bool isKnownNegation(const Value *X, const Value *Y, bool NeedNSW=false, bool AllowPoison=true)
Return true if the two given values are negation.
Definition:ValueTracking.cpp:8578
llvm::isKnownPositive
bool isKnownPositive(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the given value is known be positive (i.e.
Definition:ValueTracking.cpp:297
llvm::ConstantFoldIntegerCast
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...
Definition:ConstantFolding.cpp:1549
llvm::isKnownNonNegative
bool isKnownNonNegative(const Value *V, const SimplifyQuery &SQ, unsigned Depth=0)
Returns true if the give value is known to be non-negative.
Definition:ValueTracking.cpp:292
llvm::ComputeMaxSignificantBits
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.
Definition:ValueTracking.cpp:359
llvm::mayHaveNonDefUseDependency
bool mayHaveNonDefUseDependency(const Instruction &I)
Returns true if the result or effects of the given instructions I depend values not reachable through...
Definition:ValueTracking.cpp:7163
llvm::isTriviallyVectorizable
bool isTriviallyVectorizable(Intrinsic::ID ID)
Identify if the intrinsic is trivially vectorizable.
Definition:VectorUtils.cpp:46
llvm::isIdentifiedObject
bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
Definition:AliasAnalysis.cpp:813
llvm::isImpliedCondition
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.
Definition:ValueTracking.cpp:9603
llvm::findValuesAffectedByCondition
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.
Definition:ValueTracking.cpp:10183
std::swap
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition:BitVector.h:860
llvm::APFloatBase::semanticsPrecision
static unsigned int semanticsPrecision(const fltSemantics &)
Definition:APFloat.cpp:315
llvm::APFloatBase::isRepresentableAsNormalIn
static bool isRepresentableAsNormalIn(const fltSemantics &Src, const fltSemantics &Dst)
Definition:APFloat.cpp:356
llvm::Align
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition:Alignment.h:39
llvm::AssumptionCache::ResultElem
Definition:AssumptionCache.h:48
llvm::CondContext::Invert
bool Invert
Definition:SimplifyQuery.h:64
llvm::CondContext::AffectedValues
SmallPtrSet< Value *, 4 > AffectedValues
Definition:SimplifyQuery.h:65
llvm::CondContext::Cond
Value * Cond
Definition:SimplifyQuery.h:63
llvm::ConstantDataArraySlice
Represents offset+length into a ConstantDataArray.
Definition:ValueTracking.h:662
llvm::ConstantDataArraySlice::Length
uint64_t Length
Length of the slice.
Definition:ValueTracking.h:671
llvm::ConstantDataArraySlice::Offset
uint64_t Offset
Slice starts at this Offset.
Definition:ValueTracking.h:668
llvm::ConstantDataArraySlice::Array
const ConstantDataArray * Array
ConstantDataArray pointer.
Definition:ValueTracking.h:665
llvm::DenormalMode
Represent subnormal handling kind for floating point instruction inputs and outputs.
Definition:FloatingPointMode.h:70
llvm::DenormalMode::Input
DenormalModeKind Input
Denormal treatment kind for floating point instruction inputs in the default floating-point environme...
Definition:FloatingPointMode.h:96
llvm::DenormalMode::outputsAreZero
constexpr bool outputsAreZero() const
Return true if output denormals should be flushed to 0.
Definition:FloatingPointMode.h:156
llvm::DenormalMode::PreserveSign
@ PreserveSign
The sign of a flushed-to-zero number is preserved in the sign of 0.
Definition:FloatingPointMode.h:80
llvm::DenormalMode::PositiveZero
@ PositiveZero
Denormals are flushed to positive zero.
Definition:FloatingPointMode.h:83
llvm::DenormalMode::Dynamic
@ Dynamic
Denormals have unknown treatment.
Definition:FloatingPointMode.h:86
llvm::DenormalMode::IEEE
@ IEEE
IEEE-754 denormal numbers preserved.
Definition:FloatingPointMode.h:77
llvm::DenormalMode::getPositiveZero
static constexpr DenormalMode getPositiveZero()
Definition:FloatingPointMode.h:123
llvm::DenormalMode::inputsAreZero
constexpr bool inputsAreZero() const
Return true if input denormals must be implicitly treated as 0.
Definition:FloatingPointMode.h:150
llvm::DenormalMode::Output
DenormalModeKind Output
Denormal flushing mode for floating point instruction results in the default floating point environme...
Definition:FloatingPointMode.h:91
llvm::DenormalMode::getIEEE
static constexpr DenormalMode getIEEE()
Definition:FloatingPointMode.h:114
llvm::InstrInfoQuery
InstrInfoQuery provides an interface to query additional information for instructions like metadata o...
Definition:SimplifyQuery.h:25
llvm::InstrInfoQuery::isExact
bool isExact(const BinaryOperator *Op) const
Definition:SimplifyQuery.h:48
llvm::InstrInfoQuery::getMetadata
MDNode * getMetadata(const Instruction *I, unsigned KindID) const
Definition:SimplifyQuery.h:30
llvm::InstrInfoQuery::hasNoSignedZeros
bool hasNoSignedZeros(const InstT *Op) const
Definition:SimplifyQuery.h:54
llvm::InstrInfoQuery::hasNoSignedWrap
bool hasNoSignedWrap(const InstT *Op) const
Definition:SimplifyQuery.h:42
llvm::InstrInfoQuery::UseInstrInfo
bool UseInstrInfo
Definition:SimplifyQuery.h:28
llvm::InstrInfoQuery::hasNoUnsignedWrap
bool hasNoUnsignedWrap(const InstT *Op) const
Definition:SimplifyQuery.h:36
llvm::KnownBits
Definition:KnownBits.h:23
llvm::KnownBits::makeConstant
static KnownBits makeConstant(const APInt &C)
Create known bits from a known constant.
Definition:KnownBits.h:293
llvm::KnownBits::sadd_sat
static KnownBits sadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.sadd.sat(LHS, RHS)
Definition:KnownBits.cpp:765
llvm::KnownBits::eq
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
llvm::KnownBits::anyextOrTrunc
KnownBits anyextOrTrunc(unsigned BitWidth) const
Return known bits for an "any" extension or truncation of the value we're tracking.
Definition:KnownBits.h:178
llvm::KnownBits::mulhu
static KnownBits mulhu(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from zero-extended multiply-hi.
Definition:KnownBits.cpp:909
llvm::KnownBits::countMinSignBits
unsigned countMinSignBits() const
Returns the number of times the sign bit is replicated into the other bits.
Definition:KnownBits.h:247
llvm::KnownBits::smax
static KnownBits smax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smax(LHS, RHS).
Definition:KnownBits.cpp:211
llvm::KnownBits::isNonNegative
bool isNonNegative() const
Returns true if this value is known to be non-negative.
Definition:KnownBits.h:100
llvm::KnownBits::blsi
KnownBits blsi() const
Compute known bits for X & -X, which has only the lowest bit set of X set.
Definition:KnownBits.cpp:1120
llvm::KnownBits::makeNonNegative
void makeNonNegative()
Make this value non-negative.
Definition:KnownBits.h:116
llvm::KnownBits::usub_sat
static KnownBits usub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.usub.sat(LHS, RHS)
Definition:KnownBits.cpp:774
llvm::KnownBits::countMinLeadingOnes
unsigned countMinLeadingOnes() const
Returns the minimum number of leading one bits.
Definition:KnownBits.h:243
llvm::KnownBits::countMinTrailingZeros
unsigned countMinTrailingZeros() const
Returns the minimum number of trailing zero bits.
Definition:KnownBits.h:234
llvm::KnownBits::ashr
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
llvm::KnownBits::ssub_sat
static KnownBits ssub_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.ssub.sat(LHS, RHS)
Definition:KnownBits.cpp:768
llvm::KnownBits::urem
static KnownBits urem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for urem(LHS, RHS).
Definition:KnownBits.cpp:1049
llvm::KnownBits::isUnknown
bool isUnknown() const
Returns true if we don't know any bits.
Definition:KnownBits.h:65
llvm::KnownBits::countMaxTrailingZeros
unsigned countMaxTrailingZeros() const
Returns the maximum number of trailing zero bits possible.
Definition:KnownBits.h:266
llvm::KnownBits::blsmsk
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
llvm::KnownBits::makeNegative
void makeNegative()
Make this value negative.
Definition:KnownBits.h:111
llvm::KnownBits::trunc
KnownBits trunc(unsigned BitWidth) const
Return known bits for a truncation of the value we're tracking.
Definition:KnownBits.h:153
llvm::KnownBits::hasConflict
bool hasConflict() const
Returns true if there is conflicting information.
Definition:KnownBits.h:50
llvm::KnownBits::countMaxPopulation
unsigned countMaxPopulation() const
Returns the maximum number of bits that could be one.
Definition:KnownBits.h:281
llvm::KnownBits::setAllZero
void setAllZero()
Make all bits known to be zero and discard any previous information.
Definition:KnownBits.h:85
llvm::KnownBits::getBitWidth
unsigned getBitWidth() const
Get the bit width of this value.
Definition:KnownBits.h:43
llvm::KnownBits::umax
static KnownBits umax(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umax(LHS, RHS).
Definition:KnownBits.cpp:187
llvm::KnownBits::isConstant
bool isConstant() const
Returns true if we know the value of all bits.
Definition:KnownBits.h:53
llvm::KnownBits::resetAll
void resetAll()
Resets the known state of all bits.
Definition:KnownBits.h:73
llvm::KnownBits::unionWith
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
llvm::KnownBits::lshr
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
llvm::KnownBits::isNonZero
bool isNonZero() const
Returns true if this value is known to be non-zero.
Definition:KnownBits.h:103
llvm::KnownBits::intersectWith
KnownBits intersectWith(const KnownBits &RHS) const
Returns KnownBits information that is known to be true for both this and RHS.
Definition:KnownBits.h:303
llvm::KnownBits::sext
KnownBits sext(unsigned BitWidth) const
Return known bits for a sign extension of the value we're tracking.
Definition:KnownBits.h:172
llvm::KnownBits::countMinTrailingOnes
unsigned countMinTrailingOnes() const
Returns the minimum number of trailing one bits.
Definition:KnownBits.h:237
llvm::KnownBits::add
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
llvm::KnownBits::zextOrTrunc
KnownBits zextOrTrunc(unsigned BitWidth) const
Return known bits for a zero extension or truncation of the value we're tracking.
Definition:KnownBits.h:188
llvm::KnownBits::countMinLeadingZeros
unsigned countMinLeadingZeros() const
Returns the minimum number of leading zero bits.
Definition:KnownBits.h:240
llvm::KnownBits::getMaxValue
APInt getMaxValue() const
Return the maximal unsigned value possible given these KnownBits.
Definition:KnownBits.h:137
llvm::KnownBits::smin
static KnownBits smin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for smin(LHS, RHS).
Definition:KnownBits.cpp:215
llvm::KnownBits::mulhs
static KnownBits mulhs(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits from sign-extended multiply-hi.
Definition:KnownBits.cpp:901
llvm::KnownBits::srem
static KnownBits srem(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for srem(LHS, RHS).
Definition:KnownBits.cpp:1066
llvm::KnownBits::udiv
static KnownBits udiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for udiv(LHS, RHS).
Definition:KnownBits.cpp:1009
llvm::KnownBits::computeForAddSub
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
llvm::KnownBits::sdiv
static KnownBits sdiv(const KnownBits &LHS, const KnownBits &RHS, bool Exact=false)
Compute known bits for sdiv(LHS, RHS).
Definition:KnownBits.cpp:953
llvm::KnownBits::haveNoCommonBitsSet
static bool haveNoCommonBitsSet(const KnownBits &LHS, const KnownBits &RHS)
Return true if LHS and RHS have no common bits set.
Definition:KnownBits.h:318
llvm::KnownBits::isNegative
bool isNegative() const
Returns true if this value is known to be negative.
Definition:KnownBits.h:97
llvm::KnownBits::sub
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
llvm::KnownBits::countMaxLeadingZeros
unsigned countMaxLeadingZeros() const
Returns the maximum number of leading zero bits possible.
Definition:KnownBits.h:272
llvm::KnownBits::setAllOnes
void setAllOnes()
Make all bits known to be one and discard any previous information.
Definition:KnownBits.h:91
llvm::KnownBits::insertBits
void insertBits(const KnownBits &SubBits, unsigned BitPosition)
Insert the bits from a smaller known bits starting at bitPosition.
Definition:KnownBits.h:211
llvm::KnownBits::One
APInt One
Definition:KnownBits.h:25
llvm::KnownBits::uadd_sat
static KnownBits uadd_sat(const KnownBits &LHS, const KnownBits &RHS)
Compute knownbits resulting from llvm.uadd.sat(LHS, RHS)
Definition:KnownBits.cpp:771
llvm::KnownBits::mul
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
llvm::KnownBits::anyext
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
llvm::KnownBits::Zero
APInt Zero
Definition:KnownBits.h:24
llvm::KnownBits::abs
KnownBits abs(bool IntMinIsPoison=false) const
Compute known bits for the absolute value.
Definition:KnownBits.cpp:550
llvm::KnownBits::sgt
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
llvm::KnownBits::uge
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
llvm::KnownBits::shl
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
llvm::KnownBits::umin
static KnownBits umin(const KnownBits &LHS, const KnownBits &RHS)
Compute known bits for umin(LHS, RHS).
Definition:KnownBits.cpp:205
llvm::KnownBits::sextOrTrunc
KnownBits sextOrTrunc(unsigned BitWidth) const
Return known bits for a sign extension or truncation of the value we're tracking.
Definition:KnownBits.h:198
llvm::KnownBits::getConstant
const APInt & getConstant() const
Returns the value when all bits have a known value.
Definition:KnownBits.h:59
llvm::KnownFPClass
Definition:ValueTracking.h:261
llvm::KnownFPClass::KnownFPClasses
FPClassTest KnownFPClasses
Floating-point classes the value could be one of.
Definition:ValueTracking.h:263
llvm::KnownFPClass::isKnownNeverInfinity
bool isKnownNeverInfinity() const
Return true if it's known this can never be an infinity.
Definition:ValueTracking.h:293
llvm::KnownFPClass::cannotBeOrderedGreaterThanZero
bool cannotBeOrderedGreaterThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never greater tha...
Definition:ValueTracking.h:374
llvm::KnownFPClass::resetAll
void resetAll()
Definition:ValueTracking.h:486
llvm::KnownFPClass::OrderedGreaterThanZeroMask
static constexpr FPClassTest OrderedGreaterThanZeroMask
Definition:ValueTracking.h:352
llvm::KnownFPClass::OrderedLessThanZeroMask
static constexpr FPClassTest OrderedLessThanZeroMask
Definition:ValueTracking.h:350
llvm::KnownFPClass::knownNot
void knownNot(FPClassTest RuleOut)
Definition:ValueTracking.h:386
llvm::KnownFPClass::isKnownNeverZero
bool isKnownNeverZero() const
Return true if it's known this can never be a zero.
Definition:ValueTracking.h:324
llvm::KnownFPClass::copysign
void copysign(const KnownFPClass &Sign)
Definition:ValueTracking.h:435
llvm::KnownFPClass::isKnownNeverSubnormal
bool isKnownNeverSubnormal() const
Return true if it's known this can never be a subnormal.
Definition:ValueTracking.h:308
llvm::KnownFPClass::isKnownNeverLogicalNegZero
bool isKnownNeverLogicalNegZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a negative zero.
Definition:ValueTracking.cpp:4419
llvm::KnownFPClass::isKnownNeverLogicalPosZero
bool isKnownNeverLogicalPosZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a positive zero.
Definition:ValueTracking.cpp:4425
llvm::KnownFPClass::fneg
void fneg()
Definition:ValueTracking.h:396
llvm::KnownFPClass::propagateCanonicalizingSrc
void propagateCanonicalizingSrc(const KnownFPClass &Src, const Function &F, Type *Ty)
Report known classes if Src is evaluated through a potentially canonicalizing operation.
Definition:ValueTracking.cpp:4479
llvm::KnownFPClass::propagateDenormal
void propagateDenormal(const KnownFPClass &Src, const Function &F, Type *Ty)
Propagate knowledge from a source value that could be a denormal or zero.
Definition:ValueTracking.cpp:4450
llvm::KnownFPClass::isUnknown
bool isUnknown() const
Definition:ValueTracking.h:280
llvm::KnownFPClass::isKnownNeverNegInfinity
bool isKnownNeverNegInfinity() const
Return true if it's known this can never be -infinity.
Definition:ValueTracking.h:303
llvm::KnownFPClass::isKnownNeverNegSubnormal
bool isKnownNeverNegSubnormal() const
Return true if it's known this can never be a negative subnormal.
Definition:ValueTracking.h:318
llvm::KnownFPClass::isKnownNeverPosZero
bool isKnownNeverPosZero() const
Return true if it's known this can never be a literal positive zero.
Definition:ValueTracking.h:329
llvm::KnownFPClass::SignBit
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 ...
Definition:ValueTracking.h:267
llvm::KnownFPClass::isKnownNeverNaN
bool isKnownNeverNaN() const
Return true if it's known this can never be a nan.
Definition:ValueTracking.h:285
llvm::KnownFPClass::isKnownNever
bool isKnownNever(FPClassTest Mask) const
Return true if it's known this can never be one of the mask entries.
Definition:ValueTracking.h:274
llvm::KnownFPClass::isKnownNeverNegZero
bool isKnownNeverNegZero() const
Return true if it's known this can never be a negative zero.
Definition:ValueTracking.h:335
llvm::KnownFPClass::isKnownNeverLogicalZero
bool isKnownNeverLogicalZero(const Function &F, Type *Ty) const
Return true if it's know this can never be interpreted as a zero.
Definition:ValueTracking.cpp:4414
llvm::KnownFPClass::propagateNaN
void propagateNaN(const KnownFPClass &Src, bool PreserveSign=false)
Definition:ValueTracking.h:460
llvm::KnownFPClass::cannotBeOrderedLessThanZero
bool cannotBeOrderedLessThanZero() const
Return true if we can prove that the analyzed floating-point value is either NaN or never less than -...
Definition:ValueTracking.h:363
llvm::KnownFPClass::signBitMustBeOne
void signBitMustBeOne()
Assume the sign bit is one.
Definition:ValueTracking.h:430
llvm::KnownFPClass::signBitMustBeZero
void signBitMustBeZero()
Assume the sign bit is zero.
Definition:ValueTracking.h:424
llvm::KnownFPClass::fabs
void fabs()
Definition:ValueTracking.h:402
llvm::KnownFPClass::isKnownNeverPosInfinity
bool isKnownNeverPosInfinity() const
Return true if it's known this can never be +infinity.
Definition:ValueTracking.h:298
llvm::KnownFPClass::isKnownNeverPosSubnormal
bool isKnownNeverPosSubnormal() const
Return true if it's known this can never be a positive subnormal.
Definition:ValueTracking.h:313
llvm::RetainedKnowledge
Represent one information held inside an operand bundle of an llvm.assume.
Definition:AssumeBundleQueries.h:99
llvm::SelectPatternResult
Definition:ValueTracking.h:1136
llvm::SelectPatternResult::Flavor
SelectPatternFlavor Flavor
Definition:ValueTracking.h:1137
llvm::SelectPatternResult::isMinOrMax
static bool isMinOrMax(SelectPatternFlavor SPF)
When implementing this min/max pattern as fcmp; select, does the fcmp have to be ordered?
Definition:ValueTracking.h:1145
llvm::SimplifyQuery
Definition:SimplifyQuery.h:70
llvm::SimplifyQuery::DL
const DataLayout & DL
Definition:SimplifyQuery.h:71
llvm::SimplifyQuery::getWithoutCondContext
SimplifyQuery getWithoutCondContext() const
Definition:SimplifyQuery.h:134
llvm::SimplifyQuery::CxtI
const Instruction * CxtI
Definition:SimplifyQuery.h:75
llvm::SimplifyQuery::DT
const DominatorTree * DT
Definition:SimplifyQuery.h:73
llvm::SimplifyQuery::getWithInstruction
SimplifyQuery getWithInstruction(const Instruction *I) const
Definition:SimplifyQuery.h:107
llvm::SimplifyQuery::AC
AssumptionCache * AC
Definition:SimplifyQuery.h:74
llvm::SimplifyQuery::DC
const DomConditionCache * DC
Definition:SimplifyQuery.h:76
llvm::SimplifyQuery::IIQ
const InstrInfoQuery IIQ
Definition:SimplifyQuery.h:82
llvm::SimplifyQuery::CC
const CondContext * CC
Definition:SimplifyQuery.h:77
llvm::fltSemantics
Definition:APFloat.cpp:103

Generated on Sun Jul 20 2025 08:21:24 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp