LLVM 20.0.0git
ValueLattice.cpp
Go to the documentation of this file.
1//===- ValueLattice.cpp - Value constraint analysis -------------*- C++ -*-===//
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
12
13namespace llvm {
17 const DataLayout &DL) const {
18 // Not yet resolved.
19 if (isUnknown() || Other.isUnknown())
20 return nullptr;
21
22 // TODO: Can be made more precise, but always returning undef would be
23 // incorrect.
24 if (isUndef() || Other.isUndef())
25 return nullptr;
26
27 if (isConstant() && Other.isConstant())
29 Other.getConstant(), DL);
30
31 if (ICmpInst::isEquality(Pred)) {
32 // not(C) != C => true, not(C) == C => false.
33 if ((isNotConstant() && Other.isConstant() &&
34 getNotConstant() == Other.getConstant()) ||
35 (isConstant() && Other.isNotConstant() &&
36 getConstant() == Other.getNotConstant()))
37 return Pred == ICmpInst::ICMP_NE ? ConstantInt::getTrue(Ty)
39 }
40
41 // Integer constants are represented as ConstantRanges with single
42 // elements.
43 if (!isConstantRange() || !Other.isConstantRange())
44 return nullptr;
45
46 const auto &CR = getConstantRange();
47 const auto &OtherCR = Other.getConstantRange();
48 if (CR.icmp(Pred, OtherCR))
49 return ConstantInt::getTrue(Ty);
50 if (CR.icmp(CmpInst::getInversePredicate(Pred), OtherCR))
51 return ConstantInt::getFalse(Ty);
52
53 return nullptr;
54}
55
57 if (Val.isUnknown())
58 return OS << "unknown";
59 if (Val.isUndef())
60 return OS << "undef";
61 if (Val.isOverdefined())
62 return OS << "overdefined";
63
64 if (Val.isNotConstant())
65 return OS << "notconstant<" << *Val.getNotConstant() << ">";
66
68 return OS << "constantrange incl. undef <"
69 << Val.getConstantRange(true).getLower() << ", "
70 << Val.getConstantRange(true).getUpper() << ">";
71
72 if (Val.isConstantRange())
73 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
74 << Val.getConstantRange().getUpper() << ">";
75 return OS << "constant<" << *Val.getConstant() << ">";
76}
77} // end namespace llvm
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
raw_pwrite_stream & OS
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:757
@ ICMP_NE
not equal
Definition: InstrTypes.h:779
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:871
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:850
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:857
const APInt & getLower() const
Return the lower value for this range.
const APInt & getUpper() const
Return the upper value for this range.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
bool isEquality() const
Return true if this predicate is either EQ or NE.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
This class represents lattice values for constants.
Definition: ValueLattice.h:26
Constant * getCompare(CmpInst::Predicate Pred, Type *Ty, const ValueLatticeElement &Other, const DataLayout &DL) const
true, false or undef constants, or nullptr if the comparison cannot be evaluated.
bool isConstantRangeIncludingUndef() const
Definition: ValueLattice.h:239
const ConstantRange & getConstantRange(bool UndefAllowed=true) const
Returns the constant range for this value.
Definition: ValueLattice.h:266
bool isConstantRange(bool UndefAllowed=true) const
Returns true if this value is a constant range.
Definition: ValueLattice.h:246
Constant * getNotConstant() const
Definition: ValueLattice.h:257
Constant * getConstant() const
Definition: ValueLattice.h:252
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Constant * ConstantFoldCompareInstOperands(unsigned Predicate, Constant *LHS, Constant *RHS, const DataLayout &DL, const TargetLibraryInfo *TLI=nullptr, const Instruction *I=nullptr)
Attempt to constant fold a compare instruction (icmp/fcmp) with the specified operands.
@ Other
Any other memory.
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:292