2003-11-06 19:46:29 +00:00
|
|
|
//===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +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.
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-10-20 19:43:21 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2005-04-21 23:48:37 +00:00
|
|
|
//
|
2003-05-29 15:12:27 +00:00
|
|
|
// This file provide the function DemoteRegToStack(). This function takes a
|
2004-03-16 23:23:11 +00:00
|
|
|
// virtual register computed by an Instruction and replaces it with a slot in
|
2003-05-29 15:12:27 +00:00
|
|
|
// the stack frame, allocated via alloca. It returns the pointer to the
|
2004-03-16 23:23:11 +00:00
|
|
|
// AllocaInst inserted. After this function is called on an instruction, we are
|
|
|
|
// guaranteed that the only user of the instruction is a store that is
|
|
|
|
// immediately after it.
|
2003-05-29 15:12:27 +00:00
|
|
|
//
|
2002-12-10 13:07:58 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-03-14 02:13:38 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2002-12-10 13:07:58 +00:00
|
|
|
#include "llvm/Function.h"
|
2004-03-16 23:23:11 +00:00
|
|
|
#include "llvm/Instructions.h"
|
2005-03-17 15:38:16 +00:00
|
|
|
#include "llvm/Type.h"
|
2004-04-01 20:28:45 +00:00
|
|
|
#include <map>
|
2004-01-09 06:12:26 +00:00
|
|
|
using namespace llvm;
|
2003-11-11 22:41:34 +00:00
|
|
|
|
2004-03-16 23:23:11 +00:00
|
|
|
/// DemoteRegToStack - This function takes a virtual register computed by an
|
|
|
|
/// Instruction and replaces it with a slot in the stack frame, allocated via
|
|
|
|
/// alloca. This allows the CFG to be changed around without fear of
|
|
|
|
/// invalidating the SSA information for the value. It returns the pointer to
|
|
|
|
/// the alloca inserted to create a stack slot for I.
|
|
|
|
///
|
2009-07-15 23:53:25 +00:00
|
|
|
AllocaInst* llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
|
2007-10-21 23:05:16 +00:00
|
|
|
Instruction *AllocaPoint) {
|
|
|
|
if (I.use_empty()) {
|
|
|
|
I.eraseFromParent();
|
|
|
|
return 0;
|
|
|
|
}
|
2010-06-16 22:41:09 +00:00
|
|
|
|
2004-03-16 23:23:11 +00:00
|
|
|
// Create a stack slot to hold the value.
|
2007-10-21 23:05:16 +00:00
|
|
|
AllocaInst *Slot;
|
|
|
|
if (AllocaPoint) {
|
2009-07-15 23:53:25 +00:00
|
|
|
Slot = new AllocaInst(I.getType(), 0,
|
2009-07-14 23:09:55 +00:00
|
|
|
I.getName()+".reg2mem", AllocaPoint);
|
2007-10-21 23:05:16 +00:00
|
|
|
} else {
|
|
|
|
Function *F = I.getParent()->getParent();
|
2009-07-15 23:53:25 +00:00
|
|
|
Slot = new AllocaInst(I.getType(), 0, I.getName()+".reg2mem",
|
2007-10-21 23:05:16 +00:00
|
|
|
F->getEntryBlock().begin());
|
|
|
|
}
|
2010-06-16 22:41:09 +00:00
|
|
|
|
2004-03-16 23:23:11 +00:00
|
|
|
// Change all of the users of the instruction to read from the stack slot
|
|
|
|
// instead.
|
|
|
|
while (!I.use_empty()) {
|
|
|
|
Instruction *U = cast<Instruction>(I.use_back());
|
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(U)) {
|
|
|
|
// If this is a PHI node, we can't insert a load of the value before the
|
|
|
|
// use. Instead, insert the load in the predecessor block corresponding
|
|
|
|
// to the incoming value.
|
|
|
|
//
|
|
|
|
// Note that if there are multiple edges from a basic block to this PHI
|
2004-04-01 20:28:45 +00:00
|
|
|
// node that we cannot multiple loads. The problem is that the resultant
|
|
|
|
// PHI node will have multiple values (from each load) coming in from the
|
|
|
|
// same block, which is illegal SSA form. For this reason, we keep track
|
|
|
|
// and reuse loads we insert.
|
|
|
|
std::map<BasicBlock*, Value*> Loads;
|
2004-03-16 23:23:11 +00:00
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
|
|
|
if (PN->getIncomingValue(i) == &I) {
|
2004-04-01 20:28:45 +00:00
|
|
|
Value *&V = Loads[PN->getIncomingBlock(i)];
|
|
|
|
if (V == 0) {
|
|
|
|
// Insert the load into the predecessor block
|
2010-06-16 22:41:09 +00:00
|
|
|
V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
|
2004-04-01 20:28:45 +00:00
|
|
|
PN->getIncomingBlock(i)->getTerminator());
|
|
|
|
}
|
2004-03-16 23:23:11 +00:00
|
|
|
PN->setIncomingValue(i, V);
|
2003-09-20 14:36:23 +00:00
|
|
|
}
|
|
|
|
|
2004-03-16 23:23:11 +00:00
|
|
|
} else {
|
|
|
|
// If this is a normal instruction, just insert a load.
|
2005-09-27 19:39:00 +00:00
|
|
|
Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
|
2004-03-16 23:23:11 +00:00
|
|
|
U->replaceUsesOfWith(&I, V);
|
2002-12-10 13:07:58 +00:00
|
|
|
}
|
2003-09-20 14:36:23 +00:00
|
|
|
}
|
2002-12-10 13:07:58 +00:00
|
|
|
|
|
|
|
|
2004-03-16 23:23:11 +00:00
|
|
|
// Insert stores of the computed value into the stack slot. We have to be
|
|
|
|
// careful is I is an invoke instruction though, because we can't insert the
|
|
|
|
// store AFTER the terminator instruction.
|
2005-09-27 19:39:00 +00:00
|
|
|
BasicBlock::iterator InsertPt;
|
2004-03-16 23:23:11 +00:00
|
|
|
if (!isa<TerminatorInst>(I)) {
|
2005-09-27 19:39:00 +00:00
|
|
|
InsertPt = &I;
|
2005-10-04 00:44:01 +00:00
|
|
|
++InsertPt;
|
2004-03-16 23:23:11 +00:00
|
|
|
} else {
|
2005-09-27 19:39:00 +00:00
|
|
|
// We cannot demote invoke instructions to the stack if their normal edge
|
|
|
|
// is critical.
|
|
|
|
InvokeInst &II = cast<InvokeInst>(I);
|
|
|
|
assert(II.getNormalDest()->getSinglePredecessor() &&
|
|
|
|
"Cannot demote invoke with a critical successor!");
|
|
|
|
InsertPt = II.getNormalDest()->begin();
|
2004-03-16 23:23:11 +00:00
|
|
|
}
|
2003-11-06 19:46:29 +00:00
|
|
|
|
2005-10-04 00:44:01 +00:00
|
|
|
for (; isa<PHINode>(InsertPt); ++InsertPt)
|
2005-09-27 19:39:00 +00:00
|
|
|
/* empty */; // Don't insert before any PHI nodes.
|
|
|
|
new StoreInst(&I, Slot, InsertPt);
|
|
|
|
|
2004-03-16 23:23:11 +00:00
|
|
|
return Slot;
|
2002-12-10 13:07:58 +00:00
|
|
|
}
|
2007-07-11 18:41:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// 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.
|
2009-07-15 23:53:25 +00:00
|
|
|
AllocaInst* llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
|
2007-07-11 18:41:34 +00:00
|
|
|
if (P->use_empty()) {
|
2010-06-16 22:41:09 +00:00
|
|
|
P->eraseFromParent();
|
|
|
|
return 0;
|
2007-07-11 18:41:34 +00:00
|
|
|
}
|
2007-10-21 23:05:16 +00:00
|
|
|
|
2007-07-11 18:41:34 +00:00
|
|
|
// Create a stack slot to hold the value.
|
2007-10-21 23:05:16 +00:00
|
|
|
AllocaInst *Slot;
|
|
|
|
if (AllocaPoint) {
|
2009-07-15 23:53:25 +00:00
|
|
|
Slot = new AllocaInst(P->getType(), 0,
|
2009-07-14 23:09:55 +00:00
|
|
|
P->getName()+".reg2mem", AllocaPoint);
|
2007-10-21 23:05:16 +00:00
|
|
|
} else {
|
|
|
|
Function *F = P->getParent()->getParent();
|
2009-07-15 23:53:25 +00:00
|
|
|
Slot = new AllocaInst(P->getType(), 0, P->getName()+".reg2mem",
|
2007-10-21 23:05:16 +00:00
|
|
|
F->getEntryBlock().begin());
|
|
|
|
}
|
2010-06-16 22:41:09 +00:00
|
|
|
|
2007-07-11 18:41:34 +00:00
|
|
|
// 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))) {
|
2010-06-16 22:41:09 +00:00
|
|
|
assert(II->getParent() != P->getIncomingBlock(i) &&
|
2010-12-23 00:58:24 +00:00
|
|
|
"Invoke edge not supported yet"); (void)II;
|
2007-07-11 18:41:34 +00:00
|
|
|
}
|
2010-06-16 22:41:09 +00:00
|
|
|
new StoreInst(P->getIncomingValue(i), Slot,
|
2007-07-11 18:41:34 +00:00
|
|
|
P->getIncomingBlock(i)->getTerminator());
|
|
|
|
}
|
2010-06-16 22:41:09 +00:00
|
|
|
|
2007-07-11 18:41:34 +00:00
|
|
|
// Insert load in place of the phi and replace all uses.
|
|
|
|
Value *V = new LoadInst(Slot, P->getName()+".reload", P);
|
|
|
|
P->replaceAllUsesWith(V);
|
2010-06-16 22:41:09 +00:00
|
|
|
|
2007-07-11 18:41:34 +00:00
|
|
|
// Delete phi.
|
|
|
|
P->eraseFromParent();
|
2010-06-16 22:41:09 +00:00
|
|
|
|
2007-07-11 18:41:34 +00:00
|
|
|
return Slot;
|
|
|
|
}
|