Skip to content

Commit 733fe36

Browse files
committed
CodeGen: Remove MachineFunctionAnalysis => Enable (Machine)ModulePasses
Re-apply this patch, hopefully I will get away without any warnings in the constructor now. This patch removes the MachineFunctionAnalysis. Instead we keep a map from IR Function to MachineFunction in the MachineModuleInfo. This allows the insertion of ModulePasses into the codegen pipeline without breaking it because the MachineFunctionAnalysis gets dropped before a module pass. Peak memory should stay unchanged without a ModulePass in the codegen pipeline: Previously the MachineFunction was freed at the end of a codegen function pipeline because the MachineFunctionAnalysis was dropped; With this patch the MachineFunction is freed after the AsmPrinter has finished. Differential Revision: http://reviews.llvm.org/D23736 llvm-svn: 279602
1 parent bceadcf commit 733fe36

24 files changed

+109
-162
lines changed

llvm/include/llvm/CodeGen/MachineFunctionAnalysis.h

Lines changed: 0 additions & 55 deletions
This file was deleted.

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ class BlockAddress;
5454
class MDNode;
5555
class MMIAddrLabelMap;
5656
class MachineBasicBlock;
57+
class MachineFunctionInitializer;
5758
class MachineFunction;
5859
class Module;
5960
class PointerType;
@@ -107,6 +108,8 @@ class MachineModuleInfoImpl {
107108
/// schemes and reformated for specific use.
108109
///
109110
class MachineModuleInfo : public ImmutablePass {
111+
const TargetMachine &TM;
112+
110113
/// Context - This is the MCContext used for the entire code generator.
111114
MCContext Context;
112115

@@ -184,6 +187,14 @@ class MachineModuleInfo : public ImmutablePass {
184187

185188
EHPersonality PersonalityTypeCache;
186189

190+
MachineFunctionInitializer *MFInitializer;
191+
/// Maps IR Functions to their corresponding MachineFunctions.
192+
DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
193+
/// Next unique number available for a MachineFunction.
194+
unsigned NextFnNum = 0;
195+
const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
196+
MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
197+
187198
public:
188199
static char ID; // Pass identification, replacement for typeid
189200

@@ -200,7 +211,6 @@ class MachineModuleInfo : public ImmutablePass {
200211
typedef SmallVector<VariableDbgInfo, 4> VariableDbgInfoMapTy;
201212
VariableDbgInfoMapTy VariableDbgInfos;
202213

203-
// Real constructor.
204214
explicit MachineModuleInfo(const TargetMachine *TM = nullptr);
205215
~MachineModuleInfo() override;
206216

@@ -218,6 +228,19 @@ class MachineModuleInfo : public ImmutablePass {
218228
void setModule(const Module *M) { TheModule = M; }
219229
const Module *getModule() const { return TheModule; }
220230

231+
void setMachineFunctionInitializer(MachineFunctionInitializer *MFInit) {
232+
MFInitializer = MFInit;
233+
}
234+
235+
/// Returns the MachineFunction constructed for the IR function \p F.
236+
/// Creates a new MachineFunction and runs the MachineFunctionInitializer
237+
/// if none exists yet.
238+
MachineFunction &getMachineFunction(const Function &F);
239+
240+
/// \brief Delete the MachineFunction \p MF and reset the link in the IR
241+
/// Function to Machine Function map.
242+
void deleteMachineFunctionFor(Function &F);
243+
221244
/// getInfo - Keep track of various per-function pieces of information for
222245
/// backends that would like to do so.
223246
///

llvm/include/llvm/CodeGen/Passes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,9 @@ namespace llvm {
377377

378378
/// This pass performs software pipelining on machine instructions.
379379
extern char &MachinePipelinerID;
380+
381+
/// This pass frees the memory occupied by the MachineFunction.
382+
FunctionPass *createFreeMachineFunctionPass();
380383
} // End llvm namespace
381384

382385
/// Target machine pass initializer for passes with dependencies. Use with

llvm/include/llvm/Target/TargetMachine.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,6 @@ class LLVMTargetMachine : public TargetMachine {
312312
bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
313313
raw_pwrite_stream &OS,
314314
bool DisableVerify = true) override;
315-
316-
/// Add MachineFunctionAnalysis pass to pass manager.
317-
void addMachineFunctionAnalysis(PassManagerBase &PM,
318-
MachineFunctionInitializer *MFInitializer) const;
319315
};
320316

321317
} // End llvm namespace

llvm/lib/CodeGen/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ add_llvm_library(LLVMCodeGen
5959
MachineCSE.cpp
6060
MachineDominanceFrontier.cpp
6161
MachineDominators.cpp
62-
MachineFunctionAnalysis.cpp
6362
MachineFunction.cpp
6463
MachineFunctionPass.cpp
6564
MachineFunctionPrinterPass.cpp

llvm/lib/CodeGen/LLVMTargetMachine.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "llvm/Analysis/Passes.h"
1616
#include "llvm/CodeGen/AsmPrinter.h"
1717
#include "llvm/CodeGen/BasicTTIImpl.h"
18-
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
1918
#include "llvm/CodeGen/MachineModuleInfo.h"
2019
#include "llvm/CodeGen/Passes.h"
2120
#include "llvm/CodeGen/TargetPassConfig.h"
@@ -102,11 +101,6 @@ TargetIRAnalysis LLVMTargetMachine::getTargetIRAnalysis() {
102101
});
103102
}
104103

105-
void LLVMTargetMachine::addMachineFunctionAnalysis(PassManagerBase &PM,
106-
MachineFunctionInitializer *MFInitializer) const {
107-
PM.add(new MachineFunctionAnalysis(*this, MFInitializer));
108-
}
109-
110104
/// addPassesToX helper drives creation and initialization of TargetPassConfig.
111105
static MCContext *
112106
addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
@@ -142,8 +136,8 @@ addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
142136
PassConfig->addISelPrepare();
143137

144138
MachineModuleInfo *MMI = new MachineModuleInfo(TM);
139+
MMI->setMachineFunctionInitializer(MFInitializer);
145140
PM.add(MMI);
146-
TM->addMachineFunctionAnalysis(PM, MFInitializer);
147141

148142
// Enable FastISel with -fast, but allow that to be overridden.
149143
TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE);
@@ -265,6 +259,7 @@ bool LLVMTargetMachine::addPassesToEmitFile(
265259
return true;
266260

267261
PM.add(Printer);
262+
PM.add(createFreeMachineFunctionPass());
268263

269264
return false;
270265
}
@@ -311,6 +306,7 @@ bool LLVMTargetMachine::addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
311306
return true;
312307

313308
PM.add(Printer);
309+
PM.add(createFreeMachineFunctionPass());
314310

315311
return false; // success!
316312
}

