1*9880d681SAndroid Build Coastguard Worker //===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file demotes all registers to memory references. It is intended to be
11*9880d681SAndroid Build Coastguard Worker // the inverse of PromoteMemoryToRegister. By converting to loads, the only
12*9880d681SAndroid Build Coastguard Worker // values live across basic blocks are allocas and loads before phi nodes.
13*9880d681SAndroid Build Coastguard Worker // It is intended that this should make CFG hacking much easier.
14*9880d681SAndroid Build Coastguard Worker // To make later hacking easier, the entry block is split into two, such that
15*9880d681SAndroid Build Coastguard Worker // all introduced allocas and nothing else are in the entry block.
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
18*9880d681SAndroid Build Coastguard Worker
19*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/Statistic.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/BasicBlock.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/CFG.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Function.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Instructions.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/Local.h"
29*9880d681SAndroid Build Coastguard Worker #include <list>
30*9880d681SAndroid Build Coastguard Worker using namespace llvm;
31*9880d681SAndroid Build Coastguard Worker
32*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "reg2mem"
33*9880d681SAndroid Build Coastguard Worker
34*9880d681SAndroid Build Coastguard Worker STATISTIC(NumRegsDemoted, "Number of registers demoted");
35*9880d681SAndroid Build Coastguard Worker STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker namespace {
38*9880d681SAndroid Build Coastguard Worker struct RegToMem : public FunctionPass {
39*9880d681SAndroid Build Coastguard Worker static char ID; // Pass identification, replacement for typeid
RegToMem__anon750d085a0111::RegToMem40*9880d681SAndroid Build Coastguard Worker RegToMem() : FunctionPass(ID) {
41*9880d681SAndroid Build Coastguard Worker initializeRegToMemPass(*PassRegistry::getPassRegistry());
42*9880d681SAndroid Build Coastguard Worker }
43*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage__anon750d085a0111::RegToMem44*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override {
45*9880d681SAndroid Build Coastguard Worker AU.addRequiredID(BreakCriticalEdgesID);
46*9880d681SAndroid Build Coastguard Worker AU.addPreservedID(BreakCriticalEdgesID);
47*9880d681SAndroid Build Coastguard Worker }
48*9880d681SAndroid Build Coastguard Worker
valueEscapes__anon750d085a0111::RegToMem49*9880d681SAndroid Build Coastguard Worker bool valueEscapes(const Instruction *Inst) const {
50*9880d681SAndroid Build Coastguard Worker const BasicBlock *BB = Inst->getParent();
51*9880d681SAndroid Build Coastguard Worker for (const User *U : Inst->users()) {
52*9880d681SAndroid Build Coastguard Worker const Instruction *UI = cast<Instruction>(U);
53*9880d681SAndroid Build Coastguard Worker if (UI->getParent() != BB || isa<PHINode>(UI))
54*9880d681SAndroid Build Coastguard Worker return true;
55*9880d681SAndroid Build Coastguard Worker }
56*9880d681SAndroid Build Coastguard Worker return false;
57*9880d681SAndroid Build Coastguard Worker }
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker bool runOnFunction(Function &F) override;
60*9880d681SAndroid Build Coastguard Worker };
61*9880d681SAndroid Build Coastguard Worker }
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker char RegToMem::ID = 0;
64*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(RegToMem, "reg2mem", "Demote all values to stack slots",
65*9880d681SAndroid Build Coastguard Worker false, false)
INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)66*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
67*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(RegToMem, "reg2mem", "Demote all values to stack slots",
68*9880d681SAndroid Build Coastguard Worker false, false)
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Worker bool RegToMem::runOnFunction(Function &F) {
71*9880d681SAndroid Build Coastguard Worker if (F.isDeclaration() || skipFunction(F))
72*9880d681SAndroid Build Coastguard Worker return false;
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker // Insert all new allocas into entry block.
75*9880d681SAndroid Build Coastguard Worker BasicBlock *BBEntry = &F.getEntryBlock();
76*9880d681SAndroid Build Coastguard Worker assert(pred_empty(BBEntry) &&
77*9880d681SAndroid Build Coastguard Worker "Entry block to function must not have predecessors!");
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker // Find first non-alloca instruction and create insertion point. This is
80*9880d681SAndroid Build Coastguard Worker // safe if block is well-formed: it always have terminator, otherwise
81*9880d681SAndroid Build Coastguard Worker // we'll get and assertion.
82*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator I = BBEntry->begin();
83*9880d681SAndroid Build Coastguard Worker while (isa<AllocaInst>(I)) ++I;
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker CastInst *AllocaInsertionPoint = new BitCastInst(
86*9880d681SAndroid Build Coastguard Worker Constant::getNullValue(Type::getInt32Ty(F.getContext())),
87*9880d681SAndroid Build Coastguard Worker Type::getInt32Ty(F.getContext()), "reg2mem alloca point", &*I);
88*9880d681SAndroid Build Coastguard Worker
89*9880d681SAndroid Build Coastguard Worker // Find the escaped instructions. But don't create stack slots for
90*9880d681SAndroid Build Coastguard Worker // allocas in entry block.
91*9880d681SAndroid Build Coastguard Worker std::list<Instruction*> WorkList;
92*9880d681SAndroid Build Coastguard Worker for (BasicBlock &ibb : F)
93*9880d681SAndroid Build Coastguard Worker for (BasicBlock::iterator iib = ibb.begin(), iie = ibb.end(); iib != iie;
94*9880d681SAndroid Build Coastguard Worker ++iib) {
95*9880d681SAndroid Build Coastguard Worker if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
96*9880d681SAndroid Build Coastguard Worker valueEscapes(&*iib)) {
97*9880d681SAndroid Build Coastguard Worker WorkList.push_front(&*iib);
98*9880d681SAndroid Build Coastguard Worker }
99*9880d681SAndroid Build Coastguard Worker }
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker // Demote escaped instructions
102*9880d681SAndroid Build Coastguard Worker NumRegsDemoted += WorkList.size();
103*9880d681SAndroid Build Coastguard Worker for (Instruction *ilb : WorkList)
104*9880d681SAndroid Build Coastguard Worker DemoteRegToStack(*ilb, false, AllocaInsertionPoint);
105*9880d681SAndroid Build Coastguard Worker
106*9880d681SAndroid Build Coastguard Worker WorkList.clear();
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker // Find all phi's
109*9880d681SAndroid Build Coastguard Worker for (BasicBlock &ibb : F)
110*9880d681SAndroid Build Coastguard Worker for (BasicBlock::iterator iib = ibb.begin(), iie = ibb.end(); iib != iie;
111*9880d681SAndroid Build Coastguard Worker ++iib)
112*9880d681SAndroid Build Coastguard Worker if (isa<PHINode>(iib))
113*9880d681SAndroid Build Coastguard Worker WorkList.push_front(&*iib);
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker // Demote phi nodes
116*9880d681SAndroid Build Coastguard Worker NumPhisDemoted += WorkList.size();
117*9880d681SAndroid Build Coastguard Worker for (Instruction *ilb : WorkList)
118*9880d681SAndroid Build Coastguard Worker DemotePHIToStack(cast<PHINode>(ilb), AllocaInsertionPoint);
119*9880d681SAndroid Build Coastguard Worker
120*9880d681SAndroid Build Coastguard Worker return true;
121*9880d681SAndroid Build Coastguard Worker }
122*9880d681SAndroid Build Coastguard Worker
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker // createDemoteRegisterToMemory - Provide an entry point to create this pass.
125*9880d681SAndroid Build Coastguard Worker char &llvm::DemoteRegisterToMemoryID = RegToMem::ID;
createDemoteRegisterToMemoryPass()126*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
127*9880d681SAndroid Build Coastguard Worker return new RegToMem();
128*9880d681SAndroid Build Coastguard Worker }
129