LLVM  17.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 
12 namespace llvm {
13 Constant *
15  const ValueLatticeElement &Other,
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
llvm::ValueLatticeElement::getConstantRange
const ConstantRange & getConstantRange(bool UndefAllowed=true) const
Returns the constant range for this value.
Definition: ValueLattice.h:272
llvm::ValueLatticeElement::isConstant
bool isConstant() const
Definition: ValueLattice.h:243
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::DataLayout
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:114
llvm::CmpInst::Predicate
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:718
llvm::ValueLatticeElement::getConstant
Constant * getConstant() const
Definition: ValueLattice.h:258
llvm::ValueLatticeElement::getNotConstant
Constant * getNotConstant() const
Definition: ValueLattice.h:263
llvm::CmpInst::ICMP_NE
@ ICMP_NE
not equal
Definition: InstrTypes.h:740
llvm::CmpInst::getInversePredicate
Predicate getInversePredicate() const
For example, EQ -> NE, UGT -> ULE, SLT -> SGE, OEQ -> UNE, UGT -> OLE, OLT -> UGE,...
Definition: InstrTypes.h:832
llvm::Type
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
llvm::ICmpInst::isEquality
bool isEquality() const
Return true if this predicate is either EQ or NE.
Definition: Instructions.h:1292
llvm::ValueLatticeElement::getCompare
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.
Definition: ValueLattice.cpp:14
ConstantFolding.h
llvm::ISD::Constant
@ Constant
Definition: ISDOpcodes.h:76
llvm::ValueLatticeElement::isConstantRangeIncludingUndef
bool isConstantRangeIncludingUndef() const
Definition: ValueLattice.h:245
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
llvm::operator<<
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
Definition: APFixedPoint.h:292
llvm::ConstantFoldCompareInstOperands
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.
Definition: ConstantFolding.cpp:1219
DL
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Definition: AArch64SLSHardening.cpp:76
llvm::ValueLatticeElement
This class represents lattice values for constants.
Definition: ValueLattice.h:29
llvm::ValueLatticeElement::isOverdefined
bool isOverdefined() const
Definition: ValueLattice.h:256
llvm::ConstantInt::getFalse
static ConstantInt * getFalse(LLVMContext &Context)
Definition: Constants.cpp:842
llvm::ConstantRange::getLower
const APInt & getLower() const
Return the lower value for this range.
Definition: ConstantRange.h:198
llvm::ValueLatticeElement::isUndef
bool isUndef() const
Definition: ValueLattice.h:240
llvm::ConstantInt::getTrue
static ConstantInt * getTrue(LLVMContext &Context)
Definition: Constants.cpp:835
llvm::ValueLatticeElement::isNotConstant
bool isNotConstant() const
Definition: ValueLattice.h:244
Other
std::optional< std::vector< StOtherPiece > > Other
Definition: ELFYAML.cpp:1260
llvm::ValueLatticeElement::isConstantRange
bool isConstantRange(bool UndefAllowed=true) const
Returns true if this value is a constant range.
Definition: ValueLattice.h:252
llvm::ValueLatticeElement::isUnknown
bool isUnknown() const
Definition: ValueLattice.h:241
llvm::ConstantRange::getUpper
const APInt & getUpper() const
Return the upper value for this range.
Definition: ConstantRange.h:201
ValueLattice.h