2005-11-10 09:58:38 +08:00
|
|
|
//===- Reg2Mem.cpp - Convert registers to allocas -------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2005-11-10 09:58:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-06-02 18:20:22 +08:00
|
|
|
// This file demotes all registers to memory references. It is intended to be
|
2005-11-10 09:58:38 +08:00
|
|
|
// the inverse of PromoteMemoryToRegister. By converting to loads, the only
|
2011-04-15 13:18:47 +08:00
|
|
|
// values live across basic blocks are allocas and loads before phi nodes.
|
2005-11-10 09:58:38 +08:00
|
|
|
// It is intended that this should make CFG hacking much easier.
|
|
|
|
// To make later hacking easier, the entry block is split into two, such that
|
|
|
|
// all introduced allocas and nothing else are in the entry block.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/BasicBlock.h"
|
2014-03-04 19:45:46 +08:00
|
|
|
#include "llvm/IR/CFG.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#include "llvm/IR/Function.h"
|
|
|
|
#include "llvm/IR/Instructions.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
|
|
|
#include "llvm/IR/Module.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/InitializePasses.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Pass.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Transforms/Scalar.h"
|
2018-03-29 01:44:36 +08:00
|
|
|
#include "llvm/Transforms/Utils.h"
|
Sink all InitializePasses.h includes
This file lists every pass in LLVM, and is included by Pass.h, which is
very popular. Every time we add, remove, or rename a pass in LLVM, it
caused lots of recompilation.
I found this fact by looking at this table, which is sorted by the
number of times a file was changed over the last 100,000 git commits
multiplied by the number of object files that depend on it in the
current checkout:
recompiles touches affected_files header
342380 95 3604 llvm/include/llvm/ADT/STLExtras.h
314730 234 1345 llvm/include/llvm/InitializePasses.h
307036 118 2602 llvm/include/llvm/ADT/APInt.h
213049 59 3611 llvm/include/llvm/Support/MathExtras.h
170422 47 3626 llvm/include/llvm/Support/Compiler.h
162225 45 3605 llvm/include/llvm/ADT/Optional.h
158319 63 2513 llvm/include/llvm/ADT/Triple.h
140322 39 3598 llvm/include/llvm/ADT/StringRef.h
137647 59 2333 llvm/include/llvm/Support/Error.h
131619 73 1803 llvm/include/llvm/Support/FileSystem.h
Before this change, touching InitializePasses.h would cause 1345 files
to recompile. After this change, touching it only causes 550 compiles in
an incremental rebuild.
Reviewers: bkramer, asbirlea, bollu, jdoerfert
Differential Revision: https://reviews.llvm.org/D70211
2019-11-14 05:15:01 +08:00
|
|
|
#include "llvm/Transforms/Utils/Local.h"
|
2005-11-10 09:58:38 +08:00
|
|
|
#include <list>
|
|
|
|
using namespace llvm;
|
|
|
|
|
2014-04-22 10:55:47 +08:00
|
|
|
#define DEBUG_TYPE "reg2mem"
|
|
|
|
|
2007-10-22 07:05:16 +08:00
|
|
|
STATISTIC(NumRegsDemoted, "Number of registers demoted");
|
|
|
|
STATISTIC(NumPhisDemoted, "Number of phi-nodes demoted");
|
2006-12-20 05:40:18 +08:00
|
|
|
|
2005-11-10 09:58:38 +08:00
|
|
|
namespace {
|
2009-09-02 14:11:42 +08:00
|
|
|
struct RegToMem : public FunctionPass {
|
2007-05-06 21:37:16 +08:00
|
|
|
static char ID; // Pass identification, replacement for typeid
|
2010-10-20 01:21:58 +08:00
|
|
|
RegToMem() : FunctionPass(ID) {
|
|
|
|
initializeRegToMemPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
2005-11-10 09:58:38 +08:00
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2005-11-23 05:45:19 +08:00
|
|
|
AU.addRequiredID(BreakCriticalEdgesID);
|
2005-11-26 00:04:54 +08:00
|
|
|
AU.addPreservedID(BreakCriticalEdgesID);
|
2005-11-23 05:45:19 +08:00
|
|
|
}
|
|
|
|
|
2014-03-09 11:16:01 +08:00
|
|
|
bool valueEscapes(const Instruction *Inst) const {
|
|
|
|
const BasicBlock *BB = Inst->getParent();
|
|
|
|
for (const User *U : Inst->users()) {
|
|
|
|
const Instruction *UI = cast<Instruction>(U);
|
|
|
|
if (UI->getParent() != BB || isa<PHINode>(UI))
|
2005-11-10 09:58:38 +08:00
|
|
|
return true;
|
2010-04-15 00:48:56 +08:00
|
|
|
}
|
2005-11-10 09:58:38 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-05 17:10:37 +08:00
|
|
|
bool runOnFunction(Function &F) override;
|
2005-11-10 09:58:38 +08:00
|
|
|
};
|
2015-06-23 17:49:53 +08:00
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2008-05-13 08:00:25 +08:00
|
|
|
char RegToMem::ID = 0;
|
2010-10-13 03:48:12 +08:00
|
|
|
INITIALIZE_PASS_BEGIN(RegToMem, "reg2mem", "Demote all values to stack slots",
|
|
|
|
false, false)
|
|
|
|
INITIALIZE_PASS_DEPENDENCY(BreakCriticalEdges)
|
|
|
|
INITIALIZE_PASS_END(RegToMem, "reg2mem", "Demote all values to stack slots",
|
2010-10-08 06:25:06 +08:00
|
|
|
false, false)
|
2009-09-02 14:15:37 +08:00
|
|
|
|
|
|
|
bool RegToMem::runOnFunction(Function &F) {
|
2016-05-04 06:32:30 +08:00
|
|
|
if (F.isDeclaration() || skipFunction(F))
|
2009-09-02 14:15:37 +08:00
|
|
|
return false;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2009-09-02 14:15:37 +08:00
|
|
|
// Insert all new allocas into entry block.
|
|
|
|
BasicBlock *BBEntry = &F.getEntryBlock();
|
2015-01-13 11:46:47 +08:00
|
|
|
assert(pred_empty(BBEntry) &&
|
2009-09-02 14:15:37 +08:00
|
|
|
"Entry block to function must not have predecessors!");
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2009-09-02 14:15:37 +08:00
|
|
|
// Find first non-alloca instruction and create insertion point. This is
|
|
|
|
// safe if block is well-formed: it always have terminator, otherwise
|
|
|
|
// we'll get and assertion.
|
|
|
|
BasicBlock::iterator I = BBEntry->begin();
|
|
|
|
while (isa<AllocaInst>(I)) ++I;
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2015-10-14 03:26:58 +08:00
|
|
|
CastInst *AllocaInsertionPoint = new BitCastInst(
|
|
|
|
Constant::getNullValue(Type::getInt32Ty(F.getContext())),
|
|
|
|
Type::getInt32Ty(F.getContext()), "reg2mem alloca point", &*I);
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2009-09-02 14:15:37 +08:00
|
|
|
// Find the escaped instructions. But don't create stack slots for
|
|
|
|
// allocas in entry block.
|
|
|
|
std::list<Instruction*> WorkList;
|
2016-06-26 20:28:59 +08:00
|
|
|
for (BasicBlock &ibb : F)
|
|
|
|
for (BasicBlock::iterator iib = ibb.begin(), iie = ibb.end(); iib != iie;
|
|
|
|
++iib) {
|
2009-09-02 14:15:37 +08:00
|
|
|
if (!(isa<AllocaInst>(iib) && iib->getParent() == BBEntry) &&
|
2015-10-14 03:26:58 +08:00
|
|
|
valueEscapes(&*iib)) {
|
2009-09-02 14:15:37 +08:00
|
|
|
WorkList.push_front(&*iib);
|
|
|
|
}
|
|
|
|
}
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2009-09-02 14:15:37 +08:00
|
|
|
// Demote escaped instructions
|
|
|
|
NumRegsDemoted += WorkList.size();
|
2016-06-26 20:28:59 +08:00
|
|
|
for (Instruction *ilb : WorkList)
|
|
|
|
DemoteRegToStack(*ilb, false, AllocaInsertionPoint);
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2009-09-02 14:15:37 +08:00
|
|
|
WorkList.clear();
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2009-09-02 14:15:37 +08:00
|
|
|
// Find all phi's
|
2016-06-26 20:28:59 +08:00
|
|
|
for (BasicBlock &ibb : F)
|
|
|
|
for (BasicBlock::iterator iib = ibb.begin(), iie = ibb.end(); iib != iie;
|
|
|
|
++iib)
|
2009-09-02 14:15:37 +08:00
|
|
|
if (isa<PHINode>(iib))
|
|
|
|
WorkList.push_front(&*iib);
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2009-09-02 14:15:37 +08:00
|
|
|
// Demote phi nodes
|
|
|
|
NumPhisDemoted += WorkList.size();
|
2016-06-26 20:28:59 +08:00
|
|
|
for (Instruction *ilb : WorkList)
|
|
|
|
DemotePHIToStack(cast<PHINode>(ilb), AllocaInsertionPoint);
|
2012-07-24 18:51:42 +08:00
|
|
|
|
2009-09-02 14:15:37 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-11-10 09:58:38 +08:00
|
|
|
// createDemoteRegisterToMemory - Provide an entry point to create this pass.
|
2010-08-07 02:33:48 +08:00
|
|
|
char &llvm::DemoteRegisterToMemoryID = RegToMem::ID;
|
2005-11-10 09:58:38 +08:00
|
|
|
FunctionPass *llvm::createDemoteRegisterToMemoryPass() {
|
|
|
|
return new RegToMem();
|
|
|
|
}
|