blob: b0db3177d8f40bb94963097daa93c348529e5455 [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"
Reid Spencer9133fe22007-02-05 23:32:05 +000029#include "llvm/Support/Compiler.h"
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000030#include "llvm/Support/CFG.h"
Andrew Lenharth183119c2005-11-10 01:58:38 +000031#include <list>
Andrew Lenharth183119c2005-11-10 01:58:38 +000032using namespace llvm;
33
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000034STATISTIC(NumRegsDemoted, "Number of registers demoted");
35STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
Chris Lattner0e5f4992006-12-19 21:40:18 +000036
Andrew Lenharth183119c2005-11-10 01:58:38 +000037namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000038 struct VISIBILITY_HIDDEN RegToMem : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000040 RegToMem() : FunctionPass(&ID) {}
Andrew Lenharth183119c2005-11-10 01:58:38 +000041
Andrew Lenharth7045f6c2005-11-22 21:45:19 +000042 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequiredID(BreakCriticalEdgesID);
Andrew Lenharthb0826522005-11-25 16:04:54 +000044 AU.addPreservedID(BreakCriticalEdgesID);
Andrew Lenharth7045f6c2005-11-22 21:45:19 +000045 }
46
Andrew Lenharth183119c2005-11-10 01:58:38 +000047 bool valueEscapes(Instruction* i) {
48 BasicBlock* bb = i->getParent();
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000049 for (Value::use_iterator ii = i->use_begin(), ie = i->use_end();
50 ii != ie; ++ii)
Andrew Lenharthfa25e482005-11-10 19:39:09 +000051 if (cast<Instruction>(*ii)->getParent() != bb ||
Anton Korobeynikovbed29462007-04-16 18:10:23 +000052 isa<PHINode>(*ii))
Andrew Lenharth183119c2005-11-10 01:58:38 +000053 return true;
54 return false;
55 }
56
57 virtual bool runOnFunction(Function &F) {
Reid Spencer5cbf9852007-01-30 20:08:39 +000058 if (!F.isDeclaration()) {
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000059 // Insert all new allocas into entry block.
60 BasicBlock* BBEntry = &F.getEntryBlock();
61 assert(pred_begin(BBEntry) == pred_end(BBEntry) &&
62 "Entry block to function must not have predecessors!");
Andrew Lenharth183119c2005-11-10 01:58:38 +000063
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000064 // Find first non-alloca instruction and create insertion point. This is
65 // safe if block is well-formed: it always have terminator, otherwise
66 // we'll get and assertion.
67 BasicBlock::iterator I = BBEntry->begin();
68 while (isa<AllocaInst>(I)) ++I;
69
70 CastInst *AllocaInsertionPoint =
Gabor Greif7cbd8a32008-05-16 19:29:10 +000071 CastInst::Create(Instruction::BitCast,
Owen Anderson1d0be152009-08-13 21:58:54 +000072 Constant::getNullValue(Type::getInt32Ty(F.getContext())),
73 Type::getInt32Ty(F.getContext()),
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000074 "reg2mem alloca point", I);
75
76 // Find the escaped instructions. But don't create stack slots for
77 // allocas in entry block.
Andrew Lenharth183119c2005-11-10 01:58:38 +000078 std::list<Instruction*> worklist;
79 for (Function::iterator ibb = F.begin(), ibe = F.end();
80 ibb != ibe; ++ibb)
81 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
82 iib != iie; ++iib) {
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000083 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
84 valueEscapes(iib)) {
Andrew Lenharth183119c2005-11-10 01:58:38 +000085 worklist.push_front(&*iib);
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000086 }
Andrew Lenharth183119c2005-11-10 01:58:38 +000087 }
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000088
89 // Demote escaped instructions
90 NumRegsDemoted += worklist.size();
Andrew Lenharth183119c2005-11-10 01:58:38 +000091 for (std::list<Instruction*>::iterator ilb = worklist.begin(),
92 ile = worklist.end(); ilb != ile; ++ilb)
Owen Anderson50dead02009-07-15 23:53:25 +000093 DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
Anton Korobeynikova024e8c2007-10-21 23:05:16 +000094
95 worklist.clear();
96
97 // Find all phi's
98 for (Function::iterator ibb = F.begin(), ibe = F.end();
99 ibb != ibe; ++ibb)
100 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
101 iib != iie; ++iib)
102 if (isa<PHINode>(iib))
103 worklist.push_front(&*iib);
104
105 // Demote phi nodes
106 NumPhisDemoted += worklist.size();
107 for (std::list<Instruction*>::iterator ilb = worklist.begin(),
108 ile = worklist.end(); ilb != ile; ++ilb)
Owen Anderson50dead02009-07-15 23:53:25 +0000109 DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint);
Anton Korobeynikova024e8c2007-10-21 23:05:16 +0000110
Andrew Lenharth183119c2005-11-10 01:58:38 +0000111 return true;
112 }
113 return false;
114 }
115 };
Andrew Lenharth183119c2005-11-10 01:58:38 +0000116}
Dan Gohman844731a2008-05-13 00:00:25 +0000117
118char RegToMem::ID = 0;
119static RegisterPass<RegToMem>
120X("reg2mem", "Demote all values to stack slots");
Andrew Lenharth183119c2005-11-10 01:58:38 +0000121
122// createDemoteRegisterToMemory - Provide an entry point to create this pass.
123//
Dan Gohman6ddba2b2008-05-13 02:05:11 +0000124const PassInfo *const llvm::DemoteRegisterToMemoryID = &X;
Andrew Lenharth183119c2005-11-10 01:58:38 +0000125FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
126 return new RegToMem();
127}