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

©2009-2025 Movatter.jp