LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Reg2Mem.cpp
Go to the documentation of this file.
1 //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file demotes all registers to memory references. It is intended to be
11 // the inverse of PromoteMemoryToRegister. By converting to loads, the only
12 // values live across 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/ADT/Statistic.h"
22 #include "llvm/IR/BasicBlock.h"
23 #include "llvm/IR/Function.h"
24 #include "llvm/IR/Instructions.h"
25 #include "llvm/IR/LLVMContext.h"
26 #include "llvm/IR/Module.h"
27 #include "llvm/Pass.h"
28 #include "llvm/Support/CFG.h"
30 #include <list>
31 using namespace llvm;
32 
33 STATISTIC(NumRegsDemoted, "Number of registers demoted");
34 STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
35 
36 namespace {
37  struct RegToMem : public FunctionPass {
38  static char ID; // Pass identification, replacement for typeid
39  RegToMem() : FunctionPass(ID) {
41  }
42 
43  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46  }
47 
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();
51  UI != E; ++UI) {
52  const Instruction *I = cast<Instruction>(*UI);
53  if (I->getParent() != BB || isa<PHINode>(I))
54  return true;
55  }
56  return false;
57  }
58 
59  virtual bool runOnFunction(Function &F);
60  };
61 }
62 
63 char RegToMem::ID = 0;
64 INITIALIZE_PASS_BEGIN(RegToMem, "reg2mem", "Demote all values to stack slots",
65  false, false)
66 INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
67 INITIALIZE_PASS_END(RegToMem, "reg2mem", "Demote all values to stack slots",
68  false, false)
69 
70 bool 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 =
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 
129 // createDemoteRegisterToMemory - Provide an entry point to create this pass.
132  return new RegToMem();
133 }
use_iterator use_end()
Definition: Value.h:152
static PassRegistry * getPassRegistry()
char & DemoteRegisterToMemoryID
Definition: Reg2Mem.cpp:130
AllocaInst * DemoteRegToStack(Instruction &X, bool VolatileLoads=false, Instruction *AllocaPoint=0)
ValuesClass< DataType > END_WITH_NULL values(const char *Arg, DataType Val, const char *Desc,...)
Definition: CommandLine.h:510
F(f)
FunctionPass * createDemoteRegisterToMemoryPass()
Definition: Reg2Mem.cpp:131
static Constant * getNullValue(Type *Ty)
Definition: Constants.cpp:111
iterator begin()
Definition: BasicBlock.h:193
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:167
STATISTIC(NumRegsDemoted,"Number of registers demoted")
Base class of casting instructions.
Definition: InstrTypes.h:387
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:172
ID
LLVM Calling Convention Representation.
Definition: CallingConv.h:26
This class represents a no-op cast from one type to another.
AnalysisUsage & addPreservedID(const void *ID)
INITIALIZE_PASS_BEGIN(RegToMem,"reg2mem","Demote all values to stack slots", false, false) INITIALIZE_PASS_END(RegToMem
Demote all values to stack false
Definition: Reg2Mem.cpp:67
LLVM Basic Block Representation.
Definition: BasicBlock.h:72
char & BreakCriticalEdgesID
Interval::pred_iterator pred_begin(Interval *I)
Definition: Interval.h:117
Demote all values to stack slots
Definition: Reg2Mem.cpp:67
AllocaInst * DemotePHIToStack(PHINode *P, Instruction *AllocaPoint=0)
Interval::pred_iterator pred_end(Interval *I)
Definition: Interval.h:120
void initializeRegToMemPass(PassRegistry &)
AnalysisUsage & addRequiredID(const void *ID)
Definition: Pass.cpp:262
use_iterator use_begin()
Definition: Value.h:150
static IntegerType * getInt32Ty(LLVMContext &C)
Definition: Type.cpp:241
reg2mem
Definition: Reg2Mem.cpp:67
#define I(x, y, z)
Definition: MD5.cpp:54
const BasicBlock * getParent() const
Definition: Instruction.h:52