blob: 60b1caf0fae5a7aefca14f9597e32482c23fc175 [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
Chris Lattner79a42ac2006-12-19 21:40:18 +000019#define DEBUG_TYPE "reg2mem"
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000020#include "llvm/Transforms/Scalar.h"
21#include "llvm/Transforms/Utils/Local.h"
22#include "llvm/Pass.h"
23#include "llvm/Function.h"
24#include "llvm/Module.h"
25#include "llvm/BasicBlock.h"
26#include "llvm/Instructions.h"
27#include "llvm/ADT/Statistic.h"
Reid Spencer557ab152007-02-05 23:32:05 +000028#include "llvm/Support/Compiler.h"
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000029#include <list>
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000030using namespace llvm;
31
Chris Lattner79a42ac2006-12-19 21:40:18 +000032STATISTIC(NumDemoted, "Number of registers demoted");
33
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000034namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000035 struct VISIBILITY_HIDDEN RegToMem : public FunctionPass {
Devang Patel8c78a0b2007-05-03 01:11:54 +000036 static char ID; // Pass identifcation, replacement for typeid
Devang Patel09f162c2007-05-01 21:15:47 +000037 RegToMem() : FunctionPass((intptr_t)&ID) {}
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000038
Andrew Lenharth71b09bb2005-11-22 21:45:19 +000039 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequiredID(BreakCriticalEdgesID);
Andrew Lenharth5fc37942005-11-25 16:04:54 +000041 AU.addPreservedID(BreakCriticalEdgesID);
Andrew Lenharth71b09bb2005-11-22 21:45:19 +000042 }
43
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000044 bool valueEscapes(Instruction* i) {
45 BasicBlock* bb = i->getParent();
46 for(Value::use_iterator ii = i->use_begin(), ie = i->use_end();
47 ii != ie; ++ii)
Andrew Lenharthd9c13b12005-11-10 19:39:09 +000048 if (cast<Instruction>(*ii)->getParent() != bb ||
Anton Korobeynikovfb801512007-04-16 18:10:23 +000049 isa<PHINode>(*ii))
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000050 return true;
51 return false;
52 }
53
54 virtual bool runOnFunction(Function &F) {
Reid Spencer5301e7c2007-01-30 20:08:39 +000055 if (!F.isDeclaration()) {
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000056 //give us a clean block
Anton Korobeynikovfb801512007-04-16 18:10:23 +000057 BasicBlock* bbold = &F.getEntryBlock();
Chris Lattner361e9812007-05-05 22:32:24 +000058 BasicBlock* bbnew = new BasicBlock("allocablock", &F,
59 &F.getEntryBlock());
Anton Korobeynikovfb801512007-04-16 18:10:23 +000060 new BranchInst(bbold, bbnew);
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000061
62 //find the instructions
63 std::list<Instruction*> worklist;
64 for (Function::iterator ibb = F.begin(), ibe = F.end();
65 ibb != ibe; ++ibb)
66 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
67 iib != iie; ++iib) {
68 if(valueEscapes(iib))
69 worklist.push_front(&*iib);
70 }
71 //demote escaped instructions
72 NumDemoted += worklist.size();
73 for (std::list<Instruction*>::iterator ilb = worklist.begin(),
74 ile = worklist.end(); ilb != ile; ++ilb)
75 DemoteRegToStack(**ilb, false);
76 return true;
77 }
78 return false;
79 }
80 };
81
Devang Patel8c78a0b2007-05-03 01:11:54 +000082 char RegToMem::ID = 0;
Chris Lattnerc2d3d312006-08-27 22:42:52 +000083 RegisterPass<RegToMem> X("reg2mem", "Demote all values to stack slots");
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000084}
85
86// createDemoteRegisterToMemory - Provide an entry point to create this pass.
87//
Andrew Lenharth061029d2005-11-22 22:14:23 +000088const PassInfo *llvm::DemoteRegisterToMemoryID = X.getPassInfo();
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000089FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
90 return new RegToMem();
91}