blob: 23d6ff5005583184c1c6629025087b321e80d3af [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
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"
Owen Andersonfa089ab2009-07-03 19:42:02 +000024#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Module.h"
26#include "llvm/BasicBlock.h"
27#include "llvm/Instructions.h"
28#include "llvm/ADT/Statistic.h"
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000029#include "llvm/Support/CFG.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000030#include <list>
31using namespace llvm;
32
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000033STATISTIC(NumRegsDemoted, "Number of registers demoted");
34STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000035
36namespace {
Chris Lattnerfa2d1ba2009-09-02 06:11:42 +000037 struct RegToMem : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000038 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000039 RegToMem() : FunctionPass(&ID) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequiredID(BreakCriticalEdgesID);
43 AU.addPreservedID(BreakCriticalEdgesID);
44 }
45
46 bool valueEscapes(Instruction* i) {
47 BasicBlock* bb = i->getParent();
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000048 for (Value::use_iterator ii = i->use_begin(), ie = i->use_end();
49 ii != ie; ++ii)
Dan Gohmanf17a25c2007-07-18 16:29:46 +000050 if (cast<Instruction>(*ii)->getParent() != bb ||
51 isa<PHINode>(*ii))
52 return true;
53 return false;
54 }
55
56 virtual bool runOnFunction(Function &F) {
57 if (!F.isDeclaration()) {
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000058 // Insert all new allocas into entry block.
59 BasicBlock* BBEntry = &F.getEntryBlock();
60 assert(pred_begin(BBEntry) == pred_end(BBEntry) &&
61 "Entry block to function must not have predecessors!");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000063 // Find first non-alloca instruction and create insertion point. This is
64 // safe if block is well-formed: it always have terminator, otherwise
65 // we'll get and assertion.
66 BasicBlock::iterator I = BBEntry->begin();
67 while (isa<AllocaInst>(I)) ++I;
68
69 CastInst *AllocaInsertionPoint =
Gabor Greifa645dd32008-05-16 19:29:10 +000070 CastInst::Create(Instruction::BitCast,
Owen Anderson35b47072009-08-13 21:58:54 +000071 Constant::getNullValue(Type::getInt32Ty(F.getContext())),
72 Type::getInt32Ty(F.getContext()),
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000073 "reg2mem alloca point", I);
74
75 // Find the escaped instructions. But don't create stack slots for
76 // allocas in entry block.
Dan Gohmanf17a25c2007-07-18 16:29:46 +000077 std::list<Instruction*> worklist;
78 for (Function::iterator ibb = F.begin(), ibe = F.end();
79 ibb != ibe; ++ibb)
80 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
81 iib != iie; ++iib) {
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000082 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
83 valueEscapes(iib)) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 worklist.push_front(&*iib);
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000085 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 }
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000087
88 // Demote escaped instructions
89 NumRegsDemoted += worklist.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000090 for (std::list<Instruction*>::iterator ilb = worklist.begin(),
91 ile = worklist.end(); ilb != ile; ++ilb)
Owen Anderson140166d2009-07-15 23:53:25 +000092 DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
Anton Korobeynikov3224cda2007-10-21 23:05:16 +000093
94 worklist.clear();
95
96 // Find all phi's
97 for (Function::iterator ibb = F.begin(), ibe = F.end();
98 ibb != ibe; ++ibb)
99 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
100 iib != iie; ++iib)
101 if (isa<PHINode>(iib))
102 worklist.push_front(&*iib);
103
104 // Demote phi nodes
105 NumPhisDemoted += worklist.size();
106 for (std::list<Instruction*>::iterator ilb = worklist.begin(),
107 ile = worklist.end(); ilb != ile; ++ilb)
Owen Anderson140166d2009-07-15 23:53:25 +0000108 DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint);
Anton Korobeynikov3224cda2007-10-21 23:05:16 +0000109
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 return true;
111 }
112 return false;
113 }
114 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115}
Dan Gohman089efff2008-05-13 00:00:25 +0000116
117char RegToMem::ID = 0;
118static RegisterPass<RegToMem>
119X("reg2mem", "Demote all values to stack slots");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
121// createDemoteRegisterToMemory - Provide an entry point to create this pass.
122//
Dan Gohman66a636e2008-05-13 02:05:11 +0000123const PassInfo *const llvm::DemoteRegisterToMemoryID = &X;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
125 return new RegToMem();
126}