2020-09-15 15:47:23 +02:00
|
|
|
//===-- ConstraintElimination.cpp - Eliminate conds using constraints. ----===//
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Eliminate conditions based on constraints collected from dominating
|
|
|
|
// conditions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2020-09-27 20:12:13 +02:00
|
|
|
#include "llvm/Transforms/Scalar/ConstraintElimination.h"
|
2020-11-17 18:38:19 +01:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2021-01-28 14:41:21 +01:00
|
|
|
#include "llvm/ADT/ScopeExit.h"
|
2020-09-15 15:47:23 +02:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
|
|
|
#include "llvm/ADT/Statistic.h"
|
|
|
|
#include "llvm/Analysis/ConstraintSystem.h"
|
|
|
|
#include "llvm/Analysis/GlobalsModRef.h"
|
|
|
|
#include "llvm/IR/DataLayout.h"
|
|
|
|
#include "llvm/IR/Dominators.h"
|
|
|
|
#include "llvm/IR/Function.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/Transforms/Scalar.h"
|
|
|
|
|
2021-02-02 16:27:58 +01:00
|
|
|
#include <string>
|
|
|
|
|
2020-09-15 15:47:23 +02:00
|
|
|
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();
|
|
|
|
|
2020-11-17 18:38:19 +01:00
|
|
|
// Decomposes \p V into a vector of pairs of the form { c, X } where c * X. The
|
|
|
|
// sum of the pairs equals \p V. The first pair is the constant-factor and X
|
|
|
|
// must be nullptr. If the expression cannot be decomposed, returns an empty
|
|
|
|
// vector.
|
|
|
|
static SmallVector<std::pair<int64_t, Value *>, 4> decompose(Value *V) {
|
2020-09-15 15:47:23 +02:00
|
|
|
if (auto *CI = dyn_cast<ConstantInt>(V)) {
|
|
|
|
if (CI->isNegative() || CI->uge(MaxConstraintValue))
|
|
|
|
return {};
|
|
|
|
return {{CI->getSExtValue(), nullptr}};
|
|
|
|
}
|
|
|
|
auto *GEP = dyn_cast<GetElementPtrInst>(V);
|
2021-02-07 12:07:49 +01:00
|
|
|
if (GEP && GEP->getNumOperands() == 2 && GEP->isInBounds()) {
|
2020-11-18 12:45:26 +01:00
|
|
|
if (isa<ConstantInt>(GEP->getOperand(GEP->getNumOperands() - 1))) {
|
|
|
|
return {{cast<ConstantInt>(GEP->getOperand(GEP->getNumOperands() - 1))
|
|
|
|
->getSExtValue(),
|
|
|
|
nullptr},
|
|
|
|
{1, GEP->getPointerOperand()}};
|
|
|
|
}
|
2020-11-30 16:26:51 +01:00
|
|
|
Value *Op0;
|
|
|
|
ConstantInt *CI;
|
|
|
|
if (match(GEP->getOperand(GEP->getNumOperands() - 1),
|
|
|
|
m_NUWShl(m_Value(Op0), m_ConstantInt(CI))))
|
|
|
|
return {{0, nullptr},
|
|
|
|
{1, GEP->getPointerOperand()},
|
2020-12-02 23:22:17 +01:00
|
|
|
{std::pow(int64_t(2), CI->getSExtValue()), Op0}};
|
2020-11-30 16:43:39 +01:00
|
|
|
if (match(GEP->getOperand(GEP->getNumOperands() - 1),
|
|
|
|
m_ZExt(m_NUWShl(m_Value(Op0), m_ConstantInt(CI)))))
|
|
|
|
return {{0, nullptr},
|
|
|
|
{1, GEP->getPointerOperand()},
|
2020-12-02 23:22:17 +01:00
|
|
|
{std::pow(int64_t(2), CI->getSExtValue()), Op0}};
|
2020-11-30 16:26:51 +01:00
|
|
|
|
2020-11-18 12:45:26 +01:00
|
|
|
return {{0, nullptr},
|
|
|
|
{1, GEP->getPointerOperand()},
|
|
|
|
{1, GEP->getOperand(GEP->getNumOperands() - 1)}};
|
2020-09-15 15:47:23 +02:00
|
|
|
}
|
2020-11-18 12:25:39 +01:00
|
|
|
|
|
|
|
Value *Op0;
|
2021-02-07 21:53:06 +01:00
|
|
|
if (match(V, m_ZExt(m_Value(Op0))))
|
|
|
|
V = Op0;
|
|
|
|
|
2020-11-18 12:25:39 +01:00
|
|
|
Value *Op1;
|
|
|
|
ConstantInt *CI;
|
|
|
|
if (match(V, m_NUWAdd(m_Value(Op0), m_ConstantInt(CI))))
|
|
|
|
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))))
|
|
|
|
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}};
|
|
|
|
|
2020-11-17 18:38:19 +01:00
|
|
|
return {{0, nullptr}, {1, V}};
|
2020-09-15 15:47:23 +02:00
|
|
|
}
|
|
|
|
|
2021-01-28 11:37:50 +01:00
|
|
|
struct ConstraintTy {
|
|
|
|
SmallVector<int64_t, 8> Coefficients;
|
|
|
|
|
|
|
|
ConstraintTy(SmallVector<int64_t, 8> Coefficients)
|
|
|
|
: Coefficients(Coefficients) {}
|
|
|
|
|
|
|
|
unsigned size() const { return Coefficients.size(); }
|
|
|
|
};
|
|
|
|
|
2020-09-15 15:47:23 +02:00
|
|
|
/// Turn a condition \p CmpI into a constraint vector, using indices from \p
|
|
|
|
/// Value2Index. If \p ShouldAdd is true, new indices are added for values not
|
|
|
|
/// yet in \p Value2Index.
|
2021-01-28 11:37:50 +01:00
|
|
|
static SmallVector<ConstraintTy, 4>
|
2020-09-15 15:47:23 +02:00
|
|
|
getConstraint(CmpInst::Predicate Pred, Value *Op0, Value *Op1,
|
|
|
|
DenseMap<Value *, unsigned> &Value2Index, bool ShouldAdd) {
|
|
|
|
int64_t Offset1 = 0;
|
|
|
|
int64_t Offset2 = 0;
|
|
|
|
|
|
|
|
auto TryToGetIndex = [ShouldAdd,
|
|
|
|
&Value2Index](Value *V) -> Optional<unsigned> {
|
|
|
|
if (ShouldAdd) {
|
|
|
|
Value2Index.insert({V, Value2Index.size() + 1});
|
|
|
|
return Value2Index[V];
|
|
|
|
}
|
|
|
|
auto I = Value2Index.find(V);
|
|
|
|
if (I == Value2Index.end())
|
|
|
|
return None;
|
|
|
|
return I->second;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (Pred == CmpInst::ICMP_UGT || Pred == CmpInst::ICMP_UGE)
|
|
|
|
return getConstraint(CmpInst::getSwappedPredicate(Pred), Op1, Op0,
|
|
|
|
Value2Index, ShouldAdd);
|
|
|
|
|
2021-01-28 11:37:50 +01:00
|
|
|
if (Pred == CmpInst::ICMP_EQ) {
|
|
|
|
auto A = getConstraint(CmpInst::ICMP_UGE, Op0, Op1, Value2Index, ShouldAdd);
|
|
|
|
auto B = getConstraint(CmpInst::ICMP_ULE, Op0, Op1, Value2Index, ShouldAdd);
|
|
|
|
append_range(A, B);
|
|
|
|
return A;
|
|
|
|
}
|
|
|
|
|
2020-11-17 18:38:19 +01:00
|
|
|
// Only ULE and ULT predicates are supported at the moment.
|
|
|
|
if (Pred != CmpInst::ICMP_ULE && Pred != CmpInst::ICMP_ULT)
|
|
|
|
return {};
|
|
|
|
|
2021-02-02 18:01:40 +01:00
|
|
|
auto ADec = decompose(Op0->stripPointerCasts());
|
|
|
|
auto BDec = decompose(Op1->stripPointerCasts());
|
2020-11-17 18:38:19 +01:00
|
|
|
// Skip if decomposing either of the values failed.
|
|
|
|
if (ADec.empty() || BDec.empty())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// Skip trivial constraints without any variables.
|
|
|
|
if (ADec.size() == 1 && BDec.size() == 1)
|
|
|
|
return {};
|
|
|
|
|
|
|
|
Offset1 = ADec[0].first;
|
|
|
|
Offset2 = BDec[0].first;
|
|
|
|
Offset1 *= -1;
|
|
|
|
|
|
|
|
// Create iterator ranges that skip the constant-factor.
|
|
|
|
auto VariablesA = make_range(std::next(ADec.begin()), ADec.end());
|
|
|
|
auto VariablesB = make_range(std::next(BDec.begin()), BDec.end());
|
|
|
|
|
|
|
|
// Check if each referenced value in the constraint is already in the system
|
|
|
|
// or can be added (if ShouldAdd is true).
|
|
|
|
for (const auto &KV :
|
|
|
|
concat<std::pair<int64_t, Value *>>(VariablesA, VariablesB))
|
|
|
|
if (!TryToGetIndex(KV.second))
|
2020-09-15 15:47:23 +02:00
|
|
|
return {};
|
|
|
|
|
2020-11-17 18:38:19 +01:00
|
|
|
// Build result constraint, by first adding all coefficients from A and then
|
|
|
|
// subtracting all coefficients from B.
|
|
|
|
SmallVector<int64_t, 8> R(Value2Index.size() + 1, 0);
|
|
|
|
for (const auto &KV : VariablesA)
|
|
|
|
R[Value2Index[KV.second]] += KV.first;
|
2020-09-15 15:47:23 +02:00
|
|
|
|
2020-11-17 18:38:19 +01:00
|
|
|
for (const auto &KV : VariablesB)
|
|
|
|
R[Value2Index[KV.second]] -= KV.first;
|
2020-09-15 15:47:23 +02:00
|
|
|
|
2020-11-17 18:38:19 +01:00
|
|
|
R[0] = Offset1 + Offset2 + (Pred == CmpInst::ICMP_ULT ? -1 : 0);
|
2021-01-28 11:37:50 +01:00
|
|
|
return {R};
|
2020-09-15 15:47:23 +02:00
|
|
|
}
|
|
|
|
|
2021-01-28 11:37:50 +01:00
|
|
|
static SmallVector<ConstraintTy, 4>
|
2020-09-15 15:47:23 +02:00
|
|
|
getConstraint(CmpInst *Cmp, DenseMap<Value *, unsigned> &Value2Index,
|
|
|
|
bool ShouldAdd) {
|
|
|
|
return getConstraint(Cmp->getPredicate(), Cmp->getOperand(0),
|
|
|
|
Cmp->getOperand(1), Value2Index, ShouldAdd);
|
|
|
|
}
|
|
|
|
|
2020-09-27 00:04:39 +02:00
|
|
|
namespace {
|
2020-09-15 15:47:23 +02:00
|
|
|
/// Represents either a condition that holds on entry to a block or a basic
|
|
|
|
/// block, with their respective Dominator DFS in and out numbers.
|
|
|
|
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 StackEntry {
|
|
|
|
unsigned NumIn;
|
|
|
|
unsigned NumOut;
|
|
|
|
CmpInst *Condition;
|
|
|
|
bool IsNot;
|
|
|
|
|
|
|
|
StackEntry(unsigned NumIn, unsigned NumOut, CmpInst *Condition, bool IsNot)
|
|
|
|
: NumIn(NumIn), NumOut(NumOut), Condition(Condition), IsNot(IsNot) {}
|
|
|
|
};
|
2020-09-27 00:04:39 +02:00
|
|
|
} // namespace
|
2020-09-15 15:47:23 +02:00
|
|
|
|
2021-02-02 19:23:14 +01:00
|
|
|
#ifndef NDEBUG
|
2021-02-02 16:27:58 +01:00
|
|
|
static void dumpWithNames(ConstraintTy &C,
|
|
|
|
DenseMap<Value *, unsigned> &Value2Index) {
|
|
|
|
SmallVector<std::string> Names(Value2Index.size(), "");
|
|
|
|
for (auto &KV : Value2Index) {
|
|
|
|
Names[KV.second - 1] = std::string("%") + KV.first->getName().str();
|
|
|
|
}
|
|
|
|
ConstraintSystem CS;
|
|
|
|
CS.addVariableRowFill(C.Coefficients);
|
|
|
|
CS.dump(Names);
|
|
|
|
}
|
2021-02-02 19:23:14 +01:00
|
|
|
#endif
|
2021-02-02 16:27:58 +01:00
|
|
|
|
2020-09-15 15:47:23 +02:00
|
|
|
static bool eliminateConstraints(Function &F, DominatorTree &DT) {
|
|
|
|
bool Changed = false;
|
|
|
|
DT.updateDFSNumbers();
|
|
|
|
ConstraintSystem CS;
|
|
|
|
|
|
|
|
SmallVector<ConstraintOrBlock, 64> WorkList;
|
|
|
|
|
|
|
|
// First, collect conditions implied by branches and blocks with their
|
|
|
|
// Dominator DFS in and out numbers.
|
|
|
|
for (BasicBlock &BB : F) {
|
|
|
|
if (!DT.getNode(&BB))
|
|
|
|
continue;
|
|
|
|
WorkList.emplace_back(DT.getNode(&BB));
|
|
|
|
|
|
|
|
auto *Br = dyn_cast<BranchInst>(BB.getTerminator());
|
|
|
|
if (!Br || !Br->isConditional())
|
|
|
|
continue;
|
2020-11-16 22:44:13 +01:00
|
|
|
|
2021-02-03 13:37:40 +01:00
|
|
|
// Returns true if we can add a known condition from BB to its successor
|
|
|
|
// block Succ. Each predecessor of Succ can either be BB or be dominated by
|
|
|
|
// Succ (e.g. the case when adding a condition from a pre-header to a loop
|
|
|
|
// header).
|
|
|
|
auto CanAdd = [&BB, &DT](BasicBlock *Succ) {
|
|
|
|
return all_of(predecessors(Succ), [&BB, &DT, Succ](BasicBlock *Pred) {
|
|
|
|
return Pred == &BB || DT.dominates(Succ, Pred);
|
|
|
|
});
|
|
|
|
};
|
2020-11-16 22:44:13 +01:00
|
|
|
// If the condition is an OR of 2 compares and the false successor only has
|
|
|
|
// the current block as predecessor, queue both negated conditions for the
|
|
|
|
// false successor.
|
2020-12-30 08:11:54 +01:00
|
|
|
Value *Op0, *Op1;
|
|
|
|
if (match(Br->getCondition(), m_LogicalOr(m_Value(Op0), m_Value(Op1))) &&
|
|
|
|
match(Op0, m_Cmp()) && match(Op1, m_Cmp())) {
|
2020-11-16 22:44:13 +01:00
|
|
|
BasicBlock *FalseSuccessor = Br->getSuccessor(1);
|
2021-02-03 13:37:40 +01:00
|
|
|
if (CanAdd(FalseSuccessor)) {
|
2020-12-30 08:11:54 +01:00
|
|
|
WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<CmpInst>(Op0),
|
|
|
|
true);
|
|
|
|
WorkList.emplace_back(DT.getNode(FalseSuccessor), cast<CmpInst>(Op1),
|
|
|
|
true);
|
2020-11-16 22:44:13 +01:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-08-05 16:24:54 +02:00
|
|
|
// If the condition is an AND of 2 compares and the true successor only has
|
|
|
|
// the current block as predecessor, queue both conditions for the true
|
|
|
|
// successor.
|
2020-12-30 08:11:54 +01:00
|
|
|
if (match(Br->getCondition(), m_LogicalAnd(m_Value(Op0), m_Value(Op1))) &&
|
|
|
|
match(Op0, m_Cmp()) && match(Op1, m_Cmp())) {
|
2020-08-05 16:24:54 +02:00
|
|
|
BasicBlock *TrueSuccessor = Br->getSuccessor(0);
|
2021-02-03 13:37:40 +01:00
|
|
|
if (CanAdd(TrueSuccessor)) {
|
2020-12-30 08:11:54 +01:00
|
|
|
WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<CmpInst>(Op0),
|
|
|
|
false);
|
|
|
|
WorkList.emplace_back(DT.getNode(TrueSuccessor), cast<CmpInst>(Op1),
|
|
|
|
false);
|
2020-08-05 16:24:54 +02:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:47:23 +02:00
|
|
|
auto *CmpI = dyn_cast<CmpInst>(Br->getCondition());
|
|
|
|
if (!CmpI)
|
|
|
|
continue;
|
2021-02-03 13:37:40 +01:00
|
|
|
if (CanAdd(Br->getSuccessor(0)))
|
2020-09-15 15:47:23 +02:00
|
|
|
WorkList.emplace_back(DT.getNode(Br->getSuccessor(0)), CmpI, false);
|
2021-02-03 13:37:40 +01:00
|
|
|
if (CanAdd(Br->getSuccessor(1)))
|
2020-09-15 15:47:23 +02:00
|
|
|
WorkList.emplace_back(DT.getNode(Br->getSuccessor(1)), CmpI, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next, sort worklist by dominance, so that dominating blocks and conditions
|
|
|
|
// come before blocks and conditions dominated by them. If a block and a
|
|
|
|
// condition have the same numbers, the condition comes before the block, as
|
|
|
|
// it holds on entry to the block.
|
2021-01-15 05:30:31 +01:00
|
|
|
sort(WorkList, [](const ConstraintOrBlock &A, const ConstraintOrBlock &B) {
|
|
|
|
return std::tie(A.NumIn, A.IsBlock) < std::tie(B.NumIn, B.IsBlock);
|
|
|
|
});
|
2020-09-15 15:47:23 +02:00
|
|
|
|
|
|
|
// Finally, process ordered worklist and eliminate implied conditions.
|
|
|
|
SmallVector<StackEntry, 16> DFSInStack;
|
|
|
|
DenseMap<Value *, unsigned> Value2Index;
|
|
|
|
for (ConstraintOrBlock &CB : WorkList) {
|
|
|
|
// First, pop entries from the stack that are out-of-scope for CB. Remove
|
|
|
|
// the corresponding entry from the constraint system.
|
|
|
|
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");
|
2020-09-27 00:04:39 +02:00
|
|
|
assert(E.NumIn <= CB.NumIn);
|
|
|
|
if (CB.NumOut <= E.NumOut)
|
2020-09-15 15:47:23 +02:00
|
|
|
break;
|
|
|
|
LLVM_DEBUG(dbgs() << "Removing " << *E.Condition << " " << E.IsNot
|
|
|
|
<< "\n");
|
|
|
|
DFSInStack.pop_back();
|
|
|
|
CS.popLastConstraint();
|
|
|
|
}
|
|
|
|
|
|
|
|
LLVM_DEBUG({
|
|
|
|
dbgs() << "Processing ";
|
|
|
|
if (CB.IsBlock)
|
|
|
|
dbgs() << *CB.BB;
|
|
|
|
else
|
|
|
|
dbgs() << *CB.Condition;
|
|
|
|
dbgs() << "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
// For a block, check if any CmpInsts become known based on the current set
|
|
|
|
// of constraints.
|
|
|
|
if (CB.IsBlock) {
|
|
|
|
for (Instruction &I : *CB.BB) {
|
|
|
|
auto *Cmp = dyn_cast<CmpInst>(&I);
|
|
|
|
if (!Cmp)
|
|
|
|
continue;
|
|
|
|
auto R = getConstraint(Cmp, Value2Index, false);
|
2021-01-28 11:37:50 +01:00
|
|
|
if (R.size() != 1 || R[0].size() == 1)
|
2020-09-15 15:47:23 +02:00
|
|
|
continue;
|
2021-01-28 11:37:50 +01:00
|
|
|
if (CS.isConditionImplied(R[0].Coefficients)) {
|
2020-09-15 15:47:23 +02:00
|
|
|
if (!DebugCounter::shouldExecute(EliminatedCounter))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Condition " << *Cmp
|
|
|
|
<< " implied by dominating constraints\n");
|
|
|
|
LLVM_DEBUG({
|
|
|
|
for (auto &E : reverse(DFSInStack))
|
|
|
|
dbgs() << " C " << *E.Condition << " " << E.IsNot << "\n";
|
|
|
|
});
|
|
|
|
Cmp->replaceAllUsesWith(
|
|
|
|
ConstantInt::getTrue(F.getParent()->getContext()));
|
|
|
|
NumCondsRemoved++;
|
|
|
|
Changed = true;
|
|
|
|
}
|
2021-01-28 11:37:50 +01:00
|
|
|
if (CS.isConditionImplied(
|
|
|
|
ConstraintSystem::negate(R[0].Coefficients))) {
|
2020-09-15 15:47:23 +02:00
|
|
|
if (!DebugCounter::shouldExecute(EliminatedCounter))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Condition !" << *Cmp
|
|
|
|
<< " implied by dominating constraints\n");
|
|
|
|
LLVM_DEBUG({
|
|
|
|
for (auto &E : reverse(DFSInStack))
|
|
|
|
dbgs() << " C " << *E.Condition << " " << E.IsNot << "\n";
|
|
|
|
});
|
|
|
|
Cmp->replaceAllUsesWith(
|
|
|
|
ConstantInt::getFalse(F.getParent()->getContext()));
|
|
|
|
NumCondsRemoved++;
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-01-28 14:41:21 +01:00
|
|
|
// Set up a function to restore the predicate at the end of the scope if it
|
|
|
|
// has been negated. Negate the predicate in-place, if required.
|
|
|
|
auto *CI = dyn_cast<CmpInst>(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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:47:23 +02:00
|
|
|
// Otherwise, add the condition to the system and stack, if we can transform
|
|
|
|
// it into a constraint.
|
|
|
|
auto R = getConstraint(CB.Condition, Value2Index, true);
|
|
|
|
if (R.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LLVM_DEBUG(dbgs() << "Adding " << *CB.Condition << " " << CB.Not << "\n");
|
2021-01-28 11:37:50 +01:00
|
|
|
bool Added = false;
|
|
|
|
for (auto &C : R) {
|
|
|
|
auto Coeffs = C.Coefficients;
|
2021-02-02 16:27:58 +01:00
|
|
|
LLVM_DEBUG({
|
|
|
|
dbgs() << " constraint: ";
|
|
|
|
dumpWithNames(C, Value2Index);
|
|
|
|
});
|
2021-01-28 11:37:50 +01:00
|
|
|
Added |= CS.addVariableRowFill(Coeffs);
|
|
|
|
// If R has been added to the system, queue it for removal once it goes
|
|
|
|
// out-of-scope.
|
|
|
|
if (Added)
|
|
|
|
DFSInStack.emplace_back(CB.NumIn, CB.NumOut, CB.Condition, CB.Not);
|
|
|
|
}
|
2020-09-15 15:47:23 +02:00
|
|
|
}
|
|
|
|
|
2021-01-30 18:56:51 +01:00
|
|
|
assert(CS.size() == DFSInStack.size() &&
|
|
|
|
"updates to CS and DFSInStack are out of sync");
|
2020-09-15 15:47:23 +02:00
|
|
|
return Changed;
|
|
|
|
}
|
|
|
|
|
2020-09-27 20:12:13 +02:00
|
|
|
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.preserve<GlobalsAA>();
|
|
|
|
PA.preserveSet<CFGAnalyses>();
|
|
|
|
return PA;
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:47:23 +02:00
|
|
|
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>();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // end anonymous namespace
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|