2008-12-05 07:49:08 +00:00
|
|
|
//===- GVN.cpp - Eliminate redundant values and loads ---------------------===//
|
2007-07-24 17:55:58 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 20:36:04 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2007-07-24 17:55:58 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This pass performs global value numbering to eliminate fully redundant
|
|
|
|
// instructions. It also performs simple dead load elimination.
|
|
|
|
//
|
2009-03-10 15:04:53 +00:00
|
|
|
// Note that this pass does the value numbering itself; it does not use the
|
2008-06-05 07:55:49 +00:00
|
|
|
// ValueNumbering analysis passes.
|
|
|
|
//
|
2007-07-24 17:55:58 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2012-06-29 12:38:19 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/DepthFirstIterator.h"
|
|
|
|
#include "llvm/ADT/Hashing.h"
|
2014-05-13 21:06:40 +00:00
|
|
|
#include "llvm/ADT/MapVector.h"
|
2015-01-09 19:19:56 +00:00
|
|
|
#include "llvm/ADT/PostOrderIterator.h"
|
2013-11-11 22:00:23 +00:00
|
|
|
#include "llvm/ADT/SetVector.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/ADT/SmallPtrSet.h"
|
2012-06-29 12:38:19 +00:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2007-10-18 19:39:33 +00:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2015-01-04 12:03:27 +00:00
|
|
|
#include "llvm/Analysis/AssumptionCache.h"
|
2013-07-27 01:24:00 +00:00
|
|
|
#include "llvm/Analysis/CFG.h"
|
2009-12-06 05:29:56 +00:00
|
|
|
#include "llvm/Analysis/ConstantFolding.h"
|
2010-11-12 21:10:24 +00:00
|
|
|
#include "llvm/Analysis/InstructionSimplify.h"
|
2010-05-28 16:19:17 +00:00
|
|
|
#include "llvm/Analysis/Loads.h"
|
2009-10-27 20:05:49 +00:00
|
|
|
#include "llvm/Analysis/MemoryBuiltins.h"
|
2007-07-24 17:55:58 +00:00
|
|
|
#include "llvm/Analysis/MemoryDependenceAnalysis.h"
|
2009-12-09 01:59:31 +00:00
|
|
|
#include "llvm/Analysis/PHITransAddr.h"
|
2010-11-30 22:25:26 +00:00
|
|
|
#include "llvm/Analysis/ValueTracking.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/DataLayout.h"
|
2014-01-13 09:26:24 +00:00
|
|
|
#include "llvm/IR/Dominators.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/GlobalVariable.h"
|
|
|
|
#include "llvm/IR/IRBuilder.h"
|
|
|
|
#include "llvm/IR/IntrinsicInst.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Metadata.h"
|
2014-03-04 11:08:18 +00:00
|
|
|
#include "llvm/IR/PatternMatch.h"
|
2010-11-18 18:32:40 +00:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2008-06-19 19:57:25 +00:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2008-03-29 04:36:18 +00:00
|
|
|
#include "llvm/Support/Debug.h"
|
2015-01-15 02:16:27 +00:00
|
|
|
#include "llvm/Analysis/TargetLibraryInfo.h"
|
2012-06-29 12:38:19 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2014-08-15 15:46:38 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2012-06-29 12:38:19 +00:00
|
|
|
#include "llvm/Transforms/Utils/SSAUpdater.h"
|
2013-05-09 18:34:27 +00:00
|
|
|
#include <vector>
|
2007-07-24 17:55:58 +00:00
|
|
|
using namespace llvm;
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
using namespace PatternMatch;
|
2007-07-24 17:55:58 +00:00
|
|
|
|
2014-04-22 02:55:47 +00:00
|
|
|
#define DEBUG_TYPE "gvn"
|
|
|
|
|
2008-12-22 22:14:07 +00:00
|
|
|
STATISTIC(NumGVNInstr, "Number of instructions deleted");
|
|
|
|
STATISTIC(NumGVNLoad, "Number of loads deleted");
|
|
|
|
STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
|
2008-07-15 16:28:06 +00:00
|
|
|
STATISTIC(NumGVNBlocks, "Number of blocks merged");
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
STATISTIC(NumGVNSimpl, "Number of instructions simplified");
|
|
|
|
STATISTIC(NumGVNEqProp, "Number of equalities propagated");
|
2008-12-22 22:14:07 +00:00
|
|
|
STATISTIC(NumPRELoad, "Number of loads PRE'd");
|
2008-03-22 04:13:49 +00:00
|
|
|
|
2008-06-20 01:01:07 +00:00
|
|
|
static cl::opt<bool> EnablePRE("enable-pre",
|
2008-07-17 19:41:00 +00:00
|
|
|
cl::init(true), cl::Hidden);
|
2009-06-15 18:30:15 +00:00
|
|
|
static cl::opt<bool> EnableLoadPRE("enable-load-pre", cl::init(true));
|
2008-06-19 19:57:25 +00:00
|
|
|
|
2012-04-27 18:09:28 +00:00
|
|
|
// Maximum allowed recursion depth.
|
2012-04-27 19:30:32 +00:00
|
|
|
static cl::opt<uint32_t>
|
2012-04-27 18:09:28 +00:00
|
|
|
MaxRecurseDepth("max-recurse-depth", cl::Hidden, cl::init(1000), cl::ZeroOrMore,
|
|
|
|
cl::desc("Max recurse depth (default = 1000)"));
|
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueTable Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// This class holds the mapping between values and value numbers. It is used
|
|
|
|
/// as an efficient mechanism to determine the expression-wise equivalence of
|
|
|
|
/// two values.
|
|
|
|
namespace {
|
2009-09-02 06:11:42 +00:00
|
|
|
struct Expression {
|
2011-01-03 19:00:11 +00:00
|
|
|
uint32_t opcode;
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *type;
|
2007-07-24 17:55:58 +00:00
|
|
|
SmallVector<uint32_t, 4> varargs;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2011-04-28 18:08:21 +00:00
|
|
|
Expression(uint32_t o = ~2U) : opcode(o) { }
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
bool operator==(const Expression &other) const {
|
|
|
|
if (opcode != other.opcode)
|
|
|
|
return false;
|
2011-04-28 18:08:21 +00:00
|
|
|
if (opcode == ~0U || opcode == ~1U)
|
2007-07-24 17:55:58 +00:00
|
|
|
return true;
|
2011-04-28 18:08:21 +00:00
|
|
|
if (type != other.type)
|
2007-07-24 17:55:58 +00:00
|
|
|
return false;
|
2011-04-28 18:08:21 +00:00
|
|
|
if (varargs != other.varargs)
|
2010-12-21 21:30:19 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
2007-07-24 17:55:58 +00:00
|
|
|
}
|
2012-03-05 11:29:54 +00:00
|
|
|
|
|
|
|
friend hash_code hash_value(const Expression &Value) {
|
|
|
|
return hash_combine(Value.opcode, Value.type,
|
|
|
|
hash_combine_range(Value.varargs.begin(),
|
|
|
|
Value.varargs.end()));
|
|
|
|
}
|
2007-07-24 17:55:58 +00:00
|
|
|
};
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-09-02 06:11:42 +00:00
|
|
|
class ValueTable {
|
2011-04-28 18:08:21 +00:00
|
|
|
DenseMap<Value*, uint32_t> valueNumbering;
|
|
|
|
DenseMap<Expression, uint32_t> expressionNumbering;
|
|
|
|
AliasAnalysis *AA;
|
|
|
|
MemoryDependenceAnalysis *MD;
|
|
|
|
DominatorTree *DT;
|
|
|
|
|
|
|
|
uint32_t nextValueNumber;
|
|
|
|
|
|
|
|
Expression create_expression(Instruction* I);
|
2012-02-27 08:14:30 +00:00
|
|
|
Expression create_cmp_expression(unsigned Opcode,
|
|
|
|
CmpInst::Predicate Predicate,
|
|
|
|
Value *LHS, Value *RHS);
|
2014-03-28 14:42:34 +00:00
|
|
|
Expression create_extractvalue_expression(ExtractValueInst* EI);
|
2011-04-28 18:08:21 +00:00
|
|
|
uint32_t lookup_or_add_call(CallInst* C);
|
|
|
|
public:
|
|
|
|
ValueTable() : nextValueNumber(1) { }
|
|
|
|
uint32_t lookup_or_add(Value *V);
|
|
|
|
uint32_t lookup(Value *V) const;
|
2012-02-27 08:14:30 +00:00
|
|
|
uint32_t lookup_or_add_cmp(unsigned Opcode, CmpInst::Predicate Pred,
|
|
|
|
Value *LHS, Value *RHS);
|
2011-04-28 18:08:21 +00:00
|
|
|
void add(Value *V, uint32_t num);
|
|
|
|
void clear();
|
|
|
|
void erase(Value *v);
|
|
|
|
void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
|
|
|
|
AliasAnalysis *getAliasAnalysis() const { return AA; }
|
|
|
|
void setMemDep(MemoryDependenceAnalysis* M) { MD = M; }
|
|
|
|
void setDomTree(DominatorTree* D) { DT = D; }
|
|
|
|
uint32_t getNextUnusedValueNumber() { return nextValueNumber; }
|
|
|
|
void verifyRemoved(const Value *) const;
|
2007-07-24 17:55:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace llvm {
|
2007-09-17 18:34:04 +00:00
|
|
|
template <> struct DenseMapInfo<Expression> {
|
2007-08-02 18:16:06 +00:00
|
|
|
static inline Expression getEmptyKey() {
|
2011-01-03 19:00:11 +00:00
|
|
|
return ~0U;
|
2007-08-02 18:16:06 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2007-08-02 18:16:06 +00:00
|
|
|
static inline Expression getTombstoneKey() {
|
2011-01-03 19:00:11 +00:00
|
|
|
return ~1U;
|
2007-08-02 18:16:06 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
static unsigned getHashValue(const Expression e) {
|
2012-03-05 11:29:54 +00:00
|
|
|
using llvm::hash_value;
|
|
|
|
return static_cast<unsigned>(hash_value(e));
|
2007-07-24 17:55:58 +00:00
|
|
|
}
|
2007-09-17 18:34:04 +00:00
|
|
|
static bool isEqual(const Expression &LHS, const Expression &RHS) {
|
|
|
|
return LHS == RHS;
|
|
|
|
}
|
2007-07-24 17:55:58 +00:00
|
|
|
};
|
2009-12-15 07:26:43 +00:00
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueTable Internal Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2011-01-03 19:00:11 +00:00
|
|
|
Expression ValueTable::create_expression(Instruction *I) {
|
2007-07-24 17:55:58 +00:00
|
|
|
Expression e;
|
|
|
|
e.type = I->getType();
|
2011-01-03 19:00:11 +00:00
|
|
|
e.opcode = I->getOpcode();
|
|
|
|
for (Instruction::op_iterator OI = I->op_begin(), OE = I->op_end();
|
|
|
|
OI != OE; ++OI)
|
|
|
|
e.varargs.push_back(lookup_or_add(*OI));
|
2012-02-24 15:16:31 +00:00
|
|
|
if (I->isCommutative()) {
|
|
|
|
// Ensure that commutative instructions that only differ by a permutation
|
|
|
|
// of their operands get the same value number by sorting the operand value
|
|
|
|
// numbers. Since all commutative instructions have two operands it is more
|
|
|
|
// efficient to sort by hand rather than using, say, std::sort.
|
|
|
|
assert(I->getNumOperands() == 2 && "Unsupported commutative instruction!");
|
|
|
|
if (e.varargs[0] > e.varargs[1])
|
|
|
|
std::swap(e.varargs[0], e.varargs[1]);
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-07-08 01:50:54 +00:00
|
|
|
if (CmpInst *C = dyn_cast<CmpInst>(I)) {
|
2012-02-24 15:16:31 +00:00
|
|
|
// Sort the operand value numbers so x<y and y>x get the same value number.
|
|
|
|
CmpInst::Predicate Predicate = C->getPredicate();
|
|
|
|
if (e.varargs[0] > e.varargs[1]) {
|
|
|
|
std::swap(e.varargs[0], e.varargs[1]);
|
|
|
|
Predicate = CmpInst::getSwappedPredicate(Predicate);
|
|
|
|
}
|
|
|
|
e.opcode = (C->getOpcode() << 8) | Predicate;
|
2011-01-03 19:00:11 +00:00
|
|
|
} else if (InsertValueInst *E = dyn_cast<InsertValueInst>(I)) {
|
|
|
|
for (InsertValueInst::idx_iterator II = E->idx_begin(), IE = E->idx_end();
|
|
|
|
II != IE; ++II)
|
|
|
|
e.varargs.push_back(*II);
|
2014-03-11 09:36:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2012-02-27 08:14:30 +00:00
|
|
|
Expression ValueTable::create_cmp_expression(unsigned Opcode,
|
|
|
|
CmpInst::Predicate Predicate,
|
|
|
|
Value *LHS, Value *RHS) {
|
|
|
|
assert((Opcode == Instruction::ICmp || Opcode == Instruction::FCmp) &&
|
|
|
|
"Not a comparison!");
|
|
|
|
Expression e;
|
|
|
|
e.type = CmpInst::makeCmpResultType(LHS->getType());
|
|
|
|
e.varargs.push_back(lookup_or_add(LHS));
|
|
|
|
e.varargs.push_back(lookup_or_add(RHS));
|
|
|
|
|
|
|
|
// Sort the operand value numbers so x<y and y>x get the same value number.
|
|
|
|
if (e.varargs[0] > e.varargs[1]) {
|
|
|
|
std::swap(e.varargs[0], e.varargs[1]);
|
|
|
|
Predicate = CmpInst::getSwappedPredicate(Predicate);
|
|
|
|
}
|
|
|
|
e.opcode = (Opcode << 8) | Predicate;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2014-03-28 14:42:34 +00:00
|
|
|
Expression ValueTable::create_extractvalue_expression(ExtractValueInst *EI) {
|
2014-04-28 04:05:08 +00:00
|
|
|
assert(EI && "Not an ExtractValueInst?");
|
2014-03-28 14:42:34 +00:00
|
|
|
Expression e;
|
|
|
|
e.type = EI->getType();
|
|
|
|
e.opcode = 0;
|
|
|
|
|
|
|
|
IntrinsicInst *I = dyn_cast<IntrinsicInst>(EI->getAggregateOperand());
|
2014-04-25 05:29:35 +00:00
|
|
|
if (I != nullptr && EI->getNumIndices() == 1 && *EI->idx_begin() == 0 ) {
|
2014-03-28 14:42:34 +00:00
|
|
|
// EI might be an extract from one of our recognised intrinsics. If it
|
|
|
|
// is we'll synthesize a semantically equivalent expression instead on
|
|
|
|
// an extract value expression.
|
|
|
|
switch (I->getIntrinsicID()) {
|
|
|
|
case Intrinsic::sadd_with_overflow:
|
|
|
|
case Intrinsic::uadd_with_overflow:
|
|
|
|
e.opcode = Instruction::Add;
|
|
|
|
break;
|
|
|
|
case Intrinsic::ssub_with_overflow:
|
|
|
|
case Intrinsic::usub_with_overflow:
|
|
|
|
e.opcode = Instruction::Sub;
|
|
|
|
break;
|
|
|
|
case Intrinsic::smul_with_overflow:
|
|
|
|
case Intrinsic::umul_with_overflow:
|
|
|
|
e.opcode = Instruction::Mul;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e.opcode != 0) {
|
|
|
|
// Intrinsic recognized. Grab its args to finish building the expression.
|
|
|
|
assert(I->getNumArgOperands() == 2 &&
|
|
|
|
"Expect two args for recognised intrinsics.");
|
|
|
|
e.varargs.push_back(lookup_or_add(I->getArgOperand(0)));
|
|
|
|
e.varargs.push_back(lookup_or_add(I->getArgOperand(1)));
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Not a recognised intrinsic. Fall back to producing an extract value
|
|
|
|
// expression.
|
|
|
|
e.opcode = EI->getOpcode();
|
|
|
|
for (Instruction::op_iterator OI = EI->op_begin(), OE = EI->op_end();
|
|
|
|
OI != OE; ++OI)
|
|
|
|
e.varargs.push_back(lookup_or_add(*OI));
|
|
|
|
|
|
|
|
for (ExtractValueInst::idx_iterator II = EI->idx_begin(), IE = EI->idx_end();
|
|
|
|
II != IE; ++II)
|
|
|
|
e.varargs.push_back(*II);
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// ValueTable External Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2008-06-18 21:41:49 +00:00
|
|
|
/// add - Insert a value into the table with a specified value number.
|
2009-09-21 02:42:51 +00:00
|
|
|
void ValueTable::add(Value *V, uint32_t num) {
|
2008-06-18 21:41:49 +00:00
|
|
|
valueNumbering.insert(std::make_pair(V, num));
|
|
|
|
}
|
|
|
|
|
2012-09-09 23:41:11 +00:00
|
|
|
uint32_t ValueTable::lookup_or_add_call(CallInst *C) {
|
2009-10-19 22:14:22 +00:00
|
|
|
if (AA->doesNotAccessMemory(C)) {
|
|
|
|
Expression exp = create_expression(C);
|
2012-09-09 23:41:11 +00:00
|
|
|
uint32_t &e = expressionNumbering[exp];
|
2009-10-19 22:14:22 +00:00
|
|
|
if (!e) e = nextValueNumber++;
|
|
|
|
valueNumbering[C] = e;
|
|
|
|
return e;
|
|
|
|
} else if (AA->onlyReadsMemory(C)) {
|
|
|
|
Expression exp = create_expression(C);
|
2012-09-09 23:41:11 +00:00
|
|
|
uint32_t &e = expressionNumbering[exp];
|
2009-10-19 22:14:22 +00:00
|
|
|
if (!e) {
|
|
|
|
e = nextValueNumber++;
|
|
|
|
valueNumbering[C] = e;
|
2009-10-19 21:14:57 +00:00
|
|
|
return e;
|
2009-10-19 22:14:22 +00:00
|
|
|
}
|
2009-11-14 02:27:51 +00:00
|
|
|
if (!MD) {
|
|
|
|
e = nextValueNumber++;
|
|
|
|
valueNumbering[C] = e;
|
|
|
|
return e;
|
|
|
|
}
|
2009-10-19 22:14:22 +00:00
|
|
|
|
|
|
|
MemDepResult local_dep = MD->getDependency(C);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-10-19 22:14:22 +00:00
|
|
|
if (!local_dep.isDef() && !local_dep.isNonLocal()) {
|
|
|
|
valueNumbering[C] = nextValueNumber;
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-10-19 22:14:22 +00:00
|
|
|
if (local_dep.isDef()) {
|
|
|
|
CallInst* local_cdep = cast<CallInst>(local_dep.getInst());
|
|
|
|
|
2010-06-30 09:17:53 +00:00
|
|
|
if (local_cdep->getNumArgOperands() != C->getNumArgOperands()) {
|
2009-10-19 22:14:22 +00:00
|
|
|
valueNumbering[C] = nextValueNumber;
|
2008-05-13 23:18:30 +00:00
|
|
|
return nextValueNumber++;
|
2008-11-30 23:39:23 +00:00
|
|
|
}
|
2008-12-05 21:04:20 +00:00
|
|
|
|
2010-06-24 10:17:17 +00:00
|
|
|
for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) {
|
|
|
|
uint32_t c_vn = lookup_or_add(C->getArgOperand(i));
|
|
|
|
uint32_t cd_vn = lookup_or_add(local_cdep->getArgOperand(i));
|
2009-10-19 22:14:22 +00:00
|
|
|
if (c_vn != cd_vn) {
|
|
|
|
valueNumbering[C] = nextValueNumber;
|
2008-05-13 23:18:30 +00:00
|
|
|
return nextValueNumber++;
|
2008-11-30 23:39:23 +00:00
|
|
|
}
|
2008-05-13 23:18:30 +00:00
|
|
|
}
|
2008-12-01 01:15:42 +00:00
|
|
|
|
2009-10-19 22:14:22 +00:00
|
|
|
uint32_t v = lookup_or_add(local_cdep);
|
|
|
|
valueNumbering[C] = v;
|
|
|
|
return v;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-10-19 22:14:22 +00:00
|
|
|
// Non-local case.
|
|
|
|
const MemoryDependenceAnalysis::NonLocalDepInfo &deps =
|
|
|
|
MD->getNonLocalCallDependency(CallSite(C));
|
2011-06-15 00:47:34 +00:00
|
|
|
// FIXME: Move the checking logic to MemDep!
|
2014-04-25 05:29:35 +00:00
|
|
|
CallInst* cdep = nullptr;
|
2009-10-19 22:14:22 +00:00
|
|
|
|
|
|
|
// Check to see if we have a single dominating call instruction that is
|
|
|
|
// identical to C.
|
|
|
|
for (unsigned i = 0, e = deps.size(); i != e; ++i) {
|
2009-12-09 07:08:01 +00:00
|
|
|
const NonLocalDepEntry *I = &deps[i];
|
|
|
|
if (I->getResult().isNonLocal())
|
2009-10-19 22:14:22 +00:00
|
|
|
continue;
|
|
|
|
|
2011-06-15 00:47:34 +00:00
|
|
|
// We don't handle non-definitions. If we already have a call, reject
|
2009-10-19 22:14:22 +00:00
|
|
|
// instruction dependencies.
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!I->getResult().isDef() || cdep != nullptr) {
|
|
|
|
cdep = nullptr;
|
2008-11-30 23:39:23 +00:00
|
|
|
break;
|
2008-05-13 08:17:22 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-12-09 07:08:01 +00:00
|
|
|
CallInst *NonLocalDepCall = dyn_cast<CallInst>(I->getResult().getInst());
|
2009-10-19 22:14:22 +00:00
|
|
|
// FIXME: All duplicated with non-local case.
|
2009-12-09 07:08:01 +00:00
|
|
|
if (NonLocalDepCall && DT->properlyDominates(I->getBB(), C->getParent())){
|
2009-10-19 22:14:22 +00:00
|
|
|
cdep = NonLocalDepCall;
|
|
|
|
continue;
|
2008-04-17 05:36:50 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
cdep = nullptr;
|
2009-10-19 22:14:22 +00:00
|
|
|
break;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-10-19 22:14:22 +00:00
|
|
|
if (!cdep) {
|
|
|
|
valueNumbering[C] = nextValueNumber;
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2010-06-30 09:17:53 +00:00
|
|
|
if (cdep->getNumArgOperands() != C->getNumArgOperands()) {
|
2009-10-19 22:14:22 +00:00
|
|
|
valueNumbering[C] = nextValueNumber;
|
2007-10-18 19:39:33 +00:00
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
2010-06-24 10:17:17 +00:00
|
|
|
for (unsigned i = 0, e = C->getNumArgOperands(); i < e; ++i) {
|
|
|
|
uint32_t c_vn = lookup_or_add(C->getArgOperand(i));
|
|
|
|
uint32_t cd_vn = lookup_or_add(cdep->getArgOperand(i));
|
2009-10-19 22:14:22 +00:00
|
|
|
if (c_vn != cd_vn) {
|
|
|
|
valueNumbering[C] = nextValueNumber;
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t v = lookup_or_add(cdep);
|
|
|
|
valueNumbering[C] = v;
|
|
|
|
return v;
|
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
} else {
|
2009-10-19 22:14:22 +00:00
|
|
|
valueNumbering[C] = nextValueNumber;
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// lookup_or_add - Returns the value number for the specified value, assigning
|
|
|
|
/// it a new number if it did not have one before.
|
|
|
|
uint32_t ValueTable::lookup_or_add(Value *V) {
|
|
|
|
DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
|
|
|
|
if (VI != valueNumbering.end())
|
|
|
|
return VI->second;
|
|
|
|
|
|
|
|
if (!isa<Instruction>(V)) {
|
2009-10-19 21:14:57 +00:00
|
|
|
valueNumbering[V] = nextValueNumber;
|
2007-07-24 17:55:58 +00:00
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-10-19 22:14:22 +00:00
|
|
|
Instruction* I = cast<Instruction>(V);
|
|
|
|
Expression exp;
|
|
|
|
switch (I->getOpcode()) {
|
2014-03-28 14:42:34 +00:00
|
|
|
case Instruction::Call:
|
|
|
|
return lookup_or_add_call(cast<CallInst>(I));
|
2009-10-19 22:14:22 +00:00
|
|
|
case Instruction::Add:
|
|
|
|
case Instruction::FAdd:
|
|
|
|
case Instruction::Sub:
|
|
|
|
case Instruction::FSub:
|
|
|
|
case Instruction::Mul:
|
|
|
|
case Instruction::FMul:
|
|
|
|
case Instruction::UDiv:
|
|
|
|
case Instruction::SDiv:
|
|
|
|
case Instruction::FDiv:
|
|
|
|
case Instruction::URem:
|
|
|
|
case Instruction::SRem:
|
|
|
|
case Instruction::FRem:
|
|
|
|
case Instruction::Shl:
|
|
|
|
case Instruction::LShr:
|
|
|
|
case Instruction::AShr:
|
|
|
|
case Instruction::And:
|
2012-09-09 23:41:11 +00:00
|
|
|
case Instruction::Or:
|
2009-10-19 22:14:22 +00:00
|
|
|
case Instruction::Xor:
|
|
|
|
case Instruction::ICmp:
|
|
|
|
case Instruction::FCmp:
|
|
|
|
case Instruction::Trunc:
|
|
|
|
case Instruction::ZExt:
|
|
|
|
case Instruction::SExt:
|
|
|
|
case Instruction::FPToUI:
|
|
|
|
case Instruction::FPToSI:
|
|
|
|
case Instruction::UIToFP:
|
|
|
|
case Instruction::SIToFP:
|
|
|
|
case Instruction::FPTrunc:
|
|
|
|
case Instruction::FPExt:
|
|
|
|
case Instruction::PtrToInt:
|
|
|
|
case Instruction::IntToPtr:
|
|
|
|
case Instruction::BitCast:
|
|
|
|
case Instruction::Select:
|
|
|
|
case Instruction::ExtractElement:
|
|
|
|
case Instruction::InsertElement:
|
|
|
|
case Instruction::ShuffleVector:
|
|
|
|
case Instruction::InsertValue:
|
|
|
|
case Instruction::GetElementPtr:
|
2014-03-11 09:36:48 +00:00
|
|
|
exp = create_expression(I);
|
2011-07-08 01:50:54 +00:00
|
|
|
break;
|
2014-03-28 14:42:34 +00:00
|
|
|
case Instruction::ExtractValue:
|
|
|
|
exp = create_extractvalue_expression(cast<ExtractValueInst>(I));
|
|
|
|
break;
|
2009-10-19 22:14:22 +00:00
|
|
|
default:
|
|
|
|
valueNumbering[V] = nextValueNumber;
|
|
|
|
return nextValueNumber++;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t& e = expressionNumbering[exp];
|
|
|
|
if (!e) e = nextValueNumber++;
|
|
|
|
valueNumbering[V] = e;
|
|
|
|
return e;
|
2007-07-24 17:55:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// lookup - Returns the value number of the specified value. Fails if
|
|
|
|
/// the value has not yet been numbered.
|
2009-09-21 02:42:51 +00:00
|
|
|
uint32_t ValueTable::lookup(Value *V) const {
|
2009-11-10 01:02:17 +00:00
|
|
|
DenseMap<Value*, uint32_t>::const_iterator VI = valueNumbering.find(V);
|
2008-03-21 21:14:38 +00:00
|
|
|
assert(VI != valueNumbering.end() && "Value not numbered?");
|
|
|
|
return VI->second;
|
2007-07-24 17:55:58 +00:00
|
|
|
}
|
|
|
|
|
2012-02-27 08:14:30 +00:00
|
|
|
/// lookup_or_add_cmp - Returns the value number of the given comparison,
|
|
|
|
/// assigning it a new number if it did not have one before. Useful when
|
|
|
|
/// we deduced the result of a comparison, but don't immediately have an
|
|
|
|
/// instruction realizing that comparison to hand.
|
|
|
|
uint32_t ValueTable::lookup_or_add_cmp(unsigned Opcode,
|
|
|
|
CmpInst::Predicate Predicate,
|
|
|
|
Value *LHS, Value *RHS) {
|
|
|
|
Expression exp = create_cmp_expression(Opcode, Predicate, LHS, RHS);
|
|
|
|
uint32_t& e = expressionNumbering[exp];
|
|
|
|
if (!e) e = nextValueNumber++;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2011-04-28 18:08:21 +00:00
|
|
|
/// clear - Remove all entries from the ValueTable.
|
2007-07-24 17:55:58 +00:00
|
|
|
void ValueTable::clear() {
|
|
|
|
valueNumbering.clear();
|
|
|
|
expressionNumbering.clear();
|
|
|
|
nextValueNumber = 1;
|
|
|
|
}
|
|
|
|
|
2011-04-28 18:08:21 +00:00
|
|
|
/// erase - Remove a value from the value numbering.
|
2009-09-21 02:42:51 +00:00
|
|
|
void ValueTable::erase(Value *V) {
|
2007-07-31 23:27:13 +00:00
|
|
|
valueNumbering.erase(V);
|
|
|
|
}
|
|
|
|
|
2008-12-22 21:36:08 +00:00
|
|
|
/// verifyRemoved - Verify that the value is removed from all internal data
|
|
|
|
/// structures.
|
|
|
|
void ValueTable::verifyRemoved(const Value *V) const {
|
2009-11-10 01:02:17 +00:00
|
|
|
for (DenseMap<Value*, uint32_t>::const_iterator
|
2008-12-22 21:36:08 +00:00
|
|
|
I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
|
|
|
|
assert(I->first != V && "Inst still occurs in value numbering map!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-12-22 22:32:22 +00:00
|
|
|
// GVN Pass
|
2007-07-24 17:55:58 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2008-06-12 19:25:32 +00:00
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
namespace {
|
2013-05-03 19:17:26 +00:00
|
|
|
class GVN;
|
|
|
|
struct AvailableValueInBlock {
|
|
|
|
/// BB - The basic block in question.
|
|
|
|
BasicBlock *BB;
|
|
|
|
enum ValType {
|
|
|
|
SimpleVal, // A simple offsetted value that is accessed.
|
|
|
|
LoadVal, // A value produced by a load.
|
2013-11-11 22:00:23 +00:00
|
|
|
MemIntrin, // A memory intrinsic which is loaded from.
|
|
|
|
UndefVal // A UndefValue representing a value from dead block (which
|
|
|
|
// is not yet physically removed from the CFG).
|
2013-05-03 19:17:26 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// V - The value that is live out of the block.
|
|
|
|
PointerIntPair<Value *, 2, ValType> Val;
|
|
|
|
|
|
|
|
/// Offset - The byte offset in Val that is interesting for the load query.
|
|
|
|
unsigned Offset;
|
|
|
|
|
|
|
|
static AvailableValueInBlock get(BasicBlock *BB, Value *V,
|
|
|
|
unsigned Offset = 0) {
|
|
|
|
AvailableValueInBlock Res;
|
|
|
|
Res.BB = BB;
|
|
|
|
Res.Val.setPointer(V);
|
|
|
|
Res.Val.setInt(SimpleVal);
|
|
|
|
Res.Offset = Offset;
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static AvailableValueInBlock getMI(BasicBlock *BB, MemIntrinsic *MI,
|
|
|
|
unsigned Offset = 0) {
|
|
|
|
AvailableValueInBlock Res;
|
|
|
|
Res.BB = BB;
|
|
|
|
Res.Val.setPointer(MI);
|
|
|
|
Res.Val.setInt(MemIntrin);
|
|
|
|
Res.Offset = Offset;
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static AvailableValueInBlock getLoad(BasicBlock *BB, LoadInst *LI,
|
|
|
|
unsigned Offset = 0) {
|
|
|
|
AvailableValueInBlock Res;
|
|
|
|
Res.BB = BB;
|
|
|
|
Res.Val.setPointer(LI);
|
|
|
|
Res.Val.setInt(LoadVal);
|
|
|
|
Res.Offset = Offset;
|
|
|
|
return Res;
|
|
|
|
}
|
2013-11-11 22:00:23 +00:00
|
|
|
|
|
|
|
static AvailableValueInBlock getUndef(BasicBlock *BB) {
|
|
|
|
AvailableValueInBlock Res;
|
|
|
|
Res.BB = BB;
|
2014-04-25 05:29:35 +00:00
|
|
|
Res.Val.setPointer(nullptr);
|
2013-11-11 22:00:23 +00:00
|
|
|
Res.Val.setInt(UndefVal);
|
|
|
|
Res.Offset = 0;
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2013-05-03 19:17:26 +00:00
|
|
|
bool isSimpleValue() const { return Val.getInt() == SimpleVal; }
|
|
|
|
bool isCoercedLoadValue() const { return Val.getInt() == LoadVal; }
|
|
|
|
bool isMemIntrinValue() const { return Val.getInt() == MemIntrin; }
|
2013-11-11 22:00:23 +00:00
|
|
|
bool isUndefValue() const { return Val.getInt() == UndefVal; }
|
2013-05-03 19:17:26 +00:00
|
|
|
|
|
|
|
Value *getSimpleValue() const {
|
|
|
|
assert(isSimpleValue() && "Wrong accessor");
|
|
|
|
return Val.getPointer();
|
|
|
|
}
|
|
|
|
|
|
|
|
LoadInst *getCoercedLoadValue() const {
|
|
|
|
assert(isCoercedLoadValue() && "Wrong accessor");
|
|
|
|
return cast<LoadInst>(Val.getPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
MemIntrinsic *getMemIntrinValue() const {
|
|
|
|
assert(isMemIntrinValue() && "Wrong accessor");
|
|
|
|
return cast<MemIntrinsic>(Val.getPointer());
|
|
|
|
}
|
|
|
|
|
|
|
|
/// MaterializeAdjustedValue - Emit code into this block to adjust the value
|
|
|
|
/// defined here to the specified type. This handles various coercion cases.
|
|
|
|
Value *MaterializeAdjustedValue(Type *LoadTy, GVN &gvn) const;
|
|
|
|
};
|
2007-07-24 17:55:58 +00:00
|
|
|
|
2009-09-02 06:11:42 +00:00
|
|
|
class GVN : public FunctionPass {
|
2009-11-14 02:27:51 +00:00
|
|
|
bool NoLoads;
|
2008-12-01 00:40:32 +00:00
|
|
|
MemoryDependenceAnalysis *MD;
|
|
|
|
DominatorTree *DT;
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout *DL;
|
2011-12-01 03:08:23 +00:00
|
|
|
const TargetLibraryInfo *TLI;
|
2015-01-04 12:03:27 +00:00
|
|
|
AssumptionCache *AC;
|
2013-11-11 22:00:23 +00:00
|
|
|
SetVector<BasicBlock *> DeadBlocks;
|
2011-12-01 03:08:23 +00:00
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
ValueTable VN;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-01-04 19:29:46 +00:00
|
|
|
/// LeaderTable - A mapping from value numbers to lists of Value*'s that
|
2011-01-04 19:13:25 +00:00
|
|
|
/// have that value number. Use findLeader to query it.
|
|
|
|
struct LeaderTableEntry {
|
2010-12-21 23:54:34 +00:00
|
|
|
Value *Val;
|
2012-08-10 15:55:25 +00:00
|
|
|
const BasicBlock *BB;
|
2011-01-04 19:13:25 +00:00
|
|
|
LeaderTableEntry *Next;
|
2010-12-21 23:54:34 +00:00
|
|
|
};
|
2011-01-04 19:29:46 +00:00
|
|
|
DenseMap<uint32_t, LeaderTableEntry> LeaderTable;
|
2010-11-18 18:32:40 +00:00
|
|
|
BumpPtrAllocator TableAllocator;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-28 16:18:52 +00:00
|
|
|
SmallVector<Instruction*, 8> InstrsToErase;
|
2013-05-03 19:17:26 +00:00
|
|
|
|
|
|
|
typedef SmallVector<NonLocalDepResult, 64> LoadDepVect;
|
|
|
|
typedef SmallVector<AvailableValueInBlock, 64> AvailValInBlkVect;
|
|
|
|
typedef SmallVector<BasicBlock*, 64> UnavailBlkVect;
|
|
|
|
|
2011-04-28 16:36:48 +00:00
|
|
|
public:
|
|
|
|
static char ID; // Pass identification, replacement for typeid
|
|
|
|
explicit GVN(bool noloads = false)
|
2014-04-25 05:29:35 +00:00
|
|
|
: FunctionPass(ID), NoLoads(noloads), MD(nullptr) {
|
2011-04-28 16:36:48 +00:00
|
|
|
initializeGVNPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2014-03-05 09:10:37 +00:00
|
|
|
bool runOnFunction(Function &F) override;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-28 16:36:48 +00:00
|
|
|
/// markInstructionForDeletion - This removes the specified instruction from
|
|
|
|
/// our various maps and marks it for deletion.
|
|
|
|
void markInstructionForDeletion(Instruction *I) {
|
|
|
|
VN.erase(I);
|
|
|
|
InstrsToErase.push_back(I);
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout *getDataLayout() const { return DL; }
|
2011-04-28 16:36:48 +00:00
|
|
|
DominatorTree &getDominatorTree() const { return *DT; }
|
|
|
|
AliasAnalysis *getAliasAnalysis() const { return VN.getAliasAnalysis(); }
|
2011-04-28 18:08:21 +00:00
|
|
|
MemoryDependenceAnalysis &getMemDep() const { return *MD; }
|
2011-04-28 16:36:48 +00:00
|
|
|
private:
|
2011-01-04 19:29:46 +00:00
|
|
|
/// addToLeaderTable - Push a new Value to the LeaderTable onto the list for
|
2010-11-19 22:48:40 +00:00
|
|
|
/// its value number.
|
2012-08-10 15:55:25 +00:00
|
|
|
void addToLeaderTable(uint32_t N, Value *V, const BasicBlock *BB) {
|
2011-04-28 18:15:47 +00:00
|
|
|
LeaderTableEntry &Curr = LeaderTable[N];
|
2010-12-21 23:54:34 +00:00
|
|
|
if (!Curr.Val) {
|
|
|
|
Curr.Val = V;
|
|
|
|
Curr.BB = BB;
|
2010-11-18 18:32:40 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-28 18:15:47 +00:00
|
|
|
LeaderTableEntry *Node = TableAllocator.Allocate<LeaderTableEntry>();
|
2010-12-21 23:54:34 +00:00
|
|
|
Node->Val = V;
|
|
|
|
Node->BB = BB;
|
|
|
|
Node->Next = Curr.Next;
|
|
|
|
Curr.Next = Node;
|
2010-11-18 18:32:40 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-01-04 19:29:46 +00:00
|
|
|
/// removeFromLeaderTable - Scan the list of values corresponding to a given
|
2012-05-22 14:17:53 +00:00
|
|
|
/// value number, and remove the given instruction if encountered.
|
|
|
|
void removeFromLeaderTable(uint32_t N, Instruction *I, BasicBlock *BB) {
|
2014-04-25 05:29:35 +00:00
|
|
|
LeaderTableEntry* Prev = nullptr;
|
2011-01-04 19:29:46 +00:00
|
|
|
LeaderTableEntry* Curr = &LeaderTable[N];
|
2010-11-18 18:32:40 +00:00
|
|
|
|
2012-05-22 14:17:53 +00:00
|
|
|
while (Curr->Val != I || Curr->BB != BB) {
|
2010-11-18 18:32:40 +00:00
|
|
|
Prev = Curr;
|
2010-12-21 23:54:34 +00:00
|
|
|
Curr = Curr->Next;
|
2010-11-18 18:32:40 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-11-18 18:32:40 +00:00
|
|
|
if (Prev) {
|
2010-12-21 23:54:34 +00:00
|
|
|
Prev->Next = Curr->Next;
|
2010-11-18 18:32:40 +00:00
|
|
|
} else {
|
2010-12-21 23:54:34 +00:00
|
|
|
if (!Curr->Next) {
|
2014-04-25 05:29:35 +00:00
|
|
|
Curr->Val = nullptr;
|
|
|
|
Curr->BB = nullptr;
|
2010-11-18 18:32:40 +00:00
|
|
|
} else {
|
2011-01-04 19:13:25 +00:00
|
|
|
LeaderTableEntry* Next = Curr->Next;
|
2010-12-21 23:54:34 +00:00
|
|
|
Curr->Val = Next->Val;
|
|
|
|
Curr->BB = Next->BB;
|
2011-01-04 19:10:54 +00:00
|
|
|
Curr->Next = Next->Next;
|
2010-11-18 18:32:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2010-02-16 19:51:59 +00:00
|
|
|
// List of critical edges to be split between iterations.
|
|
|
|
SmallVector<std::pair<TerminatorInst*, unsigned>, 4> toSplit;
|
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
// This transformation requires dominator postdominator info
|
2014-03-05 09:10:37 +00:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2015-01-04 12:03:27 +00:00
|
|
|
AU.addRequired<AssumptionCacheTracker>();
|
2014-01-13 13:07:17 +00:00
|
|
|
AU.addRequired<DominatorTreeWrapperPass>();
|
2011-12-01 03:08:23 +00:00
|
|
|
AU.addRequired<TargetLibraryInfo>();
|
2009-11-14 02:27:51 +00:00
|
|
|
if (!NoLoads)
|
|
|
|
AU.addRequired<MemoryDependenceAnalysis>();
|
2007-10-18 19:39:33 +00:00
|
|
|
AU.addRequired<AliasAnalysis>();
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2014-01-13 13:07:17 +00:00
|
|
|
AU.addPreserved<DominatorTreeWrapperPass>();
|
2007-10-18 19:39:33 +00:00
|
|
|
AU.addPreserved<AliasAnalysis>();
|
2007-07-24 17:55:58 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2013-05-03 19:17:26 +00:00
|
|
|
// Helper fuctions of redundant load elimination
|
2011-04-28 16:18:52 +00:00
|
|
|
bool processLoad(LoadInst *L);
|
|
|
|
bool processNonLocalLoad(LoadInst *L);
|
2013-05-03 19:17:26 +00:00
|
|
|
void AnalyzeLoadAvailability(LoadInst *LI, LoadDepVect &Deps,
|
|
|
|
AvailValInBlkVect &ValuesPerBlock,
|
|
|
|
UnavailBlkVect &UnavailableBlocks);
|
|
|
|
bool PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
|
|
|
|
UnavailBlkVect &UnavailableBlocks);
|
|
|
|
|
|
|
|
// Other helper routines
|
|
|
|
bool processInstruction(Instruction *I);
|
2009-09-21 02:42:51 +00:00
|
|
|
bool processBlock(BasicBlock *BB);
|
2011-04-28 16:18:52 +00:00
|
|
|
void dump(DenseMap<uint32_t, Value*> &d);
|
2007-08-14 18:04:11 +00:00
|
|
|
bool iterateOnFunction(Function &F);
|
2011-04-28 16:18:52 +00:00
|
|
|
bool performPRE(Function &F);
|
2015-01-09 19:19:56 +00:00
|
|
|
bool performScalarPRE(Instruction *I);
|
2012-08-10 15:55:25 +00:00
|
|
|
Value *findLeader(const BasicBlock *BB, uint32_t num);
|
2008-10-10 16:25:50 +00:00
|
|
|
void cleanupGlobalSets();
|
2008-12-22 21:36:08 +00:00
|
|
|
void verifyRemoved(const Instruction *I) const;
|
2010-02-16 19:51:59 +00:00
|
|
|
bool splitCriticalEdges();
|
2013-05-09 18:34:27 +00:00
|
|
|
BasicBlock *splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ);
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
unsigned replaceAllDominatedUsesWith(Value *From, Value *To,
|
2012-08-16 15:09:43 +00:00
|
|
|
const BasicBlockEdge &Root);
|
|
|
|
bool propagateEquality(Value *LHS, Value *RHS, const BasicBlockEdge &Root);
|
2013-11-11 22:00:23 +00:00
|
|
|
bool processFoldableCondBr(BranchInst *BI);
|
|
|
|
void addDeadBlock(BasicBlock *BB);
|
|
|
|
void assignValNumForDeadCode();
|
2007-07-24 17:55:58 +00:00
|
|
|
};
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
char GVN::ID = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// createGVNPass - The public interface to this file...
|
2010-02-28 05:34:05 +00:00
|
|
|
FunctionPass *llvm::createGVNPass(bool NoLoads) {
|
|
|
|
return new GVN(NoLoads);
|
2009-11-14 02:27:51 +00:00
|
|
|
}
|
2007-07-24 17:55:58 +00:00
|
|
|
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_BEGIN(GVN, "gvn", "Global Value Numbering", false, false)
|
2015-01-04 12:03:27 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(MemoryDependenceAnalysis)
|
2014-01-13 13:07:17 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass)
|
2011-12-01 03:08:23 +00:00
|
|
|
INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
|
2010-10-12 19:48:12 +00:00
|
|
|
INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
|
|
|
|
INITIALIZE_PASS_END(GVN, "gvn", "Global Value Numbering", false, false)
|
2007-07-24 17:55:58 +00:00
|
|
|
|
2012-09-12 05:06:18 +00:00
|
|
|
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
|
2008-06-18 21:41:49 +00:00
|
|
|
void GVN::dump(DenseMap<uint32_t, Value*>& d) {
|
2009-12-18 03:25:51 +00:00
|
|
|
errs() << "{\n";
|
2008-06-18 21:41:49 +00:00
|
|
|
for (DenseMap<uint32_t, Value*>::iterator I = d.begin(),
|
2007-07-25 19:57:03 +00:00
|
|
|
E = d.end(); I != E; ++I) {
|
2009-12-18 03:25:51 +00:00
|
|
|
errs() << I->first << "\n";
|
2007-07-25 19:57:03 +00:00
|
|
|
I->second->dump();
|
|
|
|
}
|
2009-12-18 03:25:51 +00:00
|
|
|
errs() << "}\n";
|
2007-07-25 19:57:03 +00:00
|
|
|
}
|
2012-09-06 19:55:56 +00:00
|
|
|
#endif
|
2007-07-25 19:57:03 +00:00
|
|
|
|
2008-12-02 08:16:11 +00:00
|
|
|
/// IsValueFullyAvailableInBlock - Return true if we can prove that the value
|
|
|
|
/// we're analyzing is fully available in the specified block. As we go, keep
|
2008-12-05 07:49:08 +00:00
|
|
|
/// track of which blocks we know are fully alive in FullyAvailableBlocks. This
|
|
|
|
/// map is actually a tri-state map with the following values:
|
|
|
|
/// 0) we know the block *is not* fully available.
|
|
|
|
/// 1) we know the block *is* fully available.
|
|
|
|
/// 2) we do not know whether the block is fully available or not, but we are
|
|
|
|
/// currently speculating that it will be.
|
|
|
|
/// 3) we are speculating for this block and have used that to speculate for
|
|
|
|
/// other blocks.
|
2009-09-20 02:20:51 +00:00
|
|
|
static bool IsValueFullyAvailableInBlock(BasicBlock *BB,
|
2012-04-27 18:09:28 +00:00
|
|
|
DenseMap<BasicBlock*, char> &FullyAvailableBlocks,
|
|
|
|
uint32_t RecurseDepth) {
|
|
|
|
if (RecurseDepth > MaxRecurseDepth)
|
|
|
|
return false;
|
|
|
|
|
2008-12-02 08:16:11 +00:00
|
|
|
// Optimistically assume that the block is fully available and check to see
|
|
|
|
// if we already know about this block in one lookup.
|
2009-09-20 02:20:51 +00:00
|
|
|
std::pair<DenseMap<BasicBlock*, char>::iterator, char> IV =
|
2008-12-05 07:49:08 +00:00
|
|
|
FullyAvailableBlocks.insert(std::make_pair(BB, 2));
|
2008-12-02 08:16:11 +00:00
|
|
|
|
|
|
|
// If the entry already existed for this block, return the precomputed value.
|
2008-12-05 07:49:08 +00:00
|
|
|
if (!IV.second) {
|
|
|
|
// If this is a speculative "available" value, mark it as being used for
|
|
|
|
// speculation of other blocks.
|
|
|
|
if (IV.first->second == 2)
|
|
|
|
IV.first->second = 3;
|
|
|
|
return IV.first->second != 0;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-02 08:16:11 +00:00
|
|
|
// Otherwise, see if it is fully available in all predecessors.
|
|
|
|
pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-02 08:16:11 +00:00
|
|
|
// If this block has no predecessors, it isn't live-in here.
|
|
|
|
if (PI == PE)
|
2008-12-05 07:49:08 +00:00
|
|
|
goto SpeculationFailure;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-02 08:16:11 +00:00
|
|
|
for (; PI != PE; ++PI)
|
|
|
|
// If the value isn't fully available in one of our predecessors, then it
|
|
|
|
// isn't fully available in this block either. Undo our previous
|
|
|
|
// optimistic assumption and bail out.
|
2012-04-27 18:09:28 +00:00
|
|
|
if (!IsValueFullyAvailableInBlock(*PI, FullyAvailableBlocks,RecurseDepth+1))
|
2008-12-05 07:49:08 +00:00
|
|
|
goto SpeculationFailure;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-02 08:16:11 +00:00
|
|
|
return true;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-05 07:49:08 +00:00
|
|
|
// SpeculationFailure - If we get here, we found out that this is not, after
|
|
|
|
// all, a fully-available block. We have a problem if we speculated on this and
|
|
|
|
// used the speculation to mark other blocks as available.
|
|
|
|
SpeculationFailure:
|
|
|
|
char &BBVal = FullyAvailableBlocks[BB];
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-05 07:49:08 +00:00
|
|
|
// If we didn't speculate on this, just return with it set to false.
|
|
|
|
if (BBVal == 2) {
|
|
|
|
BBVal = 0;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we did speculate on this value, we could have blocks set to 1 that are
|
|
|
|
// incorrect. Walk the (transitive) successors of this block and mark them as
|
|
|
|
// 0 if set to one.
|
|
|
|
SmallVector<BasicBlock*, 32> BBWorklist;
|
|
|
|
BBWorklist.push_back(BB);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2010-01-05 16:27:25 +00:00
|
|
|
do {
|
2008-12-05 07:49:08 +00:00
|
|
|
BasicBlock *Entry = BBWorklist.pop_back_val();
|
|
|
|
// Note that this sets blocks to 0 (unavailable) if they happen to not
|
|
|
|
// already be in FullyAvailableBlocks. This is safe.
|
|
|
|
char &EntryVal = FullyAvailableBlocks[Entry];
|
|
|
|
if (EntryVal == 0) continue; // Already unavailable.
|
|
|
|
|
|
|
|
// Mark as unavailable.
|
|
|
|
EntryVal = 0;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2014-02-10 14:17:42 +00:00
|
|
|
BBWorklist.append(succ_begin(Entry), succ_end(Entry));
|
2010-01-05 16:27:25 +00:00
|
|
|
} while (!BBWorklist.empty());
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-05 07:49:08 +00:00
|
|
|
return false;
|
2008-12-02 08:16:11 +00:00
|
|
|
}
|
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
|
2009-09-21 17:24:04 +00:00
|
|
|
/// CanCoerceMustAliasedValueToLoad - Return true if
|
|
|
|
/// CoerceAvailableValueToLoadType will succeed.
|
|
|
|
static bool CanCoerceMustAliasedValueToLoad(Value *StoredVal,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *LoadTy,
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout &DL) {
|
2009-09-21 17:24:04 +00:00
|
|
|
// If the loaded or stored value is an first class array or struct, don't try
|
|
|
|
// to transform them. We need to be able to bitcast to integer.
|
2010-02-16 11:11:14 +00:00
|
|
|
if (LoadTy->isStructTy() || LoadTy->isArrayTy() ||
|
|
|
|
StoredVal->getType()->isStructTy() ||
|
|
|
|
StoredVal->getType()->isArrayTy())
|
2009-09-21 17:24:04 +00:00
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 17:24:04 +00:00
|
|
|
// The store has to be at least as big as the load.
|
2014-02-21 00:06:31 +00:00
|
|
|
if (DL.getTypeSizeInBits(StoredVal->getType()) <
|
|
|
|
DL.getTypeSizeInBits(LoadTy))
|
2009-09-21 17:24:04 +00:00
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 17:24:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
/// CoerceAvailableValueToLoadType - If we saw a store of a value to memory, and
|
|
|
|
/// then a load from a must-aliased pointer of a different type, try to coerce
|
|
|
|
/// the stored value. LoadedTy is the type of the load we want to replace and
|
|
|
|
/// InsertPt is the place to insert new instructions.
|
|
|
|
///
|
|
|
|
/// If we can't do it, return null.
|
2012-07-24 10:51:42 +00:00
|
|
|
static Value *CoerceAvailableValueToLoadType(Value *StoredVal,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *LoadedTy,
|
2009-09-20 20:09:34 +00:00
|
|
|
Instruction *InsertPt,
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout &DL) {
|
|
|
|
if (!CanCoerceMustAliasedValueToLoad(StoredVal, LoadedTy, DL))
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-28 07:29:08 +00:00
|
|
|
// If this is already the right type, just return it.
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *StoredValTy = StoredVal->getType();
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-02-21 00:06:31 +00:00
|
|
|
uint64_t StoreSize = DL.getTypeSizeInBits(StoredValTy);
|
|
|
|
uint64_t LoadSize = DL.getTypeSizeInBits(LoadedTy);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
// If the store and reload are the same size, we can always reuse it.
|
|
|
|
if (StoreSize == LoadSize) {
|
2011-04-26 01:21:15 +00:00
|
|
|
// Pointer to Pointer -> use bitcast.
|
2012-10-24 21:22:30 +00:00
|
|
|
if (StoredValTy->getScalarType()->isPointerTy() &&
|
|
|
|
LoadedTy->getScalarType()->isPointerTy())
|
2009-09-20 20:09:34 +00:00
|
|
|
return new BitCastInst(StoredVal, LoadedTy, "", InsertPt);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
// Convert source pointers to integers, which can be bitcast.
|
2012-10-24 21:22:30 +00:00
|
|
|
if (StoredValTy->getScalarType()->isPointerTy()) {
|
2014-02-21 00:06:31 +00:00
|
|
|
StoredValTy = DL.getIntPtrType(StoredValTy);
|
2009-09-20 20:09:34 +00:00
|
|
|
StoredVal = new PtrToIntInst(StoredVal, StoredValTy, "", InsertPt);
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *TypeToCastTo = LoadedTy;
|
2012-10-24 21:22:30 +00:00
|
|
|
if (TypeToCastTo->getScalarType()->isPointerTy())
|
2014-02-21 00:06:31 +00:00
|
|
|
TypeToCastTo = DL.getIntPtrType(TypeToCastTo);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
if (StoredValTy != TypeToCastTo)
|
|
|
|
StoredVal = new BitCastInst(StoredVal, TypeToCastTo, "", InsertPt);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
// Cast to pointer if the load needs a pointer type.
|
2012-10-24 21:22:30 +00:00
|
|
|
if (LoadedTy->getScalarType()->isPointerTy())
|
2009-09-20 20:09:34 +00:00
|
|
|
StoredVal = new IntToPtrInst(StoredVal, LoadedTy, "", InsertPt);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
return StoredVal;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
// If the loaded value is smaller than the available value, then we can
|
|
|
|
// extract out a piece from it. If the available value is too small, then we
|
|
|
|
// can't do anything.
|
2009-09-21 17:24:04 +00:00
|
|
|
assert(StoreSize >= LoadSize && "CanCoerceMustAliasedValueToLoad fail");
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
// Convert source pointers to integers, which can be manipulated.
|
2012-10-24 21:22:30 +00:00
|
|
|
if (StoredValTy->getScalarType()->isPointerTy()) {
|
2014-02-21 00:06:31 +00:00
|
|
|
StoredValTy = DL.getIntPtrType(StoredValTy);
|
2009-09-20 20:09:34 +00:00
|
|
|
StoredVal = new PtrToIntInst(StoredVal, StoredValTy, "", InsertPt);
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
// Convert vectors and fp to integer, which can be manipulated.
|
2010-02-16 11:11:14 +00:00
|
|
|
if (!StoredValTy->isIntegerTy()) {
|
2009-09-20 20:09:34 +00:00
|
|
|
StoredValTy = IntegerType::get(StoredValTy->getContext(), StoreSize);
|
|
|
|
StoredVal = new BitCastInst(StoredVal, StoredValTy, "", InsertPt);
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
// If this is a big-endian system, we need to shift the value down to the low
|
|
|
|
// bits so that a truncate will work.
|
2014-02-21 00:06:31 +00:00
|
|
|
if (DL.isBigEndian()) {
|
2009-09-20 20:09:34 +00:00
|
|
|
Constant *Val = ConstantInt::get(StoredVal->getType(), StoreSize-LoadSize);
|
|
|
|
StoredVal = BinaryOperator::CreateLShr(StoredVal, Val, "tmp", InsertPt);
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
// Truncate the integer to the right size now.
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *NewIntTy = IntegerType::get(StoredValTy->getContext(), LoadSize);
|
2009-09-20 20:09:34 +00:00
|
|
|
StoredVal = new TruncInst(StoredVal, NewIntTy, "trunc", InsertPt);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
if (LoadedTy == NewIntTy)
|
|
|
|
return StoredVal;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
// If the result is a pointer, inttoptr.
|
2012-10-24 21:22:30 +00:00
|
|
|
if (LoadedTy->getScalarType()->isPointerTy())
|
2009-09-20 20:09:34 +00:00
|
|
|
return new IntToPtrInst(StoredVal, LoadedTy, "inttoptr", InsertPt);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
// Otherwise, bitcast.
|
|
|
|
return new BitCastInst(StoredVal, LoadedTy, "bitcast", InsertPt);
|
|
|
|
}
|
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
/// AnalyzeLoadFromClobberingWrite - This function is called when we have a
|
|
|
|
/// memdep query of a load that ends up being a clobbering memory write (store,
|
|
|
|
/// memset, memcpy, memmove). This means that the write *may* provide bits used
|
|
|
|
/// by the load but we can't be sure because the pointers don't mustalias.
|
|
|
|
///
|
|
|
|
/// Check this case to see if there is anything more we can do before we give
|
|
|
|
/// up. This returns -1 if we have to give up, or a byte number in the stored
|
|
|
|
/// value of the piece that feeds the load.
|
2011-07-18 04:54:35 +00:00
|
|
|
static int AnalyzeLoadFromClobberingWrite(Type *LoadTy, Value *LoadPtr,
|
2009-12-09 07:34:10 +00:00
|
|
|
Value *WritePtr,
|
2009-12-06 01:57:02 +00:00
|
|
|
uint64_t WriteSizeInBits,
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout &DL) {
|
2012-01-30 22:44:13 +00:00
|
|
|
// If the loaded or stored value is a first class array or struct, don't try
|
2009-09-21 17:24:04 +00:00
|
|
|
// to transform them. We need to be able to bitcast to integer.
|
2010-02-16 11:11:14 +00:00
|
|
|
if (LoadTy->isStructTy() || LoadTy->isArrayTy())
|
2009-09-21 17:24:04 +00:00
|
|
|
return -1;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:24:16 +00:00
|
|
|
int64_t StoreOffset = 0, LoadOffset = 0;
|
2014-02-21 00:06:31 +00:00
|
|
|
Value *StoreBase = GetPointerBaseWithConstantOffset(WritePtr,StoreOffset,&DL);
|
|
|
|
Value *LoadBase = GetPointerBaseWithConstantOffset(LoadPtr, LoadOffset, &DL);
|
2009-09-21 06:24:16 +00:00
|
|
|
if (StoreBase != LoadBase)
|
|
|
|
return -1;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:24:16 +00:00
|
|
|
// If the load and store are to the exact same address, they should have been
|
|
|
|
// a must alias. AA must have gotten confused.
|
2010-03-25 05:58:19 +00:00
|
|
|
// FIXME: Study to see if/when this happens. One case is forwarding a memset
|
|
|
|
// to a load from the base of the memset.
|
2009-09-21 06:24:16 +00:00
|
|
|
#if 0
|
2010-03-25 05:58:19 +00:00
|
|
|
if (LoadOffset == StoreOffset) {
|
2010-01-05 01:27:17 +00:00
|
|
|
dbgs() << "STORE/LOAD DEP WITH COMMON POINTER MISSED:\n"
|
2009-09-21 06:24:16 +00:00
|
|
|
<< "Base = " << *StoreBase << "\n"
|
2009-12-06 01:57:02 +00:00
|
|
|
<< "Store Ptr = " << *WritePtr << "\n"
|
|
|
|
<< "Store Offs = " << StoreOffset << "\n"
|
2009-12-10 00:04:46 +00:00
|
|
|
<< "Load Ptr = " << *LoadPtr << "\n";
|
2009-12-09 02:41:54 +00:00
|
|
|
abort();
|
2009-09-21 06:24:16 +00:00
|
|
|
}
|
2010-03-25 05:58:19 +00:00
|
|
|
#endif
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:24:16 +00:00
|
|
|
// If the load and store don't overlap at all, the store doesn't provide
|
|
|
|
// anything to the load. In this case, they really don't alias at all, AA
|
|
|
|
// must have gotten confused.
|
2014-02-21 00:06:31 +00:00
|
|
|
uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
if ((WriteSizeInBits & 7) | (LoadSize & 7))
|
2009-09-21 06:24:16 +00:00
|
|
|
return -1;
|
2009-12-06 01:57:02 +00:00
|
|
|
uint64_t StoreSize = WriteSizeInBits >> 3; // Convert to bytes.
|
2009-09-21 06:24:16 +00:00
|
|
|
LoadSize >>= 3;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
|
|
|
|
2009-09-21 06:24:16 +00:00
|
|
|
bool isAAFailure = false;
|
2010-03-25 05:58:19 +00:00
|
|
|
if (StoreOffset < LoadOffset)
|
2009-09-21 06:24:16 +00:00
|
|
|
isAAFailure = StoreOffset+int64_t(StoreSize) <= LoadOffset;
|
2010-03-25 05:58:19 +00:00
|
|
|
else
|
2009-09-21 06:24:16 +00:00
|
|
|
isAAFailure = LoadOffset+int64_t(LoadSize) <= StoreOffset;
|
2010-03-25 05:58:19 +00:00
|
|
|
|
2009-09-21 06:24:16 +00:00
|
|
|
if (isAAFailure) {
|
|
|
|
#if 0
|
2010-01-05 01:27:17 +00:00
|
|
|
dbgs() << "STORE LOAD DEP WITH COMMON BASE:\n"
|
2009-09-21 06:24:16 +00:00
|
|
|
<< "Base = " << *StoreBase << "\n"
|
2009-12-06 01:57:02 +00:00
|
|
|
<< "Store Ptr = " << *WritePtr << "\n"
|
|
|
|
<< "Store Offs = " << StoreOffset << "\n"
|
2009-12-10 00:04:46 +00:00
|
|
|
<< "Load Ptr = " << *LoadPtr << "\n";
|
2009-12-09 02:41:54 +00:00
|
|
|
abort();
|
2009-09-21 06:24:16 +00:00
|
|
|
#endif
|
|
|
|
return -1;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:24:16 +00:00
|
|
|
// If the Load isn't completely contained within the stored bits, we don't
|
|
|
|
// have all the bits to feed it. We could do something crazy in the future
|
|
|
|
// (issue a smaller load then merge the bits in) but this seems unlikely to be
|
|
|
|
// valuable.
|
|
|
|
if (StoreOffset > LoadOffset ||
|
|
|
|
StoreOffset+StoreSize < LoadOffset+LoadSize)
|
|
|
|
return -1;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:24:16 +00:00
|
|
|
// Okay, we can do this transformation. Return the number of bytes into the
|
|
|
|
// store that the load is.
|
|
|
|
return LoadOffset-StoreOffset;
|
2012-07-24 10:51:42 +00:00
|
|
|
}
|
2009-09-21 06:24:16 +00:00
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
/// AnalyzeLoadFromClobberingStore - This function is called when we have a
|
|
|
|
/// memdep query of a load that ends up being a clobbering store.
|
2011-07-18 04:54:35 +00:00
|
|
|
static int AnalyzeLoadFromClobberingStore(Type *LoadTy, Value *LoadPtr,
|
2009-12-09 07:37:07 +00:00
|
|
|
StoreInst *DepSI,
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout &DL) {
|
2009-12-06 01:57:02 +00:00
|
|
|
// Cannot handle reading from store of first-class aggregate yet.
|
2010-11-10 19:03:33 +00:00
|
|
|
if (DepSI->getValueOperand()->getType()->isStructTy() ||
|
|
|
|
DepSI->getValueOperand()->getType()->isArrayTy())
|
2009-12-06 01:57:02 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
Value *StorePtr = DepSI->getPointerOperand();
|
2014-02-21 00:06:31 +00:00
|
|
|
uint64_t StoreSize =DL.getTypeSizeInBits(DepSI->getValueOperand()->getType());
|
2009-12-09 07:37:07 +00:00
|
|
|
return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr,
|
2014-02-21 00:06:31 +00:00
|
|
|
StorePtr, StoreSize, DL);
|
2009-12-06 01:57:02 +00:00
|
|
|
}
|
|
|
|
|
2011-04-26 01:21:15 +00:00
|
|
|
/// AnalyzeLoadFromClobberingLoad - This function is called when we have a
|
|
|
|
/// memdep query of a load that ends up being clobbered by another load. See if
|
|
|
|
/// the other load can feed into the second load.
|
2011-07-18 04:54:35 +00:00
|
|
|
static int AnalyzeLoadFromClobberingLoad(Type *LoadTy, Value *LoadPtr,
|
2014-02-21 00:06:31 +00:00
|
|
|
LoadInst *DepLI, const DataLayout &DL){
|
2011-04-26 01:21:15 +00:00
|
|
|
// Cannot handle reading from store of first-class aggregate yet.
|
|
|
|
if (DepLI->getType()->isStructTy() || DepLI->getType()->isArrayTy())
|
|
|
|
return -1;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-26 01:21:15 +00:00
|
|
|
Value *DepPtr = DepLI->getPointerOperand();
|
2014-02-21 00:06:31 +00:00
|
|
|
uint64_t DepSize = DL.getTypeSizeInBits(DepLI->getType());
|
|
|
|
int R = AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, DepSize, DL);
|
2011-04-28 07:29:08 +00:00
|
|
|
if (R != -1) return R;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-28 07:29:08 +00:00
|
|
|
// If we have a load/load clobber an DepLI can be widened to cover this load,
|
|
|
|
// then we should widen it!
|
|
|
|
int64_t LoadOffs = 0;
|
|
|
|
const Value *LoadBase =
|
2014-02-21 00:06:31 +00:00
|
|
|
GetPointerBaseWithConstantOffset(LoadPtr, LoadOffs, &DL);
|
|
|
|
unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-28 07:29:08 +00:00
|
|
|
unsigned Size = MemoryDependenceAnalysis::
|
2014-02-21 00:06:31 +00:00
|
|
|
getLoadLoadClobberFullWidthSize(LoadBase, LoadOffs, LoadSize, DepLI, DL);
|
2011-04-28 07:29:08 +00:00
|
|
|
if (Size == 0) return -1;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-02-21 00:06:31 +00:00
|
|
|
return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, DepPtr, Size*8, DL);
|
2011-04-26 01:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
static int AnalyzeLoadFromClobberingMemInst(Type *LoadTy, Value *LoadPtr,
|
2009-12-09 07:37:07 +00:00
|
|
|
MemIntrinsic *MI,
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout &DL) {
|
2009-12-06 01:57:02 +00:00
|
|
|
// If the mem operation is a non-constant size, we can't handle it.
|
|
|
|
ConstantInt *SizeCst = dyn_cast<ConstantInt>(MI->getLength());
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!SizeCst) return -1;
|
2009-12-06 01:57:02 +00:00
|
|
|
uint64_t MemSizeInBits = SizeCst->getZExtValue()*8;
|
2009-12-06 05:29:56 +00:00
|
|
|
|
|
|
|
// If this is memset, we just need to see if the offset is valid in the size
|
|
|
|
// of the memset..
|
2009-12-06 01:57:02 +00:00
|
|
|
if (MI->getIntrinsicID() == Intrinsic::memset)
|
2009-12-09 07:37:07 +00:00
|
|
|
return AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr, MI->getDest(),
|
2014-02-21 00:06:31 +00:00
|
|
|
MemSizeInBits, DL);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 05:29:56 +00:00
|
|
|
// If we have a memcpy/memmove, the only case we can handle is if this is a
|
|
|
|
// copy from constant memory. In that case, we can read directly from the
|
|
|
|
// constant memory.
|
|
|
|
MemTransferInst *MTI = cast<MemTransferInst>(MI);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 05:29:56 +00:00
|
|
|
Constant *Src = dyn_cast<Constant>(MTI->getSource());
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!Src) return -1;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-02-21 00:06:31 +00:00
|
|
|
GlobalVariable *GV = dyn_cast<GlobalVariable>(GetUnderlyingObject(Src, &DL));
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!GV || !GV->isConstant()) return -1;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 05:29:56 +00:00
|
|
|
// See if the access is within the bounds of the transfer.
|
2009-12-09 07:37:07 +00:00
|
|
|
int Offset = AnalyzeLoadFromClobberingWrite(LoadTy, LoadPtr,
|
2014-02-21 00:06:31 +00:00
|
|
|
MI->getDest(), MemSizeInBits, DL);
|
2009-12-06 05:29:56 +00:00
|
|
|
if (Offset == -1)
|
|
|
|
return Offset;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2013-10-30 19:05:41 +00:00
|
|
|
unsigned AS = Src->getType()->getPointerAddressSpace();
|
2009-12-06 05:29:56 +00:00
|
|
|
// Otherwise, see if we can constant fold a load from the constant with the
|
|
|
|
// offset applied as appropriate.
|
|
|
|
Src = ConstantExpr::getBitCast(Src,
|
2013-10-30 19:05:41 +00:00
|
|
|
Type::getInt8PtrTy(Src->getContext(), AS));
|
2012-07-24 10:51:42 +00:00
|
|
|
Constant *OffsetCst =
|
2009-12-06 05:29:56 +00:00
|
|
|
ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
|
2011-07-21 14:31:17 +00:00
|
|
|
Src = ConstantExpr::getGetElementPtr(Src, OffsetCst);
|
2013-10-30 19:05:41 +00:00
|
|
|
Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
|
2014-02-21 00:06:31 +00:00
|
|
|
if (ConstantFoldLoadFromConstPtr(Src, &DL))
|
2009-12-06 05:29:56 +00:00
|
|
|
return Offset;
|
2009-12-06 01:57:02 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:24:16 +00:00
|
|
|
|
|
|
|
/// GetStoreValueForLoad - This function is called when we have a
|
|
|
|
/// memdep query of a load that ends up being a clobbering store. This means
|
2011-04-28 07:29:08 +00:00
|
|
|
/// that the store provides bits used by the load but we the pointers don't
|
|
|
|
/// mustalias. Check this case to see if there is anything more we can do
|
|
|
|
/// before we give up.
|
2009-09-21 06:48:08 +00:00
|
|
|
static Value *GetStoreValueForLoad(Value *SrcVal, unsigned Offset,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *LoadTy,
|
2014-02-21 00:06:31 +00:00
|
|
|
Instruction *InsertPt, const DataLayout &DL){
|
2009-09-21 06:24:16 +00:00
|
|
|
LLVMContext &Ctx = SrcVal->getType()->getContext();
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-02-21 00:06:31 +00:00
|
|
|
uint64_t StoreSize = (DL.getTypeSizeInBits(SrcVal->getType()) + 7) / 8;
|
|
|
|
uint64_t LoadSize = (DL.getTypeSizeInBits(LoadTy) + 7) / 8;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-09 18:13:28 +00:00
|
|
|
IRBuilder<> Builder(InsertPt->getParent(), InsertPt);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:24:16 +00:00
|
|
|
// Compute which bits of the stored value are being used by the load. Convert
|
|
|
|
// to an integer type to start with.
|
2012-10-24 21:22:30 +00:00
|
|
|
if (SrcVal->getType()->getScalarType()->isPointerTy())
|
2012-10-24 15:52:52 +00:00
|
|
|
SrcVal = Builder.CreatePtrToInt(SrcVal,
|
2014-02-21 00:06:31 +00:00
|
|
|
DL.getIntPtrType(SrcVal->getType()));
|
2010-02-16 11:11:14 +00:00
|
|
|
if (!SrcVal->getType()->isIntegerTy())
|
2011-09-27 20:39:19 +00:00
|
|
|
SrcVal = Builder.CreateBitCast(SrcVal, IntegerType::get(Ctx, StoreSize*8));
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:24:16 +00:00
|
|
|
// Shift the bits to the least significant depending on endianness.
|
|
|
|
unsigned ShiftAmt;
|
2014-02-21 00:06:31 +00:00
|
|
|
if (DL.isLittleEndian())
|
2009-09-21 06:24:16 +00:00
|
|
|
ShiftAmt = Offset*8;
|
2009-12-06 01:57:02 +00:00
|
|
|
else
|
2009-09-21 17:55:47 +00:00
|
|
|
ShiftAmt = (StoreSize-LoadSize-Offset)*8;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:48:08 +00:00
|
|
|
if (ShiftAmt)
|
2011-09-27 20:39:19 +00:00
|
|
|
SrcVal = Builder.CreateLShr(SrcVal, ShiftAmt);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:48:08 +00:00
|
|
|
if (LoadSize != StoreSize)
|
2011-09-27 20:39:19 +00:00
|
|
|
SrcVal = Builder.CreateTrunc(SrcVal, IntegerType::get(Ctx, LoadSize*8));
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-02-21 00:06:31 +00:00
|
|
|
return CoerceAvailableValueToLoadType(SrcVal, LoadTy, InsertPt, DL);
|
2009-09-21 06:24:16 +00:00
|
|
|
}
|
|
|
|
|
2012-01-30 21:13:22 +00:00
|
|
|
/// GetLoadValueForLoad - This function is called when we have a
|
2011-04-28 07:29:08 +00:00
|
|
|
/// memdep query of a load that ends up being a clobbering load. This means
|
|
|
|
/// that the load *may* provide bits used by the load but we can't be sure
|
|
|
|
/// because the pointers don't mustalias. Check this case to see if there is
|
|
|
|
/// anything more we can do before we give up.
|
|
|
|
static Value *GetLoadValueForLoad(LoadInst *SrcVal, unsigned Offset,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *LoadTy, Instruction *InsertPt,
|
2011-04-28 16:36:48 +00:00
|
|
|
GVN &gvn) {
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout &DL = *gvn.getDataLayout();
|
2011-04-28 07:29:08 +00:00
|
|
|
// If Offset+LoadTy exceeds the size of SrcVal, then we must be wanting to
|
|
|
|
// widen SrcVal out to a larger load.
|
2014-02-21 00:06:31 +00:00
|
|
|
unsigned SrcValSize = DL.getTypeStoreSize(SrcVal->getType());
|
|
|
|
unsigned LoadSize = DL.getTypeStoreSize(LoadTy);
|
2011-04-28 07:29:08 +00:00
|
|
|
if (Offset+LoadSize > SrcValSize) {
|
2011-08-17 22:22:24 +00:00
|
|
|
assert(SrcVal->isSimple() && "Cannot widen volatile/atomic load!");
|
|
|
|
assert(SrcVal->getType()->isIntegerTy() && "Can't widen non-integer load");
|
2011-04-28 07:29:08 +00:00
|
|
|
// If we have a load/load clobber an DepLI can be widened to cover this
|
|
|
|
// load, then we should widen it to the next power of 2 size big enough!
|
|
|
|
unsigned NewLoadSize = Offset+LoadSize;
|
|
|
|
if (!isPowerOf2_32(NewLoadSize))
|
|
|
|
NewLoadSize = NextPowerOf2(NewLoadSize);
|
|
|
|
|
|
|
|
Value *PtrVal = SrcVal->getPointerOperand();
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-28 18:15:47 +00:00
|
|
|
// Insert the new load after the old load. This ensures that subsequent
|
|
|
|
// memdep queries will find the new load. We can't easily remove the old
|
|
|
|
// load completely because it is already in the value numbering table.
|
|
|
|
IRBuilder<> Builder(SrcVal->getParent(), ++BasicBlock::iterator(SrcVal));
|
2012-07-24 10:51:42 +00:00
|
|
|
Type *DestPTy =
|
2011-04-28 07:29:08 +00:00
|
|
|
IntegerType::get(LoadTy->getContext(), NewLoadSize*8);
|
2012-07-24 10:51:42 +00:00
|
|
|
DestPTy = PointerType::get(DestPTy,
|
2013-10-21 19:43:56 +00:00
|
|
|
PtrVal->getType()->getPointerAddressSpace());
|
2011-05-04 23:58:50 +00:00
|
|
|
Builder.SetCurrentDebugLocation(SrcVal->getDebugLoc());
|
2011-04-28 07:29:08 +00:00
|
|
|
PtrVal = Builder.CreateBitCast(PtrVal, DestPTy);
|
|
|
|
LoadInst *NewLoad = Builder.CreateLoad(PtrVal);
|
|
|
|
NewLoad->takeName(SrcVal);
|
|
|
|
NewLoad->setAlignment(SrcVal->getAlignment());
|
2011-05-04 23:58:50 +00:00
|
|
|
|
2011-04-28 07:29:08 +00:00
|
|
|
DEBUG(dbgs() << "GVN WIDENED LOAD: " << *SrcVal << "\n");
|
|
|
|
DEBUG(dbgs() << "TO: " << *NewLoad << "\n");
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-28 07:29:08 +00:00
|
|
|
// Replace uses of the original load with the wider load. On a big endian
|
|
|
|
// system, we need to shift down to get the relevant bits.
|
|
|
|
Value *RV = NewLoad;
|
2014-02-21 00:06:31 +00:00
|
|
|
if (DL.isBigEndian())
|
2011-04-28 07:29:08 +00:00
|
|
|
RV = Builder.CreateLShr(RV,
|
|
|
|
NewLoadSize*8-SrcVal->getType()->getPrimitiveSizeInBits());
|
|
|
|
RV = Builder.CreateTrunc(RV, SrcVal->getType());
|
|
|
|
SrcVal->replaceAllUsesWith(RV);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-28 20:02:57 +00:00
|
|
|
// We would like to use gvn.markInstructionForDeletion here, but we can't
|
|
|
|
// because the load is already memoized into the leader map table that GVN
|
|
|
|
// tracks. It is potentially possible to remove the load from the table,
|
|
|
|
// but then there all of the operations based on it would need to be
|
|
|
|
// rehashed. Just leave the dead load around.
|
2011-04-28 18:08:21 +00:00
|
|
|
gvn.getMemDep().removeInstruction(SrcVal);
|
2011-04-28 07:29:08 +00:00
|
|
|
SrcVal = NewLoad;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-02-21 00:06:31 +00:00
|
|
|
return GetStoreValueForLoad(SrcVal, Offset, LoadTy, InsertPt, DL);
|
2011-04-28 07:29:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
/// GetMemInstValueForLoad - This function is called when we have a
|
|
|
|
/// memdep query of a load that ends up being a clobbering mem intrinsic.
|
|
|
|
static Value *GetMemInstValueForLoad(MemIntrinsic *SrcInst, unsigned Offset,
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *LoadTy, Instruction *InsertPt,
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout &DL){
|
2009-12-06 01:57:02 +00:00
|
|
|
LLVMContext &Ctx = LoadTy->getContext();
|
2014-02-21 00:06:31 +00:00
|
|
|
uint64_t LoadSize = DL.getTypeSizeInBits(LoadTy)/8;
|
2009-12-06 01:57:02 +00:00
|
|
|
|
|
|
|
IRBuilder<> Builder(InsertPt->getParent(), InsertPt);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
// We know that this method is only called when the mem transfer fully
|
|
|
|
// provides the bits for the load.
|
|
|
|
if (MemSetInst *MSI = dyn_cast<MemSetInst>(SrcInst)) {
|
|
|
|
// memset(P, 'x', 1234) -> splat('x'), even if x is a variable, and
|
|
|
|
// independently of what the offset is.
|
|
|
|
Value *Val = MSI->getValue();
|
|
|
|
if (LoadSize != 1)
|
|
|
|
Val = Builder.CreateZExt(Val, IntegerType::get(Ctx, LoadSize*8));
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
Value *OneElt = Val;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
// Splat the value out to the right number of bits.
|
|
|
|
for (unsigned NumBytesSet = 1; NumBytesSet != LoadSize; ) {
|
|
|
|
// If we can double the number of bytes set, do it.
|
|
|
|
if (NumBytesSet*2 <= LoadSize) {
|
|
|
|
Value *ShVal = Builder.CreateShl(Val, NumBytesSet*8);
|
|
|
|
Val = Builder.CreateOr(Val, ShVal);
|
|
|
|
NumBytesSet <<= 1;
|
|
|
|
continue;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
// Otherwise insert one byte at a time.
|
|
|
|
Value *ShVal = Builder.CreateShl(Val, 1*8);
|
|
|
|
Val = Builder.CreateOr(OneElt, ShVal);
|
|
|
|
++NumBytesSet;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-02-21 00:06:31 +00:00
|
|
|
return CoerceAvailableValueToLoadType(Val, LoadTy, InsertPt, DL);
|
2009-12-06 01:57:02 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 05:29:56 +00:00
|
|
|
// Otherwise, this is a memcpy/memmove from a constant global.
|
|
|
|
MemTransferInst *MTI = cast<MemTransferInst>(SrcInst);
|
|
|
|
Constant *Src = cast<Constant>(MTI->getSource());
|
2013-10-30 19:05:41 +00:00
|
|
|
unsigned AS = Src->getType()->getPointerAddressSpace();
|
2009-12-06 05:29:56 +00:00
|
|
|
|
|
|
|
// Otherwise, see if we can constant fold a load from the constant with the
|
|
|
|
// offset applied as appropriate.
|
|
|
|
Src = ConstantExpr::getBitCast(Src,
|
2013-10-30 19:05:41 +00:00
|
|
|
Type::getInt8PtrTy(Src->getContext(), AS));
|
2012-07-24 10:51:42 +00:00
|
|
|
Constant *OffsetCst =
|
2013-10-30 19:05:41 +00:00
|
|
|
ConstantInt::get(Type::getInt64Ty(Src->getContext()), (unsigned)Offset);
|
2011-07-21 14:31:17 +00:00
|
|
|
Src = ConstantExpr::getGetElementPtr(Src, OffsetCst);
|
2013-10-30 19:05:41 +00:00
|
|
|
Src = ConstantExpr::getBitCast(Src, PointerType::get(LoadTy, AS));
|
2014-02-21 00:06:31 +00:00
|
|
|
return ConstantFoldLoadFromConstPtr(Src, &DL);
|
2009-12-06 01:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 17:08:50 +00:00
|
|
|
|
2009-10-10 23:50:30 +00:00
|
|
|
/// ConstructSSAForLoadSet - Given a set of loads specified by ValuesPerBlock,
|
|
|
|
/// construct SSA form, allowing us to eliminate LI. This returns the value
|
|
|
|
/// that should be used at LI's definition site.
|
2012-07-24 10:51:42 +00:00
|
|
|
static Value *ConstructSSAForLoadSet(LoadInst *LI,
|
2009-10-10 23:50:30 +00:00
|
|
|
SmallVectorImpl<AvailableValueInBlock> &ValuesPerBlock,
|
2011-04-28 16:36:48 +00:00
|
|
|
GVN &gvn) {
|
2009-12-21 23:15:48 +00:00
|
|
|
// Check for the fully redundant, dominating load case. In this case, we can
|
|
|
|
// just use the dominating value directly.
|
2012-07-24 10:51:42 +00:00
|
|
|
if (ValuesPerBlock.size() == 1 &&
|
2011-04-28 16:36:48 +00:00
|
|
|
gvn.getDominatorTree().properlyDominates(ValuesPerBlock[0].BB,
|
2013-11-11 22:00:23 +00:00
|
|
|
LI->getParent())) {
|
|
|
|
assert(!ValuesPerBlock[0].isUndefValue() && "Dead BB dominate this block");
|
2011-04-28 16:36:48 +00:00
|
|
|
return ValuesPerBlock[0].MaterializeAdjustedValue(LI->getType(), gvn);
|
2013-11-11 22:00:23 +00:00
|
|
|
}
|
2009-12-21 23:15:48 +00:00
|
|
|
|
|
|
|
// Otherwise, we have to construct SSA form.
|
2009-10-10 23:50:30 +00:00
|
|
|
SmallVector<PHINode*, 8> NewPHIs;
|
|
|
|
SSAUpdater SSAUpdate(&NewPHIs);
|
2010-09-02 08:14:03 +00:00
|
|
|
SSAUpdate.Initialize(LI->getType(), LI->getName());
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-07-18 04:54:35 +00:00
|
|
|
Type *LoadTy = LI->getType();
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 20:09:34 +00:00
|
|
|
for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i) {
|
2009-12-06 04:54:31 +00:00
|
|
|
const AvailableValueInBlock &AV = ValuesPerBlock[i];
|
|
|
|
BasicBlock *BB = AV.BB;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-10-10 23:50:30 +00:00
|
|
|
if (SSAUpdate.HasValueForBlock(BB))
|
|
|
|
continue;
|
2009-12-06 04:54:31 +00:00
|
|
|
|
2011-04-28 16:36:48 +00:00
|
|
|
SSAUpdate.AddAvailableValue(BB, AV.MaterializeAdjustedValue(LoadTy, gvn));
|
2009-09-20 20:09:34 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-10-10 23:50:30 +00:00
|
|
|
// Perform PHI construction.
|
|
|
|
Value *V = SSAUpdate.GetValueInMiddleOfBlock(LI->getParent());
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-10-10 23:50:30 +00:00
|
|
|
// If new PHI nodes were created, notify alias analysis.
|
2012-10-24 21:22:30 +00:00
|
|
|
if (V->getType()->getScalarType()->isPointerTy()) {
|
2011-04-28 16:36:48 +00:00
|
|
|
AliasAnalysis *AA = gvn.getAliasAnalysis();
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-10-10 23:50:30 +00:00
|
|
|
for (unsigned i = 0, e = NewPHIs.size(); i != e; ++i)
|
|
|
|
AA->copyValue(LI, NewPHIs[i]);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-01-03 23:51:43 +00:00
|
|
|
// Now that we've copied information to the new PHIs, scan through
|
|
|
|
// them again and inform alias analysis that we've added potentially
|
|
|
|
// escaping uses to any values that are operands to these PHIs.
|
|
|
|
for (unsigned i = 0, e = NewPHIs.size(); i != e; ++i) {
|
|
|
|
PHINode *P = NewPHIs[i];
|
2011-06-20 14:18:48 +00:00
|
|
|
for (unsigned ii = 0, ee = P->getNumIncomingValues(); ii != ee; ++ii) {
|
|
|
|
unsigned jj = PHINode::getOperandNumForIncomingValue(ii);
|
|
|
|
AA->addEscapingUse(P->getOperandUse(jj));
|
|
|
|
}
|
2011-01-03 23:51:43 +00:00
|
|
|
}
|
2011-04-28 16:36:48 +00:00
|
|
|
}
|
2009-10-10 23:50:30 +00:00
|
|
|
|
|
|
|
return V;
|
2009-09-20 20:09:34 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 19:17:26 +00:00
|
|
|
Value *AvailableValueInBlock::MaterializeAdjustedValue(Type *LoadTy, GVN &gvn) const {
|
|
|
|
Value *Res;
|
|
|
|
if (isSimpleValue()) {
|
|
|
|
Res = getSimpleValue();
|
|
|
|
if (Res->getType() != LoadTy) {
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout *DL = gvn.getDataLayout();
|
|
|
|
assert(DL && "Need target data to handle type mismatch case");
|
2013-05-03 19:17:26 +00:00
|
|
|
Res = GetStoreValueForLoad(Res, Offset, LoadTy, BB->getTerminator(),
|
2014-02-21 00:06:31 +00:00
|
|
|
*DL);
|
2013-05-03 19:17:26 +00:00
|
|
|
|
|
|
|
DEBUG(dbgs() << "GVN COERCED NONLOCAL VAL:\nOffset: " << Offset << " "
|
|
|
|
<< *getSimpleValue() << '\n'
|
|
|
|
<< *Res << '\n' << "\n\n\n");
|
|
|
|
}
|
|
|
|
} else if (isCoercedLoadValue()) {
|
|
|
|
LoadInst *Load = getCoercedLoadValue();
|
|
|
|
if (Load->getType() == LoadTy && Offset == 0) {
|
|
|
|
Res = Load;
|
|
|
|
} else {
|
|
|
|
Res = GetLoadValueForLoad(Load, Offset, LoadTy, BB->getTerminator(),
|
|
|
|
gvn);
|
|
|
|
|
|
|
|
DEBUG(dbgs() << "GVN COERCED NONLOCAL LOAD:\nOffset: " << Offset << " "
|
|
|
|
<< *getCoercedLoadValue() << '\n'
|
|
|
|
<< *Res << '\n' << "\n\n\n");
|
|
|
|
}
|
2013-11-11 22:00:23 +00:00
|
|
|
} else if (isMemIntrinValue()) {
|
2014-02-21 00:06:31 +00:00
|
|
|
const DataLayout *DL = gvn.getDataLayout();
|
|
|
|
assert(DL && "Need target data to handle type mismatch case");
|
2013-05-03 19:17:26 +00:00
|
|
|
Res = GetMemInstValueForLoad(getMemIntrinValue(), Offset,
|
2014-02-21 00:06:31 +00:00
|
|
|
LoadTy, BB->getTerminator(), *DL);
|
2013-05-03 19:17:26 +00:00
|
|
|
DEBUG(dbgs() << "GVN COERCED NONLOCAL MEM INTRIN:\nOffset: " << Offset
|
|
|
|
<< " " << *getMemIntrinValue() << '\n'
|
|
|
|
<< *Res << '\n' << "\n\n\n");
|
2013-11-11 22:00:23 +00:00
|
|
|
} else {
|
|
|
|
assert(isUndefValue() && "Should be UndefVal");
|
|
|
|
DEBUG(dbgs() << "GVN COERCED NONLOCAL Undef:\n";);
|
|
|
|
return UndefValue::get(LoadTy);
|
2013-05-03 19:17:26 +00:00
|
|
|
}
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
2010-04-09 10:57:00 +00:00
|
|
|
static bool isLifetimeStart(const Instruction *Inst) {
|
|
|
|
if (const IntrinsicInst* II = dyn_cast<IntrinsicInst>(Inst))
|
2009-12-02 07:35:19 +00:00
|
|
|
return II->getIntrinsicID() == Intrinsic::lifetime_start;
|
2009-12-02 06:44:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-03 19:17:26 +00:00
|
|
|
void GVN::AnalyzeLoadAvailability(LoadInst *LI, LoadDepVect &Deps,
|
|
|
|
AvailValInBlkVect &ValuesPerBlock,
|
|
|
|
UnavailBlkVect &UnavailableBlocks) {
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-02 08:16:11 +00:00
|
|
|
// Filter out useless results (non-locals, etc). Keep track of the blocks
|
|
|
|
// where we have a value available in repl, also keep track of whether we see
|
|
|
|
// dependencies that produce an unknown value for the load (such as a call
|
|
|
|
// that could potentially clobber the load).
|
2013-05-03 19:17:26 +00:00
|
|
|
unsigned NumDeps = Deps.size();
|
2012-01-31 06:57:53 +00:00
|
|
|
for (unsigned i = 0, e = NumDeps; i != e; ++i) {
|
2009-12-09 07:08:01 +00:00
|
|
|
BasicBlock *DepBB = Deps[i].getBB();
|
|
|
|
MemDepResult DepInfo = Deps[i].getResult();
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2013-11-11 22:00:23 +00:00
|
|
|
if (DeadBlocks.count(DepBB)) {
|
|
|
|
// Dead dependent mem-op disguise as a load evaluating the same value
|
|
|
|
// as the load in question.
|
|
|
|
ValuesPerBlock.push_back(AvailableValueInBlock::getUndef(DepBB));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-10-13 22:14:57 +00:00
|
|
|
if (!DepInfo.isDef() && !DepInfo.isClobber()) {
|
2011-06-15 00:47:34 +00:00
|
|
|
UnavailableBlocks.push_back(DepBB);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-12-05 21:04:20 +00:00
|
|
|
if (DepInfo.isClobber()) {
|
2009-12-09 18:21:46 +00:00
|
|
|
// The address being loaded in this non-local block may not be the same as
|
|
|
|
// the pointer operand of the load if PHI translation occurs. Make sure
|
|
|
|
// to consider the right address.
|
|
|
|
Value *Address = Deps[i].getAddress();
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:48:08 +00:00
|
|
|
// If the dependence is to a store that writes to a superset of the bits
|
|
|
|
// read by the load, we can extract the bits we need for the load from the
|
|
|
|
// stored value.
|
|
|
|
if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInfo.getInst())) {
|
2014-02-21 00:06:31 +00:00
|
|
|
if (DL && Address) {
|
2009-12-09 18:21:46 +00:00
|
|
|
int Offset = AnalyzeLoadFromClobberingStore(LI->getType(), Address,
|
2014-02-21 00:06:31 +00:00
|
|
|
DepSI, *DL);
|
2009-09-21 06:48:08 +00:00
|
|
|
if (Offset != -1) {
|
|
|
|
ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB,
|
2010-11-10 19:03:33 +00:00
|
|
|
DepSI->getValueOperand(),
|
2009-09-21 06:48:08 +00:00
|
|
|
Offset));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-26 01:21:15 +00:00
|
|
|
// Check to see if we have something like this:
|
|
|
|
// load i32* P
|
|
|
|
// load i8* (P+1)
|
|
|
|
// if we have this, replace the later with an extraction from the former.
|
|
|
|
if (LoadInst *DepLI = dyn_cast<LoadInst>(DepInfo.getInst())) {
|
|
|
|
// If this is a clobber and L is the first instruction in its block, then
|
|
|
|
// we have the first instruction in the entry block.
|
2014-02-21 00:06:31 +00:00
|
|
|
if (DepLI != LI && Address && DL) {
|
2014-05-02 17:59:17 +00:00
|
|
|
int Offset = AnalyzeLoadFromClobberingLoad(LI->getType(), Address,
|
2014-02-21 00:06:31 +00:00
|
|
|
DepLI, *DL);
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-26 01:21:15 +00:00
|
|
|
if (Offset != -1) {
|
2011-04-28 07:29:08 +00:00
|
|
|
ValuesPerBlock.push_back(AvailableValueInBlock::getLoad(DepBB,DepLI,
|
|
|
|
Offset));
|
2011-04-26 01:21:15 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-12-06 01:57:02 +00:00
|
|
|
|
|
|
|
// If the clobbering value is a memset/memcpy/memmove, see if we can
|
|
|
|
// forward a value on from it.
|
2009-12-06 04:54:31 +00:00
|
|
|
if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(DepInfo.getInst())) {
|
2014-02-21 00:06:31 +00:00
|
|
|
if (DL && Address) {
|
2009-12-09 18:21:46 +00:00
|
|
|
int Offset = AnalyzeLoadFromClobberingMemInst(LI->getType(), Address,
|
2014-02-21 00:06:31 +00:00
|
|
|
DepMI, *DL);
|
2009-12-06 04:54:31 +00:00
|
|
|
if (Offset != -1) {
|
|
|
|
ValuesPerBlock.push_back(AvailableValueInBlock::getMI(DepBB, DepMI,
|
|
|
|
Offset));
|
|
|
|
continue;
|
2012-07-24 10:51:42 +00:00
|
|
|
}
|
2009-12-06 01:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2008-12-05 21:04:20 +00:00
|
|
|
UnavailableBlocks.push_back(DepBB);
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2011-10-13 22:14:57 +00:00
|
|
|
// DepInfo.isDef() here
|
2011-06-15 00:47:34 +00:00
|
|
|
|
2008-12-05 21:04:20 +00:00
|
|
|
Instruction *DepInst = DepInfo.getInst();
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-05 21:04:20 +00:00
|
|
|
// Loading the allocation -> undef.
|
2012-08-29 15:32:21 +00:00
|
|
|
if (isa<AllocaInst>(DepInst) || isMallocLikeFn(DepInst, TLI) ||
|
2009-12-02 07:35:19 +00:00
|
|
|
// Loading immediately after lifetime begin -> undef.
|
|
|
|
isLifetimeStart(DepInst)) {
|
2009-09-21 06:30:24 +00:00
|
|
|
ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB,
|
|
|
|
UndefValue::get(LI->getType())));
|
2008-12-01 01:15:42 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-06-12 21:16:19 +00:00
|
|
|
// Loading from calloc (which zero initializes memory) -> zero
|
|
|
|
if (isCallocLikeFn(DepInst, TLI)) {
|
|
|
|
ValuesPerBlock.push_back(AvailableValueInBlock::get(
|
|
|
|
DepBB, Constant::getNullValue(LI->getType())));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-09-21 06:30:24 +00:00
|
|
|
if (StoreInst *S = dyn_cast<StoreInst>(DepInst)) {
|
2009-09-20 02:20:51 +00:00
|
|
|
// Reject loads and stores that are to the same address but are of
|
2009-09-20 20:09:34 +00:00
|
|
|
// different types if we have to.
|
2010-11-10 19:03:33 +00:00
|
|
|
if (S->getValueOperand()->getType() != LI->getType()) {
|
2009-09-20 20:09:34 +00:00
|
|
|
// If the stored value is larger or equal to the loaded value, we can
|
|
|
|
// reuse it.
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!DL || !CanCoerceMustAliasedValueToLoad(S->getValueOperand(),
|
|
|
|
LI->getType(), *DL)) {
|
2009-09-20 20:09:34 +00:00
|
|
|
UnavailableBlocks.push_back(DepBB);
|
|
|
|
continue;
|
|
|
|
}
|
2008-12-02 08:16:11 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-09-21 06:30:24 +00:00
|
|
|
ValuesPerBlock.push_back(AvailableValueInBlock::get(DepBB,
|
2010-11-10 19:03:33 +00:00
|
|
|
S->getValueOperand()));
|
2009-09-21 06:48:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:48:08 +00:00
|
|
|
if (LoadInst *LD = dyn_cast<LoadInst>(DepInst)) {
|
2009-09-20 20:09:34 +00:00
|
|
|
// If the types mismatch and we can't handle it, reject reuse of the load.
|
2008-12-02 08:16:11 +00:00
|
|
|
if (LD->getType() != LI->getType()) {
|
2009-09-20 20:09:34 +00:00
|
|
|
// If the stored value is larger or equal to the loaded value, we can
|
|
|
|
// reuse it.
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!DL || !CanCoerceMustAliasedValueToLoad(LD, LI->getType(),*DL)) {
|
2009-09-20 20:09:34 +00:00
|
|
|
UnavailableBlocks.push_back(DepBB);
|
|
|
|
continue;
|
2012-07-24 10:51:42 +00:00
|
|
|
}
|
2008-12-02 08:16:11 +00:00
|
|
|
}
|
2011-04-28 07:29:08 +00:00
|
|
|
ValuesPerBlock.push_back(AvailableValueInBlock::getLoad(DepBB, LD));
|
2008-12-02 08:16:11 +00:00
|
|
|
continue;
|
2007-07-25 19:57:03 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-21 06:48:08 +00:00
|
|
|
UnavailableBlocks.push_back(DepBB);
|
2007-08-07 23:12:31 +00:00
|
|
|
}
|
2013-05-03 19:17:26 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2013-05-03 19:17:26 +00:00
|
|
|
bool GVN::PerformLoadPRE(LoadInst *LI, AvailValInBlkVect &ValuesPerBlock,
|
|
|
|
UnavailBlkVect &UnavailableBlocks) {
|
2008-12-02 08:16:11 +00:00
|
|
|
// Okay, we have *some* definitions of the value. This means that the value
|
|
|
|
// is available in some of our (transitive) predecessors. Lets think about
|
|
|
|
// doing PRE of this load. This will involve inserting a new load into the
|
|
|
|
// predecessor when it's not available. We could do this in general, but
|
|
|
|
// prefer to not increase code size. As such, we only do this when we know
|
|
|
|
// that we only have to insert *one* load (which means we're basically moving
|
|
|
|
// the load, not inserting a new one).
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-05-31 09:03:40 +00:00
|
|
|
SmallPtrSet<BasicBlock *, 4> Blockers;
|
|
|
|
for (unsigned i = 0, e = UnavailableBlocks.size(); i != e; ++i)
|
|
|
|
Blockers.insert(UnavailableBlocks[i]);
|
|
|
|
|
2011-08-17 21:32:02 +00:00
|
|
|
// Let's find the first basic block with more than one predecessor. Walk
|
|
|
|
// backwards through predecessors if needed.
|
2008-12-02 08:16:11 +00:00
|
|
|
BasicBlock *LoadBB = LI->getParent();
|
2009-05-31 09:03:40 +00:00
|
|
|
BasicBlock *TmpBB = LoadBB;
|
|
|
|
|
|
|
|
while (TmpBB->getSinglePredecessor()) {
|
|
|
|
TmpBB = TmpBB->getSinglePredecessor();
|
|
|
|
if (TmpBB == LoadBB) // Infinite (unreachable) loop.
|
|
|
|
return false;
|
|
|
|
if (Blockers.count(TmpBB))
|
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-09-25 05:26:18 +00:00
|
|
|
// If any of these blocks has more than one successor (i.e. if the edge we
|
2012-07-24 10:51:42 +00:00
|
|
|
// just traversed was critical), then there are other paths through this
|
|
|
|
// block along which the load may not be anticipated. Hoisting the load
|
2010-09-25 05:26:18 +00:00
|
|
|
// above this block would be adding the load to execution paths along
|
|
|
|
// which it was not previously executed.
|
2009-06-17 20:48:23 +00:00
|
|
|
if (TmpBB->getTerminator()->getNumSuccessors() != 1)
|
2010-09-25 05:26:18 +00:00
|
|
|
return false;
|
2009-05-31 09:03:40 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-05-31 09:03:40 +00:00
|
|
|
assert(TmpBB);
|
|
|
|
LoadBB = TmpBB;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2010-02-01 21:17:14 +00:00
|
|
|
// Check to see how many predecessors have the loaded value fully
|
|
|
|
// available.
|
2014-05-13 21:06:40 +00:00
|
|
|
MapVector<BasicBlock *, Value *> PredLoads;
|
2008-12-05 07:49:08 +00:00
|
|
|
DenseMap<BasicBlock*, char> FullyAvailableBlocks;
|
2008-12-02 08:16:11 +00:00
|
|
|
for (unsigned i = 0, e = ValuesPerBlock.size(); i != e; ++i)
|
2009-09-21 06:30:24 +00:00
|
|
|
FullyAvailableBlocks[ValuesPerBlock[i].BB] = true;
|
2008-12-02 08:16:11 +00:00
|
|
|
for (unsigned i = 0, e = UnavailableBlocks.size(); i != e; ++i)
|
|
|
|
FullyAvailableBlocks[UnavailableBlocks[i]] = false;
|
2008-12-01 00:40:32 +00:00
|
|
|
|
2013-05-09 18:34:27 +00:00
|
|
|
SmallVector<BasicBlock *, 4> CriticalEdgePred;
|
2014-07-21 17:06:51 +00:00
|
|
|
for (pred_iterator PI = pred_begin(LoadBB), E = pred_end(LoadBB);
|
|
|
|
PI != E; ++PI) {
|
|
|
|
BasicBlock *Pred = *PI;
|
2012-04-27 18:09:28 +00:00
|
|
|
if (IsValueFullyAvailableInBlock(Pred, FullyAvailableBlocks, 0)) {
|
2008-12-02 08:16:11 +00:00
|
|
|
continue;
|
2010-02-01 21:17:14 +00:00
|
|
|
}
|
2010-02-16 19:51:59 +00:00
|
|
|
|
2010-02-01 21:17:14 +00:00
|
|
|
if (Pred->getTerminator()->getNumSuccessors() != 1) {
|
2010-02-16 19:51:59 +00:00
|
|
|
if (isa<IndirectBrInst>(Pred->getTerminator())) {
|
|
|
|
DEBUG(dbgs() << "COULD NOT PRE LOAD BECAUSE OF INDBR CRITICAL EDGE '"
|
|
|
|
<< Pred->getName() << "': " << *LI << '\n');
|
|
|
|
return false;
|
|
|
|
}
|
2011-08-17 21:32:02 +00:00
|
|
|
|
|
|
|
if (LoadBB->isLandingPad()) {
|
|
|
|
DEBUG(dbgs()
|
|
|
|
<< "COULD NOT PRE LOAD BECAUSE OF LANDING PAD CRITICAL EDGE '"
|
|
|
|
<< Pred->getName() << "': " << *LI << '\n');
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-05-09 18:34:27 +00:00
|
|
|
CriticalEdgePred.push_back(Pred);
|
2014-05-13 21:06:40 +00:00
|
|
|
} else {
|
|
|
|
// Only add the predecessors that will not be split for now.
|
|
|
|
PredLoads[Pred] = nullptr;
|
2010-02-01 21:17:14 +00:00
|
|
|
}
|
2008-12-02 08:16:11 +00:00
|
|
|
}
|
2011-08-17 21:32:02 +00:00
|
|
|
|
2010-02-01 21:17:14 +00:00
|
|
|
// Decide whether PRE is profitable for this load.
|
2014-05-13 21:06:40 +00:00
|
|
|
unsigned NumUnavailablePreds = PredLoads.size() + CriticalEdgePred.size();
|
2010-02-01 21:17:14 +00:00
|
|
|
assert(NumUnavailablePreds != 0 &&
|
2013-05-09 18:34:27 +00:00
|
|
|
"Fully available value should already be eliminated!");
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-10-01 20:02:55 +00:00
|
|
|
// If this load is unavailable in multiple predecessors, reject it.
|
|
|
|
// FIXME: If we could restructure the CFG, we could make a common pred with
|
|
|
|
// all the preds that don't have an available LI and insert a new load into
|
|
|
|
// that one block.
|
|
|
|
if (NumUnavailablePreds != 1)
|
2010-02-01 21:17:14 +00:00
|
|
|
return false;
|
|
|
|
|
2013-05-09 18:34:27 +00:00
|
|
|
// Split critical edges, and update the unavailable predecessors accordingly.
|
2014-05-13 21:06:36 +00:00
|
|
|
for (BasicBlock *OrigPred : CriticalEdgePred) {
|
2013-05-09 18:34:27 +00:00
|
|
|
BasicBlock *NewPred = splitCriticalEdges(OrigPred, LoadBB);
|
2014-05-13 21:06:40 +00:00
|
|
|
assert(!PredLoads.count(OrigPred) && "Split edges shouldn't be in map!");
|
2014-04-25 05:29:35 +00:00
|
|
|
PredLoads[NewPred] = nullptr;
|
2013-05-09 18:34:27 +00:00
|
|
|
DEBUG(dbgs() << "Split critical edge " << OrigPred->getName() << "->"
|
|
|
|
<< LoadBB->getName() << '\n');
|
|
|
|
}
|
|
|
|
|
2010-02-01 21:17:14 +00:00
|
|
|
// Check if the load can safely be moved to all the unavailable predecessors.
|
|
|
|
bool CanDoPRE = true;
|
2009-11-28 15:39:14 +00:00
|
|
|
SmallVector<Instruction*, 8> NewInsts;
|
2014-05-13 21:06:36 +00:00
|
|
|
for (auto &PredLoad : PredLoads) {
|
|
|
|
BasicBlock *UnavailablePred = PredLoad.first;
|
2010-02-01 21:17:14 +00:00
|
|
|
|
|
|
|
// Do PHI translation to get its value in the predecessor if necessary. The
|
|
|
|
// returned pointer (if non-null) is guaranteed to dominate UnavailablePred.
|
|
|
|
|
|
|
|
// If all preds have a single successor, then we know it is safe to insert
|
|
|
|
// the load on the pred (?!?), so we can insert code to materialize the
|
|
|
|
// pointer if it is not available.
|
2015-01-04 12:03:27 +00:00
|
|
|
PHITransAddr Address(LI->getPointerOperand(), DL, AC);
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *LoadPtr = nullptr;
|
2013-05-02 21:14:31 +00:00
|
|
|
LoadPtr = Address.PHITranslateWithInsertion(LoadBB, UnavailablePred,
|
|
|
|
*DT, NewInsts);
|
2009-12-03 03:43:29 +00:00
|
|
|
|
2010-02-01 21:17:14 +00:00
|
|
|
// If we couldn't find or insert a computation of this phi translated value,
|
|
|
|
// we fail PRE.
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!LoadPtr) {
|
2010-02-01 21:17:14 +00:00
|
|
|
DEBUG(dbgs() << "COULDN'T INSERT PHI TRANSLATED VALUE OF: "
|
2010-11-10 19:03:33 +00:00
|
|
|
<< *LI->getPointerOperand() << "\n");
|
2010-02-01 21:17:14 +00:00
|
|
|
CanDoPRE = false;
|
|
|
|
break;
|
|
|
|
}
|
2009-12-09 01:59:31 +00:00
|
|
|
|
2014-05-13 21:06:36 +00:00
|
|
|
PredLoad.second = LoadPtr;
|
2009-12-09 01:59:31 +00:00
|
|
|
}
|
2010-02-01 21:17:14 +00:00
|
|
|
|
|
|
|
if (!CanDoPRE) {
|
2011-01-11 08:19:16 +00:00
|
|
|
while (!NewInsts.empty()) {
|
|
|
|
Instruction *I = NewInsts.pop_back_val();
|
|
|
|
if (MD) MD->removeInstruction(I);
|
|
|
|
I->eraseFromParent();
|
|
|
|
}
|
2014-05-13 21:06:36 +00:00
|
|
|
// HINT: Don't revert the edge-splitting as following transformation may
|
|
|
|
// also need to split these critical edges.
|
2013-05-09 18:34:27 +00:00
|
|
|
return !CriticalEdgePred.empty();
|
2009-11-28 16:08:18 +00:00
|
|
|
}
|
2009-06-17 20:48:23 +00:00
|
|
|
|
2008-12-02 08:16:11 +00:00
|
|
|
// Okay, we can eliminate this load by inserting a reload in the predecessor
|
|
|
|
// and using PHI construction to get the value in the other predecessors, do
|
|
|
|
// it.
|
2010-01-05 01:27:17 +00:00
|
|
|
DEBUG(dbgs() << "GVN REMOVING PRE LOAD: " << *LI << '\n');
|
2009-11-28 16:08:18 +00:00
|
|
|
DEBUG(if (!NewInsts.empty())
|
2010-01-05 01:27:17 +00:00
|
|
|
dbgs() << "INSERTED " << NewInsts.size() << " INSTS: "
|
2009-11-28 16:08:18 +00:00
|
|
|
<< *NewInsts.back() << '\n');
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-02-01 21:17:14 +00:00
|
|
|
// Assign value numbers to the new instructions.
|
|
|
|
for (unsigned i = 0, e = NewInsts.size(); i != e; ++i) {
|
2012-07-24 10:51:42 +00:00
|
|
|
// FIXME: We really _ought_ to insert these value numbers into their
|
2010-02-01 21:17:14 +00:00
|
|
|
// parent's availability map. However, in doing so, we risk getting into
|
|
|
|
// ordering issues. If a block hasn't been processed yet, we would be
|
|
|
|
// marking a value as AVAIL-IN, which isn't what we intend.
|
|
|
|
VN.lookup_or_add(NewInsts[i]);
|
|
|
|
}
|
|
|
|
|
2014-05-13 21:06:36 +00:00
|
|
|
for (const auto &PredLoad : PredLoads) {
|
|
|
|
BasicBlock *UnavailablePred = PredLoad.first;
|
|
|
|
Value *LoadPtr = PredLoad.second;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2010-12-15 23:53:55 +00:00
|
|
|
Instruction *NewLoad = new LoadInst(LoadPtr, LI->getName()+".pre", false,
|
|
|
|
LI->getAlignment(),
|
|
|
|
UnavailablePred->getTerminator());
|
|
|
|
|
2014-07-24 12:16:19 +00:00
|
|
|
// Transfer the old load's AA tags to the new load.
|
|
|
|
AAMDNodes Tags;
|
|
|
|
LI->getAAMetadata(Tags);
|
|
|
|
if (Tags)
|
|
|
|
NewLoad->setAAMetadata(Tags);
|
2010-02-01 21:17:14 +00:00
|
|
|
|
2011-05-17 19:43:38 +00:00
|
|
|
// Transfer DebugLoc.
|
|
|
|
NewLoad->setDebugLoc(LI->getDebugLoc());
|
|
|
|
|
2010-02-01 21:17:14 +00:00
|
|
|
// Add the newly created load.
|
|
|
|
ValuesPerBlock.push_back(AvailableValueInBlock::get(UnavailablePred,
|
|
|
|
NewLoad));
|
2010-02-23 05:55:00 +00:00
|
|
|
MD->invalidateCachedPointerInfo(LoadPtr);
|
|
|
|
DEBUG(dbgs() << "GVN INSERTED " << *NewLoad << '\n');
|
2010-02-01 21:17:14 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-02 08:16:11 +00:00
|
|
|
// Perform PHI construction.
|
2011-04-28 16:36:48 +00:00
|
|
|
Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, *this);
|
2009-09-20 20:09:34 +00:00
|
|
|
LI->replaceAllUsesWith(V);
|
|
|
|
if (isa<PHINode>(V))
|
|
|
|
V->takeName(LI);
|
2012-10-24 21:22:30 +00:00
|
|
|
if (V->getType()->getScalarType()->isPointerTy())
|
2009-09-20 20:09:34 +00:00
|
|
|
MD->invalidateCachedPointerInfo(V);
|
2011-04-28 16:36:48 +00:00
|
|
|
markInstructionForDeletion(LI);
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumPRELoad;
|
2007-07-25 19:57:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-05-03 19:17:26 +00:00
|
|
|
/// processNonLocalLoad - Attempt to eliminate a load whose dependencies are
|
|
|
|
/// non-local by performing PHI construction.
|
|
|
|
bool GVN::processNonLocalLoad(LoadInst *LI) {
|
|
|
|
// Step 1: Find the non-local dependencies of the load.
|
|
|
|
LoadDepVect Deps;
|
2015-01-09 00:04:22 +00:00
|
|
|
MD->getNonLocalPointerDependency(LI, Deps);
|
2013-05-03 19:17:26 +00:00
|
|
|
|
|
|
|
// If we had to process more than one hundred blocks to find the
|
|
|
|
// dependencies, this load isn't worth worrying about. Optimizing
|
|
|
|
// it will be too expensive.
|
|
|
|
unsigned NumDeps = Deps.size();
|
|
|
|
if (NumDeps > 100)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// If we had a phi translation failure, we'll have a single entry which is a
|
|
|
|
// clobber in the current block. Reject this early.
|
|
|
|
if (NumDeps == 1 &&
|
|
|
|
!Deps[0].getResult().isDef() && !Deps[0].getResult().isClobber()) {
|
|
|
|
DEBUG(
|
|
|
|
dbgs() << "GVN: non-local load ";
|
2014-01-09 02:29:41 +00:00
|
|
|
LI->printAsOperand(dbgs());
|
2013-05-03 19:17:26 +00:00
|
|
|
dbgs() << " has unknown dependencies\n";
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
// If this load follows a GEP, see if we can PRE the indices before analyzing.
|
|
|
|
if (GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(LI->getOperand(0))) {
|
|
|
|
for (GetElementPtrInst::op_iterator OI = GEP->idx_begin(),
|
|
|
|
OE = GEP->idx_end();
|
|
|
|
OI != OE; ++OI)
|
|
|
|
if (Instruction *I = dyn_cast<Instruction>(OI->get()))
|
|
|
|
performScalarPRE(I);
|
|
|
|
}
|
|
|
|
|
2013-05-03 19:17:26 +00:00
|
|
|
// Step 2: Analyze the availability of the load
|
|
|
|
AvailValInBlkVect ValuesPerBlock;
|
|
|
|
UnavailBlkVect UnavailableBlocks;
|
|
|
|
AnalyzeLoadAvailability(LI, Deps, ValuesPerBlock, UnavailableBlocks);
|
|
|
|
|
|
|
|
// If we have no predecessors that produce a known value for this load, exit
|
|
|
|
// early.
|
|
|
|
if (ValuesPerBlock.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Step 3: Eliminate fully redundancy.
|
|
|
|
//
|
|
|
|
// If all of the instructions we depend on produce a known value for this
|
|
|
|
// load, then it is fully redundant and we can use PHI insertion to compute
|
|
|
|
// its value. Insert PHIs and remove the fully redundant value now.
|
|
|
|
if (UnavailableBlocks.empty()) {
|
|
|
|
DEBUG(dbgs() << "GVN REMOVING NONLOCAL LOAD: " << *LI << '\n');
|
|
|
|
|
|
|
|
// Perform PHI construction.
|
|
|
|
Value *V = ConstructSSAForLoadSet(LI, ValuesPerBlock, *this);
|
|
|
|
LI->replaceAllUsesWith(V);
|
|
|
|
|
|
|
|
if (isa<PHINode>(V))
|
|
|
|
V->takeName(LI);
|
|
|
|
if (V->getType()->getScalarType()->isPointerTy())
|
|
|
|
MD->invalidateCachedPointerInfo(V);
|
|
|
|
markInstructionForDeletion(LI);
|
|
|
|
++NumGVNLoad;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 4: Eliminate partial redundancy.
|
|
|
|
if (!EnablePRE || !EnableLoadPRE)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return PerformLoadPRE(LI, ValuesPerBlock, UnavailableBlocks);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-12 16:22:56 +00:00
|
|
|
static void patchReplacementInstruction(Instruction *I, Value *Repl) {
|
2012-06-04 22:44:21 +00:00
|
|
|
// Patch the replacement so that it is not more restrictive than the value
|
|
|
|
// being replaced.
|
|
|
|
BinaryOperator *Op = dyn_cast<BinaryOperator>(I);
|
|
|
|
BinaryOperator *ReplOp = dyn_cast<BinaryOperator>(Repl);
|
|
|
|
if (Op && ReplOp && isa<OverflowingBinaryOperator>(Op) &&
|
|
|
|
isa<OverflowingBinaryOperator>(ReplOp)) {
|
|
|
|
if (ReplOp->hasNoSignedWrap() && !Op->hasNoSignedWrap())
|
|
|
|
ReplOp->setHasNoSignedWrap(false);
|
|
|
|
if (ReplOp->hasNoUnsignedWrap() && !Op->hasNoUnsignedWrap())
|
|
|
|
ReplOp->setHasNoUnsignedWrap(false);
|
|
|
|
}
|
|
|
|
if (Instruction *ReplInst = dyn_cast<Instruction>(Repl)) {
|
2014-08-15 15:46:38 +00:00
|
|
|
// FIXME: If both the original and replacement value are part of the
|
|
|
|
// same control-flow region (meaning that the execution of one
|
|
|
|
// guarentees the executation of the other), then we can combine the
|
|
|
|
// noalias scopes here and do better than the general conservative
|
|
|
|
// answer used in combineMetadata().
|
|
|
|
|
|
|
|
// In general, GVN unifies expressions over different control-flow
|
|
|
|
// regions, and so we need a conservative combination of the noalias
|
|
|
|
// scopes.
|
|
|
|
unsigned KnownIDs[] = {
|
|
|
|
LLVMContext::MD_tbaa,
|
|
|
|
LLVMContext::MD_alias_scope,
|
|
|
|
LLVMContext::MD_noalias,
|
|
|
|
LLVMContext::MD_range,
|
|
|
|
LLVMContext::MD_fpmath,
|
|
|
|
LLVMContext::MD_invariant_load,
|
|
|
|
};
|
|
|
|
combineMetadata(ReplInst, I, KnownIDs);
|
2012-06-04 22:44:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-12 16:22:56 +00:00
|
|
|
static void patchAndReplaceAllUsesWith(Instruction *I, Value *Repl) {
|
|
|
|
patchReplacementInstruction(I, Repl);
|
2012-06-04 22:44:21 +00:00
|
|
|
I->replaceAllUsesWith(Repl);
|
|
|
|
}
|
|
|
|
|
2007-08-16 22:02:55 +00:00
|
|
|
/// processLoad - Attempt to eliminate a load, first by eliminating it
|
|
|
|
/// locally, and then attempting non-local elimination if that fails.
|
2011-04-28 16:18:52 +00:00
|
|
|
bool GVN::processLoad(LoadInst *L) {
|
2009-11-14 02:27:51 +00:00
|
|
|
if (!MD)
|
|
|
|
return false;
|
|
|
|
|
2011-08-17 22:22:24 +00:00
|
|
|
if (!L->isSimple())
|
2007-07-24 17:55:58 +00:00
|
|
|
return false;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2011-05-22 07:03:34 +00:00
|
|
|
if (L->use_empty()) {
|
|
|
|
markInstructionForDeletion(L);
|
|
|
|
return true;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
// ... to a pointer that has been loaded from before...
|
2009-09-21 02:42:51 +00:00
|
|
|
MemDepResult Dep = MD->getDependency(L);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2011-04-26 01:21:15 +00:00
|
|
|
// If we have a clobber and target data is around, see if this is a clobber
|
|
|
|
// that we can fix up through code synthesis.
|
2014-02-21 00:06:31 +00:00
|
|
|
if (Dep.isClobber() && DL) {
|
Improve GVN to be able to forward substitute a small load
from a piece of a large store when both are in the same block.
This allows clang to compile the testcase in PR4216 to this code:
_test_bitfield:
movl 4(%esp), %eax
movl %eax, %ecx
andl $-65536, %ecx
orl $32962, %eax
andl $40186, %eax
orl %ecx, %eax
ret
This is not ideal, but is a whole lot better than the code produced
by llvm-gcc:
_test_bitfield:
movw $-32574, %ax
orw 4(%esp), %ax
andw $-25350, %ax
movw %ax, 4(%esp)
movw 7(%esp), %cx
shlw $8, %cx
movzbl 6(%esp), %edx
orw %cx, %dx
movzwl %dx, %ecx
shll $16, %ecx
movzwl %ax, %eax
orl %ecx, %eax
ret
and dramatically better than that produced by gcc 4.2:
_test_bitfield:
pushl %ebx
call L3
"L00000000001$pb":
L3:
popl %ebx
movl 8(%esp), %eax
leal 0(,%eax,4), %edx
sarb $7, %dl
movl %eax, %ecx
andl $7168, %ecx
andl $-7201, %ebx
movzbl %dl, %edx
andl $1, %edx
sall $5, %edx
orl %ecx, %ebx
orl %edx, %ebx
andl $24, %eax
andl $-58336, %ebx
orl %eax, %ebx
orl $32962, %ebx
movl %ebx, %eax
popl %ebx
ret
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82439 91177308-0d34-0410-b5e6-96231b3b80d8
2009-09-21 05:57:11 +00:00
|
|
|
// Check to see if we have something like this:
|
2009-09-20 19:03:47 +00:00
|
|
|
// store i32 123, i32* %P
|
|
|
|
// %A = bitcast i32* %P to i8*
|
|
|
|
// %B = gep i8* %A, i32 1
|
|
|
|
// %C = load i8* %B
|
|
|
|
//
|
|
|
|
// We could do that by recognizing if the clobber instructions are obviously
|
|
|
|
// a common base + constant offset, and if the previous store (or memset)
|
|
|
|
// completely covers this load. This sort of thing can happen in bitfield
|
|
|
|
// access code.
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *AvailVal = nullptr;
|
2011-04-26 01:21:15 +00:00
|
|
|
if (StoreInst *DepSI = dyn_cast<StoreInst>(Dep.getInst())) {
|
|
|
|
int Offset = AnalyzeLoadFromClobberingStore(L->getType(),
|
|
|
|
L->getPointerOperand(),
|
2014-02-21 00:06:31 +00:00
|
|
|
DepSI, *DL);
|
2011-04-26 01:21:15 +00:00
|
|
|
if (Offset != -1)
|
|
|
|
AvailVal = GetStoreValueForLoad(DepSI->getValueOperand(), Offset,
|
2014-02-21 00:06:31 +00:00
|
|
|
L->getType(), L, *DL);
|
2011-04-26 01:21:15 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-26 01:21:15 +00:00
|
|
|
// Check to see if we have something like this:
|
|
|
|
// load i32* P
|
|
|
|
// load i8* (P+1)
|
|
|
|
// if we have this, replace the later with an extraction from the former.
|
|
|
|
if (LoadInst *DepLI = dyn_cast<LoadInst>(Dep.getInst())) {
|
|
|
|
// If this is a clobber and L is the first instruction in its block, then
|
|
|
|
// we have the first instruction in the entry block.
|
|
|
|
if (DepLI == L)
|
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-26 01:21:15 +00:00
|
|
|
int Offset = AnalyzeLoadFromClobberingLoad(L->getType(),
|
|
|
|
L->getPointerOperand(),
|
2014-02-21 00:06:31 +00:00
|
|
|
DepLI, *DL);
|
2011-04-26 01:21:15 +00:00
|
|
|
if (Offset != -1)
|
2011-04-28 16:36:48 +00:00
|
|
|
AvailVal = GetLoadValueForLoad(DepLI, Offset, L->getType(), L, *this);
|
2011-04-26 01:21:15 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
// If the clobbering value is a memset/memcpy/memmove, see if we can forward
|
|
|
|
// a value on from it.
|
|
|
|
if (MemIntrinsic *DepMI = dyn_cast<MemIntrinsic>(Dep.getInst())) {
|
2011-04-26 01:21:15 +00:00
|
|
|
int Offset = AnalyzeLoadFromClobberingMemInst(L->getType(),
|
|
|
|
L->getPointerOperand(),
|
2014-02-21 00:06:31 +00:00
|
|
|
DepMI, *DL);
|
2011-04-26 01:21:15 +00:00
|
|
|
if (Offset != -1)
|
2014-02-21 00:06:31 +00:00
|
|
|
AvailVal = GetMemInstValueForLoad(DepMI, Offset, L->getType(), L, *DL);
|
2009-12-06 01:57:02 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
if (AvailVal) {
|
2010-01-05 01:27:17 +00:00
|
|
|
DEBUG(dbgs() << "GVN COERCED INST:\n" << *Dep.getInst() << '\n'
|
2009-12-06 01:57:02 +00:00
|
|
|
<< *AvailVal << '\n' << *L << "\n\n\n");
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-06 01:57:02 +00:00
|
|
|
// Replace the load!
|
|
|
|
L->replaceAllUsesWith(AvailVal);
|
2012-10-24 21:22:30 +00:00
|
|
|
if (AvailVal->getType()->getScalarType()->isPointerTy())
|
2009-12-06 01:57:02 +00:00
|
|
|
MD->invalidateCachedPointerInfo(AvailVal);
|
2011-04-28 16:36:48 +00:00
|
|
|
markInstructionForDeletion(L);
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumGVNLoad;
|
2009-12-06 01:57:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
2011-04-26 01:21:15 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-04-26 01:21:15 +00:00
|
|
|
// If the value isn't available, don't do anything!
|
|
|
|
if (Dep.isClobber()) {
|
2009-05-29 09:46:03 +00:00
|
|
|
DEBUG(
|
2011-04-26 01:21:15 +00:00
|
|
|
// fast print dep, using operator<< on instruction is too slow.
|
2010-01-05 01:27:17 +00:00
|
|
|
dbgs() << "GVN: load ";
|
2014-01-09 02:29:41 +00:00
|
|
|
L->printAsOperand(dbgs());
|
2009-09-21 02:42:51 +00:00
|
|
|
Instruction *I = Dep.getInst();
|
2010-01-05 01:27:17 +00:00
|
|
|
dbgs() << " is clobbered by " << *I << '\n';
|
2009-05-29 09:46:03 +00:00
|
|
|
);
|
2008-12-05 21:04:20 +00:00
|
|
|
return false;
|
2009-05-29 09:46:03 +00:00
|
|
|
}
|
2008-12-05 21:04:20 +00:00
|
|
|
|
2011-10-13 22:14:57 +00:00
|
|
|
// If it is defined in another block, try harder.
|
|
|
|
if (Dep.isNonLocal())
|
|
|
|
return processNonLocalLoad(L);
|
|
|
|
|
|
|
|
if (!Dep.isDef()) {
|
2011-06-15 00:47:34 +00:00
|
|
|
DEBUG(
|
|
|
|
// fast print dep, using operator<< on instruction is too slow.
|
|
|
|
dbgs() << "GVN: load ";
|
2014-01-09 02:29:41 +00:00
|
|
|
L->printAsOperand(dbgs());
|
2011-06-15 00:47:34 +00:00
|
|
|
dbgs() << " has unknown dependence\n";
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-09-21 02:42:51 +00:00
|
|
|
Instruction *DepInst = Dep.getInst();
|
2008-12-05 21:04:20 +00:00
|
|
|
if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
|
2010-11-10 19:03:33 +00:00
|
|
|
Value *StoredVal = DepSI->getValueOperand();
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 19:03:47 +00:00
|
|
|
// The store and load are to a must-aliased pointer, but they may not
|
|
|
|
// actually have the same type. See if we know how to reuse the stored
|
|
|
|
// value (depending on its type).
|
2009-10-21 04:11:19 +00:00
|
|
|
if (StoredVal->getType() != L->getType()) {
|
2014-02-21 00:06:31 +00:00
|
|
|
if (DL) {
|
2009-10-21 04:11:19 +00:00
|
|
|
StoredVal = CoerceAvailableValueToLoadType(StoredVal, L->getType(),
|
2014-02-21 00:06:31 +00:00
|
|
|
L, *DL);
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!StoredVal)
|
2009-10-21 04:11:19 +00:00
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-01-05 01:27:17 +00:00
|
|
|
DEBUG(dbgs() << "GVN COERCED STORE:\n" << *DepSI << '\n' << *StoredVal
|
2009-10-21 04:11:19 +00:00
|
|
|
<< '\n' << *L << "\n\n\n");
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
else
|
2009-09-20 19:03:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-05 21:04:20 +00:00
|
|
|
// Remove it!
|
2009-09-20 19:03:47 +00:00
|
|
|
L->replaceAllUsesWith(StoredVal);
|
2012-10-24 21:22:30 +00:00
|
|
|
if (StoredVal->getType()->getScalarType()->isPointerTy())
|
2009-09-20 19:03:47 +00:00
|
|
|
MD->invalidateCachedPointerInfo(StoredVal);
|
2011-04-28 16:36:48 +00:00
|
|
|
markInstructionForDeletion(L);
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumGVNLoad;
|
2008-12-05 21:04:20 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (LoadInst *DepLI = dyn_cast<LoadInst>(DepInst)) {
|
2009-09-20 19:03:47 +00:00
|
|
|
Value *AvailableVal = DepLI;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-09-20 19:03:47 +00:00
|
|
|
// The loads are of a must-aliased pointer, but they may not actually have
|
|
|
|
// the same type. See if we know how to reuse the previously loaded value
|
|
|
|
// (depending on its type).
|
2009-10-21 04:11:19 +00:00
|
|
|
if (DepLI->getType() != L->getType()) {
|
2014-02-21 00:06:31 +00:00
|
|
|
if (DL) {
|
2011-04-26 01:21:15 +00:00
|
|
|
AvailableVal = CoerceAvailableValueToLoadType(DepLI, L->getType(),
|
2014-02-21 00:06:31 +00:00
|
|
|
L, *DL);
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!AvailableVal)
|
2009-10-21 04:11:19 +00:00
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-01-05 01:27:17 +00:00
|
|
|
DEBUG(dbgs() << "GVN COERCED LOAD:\n" << *DepLI << "\n" << *AvailableVal
|
2009-10-21 04:11:19 +00:00
|
|
|
<< "\n" << *L << "\n\n\n");
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
else
|
2009-10-21 04:11:19 +00:00
|
|
|
return false;
|
2009-09-20 19:03:47 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2008-12-05 21:04:20 +00:00
|
|
|
// Remove it!
|
2013-03-12 16:22:56 +00:00
|
|
|
patchAndReplaceAllUsesWith(L, AvailableVal);
|
2012-10-24 21:22:30 +00:00
|
|
|
if (DepLI->getType()->getScalarType()->isPointerTy())
|
2008-12-09 22:06:23 +00:00
|
|
|
MD->invalidateCachedPointerInfo(DepLI);
|
2011-04-28 16:36:48 +00:00
|
|
|
markInstructionForDeletion(L);
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumGVNLoad;
|
2008-12-05 21:04:20 +00:00
|
|
|
return true;
|
2007-08-14 17:59:48 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-11-30 01:39:32 +00:00
|
|
|
// If this load really doesn't depend on anything, then we must be loading an
|
|
|
|
// undef value. This can happen when loading for a fresh allocation with no
|
|
|
|
// intervening stores, for example.
|
2012-08-29 15:32:21 +00:00
|
|
|
if (isa<AllocaInst>(DepInst) || isMallocLikeFn(DepInst, TLI)) {
|
2009-07-30 23:03:37 +00:00
|
|
|
L->replaceAllUsesWith(UndefValue::get(L->getType()));
|
2011-04-28 16:36:48 +00:00
|
|
|
markInstructionForDeletion(L);
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumGVNLoad;
|
2008-12-05 21:04:20 +00:00
|
|
|
return true;
|
2008-02-12 12:08:14 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2009-12-02 07:35:19 +00:00
|
|
|
// If this load occurs either right after a lifetime begin,
|
2009-10-28 07:05:35 +00:00
|
|
|
// then the loaded value is undefined.
|
2011-04-28 16:36:48 +00:00
|
|
|
if (IntrinsicInst *II = dyn_cast<IntrinsicInst>(DepInst)) {
|
2009-12-02 07:35:19 +00:00
|
|
|
if (II->getIntrinsicID() == Intrinsic::lifetime_start) {
|
2009-10-28 07:05:35 +00:00
|
|
|
L->replaceAllUsesWith(UndefValue::get(L->getType()));
|
2011-04-28 16:36:48 +00:00
|
|
|
markInstructionForDeletion(L);
|
2010-06-22 15:08:57 +00:00
|
|
|
++NumGVNLoad;
|
2009-10-28 07:05:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2008-02-12 12:08:14 +00:00
|
|
|
|
2014-06-12 21:16:19 +00:00
|
|
|
// If this load follows a calloc (which zero initializes memory),
|
|
|
|
// then the loaded value is zero
|
|
|
|
if (isCallocLikeFn(DepInst, TLI)) {
|
|
|
|
L->replaceAllUsesWith(Constant::getNullValue(L->getType()));
|
|
|
|
markInstructionForDeletion(L);
|
|
|
|
++NumGVNLoad;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-12-05 21:04:20 +00:00
|
|
|
return false;
|
2007-07-24 17:55:58 +00:00
|
|
|
}
|
|
|
|
|
2012-07-24 10:51:42 +00:00
|
|
|
// findLeader - In order to find a leader for a given value number at a
|
2010-11-19 22:48:40 +00:00
|
|
|
// specific basic block, we first obtain the list of all Values for that number,
|
2012-07-24 10:51:42 +00:00
|
|
|
// and then scan the list to find one whose block dominates the block in
|
2010-11-19 22:48:40 +00:00
|
|
|
// question. This is fast because dominator tree queries consist of only
|
|
|
|
// a few comparisons of DFS numbers.
|
2012-08-10 15:55:25 +00:00
|
|
|
Value *GVN::findLeader(const BasicBlock *BB, uint32_t num) {
|
2011-01-04 19:29:46 +00:00
|
|
|
LeaderTableEntry Vals = LeaderTable[num];
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!Vals.Val) return nullptr;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2014-04-25 05:29:35 +00:00
|
|
|
Value *Val = nullptr;
|
2010-12-21 23:54:34 +00:00
|
|
|
if (DT->dominates(Vals.BB, BB)) {
|
|
|
|
Val = Vals.Val;
|
|
|
|
if (isa<Constant>(Val)) return Val;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-01-04 19:13:25 +00:00
|
|
|
LeaderTableEntry* Next = Vals.Next;
|
2010-11-18 18:32:40 +00:00
|
|
|
while (Next) {
|
2010-12-21 23:54:34 +00:00
|
|
|
if (DT->dominates(Next->BB, BB)) {
|
|
|
|
if (isa<Constant>(Next->Val)) return Next->Val;
|
|
|
|
if (!Val) Val = Next->Val;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-12-21 23:54:34 +00:00
|
|
|
Next = Next->Next;
|
2008-06-20 01:15:47 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2010-12-21 23:54:34 +00:00
|
|
|
return Val;
|
2008-06-20 01:15:47 +00:00
|
|
|
}
|
|
|
|
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
/// replaceAllDominatedUsesWith - Replace all uses of 'From' with 'To' if the
|
|
|
|
/// use is dominated by the given basic block. Returns the number of uses that
|
|
|
|
/// were replaced.
|
|
|
|
unsigned GVN::replaceAllDominatedUsesWith(Value *From, Value *To,
|
2012-08-16 15:09:43 +00:00
|
|
|
const BasicBlockEdge &Root) {
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
unsigned Count = 0;
|
|
|
|
for (Value::use_iterator UI = From->use_begin(), UE = From->use_end();
|
|
|
|
UI != UE; ) {
|
2014-03-09 03:16:01 +00:00
|
|
|
Use &U = *UI++;
|
2012-03-04 13:25:19 +00:00
|
|
|
|
2012-08-16 15:09:43 +00:00
|
|
|
if (DT->dominates(Root, U)) {
|
2012-02-08 14:10:53 +00:00
|
|
|
U.set(To);
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
++Count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Count;
|
|
|
|
}
|
|
|
|
|
2012-08-16 15:09:43 +00:00
|
|
|
/// isOnlyReachableViaThisEdge - There is an edge from 'Src' to 'Dst'. Return
|
|
|
|
/// true if every path from the entry block to 'Dst' passes via this edge. In
|
|
|
|
/// particular 'Dst' must not be reachable via another edge from 'Src'.
|
|
|
|
static bool isOnlyReachableViaThisEdge(const BasicBlockEdge &E,
|
|
|
|
DominatorTree *DT) {
|
|
|
|
// While in theory it is interesting to consider the case in which Dst has
|
|
|
|
// more than one predecessor, because Dst might be part of a loop which is
|
|
|
|
// only reachable from Src, in practice it is pointless since at the time
|
|
|
|
// GVN runs all such loops have preheaders, which means that Dst will have
|
|
|
|
// been changed to have only one predecessor, namely Src.
|
|
|
|
const BasicBlock *Pred = E.getEnd()->getSinglePredecessor();
|
|
|
|
const BasicBlock *Src = E.getStart();
|
|
|
|
assert((!Pred || Pred == Src) && "No edge between these basic blocks!");
|
|
|
|
(void)Src;
|
2014-04-25 05:29:35 +00:00
|
|
|
return Pred != nullptr;
|
2012-08-16 15:09:43 +00:00
|
|
|
}
|
|
|
|
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
/// propagateEquality - The given values are known to be equal in every block
|
|
|
|
/// dominated by 'Root'. Exploit this, for example by replacing 'LHS' with
|
|
|
|
/// 'RHS' everywhere in the scope. Returns whether a change was made.
|
2012-08-16 15:09:43 +00:00
|
|
|
bool GVN::propagateEquality(Value *LHS, Value *RHS,
|
|
|
|
const BasicBlockEdge &Root) {
|
2012-04-06 15:31:09 +00:00
|
|
|
SmallVector<std::pair<Value*, Value*>, 4> Worklist;
|
|
|
|
Worklist.push_back(std::make_pair(LHS, RHS));
|
|
|
|
bool Changed = false;
|
2012-08-16 15:09:43 +00:00
|
|
|
// For speed, compute a conservative fast approximation to
|
|
|
|
// DT->dominates(Root, Root.getEnd());
|
|
|
|
bool RootDominatesEnd = isOnlyReachableViaThisEdge(Root, DT);
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
|
2012-04-06 15:31:09 +00:00
|
|
|
while (!Worklist.empty()) {
|
|
|
|
std::pair<Value*, Value*> Item = Worklist.pop_back_val();
|
|
|
|
LHS = Item.first; RHS = Item.second;
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
|
2012-04-06 15:31:09 +00:00
|
|
|
if (LHS == RHS) continue;
|
|
|
|
assert(LHS->getType() == RHS->getType() && "Equality but unequal types!");
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
|
2012-04-06 15:31:09 +00:00
|
|
|
// Don't try to propagate equalities between constants.
|
|
|
|
if (isa<Constant>(LHS) && isa<Constant>(RHS)) continue;
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
|
2012-04-06 15:31:09 +00:00
|
|
|
// Prefer a constant on the right-hand side, or an Argument if no constants.
|
|
|
|
if (isa<Constant>(LHS) || (isa<Argument>(LHS) && !isa<Constant>(RHS)))
|
|
|
|
std::swap(LHS, RHS);
|
|
|
|
assert((isa<Argument>(LHS) || isa<Instruction>(LHS)) && "Unexpected value!");
|
|
|
|
|
2015-01-12 21:21:28 +00:00
|
|
|
// If there is no obvious reason to prefer the left-hand side over the
|
|
|
|
// right-hand side, ensure the longest lived term is on the right-hand side,
|
|
|
|
// so the shortest lived term will be replaced by the longest lived.
|
|
|
|
// This tends to expose more simplifications.
|
2012-04-06 15:31:09 +00:00
|
|
|
uint32_t LVN = VN.lookup_or_add(LHS);
|
|
|
|
if ((isa<Argument>(LHS) && isa<Argument>(RHS)) ||
|
|
|
|
(isa<Instruction>(LHS) && isa<Instruction>(RHS))) {
|
2015-01-12 21:21:28 +00:00
|
|
|
// Move the 'oldest' value to the right-hand side, using the value number
|
|
|
|
// as a proxy for age.
|
2012-04-06 15:31:09 +00:00
|
|
|
uint32_t RVN = VN.lookup_or_add(RHS);
|
|
|
|
if (LVN < RVN) {
|
|
|
|
std::swap(LHS, RHS);
|
|
|
|
LVN = RVN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-22 14:17:53 +00:00
|
|
|
// If value numbering later sees that an instruction in the scope is equal
|
|
|
|
// to 'LHS' then ensure it will be turned into 'RHS'. In order to preserve
|
|
|
|
// the invariant that instructions only occur in the leader table for their
|
|
|
|
// own value number (this is used by removeFromLeaderTable), do not do this
|
|
|
|
// if RHS is an instruction (if an instruction in the scope is morphed into
|
|
|
|
// LHS then it will be turned into RHS by the next GVN iteration anyway, so
|
|
|
|
// using the leader table is about compiling faster, not optimizing better).
|
2012-08-16 15:09:43 +00:00
|
|
|
// The leader table only tracks basic blocks, not edges. Only add to if we
|
|
|
|
// have the simple case where the edge dominates the end.
|
|
|
|
if (RootDominatesEnd && !isa<Instruction>(RHS))
|
|
|
|
addToLeaderTable(LVN, RHS, Root.getEnd());
|
2012-04-06 15:31:09 +00:00
|
|
|
|
|
|
|
// Replace all occurrences of 'LHS' with 'RHS' everywhere in the scope. As
|
|
|
|
// LHS always has at least one use that is not dominated by Root, this will
|
|
|
|
// never do anything if LHS has only one use.
|
|
|
|
if (!LHS->hasOneUse()) {
|
|
|
|
unsigned NumReplacements = replaceAllDominatedUsesWith(LHS, RHS, Root);
|
|
|
|
Changed |= NumReplacements > 0;
|
|
|
|
NumGVNEqProp += NumReplacements;
|
|
|
|
}
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
|
2015-01-12 21:21:28 +00:00
|
|
|
// Now try to deduce additional equalities from this one. For example, if
|
|
|
|
// the known equality was "(A != B)" == "false" then it follows that A and B
|
|
|
|
// are equal in the scope. Only boolean equalities with an explicit true or
|
|
|
|
// false RHS are currently supported.
|
2012-04-06 15:31:09 +00:00
|
|
|
if (!RHS->getType()->isIntegerTy(1))
|
|
|
|
// Not a boolean equality - bail out.
|
|
|
|
continue;
|
|
|
|
ConstantInt *CI = dyn_cast<ConstantInt>(RHS);
|
|
|
|
if (!CI)
|
|
|
|
// RHS neither 'true' nor 'false' - bail out.
|
|
|
|
continue;
|
|
|
|
// Whether RHS equals 'true'. Otherwise it equals 'false'.
|
|
|
|
bool isKnownTrue = CI->isAllOnesValue();
|
|
|
|
bool isKnownFalse = !isKnownTrue;
|
|
|
|
|
|
|
|
// If "A && B" is known true then both A and B are known true. If "A || B"
|
|
|
|
// is known false then both A and B are known false.
|
|
|
|
Value *A, *B;
|
|
|
|
if ((isKnownTrue && match(LHS, m_And(m_Value(A), m_Value(B)))) ||
|
|
|
|
(isKnownFalse && match(LHS, m_Or(m_Value(A), m_Value(B))))) {
|
|
|
|
Worklist.push_back(std::make_pair(A, RHS));
|
|
|
|
Worklist.push_back(std::make_pair(B, RHS));
|
|
|
|
continue;
|
|
|
|
}
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
|
2012-04-06 15:31:09 +00:00
|
|
|
// If we are propagating an equality like "(A == B)" == "true" then also
|
|
|
|
// propagate the equality A == B. When propagating a comparison such as
|
|
|
|
// "(A >= B)" == "true", replace all instances of "A < B" with "false".
|
2015-01-12 19:29:48 +00:00
|
|
|
if (CmpInst *Cmp = dyn_cast<CmpInst>(LHS)) {
|
2012-04-06 15:31:09 +00:00
|
|
|
Value *Op0 = Cmp->getOperand(0), *Op1 = Cmp->getOperand(1);
|
|
|
|
|
|
|
|
// If "A == B" is known true, or "A != B" is known false, then replace
|
|
|
|
// A with B everywhere in the scope.
|
|
|
|
if ((isKnownTrue && Cmp->getPredicate() == CmpInst::ICMP_EQ) ||
|
|
|
|
(isKnownFalse && Cmp->getPredicate() == CmpInst::ICMP_NE))
|
|
|
|
Worklist.push_back(std::make_pair(Op0, Op1));
|
|
|
|
|
2015-01-12 19:29:48 +00:00
|
|
|
// Handle the floating point versions of equality comparisons too.
|
|
|
|
if ((isKnownTrue && Cmp->getPredicate() == CmpInst::FCMP_OEQ) ||
|
|
|
|
(isKnownFalse && Cmp->getPredicate() == CmpInst::FCMP_UNE))
|
|
|
|
Worklist.push_back(std::make_pair(Op0, Op1));
|
|
|
|
|
2012-04-06 15:31:09 +00:00
|
|
|
// If "A >= B" is known true, replace "A < B" with false everywhere.
|
|
|
|
CmpInst::Predicate NotPred = Cmp->getInversePredicate();
|
|
|
|
Constant *NotVal = ConstantInt::get(Cmp->getType(), isKnownFalse);
|
2015-01-12 21:21:28 +00:00
|
|
|
// Since we don't have the instruction "A < B" immediately to hand, work
|
|
|
|
// out the value number that it would have and use that to find an
|
|
|
|
// appropriate instruction (if any).
|
2012-04-06 15:31:09 +00:00
|
|
|
uint32_t NextNum = VN.getNextUnusedValueNumber();
|
|
|
|
uint32_t Num = VN.lookup_or_add_cmp(Cmp->getOpcode(), NotPred, Op0, Op1);
|
|
|
|
// If the number we were assigned was brand new then there is no point in
|
|
|
|
// looking for an instruction realizing it: there cannot be one!
|
|
|
|
if (Num < NextNum) {
|
2012-08-16 15:09:43 +00:00
|
|
|
Value *NotCmp = findLeader(Root.getEnd(), Num);
|
2012-04-06 15:31:09 +00:00
|
|
|
if (NotCmp && isa<Instruction>(NotCmp)) {
|
|
|
|
unsigned NumReplacements =
|
|
|
|
replaceAllDominatedUsesWith(NotCmp, NotVal, Root);
|
|
|
|
Changed |= NumReplacements > 0;
|
|
|
|
NumGVNEqProp += NumReplacements;
|
|
|
|
}
|
2012-02-27 12:11:41 +00:00
|
|
|
}
|
2012-04-06 15:31:09 +00:00
|
|
|
// Ensure that any instruction in scope that gets the "A < B" value number
|
|
|
|
// is replaced with false.
|
2012-08-16 15:09:43 +00:00
|
|
|
// The leader table only tracks basic blocks, not edges. Only add to if we
|
|
|
|
// have the simple case where the edge dominates the end.
|
|
|
|
if (RootDominatesEnd)
|
|
|
|
addToLeaderTable(Num, NotVal, Root.getEnd());
|
2012-02-27 08:14:30 +00:00
|
|
|
|
2012-04-06 15:31:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
2008-12-15 02:03:00 +00:00
|
|
|
|
2007-08-14 18:16:29 +00:00
|
|
|
/// processInstruction - When calculating availability, handle an instruction
|
2007-07-24 17:55:58 +00:00
|
|
|
/// by inserting it into the appropriate sets
|
2011-04-28 16:18:52 +00:00
|
|
|
bool GVN::processInstruction(Instruction *I) {
|
2010-02-11 00:20:49 +00:00
|
|
|
// Ignore dbg info intrinsics.
|
|
|
|
if (isa<DbgInfoIntrinsic>(I))
|
|
|
|
return false;
|
|
|
|
|
2010-11-12 21:10:24 +00:00
|
|
|
// If the instruction can be easily simplified then do so now in preference
|
|
|
|
// to value numbering it. Value numbering often exposes redundancies, for
|
|
|
|
// example if it determines that %y is equal to %x then the instruction
|
|
|
|
// "%z = and i32 %x, %y" becomes "%z = and i32 %x, %x" which we now simplify.
|
2015-01-04 12:03:27 +00:00
|
|
|
if (Value *V = SimplifyInstruction(I, DL, TLI, DT, AC)) {
|
2010-11-12 21:10:24 +00:00
|
|
|
I->replaceAllUsesWith(V);
|
2012-10-24 21:22:30 +00:00
|
|
|
if (MD && V->getType()->getScalarType()->isPointerTy())
|
2010-11-12 21:10:24 +00:00
|
|
|
MD->invalidateCachedPointerInfo(V);
|
2011-04-28 16:36:48 +00:00
|
|
|
markInstructionForDeletion(I);
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
++NumGVNSimpl;
|
2010-11-12 21:10:24 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-09-21 02:42:51 +00:00
|
|
|
if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
|
2011-04-28 16:18:52 +00:00
|
|
|
if (processLoad(LI))
|
|
|
|
return true;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2011-04-28 16:18:52 +00:00
|
|
|
unsigned Num = VN.lookup_or_add(LI);
|
|
|
|
addToLeaderTable(Num, LI, LI->getParent());
|
|
|
|
return false;
|
2008-06-18 21:41:49 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
// For conditional branches, we can perform simple conditional propagation on
|
2010-12-21 23:54:34 +00:00
|
|
|
// the condition value itself.
|
|
|
|
if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
|
2013-11-11 22:00:23 +00:00
|
|
|
if (!BI->isConditional())
|
2010-12-21 23:54:34 +00:00
|
|
|
return false;
|
GVN does simple propagation of conditions: when it sees a conditional
branch "br i1 %x, label %if_true, label %if_false" then it replaces
"%x" with "true" in places only reachable via the %if_true arm, and
with "false" in places only reachable via the %if_false arm. Except
that actually it doesn't: if value numbering shows that %y is equal
to %x then, yes, %y will be turned into true/false in this way, but
any occurrences of %x itself are not transformed. Fix this. What's
more, it's often the case that %x is an equality comparison such as
"%x = icmp eq %A, 0", in which case every occurrence of %A that is
only reachable via the %if_true arm can be replaced with 0. Implement
this and a few other variations on this theme. This reduces the number
of lines of LLVM IR in "GCC as one big file" by 0.2%. It has a bigger
impact on Ada code, typically reducing the number of lines of bitcode
by around 0.4% by removing repeated compiler generated checks. Passes
the LLVM nightly testsuite and the Ada ACATS testsuite.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@141177 91177308-0d34-0410-b5e6-96231b3b80d8
2011-10-05 14:28:49 +00:00
|
|
|
|
2013-11-11 22:00:23 +00:00
|
|
|
if (isa<Constant>(BI->getCondition()))
|
|
|
|
return processFoldableCondBr(BI);
|
2013-11-10 07:34:34 +00:00
|
|
|
|
2013-11-11 22:00:23 +00:00
|
|
|
Value *BranchCond = BI->getCondition();
|
2010-12-21 23:54:34 +00:00
|
|
|
BasicBlock *TrueSucc = BI->getSuccessor(0);
|
|
|
|
BasicBlock *FalseSucc = BI->getSuccessor(1);
|
2012-08-16 15:09:43 +00:00
|
|
|
// Avoid multiple edges early.
|
|
|
|
if (TrueSucc == FalseSucc)
|
|
|
|
return false;
|
|
|
|
|
2011-10-05 14:17:01 +00:00
|
|
|
BasicBlock *Parent = BI->getParent();
|
2011-10-07 08:29:06 +00:00
|
|
|
bool Changed = false;
|
2011-10-05 14:17:01 +00:00
|
|
|
|
2012-08-16 15:09:43 +00:00
|
|
|
Value *TrueVal = ConstantInt::getTrue(TrueSucc->getContext());
|
|
|
|
BasicBlockEdge TrueE(Parent, TrueSucc);
|
|
|
|
Changed |= propagateEquality(BranchCond, TrueVal, TrueE);
|
2011-10-05 14:17:01 +00:00
|
|
|
|
2012-08-16 15:09:43 +00:00
|
|
|
Value *FalseVal = ConstantInt::getFalse(FalseSucc->getContext());
|
|
|
|
BasicBlockEdge FalseE(Parent, FalseSucc);
|
|
|
|
Changed |= propagateEquality(BranchCond, FalseVal, FalseE);
|
2011-10-05 14:17:01 +00:00
|
|
|
|
2011-10-07 08:29:06 +00:00
|
|
|
return Changed;
|
|
|
|
}
|
2011-10-05 14:17:01 +00:00
|
|
|
|
2011-10-07 08:29:06 +00:00
|
|
|
// For switches, propagate the case values into the case destinations.
|
|
|
|
if (SwitchInst *SI = dyn_cast<SwitchInst>(I)) {
|
|
|
|
Value *SwitchCond = SI->getCondition();
|
|
|
|
BasicBlock *Parent = SI->getParent();
|
|
|
|
bool Changed = false;
|
2012-08-24 15:06:28 +00:00
|
|
|
|
|
|
|
// Remember how many outgoing edges there are to every successor.
|
|
|
|
SmallDenseMap<BasicBlock *, unsigned, 16> SwitchEdges;
|
|
|
|
for (unsigned i = 0, n = SI->getNumSuccessors(); i != n; ++i)
|
|
|
|
++SwitchEdges[SI->getSuccessor(i)];
|
|
|
|
|
2012-03-11 06:09:17 +00:00
|
|
|
for (SwitchInst::CaseIt i = SI->case_begin(), e = SI->case_end();
|
2012-03-08 07:06:20 +00:00
|
|
|
i != e; ++i) {
|
|
|
|
BasicBlock *Dst = i.getCaseSuccessor();
|
2012-08-24 15:06:28 +00:00
|
|
|
// If there is only a single edge, propagate the case value into it.
|
|
|
|
if (SwitchEdges.lookup(Dst) == 1) {
|
|
|
|
BasicBlockEdge E(Parent, Dst);
|
2012-08-16 15:09:43 +00:00
|
|
|
Changed |= propagateEquality(SwitchCond, i.getCaseValue(), E);
|
2012-08-24 15:06:28 +00:00
|
|
|
}
|
2011-10-07 08:29:06 +00:00
|
|
|
}
|
|
|
|
return Changed;
|
2010-12-21 23:54:34 +00:00
|
|
|
}
|
2011-10-07 08:29:06 +00:00
|
|
|
|
2011-01-04 22:15:21 +00:00
|
|
|
// Instructions with void type don't return a value, so there's
|
2012-02-27 09:54:35 +00:00
|
|
|
// no point in trying to find redundancies in them.
|
2011-01-04 22:15:21 +00:00
|
|
|
if (I->getType()->isVoidTy()) return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2011-01-04 18:54:18 +00:00
|
|
|
uint32_t NextNum = VN.getNextUnusedValueNumber();
|
|
|
|
unsigned Num = VN.lookup_or_add(I);
|
|
|
|
|
2008-04-07 09:59:07 +00:00
|
|
|
// Allocations are always uniquely numbered, so we can save time and memory
|
2009-09-20 02:20:51 +00:00
|
|
|
// by fast failing them.
|
2010-12-19 20:24:28 +00:00
|
|
|
if (isa<AllocaInst>(I) || isa<TerminatorInst>(I) || isa<PHINode>(I)) {
|
2011-01-04 19:13:25 +00:00
|
|
|
addToLeaderTable(Num, I, I->getParent());
|
2008-04-07 09:59:07 +00:00
|
|
|
return false;
|
2008-06-18 21:41:49 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-07-03 17:44:33 +00:00
|
|
|
// If the number we were assigned was a brand new VN, then we don't
|
|
|
|
// need to do a lookup to see if the number already exists
|
|
|
|
// somewhere in the domtree: it can't!
|
2012-02-27 09:54:35 +00:00
|
|
|
if (Num >= NextNum) {
|
2011-01-04 19:13:25 +00:00
|
|
|
addToLeaderTable(Num, I, I->getParent());
|
2010-12-19 20:24:28 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2008-12-15 02:03:00 +00:00
|
|
|
// Perform fast-path value-number based elimination of values inherited from
|
|
|
|
// dominators.
|
2011-01-04 19:13:25 +00:00
|
|
|
Value *repl = findLeader(I->getParent(), Num);
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!repl) {
|
2010-12-19 20:24:28 +00:00
|
|
|
// Failure, just remember this instance for future use.
|
2011-01-04 19:13:25 +00:00
|
|
|
addToLeaderTable(Num, I, I->getParent());
|
2010-12-19 20:24:28 +00:00
|
|
|
return false;
|
2007-07-24 17:55:58 +00:00
|
|
|
}
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-12-19 20:24:28 +00:00
|
|
|
// Remove it!
|
2013-03-12 16:22:56 +00:00
|
|
|
patchAndReplaceAllUsesWith(I, repl);
|
2012-10-24 21:22:30 +00:00
|
|
|
if (MD && repl->getType()->getScalarType()->isPointerTy())
|
2010-12-19 20:24:28 +00:00
|
|
|
MD->invalidateCachedPointerInfo(repl);
|
2011-04-28 16:36:48 +00:00
|
|
|
markInstructionForDeletion(I);
|
2010-12-19 20:24:28 +00:00
|
|
|
return true;
|
2007-07-24 17:55:58 +00:00
|
|
|
}
|
|
|
|
|
2008-12-22 22:32:22 +00:00
|
|
|
/// runOnFunction - This is the main transformation entry point for a function.
|
2007-08-14 18:04:11 +00:00
|
|
|
bool GVN::runOnFunction(Function& F) {
|
2014-02-06 00:07:05 +00:00
|
|
|
if (skipOptnoneFunction(F))
|
|
|
|
return false;
|
|
|
|
|
2009-11-14 02:27:51 +00:00
|
|
|
if (!NoLoads)
|
|
|
|
MD = &getAnalysis<MemoryDependenceAnalysis>();
|
2014-01-13 13:07:17 +00:00
|
|
|
DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
|
2014-02-25 17:30:31 +00:00
|
|
|
DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
|
2014-04-25 05:29:35 +00:00
|
|
|
DL = DLP ? &DLP->getDataLayout() : nullptr;
|
2015-01-04 12:03:27 +00:00
|
|
|
AC = &getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F);
|
2011-12-01 03:08:23 +00:00
|
|
|
TLI = &getAnalysis<TargetLibraryInfo>();
|
2008-05-12 20:15:55 +00:00
|
|
|
VN.setAliasAnalysis(&getAnalysis<AliasAnalysis>());
|
2008-12-01 00:40:32 +00:00
|
|
|
VN.setMemDep(MD);
|
|
|
|
VN.setDomTree(DT);
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-09-21 02:42:51 +00:00
|
|
|
bool Changed = false;
|
|
|
|
bool ShouldContinue = true;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-07-16 17:52:31 +00:00
|
|
|
// Merge unconditional branches, allowing PRE to catch more
|
|
|
|
// optimization opportunities.
|
|
|
|
for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) {
|
2011-01-11 08:13:40 +00:00
|
|
|
BasicBlock *BB = FI++;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2008-07-17 00:01:40 +00:00
|
|
|
bool removedBlock = MergeBlockIntoPredecessor(BB, this);
|
2010-06-22 15:08:57 +00:00
|
|
|
if (removedBlock) ++NumGVNBlocks;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-09-21 02:42:51 +00:00
|
|
|
Changed |= removedBlock;
|
2008-07-16 17:52:31 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-12-09 19:21:47 +00:00
|
|
|
unsigned Iteration = 0;
|
2009-09-21 02:42:51 +00:00
|
|
|
while (ShouldContinue) {
|
2010-01-05 01:27:17 +00:00
|
|
|
DEBUG(dbgs() << "GVN iteration: " << Iteration << "\n");
|
2009-09-21 02:42:51 +00:00
|
|
|
ShouldContinue = iterateOnFunction(F);
|
|
|
|
Changed |= ShouldContinue;
|
2008-12-09 19:21:47 +00:00
|
|
|
++Iteration;
|
2007-08-14 18:04:11 +00:00
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-07-18 18:03:38 +00:00
|
|
|
if (EnablePRE) {
|
2013-11-11 22:00:23 +00:00
|
|
|
// Fabricate val-num for dead-code in order to suppress assertion in
|
|
|
|
// performPRE().
|
|
|
|
assignValNumForDeadCode();
|
2008-09-03 23:06:07 +00:00
|
|
|
bool PREChanged = true;
|
|
|
|
while (PREChanged) {
|
|
|
|
PREChanged = performPRE(F);
|
2009-09-21 02:42:51 +00:00
|
|
|
Changed |= PREChanged;
|
2008-09-03 23:06:07 +00:00
|
|
|
}
|
2008-07-18 18:03:38 +00:00
|
|
|
}
|
2013-05-09 18:34:27 +00:00
|
|
|
|
2008-12-09 19:21:47 +00:00
|
|
|
// FIXME: Should perform GVN again after PRE does something. PRE can move
|
|
|
|
// computations into blocks where they become fully redundant. Note that
|
|
|
|
// we can't do this until PRE's critical edge splitting updates memdep.
|
|
|
|
// Actually, when this happens, we should just fully integrate PRE into GVN.
|
2008-10-10 16:25:50 +00:00
|
|
|
|
|
|
|
cleanupGlobalSets();
|
2013-11-11 22:00:23 +00:00
|
|
|
// Do not cleanup DeadBlocks in cleanupGlobalSets() as it's called for each
|
|
|
|
// iteration.
|
|
|
|
DeadBlocks.clear();
|
2008-10-10 16:25:50 +00:00
|
|
|
|
2009-09-21 02:42:51 +00:00
|
|
|
return Changed;
|
2007-08-14 18:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-21 02:42:51 +00:00
|
|
|
bool GVN::processBlock(BasicBlock *BB) {
|
2011-04-28 16:18:52 +00:00
|
|
|
// FIXME: Kill off InstrsToErase by doing erasing eagerly in a helper function
|
|
|
|
// (and incrementing BI before processing an instruction).
|
|
|
|
assert(InstrsToErase.empty() &&
|
|
|
|
"We expect InstrsToErase to be empty across iterations");
|
2013-11-11 22:00:23 +00:00
|
|
|
if (DeadBlocks.count(BB))
|
|
|
|
return false;
|
|
|
|
|
2009-09-21 02:42:51 +00:00
|
|
|
bool ChangedFunction = false;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-12 19:25:32 +00:00
|
|
|
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
|
|
|
|
BI != BE;) {
|
2011-04-28 16:18:52 +00:00
|
|
|
ChangedFunction |= processInstruction(BI);
|
|
|
|
if (InstrsToErase.empty()) {
|
2008-06-12 19:25:32 +00:00
|
|
|
++BI;
|
|
|
|
continue;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-12 19:25:32 +00:00
|
|
|
// If we need some instructions deleted, do it now.
|
2011-04-28 16:18:52 +00:00
|
|
|
NumGVNInstr += InstrsToErase.size();
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2008-06-12 19:25:32 +00:00
|
|
|
// Avoid iterator invalidation.
|
|
|
|
bool AtStart = BI == BB->begin();
|
|
|
|
if (!AtStart)
|
|
|
|
--BI;
|
|
|
|
|
2013-07-04 01:31:24 +00:00
|
|
|
for (SmallVectorImpl<Instruction *>::iterator I = InstrsToErase.begin(),
|
2011-04-28 16:18:52 +00:00
|
|
|
E = InstrsToErase.end(); I != E; ++I) {
|
2010-01-05 01:27:17 +00:00
|
|
|
DEBUG(dbgs() << "GVN removed: " << **I << '\n');
|
2009-11-14 02:27:51 +00:00
|
|
|
if (MD) MD->removeInstruction(*I);
|
2008-12-22 21:57:30 +00:00
|
|
|
DEBUG(verifyRemoved(*I));
|
2013-02-12 18:44:43 +00:00
|
|
|
(*I)->eraseFromParent();
|
2008-12-01 00:40:32 +00:00
|
|
|
}
|
2011-04-28 16:18:52 +00:00
|
|
|
InstrsToErase.clear();
|
2008-06-12 19:25:32 +00:00
|
|
|
|
|
|
|
if (AtStart)
|
|
|
|
BI = BB->begin();
|
|
|
|
else
|
|
|
|
++BI;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2009-09-21 02:42:51 +00:00
|
|
|
return ChangedFunction;
|
2008-06-12 19:25:32 +00:00
|
|
|
}
|
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
bool GVN::performScalarPRE(Instruction *CurInst) {
|
2013-02-12 19:49:10 +00:00
|
|
|
SmallVector<std::pair<Value*, BasicBlock*>, 8> predMap;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
if (isa<AllocaInst>(CurInst) || isa<TerminatorInst>(CurInst) ||
|
|
|
|
isa<PHINode>(CurInst) || CurInst->getType()->isVoidTy() ||
|
|
|
|
CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
|
|
|
|
isa<DbgInfoIntrinsic>(CurInst))
|
|
|
|
return false;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
// Don't do PRE on compares. The PHI would prevent CodeGenPrepare from
|
|
|
|
// sinking the compare again, and it would force the code generator to
|
|
|
|
// move the i1 from processor flags or predicate registers into a general
|
|
|
|
// purpose register.
|
|
|
|
if (isa<CmpInst>(CurInst))
|
|
|
|
return false;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
// We don't currently value number ANY inline asm calls.
|
|
|
|
if (CallInst *CallI = dyn_cast<CallInst>(CurInst))
|
|
|
|
if (CallI->isInlineAsm())
|
|
|
|
return false;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
uint32_t ValNo = VN.lookup(CurInst);
|
|
|
|
|
|
|
|
// Look for the predecessors for PRE opportunities. We're
|
|
|
|
// only trying to solve the basic diamond case, where
|
|
|
|
// a value is computed in the successor and one predecessor,
|
|
|
|
// but not the other. We also explicitly disallow cases
|
|
|
|
// where the successor is its own predecessor, because they're
|
|
|
|
// more complicated to get right.
|
|
|
|
unsigned NumWith = 0;
|
|
|
|
unsigned NumWithout = 0;
|
|
|
|
BasicBlock *PREPred = nullptr;
|
|
|
|
BasicBlock *CurrentBlock = CurInst->getParent();
|
|
|
|
predMap.clear();
|
|
|
|
|
|
|
|
for (pred_iterator PI = pred_begin(CurrentBlock), PE = pred_end(CurrentBlock);
|
|
|
|
PI != PE; ++PI) {
|
|
|
|
BasicBlock *P = *PI;
|
|
|
|
// We're not interested in PRE where the block is its
|
|
|
|
// own predecessor, or in blocks with predecessors
|
|
|
|
// that are not reachable.
|
|
|
|
if (P == CurrentBlock) {
|
|
|
|
NumWithout = 2;
|
|
|
|
break;
|
|
|
|
} else if (!DT->isReachableFromEntry(P)) {
|
|
|
|
NumWithout = 2;
|
|
|
|
break;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
Value *predV = findLeader(P, ValNo);
|
|
|
|
if (!predV) {
|
|
|
|
predMap.push_back(std::make_pair(static_cast<Value *>(nullptr), P));
|
|
|
|
PREPred = P;
|
|
|
|
++NumWithout;
|
|
|
|
} else if (predV == CurInst) {
|
|
|
|
/* CurInst dominates this predecessor. */
|
|
|
|
NumWithout = 2;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
predMap.push_back(std::make_pair(predV, P));
|
|
|
|
++NumWith;
|
|
|
|
}
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
// Don't do PRE when it might increase code size, i.e. when
|
|
|
|
// we would need to insert instructions in more than one pred.
|
|
|
|
if (NumWithout != 1 || NumWith == 0)
|
|
|
|
return false;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
// Don't do PRE across indirect branch.
|
|
|
|
if (isa<IndirectBrInst>(PREPred->getTerminator()))
|
|
|
|
return false;
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
// We can't do PRE safely on a critical edge, so instead we schedule
|
|
|
|
// the edge to be split and perform the PRE the next time we iterate
|
|
|
|
// on the function.
|
|
|
|
unsigned SuccNum = GetSuccessorNumber(PREPred, CurrentBlock);
|
|
|
|
if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
|
|
|
|
toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
|
|
|
|
return false;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
// Instantiate the expression in the predecessor that lacked it.
|
|
|
|
// Because we are going top-down through the block, all value numbers
|
|
|
|
// will be available in the predecessor by the time we need them. Any
|
|
|
|
// that weren't originally present will have been instantiated earlier
|
|
|
|
// in this loop.
|
|
|
|
Instruction *PREInstr = CurInst->clone();
|
|
|
|
bool success = true;
|
|
|
|
for (unsigned i = 0, e = CurInst->getNumOperands(); i != e; ++i) {
|
|
|
|
Value *Op = PREInstr->getOperand(i);
|
|
|
|
if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
|
|
|
|
continue;
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
if (Value *V = findLeader(PREPred, VN.lookup(Op))) {
|
|
|
|
PREInstr->setOperand(i, V);
|
|
|
|
} else {
|
|
|
|
success = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-13 21:17:58 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
// Fail out if we encounter an operand that is not available in
|
|
|
|
// the PRE predecessor. This is typically because of loads which
|
|
|
|
// are not value numbered precisely.
|
|
|
|
if (!success) {
|
|
|
|
DEBUG(verifyRemoved(PREInstr));
|
|
|
|
delete PREInstr;
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-13 21:17:58 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
PREInstr->insertBefore(PREPred->getTerminator());
|
|
|
|
PREInstr->setName(CurInst->getName() + ".pre");
|
|
|
|
PREInstr->setDebugLoc(CurInst->getDebugLoc());
|
|
|
|
VN.add(PREInstr, ValNo);
|
|
|
|
++NumGVNPRE;
|
2014-11-13 21:17:58 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
// Update the availability map to include the new instruction.
|
|
|
|
addToLeaderTable(ValNo, PREInstr, PREPred);
|
2014-11-14 21:09:13 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
// Create a PHI to make the value available in this block.
|
|
|
|
PHINode *Phi =
|
|
|
|
PHINode::Create(CurInst->getType(), predMap.size(),
|
|
|
|
CurInst->getName() + ".pre-phi", CurrentBlock->begin());
|
|
|
|
for (unsigned i = 0, e = predMap.size(); i != e; ++i) {
|
|
|
|
if (Value *V = predMap[i].first)
|
|
|
|
Phi->addIncoming(V, predMap[i].second);
|
|
|
|
else
|
|
|
|
Phi->addIncoming(PREInstr, PREPred);
|
|
|
|
}
|
|
|
|
|
|
|
|
VN.add(Phi, ValNo);
|
|
|
|
addToLeaderTable(ValNo, Phi, CurrentBlock);
|
|
|
|
Phi->setDebugLoc(CurInst->getDebugLoc());
|
|
|
|
CurInst->replaceAllUsesWith(Phi);
|
|
|
|
if (Phi->getType()->getScalarType()->isPointerTy()) {
|
|
|
|
// Because we have added a PHI-use of the pointer value, it has now
|
|
|
|
// "escaped" from alias analysis' perspective. We need to inform
|
|
|
|
// AA of this.
|
|
|
|
for (unsigned ii = 0, ee = Phi->getNumIncomingValues(); ii != ee; ++ii) {
|
|
|
|
unsigned jj = PHINode::getOperandNumForIncomingValue(ii);
|
|
|
|
VN.getAliasAnalysis()->addEscapingUse(Phi->getOperandUse(jj));
|
|
|
|
}
|
2014-11-14 21:09:13 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
if (MD)
|
|
|
|
MD->invalidateCachedPointerInfo(Phi);
|
|
|
|
}
|
|
|
|
VN.erase(CurInst);
|
|
|
|
removeFromLeaderTable(ValNo, CurInst, CurrentBlock);
|
2014-11-14 21:09:13 +00:00
|
|
|
|
2015-01-09 19:19:56 +00:00
|
|
|
DEBUG(dbgs() << "GVN PRE removed: " << *CurInst << '\n');
|
|
|
|
if (MD)
|
|
|
|
MD->removeInstruction(CurInst);
|
|
|
|
DEBUG(verifyRemoved(CurInst));
|
|
|
|
CurInst->eraseFromParent();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// performPRE - Perform a purely local form of PRE that looks for diamond
|
|
|
|
/// control flow patterns and attempts to perform simple PRE at the join point.
|
|
|
|
bool GVN::performPRE(Function &F) {
|
|
|
|
bool Changed = false;
|
|
|
|
for (BasicBlock *CurrentBlock : depth_first(&F.getEntryBlock())) {
|
|
|
|
// Nothing to PRE in the entry block.
|
|
|
|
if (CurrentBlock == &F.getEntryBlock())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Don't perform PRE on a landing pad.
|
|
|
|
if (CurrentBlock->isLandingPad())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (BasicBlock::iterator BI = CurrentBlock->begin(),
|
|
|
|
BE = CurrentBlock->end();
|
|
|
|
BI != BE;) {
|
|
|
|
Instruction *CurInst = BI++;
|
|
|
|
Changed = performScalarPRE(CurInst);
|
2008-06-18 21:41:49 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2010-02-16 19:51:59 +00:00
|
|
|
if (splitCriticalEdges())
|
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
return Changed;
|
|
|
|
}
|
2009-09-20 02:20:51 +00:00
|
|
|
|
2013-05-09 18:34:27 +00:00
|
|
|
/// Split the critical edge connecting the given two blocks, and return
|
|
|
|
/// the block inserted to the critical edge.
|
|
|
|
BasicBlock *GVN::splitCriticalEdges(BasicBlock *Pred, BasicBlock *Succ) {
|
|
|
|
BasicBlock *BB = SplitCriticalEdge(Pred, Succ, this);
|
|
|
|
if (MD)
|
|
|
|
MD->invalidateCachedPredecessors();
|
|
|
|
return BB;
|
|
|
|
}
|
|
|
|
|
2010-02-16 19:51:59 +00:00
|
|
|
/// splitCriticalEdges - Split critical edges found during the previous
|
|
|
|
/// iteration that may enable further optimization.
|
|
|
|
bool GVN::splitCriticalEdges() {
|
|
|
|
if (toSplit.empty())
|
|
|
|
return false;
|
|
|
|
do {
|
|
|
|
std::pair<TerminatorInst*, unsigned> Edge = toSplit.pop_back_val();
|
|
|
|
SplitCriticalEdge(Edge.first, Edge.second, this);
|
|
|
|
} while (!toSplit.empty());
|
2010-03-01 22:23:12 +00:00
|
|
|
if (MD) MD->invalidateCachedPredecessors();
|
2010-02-16 19:51:59 +00:00
|
|
|
return true;
|
2008-06-18 21:41:49 +00:00
|
|
|
}
|
|
|
|
|
2008-12-22 22:32:22 +00:00
|
|
|
/// iterateOnFunction - Executes one iteration of GVN
|
2007-08-14 18:04:11 +00:00
|
|
|
bool GVN::iterateOnFunction(Function &F) {
|
2008-10-10 16:25:50 +00:00
|
|
|
cleanupGlobalSets();
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2007-07-24 17:55:58 +00:00
|
|
|
// Top-down walk of the dominator tree
|
2009-09-21 02:42:51 +00:00
|
|
|
bool Changed = false;
|
2014-11-13 22:54:59 +00:00
|
|
|
// Save the blocks this function have before transformation begins. GVN may
|
|
|
|
// split critical edge, and hence may invalidate the RPO/DT iterator.
|
|
|
|
//
|
|
|
|
std::vector<BasicBlock *> BBVect;
|
|
|
|
BBVect.reserve(256);
|
2015-01-09 19:19:56 +00:00
|
|
|
// Needed for value numbering with phi construction to work.
|
|
|
|
ReversePostOrderTraversal<Function *> RPOT(&F);
|
|
|
|
for (ReversePostOrderTraversal<Function *>::rpo_iterator RI = RPOT.begin(),
|
|
|
|
RE = RPOT.end();
|
|
|
|
RI != RE; ++RI)
|
|
|
|
BBVect.push_back(*RI);
|
2014-11-13 22:54:59 +00:00
|
|
|
|
|
|
|
for (std::vector<BasicBlock *>::iterator I = BBVect.begin(), E = BBVect.end();
|
|
|
|
I != E; I++)
|
|
|
|
Changed |= processBlock(*I);
|
|
|
|
|
2009-09-21 02:42:51 +00:00
|
|
|
return Changed;
|
2007-07-24 17:55:58 +00:00
|
|
|
}
|
2008-10-10 16:25:50 +00:00
|
|
|
|
|
|
|
void GVN::cleanupGlobalSets() {
|
|
|
|
VN.clear();
|
2011-01-04 19:29:46 +00:00
|
|
|
LeaderTable.clear();
|
2010-11-18 18:32:40 +00:00
|
|
|
TableAllocator.Reset();
|
2008-10-10 16:25:50 +00:00
|
|
|
}
|
2008-12-22 21:36:08 +00:00
|
|
|
|
|
|
|
/// verifyRemoved - Verify that the specified instruction does not occur in our
|
|
|
|
/// internal data structures.
|
2008-12-22 22:28:56 +00:00
|
|
|
void GVN::verifyRemoved(const Instruction *Inst) const {
|
|
|
|
VN.verifyRemoved(Inst);
|
2008-12-22 22:14:07 +00:00
|
|
|
|
2008-12-22 22:28:56 +00:00
|
|
|
// Walk through the value number scope to make sure the instruction isn't
|
|
|
|
// ferreted away in it.
|
2011-01-04 19:13:25 +00:00
|
|
|
for (DenseMap<uint32_t, LeaderTableEntry>::const_iterator
|
2011-01-04 19:29:46 +00:00
|
|
|
I = LeaderTable.begin(), E = LeaderTable.end(); I != E; ++I) {
|
2011-01-04 19:13:25 +00:00
|
|
|
const LeaderTableEntry *Node = &I->second;
|
2010-12-21 23:54:34 +00:00
|
|
|
assert(Node->Val != Inst && "Inst still in value numbering scope!");
|
2012-07-24 10:51:42 +00:00
|
|
|
|
2010-12-21 23:54:34 +00:00
|
|
|
while (Node->Next) {
|
|
|
|
Node = Node->Next;
|
|
|
|
assert(Node->Val != Inst && "Inst still in value numbering scope!");
|
2008-12-22 22:14:07 +00:00
|
|
|
}
|
|
|
|
}
|
2008-12-22 21:36:08 +00:00
|
|
|
}
|
2013-11-11 22:00:23 +00:00
|
|
|
|
|
|
|
// BB is declared dead, which implied other blocks become dead as well. This
|
|
|
|
// function is to add all these blocks to "DeadBlocks". For the dead blocks'
|
|
|
|
// live successors, update their phi nodes by replacing the operands
|
|
|
|
// corresponding to dead blocks with UndefVal.
|
|
|
|
//
|
|
|
|
void GVN::addDeadBlock(BasicBlock *BB) {
|
|
|
|
SmallVector<BasicBlock *, 4> NewDead;
|
|
|
|
SmallSetVector<BasicBlock *, 4> DF;
|
|
|
|
|
|
|
|
NewDead.push_back(BB);
|
|
|
|
while (!NewDead.empty()) {
|
|
|
|
BasicBlock *D = NewDead.pop_back_val();
|
|
|
|
if (DeadBlocks.count(D))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// All blocks dominated by D are dead.
|
|
|
|
SmallVector<BasicBlock *, 8> Dom;
|
|
|
|
DT->getDescendants(D, Dom);
|
|
|
|
DeadBlocks.insert(Dom.begin(), Dom.end());
|
|
|
|
|
|
|
|
// Figure out the dominance-frontier(D).
|
|
|
|
for (SmallVectorImpl<BasicBlock *>::iterator I = Dom.begin(),
|
|
|
|
E = Dom.end(); I != E; I++) {
|
|
|
|
BasicBlock *B = *I;
|
2014-07-21 17:06:51 +00:00
|
|
|
for (succ_iterator SI = succ_begin(B), SE = succ_end(B); SI != SE; SI++) {
|
|
|
|
BasicBlock *S = *SI;
|
2013-11-11 22:00:23 +00:00
|
|
|
if (DeadBlocks.count(S))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
bool AllPredDead = true;
|
2014-07-21 17:06:51 +00:00
|
|
|
for (pred_iterator PI = pred_begin(S), PE = pred_end(S); PI != PE; PI++)
|
|
|
|
if (!DeadBlocks.count(*PI)) {
|
2013-11-11 22:00:23 +00:00
|
|
|
AllPredDead = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AllPredDead) {
|
|
|
|
// S could be proved dead later on. That is why we don't update phi
|
|
|
|
// operands at this moment.
|
|
|
|
DF.insert(S);
|
|
|
|
} else {
|
|
|
|
// While S is not dominated by D, it is dead by now. This could take
|
|
|
|
// place if S already have a dead predecessor before D is declared
|
|
|
|
// dead.
|
|
|
|
NewDead.push_back(S);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// For the dead blocks' live successors, update their phi nodes by replacing
|
|
|
|
// the operands corresponding to dead blocks with UndefVal.
|
|
|
|
for(SmallSetVector<BasicBlock *, 4>::iterator I = DF.begin(), E = DF.end();
|
|
|
|
I != E; I++) {
|
|
|
|
BasicBlock *B = *I;
|
|
|
|
if (DeadBlocks.count(B))
|
|
|
|
continue;
|
|
|
|
|
2013-11-12 08:33:03 +00:00
|
|
|
SmallVector<BasicBlock *, 4> Preds(pred_begin(B), pred_end(B));
|
|
|
|
for (SmallVectorImpl<BasicBlock *>::iterator PI = Preds.begin(),
|
|
|
|
PE = Preds.end(); PI != PE; PI++) {
|
2013-11-11 22:00:23 +00:00
|
|
|
BasicBlock *P = *PI;
|
|
|
|
|
|
|
|
if (!DeadBlocks.count(P))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (isCriticalEdge(P->getTerminator(), GetSuccessorNumber(P, B))) {
|
|
|
|
if (BasicBlock *S = splitCriticalEdges(P, B))
|
|
|
|
DeadBlocks.insert(P = S);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (BasicBlock::iterator II = B->begin(); isa<PHINode>(II); ++II) {
|
|
|
|
PHINode &Phi = cast<PHINode>(*II);
|
|
|
|
Phi.setIncomingValue(Phi.getBasicBlockIndex(P),
|
|
|
|
UndefValue::get(Phi.getType()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the given branch is recognized as a foldable branch (i.e. conditional
|
|
|
|
// branch with constant condition), it will perform following analyses and
|
|
|
|
// transformation.
|
|
|
|
// 1) If the dead out-coming edge is a critical-edge, split it. Let
|
|
|
|
// R be the target of the dead out-coming edge.
|
|
|
|
// 1) Identify the set of dead blocks implied by the branch's dead outcoming
|
|
|
|
// edge. The result of this step will be {X| X is dominated by R}
|
|
|
|
// 2) Identify those blocks which haves at least one dead prodecessor. The
|
|
|
|
// result of this step will be dominance-frontier(R).
|
|
|
|
// 3) Update the PHIs in DF(R) by replacing the operands corresponding to
|
|
|
|
// dead blocks with "UndefVal" in an hope these PHIs will optimized away.
|
|
|
|
//
|
|
|
|
// Return true iff *NEW* dead code are found.
|
|
|
|
bool GVN::processFoldableCondBr(BranchInst *BI) {
|
|
|
|
if (!BI || BI->isUnconditional())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ConstantInt *Cond = dyn_cast<ConstantInt>(BI->getCondition());
|
|
|
|
if (!Cond)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
BasicBlock *DeadRoot = Cond->getZExtValue() ?
|
|
|
|
BI->getSuccessor(1) : BI->getSuccessor(0);
|
|
|
|
if (DeadBlocks.count(DeadRoot))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (!DeadRoot->getSinglePredecessor())
|
|
|
|
DeadRoot = splitCriticalEdges(BI->getParent(), DeadRoot);
|
|
|
|
|
|
|
|
addDeadBlock(DeadRoot);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-05 23:27:34 +00:00
|
|
|
// performPRE() will trigger assert if it comes across an instruction without
|
2013-11-11 22:00:23 +00:00
|
|
|
// associated val-num. As it normally has far more live instructions than dead
|
|
|
|
// instructions, it makes more sense just to "fabricate" a val-number for the
|
|
|
|
// dead code than checking if instruction involved is dead or not.
|
|
|
|
void GVN::assignValNumForDeadCode() {
|
|
|
|
for (SetVector<BasicBlock *>::iterator I = DeadBlocks.begin(),
|
|
|
|
E = DeadBlocks.end(); I != E; I++) {
|
|
|
|
BasicBlock *BB = *I;
|
|
|
|
for (BasicBlock::iterator II = BB->begin(), EE = BB->end();
|
|
|
|
II != EE; II++) {
|
|
|
|
Instruction *Inst = &*II;
|
|
|
|
unsigned ValNum = VN.lookup_or_add(Inst);
|
|
|
|
addToLeaderTable(ValNum, Inst, BB);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|