1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
//===- ValueLattice.cpp - Value constraint analysis -------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/ValueLattice.h"
#include "llvm/Analysis/ConstantFolding.h"
#include "llvm/IR/Instructions.h"
namespace llvm {
Constant *
ValueLatticeElement::getCompare(CmpInst::Predicate Pred, Type *Ty,
const ValueLatticeElement &Other,
const DataLayout &DL) const {
// Not yet resolved.
if (isUnknown() || Other.isUnknown())
return nullptr;
// TODO: Can be made more precise, but always returning undef would be
// incorrect.
if (isUndef() || Other.isUndef())
return nullptr;
if (isConstant() && Other.isConstant())
return ConstantFoldCompareInstOperands(Pred, getConstant(),
Other.getConstant(), DL);
if (ICmpInst::isEquality(Pred)) {
// not(C) != C => true, not(C) == C => false.
if ((isNotConstant() && Other.isConstant() &&
getNotConstant() == Other.getConstant()) ||
(isConstant() && Other.isNotConstant() &&
getConstant() == Other.getNotConstant()))
return Pred == ICmpInst::ICMP_NE ? ConstantInt::getTrue(Ty)
: ConstantInt::getFalse(Ty);
}
// Integer constants are represented as ConstantRanges with single
// elements.
if (!isConstantRange() || !Other.isConstantRange())
return nullptr;
const auto &CR = getConstantRange();
const auto &OtherCR = Other.getConstantRange();
if (CR.icmp(Pred, OtherCR))
return ConstantInt::getTrue(Ty);
if (CR.icmp(CmpInst::getInversePredicate(Pred), OtherCR))
return ConstantInt::getFalse(Ty);
return nullptr;
}
static bool hasSingleValue(const ValueLatticeElement &Val) {
if (Val.isConstantRange() && Val.getConstantRange().isSingleElement())
// Integer constants are single element ranges
return true;
return Val.isConstant();
}
/// Combine two sets of facts about the same value into a single set of
/// facts. Note that this method is not suitable for merging facts along
/// different paths in a CFG; that's what the mergeIn function is for. This
/// is for merging facts gathered about the same value at the same location
/// through two independent means.
/// Notes:
/// * This method does not promise to return the most precise possible lattice
/// value implied by A and B. It is allowed to return any lattice element
/// which is at least as strong as *either* A or B (unless our facts
/// conflict, see below).
/// * Due to unreachable code, the intersection of two lattice values could be
/// contradictory. If this happens, we return some valid lattice value so as
/// not confuse the rest of LVI. Ideally, we'd always return Undefined, but
/// we do not make this guarantee. TODO: This would be a useful enhancement.
ValueLatticeElement
ValueLatticeElement::intersect(const ValueLatticeElement &Other) const {
if (isUnknown())
return *this;
if (Other.isUnknown())
return Other;
// If we gave up for one, but got a useable fact from the other, use it.
if (isOverdefined())
return Other;
if (Other.isOverdefined())
return *this;
// Can't get any more precise than constants.
if (hasSingleValue(*this))
return *this;
if (hasSingleValue(Other))
return Other;
// Could be either constant range or not constant here.
if (!isConstantRange() || !Other.isConstantRange()) {
// TODO: Arbitrary choice, could be improved
return *this;
}
// Intersect two constant ranges
ConstantRange Range =
getConstantRange().intersectWith(Other.getConstantRange());
// Note: An empty range is implicitly converted to unknown or undef depending
// on MayIncludeUndef internally.
return ValueLatticeElement::getRange(
std::move(Range), /*MayIncludeUndef=*/isConstantRangeIncludingUndef() ||
Other.isConstantRangeIncludingUndef());
}
raw_ostream &operator<<(raw_ostream &OS, const ValueLatticeElement &Val) {
if (Val.isUnknown())
return OS << "unknown";
if (Val.isUndef())
return OS << "undef";
if (Val.isOverdefined())
return OS << "overdefined";
if (Val.isNotConstant())
return OS << "notconstant<" << *Val.getNotConstant() << ">";
if (Val.isConstantRangeIncludingUndef())
return OS << "constantrange incl. undef <"
<< Val.getConstantRange(true).getLower() << ", "
<< Val.getConstantRange(true).getUpper() << ">";
if (Val.isConstantRange())
return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
<< Val.getConstantRange().getUpper() << ">";
return OS << "constant<" << *Val.getConstant() << ">";
}
} // end namespace llvm
|