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
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-10 13:07:58 +00:00
|
|
|
|
2013-02-05 18:23:10 +00:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2012-12-03 16:50:05 +00:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-07-27 01:24:00 +00:00
|
|
|
#include "llvm/Analysis/CFG.h"
|
2013-01-02 11:36:10 +00:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2014-01-07 11:48:04 +00:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
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.
|
2012-02-17 02:12:54 +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();
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
2007-10-21 23:05:16 +00:00
|
|
|
}
|
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) {
|
2014-04-25 05:29:35 +00:00
|
|
|
Slot = new AllocaInst(I.getType(), nullptr,
|
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();
|
2014-04-25 05:29:35 +00:00
|
|
|
Slot = new AllocaInst(I.getType(), nullptr, I.getName()+".reg2mem",
|
2007-10-21 23:05:16 +00:00
|
|
|
F->getEntryBlock().begin());
|
|
|
|
}
|
2010-06-16 22:41:09 +00:00
|
|
|
|
2012-02-17 02:09:28 +00:00
|
|
|
// Change all of the users of the instruction to read from the stack slot.
|
2004-03-16 23:23:11 +00:00
|
|
|
while (!I.use_empty()) {
|
2014-03-09 03:16:01 +00:00
|
|
|
Instruction *U = cast<Instruction>(I.user_back());
|
2004-03-16 23:23:11 +00:00
|
|
|
if (PHINode *PN = dyn_cast<PHINode>(U)) {
|
|
|
|
// If this is a PHI node, we can't insert a load of the value before the
|
2012-02-17 02:09:28 +00:00
|
|
|
// use. Instead insert the load in the predecessor block corresponding
|
2004-03-16 23:23:11 +00:00
|
|
|
// to the incoming value.
|
|
|
|
//
|
|
|
|
// Note that if there are multiple edges from a basic block to this PHI
|
2012-02-17 02:09:28 +00:00
|
|
|
// node that we cannot have multiple loads. The problem is that the
|
|
|
|
// resulting 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 of and reuse loads we insert.
|
2012-02-17 02:12:54 +00:00
|
|
|
DenseMap<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)];
|
2014-04-25 05:29:35 +00:00
|
|
|
if (!V) {
|
2004-04-01 20:28:45 +00:00
|
|
|
// 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
|
|
|
|
|
|
|
|
2012-02-17 02:09:28 +00:00
|
|
|
// Insert stores of the computed value into the stack slot. We have to be
|
|
|
|
// careful if I is an invoke instruction, 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
|
|
|
InvokeInst &II = cast<InvokeInst>(I);
|
2013-02-05 18:23:10 +00:00
|
|
|
if (II.getNormalDest()->getSinglePredecessor())
|
|
|
|
InsertPt = II.getNormalDest()->getFirstInsertionPt();
|
|
|
|
else {
|
|
|
|
// We cannot demote invoke instructions to the stack if their normal edge
|
|
|
|
// is critical. Therefore, split the critical edge and insert the store
|
|
|
|
// in the newly created basic block.
|
|
|
|
unsigned SuccNum = GetSuccessorNumber(I.getParent(), II.getNormalDest());
|
|
|
|
TerminatorInst *TI = &cast<TerminatorInst>(I);
|
|
|
|
assert (isCriticalEdge(TI, SuccNum) &&
|
|
|
|
"Expected a critical edge!");
|
|
|
|
BasicBlock *BB = SplitCriticalEdge(TI, SuccNum);
|
|
|
|
assert (BB && "Unable to split critical edge.");
|
|
|
|
InsertPt = BB->getFirstInsertionPt();
|
|
|
|
}
|
2004-03-16 23:23:11 +00:00
|
|
|
}
|
2003-11-06 19:46:29 +00:00
|
|
|
|
2011-11-07 19:38:34 +00:00
|
|
|
for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
|
2012-02-17 02:09:28 +00:00
|
|
|
/* empty */; // Don't insert before PHI nodes or landingpad instrs.
|
2005-09-27 19:39:00 +00:00
|
|
|
|
2012-02-17 02:09:28 +00:00
|
|
|
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
|
|
|
|
2012-02-17 02:09:28 +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. It returns the pointer to the alloca inserted.
|
2012-02-17 02:12:54 +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();
|
2014-04-25 05:29:35 +00:00
|
|
|
return nullptr;
|
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) {
|
2014-04-25 05:29:35 +00:00
|
|
|
Slot = new AllocaInst(P->getType(), nullptr,
|
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();
|
2014-04-25 05:29:35 +00:00
|
|
|
Slot = new AllocaInst(P->getType(), nullptr, P->getName()+".reg2mem",
|
2007-10-21 23:05:16 +00:00
|
|
|
F->getEntryBlock().begin());
|
|
|
|
}
|
2010-06-16 22:41:09 +00:00
|
|
|
|
2012-02-17 02:09:28 +00:00
|
|
|
// Iterate over each operand inserting a store in each predecessor.
|
2007-07-11 18:41:34 +00:00
|
|
|
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
|
|
|
|
2012-02-17 02:09:28 +00:00
|
|
|
// Insert a load in place of the PHI and replace all uses.
|
2013-01-08 10:51:32 +00:00
|
|
|
BasicBlock::iterator InsertPt = P;
|
|
|
|
|
|
|
|
for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
|
|
|
|
/* empty */; // Don't insert before PHI nodes or landingpad instrs.
|
|
|
|
|
|
|
|
Value *V = new LoadInst(Slot, P->getName()+".reload", InsertPt);
|
2007-07-11 18:41:34 +00:00
|
|
|
P->replaceAllUsesWith(V);
|
2010-06-16 22:41:09 +00:00
|
|
|
|
2012-02-17 02:09:28 +00:00
|
|
|
// Delete PHI.
|
2007-07-11 18:41:34 +00:00
|
|
|
P->eraseFromParent();
|
|
|
|
return Slot;
|
|
|
|
}
|