2003-11-07 03:46:29 +08:00
|
|
|
//===- DemoteRegToStack.cpp - Move a virtual register to the stack --------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2003-10-21 03:43:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-12-10 21:07:58 +08:00
|
|
|
|
2013-02-06 02:23:10 +08:00
|
|
|
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-07-27 09:24:00 +08:00
|
|
|
#include "llvm/Analysis/CFG.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/Type.h"
|
2014-01-07 19:48:04 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2004-01-09 14:12:26 +08:00
|
|
|
using namespace llvm;
|
2003-11-12 06:41:34 +08:00
|
|
|
|
2004-03-17 07:23:11 +08: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 10:12:54 +08:00
|
|
|
AllocaInst *llvm::DemoteRegToStack(Instruction &I, bool VolatileLoads,
|
2007-10-22 07:05:16 +08:00
|
|
|
Instruction *AllocaPoint) {
|
|
|
|
if (I.use_empty()) {
|
|
|
|
I.eraseFromParent();
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2007-10-22 07:05:16 +08:00
|
|
|
}
|
2010-06-17 06:41:09 +08:00
|
|
|
|
2004-03-17 07:23:11 +08:00
|
|
|
// Create a stack slot to hold the value.
|
2007-10-22 07:05:16 +08:00
|
|
|
AllocaInst *Slot;
|
|
|
|
if (AllocaPoint) {
|
2014-04-25 13:29:35 +08:00
|
|
|
Slot = new AllocaInst(I.getType(), nullptr,
|
2009-07-15 07:09:55 +08:00
|
|
|
I.getName()+".reg2mem", AllocaPoint);
|
2007-10-22 07:05:16 +08:00
|
|
|
} else {
|
|
|
|
Function *F = I.getParent()->getParent();
|
2014-04-25 13:29:35 +08:00
|
|
|
Slot = new AllocaInst(I.getType(), nullptr, I.getName()+".reg2mem",
|
2007-10-22 07:05:16 +08:00
|
|
|
F->getEntryBlock().begin());
|
|
|
|
}
|
2010-06-17 06:41:09 +08:00
|
|
|
|
2015-02-09 14:38:23 +08:00
|
|
|
// We cannot demote invoke instructions to the stack if their normal edge
|
|
|
|
// is critical. Therefore, split the critical edge and create a basic block
|
|
|
|
// into which the store can be inserted.
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
|
|
|
|
if (!II->getNormalDest()->getSinglePredecessor()) {
|
|
|
|
unsigned SuccNum = GetSuccessorNumber(II->getParent(), II->getNormalDest());
|
|
|
|
assert(isCriticalEdge(II, SuccNum) && "Expected a critical edge!");
|
|
|
|
BasicBlock *BB = SplitCriticalEdge(II, SuccNum);
|
|
|
|
assert(BB && "Unable to split critical edge.");
|
|
|
|
(void)BB;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-17 10:09:28 +08:00
|
|
|
// Change all of the users of the instruction to read from the stack slot.
|
2004-03-17 07:23:11 +08:00
|
|
|
while (!I.use_empty()) {
|
2014-03-09 11:16:01 +08:00
|
|
|
Instruction *U = cast<Instruction>(I.user_back());
|
2004-03-17 07:23:11 +08: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 10:09:28 +08:00
|
|
|
// use. Instead insert the load in the predecessor block corresponding
|
2004-03-17 07:23:11 +08:00
|
|
|
// to the incoming value.
|
|
|
|
//
|
|
|
|
// Note that if there are multiple edges from a basic block to this PHI
|
2012-02-17 10:09:28 +08: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 10:12:54 +08:00
|
|
|
DenseMap<BasicBlock*, Value*> Loads;
|
2004-03-17 07:23:11 +08:00
|
|
|
for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
|
|
|
|
if (PN->getIncomingValue(i) == &I) {
|
2004-04-02 04:28:45 +08:00
|
|
|
Value *&V = Loads[PN->getIncomingBlock(i)];
|
2014-04-25 13:29:35 +08:00
|
|
|
if (!V) {
|
2004-04-02 04:28:45 +08:00
|
|
|
// Insert the load into the predecessor block
|
2010-06-17 06:41:09 +08:00
|
|
|
V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads,
|
2004-04-02 04:28:45 +08:00
|
|
|
PN->getIncomingBlock(i)->getTerminator());
|
|
|
|
}
|
2004-03-17 07:23:11 +08:00
|
|
|
PN->setIncomingValue(i, V);
|
2003-09-20 22:36:23 +08:00
|
|
|
}
|
|
|
|
|
2004-03-17 07:23:11 +08:00
|
|
|
} else {
|
|
|
|
// If this is a normal instruction, just insert a load.
|
2005-09-28 03:39:00 +08:00
|
|
|
Value *V = new LoadInst(Slot, I.getName()+".reload", VolatileLoads, U);
|
2004-03-17 07:23:11 +08:00
|
|
|
U->replaceUsesOfWith(&I, V);
|
2002-12-10 21:07:58 +08:00
|
|
|
}
|
2003-09-20 22:36:23 +08:00
|
|
|
}
|
2002-12-10 21:07:58 +08:00
|
|
|
|
2012-02-17 10:09:28 +08: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-28 03:39:00 +08:00
|
|
|
BasicBlock::iterator InsertPt;
|
2004-03-17 07:23:11 +08:00
|
|
|
if (!isa<TerminatorInst>(I)) {
|
2005-09-28 03:39:00 +08:00
|
|
|
InsertPt = &I;
|
2005-10-04 08:44:01 +08:00
|
|
|
++InsertPt;
|
2015-02-09 14:38:23 +08:00
|
|
|
for (; isa<PHINode>(InsertPt) || isa<LandingPadInst>(InsertPt); ++InsertPt)
|
|
|
|
/* empty */; // Don't insert before PHI nodes or landingpad instrs.
|
2004-03-17 07:23:11 +08:00
|
|
|
} else {
|
2005-09-28 03:39:00 +08:00
|
|
|
InvokeInst &II = cast<InvokeInst>(I);
|
2015-02-09 14:38:23 +08:00
|
|
|
InsertPt = II.getNormalDest()->getFirstInsertionPt();
|
2004-03-17 07:23:11 +08:00
|
|
|
}
|
2003-11-07 03:46:29 +08:00
|
|
|
|
2012-02-17 10:09:28 +08:00
|
|
|
new StoreInst(&I, Slot, InsertPt);
|
2004-03-17 07:23:11 +08:00
|
|
|
return Slot;
|
2002-12-10 21:07:58 +08:00
|
|
|
}
|
2007-07-12 02:41:34 +08:00
|
|
|
|
2012-02-17 10:09:28 +08: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 10:12:54 +08:00
|
|
|
AllocaInst *llvm::DemotePHIToStack(PHINode *P, Instruction *AllocaPoint) {
|
2007-07-12 02:41:34 +08:00
|
|
|
if (P->use_empty()) {
|
2010-06-17 06:41:09 +08:00
|
|
|
P->eraseFromParent();
|
2014-04-25 13:29:35 +08:00
|
|
|
return nullptr;
|
2007-07-12 02:41:34 +08:00
|
|
|
}
|
2007-10-22 07:05:16 +08:00
|
|
|
|
2007-07-12 02:41:34 +08:00
|
|
|
// Create a stack slot to hold the value.
|
2007-10-22 07:05:16 +08:00
|
|
|
AllocaInst *Slot;
|
|
|
|
if (AllocaPoint) {
|
2014-04-25 13:29:35 +08:00
|
|
|
Slot = new AllocaInst(P->getType(), nullptr,
|
2009-07-15 07:09:55 +08:00
|
|
|
P->getName()+".reg2mem", AllocaPoint);
|
2007-10-22 07:05:16 +08:00
|
|
|
} else {
|
|
|
|
Function *F = P->getParent()->getParent();
|
2014-04-25 13:29:35 +08:00
|
|
|
Slot = new AllocaInst(P->getType(), nullptr, P->getName()+".reg2mem",
|
2007-10-22 07:05:16 +08:00
|
|
|
F->getEntryBlock().begin());
|
|
|
|
}
|
2010-06-17 06:41:09 +08:00
|
|
|
|
2012-02-17 10:09:28 +08:00
|
|
|
// Iterate over each operand inserting a store in each predecessor.
|
2007-07-12 02:41:34 +08:00
|
|
|
for (unsigned i = 0, e = P->getNumIncomingValues(); i < e; ++i) {
|
|
|
|
if (InvokeInst *II = dyn_cast<InvokeInst>(P->getIncomingValue(i))) {
|
2010-06-17 06:41:09 +08:00
|
|
|
assert(II->getParent() != P->getIncomingBlock(i) &&
|
2010-12-23 08:58:24 +08:00
|
|
|
"Invoke edge not supported yet"); (void)II;
|
2007-07-12 02:41:34 +08:00
|
|
|
}
|
2010-06-17 06:41:09 +08:00
|
|
|
new StoreInst(P->getIncomingValue(i), Slot,
|
2007-07-12 02:41:34 +08:00
|
|
|
P->getIncomingBlock(i)->getTerminator());
|
|
|
|
}
|
2010-06-17 06:41:09 +08:00
|
|
|
|
2012-02-17 10:09:28 +08:00
|
|
|
// Insert a load in place of the PHI and replace all uses.
|
2013-01-08 18:51:32 +08: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-12 02:41:34 +08:00
|
|
|
P->replaceAllUsesWith(V);
|
2010-06-17 06:41:09 +08:00
|
|
|
|
2012-02-17 10:09:28 +08:00
|
|
|
// Delete PHI.
|
2007-07-12 02:41:34 +08:00
|
|
|
P->eraseFromParent();
|
|
|
|
return Slot;
|
|
|
|
}
|