Deleted Added
full compact
Reg2Mem.cpp (195340) Reg2Mem.cpp (198090)
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//===----------------------------------------------------------------------===//

--- 12 unchanged lines hidden (view full) ---

21#include "llvm/Transforms/Utils/Local.h"
22#include "llvm/Pass.h"
23#include "llvm/Function.h"
24#include "llvm/LLVMContext.h"
25#include "llvm/Module.h"
26#include "llvm/BasicBlock.h"
27#include "llvm/Instructions.h"
28#include "llvm/ADT/Statistic.h"
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//===----------------------------------------------------------------------===//

--- 12 unchanged lines hidden (view full) ---

21#include "llvm/Transforms/Utils/Local.h"
22#include "llvm/Pass.h"
23#include "llvm/Function.h"
24#include "llvm/LLVMContext.h"
25#include "llvm/Module.h"
26#include "llvm/BasicBlock.h"
27#include "llvm/Instructions.h"
28#include "llvm/ADT/Statistic.h"
29#include "llvm/Support/Compiler.h"
30#include "llvm/Support/CFG.h"
31#include <list>
32using namespace llvm;
33
34STATISTIC(NumRegsDemoted, "Number of registers demoted");
35STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
36
37namespace {
29#include "llvm/Support/CFG.h"
30#include <list>
31using namespace llvm;
32
33STATISTIC(NumRegsDemoted, "Number of registers demoted");
34STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
35
36namespace {
38 struct VISIBILITY_HIDDEN RegToMem : public FunctionPass {
37 struct RegToMem : public FunctionPass {
39 static char ID; // Pass identification, replacement for typeid
40 RegToMem() : FunctionPass(&ID) {}
41
42 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
43 AU.addRequiredID(BreakCriticalEdgesID);
44 AU.addPreservedID(BreakCriticalEdgesID);
45 }
46
38 static char ID; // Pass identification, replacement for typeid
39 RegToMem() : FunctionPass(&ID) {}
40
41 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequiredID(BreakCriticalEdgesID);
43 AU.addPreservedID(BreakCriticalEdgesID);
44 }
45
47 bool valueEscapes(Instruction* i) {
48 BasicBlock* bb = i->getParent();
49 for (Value::use_iterator ii = i->use_begin(), ie = i->use_end();
50 ii != ie; ++ii)
51 if (cast<Instruction>(*ii)->getParent() != bb ||
52 isa<PHINode>(*ii))
46 bool valueEscapes(const Instruction *Inst) const {
47 const BasicBlock *BB = Inst->getParent();
48 for (Value::use_const_iterator UI = Inst->use_begin(),E = Inst->use_end();
49 UI != E; ++UI)
50 if (cast<Instruction>(*UI)->getParent() != BB ||
51 isa<PHINode>(*UI))
53 return true;
54 return false;
55 }
56
52 return true;
53 return false;
54 }
55
57 virtual bool runOnFunction(Function &F) {
58 if (!F.isDeclaration()) {
59 // 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!");
63
64 // 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 =
71 CastInst::Create(Instruction::BitCast,
72 Context->getNullValue(Type::Int32Ty), Type::Int32Ty,
73 "reg2mem alloca point", I);
74
75 // Find the escaped instructions. But don't create stack slots for
76 // allocas in entry block.
77 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) {
82 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
83 valueEscapes(iib)) {
84 worklist.push_front(&*iib);
85 }
86 }
87
88 // Demote escaped instructions
89 NumRegsDemoted += worklist.size();
90 for (std::list<Instruction*>::iterator ilb = worklist.begin(),
91 ile = worklist.end(); ilb != ile; ++ilb)
92 DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
93
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)
108 DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint);
109
110 return true;
111 }
112 return false;
113 }
56 virtual bool runOnFunction(Function &F);
114 };
115}
116
117char RegToMem::ID = 0;
118static RegisterPass<RegToMem>
119X("reg2mem", "Demote all values to stack slots");
120
57 };
58}
59
60char RegToMem::ID = 0;
61static RegisterPass<RegToMem>
62X("reg2mem", "Demote all values to stack slots");
63
64
65bool RegToMem::runOnFunction(Function &F) {
66 if (F.isDeclaration())
67 return false;
68
69 // Insert all new allocas into entry block.
70 BasicBlock *BBEntry = &F.getEntryBlock();
71 assert(pred_begin(BBEntry) == pred_end(BBEntry) &&
72 "Entry block to function must not have predecessors!");
73
74 // Find first non-alloca instruction and create insertion point. This is
75 // safe if block is well-formed: it always have terminator, otherwise
76 // we'll get and assertion.
77 BasicBlock::iterator I = BBEntry->begin();
78 while (isa<AllocaInst>(I)) ++I;
79
80 CastInst *AllocaInsertionPoint =
81 new BitCastInst(Constant::getNullValue(Type::getInt32Ty(F.getContext())),
82 Type::getInt32Ty(F.getContext()),
83 "reg2mem alloca point", I);
84
85 // Find the escaped instructions. But don't create stack slots for
86 // allocas in entry block.
87 std::list<Instruction*> WorkList;
88 for (Function::iterator ibb = F.begin(), ibe = F.end();
89 ibb != ibe; ++ibb)
90 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
91 iib != iie; ++iib) {
92 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
93 valueEscapes(iib)) {
94 WorkList.push_front(&*iib);
95 }
96 }
97
98 // Demote escaped instructions
99 NumRegsDemoted += WorkList.size();
100 for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
101 ile = WorkList.end(); ilb != ile; ++ilb)
102 DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
103
104 WorkList.clear();
105
106 // Find all phi's
107 for (Function::iterator ibb = F.begin(), ibe = F.end();
108 ibb != ibe; ++ibb)
109 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
110 iib != iie; ++iib)
111 if (isa<PHINode>(iib))
112 WorkList.push_front(&*iib);
113
114 // Demote phi nodes
115 NumPhisDemoted += WorkList.size();
116 for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
117 ile = WorkList.end(); ilb != ile; ++ilb)
118 DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint);
119
120 return true;
121}
122
123
121// createDemoteRegisterToMemory - Provide an entry point to create this pass.
122//
123const PassInfo *const llvm::DemoteRegisterToMemoryID = &X;
124FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
125 return new RegToMem();
126}
124// createDemoteRegisterToMemory - Provide an entry point to create this pass.
125//
126const PassInfo *const llvm::DemoteRegisterToMemoryID = &X;
127FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
128 return new RegToMem();
129}