blob: b04c362a25d49cc23bc824d7fd9f2658a53e9783 [file] [log] [blame]
Andrew Lenharth4130a4f2005-11-10 01:58:38 +00001//===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file demotes all registers to memory references. It is intented to be
11// the inverse of PromoteMemoryToRegister. By converting to loads, the only
12// values live accross basic blocks are allocas and loads before phi nodes.
13// It is intended that this should make CFG hacking much easier.
14// To make later hacking easier, the entry block is split into two, such that
15// all introduced allocas and nothing else are in the entry block.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Transforms/Scalar.h"
20#include "llvm/Transforms/Utils/Local.h"
21#include "llvm/Pass.h"
22#include "llvm/Function.h"
23#include "llvm/Module.h"
24#include "llvm/BasicBlock.h"
25#include "llvm/Instructions.h"
26#include "llvm/ADT/Statistic.h"
27
28#include <list>
29
30using namespace llvm;
31
32namespace {
33 Statistic<> NumDemoted("reg2mem", "Number of registers demoted");
34
35 struct RegToMem : public FunctionPass {
36
37 bool valueEscapes(Instruction* i) {
38 BasicBlock* bb = i->getParent();
39 for(Value::use_iterator ii = i->use_begin(), ie = i->use_end();
40 ii != ie; ++ii)
Andrew Lenharthd9c13b12005-11-10 19:39:09 +000041 if (cast<Instruction>(*ii)->getParent() != bb ||
42 isa<PHINode>(*ii))
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000043 return true;
44 return false;
45 }
46
47 virtual bool runOnFunction(Function &F) {
48 if (!F.isExternal()) {
49 //give us a clean block
Andrew Lenharth8e66c0c2005-11-10 17:35:34 +000050 BasicBlock* bbold = &F.getEntryBlock();
51 BasicBlock* bbnew = new BasicBlock("allocablock", &F, &F.getEntryBlock());
52 new BranchInst(bbold, bbnew);
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000053
54 //find the instructions
55 std::list<Instruction*> worklist;
56 for (Function::iterator ibb = F.begin(), ibe = F.end();
57 ibb != ibe; ++ibb)
58 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
59 iib != iie; ++iib) {
60 if(valueEscapes(iib))
61 worklist.push_front(&*iib);
62 }
63 //demote escaped instructions
64 NumDemoted += worklist.size();
65 for (std::list<Instruction*>::iterator ilb = worklist.begin(),
66 ile = worklist.end(); ilb != ile; ++ilb)
67 DemoteRegToStack(**ilb, false);
68 return true;
69 }
70 return false;
71 }
72 };
73
74 RegisterOpt<RegToMem> X("reg2mem", "Demote all values to stack slots");
75}
76
77// createDemoteRegisterToMemory - Provide an entry point to create this pass.
78//
79FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
80 return new RegToMem();
81}