forked from OSchip/llvm-project
Hoist the two trivial promotion routines out of the big class that
handles the general cases. The hope is to refactor this so that we don't end up building the entire class for the trivial cases. I also want to lift a lot of the early pre-processing in the initial segment of run() into a separate routine, and really none of it needs to happen inside the primary promotion class. These routines in particular used none of the actual state in the promotion class, so they don't really make sense as members. llvm-svn: 186781
This commit is contained in:
parent
48e11fd76d
commit
420fafef93
|
@ -310,12 +310,6 @@ private:
|
|||
void ComputeLiveInBlocks(AllocaInst *AI, AllocaInfo &Info,
|
||||
const SmallPtrSet<BasicBlock *, 32> &DefBlocks,
|
||||
SmallPtrSet<BasicBlock *, 32> &LiveInBlocks);
|
||||
|
||||
void RewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
|
||||
LargeBlockInfo &LBI);
|
||||
void PromoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info,
|
||||
LargeBlockInfo &LBI);
|
||||
|
||||
void RenamePass(BasicBlock *BB, BasicBlock *Pred,
|
||||
RenamePassData::ValVector &IncVals,
|
||||
std::vector<RenamePassData> &Worklist);
|
||||
|
@ -350,6 +344,162 @@ static void removeLifetimeIntrinsicUsers(AllocaInst *AI) {
|
|||
}
|
||||
}
|
||||
|
||||
/// If there is only a single store to this value, replace any loads of it that
|
||||
/// are directly dominated by the definition with the value stored.
|
||||
static void rewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
|
||||
LargeBlockInfo &LBI,
|
||||
DominatorTree &DT,
|
||||
AliasSetTracker *AST) {
|
||||
StoreInst *OnlyStore = Info.OnlyStore;
|
||||
bool StoringGlobalVal = !isa<Instruction>(OnlyStore->getOperand(0));
|
||||
BasicBlock *StoreBB = OnlyStore->getParent();
|
||||
int StoreIndex = -1;
|
||||
|
||||
// Clear out UsingBlocks. We will reconstruct it here if needed.
|
||||
Info.UsingBlocks.clear();
|
||||
|
||||
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) {
|
||||
Instruction *UserInst = cast<Instruction>(*UI++);
|
||||
if (!isa<LoadInst>(UserInst)) {
|
||||
assert(UserInst == OnlyStore && "Should only have load/stores");
|
||||
continue;
|
||||
}
|
||||
LoadInst *LI = cast<LoadInst>(UserInst);
|
||||
|
||||
// Okay, if we have a load from the alloca, we want to replace it with the
|
||||
// only value stored to the alloca. We can do this if the value is
|
||||
// dominated by the store. If not, we use the rest of the mem2reg machinery
|
||||
// to insert the phi nodes as needed.
|
||||
if (!StoringGlobalVal) { // Non-instructions are always dominated.
|
||||
if (LI->getParent() == StoreBB) {
|
||||
// If we have a use that is in the same block as the store, compare the
|
||||
// indices of the two instructions to see which one came first. If the
|
||||
// load came before the store, we can't handle it.
|
||||
if (StoreIndex == -1)
|
||||
StoreIndex = LBI.getInstructionIndex(OnlyStore);
|
||||
|
||||
if (unsigned(StoreIndex) > LBI.getInstructionIndex(LI)) {
|
||||
// Can't handle this load, bail out.
|
||||
Info.UsingBlocks.push_back(StoreBB);
|
||||
continue;
|
||||
}
|
||||
|
||||
} else if (LI->getParent() != StoreBB &&
|
||||
!DT.dominates(StoreBB, LI->getParent())) {
|
||||
// If the load and store are in different blocks, use BB dominance to
|
||||
// check their relationships. If the store doesn't dom the use, bail
|
||||
// out.
|
||||
Info.UsingBlocks.push_back(LI->getParent());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, we *can* safely rewrite this load.
|
||||
Value *ReplVal = OnlyStore->getOperand(0);
|
||||
// If the replacement value is the load, this must occur in unreachable
|
||||
// code.
|
||||
if (ReplVal == LI)
|
||||
ReplVal = UndefValue::get(LI->getType());
|
||||
LI->replaceAllUsesWith(ReplVal);
|
||||
if (AST && LI->getType()->isPointerTy())
|
||||
AST->deleteValue(LI);
|
||||
LI->eraseFromParent();
|
||||
LBI.deleteValue(LI);
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
/// This is a helper predicate used to search by the first element of a pair.
|
||||
struct StoreIndexSearchPredicate {
|
||||
bool operator()(const std::pair<unsigned, StoreInst *> &LHS,
|
||||
const std::pair<unsigned, StoreInst *> &RHS) {
|
||||
return LHS.first < RHS.first;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Many allocas are only used within a single basic block. If this is the
|
||||
/// case, avoid traversing the CFG and inserting a lot of potentially useless
|
||||
/// PHI nodes by just performing a single linear pass over the basic block
|
||||
/// using the Alloca.
|
||||
///
|
||||
/// If we cannot promote this alloca (because it is read before it is written),
|
||||
/// return true. This is necessary in cases where, due to control flow, the
|
||||
/// alloca is potentially undefined on some control flow paths. e.g. code like
|
||||
/// this is potentially correct:
|
||||
///
|
||||
/// for (...) { if (c) { A = undef; undef = B; } }
|
||||
///
|
||||
/// ... so long as A is not used before undef is set.
|
||||
static void promoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info,
|
||||
LargeBlockInfo &LBI,
|
||||
AliasSetTracker *AST) {
|
||||
// The trickiest case to handle is when we have large blocks. Because of this,
|
||||
// this code is optimized assuming that large blocks happen. This does not
|
||||
// significantly pessimize the small block case. This uses LargeBlockInfo to
|
||||
// make it efficient to get the index of various operations in the block.
|
||||
|
||||
// Clear out UsingBlocks. We will reconstruct it here if needed.
|
||||
Info.UsingBlocks.clear();
|
||||
|
||||
// Walk the use-def list of the alloca, getting the locations of all stores.
|
||||
typedef SmallVector<std::pair<unsigned, StoreInst *>, 64> StoresByIndexTy;
|
||||
StoresByIndexTy StoresByIndex;
|
||||
|
||||
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;
|
||||
++UI)
|
||||
if (StoreInst *SI = dyn_cast<StoreInst>(*UI))
|
||||
StoresByIndex.push_back(std::make_pair(LBI.getInstructionIndex(SI), SI));
|
||||
|
||||
// If there are no stores to the alloca, just replace any loads with undef.
|
||||
if (StoresByIndex.empty()) {
|
||||
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;)
|
||||
if (LoadInst *LI = dyn_cast<LoadInst>(*UI++)) {
|
||||
LI->replaceAllUsesWith(UndefValue::get(LI->getType()));
|
||||
if (AST && LI->getType()->isPointerTy())
|
||||
AST->deleteValue(LI);
|
||||
LBI.deleteValue(LI);
|
||||
LI->eraseFromParent();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Sort the stores by their index, making it efficient to do a lookup with a
|
||||
// binary search.
|
||||
std::sort(StoresByIndex.begin(), StoresByIndex.end());
|
||||
|
||||
// Walk all of the loads from this alloca, replacing them with the nearest
|
||||
// store above them, if any.
|
||||
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) {
|
||||
LoadInst *LI = dyn_cast<LoadInst>(*UI++);
|
||||
if (!LI)
|
||||
continue;
|
||||
|
||||
unsigned LoadIdx = LBI.getInstructionIndex(LI);
|
||||
|
||||
// Find the nearest store that has a lower than this load.
|
||||
StoresByIndexTy::iterator I = std::lower_bound(
|
||||
StoresByIndex.begin(), StoresByIndex.end(),
|
||||
std::pair<unsigned, StoreInst *>(LoadIdx, static_cast<StoreInst *>(0)),
|
||||
StoreIndexSearchPredicate());
|
||||
|
||||
// If there is no store before this load, then we can't promote this load.
|
||||
if (I == StoresByIndex.begin()) {
|
||||
// Can't handle this load, bail out.
|
||||
Info.UsingBlocks.push_back(LI->getParent());
|
||||
continue;
|
||||
}
|
||||
|
||||
// Otherwise, there was a store before this load, the load takes its value.
|
||||
--I;
|
||||
LI->replaceAllUsesWith(I->second->getOperand(0));
|
||||
if (AST && LI->getType()->isPointerTy())
|
||||
AST->deleteValue(LI);
|
||||
LI->eraseFromParent();
|
||||
LBI.deleteValue(LI);
|
||||
}
|
||||
}
|
||||
|
||||
void PromoteMem2Reg::run() {
|
||||
Function &F = *DT.getRoot()->getParent();
|
||||
|
||||
|
@ -388,7 +538,7 @@ void PromoteMem2Reg::run() {
|
|||
// If there is only a single store to this value, replace any loads of
|
||||
// it that are directly dominated by the definition with the value stored.
|
||||
if (Info.DefiningBlocks.size() == 1) {
|
||||
RewriteSingleStoreAlloca(AI, Info, LBI);
|
||||
rewriteSingleStoreAlloca(AI, Info, LBI, DT, AST);
|
||||
|
||||
// Finally, after the scan, check to see if the store is all that is left.
|
||||
if (Info.UsingBlocks.empty()) {
|
||||
|
@ -418,7 +568,7 @@ void PromoteMem2Reg::run() {
|
|||
// If the alloca is only read and written in one basic block, just perform a
|
||||
// linear sweep over the block to eliminate it.
|
||||
if (Info.OnlyUsedInOneBlock) {
|
||||
PromoteSingleBlockAlloca(AI, Info, LBI);
|
||||
promoteSingleBlockAlloca(AI, Info, LBI, AST);
|
||||
|
||||
// Finally, after the scan, check to see if the stores are all that is
|
||||
// left.
|
||||
|
@ -815,159 +965,6 @@ void PromoteMem2Reg::DetermineInsertionPoint(AllocaInst *AI, unsigned AllocaNum,
|
|||
QueuePhiNode(DFBlocks[i].second, AllocaNum, CurrentVersion);
|
||||
}
|
||||
|
||||
/// If there is only a single store to this value, replace any loads of it that
|
||||
/// are directly dominated by the definition with the value stored.
|
||||
void PromoteMem2Reg::RewriteSingleStoreAlloca(AllocaInst *AI, AllocaInfo &Info,
|
||||
LargeBlockInfo &LBI) {
|
||||
StoreInst *OnlyStore = Info.OnlyStore;
|
||||
bool StoringGlobalVal = !isa<Instruction>(OnlyStore->getOperand(0));
|
||||
BasicBlock *StoreBB = OnlyStore->getParent();
|
||||
int StoreIndex = -1;
|
||||
|
||||
// Clear out UsingBlocks. We will reconstruct it here if needed.
|
||||
Info.UsingBlocks.clear();
|
||||
|
||||
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) {
|
||||
Instruction *UserInst = cast<Instruction>(*UI++);
|
||||
if (!isa<LoadInst>(UserInst)) {
|
||||
assert(UserInst == OnlyStore && "Should only have load/stores");
|
||||
continue;
|
||||
}
|
||||
LoadInst *LI = cast<LoadInst>(UserInst);
|
||||
|
||||
// Okay, if we have a load from the alloca, we want to replace it with the
|
||||
// only value stored to the alloca. We can do this if the value is
|
||||
// dominated by the store. If not, we use the rest of the mem2reg machinery
|
||||
// to insert the phi nodes as needed.
|
||||
if (!StoringGlobalVal) { // Non-instructions are always dominated.
|
||||
if (LI->getParent() == StoreBB) {
|
||||
// If we have a use that is in the same block as the store, compare the
|
||||
// indices of the two instructions to see which one came first. If the
|
||||
// load came before the store, we can't handle it.
|
||||
if (StoreIndex == -1)
|
||||
StoreIndex = LBI.getInstructionIndex(OnlyStore);
|
||||
|
||||
if (unsigned(StoreIndex) > LBI.getInstructionIndex(LI)) {
|
||||
// Can't handle this load, bail out.
|
||||
Info.UsingBlocks.push_back(StoreBB);
|
||||
continue;
|
||||
}
|
||||
|
||||
} else if (LI->getParent() != StoreBB &&
|
||||
!dominates(StoreBB, LI->getParent())) {
|
||||
// If the load and store are in different blocks, use BB dominance to
|
||||
// check their relationships. If the store doesn't dom the use, bail
|
||||
// out.
|
||||
Info.UsingBlocks.push_back(LI->getParent());
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Otherwise, we *can* safely rewrite this load.
|
||||
Value *ReplVal = OnlyStore->getOperand(0);
|
||||
// If the replacement value is the load, this must occur in unreachable
|
||||
// code.
|
||||
if (ReplVal == LI)
|
||||
ReplVal = UndefValue::get(LI->getType());
|
||||
LI->replaceAllUsesWith(ReplVal);
|
||||
if (AST && LI->getType()->isPointerTy())
|
||||
AST->deleteValue(LI);
|
||||
LI->eraseFromParent();
|
||||
LBI.deleteValue(LI);
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
/// This is a helper predicate used to search by the first element of a pair.
|
||||
struct StoreIndexSearchPredicate {
|
||||
bool operator()(const std::pair<unsigned, StoreInst *> &LHS,
|
||||
const std::pair<unsigned, StoreInst *> &RHS) {
|
||||
return LHS.first < RHS.first;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Many allocas are only used within a single basic block. If this is the
|
||||
/// case, avoid traversing the CFG and inserting a lot of potentially useless
|
||||
/// PHI nodes by just performing a single linear pass over the basic block
|
||||
/// using the Alloca.
|
||||
///
|
||||
/// If we cannot promote this alloca (because it is read before it is written),
|
||||
/// return true. This is necessary in cases where, due to control flow, the
|
||||
/// alloca is potentially undefined on some control flow paths. e.g. code like
|
||||
/// this is potentially correct:
|
||||
///
|
||||
/// for (...) { if (c) { A = undef; undef = B; } }
|
||||
///
|
||||
/// ... so long as A is not used before undef is set.
|
||||
void PromoteMem2Reg::PromoteSingleBlockAlloca(AllocaInst *AI, AllocaInfo &Info,
|
||||
LargeBlockInfo &LBI) {
|
||||
// The trickiest case to handle is when we have large blocks. Because of this,
|
||||
// this code is optimized assuming that large blocks happen. This does not
|
||||
// significantly pessimize the small block case. This uses LargeBlockInfo to
|
||||
// make it efficient to get the index of various operations in the block.
|
||||
|
||||
// Clear out UsingBlocks. We will reconstruct it here if needed.
|
||||
Info.UsingBlocks.clear();
|
||||
|
||||
// Walk the use-def list of the alloca, getting the locations of all stores.
|
||||
typedef SmallVector<std::pair<unsigned, StoreInst *>, 64> StoresByIndexTy;
|
||||
StoresByIndexTy StoresByIndex;
|
||||
|
||||
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;
|
||||
++UI)
|
||||
if (StoreInst *SI = dyn_cast<StoreInst>(*UI))
|
||||
StoresByIndex.push_back(std::make_pair(LBI.getInstructionIndex(SI), SI));
|
||||
|
||||
// If there are no stores to the alloca, just replace any loads with undef.
|
||||
if (StoresByIndex.empty()) {
|
||||
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;)
|
||||
if (LoadInst *LI = dyn_cast<LoadInst>(*UI++)) {
|
||||
LI->replaceAllUsesWith(UndefValue::get(LI->getType()));
|
||||
if (AST && LI->getType()->isPointerTy())
|
||||
AST->deleteValue(LI);
|
||||
LBI.deleteValue(LI);
|
||||
LI->eraseFromParent();
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Sort the stores by their index, making it efficient to do a lookup with a
|
||||
// binary search.
|
||||
std::sort(StoresByIndex.begin(), StoresByIndex.end());
|
||||
|
||||
// Walk all of the loads from this alloca, replacing them with the nearest
|
||||
// store above them, if any.
|
||||
for (Value::use_iterator UI = AI->use_begin(), E = AI->use_end(); UI != E;) {
|
||||
LoadInst *LI = dyn_cast<LoadInst>(*UI++);
|
||||
if (!LI)
|
||||
continue;
|
||||
|
||||
unsigned LoadIdx = LBI.getInstructionIndex(LI);
|
||||
|
||||
// Find the nearest store that has a lower than this load.
|
||||
StoresByIndexTy::iterator I = std::lower_bound(
|
||||
StoresByIndex.begin(), StoresByIndex.end(),
|
||||
std::pair<unsigned, StoreInst *>(LoadIdx, static_cast<StoreInst *>(0)),
|
||||
StoreIndexSearchPredicate());
|
||||
|
||||
// If there is no store before this load, then we can't promote this load.
|
||||
if (I == StoresByIndex.begin()) {
|
||||
// Can't handle this load, bail out.
|
||||
Info.UsingBlocks.push_back(LI->getParent());
|
||||
continue;
|
||||
}
|
||||
|
||||
// Otherwise, there was a store before this load, the load takes its value.
|
||||
--I;
|
||||
LI->replaceAllUsesWith(I->second->getOperand(0));
|
||||
if (AST && LI->getType()->isPointerTy())
|
||||
AST->deleteValue(LI);
|
||||
LI->eraseFromParent();
|
||||
LBI.deleteValue(LI);
|
||||
}
|
||||
}
|
||||
|
||||
/// \brief Queue a phi-node to be added to a basic-block for a specific Alloca.
|
||||
///
|
||||
/// Returns true if there wasn't already a phi-node for that variable
|
||||
|
|
Loading…
Reference in New Issue