summaryrefslogtreecommitdiff
path: root/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
diff options
context:
space:
mode:
authorChris Lattner <sabre@nondot.org>2004-04-08 19:59:34 +0000
committerChris Lattner <sabre@nondot.org>2004-04-08 19:59:34 +0000
commit8ba725b4564a0fe8721758d78101f05a68536bdf (patch)
treef8cdd2c65b7c42aa64aee7985343b43c67c205b1 /lib/Transforms/Utils/PromoteMemoryToRegister.cpp
parent86371b70f657b75d4d7cef8f8c61046f92a4d3fd (diff)
downloadllvm-8ba725b4564a0fe8721758d78101f05a68536bdf.tar.gz
llvm-8ba725b4564a0fe8721758d78101f05a68536bdf.tar.bz2
llvm-8ba725b4564a0fe8721758d78101f05a68536bdf.tar.xz
Implement ScalarRepl/select_promote.ll
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@12779 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Transforms/Utils/PromoteMemoryToRegister.cpp')
-rw-r--r--lib/Transforms/Utils/PromoteMemoryToRegister.cpp53
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
index fd320daae5..0efab0bd5a 100644
--- a/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
+++ b/lib/Transforms/Utils/PromoteMemoryToRegister.cpp
@@ -71,11 +71,42 @@ bool llvm::isAllocaPromotable(const AllocaInst *AI, const TargetData &TD) {
if (!isa<LoadInst>(UI) || cast<LoadInst>(UI)->isVolatile()) return false;
// Scan looking for memory accesses.
+ // FIXME: this should REALLY use alias analysis.
for (--UI; !isa<PHINode>(UI); --UI)
if (isa<LoadInst>(UI) || isa<StoreInst>(UI) || isa<CallInst>(UI))
return false;
// If we got this far, we can promote the PHI use.
+ } else if (const SelectInst *SI = dyn_cast<SelectInst>(*UI)) {
+ // We only support selects in a few simple cases. The select is only
+ // allowed to have one use, which must be a load instruction, and can only
+ // use alloca instructions (no random pointers). Also, there cannot be
+ // any accesses to AI between the PHI node and the use of the PHI.
+ if (!SI->hasOneUse()) return false;
+
+ // Our transformation causes the unconditional loading of all pointer
+ // operands of the select. Because this could cause a fault if there is a
+ // critical edge in the CFG and if one of the pointers is illegal, we
+ // refuse to promote the select unless it is obviously safe. For now,
+ // obviously safe means that all of the operands are allocas.
+ //
+ if (!isa<AllocaInst>(SI->getOperand(1)) ||
+ !isa<AllocaInst>(SI->getOperand(2)))
+ return false;
+
+ // Now make sure the one user instruction is in the same basic block as
+ // the PHI, and that there are no loads or stores between the PHI node and
+ // the access.
+ BasicBlock::const_iterator UI = cast<Instruction>(SI->use_back());
+ if (!isa<LoadInst>(UI) || cast<LoadInst>(UI)->isVolatile()) return false;
+
+ // Scan looking for memory accesses.
+ // FIXME: this should REALLY use alias analysis.
+ for (--UI; &*UI != SI; --UI)
+ if (isa<LoadInst>(UI) || isa<StoreInst>(UI) || isa<CallInst>(UI))
+ return false;
+
+ // If we got this far, we can promote the select use.
} else {
return false; // Not a load, store, or promotable PHI?
}
@@ -170,6 +201,28 @@ void PromoteMem2Reg::run() {
} else if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
// Otherwise it must be a load instruction, keep track of variable reads
UsingBlocks.push_back(LI->getParent());
+ } else if (SelectInst *SI = dyn_cast<SelectInst>(User)) {
+ // Because of the restrictions we placed on Select instruction uses
+ // above things are very simple. Transform the PHI of addresses into a
+ // select of loaded values.
+ LoadInst *Load = cast<LoadInst>(SI->use_back());
+ std::string LoadName = Load->getName(); Load->setName("");
+
+ Value *TrueVal = new LoadInst(SI->getOperand(1),
+ SI->getOperand(1)->getName()+".val", SI);
+ Value *FalseVal = new LoadInst(SI->getOperand(2),
+ SI->getOperand(2)->getName()+".val", SI);
+
+ Value *NewSI = new SelectInst(SI->getOperand(0), TrueVal,
+ FalseVal, Load->getName(), SI);
+ Load->replaceAllUsesWith(NewSI);
+ Load->getParent()->getInstList().erase(Load);
+ SI->getParent()->getInstList().erase(SI);
+
+ // Restart our scan of uses...
+ DefiningBlocks.clear();
+ UsingBlocks.clear();
+ goto RestartUseScan;
} else {
// Because of the restrictions we placed on PHI node uses above, the PHI
// node reads the block in any using predecessors. Transform the PHI of