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
11
12namespace llvm {
16 const DataLayout &DL) const {
17 // Not yet resolved.
18 if (isUnknown() || Other.isUnknown())
19 return nullptr;
20
21 // TODO: Can be made more precise, but always returning undef would be
22 // incorrect.
23 if (isUndef() || Other.isUndef())
24 return nullptr;
25
26 if (isConstant() && Other.isConstant())
28 Other.getConstant(), DL);
29
30 if (ICmpInst::isEquality(Pred)) {
31 // not(C) != C => true, not(C) == C => false.
32 if ((isNotConstant() && Other.isConstant() &&
33 getNotConstant() == Other.getConstant()) ||
34 (isConstant() && Other.isNotConstant() &&
35 getConstant() == Other.getNotConstant()))
36 return Pred == ICmpInst::ICMP_NE ? ConstantInt::getTrue(Ty)
38 }
39
40 // Integer constants are represented as ConstantRanges with single
41 // elements.
42 if (!isConstantRange() || !Other.isConstantRange())
43 return nullptr;
44
45 const auto &CR = getConstantRange();
46 const auto &OtherCR = Other.getConstantRange();
47 if (CR.icmp(Pred, OtherCR))
48 return ConstantInt::getTrue(Ty);
49 if (CR.icmp(CmpInst::getInversePredicate(Pred), OtherCR))
50 return ConstantInt::getFalse(Ty);
51
52 return nullptr;
53}
54
56 if (Val.isUnknown())
57 return OS << "unknown";
58 if (Val.isUndef())
59 return OS << "undef";
60 if (Val.isOverdefined())
61 return OS << "overdefined";
62
63 if (Val.isNotConstant())
64 return OS << "notconstant<" << *Val.getNotConstant() << ">";
65
67 return OS << "constantrange incl. undef <"
68 << Val.getConstantRange(true).getLower() << ", "
69 << Val.getConstantRange(true).getUpper() << ">";
70
71 if (Val.isConstantRange())
72 return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
73 << Val.getConstantRange().getUpper() << ">";
74 return OS << "constant<" << *Val.getConstant() << ">";
75}
76} // 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:103
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:29
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:242
const ConstantRange & getConstantRange(bool UndefAllowed=true) const
Returns the constant range for this value.
Definition: ValueLattice.h:269
bool isConstantRange(bool UndefAllowed=true) const
Returns true if this value is a constant range.
Definition: ValueLattice.h:249
Constant * getNotConstant() const
Definition: ValueLattice.h:260
Constant * getConstant() const
Definition: ValueLattice.h:255
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:293