Skip to content

Commit 6186520

Browse files
committed
Make MCContext in MachineModuleInfo unique_ptr instead.
1 parent f6d4993 commit 6186520

File tree

2 files changed

+32
-37
lines changed

2 files changed

+32
-37
lines changed

llvm/include/llvm/CodeGen/MachineModuleInfo.h

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class MachineModuleInfoImpl {
6666
protected:
6767
/// Return the entries from a DenseMap in a deterministic sorted orer.
6868
/// Clears the map.
69-
static SymbolListTy getSortedStubs(DenseMap<MCSymbol*, StubValueTy>&);
69+
static SymbolListTy getSortedStubs(DenseMap<MCSymbol *, StubValueTy> &);
7070

7171
/// Return the entries from a DenseMap in a deterministic sorted orer.
7272
/// Clears the map.
@@ -86,7 +86,7 @@ class MachineModuleInfo {
8686
const LLVMTargetMachine &TM;
8787

8888
/// This is the MCContext used for the entire code generator.
89-
MCContext Context;
89+
std::unique_ptr<MCContext> Context;
9090
// This is an external context, that if assigned, will be used instead of the
9191
// internal context.
9292
MCContext *ExternalContext = nullptr;
@@ -100,7 +100,7 @@ class MachineModuleInfo {
100100
MachineModuleInfoImpl *ObjFileMMI;
101101

102102
/// Maps IR Functions to their corresponding MachineFunctions.
103-
DenseMap<const Function*, std::unique_ptr<MachineFunction>> MachineFunctions;
103+
DenseMap<const Function *, std::unique_ptr<MachineFunction>> MachineFunctions;
104104
/// Next unique number available for a MachineFunction.
105105
unsigned NextFnNum = 0;
106106
const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
@@ -124,10 +124,10 @@ class MachineModuleInfo {
124124
const LLVMTargetMachine &getTarget() const { return TM; }
125125

126126
const MCContext &getContext() const {
127-
return ExternalContext ? *ExternalContext : Context;
127+
return ExternalContext ? *ExternalContext : *Context;
128128
}
129129
MCContext &getContext() {
130-
return ExternalContext ? *ExternalContext : Context;
130+
return ExternalContext ? *ExternalContext : *Context;
131131
}
132132

133133
const Module *getModule() const { return TheModule; }
@@ -153,24 +153,21 @@ class MachineModuleInfo {
153153

154154
/// Keep track of various per-module pieces of information for backends
155155
/// that would like to do so.
156-
template<typename Ty>
157-
Ty &getObjFileInfo() {
156+
template <typename Ty> Ty &getObjFileInfo() {
158157
if (ObjFileMMI == nullptr)
159158
ObjFileMMI = new Ty(*this);
160-
return *static_cast<Ty*>(ObjFileMMI);
159+
return *static_cast<Ty *>(ObjFileMMI);
161160
}
162161

163-
template<typename Ty>
164-
const Ty &getObjFileInfo() const {
165-
return const_cast<MachineModuleInfo*>(this)->getObjFileInfo<Ty>();
162+
template <typename Ty> const Ty &getObjFileInfo() const {
163+
return const_cast<MachineModuleInfo *>(this)->getObjFileInfo<Ty>();
166164
}
167165

168166
/// \}
169167
}; // End class MachineModuleInfo
170168

171169
class MachineModuleInfoWrapperPass : public ImmutablePass {
172-
std::unique_ptr<MachineModuleInfo> MMI =
173-
std::make_unique<MachineModuleInfo>();
170+
MachineModuleInfo MMI;
174171

175172
public:
176173
static char ID; // Pass identification, replacement for typeid
@@ -183,8 +180,8 @@ class MachineModuleInfoWrapperPass : public ImmutablePass {
183180
bool doInitialization(Module &) override;
184181
bool doFinalization(Module &) override;
185182

186-
MachineModuleInfo &getMMI() { return *MMI; }
187-
const MachineModuleInfo &getMMI() const { return *MMI; }
183+
MachineModuleInfo &getMMI() { return MMI; }
184+
const MachineModuleInfo &getMMI() const { return MMI; }
188185
};
189186

190187
/// An analysis that produces \c MachineModuleInfo for a module.

llvm/lib/CodeGen/MachineModuleInfo.cpp

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,39 @@ void MachineModuleInfo::initialize() {
3030
}
3131

3232
void MachineModuleInfo::finalize() {
33-
Context.reset();
33+
if (Context)
34+
Context->reset();
3435
// We don't clear the ExternalContext.
3536

3637
delete ObjFileMMI;
3738
ObjFileMMI = nullptr;
3839
}
3940

4041
MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
41-
: TM(std::move(MMI.TM)),
42-
Context(TM.getTargetTriple(), TM.getMCAsmInfo(), TM.getMCRegisterInfo(),
43-
TM.getMCSubtargetInfo(), nullptr, &TM.Options.MCOptions, false),
42+
: TM(std::move(MMI.TM)), Context(std::move(MMI.Context)),
4443
MachineFunctions(std::move(MMI.MachineFunctions)) {
45-
Context.setObjectFileInfo(TM.getObjFileLowering());
4644
ObjFileMMI = MMI.ObjFileMMI;
4745
ExternalContext = MMI.ExternalContext;
4846
TheModule = MMI.TheModule;
4947
}
5048

5149
MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
52-
: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
53-
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
54-
nullptr, &TM->Options.MCOptions, false) {
55-
Context.setObjectFileInfo(TM->getObjFileLowering());
50+
: TM(*TM),
51+
Context(std::make_unique<MCContext>(
52+
TM->getTargetTriple(), TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
53+
TM->getMCSubtargetInfo(), nullptr, &TM->Options.MCOptions, false)) {
54+
Context->setObjectFileInfo(TM->getObjFileLowering());
5655
initialize();
5756
}
5857

5958
MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM,
6059
MCContext *ExtContext)
61-
: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
62-
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
63-
nullptr, &TM->Options.MCOptions, false),
60+
: TM(*TM),
61+
Context(std::make_unique<MCContext>(
62+
TM->getTargetTriple(), TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
63+
TM->getMCSubtargetInfo(), nullptr, &TM->Options.MCOptions, false)),
6464
ExternalContext(ExtContext) {
65-
Context.setObjectFileInfo(TM->getObjFileLowering());
65+
Context->setObjectFileInfo(TM->getObjFileLowering());
6666
initialize();
6767
}
6868

@@ -137,9 +137,7 @@ class FreeMachineFunction : public FunctionPass {
137137
return true;
138138
}
139139

140-
StringRef getPassName() const override {
141-
return "Free MachineFunction";
142-
}
140+
StringRef getPassName() const override { return "Free MachineFunction"; }
143141
};
144142

145143
} // end anonymous namespace
@@ -152,13 +150,13 @@ FunctionPass *llvm::createFreeMachineFunctionPass() {
152150

153151
MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
154152
const LLVMTargetMachine *TM)
155-
: ImmutablePass(ID), MMI(std::make_unique<MachineModuleInfo>(TM)) {
153+
: ImmutablePass(ID), MMI(TM) {
156154
initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
157155
}
158156

159157
MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
160158
const LLVMTargetMachine *TM, MCContext *ExtContext)
161-
: ImmutablePass(ID), MMI(std::make_unique<MachineModuleInfo>(TM, ExtContext)) {
159+
: ImmutablePass(ID), MMI(TM) {
162160
initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
163161
}
164162

@@ -193,10 +191,10 @@ static uint64_t getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
193191
}
194192

195193
bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
196-
MMI->initialize();
197-
MMI->TheModule = &M;
194+
MMI.initialize();
195+
MMI.TheModule = &M;
198196
LLVMContext &Ctx = M.getContext();
199-
MMI->getContext().setDiagnosticHandler(
197+
MMI.getContext().setDiagnosticHandler(
200198
[&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
201199
const SourceMgr &SrcMgr,
202200
std::vector<const MDNode *> &LocInfos) {
@@ -210,7 +208,7 @@ bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
210208
}
211209

212210
bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
213-
MMI->finalize();
211+
MMI.finalize();
214212
return false;
215213
}
216214

0 commit comments

Comments
 (0)