e1ed060bfd2add19c4e800126725d9f3d1655d7c
[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 is distributed under the University of Illinois Open Source
6 // 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 #define DEBUG_TYPE "reg2mem"
20 #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/LLVMContext.h"
25 #include "llvm/Module.h"
26 #include "llvm/BasicBlock.h"
27 #include "llvm/Instructions.h"
28 #include "llvm/ADT/Statistic.h"
29 #include "llvm/Support/CFG.h"
30 #include <list>
31 using namespace llvm;
32
33 STATISTIC(NumRegsDemoted, "Number of registers demoted");
34 STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
35
36 namespace {
37   struct RegToMem : public FunctionPass {
38     static char ID; // Pass identification, replacement for typeid
39     RegToMem() : FunctionPass(ID) {}
40
41     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42       AU.addRequiredID(BreakCriticalEdgesID);
43       AU.addPreservedID(BreakCriticalEdgesID);
44     }
45
46    bool valueEscapes(const Instruction *Inst) const {
47      const BasicBlock *BB = Inst->getParent();
48       for (Value::const_use_iterator UI = Inst->use_begin(),E = Inst->use_end();
49            UI != E; ++UI) {
50         const Instruction *I = cast<Instruction>(*UI);
51         if (I->getParent() != BB || isa<PHINode>(I))
52           return true;
53       }
54       return false;
55     }
56
57     virtual bool runOnFunction(Function &F);
58   };
59 }
60   
61 char RegToMem::ID = 0;
62 INITIALIZE_PASS_BEGIN(RegToMem, "reg2mem", "Demote all values to stack slots",
63                 false, false)
64 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
65 INITIALIZE_PASS_END(RegToMem, "reg2mem", "Demote all values to stack slots",
66                 false, false)
67
68 bool RegToMem::runOnFunction(Function &F) {
69   if (F.isDeclaration()) 
70     return false;
71   
72   // Insert all new allocas into entry block.
73   BasicBlock *BBEntry = &F.getEntryBlock();
74   assert(pred_begin(BBEntry) == pred_end(BBEntry) &&
75          "Entry block to function must not have predecessors!");
76   
77   // Find first non-alloca instruction and create insertion point. This is
78   // safe if block is well-formed: it always have terminator, otherwise
79   // we'll get and assertion.
80   BasicBlock::iterator I = BBEntry->begin();
81   while (isa<AllocaInst>(I)) ++I;
82   
83   CastInst *AllocaInsertionPoint =
84     new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())),
85                     Type::getInt32Ty(F.getContext()),
86                     "reg2mem alloca point", I);
87   
88   // Find the escaped instructions. But don't create stack slots for
89   // allocas in entry block.
90   std::list<Instruction*> WorkList;
91   for (Function::iterator ibb = F.begin(), ibe = F.end();
92        ibb != ibe; ++ibb)
93     for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
94          iib != iie; ++iib) {
95       if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
96           valueEscapes(iib)) {
97         WorkList.push_front(&*iib);
98       }
99     }
100   
101   // Demote escaped instructions
102   NumRegsDemoted += WorkList.size();
103   for (std::list<Instruction*>::iterator ilb = WorkList.begin(), 
104        ile = WorkList.end(); ilb != ile; ++ilb)
105     DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
106   
107   WorkList.clear();
108   
109   // Find all phi's
110   for (Function::iterator ibb = F.begin(), ibe = F.end();
111        ibb != ibe; ++ibb)
112     for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
113          iib != iie; ++iib)
114       if (isa<PHINode>(iib))
115         WorkList.push_front(&*iib);
116   
117   // Demote phi nodes
118   NumPhisDemoted += WorkList.size();
119   for (std::list<Instruction*>::iterator ilb = WorkList.begin(), 
120        ile = WorkList.end(); ilb != ile; ++ilb)
121     DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint);
122   
123   return true;
124 }
125
126
127 // createDemoteRegisterToMemory - Provide an entry point to create this pass.
128 //
129 char &llvm::DemoteRegisterToMemoryID = RegToMem::ID;
130 FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
131   return new RegToMem();
132 }