The pass everyone has been waiting for!
[oota-llvm.git] / lib / Transforms / Scalar / Reg2Mem.cpp
1 //===- 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
30 using namespace llvm;
31
32 namespace {
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)
41         if (cast<Instruction>(*ii)->getParent() != bb)
42           return true;
43       return false;
44     }
45
46     virtual bool runOnFunction(Function &F) {
47       if (!F.isExternal()) {
48         //give us a clean block
49         BasicBlock& bbhead = F.getEntryBlock();
50         bbhead.splitBasicBlock(bbhead.begin());
51
52         //find the instructions
53         std::list<Instruction*> worklist;
54         for (Function::iterator ibb = F.begin(), ibe = F.end();
55              ibb != ibe; ++ibb)
56           for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
57                iib != iie; ++iib) {
58             if(valueEscapes(iib))
59               worklist.push_front(&*iib);
60           }
61         //demote escaped instructions
62         NumDemoted += worklist.size();
63         for (std::list<Instruction*>::iterator ilb = worklist.begin(), 
64                ile = worklist.end(); ilb != ile; ++ilb)
65           DemoteRegToStack(**ilb, false);
66         return true;
67       }
68       return false;
69     }
70   };
71   
72   RegisterOpt<RegToMem> X("reg2mem", "Demote all values to stack slots");
73 }
74
75 // createDemoteRegisterToMemory - Provide an entry point to create this pass.
76 //
77 FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
78   return new RegToMem();
79 }