this works with backedges to the existing entry block alot better
[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* bbold = &F.getEntryBlock();
50         BasicBlock* bbnew = new BasicBlock("allocablock", &F, &F.getEntryBlock());
51         new BranchInst(bbold, bbnew);
52
53         //find the instructions
54         std::list<Instruction*> worklist;
55         for (Function::iterator ibb = F.begin(), ibe = F.end();
56              ibb != ibe; ++ibb)
57           for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
58                iib != iie; ++iib) {
59             if(valueEscapes(iib))
60               worklist.push_front(&*iib);
61           }
62         //demote escaped instructions
63         NumDemoted += worklist.size();
64         for (std::list<Instruction*>::iterator ilb = worklist.begin(), 
65                ile = worklist.end(); ilb != ile; ++ilb)
66           DemoteRegToStack(**ilb, false);
67         return true;
68       }
69       return false;
70     }
71   };
72   
73   RegisterOpt<RegToMem> X("reg2mem", "Demote all values to stack slots");
74 }
75
76 // createDemoteRegisterToMemory - Provide an entry point to create this pass.
77 //
78 FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
79   return new RegToMem();
80 }