#include "llvm/Transforms/Scalar/ConstraintElimination.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/ConstraintSystem.h"
#include "llvm/Analysis/GlobalsModRef.h"
#include "llvm/Analysis/ValueTracking.h"
#include "llvm/IR/Dominators.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/PatternMatch.h"
#include "llvm/InitializePasses.h"
#include "llvm/Pass.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/DebugCounter.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Transforms/Scalar.h"
#include <string>
using namespace llvm;
using namespace PatternMatch;
#define DEBUG_TYPE "constraint-elimination"
STATISTIC(NumCondsRemoved, "Number of instructions removed");
DEBUG_COUNTER(EliminatedCounter, "conds-eliminated",
"Controls which conditions are eliminated");
static int64_t MaxConstraintValue = std::numeric_limits<int64_t>::max();
static int64_t MinSignedConstraintValue = std::numeric_limits<int64_t>::min();
namespace {
class ConstraintInfo;
struct StackEntry {
unsigned NumIn;
unsigned NumOut;
bool IsNot;
bool IsSigned = false;
SmallVector<Value *, 2> ValuesToRelease;
StackEntry(unsigned NumIn, unsigned NumOut, bool IsNot, bool IsSigned,
SmallVector<Value *, 2> ValuesToRelease)
: NumIn(NumIn), NumOut(NumOut), IsNot(IsNot), IsSigned(IsSigned),
ValuesToRelease(ValuesToRelease) {}
};
struct PreconditionTy {
CmpInst::Predicate Pred;
Value *Op0;
Value *Op1;
PreconditionTy(CmpInst::Predicate Pred, Value *Op0, Value *Op1)
: Pred(Pred), Op0(Op0), Op1(Op1) {}
};
struct ConstraintTy {
SmallVector<int64_t, 8> Coefficients;
SmallVector<PreconditionTy, 2> Preconditions;
bool IsSigned = false;
bool IsEq = false;
ConstraintTy() = default;
ConstraintTy(SmallVector<int64_t, 8> Coefficients, bool IsSigned)
: Coefficients(Coefficients), IsSigned(IsSigned) {}
unsigned size() const { return Coefficients.size(); }
unsigned empty() const { return Coefficients.empty(); }
bool needsNewIndices(const DenseMap<Value *, unsigned> &NewIndices) {
for (unsigned I = 0; I < NewIndices.size(); ++I) {
int64_t Last = Coefficients.pop_back_val();
if (Last != 0)
return true;
}
return false;
}
bool isValid(const ConstraintInfo &Info) const;
};
class ConstraintInfo {
DenseMap<Value *, unsigned> UnsignedValue2Index;
DenseMap<Value *, unsigned> SignedValue2Index;
ConstraintSystem UnsignedCS;
ConstraintSystem SignedCS;
public:
DenseMap<Value *, unsigned> &getValue2Index(bool Signed) {
return Signed ? SignedValue2Index : UnsignedValue2Index;
}
const DenseMap<Value *, unsigned> &getValue2Index(bool Signed) const {
return Signed ? SignedValue2Index : UnsignedValue2Index;
}
ConstraintSystem &getCS(bool Signed) {
return Signed ? SignedCS : UnsignedCS;
}
const ConstraintSystem &getCS(bool Signed) const {
return Signed ? SignedCS : UnsignedCS;
}
void popLastConstraint(bool Signed) { getCS(Signed).popLastConstraint(); }
void popLastNVariables(bool Signed, unsigned N) {
getCS(Signed).popLastNVariables(N);
}
bool doesHold(CmpInst::Predicate Pred, Value *A, Value *B) const;
void addFact(CmpInst::Predicate Pred, Value *A, Value *B, bool IsNegated,
unsigned NumIn, unsigned NumOut,
SmallVectorImpl<StackEntry> &DFSInStack);
ConstraintTy getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
DenseMap<Value *, unsigned> &NewIndices) const;
ConstraintTy getConstraint(CmpInst *Cmp,
DenseMap<Value *, unsigned> &NewIndices) const {
return getConstraint(Cmp->getPredicate(), Cmp->getOperand(0),
Cmp->getOperand(1), NewIndices);
}
void transferToOtherSystem(CmpInst::Predicate Pred, Value *A, Value *B,
bool IsNegated, unsigned NumIn, unsigned NumOut,
SmallVectorImpl<StackEntry> &DFSInStack);
};
}
static SmallVector<std::pair<int64_t, Value *>, 4>
decompose(Value *V, SmallVector<PreconditionTy, 4> &Preconditions,
bool IsSigned) {
auto CanUseSExt = [](ConstantInt *CI) {
const APInt &Val = CI->getValue();
return Val.sgt(MinSignedConstraintValue) && Val.slt(MaxConstraintValue);
};
if (IsSigned) {
if (auto *CI = dyn_cast<ConstantInt>(V)) {
if (CanUseSExt(CI))
return {{CI->getSExtValue(), nullptr}};
}
return {{0, nullptr}, {1, V}};
}
if (auto *CI = dyn_cast<ConstantInt>(V)) {
if (CI->uge(MaxConstraintValue))
return {};
return {{CI->getZExtValue(), nullptr}};
}
auto *GEP = dyn_cast<GetElementPtrInst>(V);
if (GEP && GEP->getNumOperands() == 2 && GEP->isInBounds()) {
Value *Op0, *Op1;
ConstantInt *CI;
if (match(GEP->getOperand(GEP->getNumOperands() - 1),
m_ZExt(m_Value(Op0)))) {
if (match(Op0, m_NUWShl(m_Value(Op1), m_ConstantInt(CI))) &&
CanUseSExt(CI))
return {{0, nullptr},
{1, GEP->getPointerOperand()},
{std::pow(int64_t(2), CI->getSExtValue()), Op1}};
if (match(Op0, m_NSWAdd(m_Value(Op1), m_ConstantInt(CI))) &&
CanUseSExt(CI))
return {{CI->getSExtValue(), nullptr},
{1, GEP->getPointerOperand()},
{1, Op1}};
return {{0, nullptr}, {1, GEP->getPointerOperand()}, {1, Op0}};
}
if (match(GEP->getOperand(GEP->getNumOperands() - 1), m_ConstantInt(CI)) &&
!CI->isNegative() && CanUseSExt(CI))
return {{CI->getSExtValue(), nullptr}, {1, GEP->getPointerOperand()}};
SmallVector<std::pair<int64_t, Value *>, 4> Result;
if (match(GEP->getOperand(GEP->getNumOperands() - 1),
m_NUWShl(m_Value(Op0), m_ConstantInt(CI))) &&
CanUseSExt(CI))
Result = {{0, nullptr},
{1, GEP->getPointerOperand()},
{std::pow(int64_t(2), CI->getSExtValue()), Op0}};
else if (match(GEP->getOperand(GEP->getNumOperands() - 1),
m_NSWAdd(m_Value(Op0), m_ConstantInt(CI))) &&
CanUseSExt(CI))
Result = {{CI->getSExtValue(), nullptr},
{1, GEP->getPointerOperand()},
{1, Op0}};
else {
Op0 = GEP->getOperand(GEP->getNumOperands() - 1);
Result = {{0, nullptr}, {1, GEP->getPointerOperand()}, {1, Op0}};
}
Preconditions.emplace_back(CmpInst::ICMP_SGE, Op0,
ConstantInt::get(Op0->getType(), 0));
return Result;
}
Value *Op0;
if (match(V, m_ZExt(m_Value(Op0))))
V = Op0;
Value *Op1;
ConstantInt *CI;
if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI))) &&
!CI->uge(MaxConstraintValue))
return {{CI->getZExtValue(), nullptr}, {1, Op0}};
if (match(V, m_Add(m_Value(Op0), m_ConstantInt(CI))) && CI->isNegative() &&
CanUseSExt(CI)) {
Preconditions.emplace_back(
CmpInst::ICMP_UGE, Op0,
ConstantInt::get(Op0->getType(), CI->getSExtValue() * -1));
return {{CI->getSExtValue(), nullptr}, {1, Op0}};
}
if (match(V, m_NUWAdd(m_Value(Op0), m_Value(Op1))))
return {{0, nullptr}, {1, Op0}, {1, Op1}};
if (match(V, m_NUWSub(m_Value(Op0), m_ConstantInt(CI))) && CanUseSExt(CI))
return {{-1 * CI->getSExtValue(), nullptr}, {1, Op0}};
if (match(V, m_NUWSub(m_Value(Op0), m_Value(Op1))))
return {{0, nullptr}, {1, Op0}, {-1, Op1}};
return {{0, nullptr}, {1, V}};
}
ConstraintTy
ConstraintInfo::getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
DenseMap<Value *, unsigned> &NewIndices) const {
bool IsEq = false;
switch (Pred) {
case CmpInst::ICMP_UGT:
case CmpInst::ICMP_UGE:
case CmpInst::ICMP_SGT:
case CmpInst::ICMP_SGE: {
Pred = CmpInst::getSwappedPredicate(Pred);
std::swap(Op0, Op1);
break;
}
case CmpInst::ICMP_EQ:
if (match(Op1, m_Zero())) {
Pred = CmpInst::ICMP_ULE;
} else {
IsEq = true;
Pred = CmpInst::ICMP_ULE;
}
break;
case CmpInst::ICMP_NE:
if (!match(Op1, m_Zero()))
return {};
Pred = CmpInst::getSwappedPredicate(CmpInst::ICMP_UGT);
std::swap(Op0, Op1);
break;
default:
break;
}
if (Pred != CmpInst::ICMP_ULE && Pred != CmpInst::ICMP_ULT &&
Pred != CmpInst::ICMP_SLE && Pred != CmpInst::ICMP_SLT)
return {};
SmallVector<PreconditionTy, 4> Preconditions;
bool IsSigned = CmpInst::isSigned(Pred);
auto &Value2Index = getValue2Index(IsSigned);
auto ADec = decompose(Op0->stripPointerCastsSameRepresentation(),
Preconditions, IsSigned);
auto BDec = decompose(Op1->stripPointerCastsSameRepresentation(),
Preconditions, IsSigned);
if (ADec.empty() || BDec.empty())
return {};
int64_t Offset1 = ADec[0].first;
int64_t Offset2 = BDec[0].first;
Offset1 *= -1;
auto VariablesA = llvm::drop_begin(ADec);
auto VariablesB = llvm::drop_begin(BDec);
auto GetOrAddIndex = [&Value2Index, &NewIndices](Value *V) -> unsigned {
auto V2I = Value2Index.find(V);
if (V2I != Value2Index.end())
return V2I->second;
auto Insert =
NewIndices.insert({V, Value2Index.size() + NewIndices.size() + 1});
return Insert.first->second;
};
for (const auto &KV :
concat<std::pair<int64_t, Value *>>(VariablesA, VariablesB))
GetOrAddIndex(KV.second);
ConstraintTy Res(
SmallVector<int64_t, 8>(Value2Index.size() + NewIndices.size() + 1, 0),
IsSigned);
Res.IsEq = IsEq;
auto &R = Res.Coefficients;
for (const auto &KV : VariablesA)
R[GetOrAddIndex(KV.second)] += KV.first;
for (const auto &KV : VariablesB)
R[GetOrAddIndex(KV.second)] -= KV.first;
int64_t OffsetSum;
if (AddOverflow(Offset1, Offset2, OffsetSum))
return {};
if (Pred == (IsSigned ? CmpInst::ICMP_SLT : CmpInst::ICMP_ULT))
if (AddOverflow(OffsetSum, int64_t(-1), OffsetSum))
return {};
R[0] = OffsetSum;
Res.Preconditions = std::move(Preconditions);
return Res;
}
bool ConstraintTy::isValid(const ConstraintInfo &Info) const {
return Coefficients.size() > 0 &&
all_of(Preconditions, [&Info](const PreconditionTy &C) {
return Info.doesHold(C.Pred, C.Op0, C.Op1);
});
}
bool ConstraintInfo::doesHold(CmpInst::Predicate Pred, Value *A,
Value *B) const {
DenseMap<Value *, unsigned> NewIndices;
auto R = getConstraint(Pred, A, B, NewIndices);
if (!NewIndices.empty())
return false;
return NewIndices.empty() && R.Preconditions.empty() && !R.IsEq &&
!R.empty() &&
getCS(CmpInst::isSigned(Pred)).isConditionImplied(R.Coefficients);
}
void ConstraintInfo::transferToOtherSystem(
CmpInst::Predicate Pred, Value *A, Value *B, bool IsNegated, unsigned NumIn,
unsigned NumOut, SmallVectorImpl<StackEntry> &DFSInStack) {
if (!A->getType()->isIntegerTy())
return;
switch (Pred) {
default:
break;
case CmpInst::ICMP_ULT:
if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0))) {
addFact(CmpInst::ICMP_SGE, A, ConstantInt::get(B->getType(), 0),
IsNegated, NumIn, NumOut, DFSInStack);
addFact(CmpInst::ICMP_SLT, A, B, IsNegated, NumIn, NumOut, DFSInStack);
}
break;
case CmpInst::ICMP_SLT:
if (doesHold(CmpInst::ICMP_SGE, A, ConstantInt::get(B->getType(), 0)))
addFact(CmpInst::ICMP_ULT, A, B, IsNegated, NumIn, NumOut, DFSInStack);
break;
case CmpInst::ICMP_SGT:
if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), -1)))
addFact(CmpInst::ICMP_UGE, A, ConstantInt::get(B->getType(), 0),
IsNegated, NumIn, NumOut, DFSInStack);
break;
case CmpInst::ICMP_SGE:
if (doesHold(CmpInst::ICMP_SGE, B, ConstantInt::get(B->getType(), 0))) {
addFact(CmpInst::ICMP_UGE, A, B, IsNegated, NumIn, NumOut, DFSInStack);
}
break;
}
}
namespace {
struct ConstraintOrBlock {
unsigned NumIn;
unsigned NumOut;
bool IsBlock;
bool Not;
union {
BasicBlock *BB;
CmpInst *Condition;
};
ConstraintOrBlock(DomTreeNode *DTN)
: NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()), IsBlock(true),
BB(DTN->getBlock()) {}
ConstraintOrBlock(DomTreeNode *DTN, CmpInst *Condition, bool Not)
: NumIn(DTN->getDFSNumIn()), NumOut(DTN->getDFSNumOut()), IsBlock(false),
Not(Not), Condition(Condition) {}
};
struct State {
DominatorTree &DT;
SmallVector<ConstraintOrBlock, 64> WorkList;
State(DominatorTree &DT) : DT(DT) {}
void addInfoFor(BasicBlock &BB);
bool canAddSuccessor(BasicBlock &BB, BasicBlock *Succ) const {
if (BB.getSingleSuccessor()) {
assert(BB.getSingleSuccessor() == Succ);
return DT.properlyDominates(&BB, Succ);
}
return any_of(successors(&BB),
[Succ](const BasicBlock *S) { return S != Succ; }) &&
all_of(predecessors(Succ), [&BB, Succ, this](BasicBlock *Pred) {
return Pred == &BB || DT.dominates(Succ, Pred);
});
}
};
}
#ifndef NDEBUG
static void dumpWithNames(const ConstraintSystem &CS,
DenseMap<Value *, unsigned> &Value2Index) {
SmallVector<std::string> Names(Value2Index.size(), "");
for (auto &KV : Value2Index) {
Names[KV.second - 1] = std::string("%") + KV.first->getName().str();
}
CS.dump(Names);
}
static void dumpWithNames(ArrayRef<int64_t> C,
DenseMap<Value *, unsigned> &Value2Index) {
ConstraintSystem CS;
CS.addVariableRowFill(C);
dumpWithNames(CS, Value2Index);
}
#endif
void State::addInfoFor(BasicBlock &BB) {
WorkList.emplace_back(DT.getNode(&BB));
bool GuaranteedToExecute = true;
for (Instruction &I : BB) {
Value *Cond;
if (match(&I, m_Intrinsic<Intrinsic::assume>(m_Value(Cond))) &&
isa<ICmpInst>(Cond)) {
if (GuaranteedToExecute) {
WorkList.emplace_back(DT.getNode(&BB), cast<ICmpInst>(Cond), false);
} else {
for (BasicBlock *Succ : successors(&BB)) {
if (!canAddSuccessor(BB, Succ))
continue;
WorkList.emplace_back(DT.getNode(Succ), cast<ICmpInst>(Cond), false);
}
}
}
GuaranteedToExecute &= isGuaranteedToTransferExecutionToSuccessor(&I);
}
auto *Br = dyn_cast<BranchInst>(BB.getTerminator());
if (!Br || !Br->isConditional())
return;
Value *Op0, *Op1;
if (match(Br->getCondition(), m_LogicalOr(m_Value(Op0), m_Value(Op1))) &&
isa<ICmpInst>(Op0) && isa<ICmpInst>(Op1)) {
BasicBlock *FalseSuccessor = Br->getSuccessor(1);
if (canAddSuccessor(BB, FalseSuccessor)) {
WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<ICmpInst>(Op0),
true);
WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<ICmpInst>(Op1),
true);
}
return;
}
if (match(Br->getCondition(), m_LogicalAnd(m_Value(Op0), m_Value(Op1))) &&
isa<ICmpInst>(Op0) && isa<ICmpInst>(Op1)) {
BasicBlock *TrueSuccessor = Br->getSuccessor(0);
if (canAddSuccessor(BB, TrueSuccessor)) {
WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<ICmpInst>(Op0),
false);
WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<ICmpInst>(Op1),
false);
}
return;
}
auto *CmpI = dyn_cast<ICmpInst>(Br->getCondition());
if (!CmpI)
return;
if (canAddSuccessor(BB, Br->getSuccessor(0)))
WorkList.emplace_back(DT.getNode(Br->getSuccessor(0)), CmpI, false);
if (canAddSuccessor(BB, Br->getSuccessor(1)))
WorkList.emplace_back(DT.getNode(Br->getSuccessor(1)), CmpI, true);
}
void ConstraintInfo::addFact(CmpInst::Predicate Pred, Value *A, Value *B,
bool IsNegated, unsigned NumIn, unsigned NumOut,
SmallVectorImpl<StackEntry> &DFSInStack) {
DenseMap<Value *, unsigned> NewIndices;
auto R = getConstraint(Pred, A, B, NewIndices);
if (!R.isValid(*this))
return;
bool Added = false;
assert(CmpInst::isSigned(Pred) == R.IsSigned &&
"condition and constraint signs must match");
auto &CSToUse = getCS(R.IsSigned);
if (R.Coefficients.empty())
return;
Added |= CSToUse.addVariableRowFill(R.Coefficients);
if (Added) {
SmallVector<Value *, 2> ValuesToRelease;
for (auto &KV : NewIndices) {
getValue2Index(R.IsSigned).insert(KV);
ValuesToRelease.push_back(KV.first);
}
LLVM_DEBUG({
dbgs() << " constraint: ";
dumpWithNames(R.Coefficients, getValue2Index(R.IsSigned));
});
DFSInStack.emplace_back(NumIn, NumOut, IsNegated, R.IsSigned,
ValuesToRelease);
if (R.IsEq) {
for (auto &Coeff : R.Coefficients)
Coeff *= -1;
CSToUse.addVariableRowFill(R.Coefficients);
DFSInStack.emplace_back(NumIn, NumOut, IsNegated, R.IsSigned,
SmallVector<Value *, 2>());
}
}
}
static void
tryToSimplifyOverflowMath(IntrinsicInst *II, ConstraintInfo &Info,
SmallVectorImpl<Instruction *> &ToRemove) {
auto DoesConditionHold = [](CmpInst::Predicate Pred, Value *A, Value *B,
ConstraintInfo &Info) {
DenseMap<Value *, unsigned> NewIndices;
auto R = Info.getConstraint(Pred, A, B, NewIndices);
if (R.size() < 2 || R.needsNewIndices(NewIndices) || !R.isValid(Info))
return false;
auto &CSToUse = Info.getCS(CmpInst::isSigned(Pred));
return CSToUse.isConditionImplied(R.Coefficients);
};
if (II->getIntrinsicID() == Intrinsic::ssub_with_overflow) {
Value *A = II->getArgOperand(0);
Value *B = II->getArgOperand(1);
if (!DoesConditionHold(CmpInst::ICMP_SGE, A, B, Info) ||
!DoesConditionHold(CmpInst::ICMP_SGE, B,
ConstantInt::get(A->getType(), 0), Info))
return;
IRBuilder<> Builder(II->getParent(), II->getIterator());
Value *Sub = nullptr;
for (User *U : make_early_inc_range(II->users())) {
if (match(U, m_ExtractValue<0>(m_Value()))) {
if (!Sub)
Sub = Builder.CreateSub(A, B);
U->replaceAllUsesWith(Sub);
} else if (match(U, m_ExtractValue<1>(m_Value())))
U->replaceAllUsesWith(Builder.getFalse());
else
continue;
if (U->use_empty()) {
auto *I = cast<Instruction>(U);
ToRemove.push_back(I);
I->setOperand(0, PoisonValue::get(II->getType()));
}
}
if (II->use_empty())
II->eraseFromParent();
}
}
static bool eliminateConstraints(Function &F, DominatorTree &DT) {
bool Changed = false;
DT.updateDFSNumbers();
ConstraintInfo Info;
State S(DT);
for (BasicBlock &BB : F) {
if (!DT.getNode(&BB))
continue;
S.addInfoFor(BB);
}
stable_sort(S.WorkList, [](const ConstraintOrBlock &A, const ConstraintOrBlock &B) {
return std::tie(A.NumIn, A.IsBlock) < std::tie(B.NumIn, B.IsBlock);
});
SmallVector<Instruction *> ToRemove;
SmallVector<StackEntry, 16> DFSInStack;
for (ConstraintOrBlock &CB : S.WorkList) {
while (!DFSInStack.empty()) {
auto &E = DFSInStack.back();
LLVM_DEBUG(dbgs() << "Top of stack : " << E.NumIn << " " << E.NumOut
<< "\n");
LLVM_DEBUG(dbgs() << "CB: " << CB.NumIn << " " << CB.NumOut << "\n");
assert(E.NumIn <= CB.NumIn);
if (CB.NumOut <= E.NumOut)
break;
LLVM_DEBUG({
dbgs() << "Removing ";
dumpWithNames(Info.getCS(E.IsSigned).getLastConstraint(),
Info.getValue2Index(E.IsSigned));
dbgs() << "\n";
});
Info.popLastConstraint(E.IsSigned);
auto &Mapping = Info.getValue2Index(E.IsSigned);
for (Value *V : E.ValuesToRelease)
Mapping.erase(V);
Info.popLastNVariables(E.IsSigned, E.ValuesToRelease.size());
DFSInStack.pop_back();
}
LLVM_DEBUG({
dbgs() << "Processing ";
if (CB.IsBlock)
dbgs() << *CB.BB;
else
dbgs() << *CB.Condition;
dbgs() << "\n";
});
if (CB.IsBlock) {
for (Instruction &I : make_early_inc_range(*CB.BB)) {
if (auto *II = dyn_cast<WithOverflowInst>(&I)) {
tryToSimplifyOverflowMath(II, Info, ToRemove);
continue;
}
auto *Cmp = dyn_cast<ICmpInst>(&I);
if (!Cmp)
continue;
DenseMap<Value *, unsigned> NewIndices;
auto R = Info.getConstraint(Cmp, NewIndices);
if (R.IsEq || R.empty() || R.needsNewIndices(NewIndices) ||
!R.isValid(Info))
continue;
auto &CSToUse = Info.getCS(R.IsSigned);
if (CSToUse.isConditionImplied(R.Coefficients)) {
if (!DebugCounter::shouldExecute(EliminatedCounter))
continue;
LLVM_DEBUG({
dbgs() << "Condition " << *Cmp
<< " implied by dominating constraints\n";
dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned));
});
Cmp->replaceUsesWithIf(
ConstantInt::getTrue(F.getParent()->getContext()), [](Use &U) {
auto *II = dyn_cast<IntrinsicInst>(U.getUser());
return !II || II->getIntrinsicID() != Intrinsic::assume;
});
NumCondsRemoved++;
Changed = true;
}
if (CSToUse.isConditionImplied(
ConstraintSystem::negate(R.Coefficients))) {
if (!DebugCounter::shouldExecute(EliminatedCounter))
continue;
LLVM_DEBUG({
dbgs() << "Condition !" << *Cmp
<< " implied by dominating constraints\n";
dumpWithNames(CSToUse, Info.getValue2Index(R.IsSigned));
});
Cmp->replaceAllUsesWith(
ConstantInt::getFalse(F.getParent()->getContext()));
NumCondsRemoved++;
Changed = true;
}
}
continue;
}
auto *CI = dyn_cast<ICmpInst>(CB.Condition);
auto PredicateRestorer = make_scope_exit([CI, &CB]() {
if (CB.Not && CI)
CI->setPredicate(CI->getInversePredicate());
});
if (CB.Not) {
if (CI) {
CI->setPredicate(CI->getInversePredicate());
} else {
LLVM_DEBUG(dbgs() << "Can only negate compares so far.\n");
continue;
}
}
ICmpInst::Predicate Pred;
Value *A, *B;
if (match(CB.Condition, m_ICmp(Pred, m_Value(A), m_Value(B)))) {
Info.addFact(Pred, A, B, CB.Not, CB.NumIn, CB.NumOut, DFSInStack);
Info.transferToOtherSystem(Pred, A, B, CB.Not, CB.NumIn, CB.NumOut,
DFSInStack);
}
}
#ifndef NDEBUG
unsigned SignedEntries =
count_if(DFSInStack, [](const StackEntry &E) { return E.IsSigned; });
assert(Info.getCS(false).size() == DFSInStack.size() - SignedEntries &&
"updates to CS and DFSInStack are out of sync");
assert(Info.getCS(true).size() == SignedEntries &&
"updates to CS and DFSInStack are out of sync");
#endif
for (Instruction *I : ToRemove)
I->eraseFromParent();
return Changed;
}
PreservedAnalyses ConstraintEliminationPass::run(Function &F,
FunctionAnalysisManager &AM) {
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
if (!eliminateConstraints(F, DT))
return PreservedAnalyses::all();
PreservedAnalyses PA;
PA.preserve<DominatorTreeAnalysis>();
PA.preserveSet<CFGAnalyses>();
return PA;
}
namespace {
class ConstraintElimination : public FunctionPass {
public:
static char ID;
ConstraintElimination() : FunctionPass(ID) {
initializeConstraintEliminationPass(*PassRegistry::getPassRegistry());
}
bool runOnFunction(Function &F) override {
auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
return eliminateConstraints(F, DT);
}
void getAnalysisUsage(AnalysisUsage &AU) const override {
AU.setPreservesCFG();
AU.addRequired<DominatorTreeWrapperPass>();
AU.addPreserved<GlobalsAAWrapperPass>();
AU.addPreserved<DominatorTreeWrapperPass>();
}
};
}
char ConstraintElimination::ID = 0;
INITIALIZE_PASS_BEGIN(ConstraintElimination, "constraint-elimination",
"Constraint Elimination", false, false)
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
INITIALIZE_PASS_DEPENDENCY(LazyValueInfoWrapperPass)
INITIALIZE_PASS_END(ConstraintElimination, "constraint-elimination",
"Constraint Elimination", false, false)
FunctionPass *llvm::createConstraintEliminationPass() {
return new ConstraintElimination();
}