d9809ce6ef42ce8e6cfc2bc8cef7923034552041
[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 intended to be
11 // the inverse of PromoteMemoryToRegister.  By converting to loads, the only
12 // values live across 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/ADT/Statistic.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/CFG.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/IR/Instructions.h"
26 #include "llvm/IR/LLVMContext.h"
27 #include "llvm/IR/Module.h"
28 #include "llvm/Pass.h"
29 #include "llvm/Transforms/Utils/Local.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       initializeRegToMemPass(*PassRegistry::getPassRegistry());
41     }
42
43     void getAnalysisUsage(AnalysisUsage &AU) const override {
44       AU.addRequiredID(BreakCriticalEdgesID);
45       AU.addPreservedID(BreakCriticalEdgesID);
46     }
47
48     bool valueEscapes(const Instruction *Inst) const {
49       const BasicBlock *BB = Inst->getParent();
50       for (const User *U : Inst->users()) {
51         const Instruction *UI = cast<Instruction>(U);
52         if (UI->getParent() != BB || isa<PHINode>(UI))
53           return true;
54       }
55       return false;
56     }
57
58     bool runOnFunction(Function &F) override;
59   };
60 }
61
62 char RegToMem::ID = 0;
63 INITIALIZE_PASS_BEGIN(RegToMem, "reg2mem", "Demote all values to stack slots",
64                 false, false)
65 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
66 INITIALIZE_PASS_END(RegToMem, "reg2mem", "Demote all values to stack slots",
67                 false, false)
68
69 bool RegToMem::runOnFunction(Function &F) {
70   if (F.isDeclaration())
71     return false;
72
73   // Insert all new allocas into entry block.
74   BasicBlock *BBEntry = &F.getEntryBlock();
75   assert(pred_begin(BBEntry) == pred_end(BBEntry) &&
76          "Entry block to function must not have predecessors!");
77
78   // Find first non-alloca instruction and create insertion point. This is
79   // safe if block is well-formed: it always have terminator, otherwise
80   // we'll get and assertion.
81   BasicBlock::iterator I = BBEntry->begin();
82   while (isa<AllocaInst>(I)) ++I;
83
84   CastInst *AllocaInsertionPoint =
85     new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())),
86                     Type::getInt32Ty(F.getContext()),
87                     "reg2mem alloca point", I);
88
89   // Find the escaped instructions. But don't create stack slots for
90   // allocas in entry block.
91   std::list<Instruction*> WorkList;
92   for (Function::iterator ibb = F.begin(), ibe = F.end();
93        ibb != ibe; ++ibb)
94     for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
95          iib != iie; ++iib) {
96       if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
97           valueEscapes(iib)) {
98         WorkList.push_front(&*iib);
99       }
100     }
101
102   // Demote escaped instructions
103   NumRegsDemoted += WorkList.size();
104   for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
105        ile = WorkList.end(); ilb != ile; ++ilb)
106     DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
107
108   WorkList.clear();
109
110   // Find all phi's
111   for (Function::iterator ibb = F.begin(), ibe = F.end();
112        ibb != ibe; ++ibb)
113     for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
114          iib != iie; ++iib)
115       if (isa<PHINode>(iib))
116         WorkList.push_front(&*iib);
117
118   // Demote phi nodes
119   NumPhisDemoted += WorkList.size();
120   for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
121        ile = WorkList.end(); ilb != ile; ++ilb)
122     DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint);
123
124   return true;
125 }
126
127
128 // createDemoteRegisterToMemory - Provide an entry point to create this pass.
129 char &llvm::DemoteRegisterToMemoryID = RegToMem::ID;
130 FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
131   return new RegToMem();
132 }