summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Transforms/Utils
diff options
context:
space:
mode:
authorTanya Lattner <tonic@nondot.org>2007-07-11 18:41:34 +0000
committerTanya Lattner <tonic@nondot.org>2007-07-11 18:41:34 +0000
commitccecbcd7794fccc2f0d79bdf3f813fd0bd88ae55 (patch)
treeb208e7b18c8a78e908bc3c0225cb76dcb5e6b162 /llvm/lib/Transforms/Utils
parente59411d056a3be67bf5b07b354ef8726a28c0001 (diff)
downloadbcm5719-llvm-ccecbcd7794fccc2f0d79bdf3f813fd0bd88ae55.tar.gz
bcm5719-llvm-ccecbcd7794fccc2f0d79bdf3f813fd0bd88ae55.zip
Adding ability to demote phi to stack.
llvm-svn: 39744
Diffstat (limited to 'llvm/lib/Transforms/Utils')
-rw-r--r--llvm/lib/Transforms/Utils/DemoteRegToStack.cpp38
1 files changed, 38 insertions, 0 deletions
diff --git a/llvm/lib/Transforms/Utils/DemoteRegToStack.cpp b/llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
index 3eadfa7694a..df332b289d0 100644
--- a/llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
+++ b/llvm/lib/Transforms/Utils/DemoteRegToStack.cpp
@@ -93,3 +93,41 @@ AllocaInst* llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads) {
return Slot;
}
+
+
+/// DemotePHIToStack - This function takes a virtual register computed by a phi
+/// node and replaces it with a slot in the stack frame, allocated via alloca.
+/// The phi node is deleted and it returns the pointer to the alloca inserted.
+AllocaInst* llvm::DemotePHIToStack(PHINode *P) {
+ if (P->use_empty()) {
+ P->eraseFromParent();
+ return 0;
+ }
+
+ // Create a stack slot to hold the value.
+ Function *F = P->getParent()->getParent();
+ AllocaInst *Slot = new AllocaInst(P->getType(), 0, P->getName(),
+ F->getEntryBlock().begin());
+
+ // Iterate over each operand, insert store in each predecessor.
+ for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
+ if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
+ assert(II->getParent() != P->getIncomingBlock(i) &&
+ "Invoke edge not supported yet");
+ }
+ new StoreInst(P->getIncomingValue(i), Slot,
+ P->getIncomingBlock(i)->getTerminator());
+ }
+
+ // Insert load in place of the phi and replace all uses.
+ BasicBlock::iterator InsertPt;
+ for (InsertPt = P->getParent()->getInstList().begin();
+ isa<PHINode>(InsertPt); ++InsertPt);
+ Value *V = new LoadInst(Slot, P->getName()+".reload", P);
+ P->replaceAllUsesWith(V);
+
+ // Delete phi.
+ P->eraseFromParent();
+
+ return Slot;
+}
OpenPOWER on IntegriCloud