2007-01-27 05:38:26 +08:00
|
|
|
//===-- llvm/CodeGen/MachineModuleInfo.cpp ----------------------*- C++ -*-===//
|
2006-01-04 21:36:38 +08:00
|
|
|
//
|
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
|
2006-01-04 21:36:38 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/CodeGen/MachineModuleInfo.h"
|
2017-05-31 09:10:10 +08:00
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2006-11-08 03:33:46 +08:00
|
|
|
#include "llvm/CodeGen/MachineFunction.h"
|
2008-09-23 06:21:38 +08:00
|
|
|
#include "llvm/CodeGen/Passes.h"
|
2022-02-23 17:20:48 +08:00
|
|
|
#include "llvm/IR/Constants.h"
|
2021-02-24 01:47:15 +08:00
|
|
|
#include "llvm/IR/DiagnosticInfo.h"
|
|
|
|
#include "llvm/IR/LLVMContext.h"
|
2013-01-02 19:36:10 +08:00
|
|
|
#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"
|
2017-05-31 09:10:10 +08:00
|
|
|
#include "llvm/MC/MCContext.h"
|
|
|
|
#include "llvm/Pass.h"
|
2022-04-18 00:55:24 +08:00
|
|
|
#include "llvm/Support/CommandLine.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2018-03-24 07:58:19 +08:00
|
|
|
#include "llvm/Target/TargetLoweringObjectFile.h"
|
2016-08-24 08:42:05 +08:00
|
|
|
#include "llvm/Target/TargetMachine.h"
|
2017-05-31 09:10:10 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <memory>
|
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
2006-01-04 21:36:38 +08:00
|
|
|
using namespace llvm;
|
2006-03-02 04:39:36 +08:00
|
|
|
using namespace llvm::dwarf;
|
2006-01-04 21:36:38 +08:00
|
|
|
|
2022-04-18 00:55:24 +08:00
|
|
|
static cl::opt<bool>
|
|
|
|
DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
|
|
|
|
cl::desc("Disable debug info printing"));
|
|
|
|
|
2009-09-16 06:44:26 +08:00
|
|
|
// Out of line virtual method.
|
2017-05-31 09:10:10 +08:00
|
|
|
MachineModuleInfoImpl::~MachineModuleInfoImpl() = default;
|
2009-09-16 06:44:26 +08:00
|
|
|
|
2019-10-01 01:54:50 +08:00
|
|
|
void MachineModuleInfo::initialize() {
|
2014-04-14 08:51:57 +08:00
|
|
|
ObjFileMMI = nullptr;
|
2012-12-06 01:12:22 +08:00
|
|
|
CurCallSite = 0;
|
2020-08-11 14:29:39 +08:00
|
|
|
NextFnNum = 0;
|
2022-04-18 03:45:20 +08:00
|
|
|
UsesMSVCFloatingPoint = false;
|
2022-04-18 00:55:24 +08:00
|
|
|
DbgInfoAvailable = false;
|
2012-12-06 01:12:22 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 01:54:50 +08:00
|
|
|
void MachineModuleInfo::finalize() {
|
2012-12-06 01:12:22 +08:00
|
|
|
Personalities.clear();
|
2010-10-16 16:25:21 +08:00
|
|
|
|
2012-12-13 06:59:46 +08:00
|
|
|
Context.reset();
|
2020-11-12 09:56:14 +08:00
|
|
|
// We don't clear the ExternalContext.
|
2012-12-07 06:12:44 +08:00
|
|
|
|
2013-01-05 02:04:42 +08:00
|
|
|
delete ObjFileMMI;
|
2014-04-14 08:51:57 +08:00
|
|
|
ObjFileMMI = nullptr;
|
2019-10-01 01:54:50 +08:00
|
|
|
}
|
2013-01-05 02:04:42 +08:00
|
|
|
|
2019-10-01 01:54:50 +08:00
|
|
|
MachineModuleInfo::MachineModuleInfo(MachineModuleInfo &&MMI)
|
|
|
|
: TM(std::move(MMI.TM)),
|
2021-05-06 01:03:02 +08:00
|
|
|
Context(MMI.TM.getTargetTriple(), MMI.TM.getMCAsmInfo(),
|
2021-05-24 05:15:23 +08:00
|
|
|
MMI.TM.getMCRegisterInfo(), MMI.TM.getMCSubtargetInfo(), nullptr,
|
2022-06-12 22:46:49 +08:00
|
|
|
nullptr, false),
|
2020-07-25 01:44:48 +08:00
|
|
|
MachineFunctions(std::move(MMI.MachineFunctions)) {
|
2021-05-24 05:15:23 +08:00
|
|
|
Context.setObjectFileInfo(MMI.TM.getObjFileLowering());
|
2019-10-01 01:54:50 +08:00
|
|
|
ObjFileMMI = MMI.ObjFileMMI;
|
|
|
|
CurCallSite = MMI.CurCallSite;
|
2020-11-12 09:56:14 +08:00
|
|
|
ExternalContext = MMI.ExternalContext;
|
2019-10-01 01:54:50 +08:00
|
|
|
TheModule = MMI.TheModule;
|
2006-01-27 04:21:46 +08:00
|
|
|
}
|
|
|
|
|
2019-10-01 01:54:50 +08:00
|
|
|
MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM)
|
2021-05-06 01:03:02 +08:00
|
|
|
: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
|
2021-05-24 05:15:23 +08:00
|
|
|
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
|
2022-06-12 22:46:49 +08:00
|
|
|
nullptr, nullptr, false) {
|
2021-05-24 05:15:23 +08:00
|
|
|
Context.setObjectFileInfo(TM->getObjFileLowering());
|
2019-10-01 01:54:50 +08:00
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:56:14 +08:00
|
|
|
MachineModuleInfo::MachineModuleInfo(const LLVMTargetMachine *TM,
|
|
|
|
MCContext *ExtContext)
|
2021-05-06 01:03:02 +08:00
|
|
|
: TM(*TM), Context(TM->getTargetTriple(), TM->getMCAsmInfo(),
|
2021-05-24 05:15:23 +08:00
|
|
|
TM->getMCRegisterInfo(), TM->getMCSubtargetInfo(),
|
2022-06-12 22:46:49 +08:00
|
|
|
nullptr, nullptr, false),
|
2020-11-12 09:56:14 +08:00
|
|
|
ExternalContext(ExtContext) {
|
2021-05-24 05:15:23 +08:00
|
|
|
Context.setObjectFileInfo(TM->getObjFileLowering());
|
2020-11-12 09:56:14 +08:00
|
|
|
initialize();
|
|
|
|
}
|
|
|
|
|
2019-10-01 01:54:50 +08:00
|
|
|
MachineModuleInfo::~MachineModuleInfo() { finalize(); }
|
|
|
|
|
2016-12-02 03:32:15 +08:00
|
|
|
/// \name Exception Handling
|
|
|
|
/// \{
|
2007-02-22 06:38:31 +08:00
|
|
|
|
2015-07-15 03:22:51 +08:00
|
|
|
void MachineModuleInfo::addPersonality(const Function *Personality) {
|
2021-02-15 00:36:20 +08:00
|
|
|
if (!llvm::is_contained(Personalities, Personality))
|
|
|
|
Personalities.push_back(Personality);
|
2007-02-22 06:38:31 +08:00
|
|
|
}
|
|
|
|
|
2016-12-02 03:32:15 +08:00
|
|
|
/// \}
|
2016-08-24 09:52:46 +08:00
|
|
|
|
2017-06-06 08:44:35 +08:00
|
|
|
MachineFunction *
|
|
|
|
MachineModuleInfo::getMachineFunction(const Function &F) const {
|
|
|
|
auto I = MachineFunctions.find(&F);
|
|
|
|
return I != MachineFunctions.end() ? I->second.get() : nullptr;
|
|
|
|
}
|
|
|
|
|
2020-04-04 06:55:15 +08:00
|
|
|
MachineFunction &MachineModuleInfo::getOrCreateMachineFunction(Function &F) {
|
2016-08-24 09:52:46 +08:00
|
|
|
// Shortcut for the common case where a sequence of MachineFunctionPasses
|
|
|
|
// all query for the same Function.
|
|
|
|
if (LastRequest == &F)
|
|
|
|
return *LastResult;
|
|
|
|
|
|
|
|
auto I = MachineFunctions.insert(
|
|
|
|
std::make_pair(&F, std::unique_ptr<MachineFunction>()));
|
|
|
|
MachineFunction *MF;
|
|
|
|
if (I.second) {
|
|
|
|
// No pre-existing machine function, create a new one.
|
2017-12-16 06:22:46 +08:00
|
|
|
const TargetSubtargetInfo &STI = *TM.getSubtargetImpl(F);
|
|
|
|
MF = new MachineFunction(F, TM, STI, NextFnNum++, *this);
|
2016-08-24 09:52:46 +08:00
|
|
|
// Update the set entry.
|
|
|
|
I.first->second.reset(MF);
|
|
|
|
} else {
|
|
|
|
MF = I.first->second.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
LastRequest = &F;
|
|
|
|
LastResult = MF;
|
|
|
|
return *MF;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MachineModuleInfo::deleteMachineFunctionFor(Function &F) {
|
|
|
|
MachineFunctions.erase(&F);
|
|
|
|
LastRequest = nullptr;
|
|
|
|
LastResult = nullptr;
|
|
|
|
}
|
|
|
|
|
2022-04-19 10:19:57 +08:00
|
|
|
void MachineModuleInfo::insertFunction(const Function &F,
|
|
|
|
std::unique_ptr<MachineFunction> &&MF) {
|
|
|
|
auto I = MachineFunctions.insert(std::make_pair(&F, std::move(MF)));
|
|
|
|
assert(I.second && "machine function already mapped");
|
|
|
|
(void)I;
|
|
|
|
}
|
|
|
|
|
2016-08-24 09:52:46 +08:00
|
|
|
namespace {
|
2017-05-31 09:10:10 +08:00
|
|
|
|
2016-08-24 09:52:46 +08:00
|
|
|
/// This pass frees the MachineFunction object associated with a Function.
|
|
|
|
class FreeMachineFunction : public FunctionPass {
|
|
|
|
public:
|
|
|
|
static char ID;
|
2017-05-31 09:10:10 +08:00
|
|
|
|
2016-08-24 09:52:46 +08:00
|
|
|
FreeMachineFunction() : FunctionPass(ID) {}
|
|
|
|
|
|
|
|
void getAnalysisUsage(AnalysisUsage &AU) const override {
|
2019-10-01 01:54:50 +08:00
|
|
|
AU.addRequired<MachineModuleInfoWrapperPass>();
|
|
|
|
AU.addPreserved<MachineModuleInfoWrapperPass>();
|
2016-08-24 09:52:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool runOnFunction(Function &F) override {
|
2019-10-01 01:54:50 +08:00
|
|
|
MachineModuleInfo &MMI =
|
|
|
|
getAnalysis<MachineModuleInfoWrapperPass>().getMMI();
|
2016-08-24 09:52:46 +08:00
|
|
|
MMI.deleteMachineFunctionFor(F);
|
|
|
|
return true;
|
|
|
|
}
|
2018-07-31 03:41:25 +08:00
|
|
|
|
2017-03-08 04:59:08 +08:00
|
|
|
StringRef getPassName() const override {
|
|
|
|
return "Free MachineFunction";
|
2018-07-31 03:41:25 +08:00
|
|
|
}
|
2016-08-24 09:52:46 +08:00
|
|
|
};
|
2017-05-31 09:10:10 +08:00
|
|
|
|
2016-08-24 09:52:46 +08:00
|
|
|
} // end anonymous namespace
|
|
|
|
|
2017-05-31 09:10:10 +08:00
|
|
|
char FreeMachineFunction::ID;
|
|
|
|
|
|
|
|
FunctionPass *llvm::createFreeMachineFunctionPass() {
|
2016-08-24 09:52:46 +08:00
|
|
|
return new FreeMachineFunction();
|
|
|
|
}
|
2019-10-01 01:54:50 +08:00
|
|
|
|
|
|
|
MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
|
|
|
|
const LLVMTargetMachine *TM)
|
|
|
|
: ImmutablePass(ID), MMI(TM) {
|
|
|
|
initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2020-11-12 09:56:14 +08:00
|
|
|
MachineModuleInfoWrapperPass::MachineModuleInfoWrapperPass(
|
|
|
|
const LLVMTargetMachine *TM, MCContext *ExtContext)
|
|
|
|
: ImmutablePass(ID), MMI(TM, ExtContext) {
|
|
|
|
initializeMachineModuleInfoWrapperPassPass(*PassRegistry::getPassRegistry());
|
|
|
|
}
|
|
|
|
|
2019-10-01 01:54:50 +08:00
|
|
|
// Handle the Pass registration stuff necessary to use DataLayout's.
|
|
|
|
INITIALIZE_PASS(MachineModuleInfoWrapperPass, "machinemoduleinfo",
|
|
|
|
"Machine Module Information", false, false)
|
|
|
|
char MachineModuleInfoWrapperPass::ID = 0;
|
|
|
|
|
2021-02-24 01:47:15 +08:00
|
|
|
static unsigned getLocCookie(const SMDiagnostic &SMD, const SourceMgr &SrcMgr,
|
|
|
|
std::vector<const MDNode *> &LocInfos) {
|
|
|
|
// Look up a LocInfo for the buffer this diagnostic is coming from.
|
|
|
|
unsigned BufNum = SrcMgr.FindBufferContainingLoc(SMD.getLoc());
|
|
|
|
const MDNode *LocInfo = nullptr;
|
|
|
|
if (BufNum > 0 && BufNum <= LocInfos.size())
|
|
|
|
LocInfo = LocInfos[BufNum - 1];
|
|
|
|
|
|
|
|
// If the inline asm had metadata associated with it, pull out a location
|
|
|
|
// cookie corresponding to which line the error occurred on.
|
|
|
|
unsigned LocCookie = 0;
|
|
|
|
if (LocInfo) {
|
|
|
|
unsigned ErrorLine = SMD.getLineNo() - 1;
|
|
|
|
if (ErrorLine >= LocInfo->getNumOperands())
|
|
|
|
ErrorLine = 0;
|
|
|
|
|
|
|
|
if (LocInfo->getNumOperands() != 0)
|
|
|
|
if (const ConstantInt *CI =
|
|
|
|
mdconst::dyn_extract<ConstantInt>(LocInfo->getOperand(ErrorLine)))
|
|
|
|
LocCookie = CI->getZExtValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
return LocCookie;
|
|
|
|
}
|
|
|
|
|
2019-10-01 01:54:50 +08:00
|
|
|
bool MachineModuleInfoWrapperPass::doInitialization(Module &M) {
|
|
|
|
MMI.initialize();
|
|
|
|
MMI.TheModule = &M;
|
2021-02-24 01:47:15 +08:00
|
|
|
// FIXME: Do this for new pass manager.
|
|
|
|
LLVMContext &Ctx = M.getContext();
|
|
|
|
MMI.getContext().setDiagnosticHandler(
|
2022-01-29 03:32:42 +08:00
|
|
|
[&Ctx, &M](const SMDiagnostic &SMD, bool IsInlineAsm,
|
|
|
|
const SourceMgr &SrcMgr,
|
|
|
|
std::vector<const MDNode *> &LocInfos) {
|
2021-02-24 01:47:15 +08:00
|
|
|
unsigned LocCookie = 0;
|
|
|
|
if (IsInlineAsm)
|
|
|
|
LocCookie = getLocCookie(SMD, SrcMgr, LocInfos);
|
2022-01-29 03:32:42 +08:00
|
|
|
Ctx.diagnose(
|
|
|
|
DiagnosticInfoSrcMgr(SMD, M.getName(), IsInlineAsm, LocCookie));
|
2021-02-24 01:47:15 +08:00
|
|
|
});
|
2022-04-18 00:55:24 +08:00
|
|
|
MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
|
|
|
|
!M.debug_compile_units().empty();
|
2019-10-01 01:54:50 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MachineModuleInfoWrapperPass::doFinalization(Module &M) {
|
|
|
|
MMI.finalize();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnalysisKey MachineModuleAnalysis::Key;
|
|
|
|
|
|
|
|
MachineModuleInfo MachineModuleAnalysis::run(Module &M,
|
|
|
|
ModuleAnalysisManager &) {
|
|
|
|
MachineModuleInfo MMI(TM);
|
|
|
|
MMI.TheModule = &M;
|
2022-04-18 00:55:24 +08:00
|
|
|
MMI.DbgInfoAvailable = !DisableDebugInfoPrinting &&
|
|
|
|
!M.debug_compile_units().empty();
|
2019-10-01 01:54:50 +08:00
|
|
|
return MMI;
|
|
|
|
}
|