blob: 98b0d5f6d570207b10720bf154d6c90ec7fc89df [file] [log] [blame]
Andrew Lenharth4130a4f2005-11-10 01:58:38 +00001//===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lenharth4130a4f2005-11-10 01:58:38 +00007//
8//===----------------------------------------------------------------------===//
9//
Benjamin Kramerbde91762012-06-02 10:20:22 +000010// This file demotes all registers to memory references. It is intended to be
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000011// the inverse of PromoteMemoryToRegister. By converting to loads, the only
Chris Lattner0ab5e2c2011-04-15 05:18:47 +000012// values live across basic blocks are allocas and loads before phi nodes.
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000013// 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 Lattner79a42ac2006-12-19 21:40:18 +000019#define DEBUG_TYPE "reg2mem"
Andrew Lenharth4130a4f2005-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 Anderson340288c2009-07-03 19:42:02 +000024#include "llvm/LLVMContext.h"
Andrew Lenharth4130a4f2005-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 Korobeynikov7499a3b2007-10-21 23:05:16 +000029#include "llvm/Support/CFG.h"
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000030#include <list>
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000031using namespace llvm;
32
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000033STATISTIC(NumRegsDemoted, "Number of registers demoted");
34STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
Chris Lattner79a42ac2006-12-19 21:40:18 +000035
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000036namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +000037 struct RegToMem : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000038 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000039 RegToMem() : FunctionPass(ID) {
40 initializeRegToMemPass(*PassRegistry::getPassRegistry());
41 }
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000042
Andrew Lenharth71b09bb2005-11-22 21:45:19 +000043 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
44 AU.addRequiredID(BreakCriticalEdgesID);
Andrew Lenharth5fc37942005-11-25 16:04:54 +000045 AU.addPreservedID(BreakCriticalEdgesID);
Andrew Lenharth71b09bb2005-11-22 21:45:19 +000046 }
47
Chris Lattner09a79dc2009-09-02 06:15:37 +000048 bool valueEscapes(const Instruction *Inst) const {
49 const BasicBlock *BB = Inst->getParent();
Gabor Greifc78d7202010-03-25 23:06:16 +000050 for (Value::const_use_iterator UI = Inst->use_begin(),E = Inst->use_end();
Gabor Greifc08e5df2010-04-14 16:48:56 +000051 UI != E; ++UI) {
52 const Instruction *I = cast<Instruction>(*UI);
53 if (I->getParent() != BB || isa<PHINode>(I))
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000054 return true;
Gabor Greifc08e5df2010-04-14 16:48:56 +000055 }
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000056 return false;
57 }
58
Chris Lattner09a79dc2009-09-02 06:15:37 +000059 virtual bool runOnFunction(Function &F);
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000060 };
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000061}
Dan Gohmand78c4002008-05-13 00:00:25 +000062
63char RegToMem::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000064INITIALIZE_PASS_BEGIN(RegToMem, "reg2mem", "Demote all values to stack slots",
Owen Andersondf7a4f22010-10-07 22:25:06 +000065 false, false)
Owen Anderson8ac477f2010-10-12 19:48:12 +000066INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
67INITIALIZE_PASS_END(RegToMem, "reg2mem", "Demote all values to stack slots",
68 false, false)
Chris Lattner09a79dc2009-09-02 06:15:37 +000069
70bool RegToMem::runOnFunction(Function &F) {
71 if (F.isDeclaration())
72 return false;
73
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!");
78
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;
84
85 CastInst *AllocaInsertionPoint =
86 new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())),
87 Type::getInt32Ty(F.getContext()),
88 "reg2mem alloca point", I);
89
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();
94 ibb != ibe; ++ibb)
95 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
96 iib != iie; ++iib) {
97 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
98 valueEscapes(iib)) {
99 WorkList.push_front(&*iib);
100 }
101 }
102
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);
108
109 WorkList.clear();
110
111 // Find all phi's
112 for (Function::iterator ibb = F.begin(), ibe = F.end();
113 ibb != ibe; ++ibb)
114 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
115 iib != iie; ++iib)
116 if (isa<PHINode>(iib))
117 WorkList.push_front(&*iib);
118
119 // Demote phi nodes
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);
124
125 return true;
126}
127
128
Andrew Lenharth4130a4f2005-11-10 01:58:38 +0000129// createDemoteRegisterToMemory - Provide an entry point to create this pass.
130//
Owen Andersona7aed182010-08-06 18:33:48 +0000131char &llvm::DemoteRegisterToMemoryID = RegToMem::ID;
Andrew Lenharth4130a4f2005-11-10 01:58:38 +0000132FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
133 return new RegToMem();
134}