blob: 915f89780c080ab707551c940e106331e8f6008e [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
19#include "llvm/Transforms/Scalar.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/ADT/Statistic.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000021#include "llvm/IR/BasicBlock.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000022#include "llvm/IR/CFG.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/Function.h"
24#include "llvm/IR/Instructions.h"
25#include "llvm/IR/LLVMContext.h"
26#include "llvm/IR/Module.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Pass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Transforms/Utils/Local.h"
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000029#include <list>
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000030using namespace llvm;
31
Chandler Carruth964daaa2014-04-22 02:55:47 +000032#define DEBUG_TYPE "reg2mem"
33
Anton Korobeynikov7499a3b2007-10-21 23:05:16 +000034STATISTIC(NumRegsDemoted, "Number of registers demoted");
35STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
Chris Lattner79a42ac2006-12-19 21:40:18 +000036
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000037namespace {
Chris Lattner2dd09db2009-09-02 06:11:42 +000038 struct RegToMem : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +000039 static char ID; // Pass identification, replacement for typeid
Owen Anderson6c18d1a2010-10-19 17:21:58 +000040 RegToMem() : FunctionPass(ID) {
41 initializeRegToMemPass(*PassRegistry::getPassRegistry());
42 }
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000043
Craig Topper3e4c6972014-03-05 09:10:37 +000044 void getAnalysisUsage(AnalysisUsage &AU) const override {
Andrew Lenharth71b09bb2005-11-22 21:45:19 +000045 AU.addRequiredID(BreakCriticalEdgesID);
Andrew Lenharth5fc37942005-11-25 16:04:54 +000046 AU.addPreservedID(BreakCriticalEdgesID);
Andrew Lenharth71b09bb2005-11-22 21:45:19 +000047 }
48
Chandler Carruthcdf47882014-03-09 03:16:01 +000049 bool valueEscapes(const Instruction *Inst) const {
50 const BasicBlock *BB = Inst->getParent();
51 for (const User *U : Inst->users()) {
52 const Instruction *UI = cast<Instruction>(U);
53 if (UI->getParent() != BB || isa<PHINode>(UI))
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
Craig Topper3e4c6972014-03-05 09:10:37 +000059 bool runOnFunction(Function &F) override;
Andrew Lenharth4130a4f2005-11-10 01:58:38 +000060 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +000061}
Nadav Rotem465834c2012-07-24 10:51:42 +000062
Dan Gohmand78c4002008-05-13 00:00:25 +000063char 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) {
Nadav Rotem465834c2012-07-24 10:51:42 +000071 if (F.isDeclaration())
Chris Lattner09a79dc2009-09-02 06:15:37 +000072 return false;
Nadav Rotem465834c2012-07-24 10:51:42 +000073
Chris Lattner09a79dc2009-09-02 06:15:37 +000074 // Insert all new allocas into entry block.
75 BasicBlock *BBEntry = &F.getEntryBlock();
Ramkumar Ramachandra40c3e032015-01-13 03:46:47 +000076 assert(pred_empty(BBEntry) &&
Chris Lattner09a79dc2009-09-02 06:15:37 +000077 "Entry block to function must not have predecessors!");
Nadav Rotem465834c2012-07-24 10:51:42 +000078
Chris Lattner09a79dc2009-09-02 06:15:37 +000079 // 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;
Nadav Rotem465834c2012-07-24 10:51:42 +000084
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000085 CastInst *AllocaInsertionPoint = new BitCastInst(
86 Constant::getNullValue(Type::getInt32Ty(F.getContext())),
87 Type::getInt32Ty(F.getContext()), "reg2mem alloca point", &*I);
Nadav Rotem465834c2012-07-24 10:51:42 +000088
Chris Lattner09a79dc2009-09-02 06:15:37 +000089 // Find the escaped instructions. But don't create stack slots for
90 // allocas in entry block.
91 std::list<Instruction*> WorkList;
92 for (Function::iterator ibb = F.begin(), ibe = F.end();
93 ibb != ibe; ++ibb)
94 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
95 iib != iie; ++iib) {
96 if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
Duncan P. N. Exon Smithbe4d8cb2015-10-13 19:26:58 +000097 valueEscapes(&*iib)) {
Chris Lattner09a79dc2009-09-02 06:15:37 +000098 WorkList.push_front(&*iib);
99 }
100 }
Nadav Rotem465834c2012-07-24 10:51:42 +0000101
Chris Lattner09a79dc2009-09-02 06:15:37 +0000102 // Demote escaped instructions
103 NumRegsDemoted += WorkList.size();
Nadav Rotem465834c2012-07-24 10:51:42 +0000104 for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
Chris Lattner09a79dc2009-09-02 06:15:37 +0000105 ile = WorkList.end(); ilb != ile; ++ilb)
106 DemoteRegToStack(**ilb, false, AllocaInsertionPoint);
Nadav Rotem465834c2012-07-24 10:51:42 +0000107
Chris Lattner09a79dc2009-09-02 06:15:37 +0000108 WorkList.clear();
Nadav Rotem465834c2012-07-24 10:51:42 +0000109
Chris Lattner09a79dc2009-09-02 06:15:37 +0000110 // Find all phi's
111 for (Function::iterator ibb = F.begin(), ibe = F.end();
112 ibb != ibe; ++ibb)
113 for (BasicBlock::iterator iib = ibb->begin(), iie = ibb->end();
114 iib != iie; ++iib)
115 if (isa<PHINode>(iib))
116 WorkList.push_front(&*iib);
Nadav Rotem465834c2012-07-24 10:51:42 +0000117
Chris Lattner09a79dc2009-09-02 06:15:37 +0000118 // Demote phi nodes
119 NumPhisDemoted += WorkList.size();
Nadav Rotem465834c2012-07-24 10:51:42 +0000120 for (std::list<Instruction*>::iterator ilb = WorkList.begin(),
Chris Lattner09a79dc2009-09-02 06:15:37 +0000121 ile = WorkList.end(); ilb != ile; ++ilb)
122 DemotePHIToStack(cast<PHINode>(*ilb), AllocaInsertionPoint);
Nadav Rotem465834c2012-07-24 10:51:42 +0000123
Chris Lattner09a79dc2009-09-02 06:15:37 +0000124 return true;
125}
126
127
Andrew Lenharth4130a4f2005-11-10 01:58:38 +0000128// createDemoteRegisterToMemory - Provide an entry point to create this pass.
Owen Andersona7aed182010-08-06 18:33:48 +0000129char &llvm::DemoteRegisterToMemoryID = RegToMem::ID;
Andrew Lenharth4130a4f2005-11-10 01:58:38 +0000130FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
131 return new RegToMem();
132}