1 //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
17 //===----------------------------------------------------------------------===//
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"
33 STATISTIC(NumRegsDemoted, "Number of registers demoted");
34 STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
37 struct RegToMem : public FunctionPass {
38 static char ID; // Pass identification, replacement for typeid
39 RegToMem() : FunctionPass(ID) {
40 initializeRegToMemPass(*PassRegistry::getPassRegistry());
43 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.addRequiredID(BreakCriticalEdgesID);
45 AU.addPreservedID(BreakCriticalEdgesID);
48 bool valueEscapes(const Instruction *Inst) const {
49 const BasicBlock *BB = Inst->getParent();
50 for (Value::const_use_iterator UI = Inst->use_begin(),E = Inst->use_end();
52 const Instruction *I = cast<Instruction>(*UI);
53 if (I->getParent() != BB || isa<PHINode>(I))
59 virtual bool runOnFunction(Function &F);
63 char RegToMem::ID = 0;
64 INITIALIZE_PASS_BEGIN(RegToMem, "reg2mem", "Demote all values to stack slots",
66 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
67 INITIALIZE_PASS_END(RegToMem, "reg2mem", "Demote all values to stack slots",
70 bool RegToMem::runOnFunction(Function &F) {
71 if (F.isDeclaration())
74 // Insert all new allocas into entry block.
75 BasicBlock *BBEntry = &F.getEntryBlock();
76 assert(pred_begin(BBEntry) == pred_end(BBEntry) &&
77 "Entry block to function must not have predecessors!");
79 // Find first non-alloca instruction and create insertion point. This is
80 // safe if block is well-formed: it always have terminator, otherwise
81 // we'll get and assertion.
82 BasicBlock::iterator I = BBEntry->begin();
83 while (isa<AllocaInst>(I)) ++I;
85 CastInst *AllocaInsertionPoint =
86 new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())),
87 Type::getInt32Ty(F.getContext()),
88 "reg2mem alloca point", I);
90 // Find the escaped instructions. But don't create stack slots for
91 // allocas in entry block.
92 std::list<Instruction*> WorkList;
93 for (Function::iterator ibb = F.begin(), ibe = F.end();
95 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
97 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
99 WorkList.push_front(&*iib);
103 // Demote escaped instructions
104 NumRegsDemoted += WorkList.size();
105 for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
106 ile = WorkList.end(); ilb != ile; ++ilb)
107 DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
112 for (Function::iterator ibb = F.begin(), ibe = F.end();
114 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
116 if (isa<PHINode>(iib))
117 WorkList.push_front(&*iib);
120 NumPhisDemoted += WorkList.size();
121 for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
122 ile = WorkList.end(); ilb != ile; ++ilb)
123 DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint);
129 // createDemoteRegisterToMemory - Provide an entry point to create this pass.
131 char &llvm::DemoteRegisterToMemoryID = RegToMem::ID;
132 FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
133 return new RegToMem();