blob: 13222ac22004e27f0c9336322b62ce0c4a9ce3f6 [file] [log] [blame]
Andrew Lenharth183119c2005-11-10 01:58:38 +00001//===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Andrew Lenharth183119c2005-11-10 01:58:38 +00007//
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
Chris Lattner0e5f4992006-12-19 21:40:18 +000019#define DEBUG_TYPE "reg2mem"
Andrew Lenharth183119c2005-11-10 01:58:38 +000020#include "llvm/Transforms/Scalar.h"
21#include "llvm/Transforms/Utils/Local.h"
22#include "llvm/Pass.h"
23#include "llvm/Function.h"
Owen Andersonfa5cbd62009-07-03 19:42:02 +000024#include "llvm/LLVMContext.h"
Andrew Lenharth183119c2005-11-10 01:58:38 +000025#include "llvm/Module.h"
26#include "llvm/BasicBlock.h"
27#include "llvm/Instructions.h"
28#include "llvm/ADT/Statistic.h"
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000029#include "llvm/Support/CFG.h"
Andrew Lenharth183119c2005-11-10 01:58:38 +000030#include <list>
Andrew Lenharth183119c2005-11-10 01:58:38 +000031using namespace llvm;
32
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000033STATISTIC(NumRegsDemoted, "Number of registers demoted");
34STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
Chris Lattner0e5f4992006-12-19 21:40:18 +000035
Andrew Lenharth183119c2005-11-10 01:58:38 +000036namespace {
Chris Lattner3e8b6632009-09-02 06:11:42 +000037 struct RegToMem : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000039 RegToMem() : FunctionPass(&ID) {}
Andrew Lenharth183119c2005-11-10 01:58:38 +000040
Andrew Lenharth7045f6c2005-11-22 21:45:19 +000041 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequiredID(BreakCriticalEdgesID);
Andrew Lenharthb0826522005-11-25 16:04:54 +000043 AU.addPreservedID(BreakCriticalEdgesID);
Andrew Lenharth7045f6c2005-11-22 21:45:19 +000044 }
45
Chris Lattner20a2faa2009-09-02 06:15:37 +000046 bool valueEscapes(const Instruction *Inst) const {
47 const BasicBlock *BB = Inst->getParent();
Gabor Greif60ad7812010-03-25 23:06:16 +000048 for (Value::const_use_iterator UI = Inst->use_begin(),E = Inst->use_end();
Gabor Greif67894a32010-04-14 16:48:56 +000049 UI != E; ++UI) {
50 const Instruction *I = cast<Instruction>(*UI);
51 if (I->getParent() != BB || isa<PHINode>(I))
Andrew Lenharth183119c2005-11-10 01:58:38 +000052 return true;
Gabor Greif67894a32010-04-14 16:48:56 +000053 }
Andrew Lenharth183119c2005-11-10 01:58:38 +000054 return false;
55 }
56
Chris Lattner20a2faa2009-09-02 06:15:37 +000057 virtual bool runOnFunction(Function &F);
Andrew Lenharth183119c2005-11-10 01:58:38 +000058 };
Andrew Lenharth183119c2005-11-10 01:58:38 +000059}
Dan Gohman844731a2008-05-13 00:00:25 +000060
61char RegToMem::ID = 0;
62static RegisterPass<RegToMem>
63X("reg2mem", "Demote all values to stack slots");
Andrew Lenharth183119c2005-11-10 01:58:38 +000064
Chris Lattner20a2faa2009-09-02 06:15:37 +000065
66bool RegToMem::runOnFunction(Function &F) {
67 if (F.isDeclaration())
68 return false;
69
70 // Insert all new allocas into entry block.
71 BasicBlock *BBEntry = &F.getEntryBlock();
72 assert(pred_begin(BBEntry) == pred_end(BBEntry) &&
73 "Entry block to function must not have predecessors!");
74
75 // Find first non-alloca instruction and create insertion point. This is
76 // safe if block is well-formed: it always have terminator, otherwise
77 // we'll get and assertion.
78 BasicBlock::iterator I = BBEntry->begin();
79 while (isa<AllocaInst>(I)) ++I;
80
81 CastInst *AllocaInsertionPoint =
82 new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())),
83 Type::getInt32Ty(F.getContext()),
84 "reg2mem alloca point", I);
85
86 // Find the escaped instructions. But don't create stack slots for
87 // allocas in entry block.
88 std::list<Instruction*> WorkList;
89 for (Function::iterator ibb = F.begin(), ibe = F.end();
90 ibb != ibe; ++ibb)
91 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
92 iib != iie; ++iib) {
93 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
94 valueEscapes(iib)) {
95 WorkList.push_front(&*iib);
96 }
97 }
98
99 // Demote escaped instructions
100 NumRegsDemoted += WorkList.size();
101 for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
102 ile = WorkList.end(); ilb != ile; ++ilb)
103 DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
104
105 WorkList.clear();
106
107 // Find all phi's
108 for (Function::iterator ibb = F.begin(), ibe = F.end();
109 ibb != ibe; ++ibb)
110 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
111 iib != iie; ++iib)
112 if (isa<PHINode>(iib))
113 WorkList.push_front(&*iib);
114
115 // Demote phi nodes
116 NumPhisDemoted += WorkList.size();
117 for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
118 ile = WorkList.end(); ilb != ile; ++ilb)
119 DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint);
120
121 return true;
122}
123
124
Andrew Lenharth183119c2005-11-10 01:58:38 +0000125// createDemoteRegisterToMemory - Provide an entry point to create this pass.
126//
Dan Gohman6ddba2b2008-05-13 02:05:11 +0000127const PassInfo *const llvm::DemoteRegisterToMemoryID = &X;
Andrew Lenharth183119c2005-11-10 01:58:38 +0000128FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
129 return new RegToMem();
130}