10b57cec5SDimitry Andric //===- ValueLattice.cpp - Value constraint analysis -------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/Analysis/ValueLattice.h"
10*bdd1243dSDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
110b57cec5SDimitry Andric
120b57cec5SDimitry Andric namespace llvm {
13*bdd1243dSDimitry Andric Constant *
getCompare(CmpInst::Predicate Pred,Type * Ty,const ValueLatticeElement & Other,const DataLayout & DL) const14*bdd1243dSDimitry Andric ValueLatticeElement::getCompare(CmpInst::Predicate Pred, Type *Ty,
15*bdd1243dSDimitry Andric const ValueLatticeElement &Other,
16*bdd1243dSDimitry Andric const DataLayout &DL) const {
17*bdd1243dSDimitry Andric // Not yet resolved.
18*bdd1243dSDimitry Andric if (isUnknown() || Other.isUnknown())
19*bdd1243dSDimitry Andric return nullptr;
20*bdd1243dSDimitry Andric
21*bdd1243dSDimitry Andric // TODO: Can be made more precise, but always returning undef would be
22*bdd1243dSDimitry Andric // incorrect.
23*bdd1243dSDimitry Andric if (isUndef() || Other.isUndef())
24*bdd1243dSDimitry Andric return nullptr;
25*bdd1243dSDimitry Andric
26*bdd1243dSDimitry Andric if (isConstant() && Other.isConstant())
27*bdd1243dSDimitry Andric return ConstantFoldCompareInstOperands(Pred, getConstant(),
28*bdd1243dSDimitry Andric Other.getConstant(), DL);
29*bdd1243dSDimitry Andric
30*bdd1243dSDimitry Andric if (ICmpInst::isEquality(Pred)) {
31*bdd1243dSDimitry Andric // not(C) != C => true, not(C) == C => false.
32*bdd1243dSDimitry Andric if ((isNotConstant() && Other.isConstant() &&
33*bdd1243dSDimitry Andric getNotConstant() == Other.getConstant()) ||
34*bdd1243dSDimitry Andric (isConstant() && Other.isNotConstant() &&
35*bdd1243dSDimitry Andric getConstant() == Other.getNotConstant()))
36*bdd1243dSDimitry Andric return Pred == ICmpInst::ICMP_NE ? ConstantInt::getTrue(Ty)
37*bdd1243dSDimitry Andric : ConstantInt::getFalse(Ty);
38*bdd1243dSDimitry Andric }
39*bdd1243dSDimitry Andric
40*bdd1243dSDimitry Andric // Integer constants are represented as ConstantRanges with single
41*bdd1243dSDimitry Andric // elements.
42*bdd1243dSDimitry Andric if (!isConstantRange() || !Other.isConstantRange())
43*bdd1243dSDimitry Andric return nullptr;
44*bdd1243dSDimitry Andric
45*bdd1243dSDimitry Andric const auto &CR = getConstantRange();
46*bdd1243dSDimitry Andric const auto &OtherCR = Other.getConstantRange();
47*bdd1243dSDimitry Andric if (CR.icmp(Pred, OtherCR))
48*bdd1243dSDimitry Andric return ConstantInt::getTrue(Ty);
49*bdd1243dSDimitry Andric if (CR.icmp(CmpInst::getInversePredicate(Pred), OtherCR))
50*bdd1243dSDimitry Andric return ConstantInt::getFalse(Ty);
51*bdd1243dSDimitry Andric
52*bdd1243dSDimitry Andric return nullptr;
53*bdd1243dSDimitry Andric }
54*bdd1243dSDimitry Andric
operator <<(raw_ostream & OS,const ValueLatticeElement & Val)550b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, const ValueLatticeElement &Val) {
56d65cd7a5SDimitry Andric if (Val.isUnknown())
57d65cd7a5SDimitry Andric return OS << "unknown";
58d65cd7a5SDimitry Andric if (Val.isUndef())
59d65cd7a5SDimitry Andric return OS << "undef";
600b57cec5SDimitry Andric if (Val.isOverdefined())
610b57cec5SDimitry Andric return OS << "overdefined";
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric if (Val.isNotConstant())
640b57cec5SDimitry Andric return OS << "notconstant<" << *Val.getNotConstant() << ">";
655ffd83dbSDimitry Andric
665ffd83dbSDimitry Andric if (Val.isConstantRangeIncludingUndef())
675ffd83dbSDimitry Andric return OS << "constantrange incl. undef <"
685ffd83dbSDimitry Andric << Val.getConstantRange(true).getLower() << ", "
695ffd83dbSDimitry Andric << Val.getConstantRange(true).getUpper() << ">";
705ffd83dbSDimitry Andric
710b57cec5SDimitry Andric if (Val.isConstantRange())
720b57cec5SDimitry Andric return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
730b57cec5SDimitry Andric << Val.getConstantRange().getUpper() << ">";
740b57cec5SDimitry Andric return OS << "constant<" << *Val.getConstant() << ">";
750b57cec5SDimitry Andric }
760b57cec5SDimitry Andric } // end namespace llvm
77