2017-09-28 11:09:22 +00:00
|
|
|
//===- ValueLattice.cpp - Value constraint analysis -------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 08:50:56 +00:00
|
|
|
// 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
|
2017-09-28 11:09:22 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/ValueLattice.h"
|
2022-11-02 10:41:11 +01:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2017-09-28 11:09:22 +00:00
|
|
|
|
|
|
|
namespace llvm {
|
2022-11-02 10:33:44 +01:00
|
|
|
Constant *
|
|
|
|
ValueLatticeElement::getCompare(CmpInst::Predicate Pred, Type *Ty,
|
2022-11-02 10:41:11 +01:00
|
|
|
const ValueLatticeElement &Other,
|
|
|
|
const DataLayout &DL) const {
|
2022-11-02 10:33:44 +01:00
|
|
|
// Not yet resolved.
|
|
|
|
if (isUnknown() || Other.isUnknown())
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
// TODO: Can be made more precise, but always returning undef would be
|
|
|
|
// incorrect.
|
2022-11-02 17:50:02 +01:00
|
|
|
if (isUndef() || Other.isUndef())
|
2022-11-02 10:33:44 +01:00
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
if (isConstant() && Other.isConstant())
|
2022-11-02 10:41:11 +01:00
|
|
|
return ConstantFoldCompareInstOperands(Pred, getConstant(),
|
|
|
|
Other.getConstant(), DL);
|
2022-11-02 10:33:44 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-09-28 11:09:22 +00:00
|
|
|
raw_ostream &operator<<(raw_ostream &OS, const ValueLatticeElement &Val) {
|
2020-03-14 16:50:09 +00:00
|
|
|
if (Val.isUnknown())
|
|
|
|
return OS << "unknown";
|
|
|
|
if (Val.isUndef())
|
|
|
|
return OS << "undef";
|
2017-09-28 11:09:22 +00:00
|
|
|
if (Val.isOverdefined())
|
|
|
|
return OS << "overdefined";
|
|
|
|
|
|
|
|
if (Val.isNotConstant())
|
|
|
|
return OS << "notconstant<" << *Val.getNotConstant() << ">";
|
2020-03-15 10:35:39 +00:00
|
|
|
|
[ValueLattice] Distinguish between constant ranges with/without undef.
This patch updates ValueLattice to distinguish between ranges that are
guaranteed to not include undef and ranges that may include undef.
A constant range guaranteed to not contain undef can be used to simplify
instructions to arbitrary values. A constant range that may contain
undef can only be used to simplify to a constant. If the value can be
undef, it might take a value outside the range. For example, consider
the snipped below
define i32 @f(i32 %a, i1 %c) {
br i1 %c, label %true, label %false
true:
%a.255 = and i32 %a, 255
br label %exit
false:
br label %exit
exit:
%p = phi i32 [ %a.255, %true ], [ undef, %false ]
%f.1 = icmp eq i32 %p, 300
call void @use(i1 %f.1)
%res = and i32 %p, 255
ret i32 %res
}
In the exit block, %p would be a constant range [0, 256) including undef as
%p could be undef. We can use the range information to replace %f.1 with
false because we remove the compare, effectively forcing the use of the
constant to be != 300. We cannot replace %res with %p however, because
if %a would be undef %cond may be true but the second use might not be
< 256.
Currently LazyValueInfo uses the new behavior just when simplifying AND
instructions and does not distinguish between constant ranges with and
without undef otherwise. I think we should address the remaining issues
in LVI incrementally.
Reviewers: efriedma, reames, aqjune, jdoerfert, sstefan1
Reviewed By: efriedma
Differential Revision: https://reviews.llvm.org/D76931
2020-03-31 11:10:00 +01:00
|
|
|
if (Val.isConstantRangeIncludingUndef())
|
|
|
|
return OS << "constantrange incl. undef <"
|
|
|
|
<< Val.getConstantRange(true).getLower() << ", "
|
|
|
|
<< Val.getConstantRange(true).getUpper() << ">";
|
2020-03-15 10:35:39 +00:00
|
|
|
|
2017-09-28 11:09:22 +00:00
|
|
|
if (Val.isConstantRange())
|
|
|
|
return OS << "constantrange<" << Val.getConstantRange().getLower() << ", "
|
|
|
|
<< Val.getConstantRange().getUpper() << ">";
|
|
|
|
return OS << "constant<" << *Val.getConstant() << ">";
|
|
|
|
}
|
|
|
|
} // end namespace llvm
|