llvm.org GIT mirror llvm / fa5c5c7
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 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@279602 91177308-0d34-0410-b5e6-96231b3b80d8 Matthias Braun 3 years ago
24 changed file(s) with 109 addition(s) and 162 deletion(s). Raw diff Collapse all Expand all
+0
-55
include/llvm/CodeGen/MachineFunctionAnalysis.h less more
None //===-- MachineFunctionAnalysis.h - Owner of MachineFunctions ----*-C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file declares the MachineFunctionAnalysis class.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_CODEGEN_MACHINEFUNCTIONANALYSIS_H
14 #define LLVM_CODEGEN_MACHINEFUNCTIONANALYSIS_H
15
16 #include "llvm/Pass.h"
17
18 namespace llvm {
19
20 class MachineFunction;
21 class MachineFunctionInitializer;
22 class TargetMachine;
23
24 /// MachineFunctionAnalysis - This class is a Pass that manages a
25 /// MachineFunction object.
26 struct MachineFunctionAnalysis : public FunctionPass {
27 private:
28 const TargetMachine &TM;
29 MachineFunction *MF;
30 unsigned NextFnNum;
31 MachineFunctionInitializer *MFInitializer;
32
33 public:
34 static char ID;
35 explicit MachineFunctionAnalysis(const TargetMachine &tm,
36 MachineFunctionInitializer *MFInitializer);
37 ~MachineFunctionAnalysis() override;
38
39 MachineFunction &getMF() const { return *MF; }
40
41 const char* getPassName() const override {
42 return "Machine Function Analysis";
43 }
44
45 private:
46 bool doInitialization(Module &M) override;
47 bool runOnFunction(Function &F) override;
48 void releaseMemory() override;
49 void getAnalysisUsage(AnalysisUsage &AU) const override;
50 };
51
52 } // End llvm namespace
53
54 #endif
5353 class MDNode;
5454 class MMIAddrLabelMap;
5555 class MachineBasicBlock;
56 class MachineFunctionInitializer;
5657 class MachineFunction;
5758 class Module;
5859 class PointerType;
106107 /// schemes and reformated for specific use.
107108 ///
108109 class MachineModuleInfo : public ImmutablePass {
110 const TargetMachine &TM;
111
109112 /// Context - This is the MCContext used for the entire code generator.
110113 MCContext Context;
111114
183186
184187 EHPersonality PersonalityTypeCache;
185188
189 MachineFunctionInitializer *MFInitializer;
190 /// Maps IR Functions to their corresponding MachineFunctions.
191 DenseMap> MachineFunctions;
192 /// Next unique number available for a MachineFunction.
193 unsigned NextFnNum = 0;
194 const Function *LastRequest = nullptr; ///< Used for shortcut/cache.
195 MachineFunction *LastResult = nullptr; ///< Used for shortcut/cache.
196
186197 public:
187198 static char ID; // Pass identification, replacement for typeid
188199
199210 typedef SmallVector VariableDbgInfoMapTy;
200211 VariableDbgInfoMapTy VariableDbgInfos;
201212
202 // Real constructor.
203213 explicit MachineModuleInfo(const TargetMachine *TM = nullptr);
204214 ~MachineModuleInfo() override;
205215
216226
217227 void setModule(const Module *M) { TheModule = M; }
218228 const Module *getModule() const { return TheModule; }
229
230 void setMachineFunctionInitializer(MachineFunctionInitializer *MFInit) {
231 MFInitializer = MFInit;
232 }
233
234 /// Returns the MachineFunction constructed for the IR function \p F.
235 /// Creates a new MachineFunction and runs the MachineFunctionInitializer
236 /// if none exists yet.
237 MachineFunction &getMachineFunction(const Function &F);
238
239 /// \brief Delete the MachineFunction \p MF and reset the link in the IR
240 /// Function to Machine Function map.
241 void deleteMachineFunctionFor(Function &F);
219242
220243 /// getInfo - Keep track of various per-function pieces of information for
221244 /// backends that would like to do so.
376376
377377 /// This pass performs software pipelining on machine instructions.
378378 extern char &MachinePipelinerID;
379
380 /// This pass frees the memory occupied by the MachineFunction.
381 FunctionPass *createFreeMachineFunctionPass();
379382 } // End llvm namespace
380383
381384 /// Target machine pass initializer for passes with dependencies. Use with
311311 bool addPassesToEmitMC(PassManagerBase &PM, MCContext *&Ctx,
312312 raw_pwrite_stream &OS,
313313 bool DisableVerify = true) override;
314
315 /// Add MachineFunctionAnalysis pass to pass manager.
316 void addMachineFunctionAnalysis(PassManagerBase &PM,
317 MachineFunctionInitializer *MFInitializer) const;
318314 };
319315
320316 } // End llvm namespace
5858 MachineCSE.cpp
5959 MachineDominanceFrontier.cpp
6060 MachineDominators.cpp
61 MachineFunctionAnalysis.cpp
6261 MachineFunction.cpp
6362 MachineFunctionPass.cpp
6463 MachineFunctionPrinterPass.cpp
1414 #include "llvm/Analysis/Passes.h"
1515 #include "llvm/CodeGen/AsmPrinter.h"
1616 #include "llvm/CodeGen/BasicTTIImpl.h"
17 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
1817 #include "llvm/CodeGen/MachineModuleInfo.h"
1918 #include "llvm/CodeGen/Passes.h"
2019 #include "llvm/CodeGen/TargetPassConfig.h"
101100 });
102101 }
103102
104 void LLVMTargetMachine::addMachineFunctionAnalysis(PassManagerBase &PM,
105 MachineFunctionInitializer *MFInitializer) const {
106 PM.add(new MachineFunctionAnalysis(*this, MFInitializer));
107 }
108
109103 /// addPassesToX helper drives creation and initialization of TargetPassConfig.
110104 static MCContext *
111105 addPassesToGenerateCode(LLVMTargetMachine *TM, PassManagerBase &PM,
141135 PassConfig->addISelPrepare();
142136
143137 MachineModuleInfo *MMI = new MachineModuleInfo(TM);
138 MMI->setMachineFunctionInitializer(MFInitializer);
144139 PM.add(MMI);
145 TM->addMachineFunctionAnalysis(PM, MFInitializer);
146140
147141 // Enable FastISel with -fast, but allow that to be overridden.
148142 TM->setO0WantsFastISel(EnableFastISelOption != cl::BOU_FALSE);
264258 return true;
265259
266260 PM.add(Printer);
261 PM.add(createFreeMachineFunctionPass());
267262
268263 return false;
269264 }
310305 return true;
311306
312307 PM.add(Printer);
308 PM.add(createFreeMachineFunctionPass());
313309
314310 return false; // success!
315311 }
+0
-62
lib/CodeGen/MachineFunctionAnalysis.cpp less more
None //===-- MachineFunctionAnalysis.cpp ---------------------------------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the definitions of the MachineFunctionAnalysis members.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
14 #include "llvm/CodeGen/GCMetadata.h"
15 #include "llvm/CodeGen/MachineFunction.h"
16 #include "llvm/CodeGen/MachineModuleInfo.h"
17 #include "llvm/CodeGen/MachineFunctionInitializer.h"
18 using namespace llvm;
19
20 char MachineFunctionAnalysis::ID = 0;
21
22 MachineFunctionAnalysis::MachineFunctionAnalysis(
23 const TargetMachine &tm, MachineFunctionInitializer *MFInitializer)
24 : FunctionPass(ID), TM(tm), MF(nullptr), MFInitializer(MFInitializer) {
25 initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
26 }
27
28 MachineFunctionAnalysis::~MachineFunctionAnalysis() {
29 releaseMemory();
30 assert(!MF && "MachineFunctionAnalysis left initialized!");
31 }
32
33 void MachineFunctionAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
34 AU.setPreservesAll();
35 AU.addRequired();
36 }
37
38 bool MachineFunctionAnalysis::doInitialization(Module &M) {
39 MachineModuleInfo *MMI = getAnalysisIfAvailable();
40 assert(MMI && "MMI not around yet??");
41 MMI->setModule(&M);
42 NextFnNum = 0;
43 return false;
44 }
45
46
47 bool MachineFunctionAnalysis::runOnFunction(Function &F) {
48 assert(!MF && "MachineFunctionAnalysis already initialized!");
49 MF = new MachineFunction(&F, TM, NextFnNum++,
50 getAnalysis());
51 if (MFInitializer) {
52 if (MFInitializer->initializeMachineFunction(*MF))
53 report_fatal_error("Unable to initialize machine function");
54 }
55 return false;
56 }
57
58 void MachineFunctionAnalysis::releaseMemory() {
59 delete MF;
60 MF = nullptr;
61 }
2121 #include "llvm/Analysis/ScalarEvolution.h"
2222 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
2323 #include "llvm/CodeGen/MachineFunction.h"
24 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
24 #include "llvm/CodeGen/MachineModuleInfo.h"
2525 #include "llvm/CodeGen/Passes.h"
2626 #include "llvm/CodeGen/StackProtector.h"
2727 #include "llvm/IR/Dominators.h"
4040 if (F.hasAvailableExternallyLinkage())
4141 return false;
4242
43 MachineFunction &MF = getAnalysis().getMF();
43 MachineModuleInfo &MMI = getAnalysis();
44 MachineFunction &MF = MMI.getMachineFunction(F);
45
4446 MachineFunctionProperties &MFProps = MF.getProperties();
4547
4648 #ifndef NDEBUG
6466 }
6567
6668 void MachineFunctionPass::getAnalysisUsage(AnalysisUsage &AU) const {
67 AU.addRequired();
68 AU.addPreserved>();
69 AU.addRequired>();
70 AU.addPreserved();
6971
7072 // MachineFunctionPass preserves all LLVM IR passes, but there's no
7173 // high-level way to express this. Instead, just list a bunch of
1212 #include "llvm/Analysis/EHPersonalities.h"
1313 #include "llvm/Analysis/ValueTracking.h"
1414 #include "llvm/CodeGen/MachineFunction.h"
15 #include "llvm/CodeGen/MachineFunctionInitializer.h"
1516 #include "llvm/CodeGen/MachineFunctionPass.h"
1617 #include "llvm/CodeGen/Passes.h"
1718 #include "llvm/IR/Constants.h"
188189 //===----------------------------------------------------------------------===//
189190
190191 MachineModuleInfo::MachineModuleInfo(const TargetMachine *TM)
191 : ImmutablePass(ID), Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
192 TM->getObjFileLowering(), nullptr, false) {
192 : ImmutablePass(ID), TM(*TM),
193 Context(TM->getMCAsmInfo(), TM->getMCRegisterInfo(),
194 TM->getObjFileLowering(), nullptr, false) {
193195 initializeMachineModuleInfoPass(*PassRegistry::getPassRegistry());
194196 }
195197
206208 DbgInfoAvailable = UsesVAFloatArgument = UsesMorestackAddr = false;
207209 PersonalityTypeCache = EHPersonality::Unknown;
208210 AddrLabelSymbols = nullptr;
209 TheModule = nullptr;
211 TheModule = &M;
210212
211213 return false;
212214 }
454456 FilterIds.push_back(0); // terminator
455457 return FilterID;
456458 }
459
460 MachineFunction &MachineModuleInfo::getMachineFunction(const Function &F) {
461 // Shortcut for the common case where a sequence of MachineFunctionPasses
462 // all query for the same Function.
463 if (LastRequest == &F)
464 return *LastResult;
465
466 auto I = MachineFunctions.insert(
467 std::make_pair(&F, std::unique_ptr()));
468 MachineFunction *MF;
469 if (I.second) {
470 // No pre-existing machine function, create a new one.
471 MF = new MachineFunction(&F, TM, NextFnNum++, *this);
472 // Update the set entry.
473 I.first->second.reset(MF);
474
475 if (MFInitializer)
476 if (MFInitializer->initializeMachineFunction(*MF))
477 report_fatal_error("Unable to initialize machine function");
478 } else {
479 MF = I.first->second.get();
480 }
481
482 LastRequest = &F;
483 LastResult = MF;
484 return *MF;
485 }
486
487 void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
488 MachineFunctions.erase(&F);
489 LastRequest = nullptr;
490 LastResult = nullptr;
491 }
492
493 namespace {
494 /// This pass frees the MachineFunction object associated with a Function.
495 class FreeMachineFunction : public FunctionPass {
496 public:
497 static char ID;
498 FreeMachineFunction() : FunctionPass(ID) {}
499
500 void getAnalysisUsage(AnalysisUsage &AU) const override {
501 AU.addRequired();
502 AU.addPreserved();
503 }
504
505 bool runOnFunction(Function &F) override {
506 MachineModuleInfo &MMI = getAnalysis();
507 MMI.deleteMachineFunctionFor(F);
508 return true;
509 }
510 };
511 char FreeMachineFunction::ID;
512 } // end anonymous namespace
513
514 namespace llvm {
515 FunctionPass *createFreeMachineFunctionPass() {
516 return new FreeMachineFunction();
517 }
518 } // end namespace llvm
1616 #include "llvm/CodeGen/LivePhysRegs.h"
1717 #include "llvm/CodeGen/MachineFrameInfo.h"
1818 #include "llvm/CodeGen/MachineFunction.h"
19 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
2019 #include "llvm/CodeGen/MachineFunctionPass.h"
2120 #include "llvm/CodeGen/Passes.h"
2221 #include "llvm/Support/CommandLine.h"
1717 #include "llvm/ADT/Statistic.h"
1818 #include "llvm/CodeGen/MachineDominators.h"
1919 #include "llvm/CodeGen/MachineFunction.h"
20 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
2120 #include "llvm/CodeGen/MachineFunctionPass.h"
2221 #include "llvm/CodeGen/MachineInstrBuilder.h"
2322 #include "llvm/CodeGen/MachineJumpTableInfo.h"
143142 }
144143
145144 void getAnalysisUsage(AnalysisUsage &AU) const override {
146 AU.addPreserved();
147 AU.addRequired();
148145 AU.addRequired();
149146 AU.addRequired();
150147 AU.addRequired();
148 MachineFunctionPass::getAnalysisUsage(AU);
151149 }
152150
153151 /// Perform the CFG structurization
1313 #include "llvm/ADT/STLExtras.h"
1414 #include "llvm/Analysis/LoopInfo.h"
1515 #include "llvm/Analysis/PostDominators.h"
16 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
1716 #include "llvm/IR/Constants.h"
1817 #include "llvm/IR/Dominators.h"
1918 #include "llvm/IR/Function.h"
77 //===----------------------------------------------------------------------===//
88
99 #include "llvm/ADT/STLExtras.h"
10 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
1110 #include "llvm/IR/Constants.h"
1211 #include "llvm/IR/Dominators.h"
1312 #include "llvm/IR/Function.h"
5958 virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
6059 AU.addRequired();
6160 AU.addPreserved();
62 AU.addPreserved();
6361 FunctionPass::getAnalysisUsage(AU);
6462 }
6563 private:
2828 #include "HexagonTargetMachine.h"
2929 #include "llvm/ADT/Statistic.h"
3030 #include "llvm/CodeGen/LiveVariables.h"
31 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
3231 #include "llvm/CodeGen/MachineFunctionPass.h"
3332 #include "llvm/CodeGen/MachineInstrBuilder.h"
3433 #include "llvm/CodeGen/MachineRegisterInfo.h"
1111 //
1212 //===----------------------------------------------------------------------===//
1313
14 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
1514 #include "llvm/CodeGen/StackProtector.h"
1615 #include "llvm/IR/Function.h"
1716 #include "llvm/IR/Instructions.h"
4241 }
4342
4443 void getAnalysisUsage(AnalysisUsage &AU) const override {
45 AU.addRequired();
46 AU.addPreserved();
4744 AU.addPreserved();
4845 FunctionPass::getAnalysisUsage(AU);
4946 }
2121 #include "HexagonVLIWPacketizer.h"
2222 #include "llvm/Analysis/AliasAnalysis.h"
2323 #include "llvm/CodeGen/MachineDominators.h"
24 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
2524 #include "llvm/CodeGen/MachineFunctionPass.h"
2625 #include "llvm/CodeGen/MachineLoopInfo.h"
2726 #include "llvm/CodeGen/MachineRegisterInfo.h"
1111 //===----------------------------------------------------------------------===//
1212
1313 #include "NVPTXAllocaHoisting.h"
14 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
1514 #include "llvm/CodeGen/StackProtector.h"
1615 #include "llvm/IR/Constants.h"
1716 #include "llvm/IR/Function.h"
2726 NVPTXAllocaHoisting() : FunctionPass(ID) {}
2827
2928 void getAnalysisUsage(AnalysisUsage &AU) const override {
30 AU.addPreserved();
3129 AU.addPreserved();
3230 }
3331
1414 #include "NVPTX.h"
1515 #include "MCTargetDesc/NVPTXBaseInfo.h"
1616 #include "NVPTXUtilities.h"
17 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
1817 #include "llvm/CodeGen/ValueTypes.h"
1918 #include "llvm/IR/Constants.h"
2019 #include "llvm/IR/DerivedTypes.h"
1313 //===----------------------------------------------------------------------===//
1414
1515 #include "NVPTXLowerAggrCopies.h"
16 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
1716 #include "llvm/CodeGen/StackProtector.h"
1817 #include "llvm/IR/Constants.h"
1918 #include "llvm/IR/DataLayout.h"
4039 NVPTXLowerAggrCopies() : FunctionPass(ID) {}
4140
4241 void getAnalysisUsage(AnalysisUsage &AU) const override {
43 AU.addPreserved();
4442 AU.addPreserved();
4543 }
4644
1919 #include "NVPTXTargetTransformInfo.h"
2020 #include "llvm/Analysis/Passes.h"
2121 #include "llvm/CodeGen/AsmPrinter.h"
22 #include "llvm/CodeGen/MachineFunctionAnalysis.h"
2322 #include "llvm/CodeGen/MachineModuleInfo.h"
2423 #include "llvm/CodeGen/Passes.h"
2524 #include "llvm/CodeGen/TargetPassConfig.h"
22
33 ; STOP: -loop-reduce
44 ; STOP: Loop Strength Reduction
5 ; STOP-NEXT: Machine Function Analysis
65 ; STOP-NEXT: MIR Printing Pass
76
87 ; START: -machine-branch-prob -pre-isel-intrinsic-lowering
1616
1717 ; This must use movl of the stub, not an lea, since the function isn't being
1818 ; emitted here.
19 ; CHECK: movl L__ZNSbIcED1Ev$non_lazy_ptr-L1$pb(
19 ; CHECK: movl L__ZNSbIcED1Ev$non_lazy_ptr-L0$pb(
2020
2121
2222
449449 LLVMTargetMachine &LLVMTM = static_cast(*Target);
450450 TargetPassConfig &TPC = *LLVMTM.createPassConfig(PM);
451451 PM.add(&TPC);
452 PM.add(new MachineModuleInfo(&LLVMTM));
453 LLVMTM.addMachineFunctionAnalysis(PM, MIR.get());
452 MachineModuleInfo *MMI = new MachineModuleInfo(&LLVMTM);
453 MMI->setMachineFunctionInitializer(MIR.get());
454 PM.add(MMI);
454455 TPC.printAndVerify("");
455456
456457 for (const std::string &RunPassName : *RunPassNames) {
6868 if (!F)
6969 return nullptr;
7070
71 PM.add(new MachineModuleInfo(&TM));
72 const LLVMTargetMachine &LLVMTM = static_cast(TM);
73 LLVMTM.addMachineFunctionAnalysis(PM, MIR.get());
71 MachineModuleInfo *MMI = new MachineModuleInfo(&TM);
72 MMI->setMachineFunctionInitializer(MIR.get());
73 PM.add(MMI);
7474
7575 return M;
7676 }