llvm/lib/CodeGen/MachineFunctionAnalysis.cpp

Lines changed: 0 additions & 62 deletions
This file was deleted.

llvm/lib/CodeGen/MachineFunctionPass.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include "llvm/Analysis/ScalarEvolution.h"
2323
#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
2424
#include "llvm/CodeGen/MachineFunction.h"
25-
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
25+
#include "llvm/CodeGen/MachineModuleInfo.h"
2626
#include "llvm/CodeGen/Passes.h"
2727
#include "llvm/CodeGen/StackProtector.h"
2828
#include "llvm/IR/Dominators.h"
@@ -41,7 +41,9 @@ bool MachineFunctionPass::runOnFunction(Function &F) {
4141
if (F.hasAvailableExternallyLinkage())
4242
return false;
4343

44-
MachineFunction &MF = getAnalysis<MachineFunctionAnalysis>().getMF();
44+
MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
45+
MachineFunction &MF = MMI.getMachineFunction(F);
46+
4547
MachineFunctionProperties &MFProps = MF.getProperties();
4648

4749
#ifndef NDEBUG
@@ -65,8 +67,8 @@ bool MachineFunctionPass::runOnFunction(Function &F) {
6567
}
6668

6769
void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
68-
AU.addRequired<MachineFunctionAnalysis>();
69-
AU.addPreserved<MachineFunctionAnalysis>();
70+
AU.addRequired<MachineModuleInfo>();
71+
AU.addPreserved<MachineModuleInfo>();
7072

7173
// MachineFunctionPass preserves all LLVM IR passes, but there's no
7274
// high-level way to express this. Instead, just list a bunch of

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "llvm/Analysis/EHPersonalities.h"
1414
#include "llvm/Analysis/ValueTracking.h"
1515
#include "llvm/CodeGen/MachineFunction.h"
16+
#include "llvm/CodeGen/MachineFunctionInitializer.h"
1617
#include "llvm/CodeGen/MachineFunctionPass.h"
1718
#include "llvm/CodeGen/Passes.h"
1819
#include "llvm/IR/Constants.h"
@@ -189,8 +190,9 @@ void MMIAddrLabelMapCallbackPtr::allUsesReplacedWith(Value *V2) {
189190
//===----------------------------------------------------------------------===//
190191

191192
MachineModuleInfo::MachineModuleInfo(const TargetMachine *TM)
192-
: ImmutablePass(ID), Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
193-
TM->getObjFileLowering(), nullptr, false) {
193+
: ImmutablePass(ID), TM(*TM),
194+
Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
195+
TM->getObjFileLowering(), nullptr, false) {
194196
initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
195197
}
196198

@@ -207,7 +209,7 @@ bool MachineModuleInfo::doInitialization(Module &M) {
207209
DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
208210
PersonalityTypeCache = EHPersonality::Unknown;
209211
AddrLabelSymbols = nullptr;
210-
TheModule = nullptr;
212+
TheModule = &M;
211213

212214
return false;
213215
}
@@ -455,3 +457,63 @@ try_next:;
455457
FilterIds.push_back(0); // terminator
456458
return FilterID;
457459
}
460+
461+
MachineFunction &MachineModuleInfo::getMachineFunction(const Function &F) {
462+
// Shortcut for the common case where a sequence of MachineFunctionPasses
463+
// all query for the same Function.
464+
if (LastRequest == &F)
465+
return *LastResult;
466+
467+
auto I = MachineFunctions.insert(
468+
std::make_pair(&F, std::unique_ptr<MachineFunction>()));
469+
MachineFunction *MF;
470+
if (I.second) {
471+
// No pre-existing machine function, create a new one.
472+
MF = new MachineFunction(&F, TM, NextFnNum++, *this);
473+
// Update the set entry.
474+
I.first->second.reset(MF);
475+
476+
if (MFInitializer)
477+
if (MFInitializer->initializeMachineFunction(*MF))
478+
report_fatal_error("Unable to initialize machine function");
479+
} else {
480+
MF = I.first->second.get();
481+
}
482+
483+
LastRequest = &F;
484+
LastResult = MF;
485+
return *MF;
486+
}
487+
488+
void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
489+
MachineFunctions.erase(&F);
490+
LastRequest = nullptr;
491+
LastResult = nullptr;
492+
}
493+
494+
namespace {
495+
/// This pass frees the MachineFunction object associated with a Function.
496+
class FreeMachineFunction : public FunctionPass {
497+
public:
498+
static char ID;
499+
FreeMachineFunction() : FunctionPass(ID) {}
500+
501+
void getAnalysisUsage(AnalysisUsage &AU) const override {
502+
AU.addRequired<MachineModuleInfo>();
503+
AU.addPreserved<MachineModuleInfo>();
504+
}
505+
506+
bool runOnFunction(Function &F) override {
507+
MachineModuleInfo &MMI = getAnalysis<MachineModuleInfo>();
508+
MMI.deleteMachineFunctionFor(F);
509+
return true;
510+
}
511+
};
512+
char FreeMachineFunction::ID;
513+
} // end anonymous namespace
514+
515+
namespace llvm {
516+
FunctionPass *createFreeMachineFunctionPass() {
517+
return new FreeMachineFunction();
518+
}
519+
} // end namespace llvm

llvm/lib/CodeGen/StackMapLivenessAnalysis.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "llvm/CodeGen/LivePhysRegs.h"
1818
#include "llvm/CodeGen/MachineFrameInfo.h"
1919
#include "llvm/CodeGen/MachineFunction.h"
20-
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
2120
#include "llvm/CodeGen/MachineFunctionPass.h"
2221
#include "llvm/CodeGen/Passes.h"
2322
#include "llvm/Support/CommandLine.h"

0 commit comments

Comments
 (0)